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