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
)
72 // ============================================================================
74 // ============================================================================
76 // ---------------------------------------------------------------------------
77 // wxMenu construction, adding and removing menu items
78 // ---------------------------------------------------------------------------
80 // Construct a menu with optional title (then use append)
81 void wxMenu::Init(const wxString
& title
, const wxFunction func
)
85 m_eventHandler
= this;
86 m_pInvokingWindow
= NULL
;
90 m_hMenu
= (WXHMENU
) CreatePopupMenu();
92 m_topLevelMenu
= this;
93 m_clientData
= (void*) NULL
;
97 Append(idMenuTitle
, m_title
) ;
104 // The wxWindow destructor will take care of deleting the submenus.
107 // free Windows resources
110 ::DestroyMenu((HMENU
)m_hMenu
);
115 wxNode
*node
= m_menuItems
.First();
118 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
120 // Delete child menus.
121 // Beware: they must not be appended to children list!!!
122 // (because order of delete is significant)
123 if ( item
->IsSubMenu() )
124 item
->DeleteSubMenu();
126 wxNode
*next
= node
->Next();
138 // function appends a new item or submenu to the menu
139 void wxMenu::Append(wxMenuItem
*pItem
)
141 wxCHECK_RET( pItem
!= NULL
, wxT("can't append NULL item to the menu") );
144 // check for accelerators: they are given after '\t'
145 wxString label
= pItem
->GetName();
146 int posTab
= label
.Find(wxT('\t'));
147 if ( posTab
!= wxNOT_FOUND
) {
148 // parse the accelerator string
150 int accelFlags
= wxACCEL_NORMAL
;
152 for ( size_t n
= (size_t)posTab
+ 1; n
< label
.Len(); n
++ ) {
153 if ( (label
[n
] == '+') || (label
[n
] == '-') ) {
154 if ( current
== _("ctrl") )
155 accelFlags
|= wxACCEL_CTRL
;
156 else if ( current
== _("alt") )
157 accelFlags
|= wxACCEL_ALT
;
158 else if ( current
== _("shift") )
159 accelFlags
|= wxACCEL_SHIFT
;
161 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
168 current
+= wxTolower(label
[n
]);
172 if ( current
.IsEmpty() ) {
173 wxLogDebug(wxT("No accel key found, accel string ignored."));
176 if ( current
.Len() == 1 ) {
178 keyCode
= wxToupper(current
[0U]);
181 // is it a function key?
182 if ( current
[0U] == 'f' && isdigit(current
[1U]) &&
183 (current
.Len() == 2 ||
184 (current
.Len() == 3 && isdigit(current
[2U]))) ) {
186 wxSscanf(current
.c_str() + 1, wxT("%d"), &n
);
188 keyCode
= VK_F1
+ n
- 1;
191 // several special cases
193 if ( current
== wxT("DEL") ) {
196 else if ( current
== wxT("PGUP") ) {
199 else if ( current
== wxT("PGDN") ) {
203 wxLogDebug(wxT("Unrecognized accel key '%s', accel "
204 "string ignored."), current
.c_str());
212 m_accelKeyCodes
.Add(keyCode
);
213 m_accelFlags
.Add(accelFlags
);
214 m_accelIds
.Add(pItem
->GetId());
217 #endif // wxUSE_ACCEL
221 // if "Break" has just been called, insert a menu break before this item
222 // (and don't forget to reset the flag)
224 flags
|= MF_MENUBREAK
;
228 if ( pItem
->IsSeparator() ) {
229 flags
|= MF_SEPARATOR
;
232 // id is the numeric id for normal menu items and HMENU for submenus as
233 // required by ::AppendMenu() API
235 wxMenu
*submenu
= pItem
->GetSubMenu();
236 if ( submenu
!= NULL
) {
237 wxASSERT( submenu
->GetHMenu() != (WXHMENU
) NULL
);
239 id
= (UINT
)submenu
->GetHMenu();
240 submenu
->m_topLevelMenu
= m_topLevelMenu
;
241 submenu
->m_parent
= this;
242 submenu
->m_savehMenu
= (WXHMENU
)id
;
243 submenu
->m_hMenu
= 0;
253 #if wxUSE_OWNER_DRAWN
254 if ( pItem
->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
255 // item draws itself, pass pointer to it in data parameter
256 flags
|= MF_OWNERDRAW
;
257 pData
= (LPCTSTR
)pItem
;
262 // menu is just a normal string (passed in data parameter)
267 if ( !::AppendMenu(GetHmenu(), flags
, id
, pData
) )
269 wxLogLastError("AppendMenu");
274 if ( (int)id
== idMenuTitle
)
276 // visually select the menu title
278 mii
.cbSize
= sizeof(mii
);
279 mii
.fMask
= MIIM_STATE
;
280 mii
.fState
= MFS_DEFAULT
;
282 if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id
, FALSE
, &mii
) )
284 wxLogLastError(wxT("SetMenuItemInfo"));
289 m_menuItems
.Append(pItem
);
294 void wxMenu::AppendSeparator()
296 Append(new wxMenuItem(this, ID_SEPARATOR
));
300 void wxMenu::Append(int id
,
301 const wxString
& label
,
303 const wxString
& helpString
)
305 Append(new wxMenuItem(this, id
, label
, helpString
, FALSE
, SubMenu
));
308 // Ordinary menu item
309 void wxMenu::Append(int id
,
310 const wxString
& label
,
311 const wxString
& helpString
,
314 // 'checkable' parameter is useless for Windows.
315 Append(new wxMenuItem(this, id
, label
, helpString
, checkable
));
319 void wxMenu::Delete(int id
)
321 wxMenuItem
*item
= NULL
;
324 for (pos
= 0, node
= m_menuItems
.First(); node
; node
= node
->Next(), pos
++)
326 item
= (wxMenuItem
*)node
->Data();
327 if ( item
->GetId() == id
)
331 wxCHECK_RET( node
, wxT("wxMenu::Delete(): item doesn't exist") );
333 HMENU menu
= GetHmenu();
335 wxMenu
*pSubMenu
= item
->GetSubMenu();
336 if ( pSubMenu
!= NULL
) {
337 RemoveMenu(menu
, (UINT
)pos
, MF_BYPOSITION
);
338 pSubMenu
->m_hMenu
= pSubMenu
->m_savehMenu
;
339 pSubMenu
->m_savehMenu
= 0;
340 pSubMenu
->m_parent
= NULL
;
341 // RemoveChild(item->subMenu);
342 pSubMenu
->m_topLevelMenu
= NULL
;
343 // TODO: Why isn't subMenu deleted here???
344 // Will put this in for now. Assuming this is supposed
345 // to delete the menu, not just remove it.
346 item
->DeleteSubMenu();
349 DeleteMenu(menu
, (UINT
)pos
, MF_BYPOSITION
);
352 m_menuItems
.DeleteNode(node
);
358 // ---------------------------------------------------------------------------
359 // accelerator helpers
360 // ---------------------------------------------------------------------------
362 // create the wxAcceleratorEntries for our accels and put them into provided
363 // array - return the number of accels we have
364 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
366 size_t count
= GetAccelCount();
367 for ( size_t n
= 0; n
< count
; n
++ )
369 (*accels
++).Set(m_accelFlags
[n
], m_accelKeyCodes
[n
], m_accelIds
[n
]);
375 #endif // wxUSE_ACCEL
377 // ---------------------------------------------------------------------------
378 // wxMenu functions implemented in wxMenuItem
379 // ---------------------------------------------------------------------------
381 void wxMenu::Enable(int id
, bool Flag
)
383 wxMenuItem
*item
= FindItemForId(id
);
384 wxCHECK_RET( item
!= NULL
, wxT("can't enable non-existing menu item") );
389 bool wxMenu::IsEnabled(int id
) const
391 wxMenuItem
*item
= FindItemForId(id
);
392 wxCHECK_MSG( item
!= NULL
, FALSE
, wxT("invalid item id") );
394 return item
->IsEnabled();
397 void wxMenu::Check(int id
, bool Flag
)
399 wxMenuItem
*item
= FindItemForId(id
);
400 wxCHECK_RET( item
!= NULL
, wxT("can't get status of non-existing menu item") );
405 bool wxMenu::IsChecked(int id
) const
407 wxMenuItem
*item
= FindItemForId(id
);
408 wxCHECK_MSG( item
!= NULL
, FALSE
, wxT("invalid item id") );
410 return item
->IsChecked();
413 void wxMenu::SetLabel(int id
, const wxString
& label
)
415 wxMenuItem
*item
= FindItemForId(id
) ;
416 wxCHECK_RET( item
, wxT("wxMenu::SetLabel: no such item") );
418 item
->SetName(label
);
421 wxString
wxMenu::GetLabel(int id
) const
424 wxMenuItem
*pItem
= FindItemForId(id
) ;
426 label
= pItem
->GetName() ;
428 wxFAIL_MSG(wxT("wxMenu::GetLabel: item doesn't exist"));
433 void wxMenu::SetHelpString(int itemId
, const wxString
& helpString
)
435 wxMenuItem
*item
= FindItemForId (itemId
);
437 item
->SetHelp(helpString
);
439 wxFAIL_MSG(wxT("wxMenu::SetHelpString: item doesn't exist"));
442 wxString
wxMenu::GetHelpString (int itemId
) const
445 wxMenuItem
*item
= FindItemForId (itemId
);
447 help
= item
->GetHelp();
449 wxFAIL_MSG(wxT("wxMenu::GetHelpString: item doesn't exist"));
454 // ---------------------------------------------------------------------------
456 // ---------------------------------------------------------------------------
458 void wxMenu::SetTitle(const wxString
& label
)
460 bool hasNoTitle
= m_title
.IsEmpty();
463 HMENU hMenu
= GetHmenu();
467 if ( !label
.IsEmpty() )
469 if ( !InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
470 (unsigned)idMenuTitle
, m_title
) ||
471 !InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
473 wxLogLastError(wxT("InsertMenu"));
479 if ( label
.IsEmpty() )
481 // remove the title and the separator after it
482 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
483 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
485 wxLogLastError("RemoveMenu");
491 if ( !ModifyMenu(hMenu
, 0u,
492 MF_BYPOSITION
| MF_STRING
,
493 (unsigned)idMenuTitle
, m_title
) )
495 wxLogLastError("ModifyMenu");
501 // put the title string in bold face
502 if ( !m_title
.IsEmpty() )
505 mii
.cbSize
= sizeof(mii
);
506 mii
.fMask
= MIIM_STATE
;
507 mii
.fState
= MFS_DEFAULT
;
509 if ( !SetMenuItemInfo(hMenu
, (unsigned)idMenuTitle
, FALSE
, &mii
) )
511 wxLogLastError("SetMenuItemInfo");
517 const wxString
wxMenu::GetTitle() const
522 // ---------------------------------------------------------------------------
524 // ---------------------------------------------------------------------------
526 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
528 // ignore commands from the menu title
530 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
531 if ( id
!= (WXWORD
)idMenuTitle
)
533 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
);
534 event
.SetEventObject( this );
537 ProcessCommand(event
);
543 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
545 bool processed
= FALSE
;
550 (void)(*(m_callback
))(*this, event
);
554 // Try the menu's event handler
555 if ( !processed
&& GetEventHandler())
557 processed
= GetEventHandler()->ProcessEvent(event
);
560 // Try the window the menu was popped up from (and up through the
562 wxWindow
*win
= GetInvokingWindow();
563 if ( !processed
&& win
)
564 processed
= win
->GetEventHandler()->ProcessEvent(event
);
569 // ---------------------------------------------------------------------------
571 // ---------------------------------------------------------------------------
573 // Finds the item id matching the given string, -1 if not found.
574 int wxMenu::FindItem (const wxString
& itemString
) const
576 wxString itemLabel
= wxStripMenuCodes(itemString
);
577 for ( wxNode
*node
= m_menuItems
.First(); node
; node
= node
->Next() )
579 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
580 if ( item
->IsSubMenu() )
582 int ans
= item
->GetSubMenu()->FindItem(itemString
);
583 if ( ans
!= wxNOT_FOUND
)
586 else if ( !item
->IsSeparator() )
588 wxString label
= wxStripMenuCodes(item
->GetName());
589 if ( itemLabel
== label
)
590 return item
->GetId();
597 wxMenuItem
*wxMenu::FindItemForId(int itemId
, wxMenu
** itemMenu
) const
602 wxMenuItem
*item
= NULL
;
603 for ( wxNode
*node
= m_menuItems
.First(); node
&& !item
; node
= node
->Next() )
605 item
= (wxMenuItem
*)node
->Data();
607 if ( item
->GetId() == itemId
)
610 *itemMenu
= (wxMenu
*)this;
612 else if ( item
->IsSubMenu() )
614 item
= item
->GetSubMenu()->FindItemForId(itemId
, itemMenu
);
618 // don't exit the loop
626 // ---------------------------------------------------------------------------
628 // ---------------------------------------------------------------------------
630 void wxMenu::Attach(wxMenuBar
*menubar
)
632 // menu can be in at most one menubar because otherwise they would both
633 // delete the menu pointer
634 wxASSERT_MSG( !m_menuBar
, wxT("menu belongs to 2 menubars, expect a crash") );
637 m_savehMenu
= m_hMenu
;
641 void wxMenu::Detach()
643 wxASSERT_MSG( m_menuBar
, wxT("can't detach menu if it's not attached") );
645 m_hMenu
= m_savehMenu
;
649 // ---------------------------------------------------------------------------
651 // ---------------------------------------------------------------------------
653 void wxMenuBar::Init()
655 m_eventHandler
= this;
659 m_menuBarFrame
= NULL
;
663 wxMenuBar::wxMenuBar()
668 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
673 wxMenuBar::wxMenuBar(int count
, wxMenu
*menus
[], const wxString titles
[])
679 m_titles
= new wxString
[count
];
682 for ( i
= 0; i
< count
; i
++ )
683 m_titles
[i
] = titles
[i
];
685 for ( i
= 0; i
< count
; i
++ )
686 m_menus
[i
]->Attach(this);
689 wxMenuBar::~wxMenuBar()
691 for ( int i
= 0; i
< m_menuCount
; i
++ )
700 // ---------------------------------------------------------------------------
702 // ---------------------------------------------------------------------------
704 void wxMenuBar::Refresh()
706 wxCHECK_RET( m_menuBarFrame
, wxT("can't refresh a menubar withotu a frame") );
708 DrawMenuBar((HWND
)m_menuBarFrame
->GetHWND()) ;
711 WXHMENU
wxMenuBar::Create()
716 wxCHECK_MSG( !m_hMenu
, TRUE
, wxT("menubar already created") );
718 m_hMenu
= (WXHMENU
)::CreateMenu();
722 wxLogLastError("CreateMenu");
726 for ( int i
= 0; i
< m_menuCount
; i
++ )
728 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
729 (UINT
)m_menus
[i
]->GetHMenu(),
732 wxLogLastError("AppendMenu");
740 // ---------------------------------------------------------------------------
741 // wxMenuBar functions forwarded to wxMenuItem
742 // ---------------------------------------------------------------------------
744 // Must only be used AFTER menu has been attached to frame,
745 // otherwise use individual menus to enable/disable items
746 void wxMenuBar::Enable(int id
, bool enable
)
748 wxMenu
*itemMenu
= NULL
;
749 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
751 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
753 item
->Enable(enable
);
756 void wxMenuBar::EnableTop(int pos
, bool enable
)
758 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;;
760 EnableMenuItem((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
| flag
);
763 // Must only be used AFTER menu has been attached to frame,
764 // otherwise use individual menus
765 void wxMenuBar::Check(int id
, bool check
)
767 wxMenu
*itemMenu
= NULL
;
768 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
770 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
771 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
776 bool wxMenuBar::IsChecked(int id
) const
778 wxMenu
*itemMenu
= NULL
;
779 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
781 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
783 int flag
= ::GetMenuState(GetHmenuOf(itemMenu
), id
, MF_BYCOMMAND
);
785 return (flag
& MF_CHECKED
) != 0;
788 bool wxMenuBar::IsEnabled(int id
) const
790 wxMenu
*itemMenu
= NULL
;
791 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
793 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
795 int flag
= ::GetMenuState(GetHmenuOf(itemMenu
), id
, MF_BYCOMMAND
) ;
797 // don't "and" with MF_ENABLED because its value is 0
798 return (flag
& MF_DISABLED
) == 0;
801 void wxMenuBar::SetLabel(int id
, const wxString
& label
)
803 wxMenu
*itemMenu
= NULL
;
804 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
806 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
808 item
->SetName(label
);
811 wxString
wxMenuBar::GetLabel(int id
) const
813 wxMenu
*itemMenu
= NULL
;
814 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
816 wxCHECK_MSG( item
, wxT(""), wxT("wxMenuBar::GetLabel(): no such item") );
818 return item
->GetName();
821 void wxMenuBar::SetHelpString (int id
, const wxString
& helpString
)
823 wxMenu
*itemMenu
= NULL
;
824 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
826 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
828 item
->SetHelp(helpString
);
831 wxString
wxMenuBar::GetHelpString (int id
) const
833 wxMenu
*itemMenu
= NULL
;
834 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
836 wxCHECK_MSG( item
, wxT(""), wxT("wxMenuBar::GetHelpString(): no such item") );
838 return item
->GetHelp();
841 // ---------------------------------------------------------------------------
842 // wxMenuBar functions to work with the top level submenus
843 // ---------------------------------------------------------------------------
845 // NB: we don't support owner drawn top level items for now, if we do these
846 // functions would have to be changed to use wxMenuItem as well
848 void wxMenuBar::SetLabelTop(int pos
, const wxString
& label
)
851 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
);
852 if ( flagsOld
== 0xFFFFFFFF )
854 wxLogLastError(wxT("GetMenuState"));
859 if ( flagsOld
& MF_POPUP
)
861 // HIBYTE contains the number of items in the submenu in this case
863 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, pos
) ;
870 if ( ::ModifyMenu(GetHmenu(), pos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
871 id
, label
) == (int)0xFFFFFFFF )
873 wxLogLastError("ModifyMenu");
877 wxString
wxMenuBar::GetLabelTop(int pos
) const
879 int len
= ::GetMenuString((HMENU
)m_hMenu
, pos
, NULL
, 0, MF_BYCOMMAND
);
881 len
++; // for the NUL character
883 ::GetMenuString(GetHmenu(), pos
, label
.GetWriteBuf(len
), len
, MF_BYCOMMAND
);
884 label
.UngetWriteBuf();
889 // ---------------------------------------------------------------------------
890 // wxMenuBar notifications
891 // ---------------------------------------------------------------------------
893 bool wxMenuBar::OnDelete(wxMenu
*a_menu
, int pos
)
895 if ( !m_menuBarFrame
)
898 if ( ::RemoveMenu((HMENU
)m_hMenu
, (UINT
)pos
, MF_BYPOSITION
) )
900 // VZ: I'm not sure about what's going on here, so I leave an assert
901 wxASSERT_MSG( m_menus
[pos
] == a_menu
, wxT("what is this parameter for??") );
905 if ( m_menuBarFrame
)
912 wxLogLastError("RemoveMenu");
918 bool wxMenuBar::OnAppend(wxMenu
*a_menu
, const wxChar
*title
)
920 WXHMENU submenu
= a_menu
->GetHMenu();
924 if ( !m_menuBarFrame
)
927 a_menu
->Attach(this);
929 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
930 (UINT
)submenu
, title
) )
932 wxLogLastError(wxT("AppendMenu"));
940 // ---------------------------------------------------------------------------
941 // wxMenuBar construction
942 // ---------------------------------------------------------------------------
943 int wxMenuBar::FindMenu(const wxString
& title
)
945 wxString menuTitle
= wxStripMenuCodes(title
);
946 for ( int i
= 0; i
< m_menuCount
; i
++ )
948 wxString title
= wxStripMenuCodes(m_titles
[i
]);
949 if ( menuTitle
== title
)
958 void wxMenuBar::ReplaceMenu(int pos
, wxMenu
* new_menu
, const wxString
& title
)
960 if (m_menuBarFrame
) return;
962 if ( pos
>= 0 && pos
< m_menuCount
)
964 wxMenu
*old_menu
= m_menus
[pos
];
965 m_menus
[pos
] = new_menu
;
972 void wxMenuBar::Insert(int pos
, wxMenu
* menu
, const wxString
& title
)
974 if (m_menuBarFrame
) return;
975 if ( pos
< 0 && pos
>= m_menuCount
) return;
978 wxMenu
**new_menus
= new wxMenu
*[m_menuCount
];
979 wxString
*new_titles
= new wxString
[m_menuCount
];
982 for (i
= 0; i
< pos
; i
++)
984 new_menus
[i
] = m_menus
[i
];
986 new_titles
[i
] = m_titles
[i
];
987 m_titles
[i
] = wxT("");
990 new_menus
[pos
] = (wxMenu
*)menu
;
991 new_titles
[i
] = title
;
993 for (i
= pos
+1; i
< m_menuCount
; i
++)
995 new_menus
[i
] = m_menus
[i
-1];
997 new_titles
[i
] = m_titles
[i
-1];
998 m_titles
[i
-1] = wxT("");
1005 m_menus
= new_menus
;
1006 m_titles
= new_titles
;
1008 menu
->SetParent(this);
1013 void wxMenuBar::Append (wxMenu
* menu
, const wxString
& title
)
1015 if (!OnAppend(menu
, title
))
1019 wxMenu
**new_menus
= new wxMenu
*[m_menuCount
];
1020 wxString
*new_titles
= new wxString
[m_menuCount
];
1023 for (i
= 0; i
< m_menuCount
- 1; i
++)
1025 new_menus
[i
] = m_menus
[i
];
1027 new_titles
[i
] = m_titles
[i
];
1028 m_titles
[i
] = wxT("");
1035 m_menus
= new_menus
;
1036 m_titles
= new_titles
;
1038 m_menus
[m_menuCount
- 1] = (wxMenu
*)menu
;
1039 m_titles
[m_menuCount
- 1] = title
;
1041 menu
->SetParent(this);
1044 void wxMenuBar::Delete(wxMenu
* menu
, int i
)
1050 for (ii
= 0; ii
< m_menuCount
; ii
++) {
1051 if (m_menus
[ii
] == menu
)
1054 if (ii
>= m_menuCount
)
1057 if (ii
< 0 || ii
>= m_menuCount
)
1062 if (!OnDelete(menu
, ii
))
1065 menu
->SetParent(NULL
);
1068 for (j
= ii
; j
< m_menuCount
; j
++) {
1069 m_menus
[j
] = m_menus
[j
+ 1];
1070 m_titles
[j
] = m_titles
[j
+ 1];
1074 void wxMenuBar::Attach(wxFrame
*frame
)
1076 wxASSERT_MSG( !m_menuBarFrame
, wxT("menubar already attached!") );
1078 m_menuBarFrame
= frame
;
1081 // create the accel table - we consider that the menubar construction is
1083 size_t nAccelCount
= 0;
1085 for ( i
= 0; i
< m_menuCount
; i
++ )
1087 nAccelCount
+= m_menus
[i
]->GetAccelCount();
1092 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1095 for ( i
= 0; i
< m_menuCount
; i
++ )
1097 nAccelCount
+= m_menus
[i
]->CopyAccels(&accelEntries
[nAccelCount
]);
1100 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1102 delete [] accelEntries
;
1104 #endif // wxUSE_ACCEL
1107 void wxMenuBar::Detach()
1109 // ::DestroyMenu((HMENU)m_hMenu);
1110 m_hMenu
= (WXHMENU
)NULL
;
1111 m_menuBarFrame
= NULL
;
1115 // ---------------------------------------------------------------------------
1116 // wxMenuBar searching for menu items
1117 // ---------------------------------------------------------------------------
1119 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
1120 int wxMenuBar::FindMenuItem(const wxString
& menuString
,
1121 const wxString
& itemString
) const
1123 wxString menuLabel
= wxStripMenuCodes(menuString
);
1124 for ( int i
= 0; i
< m_menuCount
; i
++ )
1126 wxString title
= wxStripMenuCodes(m_titles
[i
]);
1127 if ( menuString
== title
)
1128 return m_menus
[i
]->FindItem(itemString
);
1134 wxMenuItem
*wxMenuBar::FindItemForId (int id
, wxMenu
**itemMenu
) const
1139 wxMenuItem
*item
= NULL
;
1140 for ( int i
= 0; !item
&& (i
< m_menuCount
); i
++ )
1142 item
= m_menus
[i
]->FindItemForId(id
, itemMenu
);
1149 // ----------------------------------------------------------------------------
1151 // ----------------------------------------------------------------------------
1153 wxWindow
*wxMenu::GetWindow() const
1155 if ( m_pInvokingWindow
!= NULL
)
1156 return m_pInvokingWindow
;
1157 else if ( m_menuBar
!= NULL
)
1158 return m_menuBar
->GetFrame();
1163 WXHMENU
wxMenu::GetHMenu() const
1167 else if ( m_savehMenu
!= 0 )
1170 wxFAIL_MSG(wxT("wxMenu without HMENU"));
1175 // Update a menu and all submenus recursively. source is the object that has
1176 // the update event handlers defined for it. If NULL, the menu or associated
1177 // window will be used.
1178 void wxMenu::UpdateUI(wxEvtHandler
* source
)
1180 if (!source
&& GetInvokingWindow())
1181 source
= GetInvokingWindow()->GetEventHandler();
1183 source
= GetEventHandler();
1187 wxNode
* node
= GetItems().First();
1190 wxMenuItem
* item
= (wxMenuItem
*) node
->Data();
1191 if ( !item
->IsSeparator() )
1193 wxWindowID id
= item
->GetId();
1194 wxUpdateUIEvent
event(id
);
1195 event
.SetEventObject( source
);
1197 if (source
->ProcessEvent(event
))
1199 if (event
.GetSetText())
1200 SetLabel(id
, event
.GetText());
1201 if (event
.GetSetChecked())
1202 Check(id
, event
.GetChecked());
1203 if (event
.GetSetEnabled())
1204 Enable(id
, event
.GetEnabled());
1207 if (item
->GetSubMenu())
1208 item
->GetSubMenu()->UpdateUI(source
);
1210 node
= node
->Next();