]>
Commit | Line | Data |
---|---|---|
3dfac970 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: common/menucmn.cpp | |
3 | // Purpose: wxMenu and wxMenuBar methods common to all ports | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 26.10.99 | |
7 | // RCS-ID: $Id$ | |
77ffb593 | 8 | // Copyright: (c) wxWidgets team |
65571936 | 9 | // Licence: wxWindows licence |
3dfac970 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
3dfac970 VZ |
21 | #pragma implementation "menubase.h" |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
1e6feb95 VZ |
31 | #if wxUSE_MENUS |
32 | ||
33 | #include <ctype.h> | |
34 | ||
3dfac970 | 35 | #ifndef WX_PRECOMP |
1e6feb95 VZ |
36 | #include "wx/intl.h" |
37 | #include "wx/log.h" | |
3dfac970 VZ |
38 | #include "wx/menu.h" |
39 | #endif | |
40 | ||
41 | // ---------------------------------------------------------------------------- | |
42 | // template lists | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | #include "wx/listimpl.cpp" | |
717a57c2 | 46 | |
3dfac970 | 47 | WX_DEFINE_LIST(wxMenuList); |
717a57c2 | 48 | WX_DEFINE_LIST(wxMenuItemList); |
3dfac970 VZ |
49 | |
50 | // ============================================================================ | |
51 | // implementation | |
52 | // ============================================================================ | |
53 | ||
54 | // ---------------------------------------------------------------------------- | |
717a57c2 VZ |
55 | // wxMenuItem |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
d65c269b VZ |
58 | wxMenuItemBase::wxMenuItemBase(wxMenu *parentMenu, |
59 | int id, | |
60 | const wxString& text, | |
61 | const wxString& help, | |
62 | wxItemKind kind, | |
63 | wxMenu *subMenu) | |
64 | : m_text(text), | |
65 | m_help(help) | |
66 | { | |
67 | wxASSERT_MSG( parentMenu != NULL, wxT("menuitem should have a menu") ); | |
68 | ||
69 | m_parentMenu = parentMenu; | |
70 | m_subMenu = subMenu; | |
4e32eea1 WS |
71 | m_isEnabled = true; |
72 | m_isChecked = false; | |
d65c269b VZ |
73 | m_id = id; |
74 | m_kind = kind; | |
e0dd12db RD |
75 | if (m_id == wxID_ANY) |
76 | m_id = wxNewId(); | |
77 | if (m_id == wxID_SEPARATOR) | |
78 | m_kind = wxITEM_SEPARATOR; | |
d65c269b VZ |
79 | } |
80 | ||
717a57c2 VZ |
81 | wxMenuItemBase::~wxMenuItemBase() |
82 | { | |
1e6feb95 | 83 | delete m_subMenu; |
717a57c2 VZ |
84 | } |
85 | ||
86 | #if wxUSE_ACCEL | |
87 | ||
c53207a5 VS |
88 | static inline bool CompareAccelString(const wxString& str, const wxChar *accel) |
89 | { | |
90 | #if wxUSE_INTL | |
91 | return str == accel || str == wxGetTranslation(accel); | |
92 | #else | |
93 | return str == accel; | |
94 | #endif | |
95 | } | |
96 | ||
1e6feb95 VZ |
97 | // return wxAcceleratorEntry for the given menu string or NULL if none |
98 | // specified | |
99 | wxAcceleratorEntry *wxGetAccelFromString(const wxString& label) | |
100 | { | |
6d971354 RR |
101 | // wxPrintf( wxT("label %s\n"), label.c_str() ); |
102 | ||
1e6feb95 VZ |
103 | // check for accelerators: they are given after '\t' |
104 | int posTab = label.Find(wxT('\t')); | |
105 | if ( posTab != wxNOT_FOUND ) { | |
106 | // parse the accelerator string | |
107 | int keyCode = 0; | |
108 | int accelFlags = wxACCEL_NORMAL; | |
109 | wxString current; | |
110 | for ( size_t n = (size_t)posTab + 1; n < label.Len(); n++ ) { | |
111 | if ( (label[n] == '+') || (label[n] == '-') ) { | |
c53207a5 | 112 | if ( CompareAccelString(current, wxTRANSLATE("ctrl")) ) |
1e6feb95 | 113 | accelFlags |= wxACCEL_CTRL; |
c53207a5 | 114 | else if ( CompareAccelString(current, wxTRANSLATE("alt")) ) |
1e6feb95 | 115 | accelFlags |= wxACCEL_ALT; |
c53207a5 | 116 | else if ( CompareAccelString(current, wxTRANSLATE("shift")) ) |
1e6feb95 VZ |
117 | accelFlags |= wxACCEL_SHIFT; |
118 | else { | |
a070d8ce VZ |
119 | // we may have "Ctrl-+", for example, but we still want to |
120 | // catch typos like "Crtl-A" so only give the warning if we | |
121 | // have something before the current '+' or '-', else take | |
122 | // it as a literal symbol | |
123 | if ( current.empty() ) | |
124 | { | |
125 | current += label[n]; | |
126 | ||
127 | // skip clearing it below | |
128 | continue; | |
129 | } | |
130 | else | |
131 | { | |
132 | wxLogDebug(wxT("Unknown accel modifier: '%s'"), | |
133 | current.c_str()); | |
134 | } | |
1e6feb95 VZ |
135 | } |
136 | ||
a070d8ce | 137 | current.clear(); |
1e6feb95 VZ |
138 | } |
139 | else { | |
a380af4d | 140 | current += (wxChar) wxTolower(label[n]); |
1e6feb95 VZ |
141 | } |
142 | } | |
143 | ||
525d8583 | 144 | if ( current.empty() ) { |
1e6feb95 VZ |
145 | wxLogDebug(wxT("No accel key found, accel string ignored.")); |
146 | } | |
147 | else { | |
148 | if ( current.Len() == 1 ) { | |
149 | // it's a letter | |
c921fd59 GD |
150 | keyCode = current[0U]; |
151 | ||
152 | // Only call wxToupper if control, alt, or shift is held down, | |
153 | // otherwise lower case accelerators won't work. | |
154 | if (accelFlags != wxACCEL_NORMAL) { | |
155 | keyCode = wxToupper(keyCode); | |
156 | } | |
1e6feb95 VZ |
157 | } |
158 | else { | |
159 | // is it a function key? | |
1c193821 | 160 | if ( current[0U] == 'f' && wxIsdigit(current[1U]) && |
1e6feb95 | 161 | (current.Len() == 2 || |
1c193821 | 162 | (current.Len() == 3 && wxIsdigit(current[2U]))) ) { |
d161e2f3 | 163 | keyCode = WXK_F1 + wxAtoi(current.c_str() + 1) - 1; |
1e6feb95 VZ |
164 | } |
165 | else { | |
166 | // several special cases | |
167 | current.MakeUpper(); | |
41b78190 | 168 | if ( current == wxT("DEL") ) |
1e6feb95 | 169 | keyCode = WXK_DELETE; |
41b78190 | 170 | else if ( current == wxT("DELETE") ) |
1e6feb95 | 171 | keyCode = WXK_DELETE; |
41efa9a7 JS |
172 | else if ( current == wxT("BACK") ) |
173 | keyCode = WXK_BACK; | |
41b78190 | 174 | else if ( current == wxT("INS") ) |
1e6feb95 | 175 | keyCode = WXK_INSERT; |
41b78190 | 176 | else if ( current == wxT("INSERT") ) |
1e6feb95 | 177 | keyCode = WXK_INSERT; |
41b78190 | 178 | else if ( current == wxT("ENTER") || current == wxT("RETURN") ) |
2b5f62a0 | 179 | keyCode = WXK_RETURN; |
41b78190 | 180 | else if ( current == wxT("PGUP") ) |
2b5f62a0 | 181 | keyCode = WXK_PRIOR; |
41b78190 | 182 | else if ( current == wxT("PGDN") ) |
2b5f62a0 | 183 | keyCode = WXK_NEXT; |
41b78190 | 184 | else if ( current == wxT("LEFT") ) |
2b5f62a0 | 185 | keyCode = WXK_LEFT; |
41b78190 | 186 | else if ( current == wxT("RIGHT") ) |
2b5f62a0 | 187 | keyCode = WXK_RIGHT; |
41b78190 | 188 | else if ( current == wxT("UP") ) |
2b5f62a0 | 189 | keyCode = WXK_UP; |
41b78190 | 190 | else if ( current == wxT("DOWN") ) |
2b5f62a0 | 191 | keyCode = WXK_DOWN; |
41b78190 | 192 | else if ( current == wxT("HOME") ) |
2b5f62a0 | 193 | keyCode = WXK_HOME; |
41b78190 | 194 | else if ( current == wxT("END") ) |
2b5f62a0 | 195 | keyCode = WXK_END; |
41b78190 | 196 | else if ( current == wxT("SPACE") ) |
2b5f62a0 | 197 | keyCode = WXK_SPACE; |
41b78190 | 198 | else if ( current == wxT("TAB") ) |
2b5f62a0 | 199 | keyCode = WXK_TAB; |
73baf01a | 200 | else if ( current == wxT("ESC") || current == wxT("ESCAPE") ) |
41b78190 | 201 | keyCode = WXK_ESCAPE; |
1e6feb95 VZ |
202 | else |
203 | { | |
204 | wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."), | |
205 | current.c_str()); | |
7d098cfd | 206 | return NULL; |
1e6feb95 VZ |
207 | } |
208 | } | |
209 | } | |
210 | } | |
211 | ||
212 | if ( keyCode ) { | |
213 | // we do have something | |
214 | return new wxAcceleratorEntry(accelFlags, keyCode); | |
215 | } | |
216 | } | |
217 | ||
218 | return (wxAcceleratorEntry *)NULL; | |
219 | } | |
220 | ||
221 | wxAcceleratorEntry *wxMenuItemBase::GetAccel() const | |
222 | { | |
223 | return wxGetAccelFromString(GetText()); | |
224 | } | |
225 | ||
717a57c2 VZ |
226 | void wxMenuItemBase::SetAccel(wxAcceleratorEntry *accel) |
227 | { | |
228 | wxString text = m_text.BeforeFirst(wxT('\t')); | |
229 | if ( accel ) | |
230 | { | |
231 | text += wxT('\t'); | |
232 | ||
233 | int flags = accel->GetFlags(); | |
234 | if ( flags & wxACCEL_ALT ) | |
235 | text += wxT("Alt-"); | |
236 | if ( flags & wxACCEL_CTRL ) | |
237 | text += wxT("Ctrl-"); | |
238 | if ( flags & wxACCEL_SHIFT ) | |
239 | text += wxT("Shift-"); | |
240 | ||
241 | int code = accel->GetKeyCode(); | |
242 | switch ( code ) | |
243 | { | |
244 | case WXK_F1: | |
245 | case WXK_F2: | |
246 | case WXK_F3: | |
247 | case WXK_F4: | |
248 | case WXK_F5: | |
249 | case WXK_F6: | |
250 | case WXK_F7: | |
251 | case WXK_F8: | |
252 | case WXK_F9: | |
253 | case WXK_F10: | |
254 | case WXK_F11: | |
255 | case WXK_F12: | |
256 | text << wxT('F') << code - WXK_F1 + 1; | |
257 | break; | |
258 | ||
259 | // if there are any other keys wxGetAccelFromString() may return, | |
260 | // we should process them here | |
261 | ||
262 | default: | |
0c6099b7 | 263 | if ( wxIsalnum(code) ) |
717a57c2 VZ |
264 | { |
265 | text << (wxChar)code; | |
266 | ||
267 | break; | |
268 | } | |
269 | ||
270 | wxFAIL_MSG( wxT("unknown keyboard accel") ); | |
271 | } | |
272 | } | |
273 | ||
274 | SetText(text); | |
275 | } | |
276 | ||
277 | #endif // wxUSE_ACCEL | |
278 | ||
6d971354 RR |
279 | bool wxMenuBase::ms_locked = true; |
280 | ||
717a57c2 VZ |
281 | // ---------------------------------------------------------------------------- |
282 | // wxMenu ctor and dtor | |
283 | // ---------------------------------------------------------------------------- | |
284 | ||
285 | void wxMenuBase::Init(long style) | |
286 | { | |
717a57c2 VZ |
287 | m_menuBar = (wxMenuBar *)NULL; |
288 | m_menuParent = (wxMenu *)NULL; | |
289 | ||
290 | m_invokingWindow = (wxWindow *)NULL; | |
291 | m_style = style; | |
292 | m_clientData = (void *)NULL; | |
293 | m_eventHandler = this; | |
294 | } | |
295 | ||
296 | wxMenuBase::~wxMenuBase() | |
297 | { | |
222ed1d6 | 298 | WX_CLEAR_LIST(wxMenuItemList, m_items); |
41efa9a7 | 299 | |
1e6feb95 | 300 | // Actually, in GTK, the submenus have to get deleted first. |
717a57c2 VZ |
301 | } |
302 | ||
303 | // ---------------------------------------------------------------------------- | |
304 | // wxMenu item adding/removing | |
305 | // ---------------------------------------------------------------------------- | |
306 | ||
1e6feb95 VZ |
307 | void wxMenuBase::AddSubMenu(wxMenu *submenu) |
308 | { | |
309 | wxCHECK_RET( submenu, _T("can't add a NULL submenu") ); | |
310 | ||
1e6feb95 VZ |
311 | submenu->SetParent((wxMenu *)this); |
312 | } | |
313 | ||
9add9367 | 314 | wxMenuItem* wxMenuBase::DoAppend(wxMenuItem *item) |
717a57c2 | 315 | { |
9add9367 | 316 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Append()") ); |
717a57c2 VZ |
317 | |
318 | m_items.Append(item); | |
1e93ca17 | 319 | item->SetMenu((wxMenu*)this); |
1e6feb95 VZ |
320 | if ( item->IsSubMenu() ) |
321 | { | |
322 | AddSubMenu(item->GetSubMenu()); | |
323 | } | |
717a57c2 | 324 | |
9add9367 | 325 | return item; |
717a57c2 VZ |
326 | } |
327 | ||
9add9367 | 328 | wxMenuItem* wxMenuBase::Insert(size_t pos, wxMenuItem *item) |
717a57c2 | 329 | { |
9add9367 | 330 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Insert") ); |
717a57c2 | 331 | |
32db328c VZ |
332 | if ( pos == GetMenuItemCount() ) |
333 | { | |
334 | return DoAppend(item); | |
335 | } | |
336 | else | |
337 | { | |
4e32eea1 | 338 | wxCHECK_MSG( pos < GetMenuItemCount(), NULL, |
32db328c VZ |
339 | wxT("invalid index in wxMenu::Insert") ); |
340 | ||
341 | return DoInsert(pos, item); | |
342 | } | |
717a57c2 VZ |
343 | } |
344 | ||
9add9367 | 345 | wxMenuItem* wxMenuBase::DoInsert(size_t pos, wxMenuItem *item) |
717a57c2 | 346 | { |
9add9367 | 347 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Insert()") ); |
717a57c2 | 348 | |
222ed1d6 | 349 | wxMenuItemList::compatibility_iterator node = m_items.Item(pos); |
4e32eea1 | 350 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxMenu::Insert()") ); |
717a57c2 VZ |
351 | |
352 | m_items.Insert(node, item); | |
1e93ca17 | 353 | item->SetMenu((wxMenu*)this); |
1e6feb95 VZ |
354 | if ( item->IsSubMenu() ) |
355 | { | |
356 | AddSubMenu(item->GetSubMenu()); | |
357 | } | |
717a57c2 | 358 | |
9add9367 | 359 | return item; |
717a57c2 VZ |
360 | } |
361 | ||
362 | wxMenuItem *wxMenuBase::Remove(wxMenuItem *item) | |
363 | { | |
364 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Remove") ); | |
365 | ||
366 | return DoRemove(item); | |
367 | } | |
368 | ||
369 | wxMenuItem *wxMenuBase::DoRemove(wxMenuItem *item) | |
370 | { | |
222ed1d6 | 371 | wxMenuItemList::compatibility_iterator node = m_items.Find(item); |
717a57c2 VZ |
372 | |
373 | // if we get here, the item is valid or one of Remove() functions is broken | |
374 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
375 | ||
376 | // we detach the item, but we do delete the list node (i.e. don't call | |
377 | // DetachNode() here!) | |
222ed1d6 | 378 | m_items.Erase(node); |
717a57c2 VZ |
379 | |
380 | // item isn't attached to anything any more | |
1e93ca17 | 381 | item->SetMenu((wxMenu *)NULL); |
717a57c2 VZ |
382 | wxMenu *submenu = item->GetSubMenu(); |
383 | if ( submenu ) | |
384 | { | |
385 | submenu->SetParent((wxMenu *)NULL); | |
082006f3 VZ |
386 | if ( submenu->IsAttached() ) |
387 | submenu->Detach(); | |
717a57c2 VZ |
388 | } |
389 | ||
390 | return item; | |
391 | } | |
392 | ||
393 | bool wxMenuBase::Delete(wxMenuItem *item) | |
394 | { | |
4e32eea1 | 395 | wxCHECK_MSG( item, false, wxT("invalid item in wxMenu::Delete") ); |
717a57c2 VZ |
396 | |
397 | return DoDelete(item); | |
398 | } | |
399 | ||
400 | bool wxMenuBase::DoDelete(wxMenuItem *item) | |
401 | { | |
402 | wxMenuItem *item2 = DoRemove(item); | |
4e32eea1 | 403 | wxCHECK_MSG( item2, false, wxT("failed to delete menu item") ); |
717a57c2 VZ |
404 | |
405 | // don't delete the submenu | |
406 | item2->SetSubMenu((wxMenu *)NULL); | |
407 | ||
408 | delete item2; | |
409 | ||
4e32eea1 | 410 | return true; |
717a57c2 VZ |
411 | } |
412 | ||
413 | bool wxMenuBase::Destroy(wxMenuItem *item) | |
414 | { | |
4e32eea1 | 415 | wxCHECK_MSG( item, false, wxT("invalid item in wxMenu::Destroy") ); |
717a57c2 VZ |
416 | |
417 | return DoDestroy(item); | |
418 | } | |
419 | ||
420 | bool wxMenuBase::DoDestroy(wxMenuItem *item) | |
421 | { | |
422 | wxMenuItem *item2 = DoRemove(item); | |
4e32eea1 | 423 | wxCHECK_MSG( item2, false, wxT("failed to delete menu item") ); |
717a57c2 VZ |
424 | |
425 | delete item2; | |
426 | ||
4e32eea1 | 427 | return true; |
717a57c2 VZ |
428 | } |
429 | ||
430 | // ---------------------------------------------------------------------------- | |
431 | // wxMenu searching for items | |
432 | // ---------------------------------------------------------------------------- | |
433 | ||
4e32eea1 | 434 | // Finds the item id matching the given string, wxNOT_FOUND if not found. |
717a57c2 VZ |
435 | int wxMenuBase::FindItem(const wxString& text) const |
436 | { | |
3b59cdbf | 437 | wxString label = wxMenuItem::GetLabelFromText(text); |
222ed1d6 | 438 | for ( wxMenuItemList::compatibility_iterator node = m_items.GetFirst(); |
717a57c2 VZ |
439 | node; |
440 | node = node->GetNext() ) | |
441 | { | |
442 | wxMenuItem *item = node->GetData(); | |
443 | if ( item->IsSubMenu() ) | |
444 | { | |
445 | int rc = item->GetSubMenu()->FindItem(label); | |
446 | if ( rc != wxNOT_FOUND ) | |
447 | return rc; | |
448 | } | |
adb21613 VZ |
449 | |
450 | // we execute this code for submenus as well to alllow finding them by | |
451 | // name just like the ordinary items | |
452 | if ( !item->IsSeparator() ) | |
717a57c2 VZ |
453 | { |
454 | if ( item->GetLabel() == label ) | |
455 | return item->GetId(); | |
456 | } | |
457 | } | |
458 | ||
459 | return wxNOT_FOUND; | |
460 | } | |
461 | ||
462 | // recursive search for item by id | |
463 | wxMenuItem *wxMenuBase::FindItem(int itemId, wxMenu **itemMenu) const | |
464 | { | |
465 | if ( itemMenu ) | |
466 | *itemMenu = NULL; | |
467 | ||
468 | wxMenuItem *item = NULL; | |
222ed1d6 | 469 | for ( wxMenuItemList::compatibility_iterator node = m_items.GetFirst(); |
717a57c2 VZ |
470 | node && !item; |
471 | node = node->GetNext() ) | |
472 | { | |
473 | item = node->GetData(); | |
474 | ||
475 | if ( item->GetId() == itemId ) | |
476 | { | |
477 | if ( itemMenu ) | |
478 | *itemMenu = (wxMenu *)this; | |
479 | } | |
480 | else if ( item->IsSubMenu() ) | |
481 | { | |
482 | item = item->GetSubMenu()->FindItem(itemId, itemMenu); | |
483 | } | |
484 | else | |
485 | { | |
486 | // don't exit the loop | |
487 | item = NULL; | |
488 | } | |
489 | } | |
490 | ||
491 | return item; | |
492 | } | |
493 | ||
494 | // non recursive search | |
495 | wxMenuItem *wxMenuBase::FindChildItem(int id, size_t *ppos) const | |
496 | { | |
497 | wxMenuItem *item = (wxMenuItem *)NULL; | |
222ed1d6 | 498 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); |
717a57c2 VZ |
499 | |
500 | size_t pos; | |
501 | for ( pos = 0; node; pos++ ) | |
502 | { | |
1dddf838 VZ |
503 | if ( node->GetData()->GetId() == id ) |
504 | { | |
505 | item = node->GetData(); | |
506 | ||
717a57c2 | 507 | break; |
1dddf838 | 508 | } |
717a57c2 VZ |
509 | |
510 | node = node->GetNext(); | |
511 | } | |
512 | ||
513 | if ( ppos ) | |
514 | { | |
1987af7e | 515 | *ppos = item ? pos : (size_t)wxNOT_FOUND; |
717a57c2 VZ |
516 | } |
517 | ||
518 | return item; | |
519 | } | |
520 | ||
01ebf752 JS |
521 | // find by position |
522 | wxMenuItem* wxMenuBase::FindItemByPosition(size_t position) const | |
523 | { | |
20aed026 VZ |
524 | wxCHECK_MSG( position < m_items.GetCount(), NULL, |
525 | _T("wxMenu::FindItemByPosition(): invalid menu index") ); | |
526 | ||
527 | return m_items.Item( position )->GetData(); | |
01ebf752 JS |
528 | } |
529 | ||
717a57c2 | 530 | // ---------------------------------------------------------------------------- |
1e6feb95 | 531 | // wxMenu helpers used by derived classes |
717a57c2 VZ |
532 | // ---------------------------------------------------------------------------- |
533 | ||
534 | // Update a menu and all submenus recursively. source is the object that has | |
535 | // the update event handlers defined for it. If NULL, the menu or associated | |
536 | // window will be used. | |
537 | void wxMenuBase::UpdateUI(wxEvtHandler* source) | |
538 | { | |
5ce61d9f RR |
539 | if (GetInvokingWindow()) |
540 | { | |
541 | // Don't update menus if the parent | |
542 | // frame is about to get deleted | |
543 | wxWindow *tlw = wxGetTopLevelParent( GetInvokingWindow() ); | |
544 | if (tlw && wxPendingDelete.Member(tlw)) | |
545 | return; | |
546 | } | |
547 | ||
717a57c2 VZ |
548 | if ( !source && GetInvokingWindow() ) |
549 | source = GetInvokingWindow()->GetEventHandler(); | |
550 | if ( !source ) | |
551 | source = GetEventHandler(); | |
552 | if ( !source ) | |
553 | source = this; | |
554 | ||
222ed1d6 | 555 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); |
717a57c2 VZ |
556 | while ( node ) |
557 | { | |
558 | wxMenuItem* item = node->GetData(); | |
559 | if ( !item->IsSeparator() ) | |
560 | { | |
561 | wxWindowID id = item->GetId(); | |
562 | wxUpdateUIEvent event(id); | |
563 | event.SetEventObject( source ); | |
564 | ||
565 | if ( source->ProcessEvent(event) ) | |
566 | { | |
18afa2ac | 567 | // if anything changed, update the changed attribute |
717a57c2 VZ |
568 | if (event.GetSetText()) |
569 | SetLabel(id, event.GetText()); | |
570 | if (event.GetSetChecked()) | |
571 | Check(id, event.GetChecked()); | |
572 | if (event.GetSetEnabled()) | |
573 | Enable(id, event.GetEnabled()); | |
574 | } | |
575 | ||
576 | // recurse to the submenus | |
577 | if ( item->GetSubMenu() ) | |
578 | item->GetSubMenu()->UpdateUI(source); | |
579 | } | |
18afa2ac | 580 | //else: item is a separator (which doesn't process update UI events) |
717a57c2 VZ |
581 | |
582 | node = node->GetNext(); | |
583 | } | |
584 | } | |
585 | ||
1e6feb95 VZ |
586 | bool wxMenuBase::SendEvent(int id, int checked) |
587 | { | |
588 | wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, id); | |
589 | event.SetEventObject(this); | |
590 | event.SetInt(checked); | |
591 | ||
4e32eea1 | 592 | bool processed = false; |
1e6feb95 | 593 | |
1e6feb95 VZ |
594 | // Try the menu's event handler |
595 | if ( !processed ) | |
596 | { | |
597 | wxEvtHandler *handler = GetEventHandler(); | |
598 | if ( handler ) | |
599 | processed = handler->ProcessEvent(event); | |
600 | } | |
601 | ||
602 | // Try the window the menu was popped up from (and up through the | |
603 | // hierarchy) | |
604 | if ( !processed ) | |
605 | { | |
606 | const wxMenuBase *menu = this; | |
607 | while ( menu ) | |
608 | { | |
609 | wxWindow *win = menu->GetInvokingWindow(); | |
610 | if ( win ) | |
611 | { | |
612 | processed = win->GetEventHandler()->ProcessEvent(event); | |
613 | break; | |
614 | } | |
615 | ||
616 | menu = menu->GetParent(); | |
617 | } | |
618 | } | |
619 | ||
620 | return processed; | |
621 | } | |
622 | ||
623 | // ---------------------------------------------------------------------------- | |
624 | // wxMenu attaching/detaching to/from menu bar | |
625 | // ---------------------------------------------------------------------------- | |
626 | ||
dbdf9a17 DE |
627 | wxMenuBar* wxMenuBase::GetMenuBar() const |
628 | { | |
629 | if(GetParent()) | |
630 | return GetParent()->GetMenuBar(); | |
631 | return m_menuBar; | |
632 | } | |
633 | ||
1e6feb95 VZ |
634 | void wxMenuBase::Attach(wxMenuBarBase *menubar) |
635 | { | |
636 | // use Detach() instead! | |
637 | wxASSERT_MSG( menubar, _T("menu can't be attached to NULL menubar") ); | |
638 | ||
639 | // use IsAttached() to prevent this from happening | |
640 | wxASSERT_MSG( !m_menuBar, _T("attaching menu twice?") ); | |
641 | ||
642 | m_menuBar = (wxMenuBar *)menubar; | |
643 | } | |
644 | ||
645 | void wxMenuBase::Detach() | |
646 | { | |
647 | // use IsAttached() to prevent this from happening | |
648 | wxASSERT_MSG( m_menuBar, _T("detaching unattached menu?") ); | |
649 | ||
650 | m_menuBar = NULL; | |
651 | } | |
652 | ||
717a57c2 VZ |
653 | // ---------------------------------------------------------------------------- |
654 | // wxMenu functions forwarded to wxMenuItem | |
655 | // ---------------------------------------------------------------------------- | |
656 | ||
657 | void wxMenuBase::Enable( int id, bool enable ) | |
658 | { | |
659 | wxMenuItem *item = FindItem(id); | |
660 | ||
661 | wxCHECK_RET( item, wxT("wxMenu::Enable: no such item") ); | |
662 | ||
663 | item->Enable(enable); | |
664 | } | |
665 | ||
666 | bool wxMenuBase::IsEnabled( int id ) const | |
667 | { | |
668 | wxMenuItem *item = FindItem(id); | |
669 | ||
4e32eea1 | 670 | wxCHECK_MSG( item, false, wxT("wxMenu::IsEnabled: no such item") ); |
717a57c2 VZ |
671 | |
672 | return item->IsEnabled(); | |
673 | } | |
674 | ||
675 | void wxMenuBase::Check( int id, bool enable ) | |
676 | { | |
677 | wxMenuItem *item = FindItem(id); | |
678 | ||
679 | wxCHECK_RET( item, wxT("wxMenu::Check: no such item") ); | |
680 | ||
681 | item->Check(enable); | |
682 | } | |
683 | ||
684 | bool wxMenuBase::IsChecked( int id ) const | |
685 | { | |
686 | wxMenuItem *item = FindItem(id); | |
687 | ||
4e32eea1 | 688 | wxCHECK_MSG( item, false, wxT("wxMenu::IsChecked: no such item") ); |
717a57c2 VZ |
689 | |
690 | return item->IsChecked(); | |
691 | } | |
692 | ||
693 | void wxMenuBase::SetLabel( int id, const wxString &label ) | |
694 | { | |
695 | wxMenuItem *item = FindItem(id); | |
696 | ||
697 | wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") ); | |
698 | ||
699 | item->SetText(label); | |
700 | } | |
701 | ||
702 | wxString wxMenuBase::GetLabel( int id ) const | |
703 | { | |
704 | wxMenuItem *item = FindItem(id); | |
705 | ||
525d8583 | 706 | wxCHECK_MSG( item, wxEmptyString, wxT("wxMenu::GetLabel: no such item") ); |
717a57c2 VZ |
707 | |
708 | return item->GetText(); | |
709 | } | |
710 | ||
711 | void wxMenuBase::SetHelpString( int id, const wxString& helpString ) | |
712 | { | |
713 | wxMenuItem *item = FindItem(id); | |
714 | ||
715 | wxCHECK_RET( item, wxT("wxMenu::SetHelpString: no such item") ); | |
716 | ||
717 | item->SetHelp( helpString ); | |
718 | } | |
719 | ||
720 | wxString wxMenuBase::GetHelpString( int id ) const | |
721 | { | |
722 | wxMenuItem *item = FindItem(id); | |
723 | ||
525d8583 | 724 | wxCHECK_MSG( item, wxEmptyString, wxT("wxMenu::GetHelpString: no such item") ); |
717a57c2 VZ |
725 | |
726 | return item->GetHelp(); | |
727 | } | |
728 | ||
729 | // ---------------------------------------------------------------------------- | |
730 | // wxMenuBarBase ctor and dtor | |
3dfac970 VZ |
731 | // ---------------------------------------------------------------------------- |
732 | ||
733 | wxMenuBarBase::wxMenuBarBase() | |
734 | { | |
1e6feb95 VZ |
735 | // not attached yet |
736 | m_menuBarFrame = NULL; | |
3dfac970 VZ |
737 | } |
738 | ||
739 | wxMenuBarBase::~wxMenuBarBase() | |
740 | { | |
222ed1d6 | 741 | WX_CLEAR_LIST(wxMenuList, m_menus); |
3dfac970 VZ |
742 | } |
743 | ||
744 | // ---------------------------------------------------------------------------- | |
745 | // wxMenuBar item access: the base class versions manage m_menus list, the | |
746 | // derived class should reflect the changes in the real menubar | |
747 | // ---------------------------------------------------------------------------- | |
748 | ||
749 | wxMenu *wxMenuBarBase::GetMenu(size_t pos) const | |
750 | { | |
222ed1d6 | 751 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); |
3dfac970 VZ |
752 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::GetMenu()") ); |
753 | ||
754 | return node->GetData(); | |
755 | } | |
756 | ||
757 | bool wxMenuBarBase::Append(wxMenu *menu, const wxString& WXUNUSED(title)) | |
758 | { | |
4e32eea1 | 759 | wxCHECK_MSG( menu, false, wxT("can't append NULL menu") ); |
3dfac970 VZ |
760 | |
761 | m_menus.Append(menu); | |
1e6feb95 | 762 | menu->Attach(this); |
3dfac970 | 763 | |
4e32eea1 | 764 | return true; |
3dfac970 VZ |
765 | } |
766 | ||
767 | bool wxMenuBarBase::Insert(size_t pos, wxMenu *menu, | |
32db328c | 768 | const wxString& title) |
3dfac970 | 769 | { |
32db328c VZ |
770 | if ( pos == m_menus.GetCount() ) |
771 | { | |
186baeb2 | 772 | return wxMenuBarBase::Append(menu, title); |
32db328c | 773 | } |
1e6feb95 | 774 | else // not at the end |
32db328c | 775 | { |
4e32eea1 | 776 | wxCHECK_MSG( menu, false, wxT("can't insert NULL menu") ); |
3dfac970 | 777 | |
222ed1d6 | 778 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); |
4e32eea1 | 779 | wxCHECK_MSG( node, false, wxT("bad index in wxMenuBar::Insert()") ); |
3dfac970 | 780 | |
32db328c | 781 | m_menus.Insert(node, menu); |
1e6feb95 | 782 | menu->Attach(this); |
3dfac970 | 783 | |
4e32eea1 | 784 | return true; |
32db328c | 785 | } |
3dfac970 VZ |
786 | } |
787 | ||
788 | wxMenu *wxMenuBarBase::Replace(size_t pos, wxMenu *menu, | |
789 | const wxString& WXUNUSED(title)) | |
790 | { | |
791 | wxCHECK_MSG( menu, NULL, wxT("can't insert NULL menu") ); | |
792 | ||
222ed1d6 | 793 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); |
3dfac970 VZ |
794 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Replace()") ); |
795 | ||
796 | wxMenu *menuOld = node->GetData(); | |
797 | node->SetData(menu); | |
798 | ||
1e6feb95 VZ |
799 | menu->Attach(this); |
800 | menuOld->Detach(); | |
801 | ||
3dfac970 VZ |
802 | return menuOld; |
803 | } | |
804 | ||
805 | wxMenu *wxMenuBarBase::Remove(size_t pos) | |
806 | { | |
222ed1d6 | 807 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); |
3dfac970 VZ |
808 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Remove()") ); |
809 | ||
3dfac970 | 810 | wxMenu *menu = node->GetData(); |
222ed1d6 | 811 | m_menus.Erase(node); |
1e6feb95 | 812 | menu->Detach(); |
3dfac970 | 813 | |
3dfac970 VZ |
814 | return menu; |
815 | } | |
816 | ||
270e8b6a | 817 | int wxMenuBarBase::FindMenu(const wxString& title) const |
52130557 VZ |
818 | { |
819 | wxString label = wxMenuItem::GetLabelFromText(title); | |
820 | ||
821 | size_t count = GetMenuCount(); | |
822 | for ( size_t i = 0; i < count; i++ ) | |
823 | { | |
824 | wxString title2 = GetLabelTop(i); | |
825 | if ( (title2 == title) || | |
826 | (wxMenuItem::GetLabelFromText(title2) == label) ) | |
827 | { | |
828 | // found | |
2b5f62a0 | 829 | return (int)i; |
52130557 VZ |
830 | } |
831 | } | |
832 | ||
833 | return wxNOT_FOUND; | |
834 | ||
835 | } | |
836 | ||
1e6feb95 VZ |
837 | // ---------------------------------------------------------------------------- |
838 | // wxMenuBar attaching/detaching to/from the frame | |
839 | // ---------------------------------------------------------------------------- | |
840 | ||
841 | void wxMenuBarBase::Attach(wxFrame *frame) | |
842 | { | |
843 | wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") ); | |
844 | ||
845 | m_menuBarFrame = frame; | |
846 | } | |
847 | ||
848 | void wxMenuBarBase::Detach() | |
849 | { | |
850 | wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") ); | |
851 | ||
852 | m_menuBarFrame = NULL; | |
853 | } | |
854 | ||
855 | // ---------------------------------------------------------------------------- | |
856 | // wxMenuBar searching for items | |
857 | // ---------------------------------------------------------------------------- | |
858 | ||
859 | wxMenuItem *wxMenuBarBase::FindItem(int id, wxMenu **menu) const | |
860 | { | |
861 | if ( menu ) | |
862 | *menu = NULL; | |
863 | ||
864 | wxMenuItem *item = NULL; | |
222ed1d6 MB |
865 | size_t count = GetMenuCount(), i; |
866 | wxMenuList::const_iterator it; | |
867 | for ( i = 0, it = m_menus.begin(); !item && (i < count); i++, it++ ) | |
1e6feb95 | 868 | { |
222ed1d6 | 869 | item = (*it)->FindItem(id, menu); |
1e6feb95 VZ |
870 | } |
871 | ||
872 | return item; | |
873 | } | |
874 | ||
875 | int wxMenuBarBase::FindMenuItem(const wxString& menu, const wxString& item) const | |
876 | { | |
877 | wxString label = wxMenuItem::GetLabelFromText(menu); | |
878 | ||
879 | int i = 0; | |
222ed1d6 | 880 | wxMenuList::compatibility_iterator node; |
1e6feb95 VZ |
881 | for ( node = m_menus.GetFirst(); node; node = node->GetNext(), i++ ) |
882 | { | |
883 | if ( label == wxMenuItem::GetLabelFromText(GetLabelTop(i)) ) | |
884 | return node->GetData()->FindItem(item); | |
885 | } | |
886 | ||
887 | return wxNOT_FOUND; | |
888 | } | |
889 | ||
3dfac970 VZ |
890 | // --------------------------------------------------------------------------- |
891 | // wxMenuBar functions forwarded to wxMenuItem | |
892 | // --------------------------------------------------------------------------- | |
893 | ||
894 | void wxMenuBarBase::Enable(int id, bool enable) | |
895 | { | |
896 | wxMenuItem *item = FindItem(id); | |
897 | ||
898 | wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") ); | |
899 | ||
900 | item->Enable(enable); | |
901 | } | |
902 | ||
903 | void wxMenuBarBase::Check(int id, bool check) | |
904 | { | |
905 | wxMenuItem *item = FindItem(id); | |
906 | ||
907 | wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") ); | |
908 | wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") ); | |
909 | ||
910 | item->Check(check); | |
911 | } | |
912 | ||
913 | bool wxMenuBarBase::IsChecked(int id) const | |
914 | { | |
915 | wxMenuItem *item = FindItem(id); | |
916 | ||
4e32eea1 | 917 | wxCHECK_MSG( item, false, wxT("wxMenuBar::IsChecked(): no such item") ); |
3dfac970 VZ |
918 | |
919 | return item->IsChecked(); | |
920 | } | |
921 | ||
922 | bool wxMenuBarBase::IsEnabled(int id) const | |
923 | { | |
924 | wxMenuItem *item = FindItem(id); | |
925 | ||
4e32eea1 | 926 | wxCHECK_MSG( item, false, wxT("wxMenuBar::IsEnabled(): no such item") ); |
3dfac970 VZ |
927 | |
928 | return item->IsEnabled(); | |
929 | } | |
930 | ||
931 | void wxMenuBarBase::SetLabel(int id, const wxString& label) | |
932 | { | |
933 | wxMenuItem *item = FindItem(id); | |
934 | ||
935 | wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") ); | |
936 | ||
937 | item->SetText(label); | |
938 | } | |
939 | ||
940 | wxString wxMenuBarBase::GetLabel(int id) const | |
941 | { | |
942 | wxMenuItem *item = FindItem(id); | |
943 | ||
944 | wxCHECK_MSG( item, wxEmptyString, | |
945 | wxT("wxMenuBar::GetLabel(): no such item") ); | |
946 | ||
947 | return item->GetText(); | |
948 | } | |
949 | ||
950 | void wxMenuBarBase::SetHelpString(int id, const wxString& helpString) | |
951 | { | |
952 | wxMenuItem *item = FindItem(id); | |
953 | ||
954 | wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") ); | |
955 | ||
956 | item->SetHelp(helpString); | |
957 | } | |
958 | ||
959 | wxString wxMenuBarBase::GetHelpString(int id) const | |
960 | { | |
961 | wxMenuItem *item = FindItem(id); | |
962 | ||
963 | wxCHECK_MSG( item, wxEmptyString, | |
964 | wxT("wxMenuBar::GetHelpString(): no such item") ); | |
965 | ||
966 | return item->GetHelp(); | |
967 | } | |
968 | ||
1e6feb95 | 969 | #endif // wxUSE_MENUS |