diff options
| author | Albrecht Schlosser <albrechts.fltk@online.de> | 2016-03-12 17:44:24 +0000 |
|---|---|---|
| committer | Albrecht Schlosser <albrechts.fltk@online.de> | 2016-03-12 17:44:24 +0000 |
| commit | c1fe316855ecb6a6c0159fc969c8ddef2c5d9b3c (patch) | |
| tree | 2fa9c28112269fc440cd9fb1ed34acbf9044a705 | |
| parent | 3b1434238879cd4cdb0cf1d91563c5e661698f78 (diff) | |
Add localization of modifier key names in shortcut labels.
Modifier key names like Alt, Shift, Ctrl, Meta can now be localized by
setting global string pointers. See documentation of fl_shortcut_label().
Port branch-1.3, svn r 11321.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3-porting@11354 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
| -rw-r--r-- | FL/Fl.H | 7 | ||||
| -rw-r--r-- | src/Fl.cxx | 17 | ||||
| -rw-r--r-- | src/Fl_Menu.cxx | 6 | ||||
| -rw-r--r-- | src/fl_shortcut.cxx | 108 |
4 files changed, 114 insertions, 24 deletions
@@ -65,6 +65,13 @@ class Fl_Screen_Driver; # define FL_SOCKET int #endif +// Pointers you can use to change FLTK to a foreign language. +// Note: Similar pointers are defined in FL/fl_ask.H and src/fl_ask.cxx +extern FL_EXPORT const char* fl_local_ctrl; +extern FL_EXPORT const char* fl_local_meta; +extern FL_EXPORT const char* fl_local_alt; +extern FL_EXPORT const char* fl_local_shift; + /** \defgroup callback_functions Callback function typedefs \brief Typedefs defined in <FL/Fl.H> for callback or handler functions passed as function parameters. diff --git a/src/Fl.cxx b/src/Fl.cxx index 60ce50164..88f82a151 100644 --- a/src/Fl.cxx +++ b/src/Fl.cxx @@ -160,8 +160,24 @@ bool Fl::cfg_sys_win32 = 0; // // Globals... // + +// Pointers you can use to change FLTK to a foreign language. +// Note: Similar pointers are defined in FL/fl_ask.H and src/fl_ask.cxx +#if !defined(__APPLE__) || defined(FL_DOXYGEN) + const char* fl_local_alt = "Alt"; ///< string pointer used in shortcuts, you can change it to another language + const char* fl_local_ctrl = "Ctrl"; ///< string pointer used in shortcuts, you can change it to another language + const char* fl_local_meta = "Meta"; ///< string pointer used in shortcuts, you can change it to another language + const char* fl_local_shift = "Shift"; ///< string pointer used in shortcuts, you can change it to another language +#else + const char* fl_local_alt = "\xe2\x8c\xa5\\"; // U+2325 (option key) + const char* fl_local_ctrl = "\xe2\x8c\x83\\"; // U+2303 (up arrowhead) + const char* fl_local_meta = "\xe2\x8c\x98\\"; // U+2318 (place of interest sign) + const char* fl_local_shift = "\xe2\x87\xa7\\"; // U+21E7 (upwards white arrow) +#endif + #if defined(__APPLE__) || defined(FL_DOXYGEN) // PORTME: Fl_Screen_Driver - platform text // this should probably be part of Fl_Sys_Menubar +// Apple App Menu const char *Fl_Mac_App_Menu::about = "About %@"; const char *Fl_Mac_App_Menu::print = "Print Front Window"; const char *Fl_Mac_App_Menu::services = "Services"; @@ -170,6 +186,7 @@ const char *Fl_Mac_App_Menu::hide_others = "Hide Others"; const char *Fl_Mac_App_Menu::show = "Show All"; const char *Fl_Mac_App_Menu::quit = "Quit %@"; #endif // __APPLE__ // PORTME: Fl_Screen_Driver - platform text, system menu + #ifndef FL_DOXYGEN Fl_Widget *Fl::belowmouse_, *Fl::pushed_, diff --git a/src/Fl_Menu.cxx b/src/Fl_Menu.cxx index 1e3fa5f84..97a2574b2 100644 --- a/src/Fl_Menu.cxx +++ b/src/Fl_Menu.cxx @@ -482,10 +482,12 @@ void menuwindow::drawentry(const Fl_Menu_Item* m, int n, int eraseit) { button ? button->textsize() : FL_NORMAL_SIZE); const char *k, *s = fl_shortcut_label(m->shortcut_, &k); if (fl_utf_nb_char((const unsigned char*)k, (int) strlen(k))<=4) { - // righ-align the modifiers and left-align the key - char buf[32]; strcpy(buf, s); buf[k-s] = 0; + // right-align the modifiers and left-align the key + char *buf = (char*)malloc(k-s+1); + memcpy(buf, s, k-s); buf[k-s] = 0; fl_draw(buf, xx, yy, ww-shortcutWidth, hh, FL_ALIGN_RIGHT); fl_draw( k, xx+ww-shortcutWidth, yy, shortcutWidth, hh, FL_ALIGN_LEFT); + free(buf); } else { // right-align to the menu fl_draw(s, xx, yy, ww-4, hh, FL_ALIGN_RIGHT); diff --git a/src/fl_shortcut.cxx b/src/fl_shortcut.cxx index e7937a8ab..fc1a14c19 100644 --- a/src/fl_shortcut.cxx +++ b/src/fl_shortcut.cxx @@ -3,7 +3,7 @@ // // Shortcut support routines for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2011 by Bill Spitzak and others. +// Copyright 1998-2016 by Bill Spitzak and others. // // This library is free software. Distribution and use rights are outlined in // the file "COPYING" which should have been included with this file. If this @@ -36,6 +36,7 @@ #include <FL/Fl_Widget.H> #include <FL/Fl_Button.H> #include <FL/fl_draw.H> +#include <stdlib.h> #include <ctype.h> #include "flstring.h" #if !defined(WIN32) && !defined(__APPLE__) // PORTME: Fl_Screen_Driver - platform keyboard feel @@ -125,8 +126,8 @@ static Keyname table[] = { }; #elif defined(__APPLE__) // PORTME: Fl_Screen_Driver - platform keyboard feel static Keyname table[] = { - // v - this column contains UTF-8 characters - {' ', "Space"}, + // v - this column may contain UTF-8 characters + {' ', "Space"}, {FL_BackSpace,"\xe2\x8c\xab"}, // erase to the left {FL_Tab, "\xe2\x87\xa5"}, // rightwards arrow to bar {0xff0b, "\xe2\x8c\xa6"}, // erase to the right @@ -169,6 +170,35 @@ static Keyname table[] = { zero then an empty string is returned. The return value points at a static buffer that is overwritten with each call. + \since FLTK 1.3.4 modifier key names can be localized, but key names + can not yet be localized. This may be added to a future FLTK version. + + Modifier key names (human-readable shortcut names) can be defined + with the following global const char * pointer variables: + + - fl_local_ctrl => name of FL_CTRL + - fl_local_alt => name of FL_ALT + - fl_local_shift => name of FL_SHIFT + - fl_local_meta => name of FL_META + + \code + fl_local_ctrl = "Strg"; // German for "Ctrl" + fl_local_shift = "Umschalt"; // German for "Shift" + \endcode + + The shortcut name will be constructed by adding all modifier names in the + order defined above plus the name of the key. A '+' character is added to + each modifier name unless it has a trailing '\' or a trailing '+'. + + Example: + + Ctrl+Alt+Shift+Meta+F12 + + The default values for modifier key names are as given above for all + platforms except Mac OS X. Mac OS X uses graphical characters that represent + the typical OS X modifier names in menus, e.g. cloverleaf, saucepan, etc. + You may, however, redefine Mac OS X modifier names as well. + \param [in] shortcut the integer value containing the ascii character or extended keystroke plus modifiers \return a pointer to a static buffer containing human readable text for the shortcut */ @@ -176,38 +206,74 @@ const char* fl_shortcut_label(unsigned int shortcut) { return fl_shortcut_label(shortcut, 0L); } +/* + This static function adds a modifier key name to a character + buffer and returns the pointer behind the modifier name and a + trailing '+' character. + + Exceptions: + (1) Last character = '\' : remove it, done (don't add '+') + (2) Last character = '+' : user added '+', don't add another one + + In case of buffer overflow the modifier key name is replaced with "..." + if that fits or not added at all. This should rarely (never) happen. +*/ + +static char *add_modifier_key(char *p, const char *end, const char *name) { + int ln = strlen(name); + if (p+ln > end) { // string too long + if (p+4 <= end) { // can replace with "..." ? + strcpy(p,"..."); + p += 3; + } else + return p; + } else { + strcpy(p,name); + p += ln; + } + if (p[-1] == '\\') // remove (last) '\' character + p--; + else if (p[-1] == '+') // don't add another '+' character + {/*empty*/} + else // not a '\' or '+' + *p++ = '+'; // add a '+' character + return p; +} + /** Get a human-readable string from a shortcut value. \param [in] shortcut the integer value containing the ascii character or extended keystroke plus modifiers \param [in] eom if this pointer is set, it will receive a pointer to the end of the modifier text \return a pointer to a static buffer containing human readable text for the shortcut + \see fl_shortcut_label(unsigned int shortcut) - */ +*/ + const char* fl_shortcut_label(unsigned int shortcut, const char **eom) { - static char buf[40]; + static char buf[80]; char *p = buf; + char *end = &buf[sizeof(buf)-20]; // account for key name (max. ~10 + x) if (eom) *eom = p; if (!shortcut) {*p = 0; return buf;} // fix upper case shortcuts - unsigned int v = shortcut & FL_KEY_MASK; - if (((unsigned)fl_tolower(v))!=v) { + unsigned int key = shortcut & FL_KEY_MASK; + if (((unsigned)fl_tolower(key)) != key) { shortcut |= FL_SHIFT; } -#ifdef __APPLE__ // PORTME: Fl_Screen_Driver - platform keyboard feel - // this column contains utf8 characters - v - if (shortcut & FL_SHIFT) {strcpy(p,"\xe2\x87\xa7"); p += 3;} // U+21E7 (upwards white arrow) - if (shortcut & FL_CTRL) {strcpy(p,"\xe2\x8c\x83"); p += 3;} // U+2303 (up arrowhead) - if (shortcut & FL_ALT) {strcpy(p,"\xe2\x8c\xa5"); p += 3;} // U+2325 (option key) - if (shortcut & FL_META) {strcpy(p,"\xe2\x8c\x98"); p += 3;} // U+2318 (place of interest sign) -#else - if (shortcut & FL_META) {strcpy(p,"Meta+"); p += 5;} - if (shortcut & FL_ALT) {strcpy(p,"Alt+"); p += 4;} - if (shortcut & FL_SHIFT) {strcpy(p,"Shift+"); p += 6;} - if (shortcut & FL_CTRL) {strcpy(p,"Ctrl+"); p += 5;} -#endif // __APPLE__ // PORTME: Fl_Screen_Driver - platform keyboard feel + + // Add modifier key names. + // Note: if necessary we could change the order here depending on the platform. + // However, as discussed in fltk.development, the order appears to be the + // same on all platforms, with exceptions in _some_ Linux applications. + + if (shortcut & FL_CTRL) {p = add_modifier_key(p, end, fl_local_ctrl);} + if (shortcut & FL_ALT) {p = add_modifier_key(p, end, fl_local_alt);} + if (shortcut & FL_SHIFT) {p = add_modifier_key(p, end, fl_local_shift);} + if (shortcut & FL_META) {p = add_modifier_key(p, end, fl_local_meta);} if (eom) *eom = p; - unsigned int key = shortcut & FL_KEY_MASK; + + // add key name #if defined(WIN32) || defined(__APPLE__) // if not X // PORTME: Fl_Screen_Driver - platform keyboard feel if (key >= FL_F && key <= FL_F_Last) { *p++ = 'F'; @@ -265,8 +331,6 @@ const char* fl_shortcut_label(unsigned int shortcut, const char **eom) { #endif } -// Emulation of XForms named shortcuts -#include <stdlib.h> /** Emulation of XForms named shortcuts. |
