[haiku-commits] r33866 - haiku/trunk/src/preferences/keymap

  • From: axeld@xxxxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Tue, 3 Nov 2009 12:19:54 +0100 (CET)

Author: axeld
Date: 2009-11-03 12:19:53 +0100 (Tue, 03 Nov 2009)
New Revision: 33866
Changeset: http://dev.haiku-os.org/changeset/33866/haiku

Modified:
   haiku/trunk/src/preferences/keymap/KeyboardLayoutView.cpp
   haiku/trunk/src/preferences/keymap/KeyboardLayoutView.h
Log:
* Some keymap labels now have an abbreviated version that is used when the key
  gets too small for the full text.
* This closes enhancement ticket #4024.


Modified: haiku/trunk/src/preferences/keymap/KeyboardLayoutView.cpp
===================================================================
--- haiku/trunk/src/preferences/keymap/KeyboardLayoutView.cpp   2009-11-02 
19:27:15 UTC (rev 33865)
+++ haiku/trunk/src/preferences/keymap/KeyboardLayoutView.cpp   2009-11-03 
11:19:53 UTC (rev 33866)
@@ -607,6 +607,7 @@
 
                rect.InsetBy(1, 1);
 
+               _GetAbbreviatedKeyLabelIfNeeded(view, rect, key, text, 
sizeof(text));
                be_control_look->DrawLabel(view, text, rect, updateRect,
                        base, 0, BAlignment(B_ALIGN_CENTER, B_ALIGN_MIDDLE));
        } else if (key->shape == kEnterKeyShape) {
@@ -626,6 +627,8 @@
                _DrawKeyButton(view, rect, updateRect, base, background, 
pressed);
 
                rect.left = missingRect.right;
+               _GetAbbreviatedKeyLabelIfNeeded(view, rect, key, text, 
sizeof(text));
+
                be_control_look->DrawLabel(view, text, rect, updateRect,
                        base, 0, BAlignment(B_ALIGN_CENTER, B_ALIGN_MIDDLE));
 
@@ -701,22 +704,23 @@
 
 
 const char*
-KeyboardLayoutView::_SpecialKeyLabel(const key_map& map, uint32 code)
+KeyboardLayoutView::_SpecialKeyLabel(const key_map& map, uint32 code,
+       bool abbreviated)
 {
        if (code == map.caps_key)
-               return "CAPS LOCK";
+               return abbreviated ? "CAPS" : "CAPS LOCK";
        if (code == map.scroll_key)
                return "SCROLL";
        if (code == map.num_key)
-               return "NUM LOCK";
+               return abbreviated ? "NUM" : "NUM LOCK";
        if (code == map.left_shift_key || code == map.right_shift_key)
                return "SHIFT";
        if (code == map.left_command_key || code == map.right_command_key)
-               return "COMMAND";
+               return abbreviated ? "CMD" : "COMMAND";
        if (code == map.left_control_key || code == map.right_control_key)
-               return "CONTROL";
+               return abbreviated ? "CTRL" : "CONTROL";
        if (code == map.left_option_key || code == map.right_option_key)
-               return "OPTION";
+               return abbreviated ? "OPT" : "OPTION";
        if (code == map.menu_key)
                return "MENU";
        if (code == B_PRINT_KEY)
@@ -755,7 +759,8 @@
 
 
 const char*
-KeyboardLayoutView::_SpecialMappedKeyLabel(const char* bytes, size_t numBytes)
+KeyboardLayoutView::_SpecialMappedKeyLabel(const char* bytes, size_t numBytes,
+       bool abbreviated)
 {
        if (numBytes != 1)
                return NULL;
@@ -772,9 +777,9 @@
        if (bytes[0] == B_END)
                return "END";
        if (bytes[0] == B_PAGE_UP)
-               return "PAGE \xe2\x86\x91";
+               return abbreviated ? "PG \xe2\x86\x91" : "PAGE \xe2\x86\x91";
        if (bytes[0] == B_PAGE_DOWN)
-               return "PAGE \xe2\x86\x93";
+               return abbreviated ? "PG \xe2\x86\x93" : "PAGE \xe2\x86\x93";
 
        return NULL;
 }
@@ -793,6 +798,36 @@
 
 
 void
+KeyboardLayoutView::_GetAbbreviatedKeyLabelIfNeeded(BView* view, BRect rect,
+       const Key* key, char* text, size_t textSize)
+{
+       if (floorf(rect.Width()) > ceilf(view->StringWidth(text)))
+               return;
+
+       // Check if we have a shorter version of this key
+
+       const key_map& map = fKeymap->Map();
+
+       const char* special = _SpecialKeyLabel(map, key->code, true);
+       if (special != NULL) {
+               strlcpy(text, special, textSize);
+               return;
+       }
+
+       char* bytes = NULL;
+       int32 numBytes;
+       fKeymap->GetChars(key->code, fModifiers, fDeadKey, &bytes, &numBytes);
+       if (bytes != NULL) {
+               special = _SpecialMappedKeyLabel(bytes, numBytes, true);
+               if (special != NULL)
+                       strlcpy(text, special, textSize);
+
+               delete[] bytes;
+       }
+}
+
+
+void
 KeyboardLayoutView::_GetKeyLabel(const Key* key, char* text, size_t textSize,
        key_kind& keyKind)
 {
@@ -814,28 +849,25 @@
 
        char* bytes = NULL;
        int32 numBytes;
-       fKeymap->GetChars(key->code, fModifiers, fDeadKey, &bytes,
-               &numBytes);
+       fKeymap->GetChars(key->code, fModifiers, fDeadKey, &bytes, &numBytes);
        if (bytes != NULL) {
                special = _SpecialMappedKeyLabel(bytes, numBytes);
                if (special != NULL) {
                        strlcpy(text, special, textSize);
                        keyKind = kSpecialKey;
-                       return;
+               } else {
+                       special = _SpecialMappedKeySymbol(bytes, numBytes);
+                       if (special != NULL) {
+                               strlcpy(text, special, textSize);
+                               keyKind = kSymbolKey;
+                       } else {
+                               bool hasGlyphs;
+                               fFont.GetHasGlyphs(bytes, 1, &hasGlyphs);
+                               if (hasGlyphs)
+                                       strlcpy(text, bytes, sizeof(text));
+                       }
                }
 
-               special = _SpecialMappedKeySymbol(bytes, numBytes);
-               if (special != NULL) {
-                       strlcpy(text, special, textSize);
-                       keyKind = kSymbolKey;
-                       return;
-               }
-
-               bool hasGlyphs;
-               fFont.GetHasGlyphs(bytes, 1, &hasGlyphs);
-               if (hasGlyphs)
-                       strlcpy(text, bytes, sizeof(text));
-
                delete[] bytes;
        }
 }

Modified: haiku/trunk/src/preferences/keymap/KeyboardLayoutView.h
===================================================================
--- haiku/trunk/src/preferences/keymap/KeyboardLayoutView.h     2009-11-02 
19:27:15 UTC (rev 33865)
+++ haiku/trunk/src/preferences/keymap/KeyboardLayoutView.h     2009-11-03 
11:19:53 UTC (rev 33866)
@@ -63,13 +63,17 @@
                        void                    _DrawIndicator(BView* view, 
BRect updateRect,
                                                                const 
Indicator* indicator, BRect rect,
                                                                bool lit);
-                       const char*             _SpecialKeyLabel(const key_map& 
map, uint32 code);
+                       const char*             _SpecialKeyLabel(const key_map& 
map, uint32 code,
+                                                               bool 
abbreviated = false);
                        const char*             _SpecialMappedKeySymbol(const 
char* bytes,
                                                                size_t 
numBytes);
                        const char*             _SpecialMappedKeyLabel(const 
char* bytes,
-                                                               size_t 
numBytes);
+                                                               size_t 
numBytes, bool abbreviated = false);
                        bool                    _FunctionKeyLabel(uint32 code, 
char* text,
                                                                size_t 
textSize);
+                       void                    
_GetAbbreviatedKeyLabelIfNeeded(BView* view,
+                                                               BRect rect, 
const Key* key, char* text,
+                                                               size_t 
textSize);
                        void                    _GetKeyLabel(const Key* key, 
char* text,
                                                                size_t 
textSize, key_kind& keyKind);
                        bool                    _IsKeyPressed(uint32 code);


Other related posts: