]>
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"
26 #include "wx/private/stattext.h" // for wxMarkupEntities
28 #include "wx/gtk/private/mnemonics.h"
30 // ============================================================================
32 // ============================================================================
34 // ----------------------------------------------------------------------------
35 // internal helper: apply the operation indicated by flag
36 // ----------------------------------------------------------------------------
42 MNEMONICS_CONVERT_MARKUP
45 static wxString
GTKProcessMnemonics(const wxString
& label
, MnemonicsFlag flag
)
48 labelGTK
.reserve(label
.length());
49 for ( wxString::const_iterator i
= label
.begin(); i
!= label
.end(); ++i
)
56 if ( i
+ 1 == label
.end() )
58 // "&" at the end of string is an error
59 wxLogDebug(wxT("Invalid label \"%s\"."), label
);
63 if ( flag
== MNEMONICS_CONVERT_MARKUP
)
65 bool isMnemonic
= true;
66 size_t distanceFromEnd
= label
.end() - i
;
68 // is this ampersand introducing a mnemonic or rather an entity?
69 for (size_t j
=0; j
< wxMARKUP_ENTITY_MAX
; j
++)
71 const wxChar
*entity
= wxMarkupEntities
[wxMARKUP_ELEMENT_NAME
][j
];
72 size_t entityLen
= wxStrlen(entity
);
74 if (distanceFromEnd
>= entityLen
&&
75 wxString(i
, i
+ entityLen
) == entity
)
78 i
+= entityLen
- 1; // the -1 is because main for()
79 // loop already increments i
90 ch
= *(++i
); // skip '&' itself
94 // special case: "&&" is not a mnemonic at all but just
96 if ( flag
== MNEMONICS_CONVERT_MARKUP
)
97 labelGTK
+= wxT("&");
103 if ( flag
!= MNEMONICS_REMOVE
)
105 // '_' can't be a GTK mnemonic apparently so
106 // replace it with something similar
107 labelGTK
+= wxT("_-");
113 if ( flag
!= MNEMONICS_REMOVE
)
114 labelGTK
+= wxT('_');
120 if ( flag
!= MNEMONICS_REMOVE
)
122 // escape any existing underlines in the string so that
123 // they don't become mnemonics accidentally
124 labelGTK
+= wxT("__");
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 wxString
wxGTKRemoveMnemonics(const wxString
& label
)
143 return GTKProcessMnemonics(label
, MNEMONICS_REMOVE
);
146 wxString
wxConvertMnemonicsToGTK(const wxString
& label
)
148 return GTKProcessMnemonics(label
, MNEMONICS_CONVERT
);
151 wxString
wxConvertMnemonicsToGTKMarkup(const wxString
& label
)
153 return GTKProcessMnemonics(label
, MNEMONICS_CONVERT_MARKUP
);
156 wxString
wxConvertMnemonicsFromGTK(const wxString
& gtkLabel
)
159 for ( const wxChar
*pc
= gtkLabel
.c_str(); *pc
; pc
++ )
161 // '_' is the escape character for GTK+.
163 if ( *pc
== wxT('_') && *(pc
+1) == wxT('_'))
165 // An underscore was escaped.
169 else if ( *pc
== wxT('_') )
171 // Convert GTK+ hotkey symbol to wxWidgets/Windows standard
174 else if ( *pc
== wxT('&') )
176 // Double the ampersand to escape it as far as wxWidgets is concerned
181 // don't remove ampersands '&' since if we have them in the menu title
182 // it means that they were doubled to indicate "&" instead of accelerator