X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2FTWidget.java;h=f7a83a193620fa1350bc3905291cd2c2c5815523;hb=00691e80f2f135f92be739e2b7e86775a2357276;hp=4a4ba2ce18190f361ba1830d4983b66a62565ec3;hpb=a69ed767c9c07cf35cf1c5f7821fc009cfe79cd2;p=nikiroo-utils.git diff --git a/src/jexer/TWidget.java b/src/jexer/TWidget.java index 4a4ba2c..f7a83a1 100644 --- a/src/jexer/TWidget.java +++ b/src/jexer/TWidget.java @@ -331,7 +331,7 @@ public abstract class TWidget implements Comparable { } // If I have any buttons on me AND this is an Alt-key that matches - // its mnemonic, send it an Enter keystroke + // its mnemonic, send it an Enter keystroke. for (TWidget widget: children) { if (widget instanceof TButton) { TButton button = (TButton) widget; @@ -349,6 +349,81 @@ public abstract class TWidget implements Comparable { } } + // If I have any labels on me AND this is an Alt-key that matches + // its mnemonic, call its action. + for (TWidget widget: children) { + if (widget instanceof TLabel) { + TLabel label = (TLabel) widget; + if (!keypress.getKey().isFnKey() + && keypress.getKey().isAlt() + && !keypress.getKey().isCtrl() + && (Character.toLowerCase(label.getMnemonic().getShortcut()) + == Character.toLowerCase(keypress.getKey().getChar())) + ) { + + label.dispatch(); + return; + } + } + } + + // If I have any radiobuttons on me AND this is an Alt-key that + // matches its mnemonic, select it and send a Space to it. + for (TWidget widget: children) { + if (widget instanceof TRadioButton) { + TRadioButton button = (TRadioButton) widget; + if (button.isEnabled() + && !keypress.getKey().isFnKey() + && keypress.getKey().isAlt() + && !keypress.getKey().isCtrl() + && (Character.toLowerCase(button.getMnemonic().getShortcut()) + == Character.toLowerCase(keypress.getKey().getChar())) + ) { + activate(widget); + widget.onKeypress(new TKeypressEvent(kbSpace)); + return; + } + } + if (widget instanceof TRadioGroup) { + for (TWidget child: widget.getChildren()) { + if (child instanceof TRadioButton) { + TRadioButton button = (TRadioButton) child; + if (button.isEnabled() + && !keypress.getKey().isFnKey() + && keypress.getKey().isAlt() + && !keypress.getKey().isCtrl() + && (Character.toLowerCase(button.getMnemonic().getShortcut()) + == Character.toLowerCase(keypress.getKey().getChar())) + ) { + activate(widget); + widget.activate(child); + child.onKeypress(new TKeypressEvent(kbSpace)); + return; + } + } + } + } + } + + // If I have any checkboxes on me AND this is an Alt-key that matches + // its mnemonic, select it and set it to checked. + for (TWidget widget: children) { + if (widget instanceof TCheckBox) { + TCheckBox checkBox = (TCheckBox) widget; + if (checkBox.isEnabled() + && !keypress.getKey().isFnKey() + && keypress.getKey().isAlt() + && !keypress.getKey().isCtrl() + && (Character.toLowerCase(checkBox.getMnemonic().getShortcut()) + == Character.toLowerCase(keypress.getKey().getChar())) + ) { + activate(checkBox); + checkBox.setChecked(true); + return; + } + } + } + // Dispatch the keypress to an active widget for (TWidget widget: children) { if (widget.active) { @@ -1085,6 +1160,16 @@ public abstract class TWidget implements Comparable { } } + /** + * Reset the tab order of children to match their position in the list. + * Available so that subclasses can re-order their widgets if needed. + */ + protected void resetTabOrder() { + for (int i = 0; i < children.size(); i++) { + children.get(i).tabOrder = i; + } + } + /** * Switch the active child. * @@ -1098,12 +1183,19 @@ public abstract class TWidget implements Comparable { return; } - if (child != activeChild) { - if (activeChild != null) { - activeChild.active = false; + if (children.size() == 1) { + if (children.get(0).enabled == true) { + child.active = true; + activeChild = child; + } + } else { + if (child != activeChild) { + if (activeChild != null) { + activeChild.active = false; + } + child.active = true; + activeChild = child; } - child.active = true; - activeChild = child; } } @@ -1114,9 +1206,14 @@ public abstract class TWidget implements Comparable { * isn't enabled, then the next enabled child will be activated. */ public final void activate(final int tabOrder) { - if (activeChild == null) { + if (children.size() == 1) { + if (children.get(0).enabled == true) { + children.get(0).active = true; + activeChild = children.get(0); + } return; } + TWidget child = null; for (TWidget widget: children) { if ((widget.enabled) @@ -1129,7 +1226,9 @@ public abstract class TWidget implements Comparable { } } if ((child != null) && (child != activeChild)) { - activeChild.active = false; + if (activeChild != null) { + activeChild.active = false; + } assert (child.enabled); child.active = true; activeChild = child; @@ -1144,12 +1243,29 @@ public abstract class TWidget implements Comparable { */ public final void switchWidget(final boolean forward) { - // Only switch if there are multiple enabled widgets - if ((children.size() < 2) || (activeChild == null)) { + // No children: do nothing. + if (children.size() == 0) { return; } - int tabOrder = activeChild.tabOrder; + // If there is only one child, make it active if it is enabled. + if (children.size() == 1) { + if (children.get(0).enabled == true) { + activeChild = children.get(0); + activeChild.active = true; + } else { + children.get(0).active = false; + activeChild = null; + } + return; + } + + // Two or more children: go forward or backward to the next enabled + // child. + int tabOrder = 0; + if (activeChild != null) { + tabOrder = activeChild.tabOrder; + } do { if (forward) { tabOrder++; @@ -1174,7 +1290,12 @@ public abstract class TWidget implements Comparable { tabOrder = 0; } - if (activeChild.tabOrder == tabOrder) { + if (activeChild == null) { + if (tabOrder == 0) { + // We wrapped around + break; + } + } else if (activeChild.tabOrder == tabOrder) { // We wrapped around break; } @@ -1182,11 +1303,15 @@ public abstract class TWidget implements Comparable { && !(children.get(tabOrder) instanceof THScroller) && !(children.get(tabOrder) instanceof TVScroller)); - assert (children.get(tabOrder).enabled); + if (activeChild != null) { + assert (children.get(tabOrder).enabled); - activeChild.active = false; - children.get(tabOrder).active = true; - activeChild = children.get(tabOrder); + activeChild.active = false; + } + if (children.get(tabOrder).enabled == true) { + children.get(tabOrder).active = true; + activeChild = children.get(tabOrder); + } } /** @@ -1424,6 +1549,21 @@ public abstract class TWidget implements Comparable { return addLabel(text, x, y, "tlabel"); } + /** + * Convenience function to add a label to this container/window. + * + * @param text label + * @param x column relative to parent + * @param y row relative to parent + * @param action to call when shortcut is pressed + * @return the new label + */ + public final TLabel addLabel(final String text, final int x, final int y, + final TAction action) { + + return addLabel(text, x, y, "tlabel", action); + } + /** * Convenience function to add a label to this container/window. * @@ -1440,6 +1580,23 @@ public abstract class TWidget implements Comparable { return new TLabel(this, text, x, y, colorKey); } + /** + * Convenience function to add a label to this container/window. + * + * @param text label + * @param x column relative to parent + * @param y row relative to parent + * @param colorKey ColorTheme key color to use for foreground text. + * Default is "tlabel" + * @param action to call when shortcut is pressed + * @return the new label + */ + public final TLabel addLabel(final String text, final int x, final int y, + final String colorKey, final TAction action) { + + return new TLabel(this, text, x, y, colorKey, action); + } + /** * Convenience function to add a label to this container/window. * @@ -1457,6 +1614,26 @@ public abstract class TWidget implements Comparable { return new TLabel(this, text, x, y, colorKey, useWindowBackground); } + /** + * Convenience function to add a label to this container/window. + * + * @param text label + * @param x column relative to parent + * @param y row relative to parent + * @param colorKey ColorTheme key color to use for foreground text. + * Default is "tlabel" + * @param useWindowBackground if true, use the window's background color + * @param action to call when shortcut is pressed + * @return the new label + */ + public final TLabel addLabel(final String text, final int x, final int y, + final String colorKey, final boolean useWindowBackground, + final TAction action) { + + return new TLabel(this, text, x, y, colorKey, useWindowBackground, + action); + } + /** * Convenience function to add a button to this container/window. *