1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "menu.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
39 #include "wx/ownerdrw.h"
42 #include "wx/msw/private.h"
43 #include "wx/msw/menu.h"
44 #include "wx/menuitem.h"
47 // other standard headers
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 extern wxMenu
*wxCurrentPopupMenu
;
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // the (popup) menu title has this special id
61 static const int idMenuTitle
= -2;
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 #if !USE_SHARED_LIBRARY
68 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
69 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
73 #define GetHMENU() ((HMENU)GetHMenu())
74 #define GetHMenuOf(menu) ((HMENU)menu->GetHMenu())
76 // ============================================================================
78 // ============================================================================
80 // ---------------------------------------------------------------------------
81 // wxMenu construction, adding and removing menu items
82 // ---------------------------------------------------------------------------
84 // Construct a menu with optional title (then use append)
85 wxMenu::wxMenu(const wxString
& title
, const wxFunction func
)
89 m_eventHandler
= this;
90 m_pInvokingWindow
= NULL
;
94 m_hMenu
= (WXHMENU
) CreatePopupMenu();
96 m_topLevelMenu
= this;
97 m_clientData
= (void*) NULL
;
101 Append(idMenuTitle
, m_title
) ;
105 #if WXWIN_COMPATIBILITY
110 // The wxWindow destructor will take care of deleting the submenus.
113 // free Windows resources
116 ::DestroyMenu((HMENU
)m_hMenu
);
121 wxNode
*node
= m_menuItems
.First();
124 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
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();
132 wxNode
*next
= node
->Next();
144 // function appends a new item or submenu to the menu
145 void wxMenu::Append(wxMenuItem
*pItem
)
147 wxCHECK_RET( pItem
!= NULL
, "can't append NULL item to the menu" );
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
155 int accelFlags
= wxACCEL_NORMAL
;
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
;
166 wxLogDebug(_T("Unknown accel modifier: '%s'"),
173 current
+= wxTolower(label
[n
]);
177 if ( current
.IsEmpty() ) {
178 wxLogDebug(_T("No accel key found, accel string ignored."));
181 if ( current
.Len() == 1 ) {
183 keyCode
= wxToupper(current
[0U]);
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]))) ) {
191 sscanf(current
.c_str() + 1, "%d", &n
);
193 keyCode
= VK_F1
+ n
- 1;
196 wxLogDebug(_T("Unrecognized accel key '%s', accel "
197 "string ignored."), current
.c_str());
204 m_accelKeyCodes
.Add(keyCode
);
205 m_accelFlags
.Add(accelFlags
);
206 m_accelIds
.Add(pItem
->GetId());
212 // if "Break" has just been called, insert a menu break before this item
213 // (and don't forget to reset the flag)
215 flags
|= MF_MENUBREAK
;
219 if ( pItem
->IsSeparator() ) {
220 flags
|= MF_SEPARATOR
;
223 // id is the numeric id for normal menu items and HMENU for submenus as
224 // required by ::AppendMenu() API
226 wxMenu
*submenu
= pItem
->GetSubMenu();
227 if ( submenu
!= NULL
) {
228 wxASSERT( submenu
->GetHMenu() != (WXHMENU
) NULL
);
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;
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
;
253 // menu is just a normal string (passed in data parameter)
258 if ( !::AppendMenu(GetHMENU(), flags
, id
, pData
) )
260 wxLogLastError("AppendMenu");
265 if ( id
== idMenuTitle
)
267 // visually select the menu title
269 mii
.cbSize
= sizeof(mii
);
270 mii
.fMask
= MIIM_STATE
;
271 mii
.fState
= MFS_DEFAULT
;
273 if ( !SetMenuItemInfo(GetHMENU(), (unsigned)id
, FALSE
, &mii
) )
275 wxLogLastError("SetMenuItemInfo");
280 m_menuItems
.Append(pItem
);
285 void wxMenu::AppendSeparator()
287 Append(new wxMenuItem(this, ID_SEPARATOR
));
291 void wxMenu::Append(int id
,
292 const wxString
& label
,
294 const wxString
& helpString
)
296 Append(new wxMenuItem(this, id
, label
, helpString
, FALSE
, SubMenu
));
299 // Ordinary menu item
300 void wxMenu::Append(int id
,
301 const wxString
& label
,
302 const wxString
& helpString
,
305 // 'checkable' parameter is useless for Windows.
306 Append(new wxMenuItem(this, id
, label
, helpString
, checkable
));
310 void wxMenu::Delete(int id
)
312 wxMenuItem
*item
= NULL
;
315 for (pos
= 0, node
= m_menuItems
.First(); node
; node
= node
->Next(), pos
++)
317 item
= (wxMenuItem
*)node
->Data();
318 if ( item
->GetId() == id
)
322 wxCHECK_RET( node
, "wxMenu::Delete(): item doesn't exist" );
324 HMENU menu
= GetHMENU();
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();
340 DeleteMenu(menu
, (UINT
)pos
, MF_BYPOSITION
);
343 m_menuItems
.DeleteNode(node
);
347 // ---------------------------------------------------------------------------
348 // accelerator helpers
349 // ---------------------------------------------------------------------------
351 // create the wxAcceleratorEntries for our accels and put them into provided
352 // array - return the number of accels we have
353 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
355 size_t count
= GetAccelCount();
356 for ( size_t n
= 0; n
< count
; n
++ )
358 (*accels
++).Set(m_accelFlags
[n
], m_accelKeyCodes
[n
], m_accelIds
[n
]);
364 // ---------------------------------------------------------------------------
365 // wxMenu functions implemented in wxMenuItem
366 // ---------------------------------------------------------------------------
368 void wxMenu::Enable(int id
, bool Flag
)
370 wxMenuItem
*item
= FindItemForId(id
);
371 wxCHECK_RET( item
!= NULL
, "can't enable non-existing menu item" );
376 bool wxMenu::IsEnabled(int id
) const
378 wxMenuItem
*item
= FindItemForId(id
);
379 wxCHECK_MSG( item
!= NULL
, FALSE
, "invalid item id" );
381 return item
->IsEnabled();
384 void wxMenu::Check(int id
, bool Flag
)
386 wxMenuItem
*item
= FindItemForId(id
);
387 wxCHECK_RET( item
!= NULL
, "can't get status of non-existing menu item" );
392 bool wxMenu::IsChecked(int id
) const
394 wxMenuItem
*item
= FindItemForId(id
);
395 wxCHECK_MSG( item
!= NULL
, FALSE
, "invalid item id" );
397 return item
->IsChecked();
400 void wxMenu::SetLabel(int id
, const wxString
& label
)
402 wxMenuItem
*item
= FindItemForId(id
) ;
403 wxCHECK_RET( item
, "wxMenu::SetLabel: no such item" );
405 item
->SetName(label
);
408 wxString
wxMenu::GetLabel(int id
) const
411 wxMenuItem
*pItem
= FindItemForId(id
) ;
413 label
= pItem
->GetName() ;
415 wxFAIL_MSG("wxMenu::GetLabel: item doesn't exist");
420 void wxMenu::SetHelpString(int itemId
, const wxString
& helpString
)
422 wxMenuItem
*item
= FindItemForId (itemId
);
424 item
->SetHelp(helpString
);
426 wxFAIL_MSG("wxMenu::SetHelpString: item doesn't exist");
429 wxString
wxMenu::GetHelpString (int itemId
) const
432 wxMenuItem
*item
= FindItemForId (itemId
);
434 help
= item
->GetHelp();
436 wxFAIL_MSG("wxMenu::GetHelpString: item doesn't exist");
441 // ---------------------------------------------------------------------------
443 // ---------------------------------------------------------------------------
445 void wxMenu::SetTitle(const wxString
& label
)
447 bool hasNoTitle
= m_title
.IsEmpty();
450 HMENU hMenu
= GetHMENU();
454 if ( !label
.IsEmpty() )
456 if ( !InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
457 (unsigned)idMenuTitle
, m_title
) ||
458 !InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
460 wxLogLastError("InsertMenu");
466 if ( label
.IsEmpty() )
468 // remove the title and the separator after it
469 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
470 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
472 wxLogLastError("RemoveMenu");
478 if ( !ModifyMenu(hMenu
, 0u,
479 MF_BYPOSITION
| MF_STRING
,
480 (unsigned)idMenuTitle
, m_title
) )
482 wxLogLastError("ModifyMenu");
488 // put the title string in bold face
489 if ( !m_title
.IsEmpty() )
492 mii
.cbSize
= sizeof(mii
);
493 mii
.fMask
= MIIM_STATE
;
494 mii
.fState
= MFS_DEFAULT
;
496 if ( !SetMenuItemInfo(hMenu
, (unsigned)idMenuTitle
, FALSE
, &mii
) )
498 wxLogLastError("SetMenuItemInfo");
504 const wxString
wxMenu::GetTitle() const
509 // ---------------------------------------------------------------------------
511 // ---------------------------------------------------------------------------
513 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
515 // ignore commands from the menu title
517 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
518 if ( id
!= (WXWORD
)idMenuTitle
)
520 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
);
521 event
.SetEventObject( this );
524 ProcessCommand(event
);
530 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
532 bool processed
= FALSE
;
534 #if WXWIN_COMPATIBILITY
538 (void)(*(m_callback
))(*this, event
);
541 #endif // WXWIN_COMPATIBILITY
543 // Try the menu's event handler
544 if ( !processed
&& GetEventHandler())
546 processed
= GetEventHandler()->ProcessEvent(event
);
549 // Try the window the menu was popped up from (and up through the
551 wxWindow
*win
= GetInvokingWindow();
552 if ( !processed
&& win
)
553 processed
= win
->GetEventHandler()->ProcessEvent(event
);
558 // ---------------------------------------------------------------------------
560 // ---------------------------------------------------------------------------
562 // Finds the item id matching the given string, -1 if not found.
563 int wxMenu::FindItem (const wxString
& itemString
) const
565 wxString itemLabel
= wxStripMenuCodes(itemString
);
566 for ( wxNode
*node
= m_menuItems
.First(); node
; node
= node
->Next() )
568 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
569 if ( item
->IsSubMenu() )
571 int ans
= item
->GetSubMenu()->FindItem(itemString
);
572 if ( ans
!= wxNOT_FOUND
)
575 else if ( !item
->IsSeparator() )
577 wxString label
= wxStripMenuCodes(item
->GetName());
578 if ( itemLabel
== label
)
579 return item
->GetId();
586 wxMenuItem
*wxMenu::FindItemForId(int itemId
, wxMenu
** itemMenu
) const
591 wxMenuItem
*item
= NULL
;
592 for ( wxNode
*node
= m_menuItems
.First(); node
&& !item
; node
= node
->Next() )
594 item
= (wxMenuItem
*)node
->Data();
596 if ( item
->GetId() == itemId
)
599 *itemMenu
= (wxMenu
*)this;
601 else if ( item
->IsSubMenu() )
603 item
= item
->GetSubMenu()->FindItemForId(itemId
, itemMenu
);
607 // don't exit the loop
615 // ---------------------------------------------------------------------------
617 // ---------------------------------------------------------------------------
619 bool wxWindow::PopupMenu(wxMenu
*menu
, int x
, int y
)
621 menu
->SetInvokingWindow(this);
624 HWND hWnd
= (HWND
) GetHWND();
625 HMENU hMenu
= (HMENU
)menu
->GetHMenu();
629 ::ClientToScreen(hWnd
, &point
);
630 wxCurrentPopupMenu
= menu
;
631 ::TrackPopupMenu(hMenu
, TPM_RIGHTBUTTON
, point
.x
, point
.y
, 0, hWnd
, NULL
);
633 wxCurrentPopupMenu
= NULL
;
635 menu
->SetInvokingWindow(NULL
);
640 void wxMenu::Attach(wxMenuBar
*menubar
)
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" );
647 m_savehMenu
= m_hMenu
;
651 void wxMenu::Detach()
653 wxASSERT_MSG( m_menuBar
, "can't detach menu if it's not attached" );
655 m_hMenu
= m_savehMenu
;
659 // ---------------------------------------------------------------------------
661 // ---------------------------------------------------------------------------
663 void wxMenuBar::Init()
665 m_eventHandler
= this;
669 m_menuBarFrame
= NULL
;
673 wxMenuBar::wxMenuBar()
678 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
683 wxMenuBar::wxMenuBar(int count
, wxMenu
*menus
[], const wxString titles
[])
689 m_titles
= new wxString
[count
];
692 for ( i
= 0; i
< count
; i
++ )
693 m_titles
[i
] = titles
[i
];
695 for ( i
= 0; i
< count
; i
++ )
696 m_menus
[i
]->Attach(this);
699 wxMenuBar::~wxMenuBar()
701 for ( int i
= 0; i
< m_menuCount
; i
++ )
710 // ---------------------------------------------------------------------------
712 // ---------------------------------------------------------------------------
714 void wxMenuBar::Refresh()
716 wxCHECK_RET( m_menuBarFrame
, "can't refresh a menubar withotu a frame" );
718 DrawMenuBar((HWND
)m_menuBarFrame
->GetHWND()) ;
721 WXHMENU
wxMenuBar::Create()
723 wxCHECK_MSG( !m_hMenu
, TRUE
, "menubar already created" );
725 m_hMenu
= (WXHMENU
)::CreateMenu();
729 wxLogLastError("CreateMenu");
733 for ( int i
= 0; i
< m_menuCount
; i
++ )
735 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
736 (UINT
)m_menus
[i
]->GetHMenu(),
739 wxLogLastError("AppendMenu");
747 // ---------------------------------------------------------------------------
748 // wxMenuBar functions forwarded to wxMenuItem
749 // ---------------------------------------------------------------------------
751 // Must only be used AFTER menu has been attached to frame,
752 // otherwise use individual menus to enable/disable items
753 void wxMenuBar::Enable(int id
, bool enable
)
755 wxMenu
*itemMenu
= NULL
;
756 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
758 wxCHECK_RET( item
, "attempt to enable an item which doesn't exist" );
760 item
->Enable(enable
);
763 void wxMenuBar::EnableTop(int pos
, bool enable
)
765 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;;
767 EnableMenuItem((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
| flag
);
770 // Must only be used AFTER menu has been attached to frame,
771 // otherwise use individual menus
772 void wxMenuBar::Check(int id
, bool check
)
774 wxMenu
*itemMenu
= NULL
;
775 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
777 wxCHECK_RET( item
, "attempt to check an item which doesn't exist" );
778 wxCHECK_RET( item
->IsCheckable(), "attempt to check an uncheckable item" );
783 bool wxMenuBar::IsChecked(int id
) const
785 wxMenu
*itemMenu
= NULL
;
786 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
788 wxCHECK_MSG( item
, FALSE
, "wxMenuBar::IsChecked(): no such item" );
790 int flag
= ::GetMenuState(GetHMenuOf(itemMenu
), id
, MF_BYCOMMAND
);
792 return (flag
& MF_CHECKED
) != 0;
795 bool wxMenuBar::IsEnabled(int id
) const
797 wxMenu
*itemMenu
= NULL
;
798 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
800 wxCHECK_MSG( item
, FALSE
, "wxMenuBar::IsEnabled(): no such item" );
802 int flag
= ::GetMenuState(GetHMenuOf(itemMenu
), id
, MF_BYCOMMAND
) ;
804 return (flag
& MF_ENABLED
) != 0;
807 void wxMenuBar::SetLabel(int id
, const wxString
& label
)
809 wxMenu
*itemMenu
= NULL
;
810 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
812 wxCHECK_RET( item
, "wxMenuBar::SetLabel(): no such item" );
814 item
->SetName(label
);
817 wxString
wxMenuBar::GetLabel(int id
) const
819 wxMenu
*itemMenu
= NULL
;
820 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
822 wxCHECK_MSG( item
, "", "wxMenuBar::GetLabel(): no such item" );
824 return item
->GetName();
827 void wxMenuBar::SetHelpString (int id
, const wxString
& helpString
)
829 wxMenu
*itemMenu
= NULL
;
830 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
832 wxCHECK_RET( item
, "wxMenuBar::SetHelpString(): no such item" );
834 item
->SetHelp(helpString
);
837 wxString
wxMenuBar::GetHelpString (int id
) const
839 wxMenu
*itemMenu
= NULL
;
840 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
842 wxCHECK_MSG( item
, "", "wxMenuBar::GetHelpString(): no such item" );
844 return item
->GetHelp();
847 // ---------------------------------------------------------------------------
848 // wxMenuBar functions to work with the top level submenus
849 // ---------------------------------------------------------------------------
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
854 void wxMenuBar::SetLabelTop(int pos
, const wxString
& label
)
857 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
);
858 if ( flagsOld
== 0xFFFFFFFF )
860 wxLogLastError("GetMenuState");
865 if ( flagsOld
& MF_POPUP
)
867 // HIBYTE contains the number of items in the submenu in this case
869 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, pos
) ;
876 if ( ::ModifyMenu(GetHMENU(), pos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
877 id
, label
) == 0xFFFFFFFF )
879 wxLogLastError("ModifyMenu");
883 wxString
wxMenuBar::GetLabelTop(int pos
) const
885 int len
= ::GetMenuString((HMENU
)m_hMenu
, pos
, NULL
, 0, MF_BYCOMMAND
);
887 len
++; // for the NUL character
889 ::GetMenuString(GetHMENU(), pos
, label
.GetWriteBuf(len
), len
, MF_BYCOMMAND
);
890 label
.UngetWriteBuf();
895 // ---------------------------------------------------------------------------
896 // wxMenuBar notifications
897 // ---------------------------------------------------------------------------
899 bool wxMenuBar::OnDelete(wxMenu
*a_menu
, int pos
)
901 if ( !m_menuBarFrame
)
904 if ( ::RemoveMenu((HMENU
)m_hMenu
, (UINT
)pos
, MF_BYPOSITION
) )
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??" );
911 if ( m_menuBarFrame
)
918 wxLogLastError("RemoveMenu");
924 bool wxMenuBar::OnAppend(wxMenu
*a_menu
, const char *title
)
926 WXHMENU submenu
= a_menu
->GetHMenu();
930 if ( !m_menuBarFrame
)
933 a_menu
->Attach(this);
935 if ( !::AppendMenu(GetHMENU(), MF_POPUP
| MF_STRING
,
936 (UINT
)submenu
, title
) )
938 wxLogLastError("AppendMenu");
946 // ---------------------------------------------------------------------------
947 // wxMenuBar construction
948 // ---------------------------------------------------------------------------
950 void wxMenuBar::Append (wxMenu
* menu
, const wxString
& title
)
952 if (!OnAppend(menu
, title
))
956 wxMenu
**new_menus
= new wxMenu
*[m_menuCount
];
957 wxString
*new_titles
= new wxString
[m_menuCount
];
960 for (i
= 0; i
< m_menuCount
- 1; i
++)
962 new_menus
[i
] = m_menus
[i
];
964 new_titles
[i
] = m_titles
[i
];
973 m_titles
= new_titles
;
975 m_menus
[m_menuCount
- 1] = (wxMenu
*)menu
;
976 m_titles
[m_menuCount
- 1] = title
;
978 menu
->SetParent(this);
981 void wxMenuBar::Delete(wxMenu
* menu
, int i
)
987 for (ii
= 0; ii
< m_menuCount
; ii
++) {
988 if (m_menus
[ii
] == menu
)
991 if (ii
>= m_menuCount
)
994 if (ii
< 0 || ii
>= m_menuCount
)
999 if (!OnDelete(menu
, ii
))
1002 menu
->SetParent(NULL
);
1005 for (j
= ii
; j
< m_menuCount
; j
++) {
1006 m_menus
[j
] = m_menus
[j
+ 1];
1007 m_titles
[j
] = m_titles
[j
+ 1];
1011 void wxMenuBar::Attach(wxFrame
*frame
)
1013 wxASSERT_MSG( !m_menuBarFrame
, _T("menubar already attached!") );
1015 m_menuBarFrame
= frame
;
1017 // create the accel table - we consider that the menubar construction is
1019 size_t nAccelCount
= 0;
1021 for ( i
= 0; i
< m_menuCount
; i
++ )
1023 nAccelCount
+= m_menus
[i
]->GetAccelCount();
1028 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1031 for ( i
= 0; i
< m_menuCount
; i
++ )
1033 nAccelCount
+= m_menus
[i
]->CopyAccels(&accelEntries
[nAccelCount
]);
1036 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1038 delete [] accelEntries
;
1042 // ---------------------------------------------------------------------------
1043 // wxMenuBar searching for menu items
1044 // ---------------------------------------------------------------------------
1046 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
1047 int wxMenuBar::FindMenuItem(const wxString
& menuString
,
1048 const wxString
& itemString
) const
1050 wxString menuLabel
= wxStripMenuCodes(menuString
);
1051 for ( int i
= 0; i
< m_menuCount
; i
++ )
1053 wxString title
= wxStripMenuCodes(m_titles
[i
]);
1054 if ( menuString
== title
)
1055 return m_menus
[i
]->FindItem(itemString
);
1061 wxMenuItem
*wxMenuBar::FindItemForId (int id
, wxMenu
**itemMenu
) const
1066 wxMenuItem
*item
= NULL
;
1067 for ( int i
= 0; !item
&& (i
< m_menuCount
); i
++ )
1069 item
= m_menus
[i
]->FindItemForId(id
, itemMenu
);
1076 // ----------------------------------------------------------------------------
1078 // ----------------------------------------------------------------------------
1080 wxWindow
*wxMenu::GetWindow() const
1082 if ( m_pInvokingWindow
!= NULL
)
1083 return m_pInvokingWindow
;
1084 else if ( m_menuBar
!= NULL
)
1085 return m_menuBar
->GetFrame();
1090 WXHMENU
wxMenu::GetHMenu() const
1094 else if ( m_savehMenu
!= 0 )
1097 wxFAIL_MSG("wxMenu without HMENU");
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.
1105 void wxMenu::UpdateUI(wxEvtHandler
* source
)
1107 if (!source
&& GetInvokingWindow())
1108 source
= GetInvokingWindow()->GetEventHandler();
1110 source
= GetEventHandler();
1114 wxNode
* node
= GetItems().First();
1117 wxMenuItem
* item
= (wxMenuItem
*) node
->Data();
1118 if ( !item
->IsSeparator() )
1120 wxWindowID id
= item
->GetId();
1121 wxUpdateUIEvent
event(id
);
1122 event
.SetEventObject( source
);
1124 if (source
->ProcessEvent(event
))
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());
1134 if (item
->GetSubMenu())
1135 item
->GetSubMenu()->UpdateUI(source
);
1137 node
= node
->Next();