1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/menu.cpp
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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
39 #include "wx/ownerdrw.h"
42 #include "wx/ptr_scpd.h"
44 #include "wx/msw/private.h"
45 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
53 #if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__)
57 #include "wx/msw/wince/missing.h"
61 // other standard headers
64 //VC6 needs these defining, though they are in winuser.h
66 #define MIIM_STRING 0x00000040
67 #define MIIM_BITMAP 0x00000080
68 #define MIIM_FTYPE 0x00000100
69 #define HBMMENU_CALLBACK ((HBITMAP) -1)
70 typedef struct tagMENUINFO
77 DWORD dwContextHelpID
;
79 } MENUINFO
, FAR
*LPMENUINFO
;
83 #include "wx/dynlib.h"
86 #ifndef MNS_CHECKORBMP
87 #define MNS_CHECKORBMP 0x04000000
90 #define MIM_STYLE 0x00000010
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 // the (popup) menu title has this special id
102 static const UINT idMenuTitle
= (UINT
)-3;
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 // make the given menu item default
109 static void SetDefaultMenuItem(HMENU
WXUNUSED_IN_WINCE(hmenu
),
110 UINT
WXUNUSED_IN_WINCE(id
))
115 mii
.cbSize
= sizeof(MENUITEMINFO
);
116 mii
.fMask
= MIIM_STATE
;
117 mii
.fState
= MFS_DEFAULT
;
119 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
121 wxLogLastError(wxT("SetMenuItemInfo"));
127 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
131 info
.cbSize
= sizeof(info
);
132 info
.fMask
= MIIM_STATE
;
133 // MF_BYCOMMAND is zero so test MF_BYPOSITION
134 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
135 wxLogLastError(wxT("GetMenuItemInfo"));
140 // ============================================================================
142 // ============================================================================
144 #include "wx/listimpl.cpp"
146 WX_DEFINE_LIST( wxMenuInfoList
)
148 #if wxUSE_EXTENDED_RTTI
150 WX_DEFINE_FLAGS( wxMenuStyle
)
152 wxBEGIN_FLAGS( wxMenuStyle
)
153 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
154 wxEND_FLAGS( wxMenuStyle
)
156 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
158 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
160 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
162 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
165 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
166 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
167 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
168 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
169 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
170 wxEND_PROPERTIES_TABLE()
172 wxBEGIN_HANDLERS_TABLE(wxMenu
)
173 wxEND_HANDLERS_TABLE()
175 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
177 WX_DEFINE_FLAGS( wxMenuBarStyle
)
179 wxBEGIN_FLAGS( wxMenuBarStyle
)
180 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
181 wxEND_FLAGS( wxMenuBarStyle
)
183 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
184 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
189 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
191 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
193 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
194 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
195 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
196 wxEND_PROPERTIES_TABLE()
198 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
199 wxEND_HANDLERS_TABLE()
201 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
203 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
205 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
207 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
210 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
211 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
212 wxEND_PROPERTIES_TABLE()
214 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
215 wxEND_HANDLERS_TABLE()
217 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
220 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
221 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
222 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
225 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
227 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
228 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
229 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
231 wxMenuInfo
* info
= new wxMenuInfo() ;
232 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetMenuLabel(i
) ) ;
233 list
->Append( info
) ;
238 // ---------------------------------------------------------------------------
239 // wxMenu construction, adding and removing menu items
240 // ---------------------------------------------------------------------------
242 // Construct a menu with optional title (then use append)
246 m_startRadioGroup
= -1;
249 m_hMenu
= (WXHMENU
)CreatePopupMenu();
252 wxLogLastError(wxT("CreatePopupMenu"));
255 // if we have a title, insert it in the beginning of the menu
256 if ( !m_title
.empty() )
258 Append(idMenuTitle
, m_title
);
263 // The wxWindow destructor will take care of deleting the submenus.
266 // we should free Windows resources only if Windows doesn't do it for us
267 // which happens if we're attached to a menubar or a submenu of another
269 if ( !IsAttached() && !GetParent() )
271 if ( !::DestroyMenu(GetHmenu()) )
273 wxLogLastError(wxT("DestroyMenu"));
279 WX_CLEAR_ARRAY(m_accels
);
280 #endif // wxUSE_ACCEL
285 // this will take effect during the next call to Append()
289 void wxMenu::Attach(wxMenuBarBase
*menubar
)
291 wxMenuBase::Attach(menubar
);
298 int wxMenu::FindAccel(int id
) const
300 size_t n
, count
= m_accels
.GetCount();
301 for ( n
= 0; n
< count
; n
++ )
303 if ( m_accels
[n
]->m_command
== id
)
310 void wxMenu::UpdateAccel(wxMenuItem
*item
)
312 if ( item
->IsSubMenu() )
314 wxMenu
*submenu
= item
->GetSubMenu();
315 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
318 UpdateAccel(node
->GetData());
320 node
= node
->GetNext();
323 else if ( !item
->IsSeparator() )
325 // recurse upwards: we should only modify m_accels of the top level
326 // menus, not of the submenus as wxMenuBar doesn't look at them
327 // (alternative and arguable cleaner solution would be to recurse
328 // downwards in GetAccelCount() and CopyAccels())
331 GetParent()->UpdateAccel(item
);
335 // find the (new) accel for this item
336 wxAcceleratorEntry
*accel
= wxAcceleratorEntry::Create(item
->GetItemLabel());
338 accel
->m_command
= item
->GetId();
341 int n
= FindAccel(item
->GetId());
342 if ( n
== wxNOT_FOUND
)
344 // no old, add new if any
348 return; // skipping RebuildAccelTable() below
352 // replace old with new or just remove the old one if no new
357 m_accels
.RemoveAt(n
);
362 GetMenuBar()->RebuildAccelTable();
365 //else: it is a separator, they can't have accels, nothing to do
368 #endif // wxUSE_ACCEL
370 // append a new item or submenu to the menu
371 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
375 #endif // wxUSE_ACCEL
377 // we should support disabling the item even prior to adding it to the menu
378 UINT flags
= pItem
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
;
380 // if "Break" has just been called, insert a menu break before this item
381 // (and don't forget to reset the flag)
383 flags
|= MF_MENUBREAK
;
387 if ( pItem
->IsSeparator() ) {
388 flags
|= MF_SEPARATOR
;
391 // id is the numeric id for normal menu items and HMENU for submenus as
392 // required by ::AppendMenu() API
394 wxMenu
*submenu
= pItem
->GetSubMenu();
395 if ( submenu
!= NULL
) {
396 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
398 submenu
->SetParent(this);
400 id
= (UINT_PTR
)submenu
->GetHMenu();
405 id
= pItem
->GetMSWId();
409 // prepare to insert the item in the menu
410 wxString itemText
= pItem
->GetItemLabel();
411 LPCTSTR pData
= NULL
;
412 if ( pos
== (size_t)-1 )
414 // append at the end (note that the item is already appended to
415 // internal data structures)
416 pos
= GetMenuItemCount() - 1;
419 // adjust position to account for the title, if any
420 if ( !m_title
.empty() )
421 pos
+= 2; // for the title itself and its separator
425 #if wxUSE_OWNER_DRAWN
426 // Currently, mixing owner-drawn and non-owner-drawn items results in
427 // inconsistent margins, so we force this to be owner-drawn if any other
428 // items already are. Later we might want to use a boolean in the wxMenu
429 // to avoid search. Also we might make this fix unnecessary by getting the correct
430 // margin using NONCLIENTMETRICS.
431 if ( !pItem
->IsOwnerDrawn() && !pItem
->IsSeparator() )
433 // Check if any other items are ownerdrawn, and make ownerdrawn if so
434 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
437 if (node
->GetData()->IsOwnerDrawn())
439 pItem
->SetOwnerDrawn(true);
442 node
= node
->GetNext();
447 // check if we have something more than a simple text item
448 #if wxUSE_OWNER_DRAWN
449 if ( pItem
->IsOwnerDrawn() )
451 // is the item owner-drawn just because of the [checked] bitmap?
452 if ( (pItem
->GetBitmap(false).Ok() || pItem
->GetBitmap(true).Ok()) &&
453 !pItem
->GetTextColour().Ok() &&
454 !pItem
->GetBackgroundColour().Ok() &&
455 !pItem
->GetFont().Ok() )
457 // try to use InsertMenuItem() as it's guaranteed to look correct
458 // while our owner-drawn code is not
460 // DMC at march 2007 doesn't have HBITMAP hbmpItem tagMENUITEMINFOA /W
461 // MIIM_BITMAP only works under WinME/2000+
462 WinStruct
<MENUITEMINFO
> mii
;
463 if ( wxGetWinVersion() >= wxWinVersion_98
)
465 mii
.fMask
= MIIM_STRING
| MIIM_DATA
| MIIM_BITMAP
;
466 if ( pItem
->IsCheckable() )
468 // need to set checked/unchecked bitmaps as otherwise our
469 // MSWOnDrawItem() item is not called
470 mii
.fMask
|= MIIM_CHECKMARKS
;
473 mii
.cch
= itemText
.length();
474 mii
.dwTypeData
= const_cast<wxChar
*>(itemText
.wx_str());
476 if (flags
& MF_POPUP
)
478 mii
.fMask
|= MIIM_SUBMENU
;
479 mii
.hSubMenu
= (HMENU
)pItem
->GetSubMenu()->GetHMenu();
483 mii
.fMask
|= MIIM_ID
;
487 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
488 // 1. we can't draw it with transparency then (this is not
489 // very important now but would be with themed menu bg)
490 // 2. worse, Windows inverts the bitmap for the selected
491 // item and this looks downright ugly
493 // so instead draw it ourselves in MSWOnDrawItem()
494 mii
.dwItemData
= reinterpret_cast<ULONG_PTR
>(pItem
);
495 if ( pItem
->IsCheckable() )
498 mii
.hbmpUnchecked
= HBMMENU_CALLBACK
;
500 mii
.hbmpItem
= HBMMENU_CALLBACK
;
502 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
505 wxLogLastError(wxT("InsertMenuItem()"));
507 else // InsertMenuItem() ok
509 // we need to remove the extra indent which is reserved for
510 // the checkboxes by default as it looks ugly unless check
511 // boxes are used together with bitmaps and this is not the
513 WinStruct
<MENUINFO
> mi
;
515 // don't call SetMenuInfo() directly, this would prevent
516 // the app from starting up under Windows 95/NT 4
517 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
519 wxDynamicLibrary
dllUser(_T("user32"));
520 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
521 if ( pfnSetMenuInfo
)
523 mi
.fMask
= MIM_STYLE
;
524 mi
.dwStyle
= MNS_CHECKORBMP
;
525 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
526 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
529 // tell the item that it's not really owner-drawn but only
530 // needs to draw its bitmap, the rest is done by Windows
531 pItem
->ResetOwnerDrawn();
539 // item draws itself, pass pointer to it in data parameter
540 flags
|= MF_OWNERDRAW
;
541 pData
= (LPCTSTR
)pItem
;
545 #endif // wxUSE_OWNER_DRAWN
547 // item is just a normal string (passed in data parameter)
551 itemText
= wxMenuItem::GetLabelText(itemText
);
554 pData
= (wxChar
*)itemText
.wx_str();
557 // item might have already been inserted by InsertMenuItem() above
560 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
562 wxLogLastError(wxT("InsertMenu[Item]()"));
569 // if we just appended the title, highlight it
570 if ( id
== idMenuTitle
)
572 // visually select the menu title
573 SetDefaultMenuItem(GetHmenu(), id
);
576 // if we're already attached to the menubar, we must update it
577 if ( IsAttached() && GetMenuBar()->IsAttached() )
579 GetMenuBar()->Refresh();
585 void wxMenu::EndRadioGroup()
587 // we're not inside a radio group any longer
588 m_startRadioGroup
= -1;
591 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
593 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
597 if ( item
->GetKind() == wxITEM_RADIO
)
599 int count
= GetMenuItemCount();
601 if ( m_startRadioGroup
== -1 )
603 // start a new radio group
604 m_startRadioGroup
= count
;
606 // for now it has just one element
607 item
->SetAsRadioGroupStart();
608 item
->SetRadioGroupEnd(m_startRadioGroup
);
610 // ensure that we have a checked item in the radio group
613 else // extend the current radio group
615 // we need to update its end item
616 item
->SetRadioGroupStart(m_startRadioGroup
);
617 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
621 node
->GetData()->SetRadioGroupEnd(count
);
625 wxFAIL_MSG( _T("where is the radio group start item?") );
629 else // not a radio item
634 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
641 // check the item initially
648 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
650 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
656 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
658 // we need to find the item's position in the child list
660 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
661 for ( pos
= 0; node
; pos
++ )
663 if ( node
->GetData() == item
)
666 node
= node
->GetNext();
669 // DoRemove() (unlike Remove) can only be called for an existing item!
670 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
673 // remove the corresponding accel from the accel table
674 int n
= FindAccel(item
->GetId());
675 if ( n
!= wxNOT_FOUND
)
679 m_accels
.RemoveAt(n
);
681 //else: this item doesn't have an accel, nothing to do
682 #endif // wxUSE_ACCEL
684 // remove the item from the menu
685 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
687 wxLogLastError(wxT("RemoveMenu"));
690 if ( IsAttached() && GetMenuBar()->IsAttached() )
692 // otherwise, the change won't be visible
693 GetMenuBar()->Refresh();
696 // and from internal data structures
697 return wxMenuBase::DoRemove(item
);
700 // ---------------------------------------------------------------------------
701 // accelerator helpers
702 // ---------------------------------------------------------------------------
706 // create the wxAcceleratorEntries for our accels and put them into the provided
707 // array - return the number of accels we have
708 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
710 size_t count
= GetAccelCount();
711 for ( size_t n
= 0; n
< count
; n
++ )
713 *accels
++ = *m_accels
[n
];
719 wxAcceleratorTable
*wxMenu::CreateAccelTable() const
721 const size_t count
= m_accels
.size();
722 wxScopedArray
<wxAcceleratorEntry
> accels(new wxAcceleratorEntry
[count
]);
723 CopyAccels(accels
.get());
725 return new wxAcceleratorTable(count
, accels
.get());
728 #endif // wxUSE_ACCEL
730 // ---------------------------------------------------------------------------
732 // ---------------------------------------------------------------------------
734 void wxMenu::SetTitle(const wxString
& label
)
736 bool hasNoTitle
= m_title
.empty();
739 HMENU hMenu
= GetHmenu();
743 if ( !label
.empty() )
745 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
746 idMenuTitle
, m_title
.wx_str()) ||
747 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
749 wxLogLastError(wxT("InsertMenu"));
757 // remove the title and the separator after it
758 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
759 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
761 wxLogLastError(wxT("RemoveMenu"));
770 info
.cbSize
= sizeof(info
);
771 info
.fMask
= MIIM_TYPE
;
772 info
.fType
= MFT_STRING
;
773 info
.cch
= m_title
.length();
774 info
.dwTypeData
= const_cast<wxChar
*>(m_title
.wx_str());
775 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
777 wxLogLastError(wxT("SetMenuItemInfo"));
780 if ( !ModifyMenu(hMenu
, 0u,
781 MF_BYPOSITION
| MF_STRING
,
782 idMenuTitle
, m_title
.wx_str()) )
784 wxLogLastError(wxT("ModifyMenu"));
791 // put the title string in bold face
792 if ( !m_title
.empty() )
794 SetDefaultMenuItem(GetHmenu(), idMenuTitle
);
799 // ---------------------------------------------------------------------------
801 // ---------------------------------------------------------------------------
803 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
805 const int id
= (signed short)id_
;
807 // ignore commands from the menu title
808 if ( id
!= (int)idMenuTitle
)
810 // update the check item when it's clicked
811 wxMenuItem
* const item
= FindItem(id
);
812 if ( item
&& item
->IsCheckable() )
815 // get the status of the menu item: note that it has been just changed
816 // by Toggle() above so here we already get the new state of the item
817 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
818 SendEvent(id
, menuState
& MF_CHECKED
);
824 // ---------------------------------------------------------------------------
826 // ---------------------------------------------------------------------------
828 wxWindow
*wxMenu::GetWindow() const
830 if ( m_invokingWindow
!= NULL
)
831 return m_invokingWindow
;
832 else if ( GetMenuBar() != NULL
)
833 return GetMenuBar()->GetFrame();
838 // ---------------------------------------------------------------------------
840 // ---------------------------------------------------------------------------
842 void wxMenuBar::Init()
844 m_eventHandler
= this;
846 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
849 // Not using a combined wxToolBar/wxMenuBar? then use
850 // a commandbar in WinCE .NET just to implement the
852 #if defined(WINCE_WITH_COMMANDBAR)
854 m_adornmentsAdded
= false;
858 wxMenuBar::wxMenuBar()
863 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
868 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
872 m_titles
.Alloc(count
);
874 for ( size_t i
= 0; i
< count
; i
++ )
876 m_menus
.Append(menus
[i
]);
877 m_titles
.Add(titles
[i
]);
879 menus
[i
]->Attach(this);
883 wxMenuBar::~wxMenuBar()
885 // In Windows CE (not .NET), the menubar is always associated
886 // with a toolbar, which destroys the menu implicitly.
887 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
890 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
892 toolMenuBar
->SetMenuBar(NULL
);
895 // we should free Windows resources only if Windows doesn't do it for us
896 // which happens if we're attached to a frame
897 if (m_hMenu
&& !IsAttached())
899 #if defined(WINCE_WITH_COMMANDBAR)
900 ::DestroyWindow((HWND
) m_commandBar
);
901 m_commandBar
= (WXHWND
) NULL
;
903 ::DestroyMenu((HMENU
)m_hMenu
);
905 m_hMenu
= (WXHMENU
)NULL
;
910 // ---------------------------------------------------------------------------
912 // ---------------------------------------------------------------------------
914 void wxMenuBar::Refresh()
919 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
921 #if defined(WINCE_WITHOUT_COMMANDBAR)
924 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
926 #elif defined(WINCE_WITH_COMMANDBAR)
928 DrawMenuBar((HWND
) m_commandBar
);
930 DrawMenuBar(GetHwndOf(GetFrame()));
934 WXHMENU
wxMenuBar::Create()
936 // Note: this doesn't work at all on Smartphone,
937 // since you have to use resources.
938 // We'll have to find another way to add a menu
939 // by changing/adding menu items to an existing menu.
940 #if defined(WINCE_WITHOUT_COMMANDBAR)
944 wxToolMenuBar
* const bar
= static_cast<wxToolMenuBar
*>(GetToolBar());
948 HWND hCommandBar
= GetHwndOf(bar
);
950 // notify comctl32.dll about the version of the headers we use before using
951 // any other TB_XXX messages
952 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
955 wxZeroMemory(tbButton
);
956 tbButton
.iBitmap
= I_IMAGENONE
;
957 tbButton
.fsState
= TBSTATE_ENABLED
;
958 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
959 TBSTYLE_NO_DROPDOWN_ARROW
|
962 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
964 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
965 tbButton
.dwData
= (DWORD
)hPopupMenu
;
966 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
967 tbButton
.iString
= (int) label
.wx_str();
969 tbButton
.idCommand
= NewControlId();
970 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
972 wxLogLastError(wxT("TB_INSERTBUTTON"));
976 m_hMenu
= bar
->GetHMenu();
978 #else // !__WXWINCE__
982 m_hMenu
= (WXHMENU
)::CreateMenu();
986 wxLogLastError(wxT("CreateMenu"));
990 size_t count
= GetMenuCount(), i
;
991 wxMenuList::iterator it
;
992 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
994 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
995 (UINT_PTR
)(*it
)->GetHMenu(),
996 m_titles
[i
].wx_str()) )
998 wxLogLastError(wxT("AppendMenu"));
1004 #endif // __WXWINCE__/!__WXWINCE__
1007 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
1010 wxASSERT(menu
->GetHMenu());
1013 #if defined(__WXWINCE__)
1014 int totalMSWItems
= GetMenuCount();
1016 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1019 int i
; // For old C++ compatibility
1020 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1022 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1025 for(i
=0; i
<wxpos
; i
++)
1027 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1034 // ---------------------------------------------------------------------------
1035 // wxMenuBar functions to work with the top level submenus
1036 // ---------------------------------------------------------------------------
1038 // NB: we don't support owner drawn top level items for now, if we do these
1039 // functions would have to be changed to use wxMenuItem as well
1041 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1043 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1044 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1046 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1048 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1053 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1055 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1057 m_titles
[pos
] = label
;
1059 if ( !IsAttached() )
1063 //else: have to modify the existing menu
1065 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1068 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1069 if ( flagsOld
== 0xFFFFFFFF )
1071 wxLogLastError(wxT("GetMenuState"));
1076 if ( flagsOld
& MF_POPUP
)
1078 // HIBYTE contains the number of items in the submenu in this case
1080 id
= (UINT_PTR
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1090 info
.cbSize
= sizeof(info
);
1091 info
.fMask
= MIIM_TYPE
;
1092 info
.fType
= MFT_STRING
;
1093 info
.cch
= label
.length();
1094 info
.dwTypeData
= const_cast<wxChar
*>(label
.wx_str());
1095 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, &info
) )
1097 wxLogLastError(wxT("SetMenuItemInfo"));
1101 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1102 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1104 wxLogLastError(wxT("ModifyMenu"));
1111 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1113 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1114 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1116 return m_titles
[pos
];
1119 // ---------------------------------------------------------------------------
1120 // wxMenuBar construction
1121 // ---------------------------------------------------------------------------
1123 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1125 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1129 m_titles
[pos
] = title
;
1131 #if defined(WINCE_WITHOUT_COMMANDBAR)
1137 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1139 // can't use ModifyMenu() because it deletes the submenu it replaces
1140 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1142 wxLogLastError(wxT("RemoveMenu"));
1145 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1146 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1147 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1149 wxLogLastError(wxT("InsertMenu"));
1153 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1155 // need to rebuild accell table
1156 RebuildAccelTable();
1158 #endif // wxUSE_ACCEL
1167 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1169 // Find out which MSW item before which we'll be inserting before
1170 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1171 // If IsAttached() is false this won't be used anyway
1173 #if defined(WINCE_WITHOUT_COMMANDBAR)
1179 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1180 ? -1 // append the menu
1181 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1183 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1186 m_titles
.Insert(title
, pos
);
1190 #if defined(WINCE_WITHOUT_COMMANDBAR)
1194 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1195 tbButton
.iBitmap
= I_IMAGENONE
;
1196 tbButton
.fsState
= TBSTATE_ENABLED
;
1197 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1199 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1200 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1201 wxString label
= wxStripMenuCodes(title
);
1202 tbButton
.iString
= (int) label
.wx_str();
1204 tbButton
.idCommand
= NewControlId();
1205 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1207 wxLogLastError(wxT("TB_INSERTBUTTON"));
1210 wxUnusedVar(mswpos
);
1212 if ( !::InsertMenu(GetHmenu(), mswpos
,
1213 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1214 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1216 wxLogLastError(wxT("InsertMenu"));
1220 if ( menu
->HasAccels() )
1222 // need to rebuild accell table
1223 RebuildAccelTable();
1225 #endif // wxUSE_ACCEL
1234 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1236 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1237 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1239 if ( !wxMenuBarBase::Append(menu
, title
) )
1242 m_titles
.Add(title
);
1244 #if defined(WINCE_WITHOUT_COMMANDBAR)
1250 #if defined(WINCE_WITHOUT_COMMANDBAR)
1254 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1255 tbButton
.iBitmap
= I_IMAGENONE
;
1256 tbButton
.fsState
= TBSTATE_ENABLED
;
1257 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1259 size_t pos
= GetMenuCount();
1260 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1261 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1262 wxString label
= wxStripMenuCodes(title
);
1263 tbButton
.iString
= (int) label
.wx_str();
1265 tbButton
.idCommand
= NewControlId();
1266 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1268 wxLogLastError(wxT("TB_INSERTBUTTON"));
1272 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1273 (UINT_PTR
)submenu
, title
.wx_str()) )
1275 wxLogLastError(wxT("AppendMenu"));
1280 if ( menu
->HasAccels() )
1282 // need to rebuild accelerator table
1283 RebuildAccelTable();
1285 #endif // wxUSE_ACCEL
1294 wxMenu
*wxMenuBar::Remove(size_t pos
)
1296 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1300 #if defined(WINCE_WITHOUT_COMMANDBAR)
1306 #if defined(WINCE_WITHOUT_COMMANDBAR)
1309 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1311 wxLogLastError(wxT("TB_DELETEBUTTON"));
1315 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1317 wxLogLastError(wxT("RemoveMenu"));
1322 if ( menu
->HasAccels() )
1324 // need to rebuild accell table
1325 RebuildAccelTable();
1327 #endif // wxUSE_ACCEL
1333 m_titles
.RemoveAt(pos
);
1340 void wxMenuBar::RebuildAccelTable()
1342 // merge the accelerators of all menus into one accel table
1343 size_t nAccelCount
= 0;
1344 size_t i
, count
= GetMenuCount();
1345 wxMenuList::iterator it
;
1346 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1348 nAccelCount
+= (*it
)->GetAccelCount();
1353 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1356 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1358 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1361 SetAcceleratorTable(wxAcceleratorTable(nAccelCount
, accelEntries
));
1363 delete [] accelEntries
;
1367 #endif // wxUSE_ACCEL
1369 void wxMenuBar::Attach(wxFrame
*frame
)
1371 wxMenuBarBase::Attach(frame
);
1373 #if defined(WINCE_WITH_COMMANDBAR)
1377 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1382 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1384 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1391 RebuildAccelTable();
1392 #endif // wxUSE_ACCEL
1395 #if defined(WINCE_WITH_COMMANDBAR)
1396 bool wxMenuBar::AddAdornments(long style
)
1398 if (m_adornmentsAdded
|| !m_commandBar
)
1401 if (style
& wxCLOSE_BOX
)
1403 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1404 wxLogLastError(wxT("CommandBar_AddAdornments"));
1412 void wxMenuBar::Detach()
1414 wxMenuBarBase::Detach();
1417 #endif // wxUSE_MENUS