From: Kevin Lamonte Date: Sat, 2 Nov 2019 04:46:12 +0000 (-0500) Subject: TField cut/paste working X-Git-Tag: fanfix-swing-0.0.1~12^2~13^2^2~34 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=51e46b3e9d457882e5dad35b79b0bd0d11654f04;p=fanfix-swing.git TField cut/paste working --- diff --git a/src/jexer/EditMenuUser.java b/src/jexer/EditMenuUser.java index b6e78450..52dc33e4 100644 --- a/src/jexer/EditMenuUser.java +++ b/src/jexer/EditMenuUser.java @@ -36,30 +36,30 @@ package jexer; public interface EditMenuUser { /** - * Check if cut to clipboard is supported. + * Check if the cut menu item should be enabled. * - * @return true if cut to clipboard is supported + * @return true if the cut menu item should be enabled */ public boolean isEditMenuCut(); /** - * Check if copy to clipboard is supported. + * Check if the copy menu item should be enabled. * - * @return true if copy to clipboard is supported + * @return true if the copy menu item should be enabled */ public boolean isEditMenuCopy(); /** - * Check if paste from clipboard is supported. + * Check if the paste menu item should be enabled. * - * @return true if paste from clipboard is supported + * @return true if the paste menu item should be enabled */ public boolean isEditMenuPaste(); /** - * Check if clear selection is supported. + * Check if the clear menu item should be enabled. * - * @return true if clear selection is supported + * @return true if the clear menu item should be enabled */ public boolean isEditMenuClear(); diff --git a/src/jexer/TApplication.java b/src/jexer/TApplication.java index a38b2dab..13cd0795 100644 --- a/src/jexer/TApplication.java +++ b/src/jexer/TApplication.java @@ -1025,6 +1025,24 @@ public class TApplication implements Runnable { new TFontChooserWindow(this); return true; } + + if (menu.getId() == TMenu.MID_CUT) { + postMenuEvent(new TCommandEvent(cmCut)); + return true; + } + if (menu.getId() == TMenu.MID_COPY) { + postMenuEvent(new TCommandEvent(cmCopy)); + return true; + } + if (menu.getId() == TMenu.MID_PASTE) { + postMenuEvent(new TCommandEvent(cmPaste)); + return true; + } + if (menu.getId() == TMenu.MID_CLEAR) { + postMenuEvent(new TCommandEvent(cmClear)); + return true; + } + return false; } @@ -1071,6 +1089,47 @@ public class TApplication implements Runnable { Thread.currentThread() + " finishEventProcessing()\n"); } + // See if we need to enable/disable the edit menu. + EditMenuUser widget = null; + if (activeMenu == null) { + if (activeWindow != null) { + if (activeWindow.getActiveChild() instanceof EditMenuUser) { + widget = (EditMenuUser) activeWindow.getActiveChild(); + } + } else if (desktop != null) { + if (desktop.getActiveChild() instanceof EditMenuUser) { + widget = (EditMenuUser) desktop.getActiveChild(); + } + } + if (widget == null) { + disableMenuItem(TMenu.MID_CUT); + disableMenuItem(TMenu.MID_COPY); + disableMenuItem(TMenu.MID_PASTE); + disableMenuItem(TMenu.MID_CLEAR); + } else { + if (widget.isEditMenuCut()) { + enableMenuItem(TMenu.MID_CUT); + } else { + disableMenuItem(TMenu.MID_CUT); + } + if (widget.isEditMenuCopy()) { + enableMenuItem(TMenu.MID_COPY); + } else { + disableMenuItem(TMenu.MID_COPY); + } + if (widget.isEditMenuPaste()) { + enableMenuItem(TMenu.MID_PASTE); + } else { + disableMenuItem(TMenu.MID_PASTE); + } + if (widget.isEditMenuClear()) { + enableMenuItem(TMenu.MID_CLEAR); + } else { + disableMenuItem(TMenu.MID_CLEAR); + } + } + } + // Process timers and call doIdle()'s doIdle(); @@ -1642,6 +1701,15 @@ public class TApplication implements Runnable { return theme; } + /** + * Get the clipboard. + * + * @return the clipboard + */ + public final Clipboard getClipboard() { + return clipboard; + } + /** * Repaint the screen on the next update. */ @@ -3301,10 +3369,10 @@ public class TApplication implements Runnable { */ public final TMenu addEditMenu() { TMenu editMenu = addMenu(i18n.getString("editMenuTitle")); - editMenu.addDefaultItem(TMenu.MID_CUT); - editMenu.addDefaultItem(TMenu.MID_COPY); - editMenu.addDefaultItem(TMenu.MID_PASTE); - editMenu.addDefaultItem(TMenu.MID_CLEAR); + editMenu.addDefaultItem(TMenu.MID_CUT, false); + editMenu.addDefaultItem(TMenu.MID_COPY, false); + editMenu.addDefaultItem(TMenu.MID_PASTE, false); + editMenu.addDefaultItem(TMenu.MID_CLEAR, false); TStatusBar statusBar = editMenu.newStatusBar(i18n. getString("editMenuStatus")); statusBar.addShortcutKeypress(kbF1, cmHelp, i18n.getString("Help")); diff --git a/src/jexer/TField.java b/src/jexer/TField.java index 7c8b5bc4..29632ca5 100644 --- a/src/jexer/TField.java +++ b/src/jexer/TField.java @@ -31,14 +31,16 @@ package jexer; import jexer.bits.CellAttributes; import jexer.bits.GraphicsChars; import jexer.bits.StringUtils; +import jexer.event.TCommandEvent; import jexer.event.TKeypressEvent; import jexer.event.TMouseEvent; +import static jexer.TCommand.*; import static jexer.TKeypress.*; /** * TField implements an editable text field. */ -public class TField extends TWidget { +public class TField extends TWidget implements EditMenuUser { // ------------------------------------------------------------------------ // Variables -------------------------------------------------------------- @@ -234,12 +236,13 @@ public class TField extends TWidget { if (keypress.equals(kbRight)) { if (position < text.length()) { + int lastPosition = position; screenPosition += StringUtils.width(text.codePointAt(position)); position += Character.charCount(text.codePointAt(position)); if (fixed == true) { if (screenPosition == getWidth()) { screenPosition--; - position -= Character.charCount(text.codePointAt(position)); + position -= Character.charCount(text.codePointAt(lastPosition)); } } else { while ((screenPosition - windowStart + @@ -373,6 +376,42 @@ public class TField extends TWidget { // Pass to parent for the things we don't care about. super.onKeypress(keypress); } + /** + * Handle posted command events. + * + * @param command command event + */ + @Override + public void onCommand(final TCommandEvent command) { + if (command.equals(cmCut)) { + // Copy text to clipboard, and then remove it. + getClipboard().copyText(text); + setText(""); + return; + } + + if (command.equals(cmCopy)) { + // Copy text to clipboard. + getClipboard().copyText(text); + return; + } + + if (command.equals(cmPaste)) { + // Paste text from clipboard. + String newText = getClipboard().pasteText(); + if (newText != null) { + setText(newText); + } + return; + } + + if (command.equals(cmClear)) { + // Remove text. + setText(""); + return; + } + + } // ------------------------------------------------------------------------ // TWidget ---------------------------------------------------------------- @@ -467,7 +506,11 @@ public class TField extends TWidget { assert (text != null); this.text = text; position = 0; + screenPosition = 0; windowStart = 0; + if ((fixed == true) && (this.text.length() > getWidth())) { + this.text = this.text.substring(0, getWidth()); + } } /** @@ -668,4 +711,44 @@ public class TField extends TWidget { updateAction = action; } + // ------------------------------------------------------------------------ + // EditMenuUser ----------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Check if the cut menu item should be enabled. + * + * @return true if the cut menu item should be enabled + */ + public boolean isEditMenuCut() { + return true; + } + + /** + * Check if the copy menu item should be enabled. + * + * @return true if the copy menu item should be enabled + */ + public boolean isEditMenuCopy() { + return true; + } + + /** + * Check if the paste menu item should be enabled. + * + * @return true if the paste menu item should be enabled + */ + public boolean isEditMenuPaste() { + return true; + } + + /** + * Check if the clear menu item should be enabled. + * + * @return true if the clear menu item should be enabled + */ + public boolean isEditMenuClear() { + return true; + } + } diff --git a/src/jexer/TTableWindow.java b/src/jexer/TTableWindow.java index 44ff7b48..766ceafd 100644 --- a/src/jexer/TTableWindow.java +++ b/src/jexer/TTableWindow.java @@ -112,7 +112,6 @@ public class TTableWindow extends TScrollableWindow { */ public void onFocus() { // Enable the table menu items. - getApplication().enableMenuItem(TMenu.MID_CUT); getApplication().enableMenuItem(TMenu.MID_TABLE_RENAME_COLUMN); getApplication().enableMenuItem(TMenu.MID_TABLE_RENAME_ROW); getApplication().enableMenuItem(TMenu.MID_TABLE_VIEW_ROW_LABELS); @@ -171,7 +170,6 @@ public class TTableWindow extends TScrollableWindow { */ public void onUnfocus() { // Disable the table menu items. - getApplication().disableMenuItem(TMenu.MID_CUT); getApplication().disableMenuItem(TMenu.MID_TABLE_RENAME_COLUMN); getApplication().disableMenuItem(TMenu.MID_TABLE_RENAME_ROW); getApplication().disableMenuItem(TMenu.MID_TABLE_VIEW_ROW_LABELS); diff --git a/src/jexer/TWidget.java b/src/jexer/TWidget.java index e94fed25..012ed756 100644 --- a/src/jexer/TWidget.java +++ b/src/jexer/TWidget.java @@ -36,6 +36,7 @@ import java.util.ArrayList; import jexer.backend.Screen; import jexer.bits.Cell; import jexer.bits.CellAttributes; +import jexer.bits.Clipboard; import jexer.bits.ColorTheme; import jexer.event.TCommandEvent; import jexer.event.TInputEvent; @@ -598,9 +599,8 @@ public abstract class TWidget implements Comparable { * @param command command event */ public void onCommand(final TCommandEvent command) { - // Default: do nothing, pass to children instead - for (TWidget widget: children) { - widget.onCommand(command); + if (activeChild != null) { + activeChild.onCommand(command); } } @@ -1133,6 +1133,18 @@ public abstract class TWidget implements Comparable { return null; } + /** + * Get the Clipboard. + * + * @return the Clipboard, or null if not assigned + */ + public Clipboard getClipboard() { + if (window != null) { + return window.getApplication().getClipboard(); + } + return null; + } + /** * Comparison operator. For various subclasses it sorts on: *