]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: menu.cpp | |
3 | // Purpose: wxMenu, wxMenuBar, wxMenuItem | |
4 | // Author: Julian Smart | |
5 | // Modified by: Vadim Zeitlin | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
c626a8b7 | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
c2dcfdef VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
c626a8b7 | 21 | #pragma implementation "menu.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
c626a8b7 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
c626a8b7 VZ |
32 | #include "wx/frame.h" |
33 | #include "wx/menu.h" | |
34 | #include "wx/utils.h" | |
0c589ad0 | 35 | #include "wx/intl.h" |
2bda0e17 KB |
36 | #endif |
37 | ||
47d67540 | 38 | #if wxUSE_OWNER_DRAWN |
c626a8b7 | 39 | #include "wx/ownerdrw.h" |
2bda0e17 KB |
40 | #endif |
41 | ||
42 | #include "wx/msw/private.h" | |
43 | #include "wx/msw/menu.h" | |
44 | #include "wx/menuitem.h" | |
45 | #include "wx/log.h" | |
46 | ||
47 | // other standard headers | |
2bda0e17 KB |
48 | #include <string.h> |
49 | ||
c626a8b7 VZ |
50 | // ---------------------------------------------------------------------------- |
51 | // global variables | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | extern wxMenu *wxCurrentPopupMenu; | |
55 | ||
b8d3a4f1 VZ |
56 | // ---------------------------------------------------------------------------- |
57 | // constants | |
58 | // ---------------------------------------------------------------------------- | |
59 | ||
60 | // the (popup) menu title has this special id | |
61 | static const int idMenuTitle = -2; | |
62 | ||
63 | // ---------------------------------------------------------------------------- | |
c626a8b7 | 64 | // macros |
b8d3a4f1 | 65 | // ---------------------------------------------------------------------------- |
c626a8b7 | 66 | |
2bda0e17 | 67 | #if !USE_SHARED_LIBRARY |
c626a8b7 VZ |
68 | IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) |
69 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) | |
2bda0e17 KB |
70 | #endif |
71 | ||
72 | // ============================================================================ | |
73 | // implementation | |
74 | // ============================================================================ | |
75 | ||
c2dcfdef VZ |
76 | // --------------------------------------------------------------------------- |
77 | // wxMenu construction, adding and removing menu items | |
78 | // --------------------------------------------------------------------------- | |
2bda0e17 KB |
79 | |
80 | // Construct a menu with optional title (then use append) | |
b908d224 VZ |
81 | void wxMenu::Init(const wxString& title |
82 | #ifdef WXWIN_COMPATIBILITY | |
83 | , const wxFunction func | |
84 | #endif | |
85 | ) | |
c626a8b7 | 86 | { |
b908d224 | 87 | m_title = title; |
c626a8b7 VZ |
88 | m_parent = NULL; |
89 | m_eventHandler = this; | |
90 | m_pInvokingWindow = NULL; | |
91 | m_doBreak = FALSE ; | |
92 | m_noItems = 0; | |
93 | m_menuBar = NULL; | |
94 | m_hMenu = (WXHMENU) CreatePopupMenu(); | |
95 | m_savehMenu = 0 ; | |
96 | m_topLevelMenu = this; | |
97 | m_clientData = (void*) NULL; | |
98 | ||
99 | if ( !!m_title ) | |
100 | { | |
101 | Append(idMenuTitle, m_title) ; | |
102 | AppendSeparator() ; | |
103 | } | |
2bda0e17 | 104 | |
c2dcfdef | 105 | #if WXWIN_COMPATIBILITY |
c626a8b7 | 106 | Callback(func); |
c2dcfdef | 107 | #endif |
2bda0e17 KB |
108 | } |
109 | ||
110 | // The wxWindow destructor will take care of deleting the submenus. | |
b8d3a4f1 | 111 | wxMenu::~wxMenu() |
2bda0e17 | 112 | { |
c2dcfdef VZ |
113 | // free Windows resources |
114 | if ( m_hMenu ) | |
115 | { | |
116 | ::DestroyMenu((HMENU)m_hMenu); | |
117 | m_hMenu = 0; | |
118 | } | |
c626a8b7 | 119 | |
c2dcfdef | 120 | // delete submenus |
c626a8b7 | 121 | wxNode *node = m_menuItems.First(); |
c2dcfdef | 122 | while ( node ) |
c626a8b7 VZ |
123 | { |
124 | wxMenuItem *item = (wxMenuItem *)node->Data(); | |
125 | ||
126 | // Delete child menus. | |
127 | // Beware: they must not be appended to children list!!! | |
128 | // (because order of delete is significant) | |
c2dcfdef | 129 | if ( item->IsSubMenu() ) |
c626a8b7 VZ |
130 | item->DeleteSubMenu(); |
131 | ||
132 | wxNode *next = node->Next(); | |
133 | delete item; | |
134 | delete node; | |
135 | node = next; | |
136 | } | |
2bda0e17 KB |
137 | } |
138 | ||
b8d3a4f1 | 139 | void wxMenu::Break() |
2bda0e17 | 140 | { |
c2dcfdef | 141 | m_doBreak = TRUE; |
2bda0e17 KB |
142 | } |
143 | ||
144 | // function appends a new item or submenu to the menu | |
145 | void wxMenu::Append(wxMenuItem *pItem) | |
146 | { | |
837e5743 | 147 | wxCHECK_RET( pItem != NULL, _T("can't append NULL item to the menu") ); |
2bda0e17 | 148 | |
d427503c | 149 | #if wxUSE_ACCEL |
42e69d6b VZ |
150 | // check for accelerators: they are given after '\t' |
151 | wxString label = pItem->GetName(); | |
837e5743 | 152 | int posTab = label.Find(_T('\t')); |
42e69d6b VZ |
153 | if ( posTab != wxNOT_FOUND ) { |
154 | // parse the accelerator string | |
155 | int keyCode = 0; | |
156 | int accelFlags = wxACCEL_NORMAL; | |
157 | wxString current; | |
158 | for ( size_t n = (size_t)posTab + 1; n < label.Len(); n++ ) { | |
159 | if ( (label[n] == '+') || (label[n] == '-') ) { | |
160 | if ( current == _("ctrl") ) | |
161 | accelFlags |= wxACCEL_CTRL; | |
162 | else if ( current == _("alt") ) | |
163 | accelFlags |= wxACCEL_ALT; | |
164 | else if ( current == _("shift") ) | |
165 | accelFlags |= wxACCEL_SHIFT; | |
166 | else { | |
167 | wxLogDebug(_T("Unknown accel modifier: '%s'"), | |
168 | current.c_str()); | |
169 | } | |
170 | ||
171 | current.Empty(); | |
172 | } | |
173 | else { | |
174 | current += wxTolower(label[n]); | |
175 | } | |
176 | } | |
177 | ||
178 | if ( current.IsEmpty() ) { | |
179 | wxLogDebug(_T("No accel key found, accel string ignored.")); | |
180 | } | |
181 | else { | |
182 | if ( current.Len() == 1 ) { | |
183 | // it's a letter | |
9f104fbf | 184 | keyCode = wxToupper(current[0U]); |
42e69d6b VZ |
185 | } |
186 | else { | |
187 | // it should be a function key | |
9f104fbf | 188 | if ( current[0U] == 'f' && isdigit(current[1U]) && |
42e69d6b | 189 | (current.Len() == 2 || |
9f104fbf | 190 | (current.Len() == 3 && isdigit(current[2U]))) ) { |
42e69d6b | 191 | int n; |
837e5743 | 192 | wxSscanf(current.c_str() + 1, _T("%d"), &n); |
42e69d6b VZ |
193 | |
194 | keyCode = VK_F1 + n - 1; | |
195 | } | |
196 | else { | |
de5ae7c6 | 197 | wxLogDebug(_T("Unrecognized accel key '%s', accel " |
42e69d6b VZ |
198 | "string ignored."), current.c_str()); |
199 | } | |
200 | } | |
201 | } | |
202 | ||
203 | if ( keyCode ) { | |
204 | // do add an entry | |
205 | m_accelKeyCodes.Add(keyCode); | |
206 | m_accelFlags.Add(accelFlags); | |
207 | m_accelIds.Add(pItem->GetId()); | |
208 | } | |
209 | } | |
d427503c | 210 | #endif // wxUSE_ACCEL |
42e69d6b | 211 | |
c626a8b7 | 212 | UINT flags = 0; |
2bda0e17 | 213 | |
c2dcfdef VZ |
214 | // if "Break" has just been called, insert a menu break before this item |
215 | // (and don't forget to reset the flag) | |
c626a8b7 VZ |
216 | if ( m_doBreak ) { |
217 | flags |= MF_MENUBREAK; | |
218 | m_doBreak = FALSE; | |
219 | } | |
220 | ||
221 | if ( pItem->IsSeparator() ) { | |
222 | flags |= MF_SEPARATOR; | |
223 | } | |
2bda0e17 | 224 | |
c2dcfdef VZ |
225 | // id is the numeric id for normal menu items and HMENU for submenus as |
226 | // required by ::AppendMenu() API | |
c626a8b7 | 227 | UINT id; |
c2dcfdef VZ |
228 | wxMenu *submenu = pItem->GetSubMenu(); |
229 | if ( submenu != NULL ) { | |
230 | wxASSERT( submenu->GetHMenu() != (WXHMENU) NULL ); | |
2bda0e17 | 231 | |
c2dcfdef VZ |
232 | id = (UINT)submenu->GetHMenu(); |
233 | submenu->m_topLevelMenu = m_topLevelMenu; | |
234 | submenu->m_parent = this; | |
235 | submenu->m_savehMenu = (WXHMENU)id; | |
236 | submenu->m_hMenu = 0; | |
2bda0e17 | 237 | |
c626a8b7 VZ |
238 | flags |= MF_POPUP; |
239 | } | |
240 | else { | |
241 | id = pItem->GetId(); | |
242 | } | |
2bda0e17 | 243 | |
837e5743 | 244 | LPCTSTR pData; |
2bda0e17 | 245 | |
47d67540 | 246 | #if wxUSE_OWNER_DRAWN |
c626a8b7 VZ |
247 | if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages? |
248 | // item draws itself, pass pointer to it in data parameter | |
249 | flags |= MF_OWNERDRAW; | |
837e5743 | 250 | pData = (LPCTSTR)pItem; |
c626a8b7 VZ |
251 | } |
252 | else | |
2bda0e17 | 253 | #endif |
c626a8b7 VZ |
254 | { |
255 | // menu is just a normal string (passed in data parameter) | |
256 | flags |= MF_STRING; | |
42e69d6b | 257 | pData = label; |
c626a8b7 | 258 | } |
2bda0e17 | 259 | |
c50f1fb9 | 260 | if ( !::AppendMenu(GetHmenu(), flags, id, pData) ) |
c626a8b7 VZ |
261 | { |
262 | wxLogLastError("AppendMenu"); | |
263 | } | |
c2dcfdef VZ |
264 | else |
265 | { | |
42e69d6b VZ |
266 | #ifdef __WIN32__ |
267 | if ( id == idMenuTitle ) | |
268 | { | |
269 | // visually select the menu title | |
270 | MENUITEMINFO mii; | |
271 | mii.cbSize = sizeof(mii); | |
272 | mii.fMask = MIIM_STATE; | |
273 | mii.fState = MFS_DEFAULT; | |
274 | ||
c50f1fb9 | 275 | if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id, FALSE, &mii) ) |
42e69d6b | 276 | { |
837e5743 | 277 | wxLogLastError(_T("SetMenuItemInfo")); |
42e69d6b VZ |
278 | } |
279 | } | |
280 | #endif // __WIN32__ | |
281 | ||
c2dcfdef VZ |
282 | m_menuItems.Append(pItem); |
283 | m_noItems++; | |
284 | } | |
2bda0e17 KB |
285 | } |
286 | ||
b8d3a4f1 | 287 | void wxMenu::AppendSeparator() |
2bda0e17 | 288 | { |
c626a8b7 | 289 | Append(new wxMenuItem(this, ID_SEPARATOR)); |
2bda0e17 KB |
290 | } |
291 | ||
292 | // Pullright item | |
c2dcfdef VZ |
293 | void wxMenu::Append(int id, |
294 | const wxString& label, | |
295 | wxMenu *SubMenu, | |
296 | const wxString& helpString) | |
2bda0e17 | 297 | { |
8cd85069 | 298 | Append(new wxMenuItem(this, id, label, helpString, FALSE, SubMenu)); |
2bda0e17 KB |
299 | } |
300 | ||
301 | // Ordinary menu item | |
c2dcfdef VZ |
302 | void wxMenu::Append(int id, |
303 | const wxString& label, | |
304 | const wxString& helpString, | |
305 | bool checkable) | |
2bda0e17 | 306 | { |
c626a8b7 | 307 | // 'checkable' parameter is useless for Windows. |
8cd85069 | 308 | Append(new wxMenuItem(this, id, label, helpString, checkable)); |
2bda0e17 KB |
309 | } |
310 | ||
c2dcfdef | 311 | // delete item by id |
2bda0e17 KB |
312 | void wxMenu::Delete(int id) |
313 | { | |
c626a8b7 VZ |
314 | wxMenuItem *item = NULL; |
315 | int pos; | |
316 | wxNode *node; | |
317 | for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++) | |
318 | { | |
319 | item = (wxMenuItem *)node->Data(); | |
320 | if ( item->GetId() == id ) | |
321 | break; | |
322 | } | |
323 | ||
837e5743 | 324 | wxCHECK_RET( node, _T("wxMenu::Delete(): item doesn't exist") ); |
c626a8b7 | 325 | |
c50f1fb9 | 326 | HMENU menu = GetHmenu(); |
c626a8b7 VZ |
327 | |
328 | wxMenu *pSubMenu = item->GetSubMenu(); | |
329 | if ( pSubMenu != NULL ) { | |
330 | RemoveMenu(menu, (UINT)pos, MF_BYPOSITION); | |
331 | pSubMenu->m_hMenu = pSubMenu->m_savehMenu; | |
332 | pSubMenu->m_savehMenu = 0; | |
333 | pSubMenu->m_parent = NULL; | |
334 | // RemoveChild(item->subMenu); | |
335 | pSubMenu->m_topLevelMenu = NULL; | |
336 | // TODO: Why isn't subMenu deleted here??? | |
337 | // Will put this in for now. Assuming this is supposed | |
338 | // to delete the menu, not just remove it. | |
339 | item->DeleteSubMenu(); | |
340 | } | |
341 | else { | |
342 | DeleteMenu(menu, (UINT)pos, MF_BYPOSITION); | |
343 | } | |
344 | ||
345 | m_menuItems.DeleteNode(node); | |
346 | delete item; | |
2bda0e17 KB |
347 | } |
348 | ||
d427503c VZ |
349 | #if wxUSE_ACCEL |
350 | ||
42e69d6b VZ |
351 | // --------------------------------------------------------------------------- |
352 | // accelerator helpers | |
353 | // --------------------------------------------------------------------------- | |
354 | ||
355 | // create the wxAcceleratorEntries for our accels and put them into provided | |
356 | // array - return the number of accels we have | |
357 | size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const | |
358 | { | |
359 | size_t count = GetAccelCount(); | |
360 | for ( size_t n = 0; n < count; n++ ) | |
361 | { | |
362 | (*accels++).Set(m_accelFlags[n], m_accelKeyCodes[n], m_accelIds[n]); | |
363 | } | |
364 | ||
365 | return count; | |
366 | } | |
367 | ||
d427503c VZ |
368 | #endif // wxUSE_ACCEL |
369 | ||
c2dcfdef VZ |
370 | // --------------------------------------------------------------------------- |
371 | // wxMenu functions implemented in wxMenuItem | |
372 | // --------------------------------------------------------------------------- | |
373 | ||
8cd85069 | 374 | void wxMenu::Enable(int id, bool Flag) |
2bda0e17 | 375 | { |
8cd85069 | 376 | wxMenuItem *item = FindItemForId(id); |
837e5743 | 377 | wxCHECK_RET( item != NULL, _T("can't enable non-existing menu item") ); |
2bda0e17 | 378 | |
c626a8b7 | 379 | item->Enable(Flag); |
2bda0e17 KB |
380 | } |
381 | ||
8cd85069 | 382 | bool wxMenu::IsEnabled(int id) const |
2bda0e17 | 383 | { |
8cd85069 | 384 | wxMenuItem *item = FindItemForId(id); |
837e5743 | 385 | wxCHECK_MSG( item != NULL, FALSE, _T("invalid item id") ); |
2bda0e17 | 386 | |
c626a8b7 | 387 | return item->IsEnabled(); |
2bda0e17 KB |
388 | } |
389 | ||
8cd85069 | 390 | void wxMenu::Check(int id, bool Flag) |
2bda0e17 | 391 | { |
8cd85069 | 392 | wxMenuItem *item = FindItemForId(id); |
837e5743 | 393 | wxCHECK_RET( item != NULL, _T("can't get status of non-existing menu item") ); |
2bda0e17 | 394 | |
c626a8b7 | 395 | item->Check(Flag); |
2bda0e17 KB |
396 | } |
397 | ||
8cd85069 | 398 | bool wxMenu::IsChecked(int id) const |
2bda0e17 | 399 | { |
8cd85069 | 400 | wxMenuItem *item = FindItemForId(id); |
837e5743 | 401 | wxCHECK_MSG( item != NULL, FALSE, _T("invalid item id") ); |
2bda0e17 | 402 | |
c626a8b7 | 403 | return item->IsChecked(); |
2bda0e17 KB |
404 | } |
405 | ||
c2dcfdef VZ |
406 | void wxMenu::SetLabel(int id, const wxString& label) |
407 | { | |
408 | wxMenuItem *item = FindItemForId(id) ; | |
837e5743 | 409 | wxCHECK_RET( item, _T("wxMenu::SetLabel: no such item") ); |
c2dcfdef VZ |
410 | |
411 | item->SetName(label); | |
412 | } | |
413 | ||
414 | wxString wxMenu::GetLabel(int id) const | |
415 | { | |
416 | wxString label; | |
417 | wxMenuItem *pItem = FindItemForId(id) ; | |
418 | if (pItem) | |
419 | label = pItem->GetName() ; | |
420 | else | |
837e5743 | 421 | wxFAIL_MSG(_T("wxMenu::GetLabel: item doesn't exist")); |
c2dcfdef VZ |
422 | |
423 | return label; | |
424 | } | |
425 | ||
426 | void wxMenu::SetHelpString(int itemId, const wxString& helpString) | |
427 | { | |
428 | wxMenuItem *item = FindItemForId (itemId); | |
429 | if (item) | |
430 | item->SetHelp(helpString); | |
431 | else | |
837e5743 | 432 | wxFAIL_MSG(_T("wxMenu::SetHelpString: item doesn't exist")); |
c2dcfdef VZ |
433 | } |
434 | ||
435 | wxString wxMenu::GetHelpString (int itemId) const | |
436 | { | |
437 | wxString help; | |
438 | wxMenuItem *item = FindItemForId (itemId); | |
439 | if (item) | |
440 | help = item->GetHelp(); | |
441 | else | |
837e5743 | 442 | wxFAIL_MSG(_T("wxMenu::GetHelpString: item doesn't exist")); |
c2dcfdef VZ |
443 | |
444 | return help; | |
445 | } | |
446 | ||
447 | // --------------------------------------------------------------------------- | |
448 | // wxMenu title | |
449 | // --------------------------------------------------------------------------- | |
450 | ||
2bda0e17 KB |
451 | void wxMenu::SetTitle(const wxString& label) |
452 | { | |
c626a8b7 VZ |
453 | bool hasNoTitle = m_title.IsEmpty(); |
454 | m_title = label; | |
b8d3a4f1 | 455 | |
c50f1fb9 | 456 | HMENU hMenu = GetHmenu(); |
b8d3a4f1 | 457 | |
c626a8b7 | 458 | if ( hasNoTitle ) |
b8d3a4f1 | 459 | { |
c626a8b7 VZ |
460 | if ( !label.IsEmpty() ) |
461 | { | |
462 | if ( !InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING, | |
463 | (unsigned)idMenuTitle, m_title) || | |
464 | !InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) ) | |
465 | { | |
837e5743 | 466 | wxLogLastError(_T("InsertMenu")); |
c626a8b7 VZ |
467 | } |
468 | } | |
b8d3a4f1 VZ |
469 | } |
470 | else | |
471 | { | |
c626a8b7 VZ |
472 | if ( label.IsEmpty() ) |
473 | { | |
474 | // remove the title and the separator after it | |
475 | if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) || | |
476 | !RemoveMenu(hMenu, 0, MF_BYPOSITION) ) | |
477 | { | |
478 | wxLogLastError("RemoveMenu"); | |
479 | } | |
480 | } | |
481 | else | |
482 | { | |
483 | // modify the title | |
484 | if ( !ModifyMenu(hMenu, 0u, | |
485 | MF_BYPOSITION | MF_STRING, | |
486 | (unsigned)idMenuTitle, m_title) ) | |
487 | { | |
488 | wxLogLastError("ModifyMenu"); | |
489 | } | |
490 | } | |
b8d3a4f1 | 491 | } |
b8d3a4f1 | 492 | |
42e69d6b | 493 | #ifdef __WIN32__ |
c626a8b7 VZ |
494 | // put the title string in bold face |
495 | if ( !m_title.IsEmpty() ) | |
a3f4e9e8 | 496 | { |
c626a8b7 VZ |
497 | MENUITEMINFO mii; |
498 | mii.cbSize = sizeof(mii); | |
499 | mii.fMask = MIIM_STATE; | |
500 | mii.fState = MFS_DEFAULT; | |
501 | ||
502 | if ( !SetMenuItemInfo(hMenu, (unsigned)idMenuTitle, FALSE, &mii) ) | |
503 | { | |
504 | wxLogLastError("SetMenuItemInfo"); | |
505 | } | |
a3f4e9e8 | 506 | } |
750b78ba | 507 | #endif |
2bda0e17 KB |
508 | } |
509 | ||
f7387de5 | 510 | const wxString wxMenu::GetTitle() const |
2bda0e17 | 511 | { |
c626a8b7 | 512 | return m_title; |
2bda0e17 KB |
513 | } |
514 | ||
c2dcfdef VZ |
515 | // --------------------------------------------------------------------------- |
516 | // event processing | |
517 | // --------------------------------------------------------------------------- | |
2bda0e17 | 518 | |
debe6624 | 519 | bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id) |
2bda0e17 | 520 | { |
a3f4e9e8 VZ |
521 | // ignore commands from the menu title |
522 | ||
523 | // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!! | |
524 | if ( id != (WXWORD)idMenuTitle ) | |
525 | { | |
526 | wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED); | |
527 | event.SetEventObject( this ); | |
528 | event.SetId( id ); | |
529 | event.SetInt( id ); | |
530 | ProcessCommand(event); | |
531 | } | |
532 | ||
533 | return TRUE; | |
2bda0e17 KB |
534 | } |
535 | ||
42e69d6b | 536 | bool wxMenu::ProcessCommand(wxCommandEvent & event) |
c2dcfdef VZ |
537 | { |
538 | bool processed = FALSE; | |
539 | ||
092fdc46 | 540 | #if WXWIN_COMPATIBILITY |
c2dcfdef VZ |
541 | // Try a callback |
542 | if (m_callback) | |
543 | { | |
544 | (void)(*(m_callback))(*this, event); | |
545 | processed = TRUE; | |
546 | } | |
092fdc46 | 547 | #endif // WXWIN_COMPATIBILITY |
c2dcfdef VZ |
548 | |
549 | // Try the menu's event handler | |
550 | if ( !processed && GetEventHandler()) | |
551 | { | |
552 | processed = GetEventHandler()->ProcessEvent(event); | |
553 | } | |
554 | ||
555 | // Try the window the menu was popped up from (and up through the | |
556 | // hierarchy) | |
557 | wxWindow *win = GetInvokingWindow(); | |
558 | if ( !processed && win ) | |
559 | processed = win->GetEventHandler()->ProcessEvent(event); | |
42e69d6b VZ |
560 | |
561 | return processed; | |
c2dcfdef VZ |
562 | } |
563 | ||
564 | // --------------------------------------------------------------------------- | |
565 | // Item search | |
566 | // --------------------------------------------------------------------------- | |
567 | ||
2bda0e17 KB |
568 | // Finds the item id matching the given string, -1 if not found. |
569 | int wxMenu::FindItem (const wxString& itemString) const | |
570 | { | |
c2dcfdef VZ |
571 | wxString itemLabel = wxStripMenuCodes(itemString); |
572 | for ( wxNode *node = m_menuItems.First(); node; node = node->Next() ) | |
2bda0e17 | 573 | { |
c2dcfdef VZ |
574 | wxMenuItem *item = (wxMenuItem *)node->Data(); |
575 | if ( item->IsSubMenu() ) | |
c626a8b7 VZ |
576 | { |
577 | int ans = item->GetSubMenu()->FindItem(itemString); | |
c2dcfdef | 578 | if ( ans != wxNOT_FOUND ) |
c626a8b7 VZ |
579 | return ans; |
580 | } | |
c2dcfdef | 581 | else if ( !item->IsSeparator() ) |
c626a8b7 | 582 | { |
c2dcfdef VZ |
583 | wxString label = wxStripMenuCodes(item->GetName()); |
584 | if ( itemLabel == label ) | |
c626a8b7 VZ |
585 | return item->GetId(); |
586 | } | |
2bda0e17 KB |
587 | } |
588 | ||
c626a8b7 | 589 | return wxNOT_FOUND; |
2bda0e17 KB |
590 | } |
591 | ||
debe6624 | 592 | wxMenuItem *wxMenu::FindItemForId(int itemId, wxMenu ** itemMenu) const |
2bda0e17 | 593 | { |
c2dcfdef | 594 | if ( itemMenu ) |
c626a8b7 | 595 | *itemMenu = NULL; |
c2dcfdef VZ |
596 | |
597 | wxMenuItem *item = NULL; | |
9a48c2ba | 598 | for ( wxNode *node = m_menuItems.First(); node && !item; node = node->Next() ) |
2bda0e17 | 599 | { |
c2dcfdef | 600 | item = (wxMenuItem *)node->Data(); |
c626a8b7 | 601 | |
c2dcfdef | 602 | if ( item->GetId() == itemId ) |
c626a8b7 VZ |
603 | { |
604 | if (itemMenu) | |
c2dcfdef | 605 | *itemMenu = (wxMenu *)this; |
c626a8b7 | 606 | } |
c2dcfdef | 607 | else if ( item->IsSubMenu() ) |
c626a8b7 | 608 | { |
c2dcfdef | 609 | item = item->GetSubMenu()->FindItemForId(itemId, itemMenu); |
9a48c2ba VZ |
610 | } |
611 | else | |
612 | { | |
613 | // don't exit the loop | |
614 | item = NULL; | |
c626a8b7 | 615 | } |
2bda0e17 KB |
616 | } |
617 | ||
c2dcfdef | 618 | return item; |
2bda0e17 KB |
619 | } |
620 | ||
c2dcfdef VZ |
621 | // --------------------------------------------------------------------------- |
622 | // other | |
623 | // --------------------------------------------------------------------------- | |
2bda0e17 | 624 | |
c2dcfdef VZ |
625 | void wxMenu::Attach(wxMenuBar *menubar) |
626 | { | |
627 | // menu can be in at most one menubar because otherwise they would both | |
628 | // delete the menu pointer | |
837e5743 | 629 | wxASSERT_MSG( !m_menuBar, _T("menu belongs to 2 menubars, expect a crash") ); |
c2dcfdef VZ |
630 | |
631 | m_menuBar = menubar; | |
632 | m_savehMenu = m_hMenu; | |
633 | m_hMenu = 0; | |
634 | } | |
635 | ||
636 | void wxMenu::Detach() | |
637 | { | |
837e5743 | 638 | wxASSERT_MSG( m_menuBar, _T("can't detach menu if it's not attached") ); |
c2dcfdef VZ |
639 | |
640 | m_hMenu = m_savehMenu; | |
641 | m_savehMenu = 0; | |
642 | } | |
643 | ||
644 | // --------------------------------------------------------------------------- | |
2bda0e17 | 645 | // Menu Bar |
c2dcfdef VZ |
646 | // --------------------------------------------------------------------------- |
647 | ||
648 | void wxMenuBar::Init() | |
2bda0e17 | 649 | { |
c626a8b7 VZ |
650 | m_eventHandler = this; |
651 | m_menuCount = 0; | |
652 | m_menus = NULL; | |
653 | m_titles = NULL; | |
654 | m_menuBarFrame = NULL; | |
655 | m_hMenu = 0; | |
cba2db0c | 656 | } |
2bda0e17 | 657 | |
c2dcfdef VZ |
658 | wxMenuBar::wxMenuBar() |
659 | { | |
660 | Init(); | |
661 | } | |
662 | ||
cba2db0c JS |
663 | wxMenuBar::wxMenuBar( long WXUNUSED(style) ) |
664 | { | |
c2dcfdef | 665 | Init(); |
2bda0e17 KB |
666 | } |
667 | ||
c2dcfdef | 668 | wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[]) |
2bda0e17 | 669 | { |
c2dcfdef VZ |
670 | Init(); |
671 | ||
672 | m_menuCount = count; | |
673 | m_menus = menus; | |
674 | m_titles = new wxString[count]; | |
675 | ||
c626a8b7 | 676 | int i; |
c2dcfdef VZ |
677 | for ( i = 0; i < count; i++ ) |
678 | m_titles[i] = titles[i]; | |
2bda0e17 | 679 | |
c2dcfdef VZ |
680 | for ( i = 0; i < count; i++ ) |
681 | m_menus[i]->Attach(this); | |
2bda0e17 KB |
682 | } |
683 | ||
b8d3a4f1 | 684 | wxMenuBar::~wxMenuBar() |
2bda0e17 | 685 | { |
c2dcfdef VZ |
686 | for ( int i = 0; i < m_menuCount; i++ ) |
687 | { | |
688 | delete m_menus[i]; | |
689 | } | |
2bda0e17 | 690 | |
c2dcfdef VZ |
691 | delete[] m_menus; |
692 | delete[] m_titles; | |
693 | } | |
2bda0e17 | 694 | |
c2dcfdef VZ |
695 | // --------------------------------------------------------------------------- |
696 | // wxMenuBar helpers | |
697 | // --------------------------------------------------------------------------- | |
698 | ||
699 | void wxMenuBar::Refresh() | |
700 | { | |
837e5743 | 701 | wxCHECK_RET( m_menuBarFrame, _T("can't refresh a menubar withotu a frame") ); |
c2dcfdef VZ |
702 | |
703 | DrawMenuBar((HWND)m_menuBarFrame->GetHWND()) ; | |
704 | } | |
705 | ||
706 | WXHMENU wxMenuBar::Create() | |
707 | { | |
837e5743 | 708 | wxCHECK_MSG( !m_hMenu, TRUE, _T("menubar already created") ); |
c2dcfdef VZ |
709 | |
710 | m_hMenu = (WXHMENU)::CreateMenu(); | |
2bda0e17 | 711 | |
c2dcfdef | 712 | if ( !m_hMenu ) |
c626a8b7 | 713 | { |
c2dcfdef | 714 | wxLogLastError("CreateMenu"); |
c626a8b7 | 715 | } |
c2dcfdef | 716 | else |
c626a8b7 | 717 | { |
c2dcfdef VZ |
718 | for ( int i = 0; i < m_menuCount; i++ ) |
719 | { | |
720 | if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING, | |
721 | (UINT)m_menus[i]->GetHMenu(), | |
722 | m_titles[i]) ) | |
723 | { | |
724 | wxLogLastError("AppendMenu"); | |
725 | } | |
726 | } | |
c626a8b7 | 727 | } |
c626a8b7 | 728 | |
c2dcfdef | 729 | return m_hMenu; |
2bda0e17 KB |
730 | } |
731 | ||
c2dcfdef VZ |
732 | // --------------------------------------------------------------------------- |
733 | // wxMenuBar functions forwarded to wxMenuItem | |
734 | // --------------------------------------------------------------------------- | |
735 | ||
2bda0e17 KB |
736 | // Must only be used AFTER menu has been attached to frame, |
737 | // otherwise use individual menus to enable/disable items | |
8cd85069 | 738 | void wxMenuBar::Enable(int id, bool enable) |
2bda0e17 | 739 | { |
c626a8b7 | 740 | wxMenu *itemMenu = NULL; |
8cd85069 | 741 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; |
2bda0e17 | 742 | |
837e5743 | 743 | wxCHECK_RET( item, _T("attempt to enable an item which doesn't exist") ); |
2bda0e17 | 744 | |
c2dcfdef | 745 | item->Enable(enable); |
2bda0e17 KB |
746 | } |
747 | ||
c626a8b7 | 748 | void wxMenuBar::EnableTop(int pos, bool enable) |
2bda0e17 | 749 | { |
c626a8b7 | 750 | int flag = enable ? MF_ENABLED : MF_GRAYED;; |
2bda0e17 | 751 | |
c626a8b7 | 752 | EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag); |
2bda0e17 KB |
753 | } |
754 | ||
755 | // Must only be used AFTER menu has been attached to frame, | |
756 | // otherwise use individual menus | |
8cd85069 | 757 | void wxMenuBar::Check(int id, bool check) |
c626a8b7 VZ |
758 | { |
759 | wxMenu *itemMenu = NULL; | |
8cd85069 | 760 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; |
c626a8b7 | 761 | |
837e5743 OK |
762 | wxCHECK_RET( item, _T("attempt to check an item which doesn't exist") ); |
763 | wxCHECK_RET( item->IsCheckable(), _T("attempt to check an uncheckable item") ); | |
2bda0e17 | 764 | |
c2dcfdef | 765 | item->Check(check); |
c626a8b7 VZ |
766 | } |
767 | ||
8cd85069 | 768 | bool wxMenuBar::IsChecked(int id) const |
c626a8b7 VZ |
769 | { |
770 | wxMenu *itemMenu = NULL; | |
8cd85069 | 771 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; |
2bda0e17 | 772 | |
837e5743 | 773 | wxCHECK_MSG( item, FALSE, _T("wxMenuBar::IsChecked(): no such item") ); |
2bda0e17 | 774 | |
c50f1fb9 | 775 | int flag = ::GetMenuState(GetHmenuOf(itemMenu), id, MF_BYCOMMAND); |
2bda0e17 | 776 | |
c626a8b7 | 777 | return (flag & MF_CHECKED) != 0; |
2bda0e17 KB |
778 | } |
779 | ||
8cd85069 | 780 | bool wxMenuBar::IsEnabled(int id) const |
2bda0e17 | 781 | { |
c626a8b7 | 782 | wxMenu *itemMenu = NULL; |
8cd85069 | 783 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; |
2bda0e17 | 784 | |
837e5743 | 785 | wxCHECK_MSG( item, FALSE, _T("wxMenuBar::IsEnabled(): no such item") ); |
2bda0e17 | 786 | |
c50f1fb9 | 787 | int flag = ::GetMenuState(GetHmenuOf(itemMenu), id, MF_BYCOMMAND) ; |
2bda0e17 | 788 | |
c626a8b7 | 789 | return (flag & MF_ENABLED) != 0; |
2bda0e17 KB |
790 | } |
791 | ||
8cd85069 | 792 | void wxMenuBar::SetLabel(int id, const wxString& label) |
2bda0e17 | 793 | { |
c626a8b7 | 794 | wxMenu *itemMenu = NULL; |
8cd85069 | 795 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; |
2bda0e17 | 796 | |
837e5743 | 797 | wxCHECK_RET( item, _T("wxMenuBar::SetLabel(): no such item") ); |
2bda0e17 | 798 | |
c2dcfdef | 799 | item->SetName(label); |
2bda0e17 KB |
800 | } |
801 | ||
8cd85069 | 802 | wxString wxMenuBar::GetLabel(int id) const |
2bda0e17 | 803 | { |
c626a8b7 | 804 | wxMenu *itemMenu = NULL; |
8cd85069 | 805 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; |
2bda0e17 | 806 | |
837e5743 | 807 | wxCHECK_MSG( item, _T(""), _T("wxMenuBar::GetLabel(): no such item") ); |
2bda0e17 | 808 | |
c2dcfdef VZ |
809 | return item->GetName(); |
810 | } | |
8cd85069 | 811 | |
c2dcfdef VZ |
812 | void wxMenuBar::SetHelpString (int id, const wxString& helpString) |
813 | { | |
814 | wxMenu *itemMenu = NULL; | |
815 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
2bda0e17 | 816 | |
837e5743 | 817 | wxCHECK_RET( item, _T("wxMenuBar::SetHelpString(): no such item") ); |
c2dcfdef VZ |
818 | |
819 | item->SetHelp(helpString); | |
2bda0e17 KB |
820 | } |
821 | ||
c2dcfdef VZ |
822 | wxString wxMenuBar::GetHelpString (int id) const |
823 | { | |
824 | wxMenu *itemMenu = NULL; | |
825 | wxMenuItem *item = FindItemForId(id, &itemMenu) ; | |
826 | ||
837e5743 | 827 | wxCHECK_MSG( item, _T(""), _T("wxMenuBar::GetHelpString(): no such item") ); |
c2dcfdef VZ |
828 | |
829 | return item->GetHelp(); | |
830 | } | |
831 | ||
832 | // --------------------------------------------------------------------------- | |
833 | // wxMenuBar functions to work with the top level submenus | |
834 | // --------------------------------------------------------------------------- | |
835 | ||
836 | // NB: we don't support owner drawn top level items for now, if we do these | |
837 | // functions would have to be changed to use wxMenuItem as well | |
838 | ||
debe6624 | 839 | void wxMenuBar::SetLabelTop(int pos, const wxString& label) |
2bda0e17 | 840 | { |
8cd85069 | 841 | UINT id; |
c2dcfdef VZ |
842 | UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION); |
843 | if ( flagsOld == 0xFFFFFFFF ) | |
c626a8b7 | 844 | { |
837e5743 | 845 | wxLogLastError(_T("GetMenuState")); |
c2dcfdef VZ |
846 | |
847 | return; | |
848 | } | |
849 | ||
850 | if ( flagsOld & MF_POPUP ) | |
851 | { | |
852 | // HIBYTE contains the number of items in the submenu in this case | |
853 | flagsOld &= 0xff ; | |
8cd85069 | 854 | id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos) ; |
c626a8b7 VZ |
855 | } |
856 | else | |
8cd85069 VZ |
857 | { |
858 | id = pos; | |
859 | } | |
860 | ||
c50f1fb9 | 861 | if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld, |
c2dcfdef VZ |
862 | id, label) == 0xFFFFFFFF ) |
863 | { | |
864 | wxLogLastError("ModifyMenu"); | |
865 | } | |
2bda0e17 KB |
866 | } |
867 | ||
debe6624 | 868 | wxString wxMenuBar::GetLabelTop(int pos) const |
2bda0e17 | 869 | { |
8cd85069 VZ |
870 | int len = ::GetMenuString((HMENU)m_hMenu, pos, NULL, 0, MF_BYCOMMAND); |
871 | ||
872 | len++; // for the NUL character | |
873 | wxString label; | |
c50f1fb9 | 874 | ::GetMenuString(GetHmenu(), pos, label.GetWriteBuf(len), len, MF_BYCOMMAND); |
8cd85069 VZ |
875 | label.UngetWriteBuf(); |
876 | ||
877 | return label; | |
2bda0e17 KB |
878 | } |
879 | ||
c2dcfdef VZ |
880 | // --------------------------------------------------------------------------- |
881 | // wxMenuBar notifications | |
882 | // --------------------------------------------------------------------------- | |
883 | ||
debe6624 | 884 | bool wxMenuBar::OnDelete(wxMenu *a_menu, int pos) |
2bda0e17 | 885 | { |
c2dcfdef | 886 | if ( !m_menuBarFrame ) |
c626a8b7 | 887 | return TRUE; |
2bda0e17 | 888 | |
c2dcfdef VZ |
889 | if ( ::RemoveMenu((HMENU)m_hMenu, (UINT)pos, MF_BYPOSITION) ) |
890 | { | |
891 | // VZ: I'm not sure about what's going on here, so I leave an assert | |
837e5743 | 892 | wxASSERT_MSG( m_menus[pos] == a_menu, _T("what is this parameter for??") ); |
2bda0e17 | 893 | |
c2dcfdef VZ |
894 | a_menu->Detach(); |
895 | ||
896 | if ( m_menuBarFrame ) | |
897 | Refresh(); | |
2bda0e17 | 898 | |
c626a8b7 VZ |
899 | return TRUE; |
900 | } | |
c2dcfdef VZ |
901 | else |
902 | { | |
903 | wxLogLastError("RemoveMenu"); | |
904 | } | |
2bda0e17 | 905 | |
c626a8b7 | 906 | return FALSE; |
2bda0e17 KB |
907 | } |
908 | ||
837e5743 | 909 | bool wxMenuBar::OnAppend(wxMenu *a_menu, const wxChar *title) |
2bda0e17 | 910 | { |
c2dcfdef VZ |
911 | WXHMENU submenu = a_menu->GetHMenu(); |
912 | if ( !submenu ) | |
c626a8b7 | 913 | return FALSE; |
2bda0e17 | 914 | |
c2dcfdef | 915 | if ( !m_menuBarFrame ) |
c626a8b7 | 916 | return TRUE; |
2bda0e17 | 917 | |
c2dcfdef | 918 | a_menu->Attach(this); |
2bda0e17 | 919 | |
c50f1fb9 | 920 | if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING, |
c2dcfdef VZ |
921 | (UINT)submenu, title) ) |
922 | { | |
837e5743 | 923 | wxLogLastError(_T("AppendMenu")); |
c2dcfdef | 924 | } |
2bda0e17 | 925 | |
c2dcfdef | 926 | Refresh(); |
2bda0e17 | 927 | |
c626a8b7 | 928 | return TRUE; |
2bda0e17 KB |
929 | } |
930 | ||
c2dcfdef VZ |
931 | // --------------------------------------------------------------------------- |
932 | // wxMenuBar construction | |
933 | // --------------------------------------------------------------------------- | |
934 | ||
2bda0e17 KB |
935 | void wxMenuBar::Append (wxMenu * menu, const wxString& title) |
936 | { | |
c626a8b7 VZ |
937 | if (!OnAppend(menu, title)) |
938 | return; | |
2bda0e17 | 939 | |
c626a8b7 VZ |
940 | m_menuCount ++; |
941 | wxMenu **new_menus = new wxMenu *[m_menuCount]; | |
942 | wxString *new_titles = new wxString[m_menuCount]; | |
943 | int i; | |
2bda0e17 | 944 | |
c626a8b7 VZ |
945 | for (i = 0; i < m_menuCount - 1; i++) |
946 | { | |
947 | new_menus[i] = m_menus[i]; | |
948 | m_menus[i] = NULL; | |
949 | new_titles[i] = m_titles[i]; | |
837e5743 | 950 | m_titles[i] = _T(""); |
c626a8b7 VZ |
951 | } |
952 | if (m_menus) | |
953 | { | |
954 | delete[]m_menus; | |
955 | delete[]m_titles; | |
956 | } | |
957 | m_menus = new_menus; | |
958 | m_titles = new_titles; | |
2bda0e17 | 959 | |
c626a8b7 VZ |
960 | m_menus[m_menuCount - 1] = (wxMenu *)menu; |
961 | m_titles[m_menuCount - 1] = title; | |
2bda0e17 | 962 | |
c2dcfdef | 963 | menu->SetParent(this); |
2bda0e17 KB |
964 | } |
965 | ||
debe6624 | 966 | void wxMenuBar::Delete(wxMenu * menu, int i) |
2bda0e17 | 967 | { |
c626a8b7 VZ |
968 | int j; |
969 | int ii = (int) i; | |
970 | ||
971 | if (menu != 0) { | |
972 | for (ii = 0; ii < m_menuCount; ii++) { | |
973 | if (m_menus[ii] == menu) | |
974 | break; | |
975 | } | |
976 | if (ii >= m_menuCount) | |
977 | return; | |
978 | } else { | |
979 | if (ii < 0 || ii >= m_menuCount) | |
980 | return; | |
981 | menu = m_menus[ii]; | |
982 | } | |
2bda0e17 | 983 | |
c626a8b7 VZ |
984 | if (!OnDelete(menu, ii)) |
985 | return; | |
2bda0e17 | 986 | |
c626a8b7 | 987 | menu->SetParent(NULL); |
2bda0e17 | 988 | |
c626a8b7 VZ |
989 | -- m_menuCount; |
990 | for (j = ii; j < m_menuCount; j++) { | |
991 | m_menus[j] = m_menus[j + 1]; | |
992 | m_titles[j] = m_titles[j + 1]; | |
993 | } | |
2bda0e17 KB |
994 | } |
995 | ||
42e69d6b VZ |
996 | void wxMenuBar::Attach(wxFrame *frame) |
997 | { | |
998 | wxASSERT_MSG( !m_menuBarFrame, _T("menubar already attached!") ); | |
999 | ||
1000 | m_menuBarFrame = frame; | |
1001 | ||
d427503c | 1002 | #if wxUSE_ACCEL |
5df1250b | 1003 | // create the accel table - we consider that the menubar construction is |
42e69d6b VZ |
1004 | // finished |
1005 | size_t nAccelCount = 0; | |
1006 | int i; | |
1007 | for ( i = 0; i < m_menuCount; i++ ) | |
1008 | { | |
1009 | nAccelCount += m_menus[i]->GetAccelCount(); | |
1010 | } | |
1011 | ||
5df1250b | 1012 | if ( nAccelCount ) |
42e69d6b | 1013 | { |
5df1250b | 1014 | wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount]; |
42e69d6b | 1015 | |
5df1250b VZ |
1016 | nAccelCount = 0; |
1017 | for ( i = 0; i < m_menuCount; i++ ) | |
1018 | { | |
1019 | nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]); | |
1020 | } | |
1021 | ||
1022 | m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries); | |
42e69d6b | 1023 | |
5df1250b VZ |
1024 | delete [] accelEntries; |
1025 | } | |
d427503c | 1026 | #endif // wxUSE_ACCEL |
42e69d6b VZ |
1027 | } |
1028 | ||
c2dcfdef VZ |
1029 | // --------------------------------------------------------------------------- |
1030 | // wxMenuBar searching for menu items | |
1031 | // --------------------------------------------------------------------------- | |
1032 | ||
1033 | // Find the itemString in menuString, and return the item id or wxNOT_FOUND | |
1034 | int wxMenuBar::FindMenuItem(const wxString& menuString, | |
1035 | const wxString& itemString) const | |
2bda0e17 | 1036 | { |
c2dcfdef VZ |
1037 | wxString menuLabel = wxStripMenuCodes(menuString); |
1038 | for ( int i = 0; i < m_menuCount; i++ ) | |
2bda0e17 | 1039 | { |
c2dcfdef VZ |
1040 | wxString title = wxStripMenuCodes(m_titles[i]); |
1041 | if ( menuString == title ) | |
1042 | return m_menus[i]->FindItem(itemString); | |
2bda0e17 | 1043 | } |
c2dcfdef VZ |
1044 | |
1045 | return wxNOT_FOUND; | |
2bda0e17 KB |
1046 | } |
1047 | ||
c2dcfdef | 1048 | wxMenuItem *wxMenuBar::FindItemForId (int id, wxMenu **itemMenu) const |
2bda0e17 | 1049 | { |
c2dcfdef | 1050 | if ( itemMenu ) |
c626a8b7 | 1051 | *itemMenu = NULL; |
2bda0e17 | 1052 | |
c626a8b7 | 1053 | wxMenuItem *item = NULL; |
c2dcfdef | 1054 | for ( int i = 0; !item && (i < m_menuCount); i++ ) |
c626a8b7 | 1055 | { |
c2dcfdef | 1056 | item = m_menus[i]->FindItemForId(id, itemMenu); |
c626a8b7 | 1057 | } |
2bda0e17 | 1058 | |
c2dcfdef | 1059 | return item; |
2bda0e17 KB |
1060 | } |
1061 | ||
2bda0e17 | 1062 | |
c626a8b7 VZ |
1063 | // ---------------------------------------------------------------------------- |
1064 | // helper functions | |
1065 | // ---------------------------------------------------------------------------- | |
1066 | ||
2bda0e17 | 1067 | wxWindow *wxMenu::GetWindow() const |
c626a8b7 VZ |
1068 | { |
1069 | if ( m_pInvokingWindow != NULL ) | |
1070 | return m_pInvokingWindow; | |
1071 | else if ( m_menuBar != NULL) | |
c2dcfdef | 1072 | return m_menuBar->GetFrame(); |
c626a8b7 VZ |
1073 | |
1074 | return NULL; | |
2bda0e17 KB |
1075 | } |
1076 | ||
1077 | WXHMENU wxMenu::GetHMenu() const | |
1078 | { | |
c626a8b7 VZ |
1079 | if ( m_hMenu != 0 ) |
1080 | return m_hMenu; | |
1081 | else if ( m_savehMenu != 0 ) | |
1082 | return m_savehMenu; | |
1083 | ||
837e5743 | 1084 | wxFAIL_MSG(_T("wxMenu without HMENU")); |
2bda0e17 | 1085 | |
c626a8b7 | 1086 | return 0; |
2bda0e17 KB |
1087 | } |
1088 | ||
c626a8b7 VZ |
1089 | // Update a menu and all submenus recursively. source is the object that has |
1090 | // the update event handlers defined for it. If NULL, the menu or associated | |
1091 | // window will be used. | |
631f1bfe JS |
1092 | void wxMenu::UpdateUI(wxEvtHandler* source) |
1093 | { | |
c626a8b7 VZ |
1094 | if (!source && GetInvokingWindow()) |
1095 | source = GetInvokingWindow()->GetEventHandler(); | |
1096 | if (!source) | |
1097 | source = GetEventHandler(); | |
1098 | if (!source) | |
1099 | source = this; | |
1100 | ||
1101 | wxNode* node = GetItems().First(); | |
1102 | while (node) | |
631f1bfe | 1103 | { |
c626a8b7 VZ |
1104 | wxMenuItem* item = (wxMenuItem*) node->Data(); |
1105 | if ( !item->IsSeparator() ) | |
1106 | { | |
1107 | wxWindowID id = item->GetId(); | |
1108 | wxUpdateUIEvent event(id); | |
1109 | event.SetEventObject( source ); | |
1110 | ||
1111 | if (source->ProcessEvent(event)) | |
1112 | { | |
1113 | if (event.GetSetText()) | |
1114 | SetLabel(id, event.GetText()); | |
1115 | if (event.GetSetChecked()) | |
1116 | Check(id, event.GetChecked()); | |
1117 | if (event.GetSetEnabled()) | |
1118 | Enable(id, event.GetEnabled()); | |
1119 | } | |
1120 | ||
1121 | if (item->GetSubMenu()) | |
1122 | item->GetSubMenu()->UpdateUI(source); | |
1123 | } | |
1124 | node = node->Next(); | |
1125 | } | |
631f1bfe | 1126 | } |