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
370 // helper of DoInsertOrAppend(): returns the HBITMAP to use in MENUITEMINFO
371 HBITMAP
GetHBitmapForMenu(wxMenuItem
*pItem
, bool checked
= true)
373 // Under versions of Windows older than Vista we can't pass HBITMAP
374 // directly as hbmpItem for 2 reasons:
375 // 1. We can't draw it with transparency then (this is not
376 // very important now but would be with themed menu bg)
377 // 2. Worse, Windows inverts the bitmap for the selected
378 // item and this looks downright ugly
380 // So we prefer to instead draw it ourselves in MSWOnDrawItem().by using
381 // HBMMENU_CALLBACK when inserting it
383 // However under Vista using HBMMENU_CALLBACK causes the entire menu to be
384 // drawn using the classic theme instead of the current one and it does
385 // handle transparency just fine so do use the real bitmap there
387 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
389 wxBitmap bmp
= pItem
->GetBitmap(checked
);
392 // we must use PARGB DIB for the menu bitmaps so ensure that we do
393 wxImage
img(bmp
.ConvertToImage());
394 if ( !img
.HasAlpha() )
397 pItem
->SetBitmap(img
, checked
);
400 return GetHbitmapOf(pItem
->GetBitmap());
403 #endif // wxUSE_IMAGE
405 return HBMMENU_CALLBACK
;
408 } // anonymous namespace
410 // append a new item or submenu to the menu
411 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
415 #endif // wxUSE_ACCEL
417 // we should support disabling the item even prior to adding it to the menu
418 UINT flags
= pItem
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
;
420 // if "Break" has just been called, insert a menu break before this item
421 // (and don't forget to reset the flag)
423 flags
|= MF_MENUBREAK
;
427 if ( pItem
->IsSeparator() ) {
428 flags
|= MF_SEPARATOR
;
431 // id is the numeric id for normal menu items and HMENU for submenus as
432 // required by ::AppendMenu() API
434 wxMenu
*submenu
= pItem
->GetSubMenu();
435 if ( submenu
!= NULL
) {
436 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
438 submenu
->SetParent(this);
440 id
= (UINT_PTR
)submenu
->GetHMenu();
445 id
= pItem
->GetMSWId();
449 // prepare to insert the item in the menu
450 wxString itemText
= pItem
->GetItemLabel();
451 LPCTSTR pData
= NULL
;
452 if ( pos
== (size_t)-1 )
454 // append at the end (note that the item is already appended to
455 // internal data structures)
456 pos
= GetMenuItemCount() - 1;
459 // adjust position to account for the title, if any
460 if ( !m_title
.empty() )
461 pos
+= 2; // for the title itself and its separator
465 #if wxUSE_OWNER_DRAWN
466 // Under older systems mixing owner-drawn and non-owner-drawn items results
467 // in inconsistent margins, so we force this one to be owner-drawn if any
468 // other items already are. Later we might want to use a boolean in the
469 // wxMenu to avoid search. Also we might make this fix unnecessary by
470 // getting the correct margin using NONCLIENTMETRICS.
471 static const wxWinVersion winver
= wxGetWinVersion();
472 if ( winver
< wxWinVersion_XP
&&
473 !pItem
->IsOwnerDrawn() && !pItem
->IsSeparator() )
475 // Check if any other items are ownerdrawn, and make ownerdrawn if so
476 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
479 if (node
->GetData()->IsOwnerDrawn())
481 pItem
->SetOwnerDrawn(true);
484 node
= node
->GetNext();
487 #endif // wxUSE_OWNER_DRAWN
489 // check if we have something more than a simple text item
490 #if wxUSE_OWNER_DRAWN
491 if ( pItem
->IsOwnerDrawn() )
494 // MIIM_BITMAP only works under WinME/2000+ so we always use owner
495 // drawn item under the previous versions and we also have to use them
496 // in any case if the item has custom colours or font
497 bool mustUseOwnerDrawn
= winver
< wxWinVersion_98
||
498 pItem
->GetTextColour().Ok() ||
499 pItem
->GetBackgroundColour().Ok() ||
500 pItem
->GetFont().Ok();
501 if ( !mustUseOwnerDrawn
)
503 const wxBitmap
& bmpUnchecked
= pItem
->GetBitmap(false),
504 bmpChecked
= pItem
->GetBitmap(true);
505 if ( (bmpUnchecked
.Ok() && !IsLessThanStdSize(bmpUnchecked
)) ||
506 (bmpChecked
.Ok() && !IsLessThanStdSize(bmpChecked
)) )
508 mustUseOwnerDrawn
= true;
512 // use InsertMenuItem() if possible as it's guaranteed to look correct
513 // while our owner-drawn code is not
514 if ( !mustUseOwnerDrawn
)
516 WinStruct
<MENUITEMINFO
> mii
;
517 mii
.fMask
= MIIM_STRING
| MIIM_DATA
;
519 if ( pItem
->GetBitmap().IsOk() )
521 mii
.fMask
|= MIIM_BITMAP
;
522 mii
.hbmpItem
= GetHBitmapForMenu(pItem
);
525 if ( pItem
->IsCheckable() )
527 mii
.fMask
|= MIIM_CHECKMARKS
;
528 mii
.hbmpChecked
= GetHBitmapForMenu(pItem
, true);
529 mii
.hbmpUnchecked
= GetHBitmapForMenu(pItem
, false);
532 mii
.cch
= itemText
.length();
533 mii
.dwTypeData
= const_cast<wxChar
*>(itemText
.wx_str());
535 if ( flags
& MF_POPUP
)
537 mii
.fMask
|= MIIM_SUBMENU
;
538 mii
.hSubMenu
= GetHmenuOf(pItem
->GetSubMenu());
542 mii
.fMask
|= MIIM_ID
;
546 mii
.dwItemData
= reinterpret_cast<ULONG_PTR
>(pItem
);
548 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
551 wxLogLastError(wxT("InsertMenuItem()"));
553 else // InsertMenuItem() ok
555 // we need to remove the extra indent which is reserved for
556 // the checkboxes by default as it looks ugly unless check
557 // boxes are used together with bitmaps and this is not the
559 WinStruct
<MENUINFO
> mi
;
561 // don't call SetMenuInfo() directly, this would prevent
562 // the app from starting up under Windows 95/NT 4
563 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
565 wxDynamicLibrary
dllUser(_T("user32"));
566 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
567 if ( pfnSetMenuInfo
)
569 mi
.fMask
= MIM_STYLE
;
570 mi
.dwStyle
= MNS_CHECKORBMP
;
571 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
572 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
575 // tell the item that it's not really owner-drawn but only
576 // needs to draw its bitmap, the rest is done by Windows
577 pItem
->ResetOwnerDrawn();
584 // item draws itself, pass pointer to it in data parameter
585 flags
|= MF_OWNERDRAW
;
586 pData
= (LPCTSTR
)pItem
;
590 #endif // wxUSE_OWNER_DRAWN
592 // item is just a normal string (passed in data parameter)
596 itemText
= wxMenuItem::GetLabelText(itemText
);
599 pData
= (wxChar
*)itemText
.wx_str();
602 // item might have already been inserted by InsertMenuItem() above
605 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
607 wxLogLastError(wxT("InsertMenu[Item]()"));
614 // if we just appended the title, highlight it
615 if ( id
== idMenuTitle
)
617 // visually select the menu title
618 SetDefaultMenuItem(GetHmenu(), id
);
621 // if we're already attached to the menubar, we must update it
622 if ( IsAttached() && GetMenuBar()->IsAttached() )
624 GetMenuBar()->Refresh();
630 void wxMenu::EndRadioGroup()
632 // we're not inside a radio group any longer
633 m_startRadioGroup
= -1;
636 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
638 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
642 if ( item
->GetKind() == wxITEM_RADIO
)
644 int count
= GetMenuItemCount();
646 if ( m_startRadioGroup
== -1 )
648 // start a new radio group
649 m_startRadioGroup
= count
;
651 // for now it has just one element
652 item
->SetAsRadioGroupStart();
653 item
->SetRadioGroupEnd(m_startRadioGroup
);
655 // ensure that we have a checked item in the radio group
658 else // extend the current radio group
660 // we need to update its end item
661 item
->SetRadioGroupStart(m_startRadioGroup
);
662 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
666 node
->GetData()->SetRadioGroupEnd(count
);
670 wxFAIL_MSG( _T("where is the radio group start item?") );
674 else // not a radio item
679 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
686 // check the item initially
693 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
695 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
701 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
703 // we need to find the item's position in the child list
705 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
706 for ( pos
= 0; node
; pos
++ )
708 if ( node
->GetData() == item
)
711 node
= node
->GetNext();
714 // DoRemove() (unlike Remove) can only be called for an existing item!
715 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
718 // remove the corresponding accel from the accel table
719 int n
= FindAccel(item
->GetId());
720 if ( n
!= wxNOT_FOUND
)
724 m_accels
.RemoveAt(n
);
726 //else: this item doesn't have an accel, nothing to do
727 #endif // wxUSE_ACCEL
729 // remove the item from the menu
730 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
732 wxLogLastError(wxT("RemoveMenu"));
735 if ( IsAttached() && GetMenuBar()->IsAttached() )
737 // otherwise, the change won't be visible
738 GetMenuBar()->Refresh();
741 // and from internal data structures
742 return wxMenuBase::DoRemove(item
);
745 // ---------------------------------------------------------------------------
746 // accelerator helpers
747 // ---------------------------------------------------------------------------
751 // create the wxAcceleratorEntries for our accels and put them into the provided
752 // array - return the number of accels we have
753 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
755 size_t count
= GetAccelCount();
756 for ( size_t n
= 0; n
< count
; n
++ )
758 *accels
++ = *m_accels
[n
];
764 wxAcceleratorTable
*wxMenu::CreateAccelTable() const
766 const size_t count
= m_accels
.size();
767 wxScopedArray
<wxAcceleratorEntry
> accels(new wxAcceleratorEntry
[count
]);
768 CopyAccels(accels
.get());
770 return new wxAcceleratorTable(count
, accels
.get());
773 #endif // wxUSE_ACCEL
775 // ---------------------------------------------------------------------------
777 // ---------------------------------------------------------------------------
779 void wxMenu::SetTitle(const wxString
& label
)
781 bool hasNoTitle
= m_title
.empty();
784 HMENU hMenu
= GetHmenu();
788 if ( !label
.empty() )
790 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
791 idMenuTitle
, m_title
.wx_str()) ||
792 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
794 wxLogLastError(wxT("InsertMenu"));
802 // remove the title and the separator after it
803 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
804 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
806 wxLogLastError(wxT("RemoveMenu"));
815 info
.cbSize
= sizeof(info
);
816 info
.fMask
= MIIM_TYPE
;
817 info
.fType
= MFT_STRING
;
818 info
.cch
= m_title
.length();
819 info
.dwTypeData
= const_cast<wxChar
*>(m_title
.wx_str());
820 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
822 wxLogLastError(wxT("SetMenuItemInfo"));
825 if ( !ModifyMenu(hMenu
, 0u,
826 MF_BYPOSITION
| MF_STRING
,
827 idMenuTitle
, m_title
.wx_str()) )
829 wxLogLastError(wxT("ModifyMenu"));
836 // put the title string in bold face
837 if ( !m_title
.empty() )
839 SetDefaultMenuItem(GetHmenu(), idMenuTitle
);
844 // ---------------------------------------------------------------------------
846 // ---------------------------------------------------------------------------
848 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
850 const int id
= (signed short)id_
;
852 // ignore commands from the menu title
853 if ( id
!= (int)idMenuTitle
)
855 // update the check item when it's clicked
856 wxMenuItem
* const item
= FindItem(id
);
857 if ( item
&& item
->IsCheckable() )
860 // get the status of the menu item: note that it has been just changed
861 // by Toggle() above so here we already get the new state of the item
862 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
863 SendEvent(id
, menuState
& MF_CHECKED
);
869 // ---------------------------------------------------------------------------
871 // ---------------------------------------------------------------------------
873 wxWindow
*wxMenu::GetWindow() const
875 if ( m_invokingWindow
!= NULL
)
876 return m_invokingWindow
;
877 else if ( GetMenuBar() != NULL
)
878 return GetMenuBar()->GetFrame();
883 // ---------------------------------------------------------------------------
885 // ---------------------------------------------------------------------------
887 void wxMenuBar::Init()
889 m_eventHandler
= this;
891 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
894 // Not using a combined wxToolBar/wxMenuBar? then use
895 // a commandbar in WinCE .NET just to implement the
897 #if defined(WINCE_WITH_COMMANDBAR)
899 m_adornmentsAdded
= false;
903 wxMenuBar::wxMenuBar()
908 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
913 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
917 m_titles
.Alloc(count
);
919 for ( size_t i
= 0; i
< count
; i
++ )
921 m_menus
.Append(menus
[i
]);
922 m_titles
.Add(titles
[i
]);
924 menus
[i
]->Attach(this);
928 wxMenuBar::~wxMenuBar()
930 // In Windows CE (not .NET), the menubar is always associated
931 // with a toolbar, which destroys the menu implicitly.
932 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
935 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
937 toolMenuBar
->SetMenuBar(NULL
);
940 // we should free Windows resources only if Windows doesn't do it for us
941 // which happens if we're attached to a frame
942 if (m_hMenu
&& !IsAttached())
944 #if defined(WINCE_WITH_COMMANDBAR)
945 ::DestroyWindow((HWND
) m_commandBar
);
946 m_commandBar
= (WXHWND
) NULL
;
948 ::DestroyMenu((HMENU
)m_hMenu
);
950 m_hMenu
= (WXHMENU
)NULL
;
955 // ---------------------------------------------------------------------------
957 // ---------------------------------------------------------------------------
959 void wxMenuBar::Refresh()
964 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
966 #if defined(WINCE_WITHOUT_COMMANDBAR)
969 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
971 #elif defined(WINCE_WITH_COMMANDBAR)
973 DrawMenuBar((HWND
) m_commandBar
);
975 DrawMenuBar(GetHwndOf(GetFrame()));
979 WXHMENU
wxMenuBar::Create()
981 // Note: this doesn't work at all on Smartphone,
982 // since you have to use resources.
983 // We'll have to find another way to add a menu
984 // by changing/adding menu items to an existing menu.
985 #if defined(WINCE_WITHOUT_COMMANDBAR)
989 wxToolMenuBar
* const bar
= static_cast<wxToolMenuBar
*>(GetToolBar());
993 HWND hCommandBar
= GetHwndOf(bar
);
995 // notify comctl32.dll about the version of the headers we use before using
996 // any other TB_XXX messages
997 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
1000 wxZeroMemory(tbButton
);
1001 tbButton
.iBitmap
= I_IMAGENONE
;
1002 tbButton
.fsState
= TBSTATE_ENABLED
;
1003 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
1004 TBSTYLE_NO_DROPDOWN_ARROW
|
1007 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
1009 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
1010 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1011 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
1012 tbButton
.iString
= (int) label
.wx_str();
1014 tbButton
.idCommand
= NewControlId();
1015 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
1017 wxLogLastError(wxT("TB_INSERTBUTTON"));
1021 m_hMenu
= bar
->GetHMenu();
1023 #else // !__WXWINCE__
1027 m_hMenu
= (WXHMENU
)::CreateMenu();
1031 wxLogLastError(wxT("CreateMenu"));
1035 size_t count
= GetMenuCount(), i
;
1036 wxMenuList::iterator it
;
1037 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1039 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
1040 (UINT_PTR
)(*it
)->GetHMenu(),
1041 m_titles
[i
].wx_str()) )
1043 wxLogLastError(wxT("AppendMenu"));
1049 #endif // __WXWINCE__/!__WXWINCE__
1052 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
1055 wxASSERT(menu
->GetHMenu());
1058 #if defined(__WXWINCE__)
1059 int totalMSWItems
= GetMenuCount();
1061 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1064 int i
; // For old C++ compatibility
1065 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1067 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1070 for(i
=0; i
<wxpos
; i
++)
1072 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1079 // ---------------------------------------------------------------------------
1080 // wxMenuBar functions to work with the top level submenus
1081 // ---------------------------------------------------------------------------
1083 // NB: we don't support owner drawn top level items for now, if we do these
1084 // functions would have to be changed to use wxMenuItem as well
1086 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1088 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1089 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1091 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1093 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1098 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1100 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1102 m_titles
[pos
] = label
;
1104 if ( !IsAttached() )
1108 //else: have to modify the existing menu
1110 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1113 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1114 if ( flagsOld
== 0xFFFFFFFF )
1116 wxLogLastError(wxT("GetMenuState"));
1121 if ( flagsOld
& MF_POPUP
)
1123 // HIBYTE contains the number of items in the submenu in this case
1125 id
= (UINT_PTR
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1135 info
.cbSize
= sizeof(info
);
1136 info
.fMask
= MIIM_TYPE
;
1137 info
.fType
= MFT_STRING
;
1138 info
.cch
= label
.length();
1139 info
.dwTypeData
= const_cast<wxChar
*>(label
.wx_str());
1140 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, &info
) )
1142 wxLogLastError(wxT("SetMenuItemInfo"));
1146 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1147 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1149 wxLogLastError(wxT("ModifyMenu"));
1156 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1158 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1159 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1161 return m_titles
[pos
];
1164 // ---------------------------------------------------------------------------
1165 // wxMenuBar construction
1166 // ---------------------------------------------------------------------------
1168 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1170 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1174 m_titles
[pos
] = title
;
1176 #if defined(WINCE_WITHOUT_COMMANDBAR)
1182 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1184 // can't use ModifyMenu() because it deletes the submenu it replaces
1185 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1187 wxLogLastError(wxT("RemoveMenu"));
1190 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1191 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1192 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1194 wxLogLastError(wxT("InsertMenu"));
1198 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1200 // need to rebuild accell table
1201 RebuildAccelTable();
1203 #endif // wxUSE_ACCEL
1212 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1214 // Find out which MSW item before which we'll be inserting before
1215 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1216 // If IsAttached() is false this won't be used anyway
1218 #if defined(WINCE_WITHOUT_COMMANDBAR)
1224 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1225 ? -1 // append the menu
1226 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1228 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1231 m_titles
.Insert(title
, pos
);
1235 #if defined(WINCE_WITHOUT_COMMANDBAR)
1239 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1240 tbButton
.iBitmap
= I_IMAGENONE
;
1241 tbButton
.fsState
= TBSTATE_ENABLED
;
1242 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1244 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1245 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1246 wxString label
= wxStripMenuCodes(title
);
1247 tbButton
.iString
= (int) label
.wx_str();
1249 tbButton
.idCommand
= NewControlId();
1250 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1252 wxLogLastError(wxT("TB_INSERTBUTTON"));
1255 wxUnusedVar(mswpos
);
1257 if ( !::InsertMenu(GetHmenu(), mswpos
,
1258 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1259 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1261 wxLogLastError(wxT("InsertMenu"));
1265 if ( menu
->HasAccels() )
1267 // need to rebuild accell table
1268 RebuildAccelTable();
1270 #endif // wxUSE_ACCEL
1279 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1281 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1282 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1284 if ( !wxMenuBarBase::Append(menu
, title
) )
1287 m_titles
.Add(title
);
1289 #if defined(WINCE_WITHOUT_COMMANDBAR)
1295 #if defined(WINCE_WITHOUT_COMMANDBAR)
1299 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1300 tbButton
.iBitmap
= I_IMAGENONE
;
1301 tbButton
.fsState
= TBSTATE_ENABLED
;
1302 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1304 size_t pos
= GetMenuCount();
1305 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1306 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1307 wxString label
= wxStripMenuCodes(title
);
1308 tbButton
.iString
= (int) label
.wx_str();
1310 tbButton
.idCommand
= NewControlId();
1311 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1313 wxLogLastError(wxT("TB_INSERTBUTTON"));
1317 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1318 (UINT_PTR
)submenu
, title
.wx_str()) )
1320 wxLogLastError(wxT("AppendMenu"));
1325 if ( menu
->HasAccels() )
1327 // need to rebuild accelerator table
1328 RebuildAccelTable();
1330 #endif // wxUSE_ACCEL
1339 wxMenu
*wxMenuBar::Remove(size_t pos
)
1341 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1345 #if defined(WINCE_WITHOUT_COMMANDBAR)
1351 #if defined(WINCE_WITHOUT_COMMANDBAR)
1354 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1356 wxLogLastError(wxT("TB_DELETEBUTTON"));
1360 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1362 wxLogLastError(wxT("RemoveMenu"));
1367 if ( menu
->HasAccels() )
1369 // need to rebuild accell table
1370 RebuildAccelTable();
1372 #endif // wxUSE_ACCEL
1378 m_titles
.RemoveAt(pos
);
1385 void wxMenuBar::RebuildAccelTable()
1387 // merge the accelerators of all menus into one accel table
1388 size_t nAccelCount
= 0;
1389 size_t i
, count
= GetMenuCount();
1390 wxMenuList::iterator it
;
1391 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1393 nAccelCount
+= (*it
)->GetAccelCount();
1398 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1401 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1403 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1406 SetAcceleratorTable(wxAcceleratorTable(nAccelCount
, accelEntries
));
1408 delete [] accelEntries
;
1412 #endif // wxUSE_ACCEL
1414 void wxMenuBar::Attach(wxFrame
*frame
)
1416 wxMenuBarBase::Attach(frame
);
1418 #if defined(WINCE_WITH_COMMANDBAR)
1422 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1427 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1429 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1436 RebuildAccelTable();
1437 #endif // wxUSE_ACCEL
1440 #if defined(WINCE_WITH_COMMANDBAR)
1441 bool wxMenuBar::AddAdornments(long style
)
1443 if (m_adornmentsAdded
|| !m_commandBar
)
1446 if (style
& wxCLOSE_BOX
)
1448 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1449 wxLogLastError(wxT("CommandBar_AddAdornments"));
1457 void wxMenuBar::Detach()
1459 wxMenuBarBase::Detach();
1462 #endif // wxUSE_MENUS