*/
package jexer;
+import static jexer.TKeypress.kbEnter;
+import static jexer.TKeypress.kbEsc;
import static jexer.TKeypress.kbSpace;
import jexer.bits.CellAttributes;
import jexer.bits.GraphicsChars;
+import jexer.bits.MnemonicString;
+import jexer.bits.StringUtils;
import jexer.event.TKeypressEvent;
import jexer.event.TMouseEvent;
private boolean checked = false;
/**
- * Label for this checkbox.
+ * The shortcut and checkbox label.
*/
- private String label;
+ private MnemonicString mnemonic;
/**
* If true, use the window's background color.
final String label, final boolean checked) {
// Set parent and window
- super(parent, x, y, label.length() + 4, 1);
+ super(parent, x, y, StringUtils.width(label) + 4, 1);
- this.label = label;
+ mnemonic = new MnemonicString(label);
this.checked = checked;
setCursorVisible(true);
*/
@Override
public void onKeypress(final TKeypressEvent keypress) {
- if (keypress.equals(kbSpace)) {
+ if (keypress.equals(kbSpace)
+ || keypress.equals(kbEnter)
+ ) {
checked = !checked;
return;
}
+ if (keypress.equals(kbEsc)) {
+ checked = false;
+ return;
+ }
+
// Pass to parent for the things we don't care about.
super.onKeypress(keypress);
}
@Override
public void draw() {
CellAttributes checkboxColor;
+ CellAttributes mnemonicColor;
if (isAbsoluteActive()) {
checkboxColor = getTheme().getColor("tcheckbox.active");
+ mnemonicColor = getTheme().getColor("tcheckbox.mnemonic.highlighted");
} else {
checkboxColor = getTheme().getColor("tcheckbox.inactive");
+ mnemonicColor = getTheme().getColor("tcheckbox.mnemonic");
}
if (useWindowBackground) {
CellAttributes background = getWindow().getBackground();
putCharXY(1, 0, ' ', checkboxColor);
}
putCharXY(2, 0, ']', checkboxColor);
- putStringXY(4, 0, label, checkboxColor);
+ putStringXY(4, 0, mnemonic.getRawLabel(), checkboxColor);
+ if (mnemonic.getScreenShortcutIdx() >= 0) {
+ putCharXY(4 + mnemonic.getScreenShortcutIdx(), 0,
+ mnemonic.getShortcut(), mnemonicColor);
+ }
}
// ------------------------------------------------------------------------
this.checked = checked;
}
+ /**
+ * Get the mnemonic string for this checkbox.
+ *
+ * @return mnemonic string
+ */
+ public MnemonicString getMnemonic() {
+ return mnemonic;
+ }
+
}