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"));
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 IsLessThanStdSize(const wxBitmap
& bmp
)
154 // FIXME: these +4 are chosen so that 16*16 bitmaps pass this test with
155 // default SM_CXMENUCHECK value but I have no idea what do we really
157 return bmp
.GetWidth() < ::GetSystemMetrics(SM_CXMENUCHECK
) + 4 &&
158 bmp
.GetHeight() < ::GetSystemMetrics(SM_CYMENUCHECK
) + 4;
161 } // anonymous namespace
163 // ============================================================================
165 // ============================================================================
167 #include "wx/listimpl.cpp"
169 WX_DEFINE_LIST( wxMenuInfoList
)
171 #if wxUSE_EXTENDED_RTTI
173 WX_DEFINE_FLAGS( wxMenuStyle
)
175 wxBEGIN_FLAGS( wxMenuStyle
)
176 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
177 wxEND_FLAGS( wxMenuStyle
)
179 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
181 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
183 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
185 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
188 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
189 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
190 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
191 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
192 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
193 wxEND_PROPERTIES_TABLE()
195 wxBEGIN_HANDLERS_TABLE(wxMenu
)
196 wxEND_HANDLERS_TABLE()
198 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
200 WX_DEFINE_FLAGS( wxMenuBarStyle
)
202 wxBEGIN_FLAGS( wxMenuBarStyle
)
203 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
204 wxEND_FLAGS( wxMenuBarStyle
)
206 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
207 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
212 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
214 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
216 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
217 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
218 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
219 wxEND_PROPERTIES_TABLE()
221 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
222 wxEND_HANDLERS_TABLE()
224 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
226 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
228 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
230 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
233 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
234 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
235 wxEND_PROPERTIES_TABLE()
237 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
238 wxEND_HANDLERS_TABLE()
240 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
243 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
244 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
245 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
248 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
250 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
251 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
252 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
254 wxMenuInfo
* info
= new wxMenuInfo() ;
255 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetMenuLabel(i
) ) ;
256 list
->Append( info
) ;
261 // ---------------------------------------------------------------------------
262 // wxMenu construction, adding and removing menu items
263 // ---------------------------------------------------------------------------
265 // Construct a menu with optional title (then use append)
269 m_startRadioGroup
= -1;
271 #if wxUSE_OWNER_DRAWN
272 m_ownerDrawn
= false;
273 m_maxBitmapWidth
= 0;
274 #endif // wxUSE_OWNER_DRAWN
277 m_hMenu
= (WXHMENU
)CreatePopupMenu();
280 wxLogLastError(wxT("CreatePopupMenu"));
283 // if we have a title, insert it in the beginning of the menu
284 if ( !m_title
.empty() )
286 Append(idMenuTitle
, m_title
);
291 // The wxWindow destructor will take care of deleting the submenus.
294 // we should free Windows resources only if Windows doesn't do it for us
295 // which happens if we're attached to a menubar or a submenu of another
297 if ( !IsAttached() && !GetParent() )
299 if ( !::DestroyMenu(GetHmenu()) )
301 wxLogLastError(wxT("DestroyMenu"));
307 WX_CLEAR_ARRAY(m_accels
);
308 #endif // wxUSE_ACCEL
313 // this will take effect during the next call to Append()
317 void wxMenu::Attach(wxMenuBarBase
*menubar
)
319 wxMenuBase::Attach(menubar
);
326 int wxMenu::FindAccel(int id
) const
328 size_t n
, count
= m_accels
.GetCount();
329 for ( n
= 0; n
< count
; n
++ )
331 if ( m_accels
[n
]->m_command
== id
)
338 void wxMenu::UpdateAccel(wxMenuItem
*item
)
340 if ( item
->IsSubMenu() )
342 wxMenu
*submenu
= item
->GetSubMenu();
343 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
346 UpdateAccel(node
->GetData());
348 node
= node
->GetNext();
351 else if ( !item
->IsSeparator() )
353 // recurse upwards: we should only modify m_accels of the top level
354 // menus, not of the submenus as wxMenuBar doesn't look at them
355 // (alternative and arguable cleaner solution would be to recurse
356 // downwards in GetAccelCount() and CopyAccels())
359 GetParent()->UpdateAccel(item
);
363 // find the (new) accel for this item
364 wxAcceleratorEntry
*accel
= wxAcceleratorEntry::Create(item
->GetItemLabel());
366 accel
->m_command
= item
->GetId();
369 int n
= FindAccel(item
->GetId());
370 if ( n
== wxNOT_FOUND
)
372 // no old, add new if any
376 return; // skipping RebuildAccelTable() below
380 // replace old with new or just remove the old one if no new
385 m_accels
.RemoveAt(n
);
390 GetMenuBar()->RebuildAccelTable();
393 //else: it is a separator, they can't have accels, nothing to do
396 #endif // wxUSE_ACCEL
401 // helper of DoInsertOrAppend(): returns the HBITMAP to use in MENUITEMINFO
402 HBITMAP
GetHBitmapForMenu(wxMenuItem
*pItem
, bool checked
= true)
404 // Under versions of Windows older than Vista we can't pass HBITMAP
405 // directly as hbmpItem for 2 reasons:
406 // 1. We can't draw it with transparency then (this is not
407 // very important now but would be with themed menu bg)
408 // 2. Worse, Windows inverts the bitmap for the selected
409 // item and this looks downright ugly
411 // So we prefer to instead draw it ourselves in MSWOnDrawItem().by using
412 // HBMMENU_CALLBACK when inserting it
414 // However under Vista using HBMMENU_CALLBACK causes the entire menu to be
415 // drawn using the classic theme instead of the current one and it does
416 // handle transparency just fine so do use the real bitmap there
418 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
420 wxBitmap bmp
= pItem
->GetBitmap(checked
);
423 // we must use PARGB DIB for the menu bitmaps so ensure that we do
424 wxImage
img(bmp
.ConvertToImage());
425 if ( !img
.HasAlpha() )
428 pItem
->SetBitmap(img
, checked
);
431 return GetHbitmapOf(pItem
->GetBitmap(checked
));
433 //else: bitmap is not set
437 #endif // wxUSE_IMAGE
439 return HBMMENU_CALLBACK
;
442 } // anonymous namespace
444 // append a new item or submenu to the menu
445 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
449 #endif // wxUSE_ACCEL
451 // we should support disabling the item even prior to adding it to the menu
452 UINT flags
= pItem
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
;
454 // if "Break" has just been called, insert a menu break before this item
455 // (and don't forget to reset the flag)
457 flags
|= MF_MENUBREAK
;
461 if ( pItem
->IsSeparator() ) {
462 flags
|= MF_SEPARATOR
;
465 // id is the numeric id for normal menu items and HMENU for submenus as
466 // required by ::AppendMenu() API
468 wxMenu
*submenu
= pItem
->GetSubMenu();
469 if ( submenu
!= NULL
) {
470 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
472 submenu
->SetParent(this);
474 id
= (UINT_PTR
)submenu
->GetHMenu();
479 id
= pItem
->GetMSWId();
483 // prepare to insert the item in the menu
484 wxString itemText
= pItem
->GetItemLabel();
485 LPCTSTR pData
= NULL
;
486 if ( pos
== (size_t)-1 )
488 // append at the end (note that the item is already appended to
489 // internal data structures)
490 pos
= GetMenuItemCount() - 1;
493 // adjust position to account for the title, if any
494 if ( !m_title
.empty() )
495 pos
+= 2; // for the title itself and its separator
499 #if wxUSE_OWNER_DRAWN
500 // Under older systems mixing owner-drawn and non-owner-drawn items results
501 // in inconsistent margins, so we force this one to be owner-drawn if any
502 // other items already are.
504 pItem
->SetOwnerDrawn(true);
505 #endif // wxUSE_OWNER_DRAWN
507 // check if we have something more than a simple text item
508 #if wxUSE_OWNER_DRAWN
509 if ( pItem
->IsOwnerDrawn() )
513 if ( !m_ownerDrawn
&& !pItem
->IsSeparator() )
515 // MIIM_BITMAP only works under WinME/2000+ so we always use owner
516 // drawn item under the previous versions and we also have to use
517 // them in any case if the item has custom colours or font
518 static const wxWinVersion winver
= wxGetWinVersion();
519 bool mustUseOwnerDrawn
= winver
< wxWinVersion_98
||
520 pItem
->GetTextColour().Ok() ||
521 pItem
->GetBackgroundColour().Ok() ||
522 pItem
->GetFont().Ok();
524 if ( !mustUseOwnerDrawn
)
526 const wxBitmap
& bmpUnchecked
= pItem
->GetBitmap(false),
527 bmpChecked
= pItem
->GetBitmap(true);
529 if ( (bmpUnchecked
.Ok() && !IsLessThanStdSize(bmpUnchecked
)) ||
530 (bmpChecked
.Ok() && !IsLessThanStdSize(bmpChecked
)) )
532 mustUseOwnerDrawn
= true;
536 // use InsertMenuItem() if possible as it's guaranteed to look
537 // correct while our owner-drawn code is not
538 if ( !mustUseOwnerDrawn
)
540 WinStruct
<MENUITEMINFO
> mii
;
541 mii
.fMask
= MIIM_STRING
| MIIM_DATA
;
543 // don't set hbmpItem for the checkable items as it would
544 // be used for both checked and unchecked state
545 if ( pItem
->IsCheckable() )
547 mii
.fMask
|= MIIM_CHECKMARKS
;
548 mii
.hbmpChecked
= GetHBitmapForMenu(pItem
, true);
549 mii
.hbmpUnchecked
= GetHBitmapForMenu(pItem
, false);
551 else if ( pItem
->GetBitmap().IsOk() )
553 mii
.fMask
|= MIIM_BITMAP
;
554 mii
.hbmpItem
= GetHBitmapForMenu(pItem
);
557 mii
.cch
= itemText
.length();
558 mii
.dwTypeData
= const_cast<wxChar
*>(itemText
.wx_str());
560 if ( flags
& MF_POPUP
)
562 mii
.fMask
|= MIIM_SUBMENU
;
563 mii
.hSubMenu
= GetHmenuOf(pItem
->GetSubMenu());
567 mii
.fMask
|= MIIM_ID
;
571 mii
.dwItemData
= reinterpret_cast<ULONG_PTR
>(pItem
);
573 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
576 wxLogLastError(wxT("InsertMenuItem()"));
578 else // InsertMenuItem() ok
580 // we need to remove the extra indent which is reserved for
581 // the checkboxes by default as it looks ugly unless check
582 // boxes are used together with bitmaps and this is not the
584 WinStruct
<MENUINFO
> mi
;
586 // don't call SetMenuInfo() directly, this would prevent
587 // the app from starting up under Windows 95/NT 4
588 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
590 wxDynamicLibrary
dllUser(wxT("user32"));
591 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
592 if ( pfnSetMenuInfo
)
594 mi
.fMask
= MIM_STYLE
;
595 mi
.dwStyle
= MNS_CHECKORBMP
;
596 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
598 wxLogLastError(wxT("SetMenuInfo(MNS_NOCHECK)"));
602 // tell the item that it's not really owner-drawn but only
603 // needs to draw its bitmap, the rest is done by Windows
604 pItem
->SetOwnerDrawn(false);
612 // item draws itself, pass pointer to it in data parameter
613 flags
|= MF_OWNERDRAW
;
614 pData
= (LPCTSTR
)pItem
;
616 bool updateAllMargins
= false;
618 // get size of bitmap always return valid value (0 for invalid bitmap),
619 // so we don't needed check if bitmap is valid ;)
620 int uncheckedW
= pItem
->GetBitmap(false).GetWidth();
621 int checkedW
= pItem
->GetBitmap(true).GetWidth();
623 if ( m_maxBitmapWidth
< uncheckedW
)
625 m_maxBitmapWidth
= uncheckedW
;
626 updateAllMargins
= true;
629 if ( m_maxBitmapWidth
< checkedW
)
631 m_maxBitmapWidth
= checkedW
;
632 updateAllMargins
= true;
635 // make other item ownerdrawn and update margin width for equals alignment
636 if ( !m_ownerDrawn
|| updateAllMargins
)
638 // we must use position in SetOwnerDrawnMenuItem because
639 // all separators have the same id
641 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
644 wxMenuItem
* item
= node
->GetData();
646 if ( !item
->IsOwnerDrawn())
648 item
->SetOwnerDrawn(true);
649 SetOwnerDrawnMenuItem(GetHmenu(), pos
,
650 reinterpret_cast<ULONG_PTR
>(item
), TRUE
);
653 item
->SetMarginWidth(m_maxBitmapWidth
);
655 node
= node
->GetNext();
659 // set menu as ownerdrawn
662 // only update our margin for equals alignment to other item
663 else if ( !updateAllMargins
)
665 pItem
->SetMarginWidth(m_maxBitmapWidth
);
670 #endif // wxUSE_OWNER_DRAWN
672 // item is just a normal string (passed in data parameter)
676 itemText
= wxMenuItem::GetLabelText(itemText
);
679 pData
= (wxChar
*)itemText
.wx_str();
682 // item might have already been inserted by InsertMenuItem() above
685 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
687 wxLogLastError(wxT("InsertMenu[Item]()"));
694 // if we just appended the title, highlight it
695 if ( id
== idMenuTitle
)
697 // visually select the menu title
698 SetDefaultMenuItem(GetHmenu(), id
);
701 // if we're already attached to the menubar, we must update it
702 if ( IsAttached() && GetMenuBar()->IsAttached() )
704 GetMenuBar()->Refresh();
710 void wxMenu::EndRadioGroup()
712 // we're not inside a radio group any longer
713 m_startRadioGroup
= -1;
716 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
718 wxCHECK_MSG( item
, NULL
, wxT("NULL item in wxMenu::DoAppend") );
722 if ( item
->GetKind() == wxITEM_RADIO
)
724 int count
= GetMenuItemCount();
726 if ( m_startRadioGroup
== -1 )
728 // start a new radio group
729 m_startRadioGroup
= count
;
731 // for now it has just one element
732 item
->SetAsRadioGroupStart();
733 item
->SetRadioGroupEnd(m_startRadioGroup
);
735 // ensure that we have a checked item in the radio group
738 else // extend the current radio group
740 // we need to update its end item
741 item
->SetRadioGroupStart(m_startRadioGroup
);
742 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
746 node
->GetData()->SetRadioGroupEnd(count
);
750 wxFAIL_MSG( wxT("where is the radio group start item?") );
754 else // not a radio item
759 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
766 // check the item initially
773 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
775 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
781 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
783 // we need to find the item's position in the child list
785 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
786 for ( pos
= 0; node
; pos
++ )
788 if ( node
->GetData() == item
)
791 node
= node
->GetNext();
794 // DoRemove() (unlike Remove) can only be called for an existing item!
795 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
798 // remove the corresponding accel from the accel table
799 int n
= FindAccel(item
->GetId());
800 if ( n
!= wxNOT_FOUND
)
804 m_accels
.RemoveAt(n
);
806 //else: this item doesn't have an accel, nothing to do
807 #endif // wxUSE_ACCEL
809 // remove the item from the menu
810 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
812 wxLogLastError(wxT("RemoveMenu"));
815 if ( IsAttached() && GetMenuBar()->IsAttached() )
817 // otherwise, the change won't be visible
818 GetMenuBar()->Refresh();
821 // and from internal data structures
822 return wxMenuBase::DoRemove(item
);
825 // ---------------------------------------------------------------------------
826 // accelerator helpers
827 // ---------------------------------------------------------------------------
831 // create the wxAcceleratorEntries for our accels and put them into the provided
832 // array - return the number of accels we have
833 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
835 size_t count
= GetAccelCount();
836 for ( size_t n
= 0; n
< count
; n
++ )
838 *accels
++ = *m_accels
[n
];
844 wxAcceleratorTable
*wxMenu::CreateAccelTable() const
846 const size_t count
= m_accels
.size();
847 wxScopedArray
<wxAcceleratorEntry
> accels(new wxAcceleratorEntry
[count
]);
848 CopyAccels(accels
.get());
850 return new wxAcceleratorTable(count
, accels
.get());
853 #endif // wxUSE_ACCEL
855 // ---------------------------------------------------------------------------
857 // ---------------------------------------------------------------------------
859 void wxMenu::SetTitle(const wxString
& label
)
861 bool hasNoTitle
= m_title
.empty();
864 HMENU hMenu
= GetHmenu();
868 if ( !label
.empty() )
870 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
871 idMenuTitle
, m_title
.wx_str()) ||
872 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
874 wxLogLastError(wxT("InsertMenu"));
882 // remove the title and the separator after it
883 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
884 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
886 wxLogLastError(wxT("RemoveMenu"));
895 info
.cbSize
= sizeof(info
);
896 info
.fMask
= MIIM_TYPE
;
897 info
.fType
= MFT_STRING
;
898 info
.cch
= m_title
.length();
899 info
.dwTypeData
= const_cast<wxChar
*>(m_title
.wx_str());
900 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
902 wxLogLastError(wxT("SetMenuItemInfo"));
905 if ( !ModifyMenu(hMenu
, 0u,
906 MF_BYPOSITION
| MF_STRING
,
907 idMenuTitle
, m_title
.wx_str()) )
909 wxLogLastError(wxT("ModifyMenu"));
916 // put the title string in bold face
917 if ( !m_title
.empty() )
919 SetDefaultMenuItem(GetHmenu(), idMenuTitle
);
924 // ---------------------------------------------------------------------------
926 // ---------------------------------------------------------------------------
928 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
930 const int id
= (signed short)id_
;
932 // ignore commands from the menu title
933 if ( id
!= (int)idMenuTitle
)
935 // update the check item when it's clicked
936 wxMenuItem
* const item
= FindItem(id
);
937 if ( item
&& item
->IsCheckable() )
940 // get the status of the menu item: note that it has been just changed
941 // by Toggle() above so here we already get the new state of the item
942 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
943 SendEvent(id
, menuState
& MF_CHECKED
);
949 // ---------------------------------------------------------------------------
951 // ---------------------------------------------------------------------------
953 wxWindow
*wxMenu::GetWindow() const
955 if ( m_invokingWindow
!= NULL
)
956 return m_invokingWindow
;
957 else if ( GetMenuBar() != NULL
)
958 return GetMenuBar()->GetFrame();
963 // ---------------------------------------------------------------------------
965 // ---------------------------------------------------------------------------
967 void wxMenuBar::Init()
969 m_eventHandler
= this;
971 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
974 // Not using a combined wxToolBar/wxMenuBar? then use
975 // a commandbar in WinCE .NET just to implement the
977 #if defined(WINCE_WITH_COMMANDBAR)
979 m_adornmentsAdded
= false;
983 wxMenuBar::wxMenuBar()
988 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
993 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
997 m_titles
.Alloc(count
);
999 for ( size_t i
= 0; i
< count
; i
++ )
1001 m_menus
.Append(menus
[i
]);
1002 m_titles
.Add(titles
[i
]);
1004 menus
[i
]->Attach(this);
1008 wxMenuBar::~wxMenuBar()
1010 // In Windows CE (not .NET), the menubar is always associated
1011 // with a toolbar, which destroys the menu implicitly.
1012 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
1015 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
1017 toolMenuBar
->SetMenuBar(NULL
);
1020 // we should free Windows resources only if Windows doesn't do it for us
1021 // which happens if we're attached to a frame
1022 if (m_hMenu
&& !IsAttached())
1024 #if defined(WINCE_WITH_COMMANDBAR)
1025 ::DestroyWindow((HWND
) m_commandBar
);
1026 m_commandBar
= (WXHWND
) NULL
;
1028 ::DestroyMenu((HMENU
)m_hMenu
);
1030 m_hMenu
= (WXHMENU
)NULL
;
1035 // ---------------------------------------------------------------------------
1036 // wxMenuBar helpers
1037 // ---------------------------------------------------------------------------
1039 void wxMenuBar::Refresh()
1044 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
1046 #if defined(WINCE_WITHOUT_COMMANDBAR)
1049 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
1051 #elif defined(WINCE_WITH_COMMANDBAR)
1053 DrawMenuBar((HWND
) m_commandBar
);
1055 DrawMenuBar(GetHwndOf(GetFrame()));
1059 WXHMENU
wxMenuBar::Create()
1061 // Note: this doesn't work at all on Smartphone,
1062 // since you have to use resources.
1063 // We'll have to find another way to add a menu
1064 // by changing/adding menu items to an existing menu.
1065 #if defined(WINCE_WITHOUT_COMMANDBAR)
1069 wxToolMenuBar
* const bar
= static_cast<wxToolMenuBar
*>(GetToolBar());
1073 HWND hCommandBar
= GetHwndOf(bar
);
1075 // notify comctl32.dll about the version of the headers we use before using
1076 // any other TB_XXX messages
1077 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
1080 wxZeroMemory(tbButton
);
1081 tbButton
.iBitmap
= I_IMAGENONE
;
1082 tbButton
.fsState
= TBSTATE_ENABLED
;
1083 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
1084 TBSTYLE_NO_DROPDOWN_ARROW
|
1087 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
1089 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
1090 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1091 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
1092 tbButton
.iString
= (int) label
.wx_str();
1094 tbButton
.idCommand
= NewControlId();
1095 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
1097 wxLogLastError(wxT("TB_INSERTBUTTON"));
1101 m_hMenu
= bar
->GetHMenu();
1103 #else // !__WXWINCE__
1107 m_hMenu
= (WXHMENU
)::CreateMenu();
1111 wxLogLastError(wxT("CreateMenu"));
1115 size_t count
= GetMenuCount(), i
;
1116 wxMenuList::iterator it
;
1117 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1119 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
1120 (UINT_PTR
)(*it
)->GetHMenu(),
1121 m_titles
[i
].wx_str()) )
1123 wxLogLastError(wxT("AppendMenu"));
1129 #endif // __WXWINCE__/!__WXWINCE__
1132 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
1135 wxASSERT(menu
->GetHMenu());
1138 #if defined(__WXWINCE__)
1139 int totalMSWItems
= GetMenuCount();
1141 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1144 int i
; // For old C++ compatibility
1145 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1147 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1150 for(i
=0; i
<wxpos
; i
++)
1152 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1159 // ---------------------------------------------------------------------------
1160 // wxMenuBar functions to work with the top level submenus
1161 // ---------------------------------------------------------------------------
1163 // NB: we don't support owner drawn top level items for now, if we do these
1164 // functions would have to be changed to use wxMenuItem as well
1166 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1168 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1169 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1171 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1173 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1178 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1180 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1182 m_titles
[pos
] = label
;
1184 if ( !IsAttached() )
1188 //else: have to modify the existing menu
1190 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1193 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1194 if ( flagsOld
== 0xFFFFFFFF )
1196 wxLogLastError(wxT("GetMenuState"));
1201 if ( flagsOld
& MF_POPUP
)
1203 // HIBYTE contains the number of items in the submenu in this case
1205 id
= (UINT_PTR
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1215 info
.cbSize
= sizeof(info
);
1216 info
.fMask
= MIIM_TYPE
;
1217 info
.fType
= MFT_STRING
;
1218 info
.cch
= label
.length();
1219 info
.dwTypeData
= const_cast<wxChar
*>(label
.wx_str());
1220 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, &info
) )
1222 wxLogLastError(wxT("SetMenuItemInfo"));
1226 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1227 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1229 wxLogLastError(wxT("ModifyMenu"));
1236 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1238 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1239 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1241 return m_titles
[pos
];
1244 // ---------------------------------------------------------------------------
1245 // wxMenuBar construction
1246 // ---------------------------------------------------------------------------
1248 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1250 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1254 m_titles
[pos
] = title
;
1256 #if defined(WINCE_WITHOUT_COMMANDBAR)
1262 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1264 // can't use ModifyMenu() because it deletes the submenu it replaces
1265 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1267 wxLogLastError(wxT("RemoveMenu"));
1270 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1271 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1272 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1274 wxLogLastError(wxT("InsertMenu"));
1278 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1280 // need to rebuild accell table
1281 RebuildAccelTable();
1283 #endif // wxUSE_ACCEL
1292 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1294 // Find out which MSW item before which we'll be inserting before
1295 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1296 // If IsAttached() is false this won't be used anyway
1298 #if defined(WINCE_WITHOUT_COMMANDBAR)
1304 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1305 ? -1 // append the menu
1306 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1308 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1311 m_titles
.Insert(title
, pos
);
1315 #if defined(WINCE_WITHOUT_COMMANDBAR)
1319 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1320 tbButton
.iBitmap
= I_IMAGENONE
;
1321 tbButton
.fsState
= TBSTATE_ENABLED
;
1322 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1324 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1325 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1326 wxString label
= wxStripMenuCodes(title
);
1327 tbButton
.iString
= (int) label
.wx_str();
1329 tbButton
.idCommand
= NewControlId();
1330 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1332 wxLogLastError(wxT("TB_INSERTBUTTON"));
1335 wxUnusedVar(mswpos
);
1337 if ( !::InsertMenu(GetHmenu(), mswpos
,
1338 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1339 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1341 wxLogLastError(wxT("InsertMenu"));
1345 if ( menu
->HasAccels() )
1347 // need to rebuild accell table
1348 RebuildAccelTable();
1350 #endif // wxUSE_ACCEL
1359 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1361 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1362 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1364 if ( !wxMenuBarBase::Append(menu
, title
) )
1367 m_titles
.Add(title
);
1369 #if defined(WINCE_WITHOUT_COMMANDBAR)
1375 #if defined(WINCE_WITHOUT_COMMANDBAR)
1379 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1380 tbButton
.iBitmap
= I_IMAGENONE
;
1381 tbButton
.fsState
= TBSTATE_ENABLED
;
1382 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1384 size_t pos
= GetMenuCount();
1385 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1386 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1387 wxString label
= wxStripMenuCodes(title
);
1388 tbButton
.iString
= (int) label
.wx_str();
1390 tbButton
.idCommand
= NewControlId();
1391 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1393 wxLogLastError(wxT("TB_INSERTBUTTON"));
1397 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1398 (UINT_PTR
)submenu
, title
.wx_str()) )
1400 wxLogLastError(wxT("AppendMenu"));
1405 if ( menu
->HasAccels() )
1407 // need to rebuild accelerator table
1408 RebuildAccelTable();
1410 #endif // wxUSE_ACCEL
1419 wxMenu
*wxMenuBar::Remove(size_t pos
)
1421 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1425 #if defined(WINCE_WITHOUT_COMMANDBAR)
1431 #if defined(WINCE_WITHOUT_COMMANDBAR)
1434 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1436 wxLogLastError(wxT("TB_DELETEBUTTON"));
1440 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1442 wxLogLastError(wxT("RemoveMenu"));
1447 if ( menu
->HasAccels() )
1449 // need to rebuild accell table
1450 RebuildAccelTable();
1452 #endif // wxUSE_ACCEL
1458 m_titles
.RemoveAt(pos
);
1465 void wxMenuBar::RebuildAccelTable()
1467 // merge the accelerators of all menus into one accel table
1468 size_t nAccelCount
= 0;
1469 size_t i
, count
= GetMenuCount();
1470 wxMenuList::iterator it
;
1471 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1473 nAccelCount
+= (*it
)->GetAccelCount();
1478 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1481 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1483 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1486 SetAcceleratorTable(wxAcceleratorTable(nAccelCount
, accelEntries
));
1488 delete [] accelEntries
;
1492 #endif // wxUSE_ACCEL
1494 void wxMenuBar::Attach(wxFrame
*frame
)
1496 wxMenuBarBase::Attach(frame
);
1498 #if defined(WINCE_WITH_COMMANDBAR)
1502 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1507 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1509 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1516 RebuildAccelTable();
1517 #endif // wxUSE_ACCEL
1520 #if defined(WINCE_WITH_COMMANDBAR)
1521 bool wxMenuBar::AddAdornments(long style
)
1523 if (m_adornmentsAdded
|| !m_commandBar
)
1526 if (style
& wxCLOSE_BOX
)
1528 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1530 wxLogLastError(wxT("CommandBar_AddAdornments"));
1541 void wxMenuBar::Detach()
1543 wxMenuBarBase::Detach();
1546 #endif // wxUSE_MENUS