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 // it should be 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 wxLogDebug(wxT("Unrecognized accel key '%s', accel "
192 "string ignored."), current
.c_str());
199 m_accelKeyCodes
.Add(keyCode
);
200 m_accelFlags
.Add(accelFlags
);
201 m_accelIds
.Add(pItem
->GetId());
204 #endif // wxUSE_ACCEL
208 // if "Break" has just been called, insert a menu break before this item
209 // (and don't forget to reset the flag)
211 flags
|= MF_MENUBREAK
;
215 if ( pItem
->IsSeparator() ) {
216 flags
|= MF_SEPARATOR
;
219 // id is the numeric id for normal menu items and HMENU for submenus as
220 // required by ::AppendMenu() API
222 wxMenu
*submenu
= pItem
->GetSubMenu();
223 if ( submenu
!= NULL
) {
224 wxASSERT( submenu
->GetHMenu() != (WXHMENU
) NULL
);
226 id
= (UINT
)submenu
->GetHMenu();
227 submenu
->m_topLevelMenu
= m_topLevelMenu
;
228 submenu
->m_parent
= this;
229 submenu
->m_savehMenu
= (WXHMENU
)id
;
230 submenu
->m_hMenu
= 0;
240 #if wxUSE_OWNER_DRAWN
241 if ( pItem
->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
242 // item draws itself, pass pointer to it in data parameter
243 flags
|= MF_OWNERDRAW
;
244 pData
= (LPCTSTR
)pItem
;
249 // menu is just a normal string (passed in data parameter)
254 if ( !::AppendMenu(GetHmenu(), flags
, id
, pData
) )
256 wxLogLastError("AppendMenu");
261 if ( (int)id
== idMenuTitle
)
263 // visually select the menu title
265 mii
.cbSize
= sizeof(mii
);
266 mii
.fMask
= MIIM_STATE
;
267 mii
.fState
= MFS_DEFAULT
;
269 if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id
, FALSE
, &mii
) )
271 wxLogLastError(wxT("SetMenuItemInfo"));
276 m_menuItems
.Append(pItem
);
281 void wxMenu::AppendSeparator()
283 Append(new wxMenuItem(this, ID_SEPARATOR
));
287 void wxMenu::Append(int id
,
288 const wxString
& label
,
290 const wxString
& helpString
)
292 Append(new wxMenuItem(this, id
, label
, helpString
, FALSE
, SubMenu
));
295 // Ordinary menu item
296 void wxMenu::Append(int id
,
297 const wxString
& label
,
298 const wxString
& helpString
,
301 // 'checkable' parameter is useless for Windows.
302 Append(new wxMenuItem(this, id
, label
, helpString
, checkable
));
306 void wxMenu::Delete(int id
)
308 wxMenuItem
*item
= NULL
;
311 for (pos
= 0, node
= m_menuItems
.First(); node
; node
= node
->Next(), pos
++)
313 item
= (wxMenuItem
*)node
->Data();
314 if ( item
->GetId() == id
)
318 wxCHECK_RET( node
, wxT("wxMenu::Delete(): item doesn't exist") );
320 HMENU menu
= GetHmenu();
322 wxMenu
*pSubMenu
= item
->GetSubMenu();
323 if ( pSubMenu
!= NULL
) {
324 RemoveMenu(menu
, (UINT
)pos
, MF_BYPOSITION
);
325 pSubMenu
->m_hMenu
= pSubMenu
->m_savehMenu
;
326 pSubMenu
->m_savehMenu
= 0;
327 pSubMenu
->m_parent
= NULL
;
328 // RemoveChild(item->subMenu);
329 pSubMenu
->m_topLevelMenu
= NULL
;
330 // TODO: Why isn't subMenu deleted here???
331 // Will put this in for now. Assuming this is supposed
332 // to delete the menu, not just remove it.
333 item
->DeleteSubMenu();
336 DeleteMenu(menu
, (UINT
)pos
, MF_BYPOSITION
);
339 m_menuItems
.DeleteNode(node
);
345 // ---------------------------------------------------------------------------
346 // accelerator helpers
347 // ---------------------------------------------------------------------------
349 // create the wxAcceleratorEntries for our accels and put them into provided
350 // array - return the number of accels we have
351 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
353 size_t count
= GetAccelCount();
354 for ( size_t n
= 0; n
< count
; n
++ )
356 (*accels
++).Set(m_accelFlags
[n
], m_accelKeyCodes
[n
], m_accelIds
[n
]);
362 #endif // wxUSE_ACCEL
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
, wxT("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
, wxT("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
, wxT("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
, wxT("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
, wxT("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(wxT("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(wxT("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(wxT("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(wxT("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
;
537 (void)(*(m_callback
))(*this, event
);
541 // Try the menu's event handler
542 if ( !processed
&& GetEventHandler())
544 processed
= GetEventHandler()->ProcessEvent(event
);
547 // Try the window the menu was popped up from (and up through the
549 wxWindow
*win
= GetInvokingWindow();
550 if ( !processed
&& win
)
551 processed
= win
->GetEventHandler()->ProcessEvent(event
);
556 // ---------------------------------------------------------------------------
558 // ---------------------------------------------------------------------------
560 // Finds the item id matching the given string, -1 if not found.
561 int wxMenu::FindItem (const wxString
& itemString
) const
563 wxString itemLabel
= wxStripMenuCodes(itemString
);
564 for ( wxNode
*node
= m_menuItems
.First(); node
; node
= node
->Next() )
566 wxMenuItem
*item
= (wxMenuItem
*)node
->Data();
567 if ( item
->IsSubMenu() )
569 int ans
= item
->GetSubMenu()->FindItem(itemString
);
570 if ( ans
!= wxNOT_FOUND
)
573 else if ( !item
->IsSeparator() )
575 wxString label
= wxStripMenuCodes(item
->GetName());
576 if ( itemLabel
== label
)
577 return item
->GetId();
584 wxMenuItem
*wxMenu::FindItemForId(int itemId
, wxMenu
** itemMenu
) const
589 wxMenuItem
*item
= NULL
;
590 for ( wxNode
*node
= m_menuItems
.First(); node
&& !item
; node
= node
->Next() )
592 item
= (wxMenuItem
*)node
->Data();
594 if ( item
->GetId() == itemId
)
597 *itemMenu
= (wxMenu
*)this;
599 else if ( item
->IsSubMenu() )
601 item
= item
->GetSubMenu()->FindItemForId(itemId
, itemMenu
);
605 // don't exit the loop
613 // ---------------------------------------------------------------------------
615 // ---------------------------------------------------------------------------
617 void wxMenu::Attach(wxMenuBar
*menubar
)
619 // menu can be in at most one menubar because otherwise they would both
620 // delete the menu pointer
621 wxASSERT_MSG( !m_menuBar
, wxT("menu belongs to 2 menubars, expect a crash") );
624 m_savehMenu
= m_hMenu
;
628 void wxMenu::Detach()
630 wxASSERT_MSG( m_menuBar
, wxT("can't detach menu if it's not attached") );
632 m_hMenu
= m_savehMenu
;
636 // ---------------------------------------------------------------------------
638 // ---------------------------------------------------------------------------
640 void wxMenuBar::Init()
642 m_eventHandler
= this;
646 m_menuBarFrame
= NULL
;
650 wxMenuBar::wxMenuBar()
655 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
660 wxMenuBar::wxMenuBar(int count
, wxMenu
*menus
[], const wxString titles
[])
666 m_titles
= new wxString
[count
];
669 for ( i
= 0; i
< count
; i
++ )
670 m_titles
[i
] = titles
[i
];
672 for ( i
= 0; i
< count
; i
++ )
673 m_menus
[i
]->Attach(this);
676 wxMenuBar::~wxMenuBar()
678 for ( int i
= 0; i
< m_menuCount
; i
++ )
687 // ---------------------------------------------------------------------------
689 // ---------------------------------------------------------------------------
691 void wxMenuBar::Refresh()
693 wxCHECK_RET( m_menuBarFrame
, wxT("can't refresh a menubar withotu a frame") );
695 DrawMenuBar((HWND
)m_menuBarFrame
->GetHWND()) ;
698 WXHMENU
wxMenuBar::Create()
703 wxCHECK_MSG( !m_hMenu
, TRUE
, wxT("menubar already created") );
705 m_hMenu
= (WXHMENU
)::CreateMenu();
709 wxLogLastError("CreateMenu");
713 for ( int i
= 0; i
< m_menuCount
; i
++ )
715 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
716 (UINT
)m_menus
[i
]->GetHMenu(),
719 wxLogLastError("AppendMenu");
727 // ---------------------------------------------------------------------------
728 // wxMenuBar functions forwarded to wxMenuItem
729 // ---------------------------------------------------------------------------
731 // Must only be used AFTER menu has been attached to frame,
732 // otherwise use individual menus to enable/disable items
733 void wxMenuBar::Enable(int id
, bool enable
)
735 wxMenu
*itemMenu
= NULL
;
736 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
738 wxCHECK_RET( item
, wxT("attempt to enable an item which doesn't exist") );
740 item
->Enable(enable
);
743 void wxMenuBar::EnableTop(int pos
, bool enable
)
745 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;;
747 EnableMenuItem((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
| flag
);
750 // Must only be used AFTER menu has been attached to frame,
751 // otherwise use individual menus
752 void wxMenuBar::Check(int id
, bool check
)
754 wxMenu
*itemMenu
= NULL
;
755 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
757 wxCHECK_RET( item
, wxT("attempt to check an item which doesn't exist") );
758 wxCHECK_RET( item
->IsCheckable(), wxT("attempt to check an uncheckable item") );
763 bool wxMenuBar::IsChecked(int id
) const
765 wxMenu
*itemMenu
= NULL
;
766 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
768 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsChecked(): no such item") );
770 int flag
= ::GetMenuState(GetHmenuOf(itemMenu
), id
, MF_BYCOMMAND
);
772 return (flag
& MF_CHECKED
) != 0;
775 bool wxMenuBar::IsEnabled(int id
) const
777 wxMenu
*itemMenu
= NULL
;
778 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
780 wxCHECK_MSG( item
, FALSE
, wxT("wxMenuBar::IsEnabled(): no such item") );
782 int flag
= ::GetMenuState(GetHmenuOf(itemMenu
), id
, MF_BYCOMMAND
) ;
784 // don't "and" with MF_ENABLED because its value is 0
785 return (flag
& MF_DISABLED
) == 0;
788 void wxMenuBar::SetLabel(int id
, const wxString
& label
)
790 wxMenu
*itemMenu
= NULL
;
791 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
793 wxCHECK_RET( item
, wxT("wxMenuBar::SetLabel(): no such item") );
795 item
->SetName(label
);
798 wxString
wxMenuBar::GetLabel(int id
) const
800 wxMenu
*itemMenu
= NULL
;
801 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
803 wxCHECK_MSG( item
, wxT(""), wxT("wxMenuBar::GetLabel(): no such item") );
805 return item
->GetName();
808 void wxMenuBar::SetHelpString (int id
, const wxString
& helpString
)
810 wxMenu
*itemMenu
= NULL
;
811 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
813 wxCHECK_RET( item
, wxT("wxMenuBar::SetHelpString(): no such item") );
815 item
->SetHelp(helpString
);
818 wxString
wxMenuBar::GetHelpString (int id
) const
820 wxMenu
*itemMenu
= NULL
;
821 wxMenuItem
*item
= FindItemForId(id
, &itemMenu
) ;
823 wxCHECK_MSG( item
, wxT(""), wxT("wxMenuBar::GetHelpString(): no such item") );
825 return item
->GetHelp();
828 // ---------------------------------------------------------------------------
829 // wxMenuBar functions to work with the top level submenus
830 // ---------------------------------------------------------------------------
832 // NB: we don't support owner drawn top level items for now, if we do these
833 // functions would have to be changed to use wxMenuItem as well
835 void wxMenuBar::SetLabelTop(int pos
, const wxString
& label
)
838 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
);
839 if ( flagsOld
== 0xFFFFFFFF )
841 wxLogLastError(wxT("GetMenuState"));
846 if ( flagsOld
& MF_POPUP
)
848 // HIBYTE contains the number of items in the submenu in this case
850 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, pos
) ;
857 if ( ::ModifyMenu(GetHmenu(), pos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
858 id
, label
) == (int)0xFFFFFFFF )
860 wxLogLastError("ModifyMenu");
864 wxString
wxMenuBar::GetLabelTop(int pos
) const
866 int len
= ::GetMenuString((HMENU
)m_hMenu
, pos
, NULL
, 0, MF_BYCOMMAND
);
868 len
++; // for the NUL character
870 ::GetMenuString(GetHmenu(), pos
, label
.GetWriteBuf(len
), len
, MF_BYCOMMAND
);
871 label
.UngetWriteBuf();
876 // ---------------------------------------------------------------------------
877 // wxMenuBar notifications
878 // ---------------------------------------------------------------------------
880 bool wxMenuBar::OnDelete(wxMenu
*a_menu
, int pos
)
882 if ( !m_menuBarFrame
)
885 if ( ::RemoveMenu((HMENU
)m_hMenu
, (UINT
)pos
, MF_BYPOSITION
) )
887 // VZ: I'm not sure about what's going on here, so I leave an assert
888 wxASSERT_MSG( m_menus
[pos
] == a_menu
, wxT("what is this parameter for??") );
892 if ( m_menuBarFrame
)
899 wxLogLastError("RemoveMenu");
905 bool wxMenuBar::OnAppend(wxMenu
*a_menu
, const wxChar
*title
)
907 WXHMENU submenu
= a_menu
->GetHMenu();
911 if ( !m_menuBarFrame
)
914 a_menu
->Attach(this);
916 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
917 (UINT
)submenu
, title
) )
919 wxLogLastError(wxT("AppendMenu"));
927 // ---------------------------------------------------------------------------
928 // wxMenuBar construction
929 // ---------------------------------------------------------------------------
930 int wxMenuBar::FindMenu(const wxString
& title
)
932 wxString menuTitle
= wxStripMenuCodes(title
);
933 for ( int i
= 0; i
< m_menuCount
; i
++ )
935 wxString title
= wxStripMenuCodes(m_titles
[i
]);
936 if ( menuTitle
== title
)
945 void wxMenuBar::ReplaceMenu(int pos
, wxMenu
* new_menu
, const wxString
& title
)
947 if (m_menuBarFrame
) return;
949 if ( pos
>= 0 && pos
< m_menuCount
)
951 wxMenu
*old_menu
= m_menus
[pos
];
952 m_menus
[pos
] = new_menu
;
959 void wxMenuBar::Insert(int pos
, wxMenu
* menu
, const wxString
& title
)
961 if (m_menuBarFrame
) return;
962 if ( pos
< 0 && pos
>= m_menuCount
) return;
965 wxMenu
**new_menus
= new wxMenu
*[m_menuCount
];
966 wxString
*new_titles
= new wxString
[m_menuCount
];
969 for (i
= 0; i
< pos
; i
++)
971 new_menus
[i
] = m_menus
[i
];
973 new_titles
[i
] = m_titles
[i
];
974 m_titles
[i
] = wxT("");
977 new_menus
[pos
] = (wxMenu
*)menu
;
978 new_titles
[i
] = title
;
980 for (i
= pos
+1; i
< m_menuCount
; i
++)
982 new_menus
[i
] = m_menus
[i
-1];
984 new_titles
[i
] = m_titles
[i
-1];
985 m_titles
[i
-1] = wxT("");
993 m_titles
= new_titles
;
995 menu
->SetParent(this);
1000 void wxMenuBar::Append (wxMenu
* menu
, const wxString
& title
)
1002 if (!OnAppend(menu
, title
))
1006 wxMenu
**new_menus
= new wxMenu
*[m_menuCount
];
1007 wxString
*new_titles
= new wxString
[m_menuCount
];
1010 for (i
= 0; i
< m_menuCount
- 1; i
++)
1012 new_menus
[i
] = m_menus
[i
];
1014 new_titles
[i
] = m_titles
[i
];
1015 m_titles
[i
] = wxT("");
1022 m_menus
= new_menus
;
1023 m_titles
= new_titles
;
1025 m_menus
[m_menuCount
- 1] = (wxMenu
*)menu
;
1026 m_titles
[m_menuCount
- 1] = title
;
1028 menu
->SetParent(this);
1031 void wxMenuBar::Delete(wxMenu
* menu
, int i
)
1037 for (ii
= 0; ii
< m_menuCount
; ii
++) {
1038 if (m_menus
[ii
] == menu
)
1041 if (ii
>= m_menuCount
)
1044 if (ii
< 0 || ii
>= m_menuCount
)
1049 if (!OnDelete(menu
, ii
))
1052 menu
->SetParent(NULL
);
1055 for (j
= ii
; j
< m_menuCount
; j
++) {
1056 m_menus
[j
] = m_menus
[j
+ 1];
1057 m_titles
[j
] = m_titles
[j
+ 1];
1061 void wxMenuBar::Attach(wxFrame
*frame
)
1063 wxASSERT_MSG( !m_menuBarFrame
, wxT("menubar already attached!") );
1065 m_menuBarFrame
= frame
;
1068 // create the accel table - we consider that the menubar construction is
1070 size_t nAccelCount
= 0;
1072 for ( i
= 0; i
< m_menuCount
; i
++ )
1074 nAccelCount
+= m_menus
[i
]->GetAccelCount();
1079 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1082 for ( i
= 0; i
< m_menuCount
; i
++ )
1084 nAccelCount
+= m_menus
[i
]->CopyAccels(&accelEntries
[nAccelCount
]);
1087 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1089 delete [] accelEntries
;
1091 #endif // wxUSE_ACCEL
1094 void wxMenuBar::Detach()
1096 // ::DestroyMenu((HMENU)m_hMenu);
1097 m_hMenu
= (WXHMENU
)NULL
;
1098 m_menuBarFrame
= NULL
;
1102 // ---------------------------------------------------------------------------
1103 // wxMenuBar searching for menu items
1104 // ---------------------------------------------------------------------------
1106 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
1107 int wxMenuBar::FindMenuItem(const wxString
& menuString
,
1108 const wxString
& itemString
) const
1110 wxString menuLabel
= wxStripMenuCodes(menuString
);
1111 for ( int i
= 0; i
< m_menuCount
; i
++ )
1113 wxString title
= wxStripMenuCodes(m_titles
[i
]);
1114 if ( menuString
== title
)
1115 return m_menus
[i
]->FindItem(itemString
);
1121 wxMenuItem
*wxMenuBar::FindItemForId (int id
, wxMenu
**itemMenu
) const
1126 wxMenuItem
*item
= NULL
;
1127 for ( int i
= 0; !item
&& (i
< m_menuCount
); i
++ )
1129 item
= m_menus
[i
]->FindItemForId(id
, itemMenu
);
1136 // ----------------------------------------------------------------------------
1138 // ----------------------------------------------------------------------------
1140 wxWindow
*wxMenu::GetWindow() const
1142 if ( m_pInvokingWindow
!= NULL
)
1143 return m_pInvokingWindow
;
1144 else if ( m_menuBar
!= NULL
)
1145 return m_menuBar
->GetFrame();
1150 WXHMENU
wxMenu::GetHMenu() const
1154 else if ( m_savehMenu
!= 0 )
1157 wxFAIL_MSG(wxT("wxMenu without HMENU"));
1162 // Update a menu and all submenus recursively. source is the object that has
1163 // the update event handlers defined for it. If NULL, the menu or associated
1164 // window will be used.
1165 void wxMenu::UpdateUI(wxEvtHandler
* source
)
1167 if (!source
&& GetInvokingWindow())
1168 source
= GetInvokingWindow()->GetEventHandler();
1170 source
= GetEventHandler();
1174 wxNode
* node
= GetItems().First();
1177 wxMenuItem
* item
= (wxMenuItem
*) node
->Data();
1178 if ( !item
->IsSeparator() )
1180 wxWindowID id
= item
->GetId();
1181 wxUpdateUIEvent
event(id
);
1182 event
.SetEventObject( source
);
1184 if (source
->ProcessEvent(event
))
1186 if (event
.GetSetText())
1187 SetLabel(id
, event
.GetText());
1188 if (event
.GetSetChecked())
1189 Check(id
, event
.GetChecked());
1190 if (event
.GetSetEnabled())
1191 Enable(id
, event
.GetEnabled());
1194 if (item
->GetSubMenu())
1195 item
->GetSubMenu()->UpdateUI(source
);
1197 node
= node
->Next();