some minor EditableWidget enhancements and code cosmetics

This commit is contained in:
thrust26 2020-11-05 11:22:52 +01:00
parent 5fd48d8a99
commit f550d727ce
3 changed files with 56 additions and 34 deletions

View File

@ -12,6 +12,13 @@
Release History Release History
=========================================================================== ===========================================================================
6.4 to 6.5 (December XX, 2020)
* Enhanced cut/copy/paste to allow selecting text (TODO: PromptWidget, doc)
-Have fun!
6.3 to 6.4 (November 2, 2020) 6.3 to 6.4 (November 2, 2020)
* Added basic (entire and single line only) text cut/copy and paste. * Added basic (entire and single line only) text cut/copy and paste.
@ -42,8 +49,6 @@
* Fixed bug in ROM launcher, with last ROM selected not being remembered * Fixed bug in ROM launcher, with last ROM selected not being remembered
when exiting and re-entering a directory. when exiting and re-entering a directory.
-Have fun!
6.2.1 to 6.3 (October 7, 2020) 6.2.1 to 6.3 (October 7, 2020)

View File

@ -23,10 +23,6 @@
#include "EventHandler.hxx" #include "EventHandler.hxx"
#include "EditableWidget.hxx" #include "EditableWidget.hxx"
// 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, EditableWidget::EditableWidget(GuiObject* boss, const GUI::Font& font,
int x, int y, int w, int h, const string& str) int x, int y, int w, int h, const string& str)
@ -148,7 +144,7 @@ bool EditableWidget::handleControlKeys(StellaKey key, StellaMod mod)
case KBDK_C: case KBDK_C:
case KBDK_INSERT: case KBDK_INSERT:
copySelectedText(); handled = copySelectedText();
break; break;
case KBDK_E: case KBDK_E:
@ -175,8 +171,9 @@ bool EditableWidget::handleControlKeys(StellaKey key, StellaMod mod)
break; break;
case KBDK_V: case KBDK_V:
pasteSelectedText(); handled = pasteSelectedText();
sendCommand(EditableWidget::kChangedCmd, key, _id); if(handled)
sendCommand(EditableWidget::kChangedCmd, key, _id);
break; break;
case KBDK_W: case KBDK_W:
@ -185,8 +182,9 @@ bool EditableWidget::handleControlKeys(StellaKey key, StellaMod mod)
break; break;
case KBDK_X: case KBDK_X:
cutSelectedText(); handled = cutSelectedText();
sendCommand(EditableWidget::kChangedCmd, key, _id); if(handled)
sendCommand(EditableWidget::kChangedCmd, key, _id);
break; break;
case KBDK_LEFT: case KBDK_LEFT:
@ -221,13 +219,15 @@ bool EditableWidget::handleShiftKeys(StellaKey key)
{ {
case KBDK_DELETE: case KBDK_DELETE:
case KBDK_KP_PERIOD: case KBDK_KP_PERIOD:
cutSelectedText(); handled = cutSelectedText();
sendCommand(EditableWidget::kChangedCmd, key, _id); if(handled)
sendCommand(EditableWidget::kChangedCmd, key, _id);
break; break;
case KBDK_INSERT: case KBDK_INSERT:
pasteSelectedText(); handled = pasteSelectedText();
sendCommand(EditableWidget::kChangedCmd, key, _id); if(handled)
sendCommand(EditableWidget::kChangedCmd, key, _id);
break; break;
case KBDK_LEFT: case KBDK_LEFT:
@ -662,31 +662,48 @@ bool EditableWidget::killSelectedText()
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EditableWidget::cutSelectedText() bool EditableWidget::cutSelectedText()
{ {
#if defined(PSEUDO_CUT_COPY_PASTE) string selected = selectString();
instance().eventHandler().copyText(selectString());
killSelectedText(); // only cut and copy if anything is selected, else keep old cut text
#endif if(!selected.empty())
{
instance().eventHandler().copyText(selected);
killSelectedText();
return true;
}
return false;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EditableWidget::copySelectedText() bool EditableWidget::copySelectedText()
{ {
#if defined(PSEUDO_CUT_COPY_PASTE) string selected = selectString();
instance().eventHandler().copyText(selectString());
#endif // only copy if anything is selected, else keep old copied text
if(!selected.empty())
{
instance().eventHandler().copyText(selected);
return true;
}
return false;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EditableWidget::pasteSelectedText() bool EditableWidget::pasteSelectedText()
{ {
#if defined(PSEUDO_CUT_COPY_PASTE) bool selected = !selectString().empty();
string text; string pasted;
instance().eventHandler().pasteText(text); // retrieve the pasted text
instance().eventHandler().pasteText(pasted);
// remove the currently selected text
killSelectedText(); killSelectedText();
_editString.insert(_caretPos, text); // insert paste text instead
_caretPos += int(text.length()); _editString.insert(_caretPos, pasted);
#endif // position cursor at the end of pasted text
_caretPos += int(pasted.length());
return selected || !pasted.empty();
} }

View File

@ -98,9 +98,9 @@ class EditableWidget : public Widget, public CommandSender
int selectStartPos(); int selectStartPos();
int selectEndPos(); int selectEndPos();
// Clipboard // Clipboard
void cutSelectedText(); bool cutSelectedText();
void copySelectedText(); bool copySelectedText();
void pasteSelectedText(); bool pasteSelectedText();
// Use the current TextFilter to insert a character into the // Use the current TextFilter to insert a character into the
// internal buffer // internal buffer