]>
Commit | Line | Data |
---|---|---|
3dfac970 | 1 | /////////////////////////////////////////////////////////////////////////////// |
0ad966ee | 2 | // Name: src/common/menucmn.cpp |
3dfac970 VZ |
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 | ||
3dfac970 VZ |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
1e6feb95 VZ |
27 | #if wxUSE_MENUS |
28 | ||
3dfac970 | 29 | #ifndef WX_PRECOMP |
1e6feb95 VZ |
30 | #include "wx/intl.h" |
31 | #include "wx/log.h" | |
3dfac970 | 32 | #include "wx/menu.h" |
1aecefa5 | 33 | #include "wx/frame.h" |
3dfac970 VZ |
34 | #endif |
35 | ||
345319d6 VZ |
36 | #include "wx/stockitem.h" |
37 | ||
3dfac970 VZ |
38 | // ---------------------------------------------------------------------------- |
39 | // template lists | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | #include "wx/listimpl.cpp" | |
717a57c2 | 43 | |
259c43f6 VZ |
44 | WX_DEFINE_LIST(wxMenuList) |
45 | WX_DEFINE_LIST(wxMenuItemList) | |
3dfac970 VZ |
46 | |
47 | // ============================================================================ | |
48 | // implementation | |
49 | // ============================================================================ | |
50 | ||
51 | // ---------------------------------------------------------------------------- | |
c36d4774 | 52 | // wxMenuItemBase |
ee0a94cf RR |
53 | // ---------------------------------------------------------------------------- |
54 | ||
55 | wxMenuItemBase::wxMenuItemBase(wxMenu *parentMenu, | |
56 | int id, | |
57 | const wxString& text, | |
58 | const wxString& help, | |
59 | wxItemKind kind, | |
60 | wxMenu *subMenu) | |
ee0a94cf | 61 | { |
d93b9874 VZ |
62 | switch ( id ) |
63 | { | |
64 | case wxID_ANY: | |
65 | m_id = wxWindow::NewControlId(); | |
66 | break; | |
67 | ||
68 | case wxID_SEPARATOR: | |
69 | m_id = wxID_SEPARATOR; | |
a1040229 VZ |
70 | |
71 | // there is a lot of existing code just doing Append(wxID_SEPARATOR) | |
72 | // and it makes sense to omit the following optional parameters, | |
73 | // including the kind one which doesn't default to wxITEM_SEPARATOR, | |
74 | // of course, so override it here | |
75 | kind = wxITEM_SEPARATOR; | |
d93b9874 VZ |
76 | break; |
77 | ||
0c51948f VS |
78 | case wxID_NONE: |
79 | // (popup) menu titles in wxMSW use this ID to indicate that | |
80 | // it's not a real menu item, so we don't want the check below to | |
81 | // apply to it | |
82 | m_id = id; | |
83 | break; | |
84 | ||
d93b9874 VZ |
85 | default: |
86 | // ids are limited to 16 bits under MSW so portable code shouldn't | |
87 | // use ids outside of this range (negative ids generated by wx are | |
88 | // fine though) | |
89 | wxASSERT_MSG( (id >= 0 && id < SHRT_MAX) || | |
90 | (id >= wxID_AUTO_LOWEST && id <= wxID_AUTO_HIGHEST), | |
91 | wxS("invalid id value") ); | |
92 | m_id = id; | |
93 | } | |
94 | ||
9cd28f48 VZ |
95 | // notice that parentMenu can be NULL: the item can be attached to the menu |
96 | // later with SetMenu() | |
ee0a94cf RR |
97 | |
98 | m_parentMenu = parentMenu; | |
99 | m_subMenu = subMenu; | |
100 | m_isEnabled = true; | |
101 | m_isChecked = false; | |
ee0a94cf | 102 | m_kind = kind; |
345319d6 | 103 | |
52af3158 | 104 | SetItemLabel(text); |
345319d6 | 105 | SetHelp(help); |
ee0a94cf RR |
106 | } |
107 | ||
108 | wxMenuItemBase::~wxMenuItemBase() | |
109 | { | |
110 | delete m_subMenu; | |
111 | } | |
112 | ||
113 | #if wxUSE_ACCEL | |
114 | ||
1e6feb95 VZ |
115 | wxAcceleratorEntry *wxMenuItemBase::GetAccel() const |
116 | { | |
65776a15 | 117 | return wxAcceleratorEntry::Create(GetItemLabel()); |
1e6feb95 VZ |
118 | } |
119 | ||
717a57c2 VZ |
120 | void wxMenuItemBase::SetAccel(wxAcceleratorEntry *accel) |
121 | { | |
122 | wxString text = m_text.BeforeFirst(wxT('\t')); | |
123 | if ( accel ) | |
124 | { | |
125 | text += wxT('\t'); | |
ee0a94cf | 126 | text += accel->ToString(); |
717a57c2 VZ |
127 | } |
128 | ||
52af3158 | 129 | SetItemLabel(text); |
717a57c2 VZ |
130 | } |
131 | ||
132 | #endif // wxUSE_ACCEL | |
133 | ||
52af3158 | 134 | void wxMenuItemBase::SetItemLabel(const wxString& str) |
345319d6 VZ |
135 | { |
136 | m_text = str; | |
137 | ||
138 | if ( m_text.empty() && !IsSeparator() ) | |
139 | { | |
140 | wxASSERT_MSG( wxIsStockID(GetId()), | |
141 | wxT("A non-stock menu item with an empty label?") ); | |
142 | m_text = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR | | |
143 | wxSTOCK_WITH_MNEMONIC); | |
144 | } | |
145 | } | |
146 | ||
147 | void wxMenuItemBase::SetHelp(const wxString& str) | |
148 | { | |
149 | m_help = str; | |
150 | ||
151 | if ( m_help.empty() && !IsSeparator() && wxIsStockID(GetId()) ) | |
152 | { | |
153 | // get a stock help string | |
154 | m_help = wxGetStockHelpString(GetId()); | |
155 | } | |
156 | } | |
157 | ||
cabb286d PC |
158 | #ifndef __WXPM__ |
159 | wxString wxMenuItemBase::GetLabelText(const wxString& text) | |
160 | { | |
161 | return wxStripMenuCodes(text); | |
162 | } | |
163 | #endif | |
164 | ||
52af3158 JS |
165 | #if WXWIN_COMPATIBILITY_2_8 |
166 | wxString wxMenuItemBase::GetLabelFromText(const wxString& text) | |
167 | { | |
168 | return GetLabelText(text); | |
169 | } | |
170 | #endif | |
171 | ||
6d971354 RR |
172 | bool wxMenuBase::ms_locked = true; |
173 | ||
717a57c2 VZ |
174 | // ---------------------------------------------------------------------------- |
175 | // wxMenu ctor and dtor | |
176 | // ---------------------------------------------------------------------------- | |
177 | ||
178 | void wxMenuBase::Init(long style) | |
179 | { | |
d3b9f782 VZ |
180 | m_menuBar = NULL; |
181 | m_menuParent = NULL; | |
717a57c2 | 182 | |
d3b9f782 | 183 | m_invokingWindow = NULL; |
717a57c2 | 184 | m_style = style; |
d3b9f782 | 185 | m_clientData = NULL; |
717a57c2 VZ |
186 | m_eventHandler = this; |
187 | } | |
188 | ||
189 | wxMenuBase::~wxMenuBase() | |
190 | { | |
222ed1d6 | 191 | WX_CLEAR_LIST(wxMenuItemList, m_items); |
717a57c2 VZ |
192 | } |
193 | ||
194 | // ---------------------------------------------------------------------------- | |
195 | // wxMenu item adding/removing | |
196 | // ---------------------------------------------------------------------------- | |
197 | ||
1e6feb95 VZ |
198 | void wxMenuBase::AddSubMenu(wxMenu *submenu) |
199 | { | |
9a83f860 | 200 | wxCHECK_RET( submenu, wxT("can't add a NULL submenu") ); |
1e6feb95 | 201 | |
1e6feb95 VZ |
202 | submenu->SetParent((wxMenu *)this); |
203 | } | |
204 | ||
9add9367 | 205 | wxMenuItem* wxMenuBase::DoAppend(wxMenuItem *item) |
717a57c2 | 206 | { |
9add9367 | 207 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Append()") ); |
717a57c2 VZ |
208 | |
209 | m_items.Append(item); | |
1e93ca17 | 210 | item->SetMenu((wxMenu*)this); |
1e6feb95 VZ |
211 | if ( item->IsSubMenu() ) |
212 | { | |
213 | AddSubMenu(item->GetSubMenu()); | |
214 | } | |
717a57c2 | 215 | |
9add9367 | 216 | return item; |
717a57c2 VZ |
217 | } |
218 | ||
9add9367 | 219 | wxMenuItem* wxMenuBase::Insert(size_t pos, wxMenuItem *item) |
717a57c2 | 220 | { |
9add9367 | 221 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Insert") ); |
717a57c2 | 222 | |
32db328c VZ |
223 | if ( pos == GetMenuItemCount() ) |
224 | { | |
225 | return DoAppend(item); | |
226 | } | |
227 | else | |
228 | { | |
4e32eea1 | 229 | wxCHECK_MSG( pos < GetMenuItemCount(), NULL, |
32db328c VZ |
230 | wxT("invalid index in wxMenu::Insert") ); |
231 | ||
232 | return DoInsert(pos, item); | |
233 | } | |
717a57c2 VZ |
234 | } |
235 | ||
9add9367 | 236 | wxMenuItem* wxMenuBase::DoInsert(size_t pos, wxMenuItem *item) |
717a57c2 | 237 | { |
9add9367 | 238 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Insert()") ); |
717a57c2 | 239 | |
222ed1d6 | 240 | wxMenuItemList::compatibility_iterator node = m_items.Item(pos); |
4e32eea1 | 241 | wxCHECK_MSG( node, NULL, wxT("invalid index in wxMenu::Insert()") ); |
717a57c2 VZ |
242 | |
243 | m_items.Insert(node, item); | |
1e93ca17 | 244 | item->SetMenu((wxMenu*)this); |
1e6feb95 VZ |
245 | if ( item->IsSubMenu() ) |
246 | { | |
247 | AddSubMenu(item->GetSubMenu()); | |
248 | } | |
717a57c2 | 249 | |
9add9367 | 250 | return item; |
717a57c2 VZ |
251 | } |
252 | ||
253 | wxMenuItem *wxMenuBase::Remove(wxMenuItem *item) | |
254 | { | |
255 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Remove") ); | |
256 | ||
257 | return DoRemove(item); | |
258 | } | |
259 | ||
260 | wxMenuItem *wxMenuBase::DoRemove(wxMenuItem *item) | |
261 | { | |
222ed1d6 | 262 | wxMenuItemList::compatibility_iterator node = m_items.Find(item); |
717a57c2 VZ |
263 | |
264 | // if we get here, the item is valid or one of Remove() functions is broken | |
265 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
266 | ||
267 | // we detach the item, but we do delete the list node (i.e. don't call | |
268 | // DetachNode() here!) | |
222ed1d6 | 269 | m_items.Erase(node); |
717a57c2 VZ |
270 | |
271 | // item isn't attached to anything any more | |
d3b9f782 | 272 | item->SetMenu(NULL); |
717a57c2 VZ |
273 | wxMenu *submenu = item->GetSubMenu(); |
274 | if ( submenu ) | |
275 | { | |
d3b9f782 | 276 | submenu->SetParent(NULL); |
082006f3 VZ |
277 | if ( submenu->IsAttached() ) |
278 | submenu->Detach(); | |
717a57c2 VZ |
279 | } |
280 | ||
281 | return item; | |
282 | } | |
283 | ||
284 | bool wxMenuBase::Delete(wxMenuItem *item) | |
285 | { | |
4e32eea1 | 286 | wxCHECK_MSG( item, false, wxT("invalid item in wxMenu::Delete") ); |
717a57c2 VZ |
287 | |
288 | return DoDelete(item); | |
289 | } | |
290 | ||
291 | bool wxMenuBase::DoDelete(wxMenuItem *item) | |
292 | { | |
293 | wxMenuItem *item2 = DoRemove(item); | |
4e32eea1 | 294 | wxCHECK_MSG( item2, false, wxT("failed to delete menu item") ); |
717a57c2 VZ |
295 | |
296 | // don't delete the submenu | |
d3b9f782 | 297 | item2->SetSubMenu(NULL); |
717a57c2 VZ |
298 | |
299 | delete item2; | |
300 | ||
4e32eea1 | 301 | return true; |
717a57c2 VZ |
302 | } |
303 | ||
304 | bool wxMenuBase::Destroy(wxMenuItem *item) | |
305 | { | |
4e32eea1 | 306 | wxCHECK_MSG( item, false, wxT("invalid item in wxMenu::Destroy") ); |
717a57c2 VZ |
307 | |
308 | return DoDestroy(item); | |
309 | } | |
310 | ||
311 | bool wxMenuBase::DoDestroy(wxMenuItem *item) | |
312 | { | |
313 | wxMenuItem *item2 = DoRemove(item); | |
4e32eea1 | 314 | wxCHECK_MSG( item2, false, wxT("failed to delete menu item") ); |
717a57c2 VZ |
315 | |
316 | delete item2; | |
317 | ||
4e32eea1 | 318 | return true; |
717a57c2 VZ |
319 | } |
320 | ||
321 | // ---------------------------------------------------------------------------- | |
322 | // wxMenu searching for items | |
323 | // ---------------------------------------------------------------------------- | |
324 | ||
4e32eea1 | 325 | // Finds the item id matching the given string, wxNOT_FOUND if not found. |
717a57c2 VZ |
326 | int wxMenuBase::FindItem(const wxString& text) const |
327 | { | |
52af3158 | 328 | wxString label = wxMenuItem::GetLabelText(text); |
222ed1d6 | 329 | for ( wxMenuItemList::compatibility_iterator node = m_items.GetFirst(); |
717a57c2 VZ |
330 | node; |
331 | node = node->GetNext() ) | |
332 | { | |
333 | wxMenuItem *item = node->GetData(); | |
334 | if ( item->IsSubMenu() ) | |
335 | { | |
336 | int rc = item->GetSubMenu()->FindItem(label); | |
337 | if ( rc != wxNOT_FOUND ) | |
338 | return rc; | |
339 | } | |
adb21613 VZ |
340 | |
341 | // we execute this code for submenus as well to alllow finding them by | |
342 | // name just like the ordinary items | |
343 | if ( !item->IsSeparator() ) | |
717a57c2 | 344 | { |
68bc148c | 345 | if ( item->GetItemLabelText() == label ) |
717a57c2 VZ |
346 | return item->GetId(); |
347 | } | |
348 | } | |
349 | ||
350 | return wxNOT_FOUND; | |
351 | } | |
352 | ||
353 | // recursive search for item by id | |
354 | wxMenuItem *wxMenuBase::FindItem(int itemId, wxMenu **itemMenu) const | |
355 | { | |
356 | if ( itemMenu ) | |
357 | *itemMenu = NULL; | |
358 | ||
359 | wxMenuItem *item = NULL; | |
222ed1d6 | 360 | for ( wxMenuItemList::compatibility_iterator node = m_items.GetFirst(); |
717a57c2 VZ |
361 | node && !item; |
362 | node = node->GetNext() ) | |
363 | { | |
364 | item = node->GetData(); | |
365 | ||
366 | if ( item->GetId() == itemId ) | |
367 | { | |
368 | if ( itemMenu ) | |
369 | *itemMenu = (wxMenu *)this; | |
370 | } | |
371 | else if ( item->IsSubMenu() ) | |
372 | { | |
373 | item = item->GetSubMenu()->FindItem(itemId, itemMenu); | |
374 | } | |
375 | else | |
376 | { | |
377 | // don't exit the loop | |
378 | item = NULL; | |
379 | } | |
380 | } | |
381 | ||
382 | return item; | |
383 | } | |
384 | ||
385 | // non recursive search | |
386 | wxMenuItem *wxMenuBase::FindChildItem(int id, size_t *ppos) const | |
387 | { | |
d3b9f782 | 388 | wxMenuItem *item = NULL; |
222ed1d6 | 389 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); |
717a57c2 VZ |
390 | |
391 | size_t pos; | |
392 | for ( pos = 0; node; pos++ ) | |
393 | { | |
1dddf838 VZ |
394 | if ( node->GetData()->GetId() == id ) |
395 | { | |
396 | item = node->GetData(); | |
397 | ||
717a57c2 | 398 | break; |
1dddf838 | 399 | } |
717a57c2 VZ |
400 | |
401 | node = node->GetNext(); | |
402 | } | |
403 | ||
404 | if ( ppos ) | |
405 | { | |
1987af7e | 406 | *ppos = item ? pos : (size_t)wxNOT_FOUND; |
717a57c2 VZ |
407 | } |
408 | ||
409 | return item; | |
410 | } | |
411 | ||
01ebf752 JS |
412 | // find by position |
413 | wxMenuItem* wxMenuBase::FindItemByPosition(size_t position) const | |
414 | { | |
20aed026 | 415 | wxCHECK_MSG( position < m_items.GetCount(), NULL, |
9a83f860 | 416 | wxT("wxMenu::FindItemByPosition(): invalid menu index") ); |
20aed026 VZ |
417 | |
418 | return m_items.Item( position )->GetData(); | |
01ebf752 JS |
419 | } |
420 | ||
717a57c2 | 421 | // ---------------------------------------------------------------------------- |
1e6feb95 | 422 | // wxMenu helpers used by derived classes |
717a57c2 VZ |
423 | // ---------------------------------------------------------------------------- |
424 | ||
425 | // Update a menu and all submenus recursively. source is the object that has | |
426 | // the update event handlers defined for it. If NULL, the menu or associated | |
427 | // window will be used. | |
428 | void wxMenuBase::UpdateUI(wxEvtHandler* source) | |
429 | { | |
5ce61d9f RR |
430 | if (GetInvokingWindow()) |
431 | { | |
432 | // Don't update menus if the parent | |
433 | // frame is about to get deleted | |
434 | wxWindow *tlw = wxGetTopLevelParent( GetInvokingWindow() ); | |
435 | if (tlw && wxPendingDelete.Member(tlw)) | |
436 | return; | |
437 | } | |
438 | ||
717a57c2 VZ |
439 | if ( !source && GetInvokingWindow() ) |
440 | source = GetInvokingWindow()->GetEventHandler(); | |
441 | if ( !source ) | |
442 | source = GetEventHandler(); | |
443 | if ( !source ) | |
444 | source = this; | |
445 | ||
222ed1d6 | 446 | wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst(); |
717a57c2 VZ |
447 | while ( node ) |
448 | { | |
449 | wxMenuItem* item = node->GetData(); | |
450 | if ( !item->IsSeparator() ) | |
451 | { | |
452 | wxWindowID id = item->GetId(); | |
453 | wxUpdateUIEvent event(id); | |
454 | event.SetEventObject( source ); | |
455 | ||
456 | if ( source->ProcessEvent(event) ) | |
457 | { | |
18afa2ac | 458 | // if anything changed, update the changed attribute |
717a57c2 VZ |
459 | if (event.GetSetText()) |
460 | SetLabel(id, event.GetText()); | |
461 | if (event.GetSetChecked()) | |
462 | Check(id, event.GetChecked()); | |
463 | if (event.GetSetEnabled()) | |
464 | Enable(id, event.GetEnabled()); | |
465 | } | |
466 | ||
467 | // recurse to the submenus | |
468 | if ( item->GetSubMenu() ) | |
469 | item->GetSubMenu()->UpdateUI(source); | |
470 | } | |
18afa2ac | 471 | //else: item is a separator (which doesn't process update UI events) |
717a57c2 VZ |
472 | |
473 | node = node->GetNext(); | |
474 | } | |
475 | } | |
476 | ||
1e6feb95 VZ |
477 | bool wxMenuBase::SendEvent(int id, int checked) |
478 | { | |
479 | wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, id); | |
480 | event.SetEventObject(this); | |
481 | event.SetInt(checked); | |
482 | ||
4e32eea1 | 483 | bool processed = false; |
1e6feb95 | 484 | |
7739f9c9 VZ |
485 | // Try the menu's event handler first |
486 | wxEvtHandler *handler = GetEventHandler(); | |
487 | if ( handler ) | |
488 | processed = handler->SafelyProcessEvent(event); | |
1e6feb95 | 489 | |
7739f9c9 | 490 | // Try the window the menu was popped up from or its menu bar belongs to |
1e6feb95 VZ |
491 | if ( !processed ) |
492 | { | |
7739f9c9 VZ |
493 | wxWindow * const win = GetWindow(); |
494 | if ( win ) | |
495 | processed = win->HandleWindowEvent(event); | |
1e6feb95 VZ |
496 | } |
497 | ||
498 | return processed; | |
499 | } | |
500 | ||
501 | // ---------------------------------------------------------------------------- | |
502 | // wxMenu attaching/detaching to/from menu bar | |
503 | // ---------------------------------------------------------------------------- | |
504 | ||
dbdf9a17 DE |
505 | wxMenuBar* wxMenuBase::GetMenuBar() const |
506 | { | |
507 | if(GetParent()) | |
508 | return GetParent()->GetMenuBar(); | |
509 | return m_menuBar; | |
510 | } | |
511 | ||
1e6feb95 VZ |
512 | void wxMenuBase::Attach(wxMenuBarBase *menubar) |
513 | { | |
514 | // use Detach() instead! | |
9a83f860 | 515 | wxASSERT_MSG( menubar, wxT("menu can't be attached to NULL menubar") ); |
1e6feb95 VZ |
516 | |
517 | // use IsAttached() to prevent this from happening | |
9a83f860 | 518 | wxASSERT_MSG( !m_menuBar, wxT("attaching menu twice?") ); |
1e6feb95 VZ |
519 | |
520 | m_menuBar = (wxMenuBar *)menubar; | |
521 | } | |
522 | ||
523 | void wxMenuBase::Detach() | |
524 | { | |
525 | // use IsAttached() to prevent this from happening | |
9a83f860 | 526 | wxASSERT_MSG( m_menuBar, wxT("detaching unattached menu?") ); |
1e6feb95 VZ |
527 | |
528 | m_menuBar = NULL; | |
529 | } | |
530 | ||
e3f5caa2 VZ |
531 | // ---------------------------------------------------------------------------- |
532 | // wxMenu invoking window handling | |
533 | // ---------------------------------------------------------------------------- | |
534 | ||
535 | void wxMenuBase::SetInvokingWindow(wxWindow *win) | |
536 | { | |
537 | wxASSERT_MSG( !GetParent(), | |
538 | "should only be called for top level popup menus" ); | |
539 | wxASSERT_MSG( !IsAttached(), | |
540 | "menus attached to menu bar can't have invoking window" ); | |
541 | ||
542 | m_invokingWindow = win; | |
543 | } | |
544 | ||
394cfde3 | 545 | wxWindow *wxMenuBase::GetWindow() const |
e3f5caa2 | 546 | { |
394cfde3 VZ |
547 | // only the top level menus have non-NULL invoking window or a pointer to |
548 | // the menu bar so recurse upwards until we find it | |
e3f5caa2 VZ |
549 | const wxMenuBase *menu = this; |
550 | while ( menu->GetParent() ) | |
551 | { | |
552 | menu = menu->GetParent(); | |
553 | } | |
554 | ||
394cfde3 VZ |
555 | return menu->GetMenuBar() ? menu->GetMenuBar()->GetFrame() |
556 | : menu->GetInvokingWindow(); | |
7118e711 VZ |
557 | } |
558 | ||
717a57c2 VZ |
559 | // ---------------------------------------------------------------------------- |
560 | // wxMenu functions forwarded to wxMenuItem | |
561 | // ---------------------------------------------------------------------------- | |
562 | ||
563 | void wxMenuBase::Enable( int id, bool enable ) | |
564 | { | |
565 | wxMenuItem *item = FindItem(id); | |
566 | ||
567 | wxCHECK_RET( item, wxT("wxMenu::Enable: no such item") ); | |
568 | ||
569 | item->Enable(enable); | |
570 | } | |
571 | ||
572 | bool wxMenuBase::IsEnabled( int id ) const | |
573 | { | |
574 | wxMenuItem *item = FindItem(id); | |
575 | ||
4e32eea1 | 576 | wxCHECK_MSG( item, false, wxT("wxMenu::IsEnabled: no such item") ); |
717a57c2 VZ |
577 | |
578 | return item->IsEnabled(); | |
579 | } | |
580 | ||
581 | void wxMenuBase::Check( int id, bool enable ) | |
582 | { | |
583 | wxMenuItem *item = FindItem(id); | |
584 | ||
585 | wxCHECK_RET( item, wxT("wxMenu::Check: no such item") ); | |
586 | ||
587 | item->Check(enable); | |
588 | } | |
589 | ||
590 | bool wxMenuBase::IsChecked( int id ) const | |
591 | { | |
592 | wxMenuItem *item = FindItem(id); | |
593 | ||
4e32eea1 | 594 | wxCHECK_MSG( item, false, wxT("wxMenu::IsChecked: no such item") ); |
717a57c2 VZ |
595 | |
596 | return item->IsChecked(); | |
597 | } | |
598 | ||
599 | void wxMenuBase::SetLabel( int id, const wxString &label ) | |
600 | { | |
601 | wxMenuItem *item = FindItem(id); | |
602 | ||
603 | wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") ); | |
604 | ||
52af3158 | 605 | item->SetItemLabel(label); |
717a57c2 VZ |
606 | } |
607 | ||
608 | wxString wxMenuBase::GetLabel( int id ) const | |
609 | { | |
610 | wxMenuItem *item = FindItem(id); | |
611 | ||
525d8583 | 612 | wxCHECK_MSG( item, wxEmptyString, wxT("wxMenu::GetLabel: no such item") ); |
717a57c2 | 613 | |
52af3158 | 614 | return item->GetItemLabel(); |
717a57c2 VZ |
615 | } |
616 | ||
617 | void wxMenuBase::SetHelpString( int id, const wxString& helpString ) | |
618 | { | |
619 | wxMenuItem *item = FindItem(id); | |
620 | ||
621 | wxCHECK_RET( item, wxT("wxMenu::SetHelpString: no such item") ); | |
622 | ||
623 | item->SetHelp( helpString ); | |
624 | } | |
625 | ||
626 | wxString wxMenuBase::GetHelpString( int id ) const | |
627 | { | |
628 | wxMenuItem *item = FindItem(id); | |
629 | ||
525d8583 | 630 | wxCHECK_MSG( item, wxEmptyString, wxT("wxMenu::GetHelpString: no such item") ); |
717a57c2 VZ |
631 | |
632 | return item->GetHelp(); | |
633 | } | |
634 | ||
635 | // ---------------------------------------------------------------------------- | |
636 | // wxMenuBarBase ctor and dtor | |
3dfac970 VZ |
637 | // ---------------------------------------------------------------------------- |
638 | ||
639 | wxMenuBarBase::wxMenuBarBase() | |
640 | { | |
1e6feb95 VZ |
641 | // not attached yet |
642 | m_menuBarFrame = NULL; | |
3dfac970 VZ |
643 | } |
644 | ||
645 | wxMenuBarBase::~wxMenuBarBase() | |
646 | { | |
222ed1d6 | 647 | WX_CLEAR_LIST(wxMenuList, m_menus); |
3dfac970 VZ |
648 | } |
649 | ||
650 | // ---------------------------------------------------------------------------- | |
651 | // wxMenuBar item access: the base class versions manage m_menus list, the | |
652 | // derived class should reflect the changes in the real menubar | |
653 | // ---------------------------------------------------------------------------- | |
654 | ||
655 | wxMenu *wxMenuBarBase::GetMenu(size_t pos) const | |
656 | { | |
222ed1d6 | 657 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); |
3dfac970 VZ |
658 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::GetMenu()") ); |
659 | ||
660 | return node->GetData(); | |
661 | } | |
662 | ||
b871bb95 | 663 | bool wxMenuBarBase::Append(wxMenu *menu, const wxString& title) |
3dfac970 | 664 | { |
4e32eea1 | 665 | wxCHECK_MSG( menu, false, wxT("can't append NULL menu") ); |
b871bb95 | 666 | wxCHECK_MSG( !title.empty(), false, wxT("can't append menu with empty title") ); |
3dfac970 VZ |
667 | |
668 | m_menus.Append(menu); | |
1e6feb95 | 669 | menu->Attach(this); |
3dfac970 | 670 | |
4e32eea1 | 671 | return true; |
3dfac970 VZ |
672 | } |
673 | ||
674 | bool wxMenuBarBase::Insert(size_t pos, wxMenu *menu, | |
32db328c | 675 | const wxString& title) |
3dfac970 | 676 | { |
32db328c VZ |
677 | if ( pos == m_menus.GetCount() ) |
678 | { | |
186baeb2 | 679 | return wxMenuBarBase::Append(menu, title); |
32db328c | 680 | } |
1e6feb95 | 681 | else // not at the end |
32db328c | 682 | { |
4e32eea1 | 683 | wxCHECK_MSG( menu, false, wxT("can't insert NULL menu") ); |
3dfac970 | 684 | |
222ed1d6 | 685 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); |
4e32eea1 | 686 | wxCHECK_MSG( node, false, wxT("bad index in wxMenuBar::Insert()") ); |
3dfac970 | 687 | |
32db328c | 688 | m_menus.Insert(node, menu); |
1e6feb95 | 689 | menu->Attach(this); |
3dfac970 | 690 | |
4e32eea1 | 691 | return true; |
32db328c | 692 | } |
3dfac970 VZ |
693 | } |
694 | ||
695 | wxMenu *wxMenuBarBase::Replace(size_t pos, wxMenu *menu, | |
696 | const wxString& WXUNUSED(title)) | |
697 | { | |
698 | wxCHECK_MSG( menu, NULL, wxT("can't insert NULL menu") ); | |
699 | ||
222ed1d6 | 700 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); |
3dfac970 VZ |
701 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Replace()") ); |
702 | ||
703 | wxMenu *menuOld = node->GetData(); | |
704 | node->SetData(menu); | |
705 | ||
1e6feb95 VZ |
706 | menu->Attach(this); |
707 | menuOld->Detach(); | |
708 | ||
3dfac970 VZ |
709 | return menuOld; |
710 | } | |
711 | ||
712 | wxMenu *wxMenuBarBase::Remove(size_t pos) | |
713 | { | |
222ed1d6 | 714 | wxMenuList::compatibility_iterator node = m_menus.Item(pos); |
3dfac970 VZ |
715 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Remove()") ); |
716 | ||
3dfac970 | 717 | wxMenu *menu = node->GetData(); |
222ed1d6 | 718 | m_menus.Erase(node); |
1e6feb95 | 719 | menu->Detach(); |
3dfac970 | 720 | |
3dfac970 VZ |
721 | return menu; |
722 | } | |
723 | ||
270e8b6a | 724 | int wxMenuBarBase::FindMenu(const wxString& title) const |
52130557 | 725 | { |
52af3158 | 726 | wxString label = wxMenuItem::GetLabelText(title); |
52130557 VZ |
727 | |
728 | size_t count = GetMenuCount(); | |
729 | for ( size_t i = 0; i < count; i++ ) | |
730 | { | |
52af3158 | 731 | wxString title2 = GetMenuLabel(i); |
52130557 | 732 | if ( (title2 == title) || |
52af3158 | 733 | (wxMenuItem::GetLabelText(title2) == label) ) |
52130557 VZ |
734 | { |
735 | // found | |
2b5f62a0 | 736 | return (int)i; |
52130557 VZ |
737 | } |
738 | } | |
739 | ||
740 | return wxNOT_FOUND; | |
741 | ||
742 | } | |
743 | ||
1e6feb95 VZ |
744 | // ---------------------------------------------------------------------------- |
745 | // wxMenuBar attaching/detaching to/from the frame | |
746 | // ---------------------------------------------------------------------------- | |
747 | ||
748 | void wxMenuBarBase::Attach(wxFrame *frame) | |
749 | { | |
750 | wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") ); | |
751 | ||
752 | m_menuBarFrame = frame; | |
753 | } | |
754 | ||
755 | void wxMenuBarBase::Detach() | |
756 | { | |
757 | wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") ); | |
758 | ||
759 | m_menuBarFrame = NULL; | |
760 | } | |
761 | ||
762 | // ---------------------------------------------------------------------------- | |
763 | // wxMenuBar searching for items | |
764 | // ---------------------------------------------------------------------------- | |
765 | ||
766 | wxMenuItem *wxMenuBarBase::FindItem(int id, wxMenu **menu) const | |
767 | { | |
768 | if ( menu ) | |
769 | *menu = NULL; | |
770 | ||
771 | wxMenuItem *item = NULL; | |
222ed1d6 MB |
772 | size_t count = GetMenuCount(), i; |
773 | wxMenuList::const_iterator it; | |
774 | for ( i = 0, it = m_menus.begin(); !item && (i < count); i++, it++ ) | |
1e6feb95 | 775 | { |
222ed1d6 | 776 | item = (*it)->FindItem(id, menu); |
1e6feb95 VZ |
777 | } |
778 | ||
779 | return item; | |
780 | } | |
781 | ||
782 | int wxMenuBarBase::FindMenuItem(const wxString& menu, const wxString& item) const | |
783 | { | |
52af3158 | 784 | wxString label = wxMenuItem::GetLabelText(menu); |
1e6feb95 VZ |
785 | |
786 | int i = 0; | |
222ed1d6 | 787 | wxMenuList::compatibility_iterator node; |
1e6feb95 VZ |
788 | for ( node = m_menus.GetFirst(); node; node = node->GetNext(), i++ ) |
789 | { | |
52af3158 | 790 | if ( label == wxMenuItem::GetLabelText(GetMenuLabel(i)) ) |
1e6feb95 VZ |
791 | return node->GetData()->FindItem(item); |
792 | } | |
793 | ||
794 | return wxNOT_FOUND; | |
795 | } | |
796 | ||
3dfac970 VZ |
797 | // --------------------------------------------------------------------------- |
798 | // wxMenuBar functions forwarded to wxMenuItem | |
799 | // --------------------------------------------------------------------------- | |
800 | ||
801 | void wxMenuBarBase::Enable(int id, bool enable) | |
802 | { | |
803 | wxMenuItem *item = FindItem(id); | |
804 | ||
805 | wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") ); | |
806 | ||
807 | item->Enable(enable); | |
808 | } | |
809 | ||
810 | void wxMenuBarBase::Check(int id, bool check) | |
811 | { | |
812 | wxMenuItem *item = FindItem(id); | |
813 | ||
814 | wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") ); | |
815 | wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") ); | |
816 | ||
817 | item->Check(check); | |
818 | } | |
819 | ||
820 | bool wxMenuBarBase::IsChecked(int id) const | |
821 | { | |
822 | wxMenuItem *item = FindItem(id); | |
823 | ||
4e32eea1 | 824 | wxCHECK_MSG( item, false, wxT("wxMenuBar::IsChecked(): no such item") ); |
3dfac970 VZ |
825 | |
826 | return item->IsChecked(); | |
827 | } | |
828 | ||
829 | bool wxMenuBarBase::IsEnabled(int id) const | |
830 | { | |
831 | wxMenuItem *item = FindItem(id); | |
832 | ||
4e32eea1 | 833 | wxCHECK_MSG( item, false, wxT("wxMenuBar::IsEnabled(): no such item") ); |
3dfac970 VZ |
834 | |
835 | return item->IsEnabled(); | |
836 | } | |
837 | ||
838 | void wxMenuBarBase::SetLabel(int id, const wxString& label) | |
839 | { | |
840 | wxMenuItem *item = FindItem(id); | |
841 | ||
842 | wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") ); | |
843 | ||
52af3158 | 844 | item->SetItemLabel(label); |
3dfac970 VZ |
845 | } |
846 | ||
847 | wxString wxMenuBarBase::GetLabel(int id) const | |
848 | { | |
849 | wxMenuItem *item = FindItem(id); | |
850 | ||
851 | wxCHECK_MSG( item, wxEmptyString, | |
852 | wxT("wxMenuBar::GetLabel(): no such item") ); | |
853 | ||
52af3158 | 854 | return item->GetItemLabel(); |
3dfac970 VZ |
855 | } |
856 | ||
857 | void wxMenuBarBase::SetHelpString(int id, const wxString& helpString) | |
858 | { | |
859 | wxMenuItem *item = FindItem(id); | |
860 | ||
861 | wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") ); | |
862 | ||
863 | item->SetHelp(helpString); | |
864 | } | |
865 | ||
866 | wxString wxMenuBarBase::GetHelpString(int id) const | |
867 | { | |
868 | wxMenuItem *item = FindItem(id); | |
869 | ||
870 | wxCHECK_MSG( item, wxEmptyString, | |
871 | wxT("wxMenuBar::GetHelpString(): no such item") ); | |
872 | ||
873 | return item->GetHelp(); | |
874 | } | |
875 | ||
9cd28f48 | 876 | void wxMenuBarBase::UpdateMenus() |
4d538595 DS |
877 | { |
878 | wxEvtHandler* source; | |
879 | wxMenu* menu; | |
880 | int nCount = GetMenuCount(); | |
881 | for (int n = 0; n < nCount; n++) | |
882 | { | |
883 | menu = GetMenu( n ); | |
884 | if (menu != NULL) | |
885 | { | |
886 | source = menu->GetEventHandler(); | |
887 | if (source != NULL) | |
888 | menu->UpdateUI( source ); | |
889 | } | |
890 | } | |
891 | } | |
892 | ||
68bc148c JS |
893 | #if WXWIN_COMPATIBILITY_2_8 |
894 | // get or change the label of the menu at given position | |
895 | void wxMenuBarBase::SetLabelTop(size_t pos, const wxString& label) | |
896 | { | |
897 | SetMenuLabel(pos, label); | |
898 | } | |
899 | ||
900 | wxString wxMenuBarBase::GetLabelTop(size_t pos) const | |
901 | { | |
c4e43bea | 902 | return GetMenuLabelText(pos); |
68bc148c JS |
903 | } |
904 | #endif | |
905 | ||
1e6feb95 | 906 | #endif // wxUSE_MENUS |