From bcff4ec8e0e8d75880312d31e3e678b8f270de6b Mon Sep 17 00:00:00 2001 From: Matthias Melcher Date: Thu, 17 Sep 2009 22:12:24 +0000 Subject: International menu shortcuts working on Mac OS (and probably the other OS's) git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@6878 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- src/fl_shortcut.cxx | 71 ++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 39 deletions(-) (limited to 'src/fl_shortcut.cxx') diff --git a/src/fl_shortcut.cxx b/src/fl_shortcut.cxx index cd6f1cf9e..a189cfdba 100644 --- a/src/fl_shortcut.cxx +++ b/src/fl_shortcut.cxx @@ -39,7 +39,7 @@ // It also checks against the first character of Fl::event_text(), // and zero for FL_SHIFT means "don't care". // This allows punctuation shortcuts like "#" to work (rather than -// calling it "shift+3") +// calling it "shift+3" on a US keyboard) #include #include @@ -58,19 +58,12 @@ be confused with Fl_Widget::test_shortcut(). */ -int Fl::test_shortcut(int shortcut) { +int Fl::test_shortcut(Fl_Shortcut shortcut) { if (!shortcut) return 0; - int v = shortcut & 0xffff; -#ifdef __APPLE__ - if (v > 32 && v < 0x7f || v >= 0x80 && v <= 0xff) { -#else - // most X11 use MSWindows Latin-1 if set to Western encoding, so 0x80 to 0xa0 are defined - if (v > 32 && v < 0x7f || v >= 0x80 && v <= 0xff) { -#endif - if (isupper(v)) { - shortcut |= FL_SHIFT; - } + Fl_Char v = shortcut & FL_KEY_MASK; + if (fl_tolower(v)!=v) { + shortcut |= FL_SHIFT; } int shift = Fl::event_state(); @@ -81,17 +74,18 @@ int Fl::test_shortcut(int shortcut) { // these three must always be correct: if (mismatch&(FL_META|FL_ALT|FL_CTRL)) return 0; - int key = shortcut & 0xffff; + Fl_Char key = shortcut & FL_KEY_MASK; // if shift is also correct, check for exactly equal keysyms: if (!(mismatch&(FL_SHIFT)) && key == Fl::event_key()) return 1; - // try matching ascii, ignore shift: - if (key == event_text()[0]) return 1; + // try matching utf8, ignore shift: + Fl_Char firstChar = fl_utf8decode(Fl::event_text(), Fl::event_text()+Fl::event_length(), 0); + if (key==firstChar) return 1; // kludge so that Ctrl+'_' works (as opposed to Ctrl+'^_'): if ((shift&FL_CTRL) && key >= 0x3f && key <= 0x5F - && event_text()[0]==(key^0x40)) return 1; + && firstChar==(key^0x40)) return 1; // firstChar should be within a-z return 0; } @@ -180,7 +174,7 @@ static Keyname table[] = { \param [in] shortcut the integer value containing the ascii charcter or extended keystroke plus modifiers \return a pointer to a static buffer containing human readable text for the shortcut */ -const char* fl_shortcut_label(int shortcut) { +const char* fl_shortcut_label(Fl_Shortcut shortcut) { return fl_shortcut_label(shortcut, 0L); } @@ -190,23 +184,17 @@ const char* fl_shortcut_label(int shortcut) { \param [in] shortcut the integer value containing the ascii charcter 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(int shortcut) + \see fl_shortcut_label(Fl_Shortcut shortcut) */ -const char* fl_shortcut_label(int shortcut, const char **eom) { +const char* fl_shortcut_label(Fl_Shortcut shortcut, const char **eom) { static char buf[20]; char *p = buf; if (eom) *eom = p; if (!shortcut) {*p = 0; return buf;} // fix upper case shortcuts - int v = shortcut & 0xffff; -#ifdef __APPLE__ - if (v > 32 && v < 0x7f || v >= 0x80 && v <= 0xff) { -#else - if (v > 32 && v < 0x7f || v >= 0xa0 && v <= 0xff) { -#endif - if (isupper(v)) { - shortcut |= FL_SHIFT; - } + Fl_Char v = shortcut & FL_KEY_MASK; + if (fl_tolower(v)!=v) { + shortcut |= FL_SHIFT; } #ifdef __APPLE__ // this column contains utf8 characters - v @@ -221,7 +209,7 @@ const char* fl_shortcut_label(int shortcut, const char **eom) { if (shortcut & FL_CTRL) {strcpy(p,"Ctrl+"); p += 5;} #endif // __APPLE__ if (eom) *eom = p; - int key = shortcut & 0xFFFF; + Fl_Char key = shortcut & FL_KEY_MASK; #if defined(WIN32) || defined(__APPLE__) // if not X if (key >= FL_F && key <= FL_F_Last) { *p++ = 'F'; @@ -234,7 +222,7 @@ const char* fl_shortcut_label(int shortcut, const char **eom) { while (a < b) { int c = (a+b)/2; if (table[c].key == key) { - if (p > buf) { + if (p > buf) { strcpy(p,table[c].name); return buf; } else { @@ -252,7 +240,7 @@ const char* fl_shortcut_label(int shortcut, const char **eom) { *p++ = uchar(key & 127); } else { // if none found, use the keystroke as a match: - *p++ = uchar(toupper(key & 255)); + p += fl_utf8encode(fl_toupper(key), p); } } *p = 0; @@ -262,7 +250,11 @@ const char* fl_shortcut_label(int shortcut, const char **eom) { if (key == FL_Enter || key == '\r') q="Enter"; // don't use Xlib's "Return": else if (key > 32 && key < 0x100) q = 0; else q = XKeysymToString(key); - if (!q) {*p++ = uchar(toupper(key & 255)); *p = 0; return buf;} + if (!q) { + p += fl_utf8encode(fl_toupper(key), p); + *p = 0; + return buf; + } if (p > buf) { strcpy(p,q); return buf; @@ -278,9 +270,9 @@ const char* fl_shortcut_label(int shortcut, const char **eom) { /** Emulation of XForms named shortcuts. */ -int fl_old_shortcut(const char* s) { +Fl_Shortcut fl_old_shortcut(const char* s) { if (!s || !*s) return 0; - int n = 0; + Fl_Shortcut n = 0; if (*s == '#') {n |= FL_ALT; s++;} if (*s == '+') {n |= FL_SHIFT; s++;} if (*s == '^') {n |= FL_CTRL; s++;} @@ -290,14 +282,14 @@ int fl_old_shortcut(const char* s) { // Tests for &x shortcuts in button labels: -char Fl_Widget::label_shortcut(const char *t) { +Fl_Shortcut Fl_Widget::label_shortcut(const char *t) { if (!t) return 0; for (;;) { if (*t==0) return 0; if (*t=='&') { - char s = t[1]; + Fl_Shortcut s = fl_utf8decode(t+1, 0, 0); if (s==0) return 0; - else if (s=='&') t++; + else if (s==(Fl_Char)'&') t++; else return s; } t++; @@ -309,8 +301,9 @@ int Fl_Widget::test_shortcut(const char *t) { // on MSWindows, users expect shortcuts to work only when the Alt modifier is pressed if (Fl::event_state(FL_ALT)==0) return 0; #endif - char c = Fl::event_text()[0]; - if (!c || !t) return 0; + if (!t) return 0; + Fl_Shortcut c = fl_utf8decode(Fl::event_text(), Fl::event_text()+Fl::event_length(), 0); + if (!c) return 0; if (c == label_shortcut(t)) return 1; return 0; -- cgit v1.2.3