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 int idMenuTitle
= wxID_NONE
;
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"));
109 #endif // !__WXWINCE__
112 // make the given menu item owner-drawn
113 void SetOwnerDrawnMenuItem(HMENU
WXUNUSED_IN_WINCE(hmenu
),
114 UINT
WXUNUSED_IN_WINCE(id
),
115 ULONG_PTR
WXUNUSED_IN_WINCE(data
),
116 BOOL
WXUNUSED_IN_WINCE(byPositon
= FALSE
))
121 mii
.cbSize
= sizeof(MENUITEMINFO
);
122 mii
.fMask
= MIIM_FTYPE
| MIIM_DATA
;
123 mii
.fType
= MFT_OWNERDRAW
;
124 mii
.dwItemData
= data
;
126 if ( reinterpret_cast<wxMenuItem
*>(data
)->IsSeparator() )
127 mii
.fType
|= MFT_SEPARATOR
;
129 if ( !::SetMenuItemInfo(hmenu
, id
, byPositon
, &mii
) )
131 wxLogLastError(wxT("SetMenuItemInfo"));
133 #endif // !__WXWINCE__
137 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
141 info
.cbSize
= sizeof(info
);
142 info
.fMask
= MIIM_STATE
;
143 // MF_BYCOMMAND is zero so test MF_BYPOSITION
144 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
146 wxLogLastError(wxT("GetMenuItemInfo"));
150 #endif // __WXWINCE__
152 inline bool IsGreaterThanStdSize(const wxBitmap
& bmp
)
154 return bmp
.GetWidth() > ::GetSystemMetrics(SM_CXMENUCHECK
) ||
155 bmp
.GetHeight() > ::GetSystemMetrics(SM_CYMENUCHECK
);
158 } // anonymous namespace
160 // ============================================================================
162 // ============================================================================
164 #include "wx/listimpl.cpp"
166 #if wxUSE_EXTENDED_RTTI
168 WX_DEFINE_FLAGS( wxMenuStyle
)
170 wxBEGIN_FLAGS( wxMenuStyle
)
171 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
172 wxEND_FLAGS( wxMenuStyle
)
174 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
176 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
178 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
180 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
183 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
184 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
185 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
186 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
187 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
188 wxEND_PROPERTIES_TABLE()
190 wxBEGIN_HANDLERS_TABLE(wxMenu
)
191 wxEND_HANDLERS_TABLE()
193 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
195 WX_DEFINE_FLAGS( wxMenuBarStyle
)
197 wxBEGIN_FLAGS( wxMenuBarStyle
)
198 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
199 wxEND_FLAGS( wxMenuBarStyle
)
201 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
202 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
207 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
209 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
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
)
221 // ---------------------------------------------------------------------------
222 // wxMenu construction, adding and removing menu items
223 // ---------------------------------------------------------------------------
225 // Construct a menu with optional title (then use append)
229 m_startRadioGroup
= -1;
231 #if wxUSE_OWNER_DRAWN
232 m_ownerDrawn
= false;
233 m_maxBitmapWidth
= 0;
234 m_maxAccelWidth
= -1;
235 #endif // wxUSE_OWNER_DRAWN
238 m_hMenu
= (WXHMENU
)CreatePopupMenu();
241 wxLogLastError(wxT("CreatePopupMenu"));
244 // if we have a title, insert it in the beginning of the menu
245 if ( !m_title
.empty() )
247 const wxString title
= m_title
;
248 m_title
.clear(); // so that SetTitle() knows there was no title before
253 // The wxWindow destructor will take care of deleting the submenus.
256 // we should free Windows resources only if Windows doesn't do it for us
257 // which happens if we're attached to a menubar or a submenu of another
259 if ( !IsAttached() && !GetParent() )
261 if ( !::DestroyMenu(GetHmenu()) )
263 wxLogLastError(wxT("DestroyMenu"));
269 WX_CLEAR_ARRAY(m_accels
);
270 #endif // wxUSE_ACCEL
275 // this will take effect during the next call to Append()
279 void wxMenu::Attach(wxMenuBarBase
*menubar
)
281 wxMenuBase::Attach(menubar
);
288 int wxMenu::FindAccel(int id
) const
290 size_t n
, count
= m_accels
.GetCount();
291 for ( n
= 0; n
< count
; n
++ )
293 if ( m_accels
[n
]->m_command
== id
)
300 void wxMenu::UpdateAccel(wxMenuItem
*item
)
302 if ( item
->IsSubMenu() )
304 wxMenu
*submenu
= item
->GetSubMenu();
305 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
308 UpdateAccel(node
->GetData());
310 node
= node
->GetNext();
313 else if ( !item
->IsSeparator() )
315 // recurse upwards: we should only modify m_accels of the top level
316 // menus, not of the submenus as wxMenuBar doesn't look at them
317 // (alternative and arguable cleaner solution would be to recurse
318 // downwards in GetAccelCount() and CopyAccels())
321 GetParent()->UpdateAccel(item
);
325 // find the (new) accel for this item
326 wxAcceleratorEntry
*accel
= wxAcceleratorEntry::Create(item
->GetItemLabel());
328 accel
->m_command
= item
->GetId();
331 int n
= FindAccel(item
->GetId());
332 if ( n
== wxNOT_FOUND
)
334 // no old, add new if any
338 return; // skipping RebuildAccelTable() below
342 // replace old with new or just remove the old one if no new
347 m_accels
.RemoveAt(n
);
352 GetMenuBar()->RebuildAccelTable();
355 ResetMaxAccelWidth();
357 //else: it is a separator, they can't have accels, nothing to do
360 #endif // wxUSE_ACCEL
365 // helper of DoInsertOrAppend(): returns the HBITMAP to use in MENUITEMINFO
366 HBITMAP
GetHBitmapForMenu(wxMenuItem
*pItem
, bool checked
= true)
368 // Under versions of Windows older than Vista we can't pass HBITMAP
369 // directly as hbmpItem for 2 reasons:
370 // 1. We can't draw it with transparency then (this is not
371 // very important now but would be with themed menu bg)
372 // 2. Worse, Windows inverts the bitmap for the selected
373 // item and this looks downright ugly
375 // So we prefer to instead draw it ourselves in MSWOnDrawItem().by using
376 // HBMMENU_CALLBACK when inserting it
378 // However under Vista using HBMMENU_CALLBACK causes the entire menu to be
379 // drawn using the classic theme instead of the current one and it does
380 // handle transparency just fine so do use the real bitmap there
382 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
384 wxBitmap bmp
= pItem
->GetBitmap(checked
);
387 // we must use PARGB DIB for the menu bitmaps so ensure that we do
388 wxImage
img(bmp
.ConvertToImage());
389 if ( !img
.HasAlpha() )
392 pItem
->SetBitmap(img
, checked
);
395 return GetHbitmapOf(pItem
->GetBitmap(checked
));
397 //else: bitmap is not set
401 #endif // wxUSE_IMAGE
403 return HBMMENU_CALLBACK
;
406 } // anonymous namespace
408 // append a new item or submenu to the menu
409 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
413 #endif // wxUSE_ACCEL
415 // we should support disabling the item even prior to adding it to the menu
416 UINT flags
= pItem
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
;
418 // if "Break" has just been called, insert a menu break before this item
419 // (and don't forget to reset the flag)
421 flags
|= MF_MENUBREAK
;
425 if ( pItem
->IsSeparator() ) {
426 flags
|= MF_SEPARATOR
;
429 // id is the numeric id for normal menu items and HMENU for submenus as
430 // required by ::AppendMenu() API
432 wxMenu
*submenu
= pItem
->GetSubMenu();
433 if ( submenu
!= NULL
) {
434 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
436 submenu
->SetParent(this);
438 id
= (UINT_PTR
)submenu
->GetHMenu();
443 id
= pItem
->GetMSWId();
447 // prepare to insert the item in the menu
448 wxString itemText
= pItem
->GetItemLabel();
449 LPCTSTR pData
= NULL
;
450 if ( pos
== (size_t)-1 )
452 // append at the end (note that the item is already appended to
453 // internal data structures)
454 pos
= GetMenuItemCount() - 1;
457 // adjust position to account for the title, if any
458 if ( !m_title
.empty() )
459 pos
+= 2; // for the title itself and its separator
463 #if wxUSE_OWNER_DRAWN
464 // Under older systems mixing owner-drawn and non-owner-drawn items results
465 // in inconsistent margins, so we force this one to be owner-drawn if any
466 // other items already are.
468 pItem
->SetOwnerDrawn(true);
469 #endif // wxUSE_OWNER_DRAWN
471 // check if we have something more than a simple text item
472 #if wxUSE_OWNER_DRAWN
473 if ( pItem
->IsOwnerDrawn() )
477 if ( !m_ownerDrawn
&& !pItem
->IsSeparator() )
479 // MIIM_BITMAP only works under WinME/2000+ so we always use owner
480 // drawn item under the previous versions and we also have to use
481 // them in any case if the item has custom colours or font
482 static const wxWinVersion winver
= wxGetWinVersion();
483 bool mustUseOwnerDrawn
= winver
< wxWinVersion_98
||
484 pItem
->GetTextColour().Ok() ||
485 pItem
->GetBackgroundColour().Ok() ||
486 pItem
->GetFont().Ok();
488 if ( !mustUseOwnerDrawn
)
490 const wxBitmap
& bmpUnchecked
= pItem
->GetBitmap(false),
491 bmpChecked
= pItem
->GetBitmap(true);
493 if ( (bmpUnchecked
.Ok() && IsGreaterThanStdSize(bmpUnchecked
)) ||
494 (bmpChecked
.Ok() && IsGreaterThanStdSize(bmpChecked
)) )
496 mustUseOwnerDrawn
= true;
500 // use InsertMenuItem() if possible as it's guaranteed to look
501 // correct while our owner-drawn code is not
502 if ( !mustUseOwnerDrawn
)
504 WinStruct
<MENUITEMINFO
> mii
;
505 mii
.fMask
= MIIM_STRING
| MIIM_DATA
;
507 // don't set hbmpItem for the checkable items as it would
508 // be used for both checked and unchecked state
509 if ( pItem
->IsCheckable() )
511 mii
.fMask
|= MIIM_CHECKMARKS
;
512 mii
.hbmpChecked
= GetHBitmapForMenu(pItem
, true);
513 mii
.hbmpUnchecked
= GetHBitmapForMenu(pItem
, false);
515 else if ( pItem
->GetBitmap().IsOk() )
517 mii
.fMask
|= MIIM_BITMAP
;
518 mii
.hbmpItem
= GetHBitmapForMenu(pItem
);
521 mii
.cch
= itemText
.length();
522 mii
.dwTypeData
= const_cast<wxChar
*>(itemText
.wx_str());
524 if ( flags
& MF_POPUP
)
526 mii
.fMask
|= MIIM_SUBMENU
;
527 mii
.hSubMenu
= GetHmenuOf(pItem
->GetSubMenu());
531 mii
.fMask
|= MIIM_ID
;
535 mii
.dwItemData
= reinterpret_cast<ULONG_PTR
>(pItem
);
537 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
540 wxLogLastError(wxT("InsertMenuItem()"));
542 else // InsertMenuItem() ok
544 // we need to remove the extra indent which is reserved for
545 // the checkboxes by default as it looks ugly unless check
546 // boxes are used together with bitmaps and this is not the
548 WinStruct
<MENUINFO
> mi
;
550 // don't call SetMenuInfo() directly, this would prevent
551 // the app from starting up under Windows 95/NT 4
552 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
554 wxDynamicLibrary
dllUser(wxT("user32"));
555 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
556 if ( pfnSetMenuInfo
)
558 mi
.fMask
= MIM_STYLE
;
559 mi
.dwStyle
= MNS_CHECKORBMP
;
560 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
562 wxLogLastError(wxT("SetMenuInfo(MNS_NOCHECK)"));
566 // tell the item that it's not really owner-drawn but only
567 // needs to draw its bitmap, the rest is done by Windows
568 pItem
->SetOwnerDrawn(false);
576 // item draws itself, pass pointer to it in data parameter
577 flags
|= MF_OWNERDRAW
;
578 pData
= (LPCTSTR
)pItem
;
580 bool updateAllMargins
= false;
582 // get size of bitmap always return valid value (0 for invalid bitmap),
583 // so we don't needed check if bitmap is valid ;)
584 int uncheckedW
= pItem
->GetBitmap(false).GetWidth();
585 int checkedW
= pItem
->GetBitmap(true).GetWidth();
587 if ( m_maxBitmapWidth
< uncheckedW
)
589 m_maxBitmapWidth
= uncheckedW
;
590 updateAllMargins
= true;
593 if ( m_maxBitmapWidth
< checkedW
)
595 m_maxBitmapWidth
= checkedW
;
596 updateAllMargins
= true;
599 // make other item ownerdrawn and update margin width for equals alignment
600 if ( !m_ownerDrawn
|| updateAllMargins
)
602 // we must use position in SetOwnerDrawnMenuItem because
603 // all separators have the same id
605 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
608 wxMenuItem
* item
= node
->GetData();
610 if ( !item
->IsOwnerDrawn())
612 item
->SetOwnerDrawn(true);
613 SetOwnerDrawnMenuItem(GetHmenu(), pos
,
614 reinterpret_cast<ULONG_PTR
>(item
), TRUE
);
617 item
->SetMarginWidth(m_maxBitmapWidth
);
619 node
= node
->GetNext();
623 // set menu as ownerdrawn
626 ResetMaxAccelWidth();
628 // only update our margin for equals alignment to other item
629 else if ( !updateAllMargins
)
631 pItem
->SetMarginWidth(m_maxBitmapWidth
);
636 #endif // wxUSE_OWNER_DRAWN
638 // item is just a normal string (passed in data parameter)
642 itemText
= wxMenuItem::GetLabelText(itemText
);
645 pData
= (wxChar
*)itemText
.wx_str();
648 // item might have already been inserted by InsertMenuItem() above
651 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
653 wxLogLastError(wxT("InsertMenu[Item]()"));
660 // if we just appended the title, highlight it
661 if ( id
== (UINT_PTR
)idMenuTitle
)
663 // visually select the menu title
664 SetDefaultMenuItem(GetHmenu(), id
);
667 // if we're already attached to the menubar, we must update it
668 if ( IsAttached() && GetMenuBar()->IsAttached() )
670 GetMenuBar()->Refresh();
676 void wxMenu::EndRadioGroup()
678 // we're not inside a radio group any longer
679 m_startRadioGroup
= -1;
682 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
684 wxCHECK_MSG( item
, NULL
, wxT("NULL item in wxMenu::DoAppend") );
688 if ( item
->GetKind() == wxITEM_RADIO
)
690 int count
= GetMenuItemCount();
692 if ( m_startRadioGroup
== -1 )
694 // start a new radio group
695 m_startRadioGroup
= count
;
697 // for now it has just one element
698 item
->SetAsRadioGroupStart();
699 item
->SetRadioGroupEnd(m_startRadioGroup
);
701 // ensure that we have a checked item in the radio group
704 else // extend the current radio group
706 // we need to update its end item
707 item
->SetRadioGroupStart(m_startRadioGroup
);
708 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
712 node
->GetData()->SetRadioGroupEnd(count
);
716 wxFAIL_MSG( wxT("where is the radio group start item?") );
720 else // not a radio item
725 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
732 // check the item initially
739 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
741 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
747 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
749 // we need to find the item's position in the child list
751 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
752 for ( pos
= 0; node
; pos
++ )
754 if ( node
->GetData() == item
)
757 node
= node
->GetNext();
760 // DoRemove() (unlike Remove) can only be called for an existing item!
761 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
764 // remove the corresponding accel from the accel table
765 int n
= FindAccel(item
->GetId());
766 if ( n
!= wxNOT_FOUND
)
770 m_accels
.RemoveAt(n
);
772 ResetMaxAccelWidth();
774 //else: this item doesn't have an accel, nothing to do
775 #endif // wxUSE_ACCEL
777 // remove the item from the menu
778 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
780 wxLogLastError(wxT("RemoveMenu"));
783 if ( IsAttached() && GetMenuBar()->IsAttached() )
785 // otherwise, the change won't be visible
786 GetMenuBar()->Refresh();
789 // and from internal data structures
790 return wxMenuBase::DoRemove(item
);
793 // ---------------------------------------------------------------------------
794 // accelerator helpers
795 // ---------------------------------------------------------------------------
799 // create the wxAcceleratorEntries for our accels and put them into the provided
800 // array - return the number of accels we have
801 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
803 size_t count
= GetAccelCount();
804 for ( size_t n
= 0; n
< count
; n
++ )
806 *accels
++ = *m_accels
[n
];
812 wxAcceleratorTable
*wxMenu::CreateAccelTable() const
814 const size_t count
= m_accels
.size();
815 wxScopedArray
<wxAcceleratorEntry
> accels(new wxAcceleratorEntry
[count
]);
816 CopyAccels(accels
.get());
818 return new wxAcceleratorTable(count
, accels
.get());
821 #endif // wxUSE_ACCEL
823 // ---------------------------------------------------------------------------
824 // ownerdrawn helpers
825 // ---------------------------------------------------------------------------
827 #if wxUSE_OWNER_DRAWN
829 void wxMenu::CalculateMaxAccelWidth()
831 wxASSERT_MSG( m_maxAccelWidth
== -1, wxT("it's really needed?") );
833 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
836 wxMenuItem
* item
= node
->GetData();
838 if ( item
->IsOwnerDrawn() )
840 int width
= item
->MeasureAccelWidth();
841 if (width
> m_maxAccelWidth
)
842 m_maxAccelWidth
= width
;
845 node
= node
->GetNext();
849 #endif // wxUSE_OWNER_DRAWN
851 // ---------------------------------------------------------------------------
853 // ---------------------------------------------------------------------------
855 void wxMenu::SetTitle(const wxString
& label
)
857 bool hasNoTitle
= m_title
.empty();
860 HMENU hMenu
= GetHmenu();
864 if ( !label
.empty() )
866 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
867 (UINT_PTR
)idMenuTitle
, m_title
.wx_str()) ||
868 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
870 wxLogLastError(wxT("InsertMenu"));
878 // remove the title and the separator after it
879 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
880 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
882 wxLogLastError(wxT("RemoveMenu"));
891 info
.cbSize
= sizeof(info
);
892 info
.fMask
= MIIM_TYPE
;
893 info
.fType
= MFT_STRING
;
894 info
.cch
= m_title
.length();
895 info
.dwTypeData
= const_cast<wxChar
*>(m_title
.wx_str());
896 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
898 wxLogLastError(wxT("SetMenuItemInfo"));
901 if ( !ModifyMenu(hMenu
, 0u,
902 MF_BYPOSITION
| MF_STRING
,
903 (UINT_PTR
)idMenuTitle
, m_title
.wx_str()) )
905 wxLogLastError(wxT("ModifyMenu"));
912 // put the title string in bold face
913 if ( !m_title
.empty() )
915 SetDefaultMenuItem(GetHmenu(), (UINT_PTR
)idMenuTitle
);
920 // ---------------------------------------------------------------------------
922 // ---------------------------------------------------------------------------
924 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
926 const int id
= (signed short)id_
;
928 // ignore commands from the menu title
929 if ( id
!= idMenuTitle
)
931 // update the check item when it's clicked
932 wxMenuItem
* const item
= FindItem(id
);
933 if ( item
&& item
->IsCheckable() )
936 // get the status of the menu item: note that it has been just changed
937 // by Toggle() above so here we already get the new state of the item
938 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
939 SendEvent(id
, menuState
& MF_CHECKED
);
945 // ---------------------------------------------------------------------------
947 // ---------------------------------------------------------------------------
949 void wxMenuBar::Init()
951 m_eventHandler
= this;
953 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
956 // Not using a combined wxToolBar/wxMenuBar? then use
957 // a commandbar in WinCE .NET just to implement the
959 #if defined(WINCE_WITH_COMMANDBAR)
961 m_adornmentsAdded
= false;
965 wxMenuBar::wxMenuBar()
970 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
975 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
979 for ( size_t i
= 0; i
< count
; i
++ )
981 // We just want to store the menu title in the menu itself, not to
982 // show it as a dummy item in the menu itself as we do with the popup
983 // menu titles in overridden wxMenu::SetTitle().
984 menus
[i
]->wxMenuBase::SetTitle(titles
[i
]);
985 m_menus
.Append(menus
[i
]);
987 menus
[i
]->Attach(this);
991 wxMenuBar::~wxMenuBar()
993 // In Windows CE (not .NET), the menubar is always associated
994 // with a toolbar, which destroys the menu implicitly.
995 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
998 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
1000 toolMenuBar
->SetMenuBar(NULL
);
1003 // we should free Windows resources only if Windows doesn't do it for us
1004 // which happens if we're attached to a frame
1005 if (m_hMenu
&& !IsAttached())
1007 #if defined(WINCE_WITH_COMMANDBAR)
1008 ::DestroyWindow((HWND
) m_commandBar
);
1009 m_commandBar
= (WXHWND
) NULL
;
1011 ::DestroyMenu((HMENU
)m_hMenu
);
1013 m_hMenu
= (WXHMENU
)NULL
;
1018 // ---------------------------------------------------------------------------
1019 // wxMenuBar helpers
1020 // ---------------------------------------------------------------------------
1022 void wxMenuBar::Refresh()
1027 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
1029 #if defined(WINCE_WITHOUT_COMMANDBAR)
1032 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
1034 #elif defined(WINCE_WITH_COMMANDBAR)
1036 DrawMenuBar((HWND
) m_commandBar
);
1038 DrawMenuBar(GetHwndOf(GetFrame()));
1042 WXHMENU
wxMenuBar::Create()
1044 // Note: this doesn't work at all on Smartphone,
1045 // since you have to use resources.
1046 // We'll have to find another way to add a menu
1047 // by changing/adding menu items to an existing menu.
1048 #if defined(WINCE_WITHOUT_COMMANDBAR)
1052 wxToolMenuBar
* const bar
= static_cast<wxToolMenuBar
*>(GetToolBar());
1056 HWND hCommandBar
= GetHwndOf(bar
);
1058 // notify comctl32.dll about the version of the headers we use before using
1059 // any other TB_XXX messages
1060 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
1063 wxZeroMemory(tbButton
);
1064 tbButton
.iBitmap
= I_IMAGENONE
;
1065 tbButton
.fsState
= TBSTATE_ENABLED
;
1066 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
1067 TBSTYLE_NO_DROPDOWN_ARROW
|
1070 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
1072 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
1073 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1074 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
1075 tbButton
.iString
= (int) label
.wx_str();
1077 tbButton
.idCommand
= NewControlId();
1078 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
1080 wxLogLastError(wxT("TB_INSERTBUTTON"));
1084 m_hMenu
= bar
->GetHMenu();
1086 #else // !__WXWINCE__
1090 m_hMenu
= (WXHMENU
)::CreateMenu();
1094 wxLogLastError(wxT("CreateMenu"));
1098 for ( wxMenuList::iterator it
= m_menus
.begin();
1099 it
!= m_menus
.end();
1102 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
1103 (UINT_PTR
)(*it
)->GetHMenu(),
1104 (*it
)->GetTitle().wx_str()) )
1106 wxLogLastError(wxT("AppendMenu"));
1112 #endif // __WXWINCE__/!__WXWINCE__
1115 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
1118 wxASSERT(menu
->GetHMenu());
1121 #if defined(__WXWINCE__)
1122 int totalMSWItems
= GetMenuCount();
1124 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1127 int i
; // For old C++ compatibility
1128 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1130 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1133 for(i
=0; i
<wxpos
; i
++)
1135 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1142 // ---------------------------------------------------------------------------
1143 // wxMenuBar functions to work with the top level submenus
1144 // ---------------------------------------------------------------------------
1146 // NB: we don't support owner drawn top level items for now, if we do these
1147 // functions would have to be changed to use wxMenuItem as well
1149 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1151 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1152 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1154 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1156 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1161 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1163 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1165 m_menus
[pos
]->wxMenuBase::SetTitle(label
);
1167 if ( !IsAttached() )
1171 //else: have to modify the existing menu
1173 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1176 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1177 if ( flagsOld
== 0xFFFFFFFF )
1179 wxLogLastError(wxT("GetMenuState"));
1184 if ( flagsOld
& MF_POPUP
)
1186 // HIBYTE contains the number of items in the submenu in this case
1188 id
= (UINT_PTR
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1198 info
.cbSize
= sizeof(info
);
1199 info
.fMask
= MIIM_TYPE
;
1200 info
.fType
= MFT_STRING
;
1201 info
.cch
= label
.length();
1202 info
.dwTypeData
= const_cast<wxChar
*>(label
.wx_str());
1203 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, &info
) )
1205 wxLogLastError(wxT("SetMenuItemInfo"));
1209 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1210 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1212 wxLogLastError(wxT("ModifyMenu"));
1219 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1221 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1222 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1224 return m_menus
[pos
]->GetTitle();
1227 // ---------------------------------------------------------------------------
1228 // wxMenuBar construction
1229 // ---------------------------------------------------------------------------
1231 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1233 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1237 menu
->wxMenuBase::SetTitle(title
);
1239 #if defined(WINCE_WITHOUT_COMMANDBAR)
1245 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1247 // can't use ModifyMenu() because it deletes the submenu it replaces
1248 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1250 wxLogLastError(wxT("RemoveMenu"));
1253 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1254 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1255 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1257 wxLogLastError(wxT("InsertMenu"));
1261 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1263 // need to rebuild accell table
1264 RebuildAccelTable();
1266 #endif // wxUSE_ACCEL
1275 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1277 // Find out which MSW item before which we'll be inserting before
1278 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1279 // If IsAttached() is false this won't be used anyway
1281 #if defined(WINCE_WITHOUT_COMMANDBAR)
1287 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1288 ? -1 // append the menu
1289 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1291 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1294 menu
->wxMenuBase::SetTitle(title
);
1298 #if defined(WINCE_WITHOUT_COMMANDBAR)
1302 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1303 tbButton
.iBitmap
= I_IMAGENONE
;
1304 tbButton
.fsState
= TBSTATE_ENABLED
;
1305 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1307 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1308 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1309 wxString label
= wxStripMenuCodes(title
);
1310 tbButton
.iString
= (int) label
.wx_str();
1312 tbButton
.idCommand
= NewControlId();
1313 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1315 wxLogLastError(wxT("TB_INSERTBUTTON"));
1318 wxUnusedVar(mswpos
);
1320 if ( !::InsertMenu(GetHmenu(), mswpos
,
1321 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1322 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1324 wxLogLastError(wxT("InsertMenu"));
1328 if ( menu
->HasAccels() )
1330 // need to rebuild accell table
1331 RebuildAccelTable();
1333 #endif // wxUSE_ACCEL
1342 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1344 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1345 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1347 if ( !wxMenuBarBase::Append(menu
, title
) )
1350 menu
->wxMenuBase::SetTitle(title
);
1352 #if defined(WINCE_WITHOUT_COMMANDBAR)
1358 #if defined(WINCE_WITHOUT_COMMANDBAR)
1362 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1363 tbButton
.iBitmap
= I_IMAGENONE
;
1364 tbButton
.fsState
= TBSTATE_ENABLED
;
1365 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1367 size_t pos
= GetMenuCount();
1368 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1369 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1370 wxString label
= wxStripMenuCodes(title
);
1371 tbButton
.iString
= (int) label
.wx_str();
1373 tbButton
.idCommand
= NewControlId();
1374 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1376 wxLogLastError(wxT("TB_INSERTBUTTON"));
1380 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1381 (UINT_PTR
)submenu
, title
.wx_str()) )
1383 wxLogLastError(wxT("AppendMenu"));
1388 if ( menu
->HasAccels() )
1390 // need to rebuild accelerator table
1391 RebuildAccelTable();
1393 #endif // wxUSE_ACCEL
1402 wxMenu
*wxMenuBar::Remove(size_t pos
)
1404 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1408 #if defined(WINCE_WITHOUT_COMMANDBAR)
1414 #if defined(WINCE_WITHOUT_COMMANDBAR)
1417 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1419 wxLogLastError(wxT("TB_DELETEBUTTON"));
1423 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1425 wxLogLastError(wxT("RemoveMenu"));
1430 if ( menu
->HasAccels() )
1432 // need to rebuild accell table
1433 RebuildAccelTable();
1435 #endif // wxUSE_ACCEL
1446 void wxMenuBar::RebuildAccelTable()
1448 // merge the accelerators of all menus into one accel table
1449 size_t nAccelCount
= 0;
1450 size_t i
, count
= GetMenuCount();
1451 wxMenuList::iterator it
;
1452 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1454 nAccelCount
+= (*it
)->GetAccelCount();
1459 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1462 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1464 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1467 SetAcceleratorTable(wxAcceleratorTable(nAccelCount
, accelEntries
));
1469 delete [] accelEntries
;
1473 #endif // wxUSE_ACCEL
1475 void wxMenuBar::Attach(wxFrame
*frame
)
1477 wxMenuBarBase::Attach(frame
);
1479 #if defined(WINCE_WITH_COMMANDBAR)
1483 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1488 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1490 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1497 RebuildAccelTable();
1498 #endif // wxUSE_ACCEL
1501 #if defined(WINCE_WITH_COMMANDBAR)
1502 bool wxMenuBar::AddAdornments(long style
)
1504 if (m_adornmentsAdded
|| !m_commandBar
)
1507 if (style
& wxCLOSE_BOX
)
1509 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1511 wxLogLastError(wxT("CommandBar_AddAdornments"));
1522 void wxMenuBar::Detach()
1524 wxMenuBarBase::Detach();
1527 #endif // wxUSE_MENUS