]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/mnemonics.cpp
   1 /////////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/gtk/mnemonics.cpp 
   3 // Purpose:     implementation of GTK mnemonics conversion functions 
   4 // Author:      Vadim Zeitlin 
   7 // Copyright:   (c) 2007 Vadim Zeitlin <vadim@wxwindows.org> 
   8 // Licence:     wxWindows licence 
   9 /////////////////////////////////////////////////////////////////////////////// 
  11 // ============================================================================ 
  13 // ============================================================================ 
  15 // ---------------------------------------------------------------------------- 
  17 // ---------------------------------------------------------------------------- 
  19 // for compilers that support precompilation, includes "wx.h". 
  20 #include "wx/wxprec.h" 
  27 #include "wx/private/stattext.h"        // for wxMarkupEntities 
  29 #include "wx/gtk/private/mnemonics.h" 
  31 // ============================================================================ 
  33 // ============================================================================ 
  35 // ---------------------------------------------------------------------------- 
  36 // internal helper: apply the operation indicated by flag 
  37 // ---------------------------------------------------------------------------- 
  43     MNEMONICS_CONVERT_MARKUP
 
  46 static wxString 
GTKProcessMnemonics(const wxString
& label
, MnemonicsFlag flag
) 
  49     labelGTK
.reserve(label
.length()); 
  50     for ( wxString::const_iterator i 
= label
.begin(); i 
!= label
.end(); ++i 
) 
  57                 if ( i 
+ 1 == label
.end() ) 
  59                     // "&" at the end of string is an error 
  60                     wxLogDebug(wxT("Invalid label \"%s\"."), label
); 
  64                 if ( flag 
== MNEMONICS_CONVERT_MARKUP 
) 
  66                     bool isMnemonic 
= true; 
  67                     size_t distanceFromEnd 
= label
.end() - i
; 
  69                     // is this ampersand introducing a mnemonic or rather an entity? 
  70                     for (size_t j
=0; j 
< wxMARKUP_ENTITY_MAX
; j
++) 
  72                         const wxChar 
*entity 
= wxMarkupEntities
[wxMARKUP_ELEMENT_NAME
][j
]; 
  73                         size_t entityLen 
= wxStrlen(entity
); 
  75                         if (distanceFromEnd 
>= entityLen 
&& 
  76                             wxString(i
, i 
+ entityLen
) == entity
) 
  79                             i 
+= entityLen 
- 1;     // the -1 is because main for() 
  80                                                     // loop already increments i 
  91                 ch 
= *(++i
); // skip '&' itself 
  95                         // special case: "&&" is not a mnemonic at all but just 
  97                         if ( flag 
== MNEMONICS_CONVERT_MARKUP 
) 
  98                             labelGTK 
+= wxT("&"); 
 100                             labelGTK 
+= wxT('&'); 
 104                         if ( flag 
!= MNEMONICS_REMOVE 
) 
 106                             // '_' can't be a GTK mnemonic apparently so 
 107                             // replace it with something similar 
 108                             labelGTK 
+= wxT("_-"); 
 114                         if ( flag 
!= MNEMONICS_REMOVE 
) 
 115                             labelGTK 
+= wxT('_'); 
 121                 if ( flag 
!= MNEMONICS_REMOVE 
) 
 123                     // escape any existing underlines in the string so that 
 124                     // they don't become mnemonics accidentally 
 125                     labelGTK 
+= wxT("__"); 
 138 // ---------------------------------------------------------------------------- 
 140 // ---------------------------------------------------------------------------- 
 142 wxString 
wxGTKRemoveMnemonics(const wxString
& label
) 
 144     return GTKProcessMnemonics(label
, MNEMONICS_REMOVE
); 
 147 wxString 
wxConvertMnemonicsToGTK(const wxString
& label
) 
 149     return GTKProcessMnemonics(label
, MNEMONICS_CONVERT
); 
 152 wxString 
wxConvertMnemonicsToGTKMarkup(const wxString
& label
) 
 154     return GTKProcessMnemonics(label
, MNEMONICS_CONVERT_MARKUP
); 
 157 wxString 
wxConvertMnemonicsFromGTK(const wxString
& gtkLabel
) 
 160     for ( const wxChar 
*pc 
= gtkLabel
.c_str(); *pc
; pc
++ ) 
 162         // '_' is the escape character for GTK+. 
 164         if ( *pc 
== wxT('_') && *(pc
+1) == wxT('_')) 
 166             // An underscore was escaped. 
 170         else if ( *pc 
== wxT('_') ) 
 172             // Convert GTK+ hotkey symbol to wxWidgets/Windows standard 
 175         else if ( *pc 
== wxT('&') ) 
 177             // Double the ampersand to escape it as far as wxWidgets is concerned 
 182             // don't remove ampersands '&' since if we have them in the menu title 
 183             // it means that they were doubled to indicate "&" instead of accelerator