]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: menu.cpp | |
3 | // Purpose: wxMenu, wxMenuBar, wxMenuItem | |
4 | // Author: David Webster | |
5 | // Modified by: | |
6 | // Created: 10/10/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) David Webster | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifndef WX_PRECOMP | |
16 | #include "wx/frame.h" | |
17 | #include "wx/menu.h" | |
18 | #include "wx/utils.h" | |
19 | #include "wx/intl.h" | |
20 | #include "wx/log.h" | |
21 | #endif | |
22 | ||
23 | #if wxUSE_OWNER_DRAWN | |
24 | #include "wx/ownerdrw.h" | |
25 | #endif | |
26 | ||
27 | #include "wx/os2/private.h" | |
28 | ||
29 | // other standard headers | |
30 | #include <string.h> | |
31 | ||
32 | // ---------------------------------------------------------------------------- | |
33 | // global variables | |
34 | // ---------------------------------------------------------------------------- | |
35 | ||
36 | extern wxMenu *wxCurrentPopupMenu; | |
37 | ||
38 | // ---------------------------------------------------------------------------- | |
39 | // constants | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | // the (popup) menu title has this special id | |
43 | static const int idMenuTitle = -2; | |
44 | ||
45 | // ---------------------------------------------------------------------------- | |
46 | // macros | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
49 | IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler) | |
50 | IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler) | |
51 | ||
52 | // ============================================================================ | |
53 | // implementation | |
54 | // ============================================================================ | |
55 | ||
56 | // --------------------------------------------------------------------------- | |
57 | // wxMenu construction, adding and removing menu items | |
58 | // --------------------------------------------------------------------------- | |
59 | ||
60 | // Construct a menu with optional title (then use append) | |
61 | void wxMenu::Init() | |
62 | { | |
63 | m_doBreak = FALSE; | |
64 | ||
65 | // create the menu | |
66 | m_hMenu = (WXHMENU)0; // CreatePopupMenu(); | |
67 | if ( !m_hMenu ) | |
68 | { | |
69 | wxLogLastError("CreatePopupMenu"); | |
70 | } | |
71 | ||
72 | // if we have a title, insert it in the beginning of the menu | |
73 | if ( !!m_title ) | |
74 | { | |
75 | Append(idMenuTitle, m_title); | |
76 | AppendSeparator(); | |
77 | } | |
78 | } | |
79 | ||
80 | // The wxWindow destructor will take care of deleting the submenus. | |
81 | wxMenu::~wxMenu() | |
82 | { | |
83 | // we should free Windows resources only if Windows doesn't do it for us | |
84 | // which happens if we're attached to a menubar or a submenu of another | |
85 | // menu | |
86 | if ( !IsAttached() && !GetParent() ) | |
87 | { | |
88 | /* | |
89 | if ( !::DestroyMenu(GetHmenu()) ) | |
90 | { | |
91 | wxLogLastError("DestroyMenu"); | |
92 | } | |
93 | */ | |
94 | } | |
95 | ||
96 | #if wxUSE_ACCEL | |
97 | // delete accels | |
98 | WX_CLEAR_ARRAY(m_accels); | |
99 | #endif // wxUSE_ACCEL | |
100 | } | |
101 | ||
102 | void wxMenu::Break() | |
103 | { | |
104 | // this will take effect during the next call to Append() | |
105 | m_doBreak = TRUE; | |
106 | } | |
107 | ||
108 | #if wxUSE_ACCEL | |
109 | ||
110 | int wxMenu::FindAccel(int id) const | |
111 | { | |
112 | size_t n, count = m_accels.GetCount(); | |
113 | for ( n = 0; n < count; n++ ) | |
114 | { | |
115 | if ( m_accels[n]->m_command == id ) | |
116 | return n; | |
117 | } | |
118 | ||
119 | return wxNOT_FOUND; | |
120 | } | |
121 | ||
122 | void wxMenu::UpdateAccel(wxMenuItem *item) | |
123 | { | |
124 | // find the (new) accel for this item | |
125 | wxAcceleratorEntry *accel = wxGetAccelFromString(item->GetText()); | |
126 | if ( accel ) | |
127 | accel->m_command = item->GetId(); | |
128 | ||
129 | // find the old one | |
130 | int n = FindAccel(item->GetId()); | |
131 | if ( n == wxNOT_FOUND ) | |
132 | { | |
133 | // no old, add new if any | |
134 | if ( accel ) | |
135 | m_accels.Add(accel); | |
136 | else | |
137 | return; // skipping RebuildAccelTable() below | |
138 | } | |
139 | else | |
140 | { | |
141 | // replace old with new or just remove the old one if no new | |
142 | delete m_accels[n]; | |
143 | if ( accel ) | |
144 | m_accels[n] = accel; | |
145 | else | |
146 | m_accels.Remove(n); | |
147 | } | |
148 | ||
149 | if ( IsAttached() ) | |
150 | { | |
151 | m_menuBar->RebuildAccelTable(); | |
152 | } | |
153 | } | |
154 | ||
155 | #endif // wxUSE_ACCEL | |
156 | ||
157 | // append a new item or submenu to the menu | |
158 | bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos) | |
159 | { | |
160 | #if wxUSE_ACCEL | |
161 | UpdateAccel(pItem); | |
162 | #endif // wxUSE_ACCEL | |
163 | ||
164 | UINT flags = 0; | |
165 | // TODO: | |
166 | /* | |
167 | // if "Break" has just been called, insert a menu break before this item | |
168 | // (and don't forget to reset the flag) | |
169 | if ( m_doBreak ) { | |
170 | flags |= MF_MENUBREAK; | |
171 | m_doBreak = FALSE; | |
172 | } | |
173 | ||
174 | if ( pItem->IsSeparator() ) { | |
175 | flags |= MF_SEPARATOR; | |
176 | } | |
177 | ||
178 | // id is the numeric id for normal menu items and HMENU for submenus as | |
179 | // required by ::AppendMenu() API | |
180 | UINT id; | |
181 | wxMenu *submenu = pItem->GetSubMenu(); | |
182 | if ( submenu != NULL ) { | |
183 | wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") ); | |
184 | ||
185 | submenu->SetParent(this); | |
186 | ||
187 | id = (UINT)submenu->GetHMenu(); | |
188 | ||
189 | flags |= MF_POPUP; | |
190 | } | |
191 | else { | |
192 | id = pItem->GetId(); | |
193 | } | |
194 | ||
195 | LPCTSTR pData; | |
196 | ||
197 | #if wxUSE_OWNER_DRAWN | |
198 | if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages? | |
199 | // item draws itself, pass pointer to it in data parameter | |
200 | flags |= MF_OWNERDRAW; | |
201 | pData = (LPCTSTR)pItem; | |
202 | } | |
203 | else | |
204 | #endif | |
205 | { | |
206 | // menu is just a normal string (passed in data parameter) | |
207 | flags |= MF_STRING; | |
208 | ||
209 | pData = (char*)pItem->GetText().c_str(); | |
210 | } | |
211 | ||
212 | BOOL ok; | |
213 | if ( pos == (size_t)-1 ) | |
214 | { | |
215 | ok = ::AppendMenu(GetHmenu(), flags, id, pData); | |
216 | } | |
217 | else | |
218 | { | |
219 | ok = ::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData); | |
220 | } | |
221 | ||
222 | if ( !ok ) | |
223 | { | |
224 | wxLogLastError("Insert or AppendMenu"); | |
225 | ||
226 | return FALSE; | |
227 | } | |
228 | else | |
229 | { | |
230 | // if we just appended the title, highlight it | |
231 | #ifdef __WIN32__ | |
232 | if ( (int)id == idMenuTitle ) | |
233 | { | |
234 | // visually select the menu title | |
235 | MENUITEMINFO mii; | |
236 | mii.cbSize = sizeof(mii); | |
237 | mii.fMask = MIIM_STATE; | |
238 | mii.fState = MFS_DEFAULT; | |
239 | ||
240 | if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id, FALSE, &mii) ) | |
241 | { | |
242 | wxLogLastError(wxT("SetMenuItemInfo")); | |
243 | } | |
244 | } | |
245 | #endif // __WIN32__ | |
246 | ||
247 | // if we're already attached to the menubar, we must update it | |
248 | if ( IsAttached() ) | |
249 | { | |
250 | m_menuBar->Refresh(); | |
251 | } | |
252 | ||
253 | return TRUE; | |
254 | } | |
255 | */ | |
256 | return FALSE; | |
257 | } | |
258 | ||
259 | bool wxMenu::DoAppend(wxMenuItem *item) | |
260 | { | |
261 | return wxMenuBase::DoAppend(item) && DoInsertOrAppend(item); | |
262 | } | |
263 | ||
264 | bool wxMenu::DoInsert(size_t pos, wxMenuItem *item) | |
265 | { | |
266 | return wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos); | |
267 | } | |
268 | ||
269 | wxMenuItem *wxMenu::DoRemove(wxMenuItem *item) | |
270 | { | |
271 | // we need to find the items position in the child list | |
272 | size_t pos; | |
273 | wxMenuItemList::Node *node = GetMenuItems().GetFirst(); | |
274 | for ( pos = 0; node; pos++ ) | |
275 | { | |
276 | if ( node->GetData() == item ) | |
277 | break; | |
278 | ||
279 | node = node->GetNext(); | |
280 | } | |
281 | ||
282 | // DoRemove() (unlike Remove) can only be called for existing item! | |
283 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); | |
284 | ||
285 | #if wxUSE_ACCEL | |
286 | // remove the corresponding accel from the accel table | |
287 | int n = FindAccel(item->GetId()); | |
288 | if ( n != wxNOT_FOUND ) | |
289 | { | |
290 | delete m_accels[n]; | |
291 | ||
292 | m_accels.Remove(n); | |
293 | } | |
294 | //else: this item doesn't have an accel, nothing to do | |
295 | #endif // wxUSE_ACCEL | |
296 | /* | |
297 | // remove the item from the menu | |
298 | if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) ) | |
299 | { | |
300 | wxLogLastError("RemoveMenu"); | |
301 | } | |
302 | */ | |
303 | if ( IsAttached() ) | |
304 | { | |
305 | // otherwise, the chane won't be visible | |
306 | m_menuBar->Refresh(); | |
307 | } | |
308 | ||
309 | // and from internal data structures | |
310 | return wxMenuBase::DoRemove(item); | |
311 | } | |
312 | ||
313 | // --------------------------------------------------------------------------- | |
314 | // accelerator helpers | |
315 | // --------------------------------------------------------------------------- | |
316 | ||
317 | #if wxUSE_ACCEL | |
318 | ||
319 | // create the wxAcceleratorEntries for our accels and put them into provided | |
320 | // array - return the number of accels we have | |
321 | size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const | |
322 | { | |
323 | size_t count = GetAccelCount(); | |
324 | for ( size_t n = 0; n < count; n++ ) | |
325 | { | |
326 | *accels++ = *m_accels[n]; | |
327 | } | |
328 | ||
329 | return count; | |
330 | } | |
331 | ||
332 | #endif // wxUSE_ACCEL | |
333 | ||
334 | // --------------------------------------------------------------------------- | |
335 | // set wxMenu title | |
336 | // --------------------------------------------------------------------------- | |
337 | ||
338 | void wxMenu::SetTitle(const wxString& label) | |
339 | { | |
340 | bool hasNoTitle = m_title.IsEmpty(); | |
341 | m_title = label; | |
342 | ||
343 | HMENU hMenu = GetHmenu(); | |
344 | ||
345 | if ( hasNoTitle ) | |
346 | { | |
347 | if ( !label.IsEmpty() ) | |
348 | { | |
349 | /* | |
350 | if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING, | |
351 | (unsigned)idMenuTitle, m_title) || | |
352 | !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) ) | |
353 | { | |
354 | wxLogLastError("InsertMenu"); | |
355 | } | |
356 | */ | |
357 | } | |
358 | } | |
359 | else | |
360 | { | |
361 | if ( label.IsEmpty() ) | |
362 | { | |
363 | /* | |
364 | // remove the title and the separator after it | |
365 | if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) || | |
366 | !RemoveMenu(hMenu, 0, MF_BYPOSITION) ) | |
367 | { | |
368 | wxLogLastError("RemoveMenu"); | |
369 | } | |
370 | */ | |
371 | } | |
372 | else | |
373 | { | |
374 | /* | |
375 | // modify the title | |
376 | if ( !ModifyMenu(hMenu, 0u, | |
377 | MF_BYPOSITION | MF_STRING, | |
378 | (unsigned)idMenuTitle, m_title) ) | |
379 | { | |
380 | wxLogLastError("ModifyMenu"); | |
381 | } | |
382 | */ | |
383 | } | |
384 | } | |
385 | /* | |
386 | #ifdef __WIN32__ | |
387 | // put the title string in bold face | |
388 | if ( !m_title.IsEmpty() ) | |
389 | { | |
390 | MENUITEMINFO mii; | |
391 | mii.cbSize = sizeof(mii); | |
392 | mii.fMask = MIIM_STATE; | |
393 | mii.fState = MFS_DEFAULT; | |
394 | ||
395 | if ( !SetMenuItemInfo(hMenu, (unsigned)idMenuTitle, FALSE, &mii) ) | |
396 | { | |
397 | wxLogLastError("SetMenuItemInfo"); | |
398 | } | |
399 | } | |
400 | #endif // Win32 | |
401 | */ | |
402 | } | |
403 | ||
404 | // --------------------------------------------------------------------------- | |
405 | // event processing | |
406 | // --------------------------------------------------------------------------- | |
407 | ||
408 | bool wxMenu::OS2Command(WXUINT WXUNUSED(param), WXWORD id) | |
409 | { | |
410 | // ignore commands from the menu title | |
411 | ||
412 | // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!! | |
413 | if ( id != (WXWORD)idMenuTitle ) | |
414 | { | |
415 | wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED); | |
416 | event.SetEventObject( this ); | |
417 | event.SetId( id ); | |
418 | event.SetInt( id ); | |
419 | ProcessCommand(event); | |
420 | } | |
421 | ||
422 | return TRUE; | |
423 | } | |
424 | ||
425 | bool wxMenu::ProcessCommand(wxCommandEvent & event) | |
426 | { | |
427 | bool processed = FALSE; | |
428 | ||
429 | #if WXWIN_COMPATIBILITY | |
430 | // Try a callback | |
431 | if (m_callback) | |
432 | { | |
433 | (void)(*(m_callback))(*this, event); | |
434 | processed = TRUE; | |
435 | } | |
436 | #endif // WXWIN_COMPATIBILITY | |
437 | ||
438 | // Try the menu's event handler | |
439 | if ( !processed && GetEventHandler()) | |
440 | { | |
441 | processed = GetEventHandler()->ProcessEvent(event); | |
442 | } | |
443 | ||
444 | // Try the window the menu was popped up from (and up through the | |
445 | // hierarchy) | |
446 | wxWindow *win = GetInvokingWindow(); | |
447 | if ( !processed && win ) | |
448 | processed = win->GetEventHandler()->ProcessEvent(event); | |
449 | ||
450 | return processed; | |
451 | } | |
452 | ||
453 | // --------------------------------------------------------------------------- | |
454 | // other | |
455 | // --------------------------------------------------------------------------- | |
456 | ||
457 | void wxMenu::Attach(wxMenuBar *menubar) | |
458 | { | |
459 | // menu can be in at most one menubar because otherwise they would both | |
460 | // delete the menu pointer | |
461 | wxASSERT_MSG( !m_menuBar, wxT("menu belongs to 2 menubars, expect a crash") ); | |
462 | ||
463 | m_menuBar = menubar; | |
464 | } | |
465 | ||
466 | void wxMenu::Detach() | |
467 | { | |
468 | wxASSERT_MSG( m_menuBar, wxT("can't detach menu if it's not attached") ); | |
469 | ||
470 | m_menuBar = NULL; | |
471 | } | |
472 | ||
473 | wxWindow *wxMenu::GetWindow() const | |
474 | { | |
475 | if ( m_invokingWindow != NULL ) | |
476 | return m_invokingWindow; | |
477 | else if ( m_menuBar != NULL) | |
478 | return m_menuBar->GetFrame(); | |
479 | ||
480 | return NULL; | |
481 | } | |
482 | ||
483 | // --------------------------------------------------------------------------- | |
484 | // Menu Bar | |
485 | // --------------------------------------------------------------------------- | |
486 | ||
487 | void wxMenuBar::Init() | |
488 | { | |
489 | m_eventHandler = this; | |
490 | m_menuBarFrame = NULL; | |
491 | m_hMenu = 0; | |
492 | } | |
493 | ||
494 | wxMenuBar::wxMenuBar() | |
495 | { | |
496 | Init(); | |
497 | } | |
498 | ||
499 | wxMenuBar::wxMenuBar( long WXUNUSED(style) ) | |
500 | { | |
501 | Init(); | |
502 | } | |
503 | ||
504 | wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[]) | |
505 | { | |
506 | Init(); | |
507 | ||
508 | m_titles.Alloc(count); | |
509 | ||
510 | for ( int i = 0; i < count; i++ ) | |
511 | { | |
512 | m_menus.Append(menus[i]); | |
513 | m_titles.Add(titles[i]); | |
514 | ||
515 | menus[i]->Attach(this); | |
516 | } | |
517 | } | |
518 | ||
519 | wxMenuBar::~wxMenuBar() | |
520 | { | |
521 | } | |
522 | ||
523 | // --------------------------------------------------------------------------- | |
524 | // wxMenuBar helpers | |
525 | // --------------------------------------------------------------------------- | |
526 | ||
527 | void wxMenuBar::Refresh( | |
528 | bool WXUNUSED(bEraseBackground) | |
529 | , const wxRect* WXUNUSED(pRect) | |
530 | ) | |
531 | { | |
532 | wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") ); | |
533 | ||
534 | // DrawMenuBar(GetHwndOf(m_menuBarFrame)); | |
535 | } | |
536 | ||
537 | WXHMENU wxMenuBar::Create() | |
538 | { | |
539 | if (m_hMenu != 0 ) | |
540 | return m_hMenu; | |
541 | ||
542 | wxCHECK_MSG( !m_hMenu, TRUE, wxT("menubar already created") ); | |
543 | ||
544 | // TODO: | |
545 | /* | |
546 | ||
547 | m_hMenu = (WXHMENU)::CreateMenu(); | |
548 | ||
549 | if ( !m_hMenu ) | |
550 | { | |
551 | wxLogLastError("CreateMenu"); | |
552 | } | |
553 | else | |
554 | { | |
555 | size_t count = GetMenuCount(); | |
556 | for ( size_t i = 0; i < count; i++ ) | |
557 | { | |
558 | if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING, | |
559 | (UINT)m_menus[i]->GetHMenu(), | |
560 | m_titles[i]) ) | |
561 | { | |
562 | wxLogLastError("AppendMenu"); | |
563 | } | |
564 | } | |
565 | } | |
566 | ||
567 | return m_hMenu; | |
568 | */ | |
569 | return (WXHMENU)0; | |
570 | } | |
571 | ||
572 | // --------------------------------------------------------------------------- | |
573 | // wxMenuBar functions to work with the top level submenus | |
574 | // --------------------------------------------------------------------------- | |
575 | ||
576 | // NB: we don't support owner drawn top level items for now, if we do these | |
577 | // functions would have to be changed to use wxMenuItem as well | |
578 | ||
579 | void wxMenuBar::EnableTop(size_t pos, bool enable) | |
580 | { | |
581 | wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") ); | |
582 | ||
583 | // int flag = enable ? MF_ENABLED : MF_GRAYED; | |
584 | ||
585 | // EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag); | |
586 | ||
587 | Refresh(); | |
588 | } | |
589 | ||
590 | void wxMenuBar::SetLabelTop(size_t pos, const wxString& label) | |
591 | { | |
592 | wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") ); | |
593 | ||
594 | m_titles[pos] = label; | |
595 | ||
596 | if ( !IsAttached() ) | |
597 | { | |
598 | return; | |
599 | } | |
600 | //else: have to modify the existing menu | |
601 | ||
602 | // TODO: | |
603 | /* | |
604 | UINT id; | |
605 | UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION); | |
606 | if ( flagsOld == 0xFFFFFFFF ) | |
607 | { | |
608 | wxLogLastError(wxT("GetMenuState")); | |
609 | ||
610 | return; | |
611 | } | |
612 | ||
613 | if ( flagsOld & MF_POPUP ) | |
614 | { | |
615 | // HIBYTE contains the number of items in the submenu in this case | |
616 | flagsOld &= 0xff; | |
617 | id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos); | |
618 | } | |
619 | else | |
620 | { | |
621 | id = pos; | |
622 | } | |
623 | ||
624 | if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld, | |
625 | id, label) == (int)0xFFFFFFFF ) | |
626 | { | |
627 | wxLogLastError("ModifyMenu"); | |
628 | } | |
629 | */ | |
630 | Refresh(); | |
631 | } | |
632 | ||
633 | wxString wxMenuBar::GetLabelTop(size_t pos) const | |
634 | { | |
635 | wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString, | |
636 | wxT("invalid menu index in wxMenuBar::GetLabelTop") ); | |
637 | ||
638 | return m_titles[pos]; | |
639 | } | |
640 | ||
641 | int wxMenuBar::FindMenu(const wxString& title) | |
642 | { | |
643 | wxString menuTitle = wxStripMenuCodes(title); | |
644 | ||
645 | size_t count = GetMenuCount(); | |
646 | for ( size_t i = 0; i < count; i++ ) | |
647 | { | |
648 | wxString title = wxStripMenuCodes(m_titles[i]); | |
649 | if ( menuTitle == title ) | |
650 | return i; | |
651 | } | |
652 | ||
653 | return wxNOT_FOUND; | |
654 | ||
655 | } | |
656 | ||
657 | // --------------------------------------------------------------------------- | |
658 | // wxMenuBar construction | |
659 | // --------------------------------------------------------------------------- | |
660 | ||
661 | wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title) | |
662 | { | |
663 | wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title); | |
664 | if ( !menuOld ) | |
665 | return FALSE; | |
666 | m_titles[pos] = title; | |
667 | // TODO: | |
668 | /* | |
669 | if ( IsAttached() ) | |
670 | { | |
671 | // can't use ModifyMenu() because it deletes the submenu it replaces | |
672 | if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) ) | |
673 | { | |
674 | wxLogLastError("RemoveMenu"); | |
675 | } | |
676 | ||
677 | if ( !::InsertMenu(GetHmenu(), (UINT)pos, | |
678 | MF_BYPOSITION | MF_POPUP | MF_STRING, | |
679 | (UINT)GetHmenuOf(menu), title) ) | |
680 | { | |
681 | wxLogLastError("InsertMenu"); | |
682 | } | |
683 | ||
684 | #if wxUSE_ACCEL | |
685 | if ( menuOld->HasAccels() || menu->HasAccels() ) | |
686 | { | |
687 | // need to rebuild accell table | |
688 | RebuildAccelTable(); | |
689 | } | |
690 | #endif // wxUSE_ACCEL | |
691 | ||
692 | Refresh(); | |
693 | } | |
694 | */ | |
695 | return menuOld; | |
696 | } | |
697 | ||
698 | bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title) | |
699 | { | |
700 | if ( !wxMenuBarBase::Insert(pos, menu, title) ) | |
701 | return FALSE; | |
702 | ||
703 | m_titles.Insert(title, pos); | |
704 | ||
705 | menu->Attach(this); | |
706 | // TODO: | |
707 | /* | |
708 | if ( IsAttached() ) | |
709 | { | |
710 | if ( !::InsertMenu(GetHmenu(), pos, | |
711 | MF_BYPOSITION | MF_POPUP | MF_STRING, | |
712 | (UINT)GetHmenuOf(menu), title) ) | |
713 | { | |
714 | wxLogLastError("InsertMenu"); | |
715 | } | |
716 | ||
717 | #if wxUSE_ACCEL | |
718 | if ( menu->HasAccels() ) | |
719 | { | |
720 | // need to rebuild accell table | |
721 | RebuildAccelTable(); | |
722 | } | |
723 | #endif // wxUSE_ACCEL | |
724 | ||
725 | Refresh(); | |
726 | } | |
727 | */ | |
728 | return TRUE; | |
729 | } | |
730 | ||
731 | bool wxMenuBar::Append(wxMenu *menu, const wxString& title) | |
732 | { | |
733 | WXHMENU submenu = menu ? menu->GetHMenu() : 0; | |
734 | wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") ); | |
735 | ||
736 | if ( !wxMenuBarBase::Append(menu, title) ) | |
737 | return FALSE; | |
738 | ||
739 | menu->Attach(this); | |
740 | ||
741 | m_titles.Add(title); | |
742 | // TODO: | |
743 | /* | |
744 | if ( IsAttached() ) | |
745 | { | |
746 | if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING, | |
747 | (UINT)submenu, title) ) | |
748 | { | |
749 | wxLogLastError(wxT("AppendMenu")); | |
750 | } | |
751 | ||
752 | #if wxUSE_ACCEL | |
753 | if ( menu->HasAccels() ) | |
754 | { | |
755 | // need to rebuild accell table | |
756 | RebuildAccelTable(); | |
757 | } | |
758 | #endif // wxUSE_ACCEL | |
759 | ||
760 | Refresh(); | |
761 | } | |
762 | */ | |
763 | return TRUE; | |
764 | } | |
765 | ||
766 | wxMenu *wxMenuBar::Remove(size_t pos) | |
767 | { | |
768 | wxMenu *menu = wxMenuBarBase::Remove(pos); | |
769 | if ( !menu ) | |
770 | return NULL; | |
771 | // TODO: | |
772 | /* | |
773 | if ( IsAttached() ) | |
774 | { | |
775 | if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) ) | |
776 | { | |
777 | wxLogLastError("RemoveMenu"); | |
778 | } | |
779 | ||
780 | menu->Detach(); | |
781 | ||
782 | #if wxUSE_ACCEL | |
783 | if ( menu->HasAccels() ) | |
784 | { | |
785 | // need to rebuild accell table | |
786 | RebuildAccelTable(); | |
787 | } | |
788 | #endif // wxUSE_ACCEL | |
789 | ||
790 | Refresh(); | |
791 | } | |
792 | ||
793 | m_titles.Remove(pos); | |
794 | */ | |
795 | return menu; | |
796 | } | |
797 | ||
798 | #if wxUSE_ACCEL | |
799 | ||
800 | void wxMenuBar::RebuildAccelTable() | |
801 | { | |
802 | // merge the accelerators of all menus into one accel table | |
803 | size_t nAccelCount = 0; | |
804 | size_t i, count = GetMenuCount(); | |
805 | for ( i = 0; i < count; i++ ) | |
806 | { | |
807 | nAccelCount += m_menus[i]->GetAccelCount(); | |
808 | } | |
809 | ||
810 | if ( nAccelCount ) | |
811 | { | |
812 | wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount]; | |
813 | ||
814 | nAccelCount = 0; | |
815 | for ( i = 0; i < count; i++ ) | |
816 | { | |
817 | nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]); | |
818 | } | |
819 | ||
820 | m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries); | |
821 | ||
822 | delete [] accelEntries; | |
823 | } | |
824 | } | |
825 | ||
826 | #endif // wxUSE_ACCEL | |
827 | ||
828 | void wxMenuBar::Attach(wxFrame *frame) | |
829 | { | |
830 | wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") ); | |
831 | ||
832 | m_menuBarFrame = frame; | |
833 | ||
834 | #if wxUSE_ACCEL | |
835 | RebuildAccelTable(); | |
836 | #endif // wxUSE_ACCEL | |
837 | } | |
838 | ||
839 | void wxMenuBar::Detach() | |
840 | { | |
841 | // ::DestroyMenu((HMENU)m_hMenu); | |
842 | m_hMenu = (WXHMENU)NULL; | |
843 | m_menuBarFrame = NULL; | |
844 | } | |
845 | ||
846 | ||
847 | // --------------------------------------------------------------------------- | |
848 | // wxMenuBar searching for menu items | |
849 | // --------------------------------------------------------------------------- | |
850 | ||
851 | // Find the itemString in menuString, and return the item id or wxNOT_FOUND | |
852 | int wxMenuBar::FindMenuItem(const wxString& menuString, | |
853 | const wxString& itemString) const | |
854 | { | |
855 | wxString menuLabel = wxStripMenuCodes(menuString); | |
856 | size_t count = GetMenuCount(); | |
857 | for ( size_t i = 0; i < count; i++ ) | |
858 | { | |
859 | wxString title = wxStripMenuCodes(m_titles[i]); | |
860 | if ( menuString == title ) | |
861 | return m_menus[i]->FindItem(itemString); | |
862 | } | |
863 | ||
864 | return wxNOT_FOUND; | |
865 | } | |
866 | ||
867 | wxMenuItem *wxMenuBar::FindItem(int id, wxMenu **itemMenu) const | |
868 | { | |
869 | if ( itemMenu ) | |
870 | *itemMenu = NULL; | |
871 | ||
872 | wxMenuItem *item = NULL; | |
873 | size_t count = GetMenuCount(); | |
874 | for ( size_t i = 0; !item && (i < count); i++ ) | |
875 | { | |
876 | item = m_menus[i]->FindItem(id, itemMenu); | |
877 | } | |
878 | ||
879 | return item; | |
880 | } | |
881 |