]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/mnemonics.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/mnemonics.cpp
3 // Purpose: implementation of GTK mnemonics conversion functions
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // for compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
26 #include "wx/gtk1/private/mnemonics.h"
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 // Names of the standard XML entities.
36 const char *const entitiesNames
[] =
38 "&", "<", ">", "'", """
41 } // anonymous namespace
43 // ============================================================================
45 // ============================================================================
47 // ----------------------------------------------------------------------------
48 // internal helper: apply the operation indicated by flag
49 // ----------------------------------------------------------------------------
55 MNEMONICS_CONVERT_MARKUP
58 static wxString
GTKProcessMnemonics(const wxString
& label
, MnemonicsFlag flag
)
61 labelGTK
.reserve(label
.length());
62 for ( wxString::const_iterator i
= label
.begin(); i
!= label
.end(); ++i
)
69 if ( i
+ 1 == label
.end() )
71 // "&" at the end of string is an error
72 wxLogDebug(wxT("Invalid label \"%s\"."), label
);
76 if ( flag
== MNEMONICS_CONVERT_MARKUP
)
78 bool isMnemonic
= true;
79 size_t distanceFromEnd
= label
.end() - i
;
81 // is this ampersand introducing a mnemonic or rather an entity?
82 for (size_t j
=0; j
< WXSIZEOF(entitiesNames
); j
++)
84 const char *entity
= entitiesNames
[j
];
85 size_t entityLen
= wxStrlen(entity
);
87 if (distanceFromEnd
>= entityLen
&&
88 wxString(i
, i
+ entityLen
) == entity
)
91 i
+= entityLen
- 1; // the -1 is because main for()
92 // loop already increments i
103 ch
= *(++i
); // skip '&' itself
107 // special case: "&&" is not a mnemonic at all but just
109 if ( flag
== MNEMONICS_CONVERT_MARKUP
)
110 labelGTK
+= wxT("&");
112 labelGTK
+= wxT('&');
116 if ( flag
!= MNEMONICS_REMOVE
)
118 // '_' can't be a GTK mnemonic apparently so
119 // replace it with something similar
120 labelGTK
+= wxT("_-");
126 if ( flag
!= MNEMONICS_REMOVE
)
127 labelGTK
+= wxT('_');
133 if ( flag
!= MNEMONICS_REMOVE
)
135 // escape any existing underlines in the string so that
136 // they don't become mnemonics accidentally
137 labelGTK
+= wxT("__");
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
154 wxString
wxGTKRemoveMnemonics(const wxString
& label
)
156 return GTKProcessMnemonics(label
, MNEMONICS_REMOVE
);
159 wxString
wxConvertMnemonicsToGTK(const wxString
& label
)
161 return GTKProcessMnemonics(label
, MNEMONICS_CONVERT
);
164 wxString
wxConvertMnemonicsToGTKMarkup(const wxString
& label
)
166 return GTKProcessMnemonics(label
, MNEMONICS_CONVERT_MARKUP
);
169 wxString
wxConvertMnemonicsFromGTK(const wxString
& gtkLabel
)
172 for ( const wxChar
*pc
= gtkLabel
.c_str(); *pc
; pc
++ )
174 // '_' is the escape character for GTK+.
176 if ( *pc
== wxT('_') && *(pc
+1) == wxT('_'))
178 // An underscore was escaped.
182 else if ( *pc
== wxT('_') )
184 // Convert GTK+ hotkey symbol to wxWidgets/Windows standard
187 else if ( *pc
== wxT('&') )
189 // Double the ampersand to escape it as far as wxWidgets is concerned
194 // don't remove ampersands '&' since if we have them in the menu title
195 // it means that they were doubled to indicate "&" instead of accelerator