]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: menuitem.cpp | |
3 | // Purpose: wxMenuItem implementation | |
4 | // Author: AUTHOR | |
5 | // Modified by: | |
6 | // Created: ??/??/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) AUTHOR | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // headers & declarations | |
14 | // ============================================================================ | |
15 | ||
16 | #include "wx/app.h" | |
17 | #include "wx/menu.h" | |
18 | #include "wx/menuitem.h" | |
19 | ||
20 | #include "wx/mac/uma.h" | |
21 | // ============================================================================ | |
22 | // implementation | |
23 | // ============================================================================ | |
24 | ||
25 | // ---------------------------------------------------------------------------- | |
26 | // dynamic classes implementation | |
27 | // ---------------------------------------------------------------------------- | |
28 | ||
29 | #if !USE_SHARED_LIBRARY | |
30 | IMPLEMENT_DYNAMIC_CLASS(wxMenuItem, wxObject) | |
31 | #endif //USE_SHARED_LIBRARY | |
32 | ||
33 | // ---------------------------------------------------------------------------- | |
34 | // wxMenuItem | |
35 | // ---------------------------------------------------------------------------- | |
36 | ||
37 | // | |
38 | // Helper Functions to get Mac Menus the way they should be ;-) | |
39 | // | |
40 | ||
41 | void wxMacCtoPString(const char* theCString, Str255 thePString); | |
42 | ||
43 | // remove inappropriate characters, if useShortcuts is false, the ampersand will not auto-generate a mac menu-shortcut | |
44 | ||
45 | int wxMenuItem::MacBuildMenuString(StringPtr outMacItemText, SInt16 *outMacShortcutChar , UInt8 *outMacModifiers , const char *inItemText , bool useShortcuts ) | |
46 | { | |
47 | char *p = (char *) &outMacItemText[1] ; | |
48 | short macModifiers = 0 ; | |
49 | char macShortCut = 0 ; | |
50 | const char *inItemName ; | |
51 | wxString inItemTextMac ; | |
52 | ||
53 | if (wxApp::s_macDefaultEncodingIsPC) | |
54 | { | |
55 | inItemTextMac = wxMacMakeMacStringFromPC( inItemText ) ; | |
56 | inItemName = inItemTextMac ; | |
57 | } | |
58 | else | |
59 | { | |
60 | inItemName = inItemText ; | |
61 | } | |
62 | ||
63 | if ( useShortcuts && !wxApp::s_macSupportPCMenuShortcuts ) | |
64 | useShortcuts = false ; | |
65 | ||
66 | // we have problems with a leading hypen - it will be taken as a separator | |
67 | ||
68 | while ( *inItemName == '-' ) | |
69 | inItemName++ ; | |
70 | ||
71 | while( *inItemName ) | |
72 | { | |
73 | switch ( *inItemName ) | |
74 | { | |
75 | // special characters for macintosh menus -> use some replacement | |
76 | case ';' : | |
77 | *p++ = ',' ; | |
78 | break ; | |
79 | case '^' : | |
80 | *p++ = ' ' ; | |
81 | break ; | |
82 | case '!' : | |
83 | *p++ = ' ' ; | |
84 | break ; | |
85 | case '<' : | |
86 | *p++ = '[' ; | |
87 | break ; | |
88 | case '>' : | |
89 | *p++ = ']' ; | |
90 | break ; | |
91 | case '/' : | |
92 | *p++ = '|' ; | |
93 | break ; | |
94 | case '(' : | |
95 | *p++ = '[' ; | |
96 | break ; | |
97 | case ')' : | |
98 | *p++ = ']' ; | |
99 | break ; | |
100 | // shortcuts | |
101 | case '&' : | |
102 | { | |
103 | ++inItemName ; | |
104 | if ( *inItemName ) | |
105 | { | |
106 | *p++ = *inItemName ; | |
107 | if ( useShortcuts ) | |
108 | macShortCut = *inItemName ; | |
109 | } | |
110 | else | |
111 | --inItemName ; | |
112 | } | |
113 | break ; | |
114 | // win-like accelerators | |
115 | case '\t' : | |
116 | { | |
117 | ++inItemName ; | |
118 | while( *inItemName ) | |
119 | { | |
120 | if (strncmp("Ctrl", inItemName, 4) == 0) | |
121 | { | |
122 | inItemName = inItemName + 5; | |
123 | macShortCut = *inItemName; | |
124 | } | |
125 | else if (strncmp("Cntrl", inItemName, 5) == 0) | |
126 | { | |
127 | inItemName = inItemName + 6; | |
128 | macShortCut = *inItemName; | |
129 | } | |
130 | else if (strncmp("Alt", inItemName, 3) == 0) | |
131 | { | |
132 | inItemName = inItemName + 4; | |
133 | macModifiers |= kMenuOptionModifier ; | |
134 | macShortCut = *inItemName ; | |
135 | } | |
136 | else if (strncmp("Shift", inItemName, 5) == 0) | |
137 | { | |
138 | inItemName = inItemName + 6; | |
139 | macModifiers |= kMenuShiftModifier ; | |
140 | macShortCut = *inItemName ; | |
141 | } | |
142 | else if (strncmp("F", inItemName, 1) == 0) | |
143 | { | |
144 | inItemName += strlen( inItemName ) ; | |
145 | // no function keys at the moment | |
146 | // macModifiers |= kMenuShiftModifier ; | |
147 | // macShortCut = *inItemName ; | |
148 | } | |
149 | else | |
150 | { | |
151 | break ; | |
152 | } | |
153 | } | |
154 | ||
155 | if ( *inItemName == 0 ) | |
156 | --inItemName ; | |
157 | ||
158 | } | |
159 | break ; | |
160 | default : | |
161 | *p++ = *inItemName ; | |
162 | } | |
163 | ++inItemName ; | |
164 | } | |
165 | ||
166 | outMacItemText[0] = (p - (char *)outMacItemText) - 1; | |
167 | if ( outMacShortcutChar ) | |
168 | *outMacShortcutChar = macShortCut ; | |
169 | if ( outMacModifiers ) | |
170 | *outMacModifiers = macModifiers ; | |
171 | ||
172 | return 0 ; | |
173 | } | |
174 | ||
175 | // ctor & dtor | |
176 | // ----------- | |
177 | ||
178 | wxMenuItem::wxMenuItem(wxMenu *pParentMenu, int id, | |
179 | const wxString& text, const wxString& strHelp, | |
180 | bool bCheckable, | |
181 | wxMenu *pSubMenu) | |
182 | { | |
183 | wxASSERT( pParentMenu != NULL ); | |
184 | ||
185 | m_parentMenu = pParentMenu; | |
186 | m_subMenu = pSubMenu; | |
187 | m_isEnabled = TRUE; | |
188 | m_isChecked = FALSE; | |
189 | m_id = id; | |
190 | m_text = text; | |
191 | m_isCheckable = bCheckable; | |
192 | m_help = strHelp; | |
193 | ||
194 | ||
195 | if ( m_text == "E&xit" ||m_text == "Exit" ) | |
196 | { | |
197 | m_text = "Quit\tCtrl+Q" ; | |
198 | } | |
199 | } | |
200 | ||
201 | wxMenuItem::~wxMenuItem() | |
202 | { | |
203 | } | |
204 | ||
205 | bool wxMenuItem::IsChecked() const | |
206 | { | |
207 | return wxMenuItemBase::IsChecked() ; | |
208 | } | |
209 | ||
210 | wxString wxMenuItem::GetLabel() const | |
211 | { | |
212 | return wxStripMenuCodes(m_text); | |
213 | } | |
214 | ||
215 | // accelerators | |
216 | // ------------ | |
217 | ||
218 | #if wxUSE_ACCEL | |
219 | ||
220 | wxAcceleratorEntry *wxMenuItem::GetAccel() const | |
221 | { | |
222 | return wxGetAccelFromString(GetText()); | |
223 | } | |
224 | ||
225 | #endif // wxUSE_ACCEL | |
226 | ||
227 | // misc | |
228 | // ---- | |
229 | ||
230 | /* | |
231 | ||
232 | // delete the sub menu | |
233 | void wxMenuItem::DeleteSubMenu() | |
234 | { | |
235 | wxASSERT( m_subMenu != NULL ); | |
236 | ||
237 | delete m_subMenu; | |
238 | m_subMenu = NULL; | |
239 | } | |
240 | ||
241 | */ | |
242 | ||
243 | // change item state | |
244 | // ----------------- | |
245 | ||
246 | void wxMenuItem::Enable(bool bDoEnable) | |
247 | { | |
248 | if ( m_isEnabled != bDoEnable ) { | |
249 | if ( m_subMenu == NULL ) | |
250 | { | |
251 | // normal menu item | |
252 | if ( m_parentMenu->GetHMenu() ) | |
253 | { | |
254 | int index = m_parentMenu->MacGetIndexFromItem( this ) ; | |
255 | if ( index >= 1 ) | |
256 | { | |
257 | if ( bDoEnable ) | |
258 | UMAEnableMenuItem( m_parentMenu->GetHMenu() , index ) ; | |
259 | else | |
260 | UMADisableMenuItem( m_parentMenu->GetHMenu() , index ) ; | |
261 | } | |
262 | } | |
263 | } | |
264 | else | |
265 | { | |
266 | // submenu | |
267 | if ( m_parentMenu->GetHMenu() ) | |
268 | { | |
269 | int index = m_parentMenu->MacGetIndexFromItem( this ) ; | |
270 | if ( index >= 1 ) | |
271 | { | |
272 | if ( bDoEnable ) | |
273 | UMAEnableMenuItem( m_parentMenu->GetHMenu() , index ) ; | |
274 | else | |
275 | UMADisableMenuItem( m_parentMenu->GetHMenu() , index ) ; | |
276 | } | |
277 | } | |
278 | } | |
279 | ||
280 | m_isEnabled = bDoEnable; | |
281 | } | |
282 | } | |
283 | ||
284 | void wxMenuItem::Check(bool bDoCheck) | |
285 | { | |
286 | wxCHECK_RET( IsCheckable(), "only checkable items may be checked" ); | |
287 | ||
288 | if ( m_isChecked != bDoCheck ) | |
289 | { | |
290 | m_isChecked = bDoCheck; | |
291 | if ( m_parentMenu->GetHMenu() ) | |
292 | { | |
293 | int index = m_parentMenu->MacGetIndexFromItem( this ) ; | |
294 | if ( index >= 1 ) | |
295 | { | |
296 | if ( bDoCheck ) | |
297 | ::SetItemMark( m_parentMenu->GetHMenu() , index , 0x12 ) ; // checkmark | |
298 | else | |
299 | ::SetItemMark( m_parentMenu->GetHMenu() , index , 0 ) ; // no mark | |
300 | } | |
301 | } | |
302 | } | |
303 | } | |
304 | ||
305 | void wxMenuItem::SetText(const wxString& text) | |
306 | { | |
307 | // don't do anything if label didn't change | |
308 | if ( m_text == text ) | |
309 | return; | |
310 | ||
311 | wxMenuItemBase::SetText(text); | |
312 | // OWNER_DRAWN_ONLY( wxOwnerDrawn::SetName(text) ); | |
313 | ||
314 | wxCHECK_RET( m_parentMenu && m_parentMenu->GetHMenu(), wxT("menuitem without menu") ); | |
315 | if ( m_parentMenu->GetHMenu() ) | |
316 | { | |
317 | int index = m_parentMenu->MacGetIndexFromItem( this ) ; | |
318 | if ( index >= 1 ) | |
319 | { | |
320 | Str255 label; | |
321 | MacBuildMenuString( label , NULL , NULL , text ,false); | |
322 | ::SetMenuItemText( m_parentMenu->GetHMenu() , index , label ) ; // checkmark | |
323 | } | |
324 | } | |
325 | ||
326 | #if wxUSE_ACCEL | |
327 | m_parentMenu->UpdateAccel(this); | |
328 | #endif // wxUSE_ACCEL | |
329 | ||
330 | } | |
331 | void wxMenuItem::SetCheckable(bool checkable) | |
332 | { | |
333 | wxMenuItemBase::SetCheckable(checkable); | |
334 | // OWNER_DRAWN_ONLY( wxOwnerDrawn::SetCheckable(checkable) ); | |
335 | } | |
336 | ||
337 | // ---------------------------------------------------------------------------- | |
338 | // wxMenuItemBase | |
339 | // ---------------------------------------------------------------------------- | |
340 | ||
341 | /* static */ | |
342 | wxString wxMenuItemBase::GetLabelFromText(const wxString& text) | |
343 | { | |
344 | return wxStripMenuCodes(text); | |
345 | } | |
346 | ||
347 | wxMenuItem *wxMenuItemBase::New(wxMenu *parentMenu, | |
348 | int id, | |
349 | const wxString& name, | |
350 | const wxString& help, | |
351 | bool isCheckable, | |
352 | wxMenu *subMenu) | |
353 | { | |
354 | return new wxMenuItem(parentMenu, id, name, help, isCheckable, subMenu); | |
355 | } |