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 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYCOMMAND
? FALSE
: TRUE
, & info
) )
110 wxLogLastError(wxT("GetMenuItemInfo"));
115 // ============================================================================
117 // ============================================================================
119 #include <wx/listimpl.cpp>
121 WX_DEFINE_LIST( wxMenuInfoList
) ;
123 #if wxUSE_EXTENDED_RTTI
125 WX_DEFINE_FLAGS( wxMenuStyle
)
127 wxBEGIN_FLAGS( wxMenuStyle
)
128 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
129 wxEND_FLAGS( wxMenuStyle
)
131 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
133 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
135 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
137 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
140 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
141 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
142 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
143 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
144 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
145 wxEND_PROPERTIES_TABLE()
147 wxBEGIN_HANDLERS_TABLE(wxMenu
)
148 wxEND_HANDLERS_TABLE()
150 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
152 WX_DEFINE_FLAGS( wxMenuBarStyle
)
154 wxBEGIN_FLAGS( wxMenuBarStyle
)
155 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
156 wxEND_FLAGS( wxMenuBarStyle
)
158 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
159 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
164 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
166 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
168 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
169 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
170 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
171 wxEND_PROPERTIES_TABLE()
173 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
174 wxEND_HANDLERS_TABLE()
176 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
178 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
180 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
182 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
185 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
186 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
187 wxEND_PROPERTIES_TABLE()
189 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
190 wxEND_HANDLERS_TABLE()
192 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
195 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
196 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
197 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
200 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
202 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
203 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
204 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
206 wxMenuInfo
* info
= new wxMenuInfo() ;
207 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetLabelTop(i
) ) ;
208 list
->Append( info
) ;
213 // ---------------------------------------------------------------------------
214 // wxMenu construction, adding and removing menu items
215 // ---------------------------------------------------------------------------
217 // Construct a menu with optional title (then use append)
221 m_startRadioGroup
= -1;
224 m_hMenu
= (WXHMENU
)CreatePopupMenu();
227 wxLogLastError(wxT("CreatePopupMenu"));
230 // if we have a title, insert it in the beginning of the menu
231 if ( !m_title
.IsEmpty() )
233 Append(idMenuTitle
, m_title
);
238 // The wxWindow destructor will take care of deleting the submenus.
241 // we should free Windows resources only if Windows doesn't do it for us
242 // which happens if we're attached to a menubar or a submenu of another
244 if ( !IsAttached() && !GetParent() )
246 if ( !::DestroyMenu(GetHmenu()) )
248 wxLogLastError(wxT("DestroyMenu"));
254 WX_CLEAR_ARRAY(m_accels
);
255 #endif // wxUSE_ACCEL
260 // this will take effect during the next call to Append()
264 void wxMenu::Attach(wxMenuBarBase
*menubar
)
266 wxMenuBase::Attach(menubar
);
273 int wxMenu::FindAccel(int id
) const
275 size_t n
, count
= m_accels
.GetCount();
276 for ( n
= 0; n
< count
; n
++ )
278 if ( m_accels
[n
]->m_command
== id
)
285 void wxMenu::UpdateAccel(wxMenuItem
*item
)
287 if ( item
->IsSubMenu() )
289 wxMenu
*submenu
= item
->GetSubMenu();
290 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
293 UpdateAccel(node
->GetData());
295 node
= node
->GetNext();
298 else if ( !item
->IsSeparator() )
300 // find the (new) accel for this item
301 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
303 accel
->m_command
= item
->GetId();
306 int n
= FindAccel(item
->GetId());
307 if ( n
== wxNOT_FOUND
)
309 // no old, add new if any
313 return; // skipping RebuildAccelTable() below
317 // replace old with new or just remove the old one if no new
322 m_accels
.RemoveAt(n
);
327 GetMenuBar()->RebuildAccelTable();
330 //else: it is a separator, they can't have accels, nothing to do
333 #endif // wxUSE_ACCEL
335 // append a new item or submenu to the menu
336 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
340 #endif // wxUSE_ACCEL
344 // if "Break" has just been called, insert a menu break before this item
345 // (and don't forget to reset the flag)
347 flags
|= MF_MENUBREAK
;
351 if ( pItem
->IsSeparator() ) {
352 flags
|= MF_SEPARATOR
;
355 // id is the numeric id for normal menu items and HMENU for submenus as
356 // required by ::AppendMenu() API
358 wxMenu
*submenu
= pItem
->GetSubMenu();
359 if ( submenu
!= NULL
) {
360 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
362 submenu
->SetParent(this);
364 id
= (UINT
)submenu
->GetHMenu();
373 wxString strippedString
;
378 #if wxUSE_OWNER_DRAWN
379 if ( pItem
->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
380 // item draws itself, pass pointer to it in data parameter
381 flags
|= MF_OWNERDRAW
;
382 pData
= (LPCTSTR
)pItem
;
387 // menu is just a normal string (passed in data parameter)
391 strippedString
= wxStripMenuCodes(pItem
->GetText());
392 pData
= (wxChar
*)strippedString
.c_str();
394 pData
= (wxChar
*)pItem
->GetText().c_str();
399 if ( pos
== (size_t)-1 )
401 ok
= ::AppendMenu(GetHmenu(), flags
, id
, pData
);
405 ok
= ::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
);
410 wxLogLastError(wxT("Insert or AppendMenu"));
415 // if we just appended the title, highlight it
417 if ( (int)id
== idMenuTitle
)
419 // visually select the menu title
420 SetDefaultMenuItem(GetHmenu(), id
);
424 // if we're already attached to the menubar, we must update it
425 if ( IsAttached() && GetMenuBar()->IsAttached() )
427 GetMenuBar()->Refresh();
433 void wxMenu::EndRadioGroup()
435 // we're not inside a radio group any longer
436 m_startRadioGroup
= -1;
439 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
441 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
445 if ( item
->GetKind() == wxITEM_RADIO
)
447 int count
= GetMenuItemCount();
449 if ( m_startRadioGroup
== -1 )
451 // start a new radio group
452 m_startRadioGroup
= count
;
454 // for now it has just one element
455 item
->SetAsRadioGroupStart();
456 item
->SetRadioGroupEnd(m_startRadioGroup
);
458 // ensure that we have a checked item in the radio group
461 else // extend the current radio group
463 // we need to update its end item
464 item
->SetRadioGroupStart(m_startRadioGroup
);
465 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
469 node
->GetData()->SetRadioGroupEnd(count
);
473 wxFAIL_MSG( _T("where is the radio group start item?") );
477 else // not a radio item
482 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
489 // check the item initially
496 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
498 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
504 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
506 // we need to find the items position in the child list
508 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
509 for ( pos
= 0; node
; pos
++ )
511 if ( node
->GetData() == item
)
514 node
= node
->GetNext();
517 // DoRemove() (unlike Remove) can only be called for existing item!
518 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
521 // remove the corresponding accel from the accel table
522 int n
= FindAccel(item
->GetId());
523 if ( n
!= wxNOT_FOUND
)
527 m_accels
.RemoveAt(n
);
529 //else: this item doesn't have an accel, nothing to do
530 #endif // wxUSE_ACCEL
532 // remove the item from the menu
533 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
535 wxLogLastError(wxT("RemoveMenu"));
538 if ( IsAttached() && GetMenuBar()->IsAttached() )
540 // otherwise, the chane won't be visible
541 GetMenuBar()->Refresh();
544 // and from internal data structures
545 return wxMenuBase::DoRemove(item
);
548 // ---------------------------------------------------------------------------
549 // accelerator helpers
550 // ---------------------------------------------------------------------------
554 // create the wxAcceleratorEntries for our accels and put them into provided
555 // array - return the number of accels we have
556 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
558 size_t count
= GetAccelCount();
559 for ( size_t n
= 0; n
< count
; n
++ )
561 *accels
++ = *m_accels
[n
];
567 #endif // wxUSE_ACCEL
569 // ---------------------------------------------------------------------------
571 // ---------------------------------------------------------------------------
573 void wxMenu::SetTitle(const wxString
& label
)
575 bool hasNoTitle
= m_title
.IsEmpty();
578 HMENU hMenu
= GetHmenu();
582 if ( !label
.IsEmpty() )
584 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
585 (unsigned)idMenuTitle
, m_title
) ||
586 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
588 wxLogLastError(wxT("InsertMenu"));
594 if ( label
.IsEmpty() )
596 // remove the title and the separator after it
597 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
598 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
600 wxLogLastError(wxT("RemoveMenu"));
609 info
.cbSize
= sizeof(info
);
610 info
.fMask
= MIIM_TYPE
;
611 info
.fType
= MFT_STRING
;
612 info
.cch
= m_title
.Length();
613 info
.dwTypeData
= (LPTSTR
) m_title
.c_str();
614 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
616 wxLogLastError(wxT("SetMenuItemInfo"));
619 if ( !ModifyMenu(hMenu
, 0u,
620 MF_BYPOSITION
| MF_STRING
,
621 (unsigned)idMenuTitle
, m_title
) )
623 wxLogLastError(wxT("ModifyMenu"));
630 // put the title string in bold face
631 if ( !m_title
.IsEmpty() )
633 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
638 // ---------------------------------------------------------------------------
640 // ---------------------------------------------------------------------------
642 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
644 // ignore commands from the menu title
645 if ( id
!= (WXWORD
)idMenuTitle
)
647 // get the checked status of the command: notice that menuState is the
648 // old state of the menu, so the test for MF_CHECKED must be inversed
649 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
650 SendEvent(id
, !(menuState
& MF_CHECKED
));
656 // ---------------------------------------------------------------------------
658 // ---------------------------------------------------------------------------
660 wxWindow
*wxMenu::GetWindow() const
662 if ( m_invokingWindow
!= NULL
)
663 return m_invokingWindow
;
664 else if ( GetMenuBar() != NULL
)
665 return GetMenuBar()->GetFrame();
670 // ---------------------------------------------------------------------------
672 // ---------------------------------------------------------------------------
674 void wxMenuBar::Init()
676 m_eventHandler
= this;
678 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
681 // Not using a combined wxToolBar/wxMenuBar? then use
682 // a commandbar in WinCE .NET just to implement the
684 #if defined(WINCE_WITH_COMMANDBAR)
686 m_adornmentsAdded
= false;
690 wxMenuBar::wxMenuBar()
695 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
700 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
704 m_titles
.Alloc(count
);
706 for ( int i
= 0; i
< count
; i
++ )
708 m_menus
.Append(menus
[i
]);
709 m_titles
.Add(titles
[i
]);
711 menus
[i
]->Attach(this);
715 wxMenuBar::~wxMenuBar()
717 // In Windows CE (not .NET), the menubar is always associated
718 // with a toolbar, which destroys the menu implicitly.
719 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
722 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
724 toolMenuBar
->SetMenuBar(NULL
);
727 // we should free Windows resources only if Windows doesn't do it for us
728 // which happens if we're attached to a frame
729 if (m_hMenu
&& !IsAttached())
731 #if defined(WINCE_WITH_COMMANDBAR)
732 ::DestroyWindow((HWND
) m_commandBar
);
733 m_commandBar
= (WXHWND
) NULL
;
735 ::DestroyMenu((HMENU
)m_hMenu
);
737 m_hMenu
= (WXHMENU
)NULL
;
742 // ---------------------------------------------------------------------------
744 // ---------------------------------------------------------------------------
746 void wxMenuBar::Refresh()
748 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
750 #if defined(WINCE_WITHOUT_COMMANDBAR)
753 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
755 #elif defined(WINCE_WITH_COMMANDBAR)
757 DrawMenuBar((HWND
) m_commandBar
);
759 DrawMenuBar(GetHwndOf(GetFrame()));
763 WXHMENU
wxMenuBar::Create()
765 // Note: this totally doesn't work on Smartphone,
766 // since you have to use resources.
767 // We'll have to find another way to add a menu
768 // by changing/adding menu items to an existing menu.
769 #if defined(WINCE_WITHOUT_COMMANDBAR)
776 HWND hCommandBar
= (HWND
) GetToolBar()->GetHWND();
777 HMENU hMenu
= (HMENU
)::SendMessage(hCommandBar
, SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
781 memset(&tbButton
, 0, sizeof(TBBUTTON
));
782 tbButton
.iBitmap
= I_IMAGENONE
;
783 tbButton
.fsState
= TBSTATE_ENABLED
;
784 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
787 for (i
= 0; i
< GetMenuCount(); i
++)
789 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu() ;
790 tbButton
.dwData
= (DWORD
)hPopupMenu
;
791 wxString label
= wxStripMenuCodes(GetLabelTop(i
));
792 tbButton
.iString
= (int) label
.c_str();
796 tbButton
.idCommand
= NewControlId();
797 if (!::SendMessage(hCommandBar
, TB_INSERTBUTTON
, position
, (LPARAM
)&tbButton
))
799 wxLogLastError(wxT("TB_INSERTBUTTON"));
803 m_hMenu
= (WXHMENU
) hMenu
;
809 m_hMenu
= (WXHMENU
)::CreateMenu();
813 wxLogLastError(wxT("CreateMenu"));
817 size_t count
= GetMenuCount(), i
;
818 wxMenuList::iterator it
;
819 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
821 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
822 (UINT
)(*it
)->GetHMenu(),
825 wxLogLastError(wxT("AppendMenu"));
834 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
837 wxASSERT(menu
->GetHMenu());
840 #if defined(__WXWINCE__)
841 int totalMSWItems
= GetMenuCount();
843 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
846 int i
; // For old C++ compatibility
847 for(i
=wxpos
; i
<totalMSWItems
; i
++)
849 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
852 for(i
=0; i
<wxpos
; i
++)
854 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
861 // ---------------------------------------------------------------------------
862 // wxMenuBar functions to work with the top level submenus
863 // ---------------------------------------------------------------------------
865 // NB: we don't support owner drawn top level items for now, if we do these
866 // functions would have to be changed to use wxMenuItem as well
868 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
870 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
871 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
873 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
875 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
880 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
882 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
884 m_titles
[pos
] = label
;
890 //else: have to modify the existing menu
892 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
895 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
896 if ( flagsOld
== 0xFFFFFFFF )
898 wxLogLastError(wxT("GetMenuState"));
903 if ( flagsOld
& MF_POPUP
)
905 // HIBYTE contains the number of items in the submenu in this case
907 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
917 info
.cbSize
= sizeof(info
);
918 info
.fMask
= MIIM_TYPE
;
919 info
.fType
= MFT_STRING
;
920 info
.cch
= label
.Length();
921 info
.dwTypeData
= (LPTSTR
) label
.c_str();
922 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, & info
) )
924 wxLogLastError(wxT("SetMenuItemInfo"));
928 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
929 id
, label
) == (int)0xFFFFFFFF )
931 wxLogLastError(wxT("ModifyMenu"));
938 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
940 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
941 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
943 return wxMenuItem::GetLabelFromText(m_titles
[pos
]);
946 // ---------------------------------------------------------------------------
947 // wxMenuBar construction
948 // ---------------------------------------------------------------------------
950 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
952 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
956 m_titles
[pos
] = title
;
960 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
962 // can't use ModifyMenu() because it deletes the submenu it replaces
963 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
965 wxLogLastError(wxT("RemoveMenu"));
968 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
969 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
970 (UINT
)GetHmenuOf(menu
), title
) )
972 wxLogLastError(wxT("InsertMenu"));
976 if ( menuOld
->HasAccels() || menu
->HasAccels() )
978 // need to rebuild accell table
981 #endif // wxUSE_ACCEL
989 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
991 // Find out which MSW item before which we'll be inserting before
992 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
993 // If IsAttached() is false this won't be used anyway
994 int mswpos
= (!IsAttached() || (pos
== m_menus
.GetCount()))
995 ? -1 // append the menu
996 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
998 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1001 m_titles
.Insert(title
, pos
);
1005 #if defined(WINCE_WITHOUT_COMMANDAR)
1009 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1010 tbButton
.iBitmap
= I_IMAGENONE
;
1011 tbButton
.fsState
= TBSTATE_ENABLED
;
1012 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1014 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1015 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1016 wxString label
= wxStripMenuCodes(title
);
1017 tbButton
.iString
= (int) label
.c_str();
1019 tbButton
.idCommand
= NewControlId();
1020 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1022 wxLogLastError(wxT("TB_INSERTBUTTON"));
1026 if ( !::InsertMenu(GetHmenu(), mswpos
,
1027 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1028 (UINT
)GetHmenuOf(menu
), title
) )
1030 wxLogLastError(wxT("InsertMenu"));
1034 if ( menu
->HasAccels() )
1036 // need to rebuild accell table
1037 RebuildAccelTable();
1039 #endif // wxUSE_ACCEL
1047 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1049 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1050 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1052 if ( !wxMenuBarBase::Append(menu
, title
) )
1055 m_titles
.Add(title
);
1059 #if defined(WINCE_WITHOUT_COMMANDAR)
1063 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1064 tbButton
.iBitmap
= I_IMAGENONE
;
1065 tbButton
.fsState
= TBSTATE_ENABLED
;
1066 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1068 size_t pos
= GetMenuCount();
1069 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1070 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1071 wxString label
= wxStripMenuCodes(title
);
1072 tbButton
.iString
= (int) label
.c_str();
1074 tbButton
.idCommand
= NewControlId();
1075 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1077 wxLogLastError(wxT("TB_INSERTBUTTON"));
1081 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1082 (UINT
)submenu
, title
) )
1084 wxLogLastError(wxT("AppendMenu"));
1089 if ( menu
->HasAccels() )
1091 // need to rebuild accelerator table
1092 RebuildAccelTable();
1094 #endif // wxUSE_ACCEL
1102 wxMenu
*wxMenuBar::Remove(size_t pos
)
1104 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1110 #if defined(WINCE_WITHOUT_COMMANDAR)
1113 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1115 wxLogLastError(wxT("TB_DELETEBUTTON"));
1119 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1121 wxLogLastError(wxT("RemoveMenu"));
1126 if ( menu
->HasAccels() )
1128 // need to rebuild accell table
1129 RebuildAccelTable();
1131 #endif // wxUSE_ACCEL
1137 m_titles
.RemoveAt(pos
);
1144 void wxMenuBar::RebuildAccelTable()
1146 // merge the accelerators of all menus into one accel table
1147 size_t nAccelCount
= 0;
1148 size_t i
, count
= GetMenuCount();
1149 wxMenuList::iterator it
;
1150 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1152 nAccelCount
+= (*it
)->GetAccelCount();
1157 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1160 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1162 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1165 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1167 delete [] accelEntries
;
1171 #endif // wxUSE_ACCEL
1173 void wxMenuBar::Attach(wxFrame
*frame
)
1175 wxMenuBarBase::Attach(frame
);
1177 #if defined(WINCE_WITH_COMMANDBAR)
1181 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1186 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1188 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1195 RebuildAccelTable();
1196 #endif // wxUSE_ACCEL
1199 #if defined(WINCE_WITH_COMMANDBAR)
1200 bool wxMenuBar::AddAdornments(long style
)
1202 if (m_adornmentsAdded
|| !m_commandBar
)
1205 if (style
& wxCLOSE_BOX
)
1207 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1208 wxLogLastError(wxT("CommandBar_AddAdornments"));
1216 void wxMenuBar::Detach()
1218 wxMenuBarBase::Detach();
1221 #endif // wxUSE_MENUS