Added basic (entire and single line only) text cut/copy and paste (partially implements #105)

This commit is contained in:
thrust26 2020-10-11 10:12:42 +02:00
parent 7fbcc95c19
commit 76c526bcb0
9 changed files with 122 additions and 18 deletions

View File

@ -12,6 +12,11 @@
Release History
===========================================================================
6.3 to 6.4 (XXXX XX, 202X)
* Added basic (entire and single line only) text cut/copy and paste
6.2.1 to 6.3 (October 7, 2020)
* Added adjustable autofire.

View File

@ -1883,8 +1883,9 @@
<tr><td>Control + w</td><td>Remove entire word to left of cursor</td></tr>
<tr><td>Control + Left</td><td>Move cursor to beginning of word to the left</td></tr>
<tr><td>Control + Right</td><td>Move cursor to beginning of word to the right</td></tr>
<tr><td>Control + c</td><td>Copy entire line to clipboard (not complete)</td></tr>
<tr><td>Control + v</td><td>Paste clipboard contents (not complete)</td></tr>
<tr><td>Control + c</td><td>Copy entire line to clipboard</td></tr>
<tr><td>Control + v</td><td>Paste clipboard contents</td></tr>
<tr><td>Control + x</td><td>Cut entire line to clipboard</td></tr>
</table>
</blockquote></br>

View File

@ -58,6 +58,30 @@ void EventHandlerSDL2::enableTextEvents(bool enable)
SDL_StopTextInput();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EventHandlerSDL2::copyText(const string& text) const
{
SDL_SetClipboardText(text.c_str());
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EventHandlerSDL2::cutText(string& text) const
{
copyText(text);
text = "";
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string EventHandlerSDL2::pasteText(string& text) const
{
if(SDL_HasClipboardText())
text = SDL_GetClipboardText();
else
text = "";
return text;
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EventHandlerSDL2::pollEvent()
{

View File

@ -38,12 +38,19 @@ class EventHandlerSDL2 : public EventHandler
explicit EventHandlerSDL2(OSystem& osystem);
~EventHandlerSDL2() override;
private:
private:
/**
Enable/disable text events (distinct from single-key events).
*/
void enableTextEvents(bool enable) override;
/**
Clipboard methods.
*/
void copyText(const string& text) const override;
void cutText(string& text) const override;
string pasteText(string& text) const override;
/**
Collects and dispatches any pending SDL2 events.
*/

View File

@ -24,12 +24,17 @@
#include "Debugger.hxx"
#include "DebuggerDialog.hxx"
#include "DebuggerParser.hxx"
#include "EventHandler.hxx"
#include "PromptWidget.hxx"
#include "CartDebug.hxx"
#define PROMPT "> "
// Uncomment the following to give full-line cut/copy/paste
// Note that this will be removed eventually, when we implement proper cut/copy/paste
#define PSEUDO_CUT_COPY_PASTE
// TODO: Github issue #361
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PromptWidget::PromptWidget(GuiObject* boss, const GUI::Font& font,
@ -673,19 +678,61 @@ void PromptWidget::textSelectAll()
{
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string PromptWidget::getLine()
{
#if defined(PSEUDO_CUT_COPY_PASTE)
assert(_promptEndPos >= _promptStartPos);
int len = _promptEndPos - _promptStartPos;
string text;
// Copy current line to text
for(int i = 0; i < len; i++)
text += buffer(_promptStartPos + i) & 0x7f;
return text;
#endif
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PromptWidget::textCut()
{
#if defined(PSEUDO_CUT_COPY_PASTE)
string text = getLine();
instance().eventHandler().cutText(text);
// Remove the current line
_currentPos = _promptStartPos;
killLine(1); // to end of line
_promptEndPos = _currentPos;
#endif
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PromptWidget::textCopy()
{
#if defined(PSEUDO_CUT_COPY_PASTE)
string text = getLine();
instance().eventHandler().copyText(text);
#endif
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PromptWidget::textPaste()
{
#if defined(PSEUDO_CUT_COPY_PASTE)
string text;
// Remove the current line
_currentPos = _promptStartPos;
killLine(1); // to end of line
instance().eventHandler().pasteText(text);
print(text);
_promptEndPos = _currentPos;
#endif
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -72,6 +72,7 @@ class PromptWidget : public Widget, public CommandSender
// Clipboard
void textSelectAll();
string getLine();
void textCut();
void textCopy();
void textPaste();

View File

@ -326,6 +326,13 @@ class EventHandler
*/
virtual void enableTextEvents(bool enable) = 0;
/**
Clipboard methods.
*/
virtual void copyText(const string& text) const = 0;
virtual void cutText(string& text) const = 0;
virtual string pasteText(string& text) const = 0;
/**
Handle changing mouse modes.
*/

View File

@ -19,11 +19,13 @@
#include "StellaKeys.hxx"
#include "FBSurface.hxx"
#include "Font.hxx"
#include "OSystem.hxx"
#include "EventHandler.hxx"
#include "EditableWidget.hxx"
// Uncomment the following to give full-line copy/paste
// Note that this will be removed eventually, when we implement proper copy/paste
//#define PSEUDO_COPY_PASTE
// Uncomment the following to give full-line cut/copy/paste
// Note that this will be removed eventually, when we implement proper cut/copy/paste
#define PSEUDO_CUT_COPY_PASTE
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
EditableWidget::EditableWidget(GuiObject* boss, const GUI::Font& font,
@ -260,7 +262,7 @@ bool EditableWidget::specialKeys(StellaKey key)
{
bool handled = true;
switch (key)
switch(key)
{
case KBDK_A:
setCaretPos(0);
@ -268,7 +270,6 @@ bool EditableWidget::specialKeys(StellaKey key)
case KBDK_C:
copySelectedText();
if(handled) sendCommand(EditableWidget::kChangedCmd, key, _id);
break;
case KBDK_E:
@ -292,7 +293,7 @@ bool EditableWidget::specialKeys(StellaKey key)
case KBDK_V:
pasteSelectedText();
if(handled) sendCommand(EditableWidget::kChangedCmd, key, _id);
sendCommand(EditableWidget::kChangedCmd, key, _id);
break;
case KBDK_W:
@ -300,6 +301,11 @@ bool EditableWidget::specialKeys(StellaKey key)
if(handled) sendCommand(EditableWidget::kChangedCmd, key, _id);
break;
case KBDK_X:
cutSelectedText();
sendCommand(EditableWidget::kChangedCmd, key, _id);
break;
case KBDK_LEFT:
handled = moveWord(-1);
break;
@ -445,21 +451,28 @@ bool EditableWidget::moveWord(int direction)
return handled;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EditableWidget::cutSelectedText()
{
#if defined(PSEUDO_CUT_COPY_PASTE)
instance().eventHandler().cutText(_editString);
_caretPos = 0;
#endif
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EditableWidget::copySelectedText()
{
#if defined(PSEUDO_COPY_PASTE)
_clippedText = _editString;
#if defined(PSEUDO_CUT_COPY_PASTE)
instance().eventHandler().copyText(_editString);
#endif
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EditableWidget::pasteSelectedText()
{
#if defined(PSEUDO_COPY_PASTE)
_editString = _clippedText;
#if defined(PSEUDO_CUT_COPY_PASTE)
instance().eventHandler().pasteText(_editString);
_caretPos = int(_editString.length());
#endif
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string EditableWidget::_clippedText = "";

View File

@ -87,6 +87,7 @@ class EditableWidget : public Widget, public CommandSender
bool moveWord(int direction);
// Clipboard
void cutSelectedText();
void copySelectedText();
void pasteSelectedText();
@ -107,8 +108,6 @@ class EditableWidget : public Widget, public CommandSender
int _editScrollOffset{0};
static string _clippedText;
private:
TextFilter _filter;