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"
54 #if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__)
58 #include "wx/msw/wince/missing.h"
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
= -3;
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"));
103 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
107 info
.cbSize
= sizeof(info
);
108 info
.fMask
= MIIM_STATE
;
109 // MF_BYCOMMAND is zero so test MF_BYPOSITION
110 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
111 wxLogLastError(wxT("GetMenuItemInfo"));
116 // ============================================================================
118 // ============================================================================
120 #include <wx/listimpl.cpp>
122 WX_DEFINE_LIST( wxMenuInfoList
) ;
124 #if wxUSE_EXTENDED_RTTI
126 WX_DEFINE_FLAGS( wxMenuStyle
)
128 wxBEGIN_FLAGS( wxMenuStyle
)
129 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
130 wxEND_FLAGS( wxMenuStyle
)
132 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
134 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
136 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
138 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
141 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
142 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
143 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
144 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
145 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
146 wxEND_PROPERTIES_TABLE()
148 wxBEGIN_HANDLERS_TABLE(wxMenu
)
149 wxEND_HANDLERS_TABLE()
151 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
153 WX_DEFINE_FLAGS( wxMenuBarStyle
)
155 wxBEGIN_FLAGS( wxMenuBarStyle
)
156 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
157 wxEND_FLAGS( wxMenuBarStyle
)
159 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
160 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
165 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
167 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
169 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
170 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
171 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
172 wxEND_PROPERTIES_TABLE()
174 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
175 wxEND_HANDLERS_TABLE()
177 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
179 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
181 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
183 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
186 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
187 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
188 wxEND_PROPERTIES_TABLE()
190 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
191 wxEND_HANDLERS_TABLE()
193 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
196 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
197 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
198 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
201 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
203 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
204 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
205 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
207 wxMenuInfo
* info
= new wxMenuInfo() ;
208 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetLabelTop(i
) ) ;
209 list
->Append( info
) ;
214 // ---------------------------------------------------------------------------
215 // wxMenu construction, adding and removing menu items
216 // ---------------------------------------------------------------------------
218 // Construct a menu with optional title (then use append)
222 m_startRadioGroup
= -1;
225 m_hMenu
= (WXHMENU
)CreatePopupMenu();
228 wxLogLastError(wxT("CreatePopupMenu"));
231 // if we have a title, insert it in the beginning of the menu
232 if ( !m_title
.IsEmpty() )
234 Append(idMenuTitle
, m_title
);
239 // The wxWindow destructor will take care of deleting the submenus.
242 // we should free Windows resources only if Windows doesn't do it for us
243 // which happens if we're attached to a menubar or a submenu of another
245 if ( !IsAttached() && !GetParent() )
247 if ( !::DestroyMenu(GetHmenu()) )
249 wxLogLastError(wxT("DestroyMenu"));
255 WX_CLEAR_ARRAY(m_accels
);
256 #endif // wxUSE_ACCEL
261 // this will take effect during the next call to Append()
265 void wxMenu::Attach(wxMenuBarBase
*menubar
)
267 wxMenuBase::Attach(menubar
);
274 int wxMenu::FindAccel(int id
) const
276 size_t n
, count
= m_accels
.GetCount();
277 for ( n
= 0; n
< count
; n
++ )
279 if ( m_accels
[n
]->m_command
== id
)
286 void wxMenu::UpdateAccel(wxMenuItem
*item
)
288 if ( item
->IsSubMenu() )
290 wxMenu
*submenu
= item
->GetSubMenu();
291 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
294 UpdateAccel(node
->GetData());
296 node
= node
->GetNext();
299 else if ( !item
->IsSeparator() )
301 // find the (new) accel for this item
302 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
304 accel
->m_command
= item
->GetId();
307 int n
= FindAccel(item
->GetId());
308 if ( n
== wxNOT_FOUND
)
310 // no old, add new if any
314 return; // skipping RebuildAccelTable() below
318 // replace old with new or just remove the old one if no new
323 m_accels
.RemoveAt(n
);
328 GetMenuBar()->RebuildAccelTable();
331 //else: it is a separator, they can't have accels, nothing to do
334 #endif // wxUSE_ACCEL
336 // append a new item or submenu to the menu
337 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
341 #endif // wxUSE_ACCEL
345 // if "Break" has just been called, insert a menu break before this item
346 // (and don't forget to reset the flag)
348 flags
|= MF_MENUBREAK
;
352 if ( pItem
->IsSeparator() ) {
353 flags
|= MF_SEPARATOR
;
356 // id is the numeric id for normal menu items and HMENU for submenus as
357 // required by ::AppendMenu() API
359 wxMenu
*submenu
= pItem
->GetSubMenu();
360 if ( submenu
!= NULL
) {
361 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
363 submenu
->SetParent(this);
365 id
= (UINT
)submenu
->GetHMenu();
374 wxString strippedString
;
379 #if wxUSE_OWNER_DRAWN
380 if ( pItem
->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
381 // item draws itself, pass pointer to it in data parameter
382 flags
|= MF_OWNERDRAW
;
383 pData
= (LPCTSTR
)pItem
;
388 // menu is just a normal string (passed in data parameter)
392 strippedString
= wxStripMenuCodes(pItem
->GetText());
393 pData
= (wxChar
*)strippedString
.c_str();
395 pData
= (wxChar
*)pItem
->GetText().c_str();
400 if ( pos
== (size_t)-1 )
402 ok
= ::AppendMenu(GetHmenu(), flags
, id
, pData
);
406 ok
= ::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
);
411 wxLogLastError(wxT("Insert or AppendMenu"));
416 // if we just appended the title, highlight it
418 if ( (int)id
== idMenuTitle
)
420 // visually select the menu title
421 SetDefaultMenuItem(GetHmenu(), id
);
425 // if we're already attached to the menubar, we must update it
426 if ( IsAttached() && GetMenuBar()->IsAttached() )
428 GetMenuBar()->Refresh();
434 void wxMenu::EndRadioGroup()
436 // we're not inside a radio group any longer
437 m_startRadioGroup
= -1;
440 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
442 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
446 if ( item
->GetKind() == wxITEM_RADIO
)
448 int count
= GetMenuItemCount();
450 if ( m_startRadioGroup
== -1 )
452 // start a new radio group
453 m_startRadioGroup
= count
;
455 // for now it has just one element
456 item
->SetAsRadioGroupStart();
457 item
->SetRadioGroupEnd(m_startRadioGroup
);
459 // ensure that we have a checked item in the radio group
462 else // extend the current radio group
464 // we need to update its end item
465 item
->SetRadioGroupStart(m_startRadioGroup
);
466 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
470 node
->GetData()->SetRadioGroupEnd(count
);
474 wxFAIL_MSG( _T("where is the radio group start item?") );
478 else // not a radio item
483 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
490 // check the item initially
497 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
499 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
505 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
507 // we need to find the items position in the child list
509 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
510 for ( pos
= 0; node
; pos
++ )
512 if ( node
->GetData() == item
)
515 node
= node
->GetNext();
518 // DoRemove() (unlike Remove) can only be called for existing item!
519 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
522 // remove the corresponding accel from the accel table
523 int n
= FindAccel(item
->GetId());
524 if ( n
!= wxNOT_FOUND
)
528 m_accels
.RemoveAt(n
);
530 //else: this item doesn't have an accel, nothing to do
531 #endif // wxUSE_ACCEL
533 // remove the item from the menu
534 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
536 wxLogLastError(wxT("RemoveMenu"));
539 if ( IsAttached() && GetMenuBar()->IsAttached() )
541 // otherwise, the chane won't be visible
542 GetMenuBar()->Refresh();
545 // and from internal data structures
546 return wxMenuBase::DoRemove(item
);
549 // ---------------------------------------------------------------------------
550 // accelerator helpers
551 // ---------------------------------------------------------------------------
555 // create the wxAcceleratorEntries for our accels and put them into provided
556 // array - return the number of accels we have
557 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
559 size_t count
= GetAccelCount();
560 for ( size_t n
= 0; n
< count
; n
++ )
562 *accels
++ = *m_accels
[n
];
568 #endif // wxUSE_ACCEL
570 // ---------------------------------------------------------------------------
572 // ---------------------------------------------------------------------------
574 void wxMenu::SetTitle(const wxString
& label
)
576 bool hasNoTitle
= m_title
.IsEmpty();
579 HMENU hMenu
= GetHmenu();
583 if ( !label
.IsEmpty() )
585 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
586 (unsigned)idMenuTitle
, m_title
) ||
587 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
589 wxLogLastError(wxT("InsertMenu"));
595 if ( label
.IsEmpty() )
597 // remove the title and the separator after it
598 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
599 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
601 wxLogLastError(wxT("RemoveMenu"));
610 info
.cbSize
= sizeof(info
);
611 info
.fMask
= MIIM_TYPE
;
612 info
.fType
= MFT_STRING
;
613 info
.cch
= m_title
.Length();
614 info
.dwTypeData
= (LPTSTR
) m_title
.c_str();
615 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
617 wxLogLastError(wxT("SetMenuItemInfo"));
620 if ( !ModifyMenu(hMenu
, 0u,
621 MF_BYPOSITION
| MF_STRING
,
622 (unsigned)idMenuTitle
, m_title
) )
624 wxLogLastError(wxT("ModifyMenu"));
631 // put the title string in bold face
632 if ( !m_title
.IsEmpty() )
634 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
639 // ---------------------------------------------------------------------------
641 // ---------------------------------------------------------------------------
643 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
645 // ignore commands from the menu title
646 if ( id
!= (WXWORD
)idMenuTitle
)
648 // get the checked status of the command: notice that menuState is the
649 // old state of the menu, so the test for MF_CHECKED must be inversed
650 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
651 SendEvent(id
, !(menuState
& MF_CHECKED
));
657 // ---------------------------------------------------------------------------
659 // ---------------------------------------------------------------------------
661 wxWindow
*wxMenu::GetWindow() const
663 if ( m_invokingWindow
!= NULL
)
664 return m_invokingWindow
;
665 else if ( GetMenuBar() != NULL
)
666 return GetMenuBar()->GetFrame();
671 // ---------------------------------------------------------------------------
673 // ---------------------------------------------------------------------------
675 void wxMenuBar::Init()
677 m_eventHandler
= this;
679 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
682 // Not using a combined wxToolBar/wxMenuBar? then use
683 // a commandbar in WinCE .NET just to implement the
685 #if defined(WINCE_WITH_COMMANDBAR)
687 m_adornmentsAdded
= false;
691 wxMenuBar::wxMenuBar()
696 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
701 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
705 m_titles
.Alloc(count
);
707 for ( size_t i
= 0; i
< count
; i
++ )
709 m_menus
.Append(menus
[i
]);
710 m_titles
.Add(titles
[i
]);
712 menus
[i
]->Attach(this);
716 wxMenuBar::~wxMenuBar()
718 // In Windows CE (not .NET), the menubar is always associated
719 // with a toolbar, which destroys the menu implicitly.
720 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
723 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
725 toolMenuBar
->SetMenuBar(NULL
);
728 // we should free Windows resources only if Windows doesn't do it for us
729 // which happens if we're attached to a frame
730 if (m_hMenu
&& !IsAttached())
732 #if defined(WINCE_WITH_COMMANDBAR)
733 ::DestroyWindow((HWND
) m_commandBar
);
734 m_commandBar
= (WXHWND
) NULL
;
736 ::DestroyMenu((HMENU
)m_hMenu
);
738 m_hMenu
= (WXHMENU
)NULL
;
743 // ---------------------------------------------------------------------------
745 // ---------------------------------------------------------------------------
747 void wxMenuBar::Refresh()
749 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
751 #if defined(WINCE_WITHOUT_COMMANDBAR)
754 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
756 #elif defined(WINCE_WITH_COMMANDBAR)
758 DrawMenuBar((HWND
) m_commandBar
);
760 DrawMenuBar(GetHwndOf(GetFrame()));
764 WXHMENU
wxMenuBar::Create()
766 // Note: this totally doesn't work on Smartphone,
767 // since you have to use resources.
768 // We'll have to find another way to add a menu
769 // by changing/adding menu items to an existing menu.
770 #if defined(WINCE_WITHOUT_COMMANDBAR)
777 HWND hCommandBar
= (HWND
) GetToolBar()->GetHWND();
778 HMENU hMenu
= (HMENU
)::SendMessage(hCommandBar
, SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
782 memset(&tbButton
, 0, sizeof(TBBUTTON
));
783 tbButton
.iBitmap
= I_IMAGENONE
;
784 tbButton
.fsState
= TBSTATE_ENABLED
;
785 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
788 for (i
= 0; i
< GetMenuCount(); i
++)
790 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu() ;
791 tbButton
.dwData
= (DWORD
)hPopupMenu
;
792 wxString label
= wxStripMenuCodes(GetLabelTop(i
));
793 tbButton
.iString
= (int) label
.c_str();
797 tbButton
.idCommand
= NewControlId();
798 if (!::SendMessage(hCommandBar
, TB_INSERTBUTTON
, position
, (LPARAM
)&tbButton
))
800 wxLogLastError(wxT("TB_INSERTBUTTON"));
804 m_hMenu
= (WXHMENU
) hMenu
;
810 m_hMenu
= (WXHMENU
)::CreateMenu();
814 wxLogLastError(wxT("CreateMenu"));
818 size_t count
= GetMenuCount(), i
;
819 wxMenuList::iterator it
;
820 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
822 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
823 (UINT
)(*it
)->GetHMenu(),
826 wxLogLastError(wxT("AppendMenu"));
835 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
838 wxASSERT(menu
->GetHMenu());
841 #if defined(__WXWINCE__)
842 int totalMSWItems
= GetMenuCount();
844 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
847 int i
; // For old C++ compatibility
848 for(i
=wxpos
; i
<totalMSWItems
; i
++)
850 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
853 for(i
=0; i
<wxpos
; i
++)
855 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
862 // ---------------------------------------------------------------------------
863 // wxMenuBar functions to work with the top level submenus
864 // ---------------------------------------------------------------------------
866 // NB: we don't support owner drawn top level items for now, if we do these
867 // functions would have to be changed to use wxMenuItem as well
869 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
871 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
872 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
874 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
876 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
881 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
883 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
885 m_titles
[pos
] = label
;
891 //else: have to modify the existing menu
893 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
896 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
897 if ( flagsOld
== 0xFFFFFFFF )
899 wxLogLastError(wxT("GetMenuState"));
904 if ( flagsOld
& MF_POPUP
)
906 // HIBYTE contains the number of items in the submenu in this case
908 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
918 info
.cbSize
= sizeof(info
);
919 info
.fMask
= MIIM_TYPE
;
920 info
.fType
= MFT_STRING
;
921 info
.cch
= label
.Length();
922 info
.dwTypeData
= (LPTSTR
) label
.c_str();
923 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, & info
) )
925 wxLogLastError(wxT("SetMenuItemInfo"));
929 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
930 id
, label
) == (int)0xFFFFFFFF )
932 wxLogLastError(wxT("ModifyMenu"));
939 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
941 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
942 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
944 return wxMenuItem::GetLabelFromText(m_titles
[pos
]);
947 // ---------------------------------------------------------------------------
948 // wxMenuBar construction
949 // ---------------------------------------------------------------------------
951 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
953 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
957 m_titles
[pos
] = title
;
961 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
963 // can't use ModifyMenu() because it deletes the submenu it replaces
964 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
966 wxLogLastError(wxT("RemoveMenu"));
969 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
970 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
971 (UINT
)GetHmenuOf(menu
), title
) )
973 wxLogLastError(wxT("InsertMenu"));
977 if ( menuOld
->HasAccels() || menu
->HasAccels() )
979 // need to rebuild accell table
982 #endif // wxUSE_ACCEL
990 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
992 // Find out which MSW item before which we'll be inserting before
993 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
994 // If IsAttached() is false this won't be used anyway
995 int mswpos
= (!IsAttached() || (pos
== m_menus
.GetCount()))
996 ? -1 // append the menu
997 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
999 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1002 m_titles
.Insert(title
, pos
);
1006 #if defined(WINCE_WITHOUT_COMMANDAR)
1010 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1011 tbButton
.iBitmap
= I_IMAGENONE
;
1012 tbButton
.fsState
= TBSTATE_ENABLED
;
1013 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1015 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1016 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1017 wxString label
= wxStripMenuCodes(title
);
1018 tbButton
.iString
= (int) label
.c_str();
1020 tbButton
.idCommand
= NewControlId();
1021 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1023 wxLogLastError(wxT("TB_INSERTBUTTON"));
1027 if ( !::InsertMenu(GetHmenu(), mswpos
,
1028 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1029 (UINT
)GetHmenuOf(menu
), title
) )
1031 wxLogLastError(wxT("InsertMenu"));
1035 if ( menu
->HasAccels() )
1037 // need to rebuild accell table
1038 RebuildAccelTable();
1040 #endif // wxUSE_ACCEL
1048 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1050 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1051 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1053 if ( !wxMenuBarBase::Append(menu
, title
) )
1056 m_titles
.Add(title
);
1060 #if defined(WINCE_WITHOUT_COMMANDAR)
1064 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1065 tbButton
.iBitmap
= I_IMAGENONE
;
1066 tbButton
.fsState
= TBSTATE_ENABLED
;
1067 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1069 size_t pos
= GetMenuCount();
1070 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1071 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1072 wxString label
= wxStripMenuCodes(title
);
1073 tbButton
.iString
= (int) label
.c_str();
1075 tbButton
.idCommand
= NewControlId();
1076 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1078 wxLogLastError(wxT("TB_INSERTBUTTON"));
1082 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1083 (UINT
)submenu
, title
) )
1085 wxLogLastError(wxT("AppendMenu"));
1090 if ( menu
->HasAccels() )
1092 // need to rebuild accelerator table
1093 RebuildAccelTable();
1095 #endif // wxUSE_ACCEL
1103 wxMenu
*wxMenuBar::Remove(size_t pos
)
1105 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1111 #if defined(WINCE_WITHOUT_COMMANDAR)
1114 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1116 wxLogLastError(wxT("TB_DELETEBUTTON"));
1120 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1122 wxLogLastError(wxT("RemoveMenu"));
1127 if ( menu
->HasAccels() )
1129 // need to rebuild accell table
1130 RebuildAccelTable();
1132 #endif // wxUSE_ACCEL
1138 m_titles
.RemoveAt(pos
);
1145 void wxMenuBar::RebuildAccelTable()
1147 // merge the accelerators of all menus into one accel table
1148 size_t nAccelCount
= 0;
1149 size_t i
, count
= GetMenuCount();
1150 wxMenuList::iterator it
;
1151 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1153 nAccelCount
+= (*it
)->GetAccelCount();
1158 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1161 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1163 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1166 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1168 delete [] accelEntries
;
1172 #endif // wxUSE_ACCEL
1174 void wxMenuBar::Attach(wxFrame
*frame
)
1176 wxMenuBarBase::Attach(frame
);
1178 #if defined(WINCE_WITH_COMMANDBAR)
1182 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1187 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1189 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1196 RebuildAccelTable();
1197 #endif // wxUSE_ACCEL
1200 #if defined(WINCE_WITH_COMMANDBAR)
1201 bool wxMenuBar::AddAdornments(long style
)
1203 if (m_adornmentsAdded
|| !m_commandBar
)
1206 if (style
& wxCLOSE_BOX
)
1208 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1209 wxLogLastError(wxT("CommandBar_AddAdornments"));
1217 void wxMenuBar::Detach()
1219 wxMenuBarBase::Detach();
1222 #endif // wxUSE_MENUS