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
, _T("can't append NULL item to the menu") );
150 // check for accelerators: they are given after '\t'
151 wxString label
= pItem
->GetName();
152 int posTab
= label
.Find(_T('\t'));
153 if ( posTab
!= wxNOT_FOUND
) {
154 // parse the accelerator string
156 int accelFlags
= wxACCEL_NORMAL
;
158 for ( size_t n
= (size_t)posTab
+ 1; n
< label
.Len(); n
++ ) {
159 if ( (label
[n
] == '+') || (label
[n
] == '-') ) {
160 if ( current
== _("ctrl") )
161 accelFlags
|= wxACCEL_CTRL
;
162 else if ( current
== _("alt") )
163 accelFlags
|= wxACCEL_ALT
;
164 else if ( current
== _("shift") )
165 accelFlags
|= wxACCEL_SHIFT
;
167 wxLogDebug(_T("Unknown accel modifier: '%s'"),
174 current
+= wxTolower(label
[n
]);
178 if ( current
.IsEmpty() ) {
179 wxLogDebug(_T("No accel key found, accel string ignored."));
182 if ( current
.Len() == 1 ) {
184 keyCode
= wxToupper(current
[0U]);
187 // it should be a function key
188 if ( current
[0U] == 'f' && isdigit(current
[1U]) &&
189 (current
.Len() == 2 ||
190 (current
.Len() == 3 && isdigit(current
[2U]))) ) {
192 wxSscanf(current
.c_str() + 1, _T("%d"), &n
);
194 keyCode
= VK_F1
+ n
- 1;
197 wxLogDebug(_T("Unrecognized accel key '%s', accel "
198 "string ignored."), current
.c_str());
205 m_accelKeyCodes
.Add(keyCode
);
206 m_accelFlags
.Add(accelFlags
);
207 m_accelIds
.Add(pItem
->GetId());
210 #endif // wxUSE_ACCEL
214 // if "Break" has just been called, insert a menu break before this item
215 // (and don't forget to reset the flag)
217 flags
|= MF_MENUBREAK
;
221 if ( pItem
->IsSeparator() ) {
222 flags
|= MF_SEPARATOR
;
225 // id is the numeric id for normal menu items and HMENU for submenus as
226 // required by ::AppendMenu() API
228 wxMenu
*submenu
= pItem
->GetSubMenu();
229 if ( submenu
!= NULL
) {
230 wxASSERT( submenu
->GetHMenu() != (WXHMENU
) NULL
);
232 id
= (UINT
)submenu
->GetHMenu();
233 submenu
->m_topLevelMenu
= m_topLevelMenu
;
234 submenu
->m_parent
= this;
235 submenu
->m_savehMenu
= (WXHMENU
)id
;
236 submenu
->m_hMenu
= 0;
246 #if wxUSE_OWNER_DRAWN
247 if ( pItem
->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
248 // item draws itself, pass pointer to it in data parameter
249 flags
|= MF_OWNERDRAW
;
250 pData
= (LPCTSTR
)pItem
;
255 // menu is just a normal string (passed in data parameter)
260 if ( !::AppendMenu(GetHMENU(), flags
, id
, pData
) )
262 wxLogLastError("AppendMenu");
267 if ( id
== idMenuTitle
)
269 // visually select the menu title
271 mii
.cbSize
= sizeof(mii
);
272 mii
.fMask
= MIIM_STATE
;
273 mii
.fState
= MFS_DEFAULT
;
275 if ( !SetMenuItemInfo(GetHMENU(), (unsigned)id
, FALSE
, &mii
) )
277 wxLogLastError(_T("SetMenuItemInfo"));
282 m_menuItems
.Append(pItem
);
287 void wxMenu::AppendSeparator()
289 Append(new wxMenuItem(this, ID_SEPARATOR
));
293 void wxMenu::Append(int id
,
294 const wxString
& label
,
296 const wxString
& helpString
)
298 Append(new wxMenuItem(this, id
, label
, helpString
, FALSE
, SubMenu
));
301 // Ordinary menu item
302 void wxMenu::Append(int id
,
303 const wxString
& label
,
304 const wxString
& helpString
,
307 // 'checkable' parameter is useless for Windows.
308 Append(new wxMenuItem(this, id
, label
, helpString
, checkable
));
312 void wxMenu::Delete(int id
)
314 wxMenuItem
*item
= NULL
;
317 for (pos
= 0, node
= m_menuItems
.First(); node
; node
= node
->Next(), pos
++)
319 item
= (wxMenuItem
*)node
->Data();
320 if ( item
->GetId() == id
)
324 wxCHECK_RET( node
, _T("wxMenu::Delete(): item doesn't exist") );
326 HMENU menu
= GetHMENU();
328 wxMenu
*pSubMenu
= item
->GetSubMenu();
329 if ( pSubMenu
!= NULL
) {
330 RemoveMenu(menu
, (UINT
)pos
, MF_BYPOSITION
);
331 pSubMenu
->m_hMenu
= pSubMenu
->m_savehMenu
;
332 pSubMenu
->m_savehMenu
= 0;
333 pSubMenu
->m_parent
= NULL
;
334 // RemoveChild(item->subMenu);
335 pSubMenu
->m_topLevelMenu
= NULL
;
336 // TODO: Why isn't subMenu deleted here???
337 // Will put this in for now. Assuming this is supposed
338 // to delete the menu, not just remove it.
339 item
->DeleteSubMenu();
342 DeleteMenu(menu
, (UINT
)pos
, MF_BYPOSITION
);
345 m_menuItems
.DeleteNode(node
);
351 // ---------------------------------------------------------------------------
352 // accelerator helpers
353 // ---------------------------------------------------------------------------
355 // create the wxAcceleratorEntries for our accels and put them into provided
356 // array - return the number of accels we have
357 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
359 size_t count
= GetAccelCount();
360 for ( size_t n
= 0; n
< count
; n
++ )
362 (*accels
++).Set(m_accelFlags
[n
], m_accelKeyCodes
[n
], m_accelIds
[n
]);
368 #endif // wxUSE_ACCEL
370 // ---------------------------------------------------------------------------
371 // wxMenu functions implemented in wxMenuItem
372 // ---------------------------------------------------------------------------
374 void wxMenu::Enable(int id
, bool Flag
)
376 wxMenuItem
*item
= FindItemForId(id
);
377 wxCHECK_RET( item
!= NULL
, _T("can't enable non-existing menu item") );
382 bool wxMenu::IsEnabled(int id
) const
384 wxMenuItem
*item
= FindItemForId(id
);
385 wxCHECK_MSG( item
!= NULL
, FALSE
, _T("invalid item id") );
387 return item
->IsEnabled();
390 void wxMenu::Check(int id
, bool Flag
)
392 wxMenuItem
*item
= FindItemForId(id
);
393 wxCHECK_RET( item
!= NULL
, _T("can't get status of non-existing menu item") );
398 bool wxMenu::IsChecked(int id
) const
400 wxMenuItem
*item
= FindItemForId(id
);
401 wxCHECK_MSG( item
!= NULL
, FALSE
, _T("invalid item id") );
403 return item
->IsChecked();
406 void wxMenu::SetLabel(int id
, const wxString
& label
)
408 wxMenuItem
*item
= FindItemForId(id
) ;
409 wxCHECK_RET( item
, _T("wxMenu::SetLabel: no such item") );
411 item
->SetName(label
);
414 wxString
wxMenu::GetLabel(int id
) const
417 wxMenuItem
*pItem
= FindItemForId(id
) ;
419 label
= pItem
->GetName() ;
421 wxFAIL_MSG(_T("wxMenu::GetLabel: item doesn't exist"));
426 void wxMenu::SetHelpString(int itemId
, const wxString
& helpString
)
428 wxMenuItem
*item
= FindItemForId (itemId
);
430 item
->SetHelp(helpString
);
432 wxFAIL_MSG(_T("wxMenu::SetHelpString: item doesn't exist"));
435 wxString
wxMenu::GetHelpString (int itemId
) const
438 wxMenuItem
*item
= FindItemForId (itemId
);
440 help
= item
->GetHelp();
442 wxFAIL_MSG(_T("wxMenu::GetHelpString: item doesn't exist"));
447 // ---------------------------------------------------------------------------
449 // ---------------------------------------------------------------------------
451 void wxMenu::SetTitle(const wxString
& label
)
453 bool hasNoTitle
= m_title
.IsEmpty();
456 HMENU hMenu
= GetHMENU();
460 if ( !label
.IsEmpty() )
462 if ( !InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
463 (unsigned)idMenuTitle
, m_title
) ||
464 !InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
466 wxLogLastError(_T("InsertMenu"));
472 if ( label
.IsEmpty() )
474 // remove the title and the separator after it
475 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
476 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
478 wxLogLastError("RemoveMenu");
484 if ( !ModifyMenu(hMenu
, 0u,
485 MF_BYPOSITION
| MF_STRING
,
486 (unsigned)idMenuTitle
, m_title
) )
488 wxLogLastError("ModifyMenu");
494 // put the title string in bold face
495 if ( !m_title
.IsEmpty() )
498 mii
.cbSize
= sizeof(mii
);
499 mii
.fMask
= MIIM_STATE
;
500 mii
.fState
= MFS_DEFAULT
;
502 if ( !SetMenuItemInfo(hMenu
, (unsigned)idMenuTitle
, FALSE
, &mii
) )
504 wxLogLastError("SetMenuItemInfo");
510 const wxString
wxMenu::GetTitle() const
515 // ---------------------------------------------------------------------------
517 // ---------------------------------------------------------------------------
519 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
521 // ignore commands from the menu title
523 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
524 if ( id
!= (WXWORD
)idMenuTitle
)
526 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
);
527 event
.SetEventObject( this );
530 ProcessCommand(event
);
536 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
538 bool processed
= FALSE
;
540 #if WXWIN_COMPATIBILITY
544 (void)(*(m_callback
))(*this, event
);
547 #endif // WXWIN_COMPATIBILITY
549 // Try the menu's event handler
550 if ( !processed
&& GetEventHandler())
552 processed
= GetEventHandler()->ProcessEvent(event
);
555 // Try the window the menu was popped up from (and up through the
557 wxWindow
*win
= GetInvokingWindow();
558 if ( !processed
&& win
)
559 processed
= win
->GetEventHandler()->ProcessEvent(event
);
564 // ---------------------------------------------------------------------------
566 // ---------------------------------------------------------------------------
568 // Finds the item id matching the given string, -1 if not found.
569 int wxMenu::FindItem (const wxString
& itemString
) const
571 wxString itemLabel
= wxStripMenuCodes(itemString
);
572 for ( wxNode
*node
= m_menuItems
.First(); node
; node
= node
->Next() )
574 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
575 if ( item
->IsSubMenu() )
577 int ans
= item
->GetSubMenu()->FindItem(itemString
);
578 if ( ans
!= wxNOT_FOUND
)
581 else if ( !item
->IsSeparator() )
583 wxString label
= wxStripMenuCodes(item
->GetName());
584 if ( itemLabel
== label
)
585 return item
->GetId();
592 wxMenuItem
*wxMenu::FindItemForId(int itemId
, wxMenu
** itemMenu
) const
597 wxMenuItem
*item
= NULL
;
598 for ( wxNode
*node
= m_menuItems
.First(); node
&& !item
; node
= node
->Next() )
600 item
= (wxMenuItem
*)node
->Data();
602 if ( item
->GetId() == itemId
)
605 *itemMenu
= (wxMenu
*)this;
607 else if ( item
->IsSubMenu() )
609 item
= item
->GetSubMenu()->FindItemForId(itemId
, itemMenu
);
613 // don't exit the loop
621 // ---------------------------------------------------------------------------
623 // ---------------------------------------------------------------------------
625 bool wxWindow::PopupMenu(wxMenu
*menu
, int x
, int y
)
627 menu
->SetInvokingWindow(this);
630 HWND hWnd
= (HWND
) GetHWND();
631 HMENU hMenu
= (HMENU
)menu
->GetHMenu();
635 ::ClientToScreen(hWnd
, &point
);
636 wxCurrentPopupMenu
= menu
;
637 ::TrackPopupMenu(hMenu
, TPM_RIGHTBUTTON
, point
.x
, point
.y
, 0, hWnd
, NULL
);
639 wxCurrentPopupMenu
= NULL
;
641 menu
->SetInvokingWindow(NULL
);
646 void wxMenu::Attach(wxMenuBar
*menubar
)
648 // menu can be in at most one menubar because otherwise they would both
649 // delete the menu pointer
650 wxASSERT_MSG( !m_menuBar
, _T("menu belongs to 2 menubars, expect a crash") );
653 m_savehMenu
= m_hMenu
;
657 void wxMenu::Detach()
659 wxASSERT_MSG( m_menuBar
, _T("can't detach menu if it's not attached") );
661 m_hMenu
= m_savehMenu
;
665 // ---------------------------------------------------------------------------
667 // ---------------------------------------------------------------------------
669 void wxMenuBar::Init()
671 m_eventHandler
= this;
675 m_menuBarFrame
= NULL
;
679 wxMenuBar::wxMenuBar()
684 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
689 wxMenuBar::wxMenuBar(int count
, wxMenu
*menus
[], const wxString titles
[])
695 m_titles
= new wxString
[count
];
698 for ( i
= 0; i
< count
; i
++ )
699 m_titles
[i
] = titles
[i
];
701 for ( i
= 0; i
< count
; i
++ )
702 m_menus
[i
]->Attach(this);
705 wxMenuBar::~wxMenuBar()
707 for ( int i
= 0; i
< m_menuCount
; i
++ )
716 // ---------------------------------------------------------------------------
718 // ---------------------------------------------------------------------------
720 void wxMenuBar::Refresh()
722 wxCHECK_RET( m_menuBarFrame
, _T("can't refresh a menubar withotu a frame") );
724 DrawMenuBar((HWND
)m_menuBarFrame
->GetHWND()) ;
727 WXHMENU
wxMenuBar::Create()
729 wxCHECK_MSG( !m_hMenu
, TRUE
, _T("menubar already created") );
731 m_hMenu
= (WXHMENU
)::CreateMenu();
735 wxLogLastError("CreateMenu");
739 for ( int i
= 0; i
< m_menuCount
; i
++ )
741 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
742 (UINT
)m_menus
[i
]->GetHMenu(),
745 wxLogLastError("AppendMenu");
753 // ---------------------------------------------------------------------------
754 // wxMenuBar functions forwarded to wxMenuItem
755 // ---------------------------------------------------------------------------
757 // Must only be used AFTER menu has been attached to frame,
758 // otherwise use individual menus to enable/disable items
759 void wxMenuBar::Enable(int id
, bool enable
)
761 wxMenu
*itemMenu
= NULL
;
762 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
764 wxCHECK_RET( item
, _T("attempt to enable an item which doesn't exist") );
766 item
->Enable(enable
);
769 void wxMenuBar::EnableTop(int pos
, bool enable
)
771 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;;
773 EnableMenuItem((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
| flag
);
776 // Must only be used AFTER menu has been attached to frame,
777 // otherwise use individual menus
778 void wxMenuBar::Check(int id
, bool check
)
780 wxMenu
*itemMenu
= NULL
;
781 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
783 wxCHECK_RET( item
, _T("attempt to check an item which doesn't exist") );
784 wxCHECK_RET( item
->IsCheckable(), _T("attempt to check an uncheckable item") );
789 bool wxMenuBar::IsChecked(int id
) const
791 wxMenu
*itemMenu
= NULL
;
792 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
794 wxCHECK_MSG( item
, FALSE
, _T("wxMenuBar::IsChecked(): no such item") );
796 int flag
= ::GetMenuState(GetHMenuOf(itemMenu
), id
, MF_BYCOMMAND
);
798 return (flag
& MF_CHECKED
) != 0;
801 bool wxMenuBar::IsEnabled(int id
) const
803 wxMenu
*itemMenu
= NULL
;
804 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
806 wxCHECK_MSG( item
, FALSE
, _T("wxMenuBar::IsEnabled(): no such item") );
808 int flag
= ::GetMenuState(GetHMenuOf(itemMenu
), id
, MF_BYCOMMAND
) ;
810 return (flag
& MF_ENABLED
) != 0;
813 void wxMenuBar::SetLabel(int id
, const wxString
& label
)
815 wxMenu
*itemMenu
= NULL
;
816 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
818 wxCHECK_RET( item
, _T("wxMenuBar::SetLabel(): no such item") );
820 item
->SetName(label
);
823 wxString
wxMenuBar::GetLabel(int id
) const
825 wxMenu
*itemMenu
= NULL
;
826 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
828 wxCHECK_MSG( item
, _T(""), _T("wxMenuBar::GetLabel(): no such item") );
830 return item
->GetName();
833 void wxMenuBar::SetHelpString (int id
, const wxString
& helpString
)
835 wxMenu
*itemMenu
= NULL
;
836 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
838 wxCHECK_RET( item
, _T("wxMenuBar::SetHelpString(): no such item") );
840 item
->SetHelp(helpString
);
843 wxString
wxMenuBar::GetHelpString (int id
) const
845 wxMenu
*itemMenu
= NULL
;
846 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
848 wxCHECK_MSG( item
, _T(""), _T("wxMenuBar::GetHelpString(): no such item") );
850 return item
->GetHelp();
853 // ---------------------------------------------------------------------------
854 // wxMenuBar functions to work with the top level submenus
855 // ---------------------------------------------------------------------------
857 // NB: we don't support owner drawn top level items for now, if we do these
858 // functions would have to be changed to use wxMenuItem as well
860 void wxMenuBar::SetLabelTop(int pos
, const wxString
& label
)
863 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
);
864 if ( flagsOld
== 0xFFFFFFFF )
866 wxLogLastError(_T("GetMenuState"));
871 if ( flagsOld
& MF_POPUP
)
873 // HIBYTE contains the number of items in the submenu in this case
875 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, pos
) ;
882 if ( ::ModifyMenu(GetHMENU(), pos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
883 id
, label
) == 0xFFFFFFFF )
885 wxLogLastError("ModifyMenu");
889 wxString
wxMenuBar::GetLabelTop(int pos
) const
891 int len
= ::GetMenuString((HMENU
)m_hMenu
, pos
, NULL
, 0, MF_BYCOMMAND
);
893 len
++; // for the NUL character
895 ::GetMenuString(GetHMENU(), pos
, label
.GetWriteBuf(len
), len
, MF_BYCOMMAND
);
896 label
.UngetWriteBuf();
901 // ---------------------------------------------------------------------------
902 // wxMenuBar notifications
903 // ---------------------------------------------------------------------------
905 bool wxMenuBar::OnDelete(wxMenu
*a_menu
, int pos
)
907 if ( !m_menuBarFrame
)
910 if ( ::RemoveMenu((HMENU
)m_hMenu
, (UINT
)pos
, MF_BYPOSITION
) )
912 // VZ: I'm not sure about what's going on here, so I leave an assert
913 wxASSERT_MSG( m_menus
[pos
] == a_menu
, _T("what is this parameter for??") );
917 if ( m_menuBarFrame
)
924 wxLogLastError("RemoveMenu");
930 bool wxMenuBar::OnAppend(wxMenu
*a_menu
, const wxChar
*title
)
932 WXHMENU submenu
= a_menu
->GetHMenu();
936 if ( !m_menuBarFrame
)
939 a_menu
->Attach(this);
941 if ( !::AppendMenu(GetHMENU(), MF_POPUP
| MF_STRING
,
942 (UINT
)submenu
, title
) )
944 wxLogLastError(_T("AppendMenu"));
952 // ---------------------------------------------------------------------------
953 // wxMenuBar construction
954 // ---------------------------------------------------------------------------
956 void wxMenuBar::Append (wxMenu
* menu
, const wxString
& title
)
958 if (!OnAppend(menu
, title
))
962 wxMenu
**new_menus
= new wxMenu
*[m_menuCount
];
963 wxString
*new_titles
= new wxString
[m_menuCount
];
966 for (i
= 0; i
< m_menuCount
- 1; i
++)
968 new_menus
[i
] = m_menus
[i
];
970 new_titles
[i
] = m_titles
[i
];
971 m_titles
[i
] = _T("");
979 m_titles
= new_titles
;
981 m_menus
[m_menuCount
- 1] = (wxMenu
*)menu
;
982 m_titles
[m_menuCount
- 1] = title
;
984 menu
->SetParent(this);
987 void wxMenuBar::Delete(wxMenu
* menu
, int i
)
993 for (ii
= 0; ii
< m_menuCount
; ii
++) {
994 if (m_menus
[ii
] == menu
)
997 if (ii
>= m_menuCount
)
1000 if (ii
< 0 || ii
>= m_menuCount
)
1005 if (!OnDelete(menu
, ii
))
1008 menu
->SetParent(NULL
);
1011 for (j
= ii
; j
< m_menuCount
; j
++) {
1012 m_menus
[j
] = m_menus
[j
+ 1];
1013 m_titles
[j
] = m_titles
[j
+ 1];
1017 void wxMenuBar::Attach(wxFrame
*frame
)
1019 wxASSERT_MSG( !m_menuBarFrame
, _T("menubar already attached!") );
1021 m_menuBarFrame
= frame
;
1024 // create the accel table - we consider that the menubar construction is
1026 size_t nAccelCount
= 0;
1028 for ( i
= 0; i
< m_menuCount
; i
++ )
1030 nAccelCount
+= m_menus
[i
]->GetAccelCount();
1035 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1038 for ( i
= 0; i
< m_menuCount
; i
++ )
1040 nAccelCount
+= m_menus
[i
]->CopyAccels(&accelEntries
[nAccelCount
]);
1043 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1045 delete [] accelEntries
;
1047 #endif // wxUSE_ACCEL
1050 // ---------------------------------------------------------------------------
1051 // wxMenuBar searching for menu items
1052 // ---------------------------------------------------------------------------
1054 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
1055 int wxMenuBar::FindMenuItem(const wxString
& menuString
,
1056 const wxString
& itemString
) const
1058 wxString menuLabel
= wxStripMenuCodes(menuString
);
1059 for ( int i
= 0; i
< m_menuCount
; i
++ )
1061 wxString title
= wxStripMenuCodes(m_titles
[i
]);
1062 if ( menuString
== title
)
1063 return m_menus
[i
]->FindItem(itemString
);
1069 wxMenuItem
*wxMenuBar::FindItemForId (int id
, wxMenu
**itemMenu
) const
1074 wxMenuItem
*item
= NULL
;
1075 for ( int i
= 0; !item
&& (i
< m_menuCount
); i
++ )
1077 item
= m_menus
[i
]->FindItemForId(id
, itemMenu
);
1084 // ----------------------------------------------------------------------------
1086 // ----------------------------------------------------------------------------
1088 wxWindow
*wxMenu::GetWindow() const
1090 if ( m_pInvokingWindow
!= NULL
)
1091 return m_pInvokingWindow
;
1092 else if ( m_menuBar
!= NULL
)
1093 return m_menuBar
->GetFrame();
1098 WXHMENU
wxMenu::GetHMenu() const
1102 else if ( m_savehMenu
!= 0 )
1105 wxFAIL_MSG(_T("wxMenu without HMENU"));
1110 // Update a menu and all submenus recursively. source is the object that has
1111 // the update event handlers defined for it. If NULL, the menu or associated
1112 // window will be used.
1113 void wxMenu::UpdateUI(wxEvtHandler
* source
)
1115 if (!source
&& GetInvokingWindow())
1116 source
= GetInvokingWindow()->GetEventHandler();
1118 source
= GetEventHandler();
1122 wxNode
* node
= GetItems().First();
1125 wxMenuItem
* item
= (wxMenuItem
*) node
->Data();
1126 if ( !item
->IsSeparator() )
1128 wxWindowID id
= item
->GetId();
1129 wxUpdateUIEvent
event(id
);
1130 event
.SetEventObject( source
);
1132 if (source
->ProcessEvent(event
))
1134 if (event
.GetSetText())
1135 SetLabel(id
, event
.GetText());
1136 if (event
.GetSetChecked())
1137 Check(id
, event
.GetChecked());
1138 if (event
.GetSetEnabled())
1139 Enable(id
, event
.GetEnabled());
1142 if (item
->GetSubMenu())
1143 item
->GetSubMenu()->UpdateUI(source
);
1145 node
= node
->Next();