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