Merge branch 'subtree'
[fanfix.git] / src / jexer / TCheckBox.java
index 2b35843d92632d1c8ba3473ddcbbc31aa67ae3dc..1f9a351c0c0c636c538272d372c5a3b4ef43f583 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2017 Kevin Lamonte
+ * Copyright (C) 2019 Kevin Lamonte
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  */
 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;
 
@@ -44,14 +48,19 @@ public class TCheckBox extends TWidget {
     // ------------------------------------------------------------------------
 
     /**
-     * Checkbox state, true means checked.
+     * CheckBox state, true means checked.
      */
     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.
+     */
+    private boolean useWindowBackground = false;
 
     // ------------------------------------------------------------------------
     // Constructors -----------------------------------------------------------
@@ -70,9 +79,9 @@ public class TCheckBox extends TWidget {
         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);
@@ -89,7 +98,7 @@ public class TCheckBox extends TWidget {
      * @param mouse mouse event
      * @return true if the mouse is currently on the checkbox
      */
-    private boolean mouseOnCheckbox(final TMouseEvent mouse) {
+    private boolean mouseOnCheckBox(final TMouseEvent mouse) {
         if ((mouse.getY() == 0)
             && (mouse.getX() >= 0)
             && (mouse.getX() <= 2)
@@ -106,7 +115,7 @@ public class TCheckBox extends TWidget {
      */
     @Override
     public void onMouseDown(final TMouseEvent mouse) {
-        if ((mouseOnCheckbox(mouse)) && (mouse.isMouse1())) {
+        if ((mouseOnCheckBox(mouse)) && (mouse.isMouse1())) {
             // Switch state
             checked = !checked;
         }
@@ -119,11 +128,18 @@ public class TCheckBox extends TWidget {
      */
     @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);
     }
@@ -138,21 +154,32 @@ public class TCheckBox extends TWidget {
     @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();
+            checkboxColor.setBackColor(background.getBackColor());
         }
 
-        getScreen().putCharXY(0, 0, '[', checkboxColor);
+        putCharXY(0, 0, '[', checkboxColor);
         if (checked) {
-            getScreen().putCharXY(1, 0, GraphicsChars.CHECK, checkboxColor);
+            putCharXY(1, 0, GraphicsChars.CHECK, checkboxColor);
         } else {
-            getScreen().putCharXY(1, 0, ' ', checkboxColor);
+            putCharXY(1, 0, ' ', checkboxColor);
+        }
+        putCharXY(2, 0, ']', checkboxColor);
+        putStringXY(4, 0, mnemonic.getRawLabel(), checkboxColor);
+        if (mnemonic.getScreenShortcutIdx() >= 0) {
+            putCharXY(4 + mnemonic.getScreenShortcutIdx(), 0,
+                mnemonic.getShortcut(), mnemonicColor);
         }
-        getScreen().putCharXY(2, 0, ']', checkboxColor);
-        getScreen().putStringXY(4, 0, label, checkboxColor);
     }
 
     // ------------------------------------------------------------------------
@@ -177,4 +204,13 @@ public class TCheckBox extends TWidget {
         this.checked = checked;
     }
 
+    /**
+     * Get the mnemonic string for this checkbox.
+     *
+     * @return mnemonic string
+     */
+    public MnemonicString getMnemonic() {
+        return mnemonic;
+    }
+
 }