1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "menu.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
42 #include "wx/ownerdrw.h"
45 #include "wx/msw/private.h"
56 #ifndef TBSTYLE_NO_DROPDOWN_ARROW
57 #define TBSTYLE_NO_DROPDOWN_ARROW 0x0080
62 // other standard headers
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 extern wxMenu
*wxCurrentPopupMenu
;
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // the (popup) menu title has this special id
76 static const int idMenuTitle
= -2;
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 // make the given menu item default
83 static void SetDefaultMenuItem(HMENU hmenu
, UINT id
)
88 mii
.cbSize
= sizeof(MENUITEMINFO
);
89 mii
.fMask
= MIIM_STATE
;
90 mii
.fState
= MFS_DEFAULT
;
92 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
94 wxLogLastError(wxT("SetMenuItemInfo"));
100 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
104 info
.cbSize
= sizeof(info
);
105 info
.fMask
= MIIM_STATE
;
106 if ( !GetMenuItemInfo(hMenu
, id
, flags
& MF_BYCOMMAND
? FALSE
: TRUE
, & info
) )
107 wxLogLastError(wxT("GetMenuItemInfo"));
112 // ============================================================================
114 // ============================================================================
116 #include <wx/listimpl.cpp>
118 WX_DEFINE_LIST( wxMenuInfoList
) ;
120 #if wxUSE_EXTENDED_RTTI
122 WX_DEFINE_FLAGS( wxMenuStyle
)
124 wxBEGIN_FLAGS( wxMenuStyle
)
125 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
126 wxEND_FLAGS( wxMenuStyle
)
128 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
130 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
132 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
134 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
137 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
138 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
139 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
140 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
141 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
142 wxEND_PROPERTIES_TABLE()
144 wxBEGIN_HANDLERS_TABLE(wxMenu
)
145 wxEND_HANDLERS_TABLE()
147 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
149 WX_DEFINE_FLAGS( wxMenuBarStyle
)
151 wxBEGIN_FLAGS( wxMenuBarStyle
)
152 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
153 wxEND_FLAGS( wxMenuBarStyle
)
155 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
156 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
161 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
163 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
165 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
166 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
167 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
168 wxEND_PROPERTIES_TABLE()
170 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
171 wxEND_HANDLERS_TABLE()
173 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
175 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
177 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
179 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
182 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
183 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
184 wxEND_PROPERTIES_TABLE()
186 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
187 wxEND_HANDLERS_TABLE()
189 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
192 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
193 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
194 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
197 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
199 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
200 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
201 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
203 wxMenuInfo
* info
= new wxMenuInfo() ;
204 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetLabelTop(i
) ) ;
205 list
->Append( info
) ;
210 // ---------------------------------------------------------------------------
211 // wxMenu construction, adding and removing menu items
212 // ---------------------------------------------------------------------------
214 // Construct a menu with optional title (then use append)
218 m_startRadioGroup
= -1;
221 m_hMenu
= (WXHMENU
)CreatePopupMenu();
224 wxLogLastError(wxT("CreatePopupMenu"));
227 // if we have a title, insert it in the beginning of the menu
230 Append(idMenuTitle
, m_title
);
235 // The wxWindow destructor will take care of deleting the submenus.
238 // we should free Windows resources only if Windows doesn't do it for us
239 // which happens if we're attached to a menubar or a submenu of another
241 if ( !IsAttached() && !GetParent() )
243 if ( !::DestroyMenu(GetHmenu()) )
245 wxLogLastError(wxT("DestroyMenu"));
251 WX_CLEAR_ARRAY(m_accels
);
252 #endif // wxUSE_ACCEL
257 // this will take effect during the next call to Append()
261 void wxMenu::Attach(wxMenuBarBase
*menubar
)
263 wxMenuBase::Attach(menubar
);
270 int wxMenu::FindAccel(int id
) const
272 size_t n
, count
= m_accels
.GetCount();
273 for ( n
= 0; n
< count
; n
++ )
275 if ( m_accels
[n
]->m_command
== id
)
282 void wxMenu::UpdateAccel(wxMenuItem
*item
)
284 if ( item
->IsSubMenu() )
286 wxMenu
*submenu
= item
->GetSubMenu();
287 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
290 UpdateAccel(node
->GetData());
292 node
= node
->GetNext();
295 else if ( !item
->IsSeparator() )
297 // find the (new) accel for this item
298 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
300 accel
->m_command
= item
->GetId();
303 int n
= FindAccel(item
->GetId());
304 if ( n
== wxNOT_FOUND
)
306 // no old, add new if any
310 return; // skipping RebuildAccelTable() below
314 // replace old with new or just remove the old one if no new
319 m_accels
.RemoveAt(n
);
324 m_menuBar
->RebuildAccelTable();
327 //else: it is a separator, they can't have accels, nothing to do
330 #endif // wxUSE_ACCEL
332 // append a new item or submenu to the menu
333 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
337 #endif // wxUSE_ACCEL
341 // if "Break" has just been called, insert a menu break before this item
342 // (and don't forget to reset the flag)
344 flags
|= MF_MENUBREAK
;
348 if ( pItem
->IsSeparator() ) {
349 flags
|= MF_SEPARATOR
;
352 // id is the numeric id for normal menu items and HMENU for submenus as
353 // required by ::AppendMenu() API
355 wxMenu
*submenu
= pItem
->GetSubMenu();
356 if ( submenu
!= NULL
) {
357 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
359 submenu
->SetParent(this);
361 id
= (UINT
)submenu
->GetHMenu();
370 wxString strippedString
;
375 #if wxUSE_OWNER_DRAWN
376 if ( pItem
->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
377 // item draws itself, pass pointer to it in data parameter
378 flags
|= MF_OWNERDRAW
;
379 pData
= (LPCTSTR
)pItem
;
384 // menu is just a normal string (passed in data parameter)
388 strippedString
= wxStripMenuCodes(pItem
->GetText());
389 pData
= (wxChar
*)strippedString
.c_str();
391 pData
= (wxChar
*)pItem
->GetText().c_str();
396 if ( pos
== (size_t)-1 )
398 ok
= ::AppendMenu(GetHmenu(), flags
, id
, pData
);
402 ok
= ::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
);
407 wxLogLastError(wxT("Insert or AppendMenu"));
412 // if we just appended the title, highlight it
414 if ( (int)id
== idMenuTitle
)
416 // visually select the menu title
417 SetDefaultMenuItem(GetHmenu(), id
);
421 // if we're already attached to the menubar, we must update it
422 if ( IsAttached() && m_menuBar
->IsAttached() )
424 m_menuBar
->Refresh();
430 void wxMenu::EndRadioGroup()
432 // we're not inside a radio group any longer
433 m_startRadioGroup
= -1;
436 bool wxMenu::DoAppend(wxMenuItem
*item
)
438 wxCHECK_MSG( item
, FALSE
, _T("NULL item in wxMenu::DoAppend") );
442 if ( item
->GetKind() == wxITEM_RADIO
)
444 int count
= GetMenuItemCount();
446 if ( m_startRadioGroup
== -1 )
448 // start a new radio group
449 m_startRadioGroup
= count
;
451 // for now it has just one element
452 item
->SetAsRadioGroupStart();
453 item
->SetRadioGroupEnd(m_startRadioGroup
);
455 // ensure that we have a checked item in the radio group
458 else // extend the current radio group
460 // we need to update its end item
461 item
->SetRadioGroupStart(m_startRadioGroup
);
462 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
466 node
->GetData()->SetRadioGroupEnd(count
);
470 wxFAIL_MSG( _T("where is the radio group start item?") );
474 else // not a radio item
479 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
486 // check the item initially
493 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
495 return wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
);
498 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
500 // we need to find the items position in the child list
502 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
503 for ( pos
= 0; node
; pos
++ )
505 if ( node
->GetData() == item
)
508 node
= node
->GetNext();
511 // DoRemove() (unlike Remove) can only be called for existing item!
512 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
515 // remove the corresponding accel from the accel table
516 int n
= FindAccel(item
->GetId());
517 if ( n
!= wxNOT_FOUND
)
521 m_accels
.RemoveAt(n
);
523 //else: this item doesn't have an accel, nothing to do
524 #endif // wxUSE_ACCEL
526 // remove the item from the menu
527 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
529 wxLogLastError(wxT("RemoveMenu"));
532 if ( IsAttached() && m_menuBar
->IsAttached() )
534 // otherwise, the chane won't be visible
535 m_menuBar
->Refresh();
538 // and from internal data structures
539 return wxMenuBase::DoRemove(item
);
542 // ---------------------------------------------------------------------------
543 // accelerator helpers
544 // ---------------------------------------------------------------------------
548 // create the wxAcceleratorEntries for our accels and put them into provided
549 // array - return the number of accels we have
550 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
552 size_t count
= GetAccelCount();
553 for ( size_t n
= 0; n
< count
; n
++ )
555 *accels
++ = *m_accels
[n
];
561 #endif // wxUSE_ACCEL
563 // ---------------------------------------------------------------------------
565 // ---------------------------------------------------------------------------
567 void wxMenu::SetTitle(const wxString
& label
)
569 bool hasNoTitle
= m_title
.IsEmpty();
572 HMENU hMenu
= GetHmenu();
576 if ( !label
.IsEmpty() )
578 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
579 (unsigned)idMenuTitle
, m_title
) ||
580 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
582 wxLogLastError(wxT("InsertMenu"));
588 if ( label
.IsEmpty() )
590 // remove the title and the separator after it
591 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
592 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
594 wxLogLastError(wxT("RemoveMenu"));
603 info
.cbSize
= sizeof(info
);
604 info
.fMask
= MIIM_TYPE
;
605 info
.fType
= MFT_STRING
;
606 info
.cch
= m_title
.Length();
607 info
.dwTypeData
= (LPTSTR
) m_title
.c_str();
608 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
610 wxLogLastError(wxT("SetMenuItemInfo"));
613 if ( !ModifyMenu(hMenu
, 0u,
614 MF_BYPOSITION
| MF_STRING
,
615 (unsigned)idMenuTitle
, m_title
) )
617 wxLogLastError(wxT("ModifyMenu"));
624 // put the title string in bold face
625 if ( !m_title
.IsEmpty() )
627 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
632 // ---------------------------------------------------------------------------
634 // ---------------------------------------------------------------------------
636 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
638 // ignore commands from the menu title
640 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
641 if ( id
!= (WXWORD
)idMenuTitle
)
643 // VZ: previosuly, the command int was set to id too which was quite
644 // useless anyhow (as it could be retrieved using GetId()) and
645 // uncompatible with wxGTK, so now we use the command int instead
646 // to pass the checked status
647 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
) ;
648 SendEvent(id
, menuState
& MF_CHECKED
);
654 // ---------------------------------------------------------------------------
656 // ---------------------------------------------------------------------------
658 wxWindow
*wxMenu::GetWindow() const
660 if ( m_invokingWindow
!= NULL
)
661 return m_invokingWindow
;
662 else if ( m_menuBar
!= NULL
)
663 return m_menuBar
->GetFrame();
668 // ---------------------------------------------------------------------------
670 // ---------------------------------------------------------------------------
672 void wxMenuBar::Init()
674 m_eventHandler
= this;
681 wxMenuBar::wxMenuBar()
686 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
691 wxMenuBar::wxMenuBar(int count
, wxMenu
*menus
[], const wxString titles
[])
695 m_titles
.Alloc(count
);
697 for ( int i
= 0; i
< count
; i
++ )
699 m_menus
.Append(menus
[i
]);
700 m_titles
.Add(titles
[i
]);
702 menus
[i
]->Attach(this);
706 wxMenuBar::~wxMenuBar()
708 // In Windows CE, the menubar is always associated
709 // with a toolbar, which destroys the menu implicitly.
712 GetToolBar()->SetMenuBar(NULL
);
714 // we should free Windows resources only if Windows doesn't do it for us
715 // which happens if we're attached to a frame
716 if (m_hMenu
&& !IsAttached())
718 ::DestroyMenu((HMENU
)m_hMenu
);
719 m_hMenu
= (WXHMENU
)NULL
;
724 // ---------------------------------------------------------------------------
726 // ---------------------------------------------------------------------------
728 void wxMenuBar::Refresh()
730 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
735 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
738 DrawMenuBar(GetHwndOf(GetFrame()));
742 WXHMENU
wxMenuBar::Create()
744 // Note: this totally doesn't work on Smartphone,
745 // since you have to use resources.
746 // We'll have to find another way to add a menu
747 // by changing/adding menu items to an existing menu.
755 HWND hCommandBar
= (HWND
) GetToolBar()->GetHWND();
756 HMENU hMenu
= (HMENU
)::SendMessage(hCommandBar
, SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
760 memset(&tbButton
, 0, sizeof(TBBUTTON
));
761 tbButton
.iBitmap
= I_IMAGENONE
;
762 tbButton
.fsState
= TBSTATE_ENABLED
;
763 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
766 for (i
= 0; i
< GetMenuCount(); i
++)
768 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu() ;
769 tbButton
.dwData
= (DWORD
)hPopupMenu
;
770 wxString label
= wxStripMenuCodes(GetLabelTop(i
));
771 tbButton
.iString
= (int) label
.c_str();
775 tbButton
.idCommand
= NewControlId();
776 if (!::SendMessage(hCommandBar
, TB_INSERTBUTTON
, position
, (LPARAM
)&tbButton
))
778 wxLogLastError(wxT("TB_INSERTBUTTON"));
782 m_hMenu
= (WXHMENU
) hMenu
;
788 m_hMenu
= (WXHMENU
)::CreateMenu();
792 wxLogLastError(wxT("CreateMenu"));
796 size_t count
= GetMenuCount(), i
;
797 wxMenuList::iterator it
;
798 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
800 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
801 (UINT
)(*it
)->GetHMenu(),
804 wxLogLastError(wxT("AppendMenu"));
813 // ---------------------------------------------------------------------------
814 // wxMenuBar functions to work with the top level submenus
815 // ---------------------------------------------------------------------------
817 // NB: we don't support owner drawn top level items for now, if we do these
818 // functions would have to be changed to use wxMenuItem as well
820 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
822 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
824 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
826 EnableMenuItem((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
| flag
);
831 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
833 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
835 m_titles
[pos
] = label
;
841 //else: have to modify the existing menu
844 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
);
845 if ( flagsOld
== 0xFFFFFFFF )
847 wxLogLastError(wxT("GetMenuState"));
852 if ( flagsOld
& MF_POPUP
)
854 // HIBYTE contains the number of items in the submenu in this case
856 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, pos
);
866 info
.cbSize
= sizeof(info
);
867 info
.fMask
= MIIM_TYPE
;
868 info
.fType
= MFT_STRING
;
869 info
.cch
= label
.Length();
870 info
.dwTypeData
= (LPTSTR
) label
.c_str();
871 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, & info
) )
873 wxLogLastError(wxT("SetMenuItemInfo"));
877 if ( ::ModifyMenu(GetHmenu(), pos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
878 id
, label
) == (int)0xFFFFFFFF )
880 wxLogLastError(wxT("ModifyMenu"));
887 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
889 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
890 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
892 return wxMenuItem::GetLabelFromText(m_titles
[pos
]);
895 // ---------------------------------------------------------------------------
896 // wxMenuBar construction
897 // ---------------------------------------------------------------------------
899 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
901 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
905 m_titles
[pos
] = title
;
909 // can't use ModifyMenu() because it deletes the submenu it replaces
910 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
912 wxLogLastError(wxT("RemoveMenu"));
915 if ( !::InsertMenu(GetHmenu(), (UINT
)pos
,
916 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
917 (UINT
)GetHmenuOf(menu
), title
) )
919 wxLogLastError(wxT("InsertMenu"));
923 if ( menuOld
->HasAccels() || menu
->HasAccels() )
925 // need to rebuild accell table
928 #endif // wxUSE_ACCEL
936 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
938 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
941 m_titles
.Insert(title
, pos
);
949 memset(&tbButton
, 0, sizeof(TBBUTTON
));
950 tbButton
.iBitmap
= I_IMAGENONE
;
951 tbButton
.fsState
= TBSTATE_ENABLED
;
952 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
954 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
955 tbButton
.dwData
= (DWORD
)hPopupMenu
;
956 wxString label
= wxStripMenuCodes(title
);
957 tbButton
.iString
= (int) label
.c_str();
959 tbButton
.idCommand
= NewControlId();
960 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
962 wxLogLastError(wxT("TB_INSERTBUTTON"));
966 if ( !::InsertMenu(GetHmenu(), pos
,
967 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
968 (UINT
)GetHmenuOf(menu
), title
) )
970 wxLogLastError(wxT("InsertMenu"));
974 if ( menu
->HasAccels() )
976 // need to rebuild accell table
979 #endif // wxUSE_ACCEL
987 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
989 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
990 wxCHECK_MSG( submenu
, FALSE
, wxT("can't append invalid menu to menubar") );
992 if ( !wxMenuBarBase::Append(menu
, title
) )
1003 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1004 tbButton
.iBitmap
= I_IMAGENONE
;
1005 tbButton
.fsState
= TBSTATE_ENABLED
;
1006 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1008 size_t pos
= GetMenuCount();
1009 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1010 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1011 wxString label
= wxStripMenuCodes(title
);
1012 tbButton
.iString
= (int) label
.c_str();
1014 tbButton
.idCommand
= NewControlId();
1015 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1017 wxLogLastError(wxT("TB_INSERTBUTTON"));
1021 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1022 (UINT
)submenu
, title
) )
1024 wxLogLastError(wxT("AppendMenu"));
1029 if ( menu
->HasAccels() )
1031 // need to rebuild accelerator table
1032 RebuildAccelTable();
1034 #endif // wxUSE_ACCEL
1042 wxMenu
*wxMenuBar::Remove(size_t pos
)
1044 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1053 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1055 wxLogLastError(wxT("TB_DELETEBUTTON"));
1059 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
1061 wxLogLastError(wxT("RemoveMenu"));
1065 if ( menu
->HasAccels() )
1067 // need to rebuild accell table
1068 RebuildAccelTable();
1070 #endif // wxUSE_ACCEL
1075 m_titles
.RemoveAt(pos
);
1082 void wxMenuBar::RebuildAccelTable()
1084 // merge the accelerators of all menus into one accel table
1085 size_t nAccelCount
= 0;
1086 size_t i
, count
= GetMenuCount();
1087 wxMenuList::iterator it
;
1088 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1090 nAccelCount
+= (*it
)->GetAccelCount();
1095 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1098 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1100 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1103 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1105 delete [] accelEntries
;
1109 #endif // wxUSE_ACCEL
1111 void wxMenuBar::Attach(wxFrame
*frame
)
1113 wxMenuBarBase::Attach(frame
);
1116 RebuildAccelTable();
1117 #endif // wxUSE_ACCEL
1120 void wxMenuBar::Detach()
1122 wxMenuBarBase::Detach();
1125 #endif // wxUSE_MENUS