0.0.4: NEW STUFF
-- Making TMenu keyboard accelerators active/inactive
- TStatusBar
- TEditor
- TWindow
*/
private Map<TKeypress, TMenuItem> accelerators;
+ /**
+ * All menu items.
+ */
+ private List<TMenuItem> menuItems;
+
/**
* Windows and widgets pull colors from this ColorTheme.
*/
subMenus = new LinkedList<TMenu>();
timers = new LinkedList<TTimer>();
accelerators = new HashMap<TKeypress, TMenuItem>();
+ menuItems = new ArrayList<TMenuItem>();
// Setup the main consumer thread
primaryEventHandler = new WidgetEventHandler(this, true);
synchronized (windows) {
int z = window.getZ();
window.setZ(-1);
+ window.onUnfocus();
Collections.sort(windows);
windows.remove(0);
TWindow activeWindow = null;
w.setZ(w.getZ() - 1);
if (w.getZ() == 0) {
w.setActive(true);
+ w.onFocus();
assert (activeWindow == null);
activeWindow = w;
} else {
- w.setActive(false);
+ if (w.isActive()) {
+ w.setActive(false);
+ w.onUnfocus();
+ }
}
}
}
}
windows.get(activeWindowI).setActive(false);
windows.get(activeWindowI).setZ(windows.get(nextWindowI).getZ());
+ windows.get(activeWindowI).onUnfocus();
windows.get(nextWindowI).setZ(0);
windows.get(nextWindowI).setActive(true);
+ windows.get(nextWindowI).onFocus();
} // synchronized (windows)
assert (window.isModal());
}
for (TWindow w: windows) {
- w.setActive(false);
+ if (w.isActive()) {
+ w.setActive(false);
+ w.onUnfocus();
+ }
w.setZ(w.getZ() + 1);
}
windows.add(window);
- window.setActive(true);
window.setZ(0);
+ window.setActive(true);
+ window.onFocus();
}
}
// We will be switching to another window
assert (windows.get(0).isActive());
assert (!window.isActive());
+ windows.get(0).onUnfocus();
windows.get(0).setActive(false);
windows.get(0).setZ(window.getZ());
window.setZ(0);
window.setActive(true);
+ window.onFocus();
return;
}
}
}
/**
- * Add a keyboard accelerator to the global hash.
+ * Add a menu item to the global list. If it has a keyboard accelerator,
+ * that will be added the global hash.
*
- * @param item menu item this accelerator relates to
- * @param keypress keypress that will dispatch a TMenuEvent
+ * @param item the menu item
*/
- public final void addAccelerator(final TMenuItem item,
- final TKeypress keypress) {
+ public final void addMenuItem(final TMenuItem item) {
+ menuItems.add(item);
+
+ TKeypress key = item.getKey();
+ if (key != null) {
+ synchronized (accelerators) {
+ assert (accelerators.get(key) == null);
+ accelerators.put(key.toLowerCase(), item);
+ }
+ }
+ }
+
+ /**
+ * Disable one menu item.
+ *
+ * @param id the menu item ID
+ */
+ public final void disableMenuItem(final int id) {
+ for (TMenuItem item: menuItems) {
+ if (item.getId() == id) {
+ item.setEnabled(false);
+ }
+ }
+ }
- synchronized (accelerators) {
- assert (accelerators.get(keypress) == null);
- accelerators.put(keypress, item);
+ /**
+ * Disable the range of menu items with ID's between lower and upper,
+ * inclusive.
+ *
+ * @param lower the lowest menu item ID
+ * @param upper the highest menu item ID
+ */
+ public final void disableMenuItems(final int lower, final int upper) {
+ for (TMenuItem item: menuItems) {
+ if ((item.getId() >= lower) && (item.getId() <= upper)) {
+ item.setEnabled(false);
+ }
+ }
+ }
+
+ /**
+ * Enable one menu item.
+ *
+ * @param id the menu item ID
+ */
+ public final void enableMenuItem(final int id) {
+ for (TMenuItem item: menuItems) {
+ if (item.getId() == id) {
+ item.setEnabled(true);
+ }
+ }
+ }
+
+ /**
+ * Enable the range of menu items with ID's between lower and upper,
+ * inclusive.
+ *
+ * @param lower the lowest menu item ID
+ * @param upper the highest menu item ID
+ */
+ public final void enableMenuItems(final int lower, final int upper) {
+ for (TMenuItem item: menuItems) {
+ if ((item.getId() >= lower) && (item.getId() <= upper)) {
+ item.setEnabled(true);
+ }
}
}
// Default: do nothing
}
+ /**
+ * Called by application.switchWindow() when this window gets the
+ * focus, and also by application.addWindow().
+ */
+ public void onFocus() {
+ // Default: do nothing
+ }
+
+ /**
+ * Called by application.switchWindow() when another window gets the
+ * focus.
+ */
+ public void onUnfocus() {
+ // Default: do nothing
+ }
+
/**
* Called by TApplication.drawChildren() to render on screen.
*/
import jexer.*;
import jexer.event.*;
+import jexer.menu.*;
/**
* This window demonstates the TText, THScroller, and TVScroller widgets.
super(parent, title, 0, 0, 44, 20, RESIZABLE);
textField = addText(text, 1, 1, 40, 16);
}
-
+
/**
* Public constructor.
*
this(parent, "Text Area",
"This is an example of a reflowable text field. Some example text follows.\n" +
"\n" +
+"Notice that some menu items should be disabled when this window has focus.\n" +
+"\n" +
"This library implements a text-based windowing system loosely\n" +
"reminiscient of Borland's [Turbo\n" +
"Vision](http://en.wikipedia.org/wiki/Turbo_Vision) library. For those\n" +
"Sigala's updated version](http://tvision.sourceforge.net/) that runs\n" +
"on many more platforms.\n" +
"\n" +
-"Currently the only console platform supported is Posix (tested on\n" +
-"Linux). Input/output is handled through terminal escape sequences\n" +
-"generated by the library itself: ncurses is not required or linked to. \n" +
-"xterm mouse tracking using UTF8 coordinates is supported.\n" +
-"\n" +
"This library is licensed LGPL (\"GNU Lesser General Public License\")\n" +
"version 3 or greater. See the file COPYING for the full license text,\n" +
"which includes both the GPL v3 and the LGPL supplemental terms.\n" +
widget.onResize(event);
}
}
-}
+ /**
+ * Play with menu items.
+ */
+ public void onFocus() {
+ getApplication().enableMenuItem(2001);
+ getApplication().disableMenuItem(TMenu.MID_SHELL);
+ getApplication().disableMenuItem(TMenu.MID_EXIT);
+ }
+
+ /**
+ * Called by application.switchWindow() when another window gets the
+ * focus.
+ */
+ public void onUnfocus() {
+ getApplication().disableMenuItem(2001);
+ getApplication().enableMenuItem(TMenu.MID_SHELL);
+ getApplication().enableMenuItem(TMenu.MID_EXIT);
+ }
+
+}
}
/**
- * Convenience function to add a custom menu item.
+ * Convenience function to add a menu item.
*
* @param id menu item ID. Must be greater than 1024.
* @param label menu item label
- * @param key global keyboard accelerator
* @return the new menu item
*/
- public final TMenuItem addItem(final int id, final String label,
- final TKeypress key) {
-
+ public final TMenuItem addItem(final int id, final String label) {
assert (id >= 1024);
- return addItemInternal(id, label, key);
+ return addItemInternal(id, label, null);
}
/**
* @param key global keyboard accelerator
* @return the new menu item
*/
- private TMenuItem addItemInternal(final int id, final String label,
+ public final TMenuItem addItem(final int id, final String label,
final TKeypress key) {
- int newY = getChildren().size() + 1;
- assert (newY < getHeight());
-
- TMenuItem menuItem = new TMenuItem(this, id, 1, newY, label);
- menuItem.setKey(key);
- setHeight(getHeight() + 1);
- if (menuItem.getWidth() + 2 > getWidth()) {
- setWidth(menuItem.getWidth() + 2);
- }
- for (TWidget widget: getChildren()) {
- widget.setWidth(getWidth() - 2);
- }
- getApplication().addAccelerator(menuItem, key.toLowerCase());
- getApplication().recomputeMenuX();
- activate(0);
- return menuItem;
+ assert (id >= 1024);
+ return addItemInternal(id, label, key);
}
/**
- * Convenience function to add a menu item.
+ * Convenience function to add a custom menu item.
*
* @param id menu item ID. Must be greater than 1024.
* @param label menu item label
+ * @param key global keyboard accelerator
* @return the new menu item
*/
- public final TMenuItem addItem(final int id, final String label) {
- assert (id >= 1024);
- return addItemInternal(id, label);
- }
+ private TMenuItem addItemInternal(final int id, final String label,
+ final TKeypress key) {
- /**
- * Convenience function to add a menu item.
- *
- * @param id menu item ID
- * @param label menu item label
- * @return the new menu item
- */
- private TMenuItem addItemInternal(final int id, final String label) {
int newY = getChildren().size() + 1;
assert (newY < getHeight());
TMenuItem menuItem = new TMenuItem(this, id, 1, newY, label);
+ menuItem.setKey(key);
setHeight(getHeight() + 1);
if (menuItem.getWidth() + 2 > getWidth()) {
setWidth(menuItem.getWidth() + 2);
for (TWidget widget: getChildren()) {
widget.setWidth(getWidth() - 2);
}
+ getApplication().addMenuItem(menuItem);
getApplication().recomputeMenuX();
activate(0);
return menuItem;
String label;
TKeypress key = null;
- boolean hasKey = true;
switch (id) {
case MID_SHELL:
label = "O&S Shell";
- hasKey = false;
break;
case MID_OPEN_FILE:
break;
case MID_CLEAR:
label = "C&lear";
- hasKey = false;
// key = kbDel;
break;
case MID_TILE:
label = "&Tile";
- hasKey = false;
break;
case MID_CASCADE:
label = "C&ascade";
- hasKey = false;
break;
case MID_CLOSE_ALL:
label = "Cl&ose All";
- hasKey = false;
break;
case MID_WINDOW_MOVE:
label = "&Size/Move";
break;
case MID_WINDOW_CLOSE:
label = "&Close";
- hasKey = false;
// key = kbCtrlW;
break;
throw new IllegalArgumentException("Invalid menu ID: " + id);
}
- if (hasKey) {
- return addItemInternal(id, label, key);
- }
- return addItemInternal(id, label);
+ return addItemInternal(id, label, key);
}
/**
*/
private int id = TMenu.MID_UNUSED;
+ /**
+ * Get the menu item ID.
+ *
+ * @return the id
+ */
+ public final int getId() {
+ return id;
+ }
+
/**
* When true, this item can be checked or unchecked.
*/
public final void setCheckable(final boolean checkable) {
this.checkable = checkable;
}
-
+
/**
* When true, this item is checked.
*/
*/
private TKeypress key;
- /**
- * When true, a global accelerator can be used to select this item.
- */
- private boolean hasKey = false;
-
/**
* The title string. Use '&' to specify a mnemonic, i.e. "&File" will
* highlight the 'F' and allow 'f' or 'F' to select it.
return mnemonic;
}
+ /**
+ * Get a global accelerator key for this menu item.
+ *
+ * @return global keyboard accelerator, or null if no key is associated
+ * with this item
+ */
+ public final TKeypress getKey() {
+ return key;
+ }
+
/**
* Set a global accelerator key for this menu item.
*
* @param key global keyboard accelerator
*/
public final void setKey(final TKeypress key) {
- hasKey = true;
this.key = key;
- int newWidth = (label.length() + 4 + key.toString().length() + 2);
- if (newWidth > getWidth()) {
- setWidth(newWidth);
+ if (key != null) {
+ int newWidth = (label.length() + 4 + key.toString().length() + 2);
+ if (newWidth > getWidth()) {
+ setWidth(newWidth);
+ }
}
}
getScreen().hLineXY(1, 0, getWidth() - 2, ' ', menuColor);
getScreen().putStringXY(2, 0, mnemonic.getRawLabel(), menuColor);
- if (hasKey) {
+ if (key != null) {
String keyLabel = key.toString();
getScreen().putStringXY((getWidth() - keyLabel.length() - 2), 0,
keyLabel, menuColor);