]>
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/gtk/private/mnemonics.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 // Names of the standard XML entities.
37 const char *const entitiesNames
[] =
39 "&", "<", ">", "'", """
42 } // anonymous namespace
44 // ============================================================================
46 // ============================================================================
48 // ----------------------------------------------------------------------------
49 // internal helper: apply the operation indicated by flag
50 // ----------------------------------------------------------------------------
56 MNEMONICS_CONVERT_MARKUP
59 static wxString
GTKProcessMnemonics(const wxString
& label
, MnemonicsFlag flag
)
62 labelGTK
.reserve(label
.length());
63 for ( wxString::const_iterator i
= label
.begin(); i
!= label
.end(); ++i
)
70 if ( i
+ 1 == label
.end() )
72 // "&" at the end of string is an error
73 wxLogDebug(wxT("Invalid label \"%s\"."), label
);
77 if ( flag
== MNEMONICS_CONVERT_MARKUP
)
79 bool isMnemonic
= true;
80 size_t distanceFromEnd
= label
.end() - i
;
82 // is this ampersand introducing a mnemonic or rather an entity?
83 for (size_t j
=0; j
< WXSIZEOF(entitiesNames
); j
++)
85 const char *entity
= entitiesNames
[j
];
86 size_t entityLen
= wxStrlen(entity
);
88 if (distanceFromEnd
>= entityLen
&&
89 wxString(i
, i
+ entityLen
) == entity
)
92 i
+= entityLen
- 1; // the -1 is because main for()
93 // loop already increments i
104 ch
= *(++i
); // skip '&' itself
108 // special case: "&&" is not a mnemonic at all but just
110 if ( flag
== MNEMONICS_CONVERT_MARKUP
)
111 labelGTK
+= wxT("&");
113 labelGTK
+= wxT('&');
117 if ( flag
!= MNEMONICS_REMOVE
)
119 // '_' can't be a GTK mnemonic apparently so
120 // replace it with something similar
121 labelGTK
+= wxT("_-");
127 if ( flag
!= MNEMONICS_REMOVE
)
128 labelGTK
+= wxT('_');
134 if ( flag
!= MNEMONICS_REMOVE
)
136 // escape any existing underlines in the string so that
137 // they don't become mnemonics accidentally
138 labelGTK
+= wxT("__");
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 wxString
wxGTKRemoveMnemonics(const wxString
& label
)
157 return GTKProcessMnemonics(label
, MNEMONICS_REMOVE
);
160 wxString
wxConvertMnemonicsToGTK(const wxString
& label
)
162 return GTKProcessMnemonics(label
, MNEMONICS_CONVERT
);
165 wxString
wxConvertMnemonicsToGTKMarkup(const wxString
& label
)
167 return GTKProcessMnemonics(label
, MNEMONICS_CONVERT_MARKUP
);
170 wxString
wxConvertMnemonicsFromGTK(const wxString
& gtkLabel
)
173 for ( const wxChar
*pc
= gtkLabel
.c_str(); *pc
; pc
++ )
175 // '_' is the escape character for GTK+.
177 if ( *pc
== wxT('_') && *(pc
+1) == wxT('_'))
179 // An underscore was escaped.
183 else if ( *pc
== wxT('_') )
185 // Convert GTK+ hotkey symbol to wxWidgets/Windows standard
188 else if ( *pc
== wxT('&') )
190 // Double the ampersand to escape it as far as wxWidgets is concerned
195 // don't remove ampersands '&' since if we have them in the menu title
196 // it means that they were doubled to indicate "&" instead of accelerator