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"
40 #include "wx/ownerdrw.h"
43 #include "wx/scopedarray.h"
45 #include "wx/msw/private.h"
46 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
54 #if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__)
58 #include "wx/msw/wince/missing.h"
62 // other standard headers
66 #include "wx/dynlib.h"
69 #ifndef MNS_CHECKORBMP
70 #define MNS_CHECKORBMP 0x04000000
73 #define MIM_STYLE 0x00000010
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 // the (popup) menu title has this special id
85 static const UINT idMenuTitle
= (UINT
)-3;
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
94 // make the given menu item default
95 void SetDefaultMenuItem(HMENU
WXUNUSED_IN_WINCE(hmenu
),
96 UINT
WXUNUSED_IN_WINCE(id
))
101 mii
.cbSize
= sizeof(MENUITEMINFO
);
102 mii
.fMask
= MIIM_STATE
;
103 mii
.fState
= MFS_DEFAULT
;
105 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
107 wxLogLastError(wxT("SetMenuItemInfo"));
113 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
117 info
.cbSize
= sizeof(info
);
118 info
.fMask
= MIIM_STATE
;
119 // MF_BYCOMMAND is zero so test MF_BYPOSITION
120 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
121 wxLogLastError(wxT("GetMenuItemInfo"));
124 #endif // __WXWINCE__
126 bool IsLessThanStdSize(const wxBitmap
& bmp
)
128 // FIXME: these +4 are chosen so that 16*16 bitmaps pass this test with
129 // default SM_CXMENUCHECK value but I have no idea what do we really
131 return bmp
.GetWidth() < ::GetSystemMetrics(SM_CXMENUCHECK
) + 4 &&
132 bmp
.GetHeight() < ::GetSystemMetrics(SM_CYMENUCHECK
) + 4;
135 } // anonymous namespace
137 // ============================================================================
139 // ============================================================================
141 #include "wx/listimpl.cpp"
143 WX_DEFINE_LIST( wxMenuInfoList
)
145 #if wxUSE_EXTENDED_RTTI
147 WX_DEFINE_FLAGS( wxMenuStyle
)
149 wxBEGIN_FLAGS( wxMenuStyle
)
150 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
151 wxEND_FLAGS( wxMenuStyle
)
153 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
155 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
157 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
159 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
162 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
163 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
164 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
165 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
166 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
167 wxEND_PROPERTIES_TABLE()
169 wxBEGIN_HANDLERS_TABLE(wxMenu
)
170 wxEND_HANDLERS_TABLE()
172 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
174 WX_DEFINE_FLAGS( wxMenuBarStyle
)
176 wxBEGIN_FLAGS( wxMenuBarStyle
)
177 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
178 wxEND_FLAGS( wxMenuBarStyle
)
180 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
181 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
186 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
188 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
190 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
191 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
192 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
193 wxEND_PROPERTIES_TABLE()
195 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
196 wxEND_HANDLERS_TABLE()
198 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
200 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
202 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
204 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
207 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
208 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
209 wxEND_PROPERTIES_TABLE()
211 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
212 wxEND_HANDLERS_TABLE()
214 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
217 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
218 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
219 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
222 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
224 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
225 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
226 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
228 wxMenuInfo
* info
= new wxMenuInfo() ;
229 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetMenuLabel(i
) ) ;
230 list
->Append( info
) ;
235 // ---------------------------------------------------------------------------
236 // wxMenu construction, adding and removing menu items
237 // ---------------------------------------------------------------------------
239 // Construct a menu with optional title (then use append)
243 m_startRadioGroup
= -1;
246 m_hMenu
= (WXHMENU
)CreatePopupMenu();
249 wxLogLastError(wxT("CreatePopupMenu"));
252 // if we have a title, insert it in the beginning of the menu
253 if ( !m_title
.empty() )
255 Append(idMenuTitle
, m_title
);
260 // The wxWindow destructor will take care of deleting the submenus.
263 // we should free Windows resources only if Windows doesn't do it for us
264 // which happens if we're attached to a menubar or a submenu of another
266 if ( !IsAttached() && !GetParent() )
268 if ( !::DestroyMenu(GetHmenu()) )
270 wxLogLastError(wxT("DestroyMenu"));
276 WX_CLEAR_ARRAY(m_accels
);
277 #endif // wxUSE_ACCEL
282 // this will take effect during the next call to Append()
286 void wxMenu::Attach(wxMenuBarBase
*menubar
)
288 wxMenuBase::Attach(menubar
);
295 int wxMenu::FindAccel(int id
) const
297 size_t n
, count
= m_accels
.GetCount();
298 for ( n
= 0; n
< count
; n
++ )
300 if ( m_accels
[n
]->m_command
== id
)
307 void wxMenu::UpdateAccel(wxMenuItem
*item
)
309 if ( item
->IsSubMenu() )
311 wxMenu
*submenu
= item
->GetSubMenu();
312 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
315 UpdateAccel(node
->GetData());
317 node
= node
->GetNext();
320 else if ( !item
->IsSeparator() )
322 // recurse upwards: we should only modify m_accels of the top level
323 // menus, not of the submenus as wxMenuBar doesn't look at them
324 // (alternative and arguable cleaner solution would be to recurse
325 // downwards in GetAccelCount() and CopyAccels())
328 GetParent()->UpdateAccel(item
);
332 // find the (new) accel for this item
333 wxAcceleratorEntry
*accel
= wxAcceleratorEntry::Create(item
->GetItemLabel());
335 accel
->m_command
= item
->GetId();
338 int n
= FindAccel(item
->GetId());
339 if ( n
== wxNOT_FOUND
)
341 // no old, add new if any
345 return; // skipping RebuildAccelTable() below
349 // replace old with new or just remove the old one if no new
354 m_accels
.RemoveAt(n
);
359 GetMenuBar()->RebuildAccelTable();
362 //else: it is a separator, they can't have accels, nothing to do
365 #endif // wxUSE_ACCEL
367 // append a new item or submenu to the menu
368 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
372 #endif // wxUSE_ACCEL
374 // we should support disabling the item even prior to adding it to the menu
375 UINT flags
= pItem
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
;
377 // if "Break" has just been called, insert a menu break before this item
378 // (and don't forget to reset the flag)
380 flags
|= MF_MENUBREAK
;
384 if ( pItem
->IsSeparator() ) {
385 flags
|= MF_SEPARATOR
;
388 // id is the numeric id for normal menu items and HMENU for submenus as
389 // required by ::AppendMenu() API
391 wxMenu
*submenu
= pItem
->GetSubMenu();
392 if ( submenu
!= NULL
) {
393 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
395 submenu
->SetParent(this);
397 id
= (UINT_PTR
)submenu
->GetHMenu();
402 id
= pItem
->GetMSWId();
406 // prepare to insert the item in the menu
407 wxString itemText
= pItem
->GetItemLabel();
408 LPCTSTR pData
= NULL
;
409 if ( pos
== (size_t)-1 )
411 // append at the end (note that the item is already appended to
412 // internal data structures)
413 pos
= GetMenuItemCount() - 1;
416 // adjust position to account for the title, if any
417 if ( !m_title
.empty() )
418 pos
+= 2; // for the title itself and its separator
422 #if wxUSE_OWNER_DRAWN
423 // Currently, mixing owner-drawn and non-owner-drawn items results in
424 // inconsistent margins, so we force this to be owner-drawn if any other
425 // items already are. Later we might want to use a boolean in the wxMenu
426 // to avoid search. Also we might make this fix unnecessary by getting the correct
427 // margin using NONCLIENTMETRICS.
428 if ( !pItem
->IsOwnerDrawn() && !pItem
->IsSeparator() )
430 // Check if any other items are ownerdrawn, and make ownerdrawn if so
431 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
434 if (node
->GetData()->IsOwnerDrawn())
436 pItem
->SetOwnerDrawn(true);
439 node
= node
->GetNext();
444 // check if we have something more than a simple text item
445 #if wxUSE_OWNER_DRAWN
446 if ( pItem
->IsOwnerDrawn() )
449 // if the item is owner-drawn just because of the [checked] bitmap and
450 // the bitmap uses standard menu bitmap size we can avoid making it
451 // owner-drawn and use built-in support for menu bitmaps instead
452 bool mustUseOwnerDrawn
= pItem
->GetTextColour().Ok() ||
453 pItem
->GetBackgroundColour().Ok() ||
454 pItem
->GetFont().Ok();
455 if ( !mustUseOwnerDrawn
)
457 const wxBitmap
& bmpUnchecked
= pItem
->GetBitmap(false),
458 bmpChecked
= pItem
->GetBitmap(true);
459 if ( (bmpUnchecked
.Ok() && !IsLessThanStdSize(bmpUnchecked
)) ||
460 (bmpChecked
.Ok() && !IsLessThanStdSize(bmpChecked
)) )
462 mustUseOwnerDrawn
= true;
466 // MIIM_BITMAP only works under WinME/2000+
467 if ( !mustUseOwnerDrawn
&& wxGetWinVersion() >= wxWinVersion_98
)
469 // use InsertMenuItem() as it's guaranteed to look correct while
470 // our owner-drawn code is not
471 WinStruct
<MENUITEMINFO
> mii
;
472 mii
.fMask
= MIIM_STRING
| MIIM_DATA
| MIIM_BITMAP
;
473 if ( pItem
->IsCheckable() )
475 // need to set checked/unchecked bitmaps as otherwise our
476 // MSWOnDrawItem() item is not called
477 mii
.fMask
|= MIIM_CHECKMARKS
;
480 mii
.cch
= itemText
.length();
481 mii
.dwTypeData
= const_cast<wxChar
*>(itemText
.wx_str());
483 if (flags
& MF_POPUP
)
485 mii
.fMask
|= MIIM_SUBMENU
;
486 mii
.hSubMenu
= (HMENU
)pItem
->GetSubMenu()->GetHMenu();
490 mii
.fMask
|= MIIM_ID
;
494 // Under versions of Windows older than Vista we can't pass HBITMAP
495 // directly as hbmpItem for 2 reasons:
496 // 1. We can't draw it with transparency then (this is not
497 // very important now but would be with themed menu bg)
498 // 2. Worse, Windows inverts the bitmap for the selected
499 // item and this looks downright ugly
501 // so we prefer to instead draw it ourselves in MSWOnDrawItem().
502 mii
.dwItemData
= reinterpret_cast<ULONG_PTR
>(pItem
);
503 if ( pItem
->IsCheckable() )
506 mii
.hbmpUnchecked
= HBMMENU_CALLBACK
;
509 // However under Vista using HBMMENU_CALLBACK causes the entire
510 // menu to be drawn using the classic theme instead of the current
511 // one and it does handle transparency just fine so do use the real
514 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
516 // but we need to have transparency for this to work so ensure
518 wxImage
img(pItem
->GetBitmap().ConvertToImage());
519 if ( !img
.HasAlpha() )
522 pItem
->SetBitmap(img
);
525 mii
.hbmpItem
= GetHbitmapOf(pItem
->GetBitmap());
528 #endif // wxUSE_IMAGE
530 mii
.hbmpItem
= HBMMENU_CALLBACK
;
533 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
536 wxLogLastError(wxT("InsertMenuItem()"));
538 else // InsertMenuItem() ok
540 // we need to remove the extra indent which is reserved for
541 // the checkboxes by default as it looks ugly unless check
542 // boxes are used together with bitmaps and this is not the
544 WinStruct
<MENUINFO
> mi
;
546 // don't call SetMenuInfo() directly, this would prevent
547 // the app from starting up under Windows 95/NT 4
548 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
550 wxDynamicLibrary
dllUser(_T("user32"));
551 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
552 if ( pfnSetMenuInfo
)
554 mi
.fMask
= MIM_STYLE
;
555 mi
.dwStyle
= MNS_CHECKORBMP
;
556 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
557 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
560 // tell the item that it's not really owner-drawn but only
561 // needs to draw its bitmap, the rest is done by Windows
562 pItem
->ResetOwnerDrawn();
569 // item draws itself, pass pointer to it in data parameter
570 flags
|= MF_OWNERDRAW
;
571 pData
= (LPCTSTR
)pItem
;
575 #endif // wxUSE_OWNER_DRAWN
577 // item is just a normal string (passed in data parameter)
581 itemText
= wxMenuItem::GetLabelText(itemText
);
584 pData
= (wxChar
*)itemText
.wx_str();
587 // item might have already been inserted by InsertMenuItem() above
590 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
592 wxLogLastError(wxT("InsertMenu[Item]()"));
599 // if we just appended the title, highlight it
600 if ( id
== idMenuTitle
)
602 // visually select the menu title
603 SetDefaultMenuItem(GetHmenu(), id
);
606 // if we're already attached to the menubar, we must update it
607 if ( IsAttached() && GetMenuBar()->IsAttached() )
609 GetMenuBar()->Refresh();
615 void wxMenu::EndRadioGroup()
617 // we're not inside a radio group any longer
618 m_startRadioGroup
= -1;
621 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
623 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
627 if ( item
->GetKind() == wxITEM_RADIO
)
629 int count
= GetMenuItemCount();
631 if ( m_startRadioGroup
== -1 )
633 // start a new radio group
634 m_startRadioGroup
= count
;
636 // for now it has just one element
637 item
->SetAsRadioGroupStart();
638 item
->SetRadioGroupEnd(m_startRadioGroup
);
640 // ensure that we have a checked item in the radio group
643 else // extend the current radio group
645 // we need to update its end item
646 item
->SetRadioGroupStart(m_startRadioGroup
);
647 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
651 node
->GetData()->SetRadioGroupEnd(count
);
655 wxFAIL_MSG( _T("where is the radio group start item?") );
659 else // not a radio item
664 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
671 // check the item initially
678 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
680 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
686 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
688 // we need to find the item's position in the child list
690 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
691 for ( pos
= 0; node
; pos
++ )
693 if ( node
->GetData() == item
)
696 node
= node
->GetNext();
699 // DoRemove() (unlike Remove) can only be called for an existing item!
700 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
703 // remove the corresponding accel from the accel table
704 int n
= FindAccel(item
->GetId());
705 if ( n
!= wxNOT_FOUND
)
709 m_accels
.RemoveAt(n
);
711 //else: this item doesn't have an accel, nothing to do
712 #endif // wxUSE_ACCEL
714 // remove the item from the menu
715 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
717 wxLogLastError(wxT("RemoveMenu"));
720 if ( IsAttached() && GetMenuBar()->IsAttached() )
722 // otherwise, the change won't be visible
723 GetMenuBar()->Refresh();
726 // and from internal data structures
727 return wxMenuBase::DoRemove(item
);
730 // ---------------------------------------------------------------------------
731 // accelerator helpers
732 // ---------------------------------------------------------------------------
736 // create the wxAcceleratorEntries for our accels and put them into the provided
737 // array - return the number of accels we have
738 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
740 size_t count
= GetAccelCount();
741 for ( size_t n
= 0; n
< count
; n
++ )
743 *accels
++ = *m_accels
[n
];
749 wxAcceleratorTable
*wxMenu::CreateAccelTable() const
751 const size_t count
= m_accels
.size();
752 wxScopedArray
<wxAcceleratorEntry
> accels(new wxAcceleratorEntry
[count
]);
753 CopyAccels(accels
.get());
755 return new wxAcceleratorTable(count
, accels
.get());
758 #endif // wxUSE_ACCEL
760 // ---------------------------------------------------------------------------
762 // ---------------------------------------------------------------------------
764 void wxMenu::SetTitle(const wxString
& label
)
766 bool hasNoTitle
= m_title
.empty();
769 HMENU hMenu
= GetHmenu();
773 if ( !label
.empty() )
775 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
776 idMenuTitle
, m_title
.wx_str()) ||
777 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
779 wxLogLastError(wxT("InsertMenu"));
787 // remove the title and the separator after it
788 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
789 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
791 wxLogLastError(wxT("RemoveMenu"));
800 info
.cbSize
= sizeof(info
);
801 info
.fMask
= MIIM_TYPE
;
802 info
.fType
= MFT_STRING
;
803 info
.cch
= m_title
.length();
804 info
.dwTypeData
= const_cast<wxChar
*>(m_title
.wx_str());
805 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
807 wxLogLastError(wxT("SetMenuItemInfo"));
810 if ( !ModifyMenu(hMenu
, 0u,
811 MF_BYPOSITION
| MF_STRING
,
812 idMenuTitle
, m_title
.wx_str()) )
814 wxLogLastError(wxT("ModifyMenu"));
821 // put the title string in bold face
822 if ( !m_title
.empty() )
824 SetDefaultMenuItem(GetHmenu(), idMenuTitle
);
829 // ---------------------------------------------------------------------------
831 // ---------------------------------------------------------------------------
833 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
835 const int id
= (signed short)id_
;
837 // ignore commands from the menu title
838 if ( id
!= (int)idMenuTitle
)
840 // update the check item when it's clicked
841 wxMenuItem
* const item
= FindItem(id
);
842 if ( item
&& item
->IsCheckable() )
845 // get the status of the menu item: note that it has been just changed
846 // by Toggle() above so here we already get the new state of the item
847 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
848 SendEvent(id
, menuState
& MF_CHECKED
);
854 // ---------------------------------------------------------------------------
856 // ---------------------------------------------------------------------------
858 wxWindow
*wxMenu::GetWindow() const
860 if ( m_invokingWindow
!= NULL
)
861 return m_invokingWindow
;
862 else if ( GetMenuBar() != NULL
)
863 return GetMenuBar()->GetFrame();
868 // ---------------------------------------------------------------------------
870 // ---------------------------------------------------------------------------
872 void wxMenuBar::Init()
874 m_eventHandler
= this;
876 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
879 // Not using a combined wxToolBar/wxMenuBar? then use
880 // a commandbar in WinCE .NET just to implement the
882 #if defined(WINCE_WITH_COMMANDBAR)
884 m_adornmentsAdded
= false;
888 wxMenuBar::wxMenuBar()
893 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
898 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
902 m_titles
.Alloc(count
);
904 for ( size_t i
= 0; i
< count
; i
++ )
906 m_menus
.Append(menus
[i
]);
907 m_titles
.Add(titles
[i
]);
909 menus
[i
]->Attach(this);
913 wxMenuBar::~wxMenuBar()
915 // In Windows CE (not .NET), the menubar is always associated
916 // with a toolbar, which destroys the menu implicitly.
917 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
920 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
922 toolMenuBar
->SetMenuBar(NULL
);
925 // we should free Windows resources only if Windows doesn't do it for us
926 // which happens if we're attached to a frame
927 if (m_hMenu
&& !IsAttached())
929 #if defined(WINCE_WITH_COMMANDBAR)
930 ::DestroyWindow((HWND
) m_commandBar
);
931 m_commandBar
= (WXHWND
) NULL
;
933 ::DestroyMenu((HMENU
)m_hMenu
);
935 m_hMenu
= (WXHMENU
)NULL
;
940 // ---------------------------------------------------------------------------
942 // ---------------------------------------------------------------------------
944 void wxMenuBar::Refresh()
949 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
951 #if defined(WINCE_WITHOUT_COMMANDBAR)
954 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
956 #elif defined(WINCE_WITH_COMMANDBAR)
958 DrawMenuBar((HWND
) m_commandBar
);
960 DrawMenuBar(GetHwndOf(GetFrame()));
964 WXHMENU
wxMenuBar::Create()
966 // Note: this doesn't work at all on Smartphone,
967 // since you have to use resources.
968 // We'll have to find another way to add a menu
969 // by changing/adding menu items to an existing menu.
970 #if defined(WINCE_WITHOUT_COMMANDBAR)
974 wxToolMenuBar
* const bar
= static_cast<wxToolMenuBar
*>(GetToolBar());
978 HWND hCommandBar
= GetHwndOf(bar
);
980 // notify comctl32.dll about the version of the headers we use before using
981 // any other TB_XXX messages
982 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
985 wxZeroMemory(tbButton
);
986 tbButton
.iBitmap
= I_IMAGENONE
;
987 tbButton
.fsState
= TBSTATE_ENABLED
;
988 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
989 TBSTYLE_NO_DROPDOWN_ARROW
|
992 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
994 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
995 tbButton
.dwData
= (DWORD
)hPopupMenu
;
996 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
997 tbButton
.iString
= (int) label
.wx_str();
999 tbButton
.idCommand
= NewControlId();
1000 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
1002 wxLogLastError(wxT("TB_INSERTBUTTON"));
1006 m_hMenu
= bar
->GetHMenu();
1008 #else // !__WXWINCE__
1012 m_hMenu
= (WXHMENU
)::CreateMenu();
1016 wxLogLastError(wxT("CreateMenu"));
1020 size_t count
= GetMenuCount(), i
;
1021 wxMenuList::iterator it
;
1022 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1024 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
1025 (UINT_PTR
)(*it
)->GetHMenu(),
1026 m_titles
[i
].wx_str()) )
1028 wxLogLastError(wxT("AppendMenu"));
1034 #endif // __WXWINCE__/!__WXWINCE__
1037 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
1040 wxASSERT(menu
->GetHMenu());
1043 #if defined(__WXWINCE__)
1044 int totalMSWItems
= GetMenuCount();
1046 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1049 int i
; // For old C++ compatibility
1050 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1052 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1055 for(i
=0; i
<wxpos
; i
++)
1057 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1064 // ---------------------------------------------------------------------------
1065 // wxMenuBar functions to work with the top level submenus
1066 // ---------------------------------------------------------------------------
1068 // NB: we don't support owner drawn top level items for now, if we do these
1069 // functions would have to be changed to use wxMenuItem as well
1071 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1073 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1074 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1076 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1078 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1083 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1085 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1087 m_titles
[pos
] = label
;
1089 if ( !IsAttached() )
1093 //else: have to modify the existing menu
1095 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1098 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1099 if ( flagsOld
== 0xFFFFFFFF )
1101 wxLogLastError(wxT("GetMenuState"));
1106 if ( flagsOld
& MF_POPUP
)
1108 // HIBYTE contains the number of items in the submenu in this case
1110 id
= (UINT_PTR
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1120 info
.cbSize
= sizeof(info
);
1121 info
.fMask
= MIIM_TYPE
;
1122 info
.fType
= MFT_STRING
;
1123 info
.cch
= label
.length();
1124 info
.dwTypeData
= const_cast<wxChar
*>(label
.wx_str());
1125 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, &info
) )
1127 wxLogLastError(wxT("SetMenuItemInfo"));
1131 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1132 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1134 wxLogLastError(wxT("ModifyMenu"));
1141 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1143 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1144 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1146 return m_titles
[pos
];
1149 // ---------------------------------------------------------------------------
1150 // wxMenuBar construction
1151 // ---------------------------------------------------------------------------
1153 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1155 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1159 m_titles
[pos
] = title
;
1161 #if defined(WINCE_WITHOUT_COMMANDBAR)
1167 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1169 // can't use ModifyMenu() because it deletes the submenu it replaces
1170 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1172 wxLogLastError(wxT("RemoveMenu"));
1175 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1176 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1177 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1179 wxLogLastError(wxT("InsertMenu"));
1183 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1185 // need to rebuild accell table
1186 RebuildAccelTable();
1188 #endif // wxUSE_ACCEL
1197 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1199 // Find out which MSW item before which we'll be inserting before
1200 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1201 // If IsAttached() is false this won't be used anyway
1203 #if defined(WINCE_WITHOUT_COMMANDBAR)
1209 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1210 ? -1 // append the menu
1211 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1213 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1216 m_titles
.Insert(title
, pos
);
1220 #if defined(WINCE_WITHOUT_COMMANDBAR)
1224 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1225 tbButton
.iBitmap
= I_IMAGENONE
;
1226 tbButton
.fsState
= TBSTATE_ENABLED
;
1227 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1229 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1230 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1231 wxString label
= wxStripMenuCodes(title
);
1232 tbButton
.iString
= (int) label
.wx_str();
1234 tbButton
.idCommand
= NewControlId();
1235 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1237 wxLogLastError(wxT("TB_INSERTBUTTON"));
1240 wxUnusedVar(mswpos
);
1242 if ( !::InsertMenu(GetHmenu(), mswpos
,
1243 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1244 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1246 wxLogLastError(wxT("InsertMenu"));
1250 if ( menu
->HasAccels() )
1252 // need to rebuild accell table
1253 RebuildAccelTable();
1255 #endif // wxUSE_ACCEL
1264 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1266 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1267 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1269 if ( !wxMenuBarBase::Append(menu
, title
) )
1272 m_titles
.Add(title
);
1274 #if defined(WINCE_WITHOUT_COMMANDBAR)
1280 #if defined(WINCE_WITHOUT_COMMANDBAR)
1284 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1285 tbButton
.iBitmap
= I_IMAGENONE
;
1286 tbButton
.fsState
= TBSTATE_ENABLED
;
1287 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1289 size_t pos
= GetMenuCount();
1290 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1291 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1292 wxString label
= wxStripMenuCodes(title
);
1293 tbButton
.iString
= (int) label
.wx_str();
1295 tbButton
.idCommand
= NewControlId();
1296 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1298 wxLogLastError(wxT("TB_INSERTBUTTON"));
1302 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1303 (UINT_PTR
)submenu
, title
.wx_str()) )
1305 wxLogLastError(wxT("AppendMenu"));
1310 if ( menu
->HasAccels() )
1312 // need to rebuild accelerator table
1313 RebuildAccelTable();
1315 #endif // wxUSE_ACCEL
1324 wxMenu
*wxMenuBar::Remove(size_t pos
)
1326 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1330 #if defined(WINCE_WITHOUT_COMMANDBAR)
1336 #if defined(WINCE_WITHOUT_COMMANDBAR)
1339 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1341 wxLogLastError(wxT("TB_DELETEBUTTON"));
1345 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1347 wxLogLastError(wxT("RemoveMenu"));
1352 if ( menu
->HasAccels() )
1354 // need to rebuild accell table
1355 RebuildAccelTable();
1357 #endif // wxUSE_ACCEL
1363 m_titles
.RemoveAt(pos
);
1370 void wxMenuBar::RebuildAccelTable()
1372 // merge the accelerators of all menus into one accel table
1373 size_t nAccelCount
= 0;
1374 size_t i
, count
= GetMenuCount();
1375 wxMenuList::iterator it
;
1376 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1378 nAccelCount
+= (*it
)->GetAccelCount();
1383 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1386 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1388 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1391 SetAcceleratorTable(wxAcceleratorTable(nAccelCount
, accelEntries
));
1393 delete [] accelEntries
;
1397 #endif // wxUSE_ACCEL
1399 void wxMenuBar::Attach(wxFrame
*frame
)
1401 wxMenuBarBase::Attach(frame
);
1403 #if defined(WINCE_WITH_COMMANDBAR)
1407 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1412 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1414 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1421 RebuildAccelTable();
1422 #endif // wxUSE_ACCEL
1425 #if defined(WINCE_WITH_COMMANDBAR)
1426 bool wxMenuBar::AddAdornments(long style
)
1428 if (m_adornmentsAdded
|| !m_commandBar
)
1431 if (style
& wxCLOSE_BOX
)
1433 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1434 wxLogLastError(wxT("CommandBar_AddAdornments"));
1442 void wxMenuBar::Detach()
1444 wxMenuBarBase::Detach();
1447 #endif // wxUSE_MENUS