fltk: Handle '_' in button labels as keyboard shortcuts.
authorDamien Goutte-Gattat <dgouttegattat@incenp.org>
Thu, 7 Feb 2019 23:03:03 +0000 (23:03 +0000)
committerDamien Goutte-Gattat <dgouttegattat@incenp.org>
Sun, 10 Feb 2019 18:31:34 +0000 (18:31 +0000)
* fltk/main.cxx (convert_accel_utf8): New method.
(fltk_cmd_handler): Calls previous method to escape button labels.

GnuPG-bug-id: 4336
Signed-off-by: Damien Goutte-Gattat <dgouttegattat@incenp.org>
Co-authored-by: Michael Bäuerle
fltk/main.cxx

index 5d226ed..2afad1c 100644 (file)
@@ -78,6 +78,44 @@ static std::string escape_accel_utf8(const char *s)
        return result;
 }
 
+// For button labels
+// Accelerator '_' (used e.g. by GPG2) is converted to '&' (for FLTK)
+// '&' is escaped as in escape_accel_utf8()
+static std::string convert_accel_utf8(const char *s)
+{
+       static bool last_was_underscore = false;
+       std::string result;
+       if (NULL != s)
+       {
+               result.reserve(strlen(s));
+               for (const char *p = s; *p; ++p)
+               {
+                       // & => &&
+                       if ('&' == *p)
+                               result.push_back(*p);
+                       // _ => & (handle '__' as escaped underscore)
+                       if ('_' == *p)
+                       {
+                               if (last_was_underscore)
+                               {
+                                       result.push_back(*p);
+                                       last_was_underscore = false;
+                               }
+                               else
+                                       last_was_underscore = true;
+                       }
+                       else
+                       {
+                               if (last_was_underscore)
+                                       result.push_back('&');
+                               result.push_back(*p);
+                               last_was_underscore = false;
+                       }
+               }
+       }
+       return result;
+}
+
 class cancel_exception
 {
 
@@ -111,8 +149,8 @@ static int fltk_cmd_handler(pinentry_t pe)
                // TODO: Add parent window to pinentry-fltk window
                //if (pe->parent_wid){}
                std::string title  = !is_empty(pe->title)?pe->title:PGMNAME;
-               std::string ok     = escape_accel_utf8(pe->ok?pe->ok:(pe->default_ok?pe->default_ok:OK_STRING));
-               std::string cancel = escape_accel_utf8(pe->cancel?pe->cancel:(pe->default_cancel?pe->default_cancel:CANCEL_STRING));
+               std::string ok     = convert_accel_utf8(pe->ok?pe->ok:(pe->default_ok?pe->default_ok:OK_STRING));
+               std::string cancel = convert_accel_utf8(pe->cancel?pe->cancel:(pe->default_cancel?pe->default_cancel:CANCEL_STRING));
 
                if (!!pe->pin) // password (or confirmation)
                {