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