Add 'src/jexer/' from commit 'cf01c92f5809a0732409e280fb0f32f27393618d'
[nikiroo-utils.git] / src / jexer / bits / MnemonicString.java
index 9f7d178693c700d8edebc803da2a677ff4b8f3e7..58575b570cf0f84330aa93859e8c36d974b798e2 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2016 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.bits;
 
 /**
- * MnemonicString is used to render a string like "&File" into a highlighted
- * 'F' and the rest of 'ile'.  To insert a literal '&', use two '&&'
- * characters, e.g. "&File && Stuff" would be "File & Stuff" with the first
- * 'F' highlighted.
+ * MnemonicString is used to render a string like "&File" into a
+ * highlighted 'F' and the rest of 'ile'.  To insert a literal '&', use
+ * two '&&' characters, e.g. "&File && Stuff" would be
+ * "File & Stuff" with the first 'F' highlighted.
  */
-public final class MnemonicString {
+public class MnemonicString {
+
+    // ------------------------------------------------------------------------
+    // Variables --------------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
      * Keyboard shortcut to activate this item.
      */
-    private char shortcut;
-
-    /**
-     * Get the keyboard shortcut character.
-     *
-     * @return the highlighted character
-     */
-    public char getShortcut() {
-        return shortcut;
-    }
+    private int shortcut;
 
     /**
      * Location of the highlighted character.
@@ -56,64 +51,104 @@ public final class MnemonicString {
     private int shortcutIdx = -1;
 
     /**
-     * Get location of the highlighted character.
-     *
-     * @return location of the highlighted character
+     * Screen location of the highlighted character (number of text cells
+     * required to display from the beginning to shortcutIdx).
      */
-    public int getShortcutIdx() {
-        return shortcutIdx;
-    }
+    private int screenShortcutIdx = -1;
 
     /**
      * The raw (uncolored) string.
      */
     private String rawLabel;
 
-    /**
-     * Get the raw (uncolored) string.
-     *
-     * @return the raw (uncolored) string
-     */
-    public String getRawLabel() {
-        return rawLabel;
-    }
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
      * Public constructor.
      *
      * @param label widget label or title.  Label must contain a keyboard
-     * shortcut, denoted by prefixing a letter with "&", e.g. "&File"
+     * shortcut, denoted by prefixing a letter with "&", e.g. "&File"
      */
     public MnemonicString(final String label) {
 
         // Setup the menu shortcut
-        String newLabel = "";
+        StringBuilder newLabel = new StringBuilder();
         boolean foundAmp = false;
         boolean foundShortcut = false;
         int scanShortcutIdx = 0;
-        for (int i = 0; i < label.length(); i++) {
-            char c = label.charAt(i);
+        int scanScreenShortcutIdx = 0;
+        for (int i = 0; i < label.length();) {
+            int c = label.codePointAt(i);
+            i += Character.charCount(c);
+
             if (c == '&') {
                 if (foundAmp) {
-                    newLabel += '&';
+                    newLabel.append('&');
                     scanShortcutIdx++;
+                    scanScreenShortcutIdx++;
                 } else {
                     foundAmp = true;
                 }
             } else {
-                newLabel += c;
+                newLabel.append(Character.toChars(c));
                 if (foundAmp) {
                     if (!foundShortcut) {
                         shortcut = c;
                         foundAmp = false;
                         foundShortcut = true;
                         shortcutIdx = scanShortcutIdx;
+                        screenShortcutIdx = scanScreenShortcutIdx;
                     }
                 } else {
                     scanShortcutIdx++;
+                    scanScreenShortcutIdx += StringUtils.width(c);
                 }
             }
         }
-        this.rawLabel = newLabel;
+        this.rawLabel = newLabel.toString();
     }
+
+    // ------------------------------------------------------------------------
+    // MnemonicString ---------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Get the keyboard shortcut character.
+     *
+     * @return the highlighted character
+     */
+    public int getShortcut() {
+        return shortcut;
+    }
+
+    /**
+     * Get location of the highlighted character.
+     *
+     * @return location of the highlighted character
+     */
+    public int getShortcutIdx() {
+        return shortcutIdx;
+    }
+
+    /**
+     * Get the screen location of the highlighted character.
+     *
+     * @return the number of text cells required to display from the
+     * beginning of the label to shortcutIdx
+     */
+    public int getScreenShortcutIdx() {
+        return screenShortcutIdx;
+    }
+
+    /**
+     * Get the raw (uncolored) string.
+     *
+     * @return the raw (uncolored) string
+     */
+    public String getRawLabel() {
+        return rawLabel;
+    }
+
 }