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 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 WX_DEFINE_LIST( wxMenuInfoList
)
168 #if wxUSE_EXTENDED_RTTI
170 WX_DEFINE_FLAGS( wxMenuStyle
)
172 wxBEGIN_FLAGS( wxMenuStyle
)
173 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
174 wxEND_FLAGS( wxMenuStyle
)
176 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
178 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
180 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
182 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
185 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
186 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
187 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
188 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
189 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
190 wxEND_PROPERTIES_TABLE()
192 wxBEGIN_HANDLERS_TABLE(wxMenu
)
193 wxEND_HANDLERS_TABLE()
195 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
197 WX_DEFINE_FLAGS( wxMenuBarStyle
)
199 wxBEGIN_FLAGS( wxMenuBarStyle
)
200 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
201 wxEND_FLAGS( wxMenuBarStyle
)
203 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
204 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
209 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
211 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
213 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
214 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
215 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
216 wxEND_PROPERTIES_TABLE()
218 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
219 wxEND_HANDLERS_TABLE()
221 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
223 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
225 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
227 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
230 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
231 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
232 wxEND_PROPERTIES_TABLE()
234 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
235 wxEND_HANDLERS_TABLE()
237 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
240 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
241 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
242 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
245 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
247 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
248 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
249 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
251 wxMenuInfo
* info
= new wxMenuInfo() ;
252 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetMenuLabel(i
) ) ;
253 list
->Append( info
) ;
258 // ---------------------------------------------------------------------------
259 // wxMenu construction, adding and removing menu items
260 // ---------------------------------------------------------------------------
262 // Construct a menu with optional title (then use append)
266 m_startRadioGroup
= -1;
268 #if wxUSE_OWNER_DRAWN
269 m_ownerDrawn
= false;
270 m_maxBitmapWidth
= 0;
271 m_maxAccelWidth
= -1;
272 #endif // wxUSE_OWNER_DRAWN
275 m_hMenu
= (WXHMENU
)CreatePopupMenu();
278 wxLogLastError(wxT("CreatePopupMenu"));
281 // if we have a title, insert it in the beginning of the menu
282 if ( !m_title
.empty() )
284 const wxString title
= m_title
;
285 m_title
.clear(); // so that SetTitle() knows there was no title before
290 // The wxWindow destructor will take care of deleting the submenus.
293 // we should free Windows resources only if Windows doesn't do it for us
294 // which happens if we're attached to a menubar or a submenu of another
296 if ( !IsAttached() && !GetParent() )
298 if ( !::DestroyMenu(GetHmenu()) )
300 wxLogLastError(wxT("DestroyMenu"));
306 WX_CLEAR_ARRAY(m_accels
);
307 #endif // wxUSE_ACCEL
312 // this will take effect during the next call to Append()
316 void wxMenu::Attach(wxMenuBarBase
*menubar
)
318 wxMenuBase::Attach(menubar
);
325 int wxMenu::FindAccel(int id
) const
327 size_t n
, count
= m_accels
.GetCount();
328 for ( n
= 0; n
< count
; n
++ )
330 if ( m_accels
[n
]->m_command
== id
)
337 void wxMenu::UpdateAccel(wxMenuItem
*item
)
339 if ( item
->IsSubMenu() )
341 wxMenu
*submenu
= item
->GetSubMenu();
342 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
345 UpdateAccel(node
->GetData());
347 node
= node
->GetNext();
350 else if ( !item
->IsSeparator() )
352 // recurse upwards: we should only modify m_accels of the top level
353 // menus, not of the submenus as wxMenuBar doesn't look at them
354 // (alternative and arguable cleaner solution would be to recurse
355 // downwards in GetAccelCount() and CopyAccels())
358 GetParent()->UpdateAccel(item
);
362 // find the (new) accel for this item
363 wxAcceleratorEntry
*accel
= wxAcceleratorEntry::Create(item
->GetItemLabel());
365 accel
->m_command
= item
->GetId();
368 int n
= FindAccel(item
->GetId());
369 if ( n
== wxNOT_FOUND
)
371 // no old, add new if any
375 return; // skipping RebuildAccelTable() below
379 // replace old with new or just remove the old one if no new
384 m_accels
.RemoveAt(n
);
389 GetMenuBar()->RebuildAccelTable();
392 ResetMaxAccelWidth();
394 //else: it is a separator, they can't have accels, nothing to do
397 #endif // wxUSE_ACCEL
402 // helper of DoInsertOrAppend(): returns the HBITMAP to use in MENUITEMINFO
403 HBITMAP
GetHBitmapForMenu(wxMenuItem
*pItem
, bool checked
= true)
405 // Under versions of Windows older than Vista we can't pass HBITMAP
406 // directly as hbmpItem for 2 reasons:
407 // 1. We can't draw it with transparency then (this is not
408 // very important now but would be with themed menu bg)
409 // 2. Worse, Windows inverts the bitmap for the selected
410 // item and this looks downright ugly
412 // So we prefer to instead draw it ourselves in MSWOnDrawItem().by using
413 // HBMMENU_CALLBACK when inserting it
415 // However under Vista using HBMMENU_CALLBACK causes the entire menu to be
416 // drawn using the classic theme instead of the current one and it does
417 // handle transparency just fine so do use the real bitmap there
419 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
421 wxBitmap bmp
= pItem
->GetBitmap(checked
);
424 // we must use PARGB DIB for the menu bitmaps so ensure that we do
425 wxImage
img(bmp
.ConvertToImage());
426 if ( !img
.HasAlpha() )
429 pItem
->SetBitmap(img
, checked
);
432 return GetHbitmapOf(pItem
->GetBitmap(checked
));
434 //else: bitmap is not set
438 #endif // wxUSE_IMAGE
440 return HBMMENU_CALLBACK
;
443 } // anonymous namespace
445 // append a new item or submenu to the menu
446 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
450 #endif // wxUSE_ACCEL
452 // we should support disabling the item even prior to adding it to the menu
453 UINT flags
= pItem
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
;
455 // if "Break" has just been called, insert a menu break before this item
456 // (and don't forget to reset the flag)
458 flags
|= MF_MENUBREAK
;
462 if ( pItem
->IsSeparator() ) {
463 flags
|= MF_SEPARATOR
;
466 // id is the numeric id for normal menu items and HMENU for submenus as
467 // required by ::AppendMenu() API
469 wxMenu
*submenu
= pItem
->GetSubMenu();
470 if ( submenu
!= NULL
) {
471 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
473 submenu
->SetParent(this);
475 id
= (UINT_PTR
)submenu
->GetHMenu();
480 id
= pItem
->GetMSWId();
484 // prepare to insert the item in the menu
485 wxString itemText
= pItem
->GetItemLabel();
486 LPCTSTR pData
= NULL
;
487 if ( pos
== (size_t)-1 )
489 // append at the end (note that the item is already appended to
490 // internal data structures)
491 pos
= GetMenuItemCount() - 1;
494 // adjust position to account for the title, if any
495 if ( !m_title
.empty() )
496 pos
+= 2; // for the title itself and its separator
500 #if wxUSE_OWNER_DRAWN
501 // Under older systems mixing owner-drawn and non-owner-drawn items results
502 // in inconsistent margins, so we force this one to be owner-drawn if any
503 // other items already are.
505 pItem
->SetOwnerDrawn(true);
506 #endif // wxUSE_OWNER_DRAWN
508 // check if we have something more than a simple text item
509 #if wxUSE_OWNER_DRAWN
510 if ( pItem
->IsOwnerDrawn() )
514 if ( !m_ownerDrawn
&& !pItem
->IsSeparator() )
516 // MIIM_BITMAP only works under WinME/2000+ so we always use owner
517 // drawn item under the previous versions and we also have to use
518 // them in any case if the item has custom colours or font
519 static const wxWinVersion winver
= wxGetWinVersion();
520 bool mustUseOwnerDrawn
= winver
< wxWinVersion_98
||
521 pItem
->GetTextColour().Ok() ||
522 pItem
->GetBackgroundColour().Ok() ||
523 pItem
->GetFont().Ok();
525 if ( !mustUseOwnerDrawn
)
527 const wxBitmap
& bmpUnchecked
= pItem
->GetBitmap(false),
528 bmpChecked
= pItem
->GetBitmap(true);
530 if ( (bmpUnchecked
.Ok() && IsGreaterThanStdSize(bmpUnchecked
)) ||
531 (bmpChecked
.Ok() && IsGreaterThanStdSize(bmpChecked
)) )
533 mustUseOwnerDrawn
= true;
537 // use InsertMenuItem() if possible as it's guaranteed to look
538 // correct while our owner-drawn code is not
539 if ( !mustUseOwnerDrawn
)
541 WinStruct
<MENUITEMINFO
> mii
;
542 mii
.fMask
= MIIM_STRING
| MIIM_DATA
;
544 // don't set hbmpItem for the checkable items as it would
545 // be used for both checked and unchecked state
546 if ( pItem
->IsCheckable() )
548 mii
.fMask
|= MIIM_CHECKMARKS
;
549 mii
.hbmpChecked
= GetHBitmapForMenu(pItem
, true);
550 mii
.hbmpUnchecked
= GetHBitmapForMenu(pItem
, false);
552 else if ( pItem
->GetBitmap().IsOk() )
554 mii
.fMask
|= MIIM_BITMAP
;
555 mii
.hbmpItem
= GetHBitmapForMenu(pItem
);
558 mii
.cch
= itemText
.length();
559 mii
.dwTypeData
= const_cast<wxChar
*>(itemText
.wx_str());
561 if ( flags
& MF_POPUP
)
563 mii
.fMask
|= MIIM_SUBMENU
;
564 mii
.hSubMenu
= GetHmenuOf(pItem
->GetSubMenu());
568 mii
.fMask
|= MIIM_ID
;
572 mii
.dwItemData
= reinterpret_cast<ULONG_PTR
>(pItem
);
574 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
577 wxLogLastError(wxT("InsertMenuItem()"));
579 else // InsertMenuItem() ok
581 // we need to remove the extra indent which is reserved for
582 // the checkboxes by default as it looks ugly unless check
583 // boxes are used together with bitmaps and this is not the
585 WinStruct
<MENUINFO
> mi
;
587 // don't call SetMenuInfo() directly, this would prevent
588 // the app from starting up under Windows 95/NT 4
589 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
591 wxDynamicLibrary
dllUser(wxT("user32"));
592 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
593 if ( pfnSetMenuInfo
)
595 mi
.fMask
= MIM_STYLE
;
596 mi
.dwStyle
= MNS_CHECKORBMP
;
597 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
599 wxLogLastError(wxT("SetMenuInfo(MNS_NOCHECK)"));
603 // tell the item that it's not really owner-drawn but only
604 // needs to draw its bitmap, the rest is done by Windows
605 pItem
->SetOwnerDrawn(false);
613 // item draws itself, pass pointer to it in data parameter
614 flags
|= MF_OWNERDRAW
;
615 pData
= (LPCTSTR
)pItem
;
617 bool updateAllMargins
= false;
619 // get size of bitmap always return valid value (0 for invalid bitmap),
620 // so we don't needed check if bitmap is valid ;)
621 int uncheckedW
= pItem
->GetBitmap(false).GetWidth();
622 int checkedW
= pItem
->GetBitmap(true).GetWidth();
624 if ( m_maxBitmapWidth
< uncheckedW
)
626 m_maxBitmapWidth
= uncheckedW
;
627 updateAllMargins
= true;
630 if ( m_maxBitmapWidth
< checkedW
)
632 m_maxBitmapWidth
= checkedW
;
633 updateAllMargins
= true;
636 // make other item ownerdrawn and update margin width for equals alignment
637 if ( !m_ownerDrawn
|| updateAllMargins
)
639 // we must use position in SetOwnerDrawnMenuItem because
640 // all separators have the same id
642 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
645 wxMenuItem
* item
= node
->GetData();
647 if ( !item
->IsOwnerDrawn())
649 item
->SetOwnerDrawn(true);
650 SetOwnerDrawnMenuItem(GetHmenu(), pos
,
651 reinterpret_cast<ULONG_PTR
>(item
), TRUE
);
654 item
->SetMarginWidth(m_maxBitmapWidth
);
656 node
= node
->GetNext();
660 // set menu as ownerdrawn
663 ResetMaxAccelWidth();
665 // only update our margin for equals alignment to other item
666 else if ( !updateAllMargins
)
668 pItem
->SetMarginWidth(m_maxBitmapWidth
);
673 #endif // wxUSE_OWNER_DRAWN
675 // item is just a normal string (passed in data parameter)
679 itemText
= wxMenuItem::GetLabelText(itemText
);
682 pData
= (wxChar
*)itemText
.wx_str();
685 // item might have already been inserted by InsertMenuItem() above
688 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
690 wxLogLastError(wxT("InsertMenu[Item]()"));
697 // if we just appended the title, highlight it
698 if ( id
== idMenuTitle
)
700 // visually select the menu title
701 SetDefaultMenuItem(GetHmenu(), id
);
704 // if we're already attached to the menubar, we must update it
705 if ( IsAttached() && GetMenuBar()->IsAttached() )
707 GetMenuBar()->Refresh();
713 void wxMenu::EndRadioGroup()
715 // we're not inside a radio group any longer
716 m_startRadioGroup
= -1;
719 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
721 wxCHECK_MSG( item
, NULL
, wxT("NULL item in wxMenu::DoAppend") );
725 if ( item
->GetKind() == wxITEM_RADIO
)
727 int count
= GetMenuItemCount();
729 if ( m_startRadioGroup
== -1 )
731 // start a new radio group
732 m_startRadioGroup
= count
;
734 // for now it has just one element
735 item
->SetAsRadioGroupStart();
736 item
->SetRadioGroupEnd(m_startRadioGroup
);
738 // ensure that we have a checked item in the radio group
741 else // extend the current radio group
743 // we need to update its end item
744 item
->SetRadioGroupStart(m_startRadioGroup
);
745 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
749 node
->GetData()->SetRadioGroupEnd(count
);
753 wxFAIL_MSG( wxT("where is the radio group start item?") );
757 else // not a radio item
762 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
769 // check the item initially
776 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
778 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
784 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
786 // we need to find the item's position in the child list
788 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
789 for ( pos
= 0; node
; pos
++ )
791 if ( node
->GetData() == item
)
794 node
= node
->GetNext();
797 // DoRemove() (unlike Remove) can only be called for an existing item!
798 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
801 // remove the corresponding accel from the accel table
802 int n
= FindAccel(item
->GetId());
803 if ( n
!= wxNOT_FOUND
)
807 m_accels
.RemoveAt(n
);
809 ResetMaxAccelWidth();
811 //else: this item doesn't have an accel, nothing to do
812 #endif // wxUSE_ACCEL
814 // remove the item from the menu
815 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
817 wxLogLastError(wxT("RemoveMenu"));
820 if ( IsAttached() && GetMenuBar()->IsAttached() )
822 // otherwise, the change won't be visible
823 GetMenuBar()->Refresh();
826 // and from internal data structures
827 return wxMenuBase::DoRemove(item
);
830 // ---------------------------------------------------------------------------
831 // accelerator helpers
832 // ---------------------------------------------------------------------------
836 // create the wxAcceleratorEntries for our accels and put them into the provided
837 // array - return the number of accels we have
838 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
840 size_t count
= GetAccelCount();
841 for ( size_t n
= 0; n
< count
; n
++ )
843 *accels
++ = *m_accels
[n
];
849 wxAcceleratorTable
*wxMenu::CreateAccelTable() const
851 const size_t count
= m_accels
.size();
852 wxScopedArray
<wxAcceleratorEntry
> accels(new wxAcceleratorEntry
[count
]);
853 CopyAccels(accels
.get());
855 return new wxAcceleratorTable(count
, accels
.get());
858 #endif // wxUSE_ACCEL
860 // ---------------------------------------------------------------------------
861 // ownerdrawn helpers
862 // ---------------------------------------------------------------------------
864 #if wxUSE_OWNER_DRAWN
866 void wxMenu::CalculateMaxAccelWidth()
868 wxASSERT_MSG( m_maxAccelWidth
== -1, wxT("it's really needed?") );
870 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
873 wxMenuItem
* item
= node
->GetData();
875 if ( item
->IsOwnerDrawn() )
877 int width
= item
->MeasureAccelWidth();
878 if (width
> m_maxAccelWidth
)
879 m_maxAccelWidth
= width
;
882 node
= node
->GetNext();
886 #endif // wxUSE_OWNER_DRAWN
888 // ---------------------------------------------------------------------------
890 // ---------------------------------------------------------------------------
892 void wxMenu::SetTitle(const wxString
& label
)
894 bool hasNoTitle
= m_title
.empty();
897 HMENU hMenu
= GetHmenu();
901 if ( !label
.empty() )
903 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
904 idMenuTitle
, m_title
.wx_str()) ||
905 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
907 wxLogLastError(wxT("InsertMenu"));
915 // remove the title and the separator after it
916 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
917 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
919 wxLogLastError(wxT("RemoveMenu"));
928 info
.cbSize
= sizeof(info
);
929 info
.fMask
= MIIM_TYPE
;
930 info
.fType
= MFT_STRING
;
931 info
.cch
= m_title
.length();
932 info
.dwTypeData
= const_cast<wxChar
*>(m_title
.wx_str());
933 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
935 wxLogLastError(wxT("SetMenuItemInfo"));
938 if ( !ModifyMenu(hMenu
, 0u,
939 MF_BYPOSITION
| MF_STRING
,
940 idMenuTitle
, m_title
.wx_str()) )
942 wxLogLastError(wxT("ModifyMenu"));
949 // put the title string in bold face
950 if ( !m_title
.empty() )
952 SetDefaultMenuItem(GetHmenu(), idMenuTitle
);
957 // ---------------------------------------------------------------------------
959 // ---------------------------------------------------------------------------
961 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
963 const int id
= (signed short)id_
;
965 // ignore commands from the menu title
966 if ( id
!= (int)idMenuTitle
)
968 // update the check item when it's clicked
969 wxMenuItem
* const item
= FindItem(id
);
970 if ( item
&& item
->IsCheckable() )
973 // get the status of the menu item: note that it has been just changed
974 // by Toggle() above so here we already get the new state of the item
975 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
976 SendEvent(id
, menuState
& MF_CHECKED
);
982 // ---------------------------------------------------------------------------
984 // ---------------------------------------------------------------------------
986 void wxMenuBar::Init()
988 m_eventHandler
= this;
990 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
993 // Not using a combined wxToolBar/wxMenuBar? then use
994 // a commandbar in WinCE .NET just to implement the
996 #if defined(WINCE_WITH_COMMANDBAR)
998 m_adornmentsAdded
= false;
1002 wxMenuBar::wxMenuBar()
1007 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
1012 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
1016 m_titles
.Alloc(count
);
1018 for ( size_t i
= 0; i
< count
; i
++ )
1020 m_menus
.Append(menus
[i
]);
1021 m_titles
.Add(titles
[i
]);
1023 menus
[i
]->Attach(this);
1027 wxMenuBar::~wxMenuBar()
1029 // In Windows CE (not .NET), the menubar is always associated
1030 // with a toolbar, which destroys the menu implicitly.
1031 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
1034 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
1036 toolMenuBar
->SetMenuBar(NULL
);
1039 // we should free Windows resources only if Windows doesn't do it for us
1040 // which happens if we're attached to a frame
1041 if (m_hMenu
&& !IsAttached())
1043 #if defined(WINCE_WITH_COMMANDBAR)
1044 ::DestroyWindow((HWND
) m_commandBar
);
1045 m_commandBar
= (WXHWND
) NULL
;
1047 ::DestroyMenu((HMENU
)m_hMenu
);
1049 m_hMenu
= (WXHMENU
)NULL
;
1054 // ---------------------------------------------------------------------------
1055 // wxMenuBar helpers
1056 // ---------------------------------------------------------------------------
1058 void wxMenuBar::Refresh()
1063 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
1065 #if defined(WINCE_WITHOUT_COMMANDBAR)
1068 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
1070 #elif defined(WINCE_WITH_COMMANDBAR)
1072 DrawMenuBar((HWND
) m_commandBar
);
1074 DrawMenuBar(GetHwndOf(GetFrame()));
1078 WXHMENU
wxMenuBar::Create()
1080 // Note: this doesn't work at all on Smartphone,
1081 // since you have to use resources.
1082 // We'll have to find another way to add a menu
1083 // by changing/adding menu items to an existing menu.
1084 #if defined(WINCE_WITHOUT_COMMANDBAR)
1088 wxToolMenuBar
* const bar
= static_cast<wxToolMenuBar
*>(GetToolBar());
1092 HWND hCommandBar
= GetHwndOf(bar
);
1094 // notify comctl32.dll about the version of the headers we use before using
1095 // any other TB_XXX messages
1096 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
1099 wxZeroMemory(tbButton
);
1100 tbButton
.iBitmap
= I_IMAGENONE
;
1101 tbButton
.fsState
= TBSTATE_ENABLED
;
1102 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
1103 TBSTYLE_NO_DROPDOWN_ARROW
|
1106 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
1108 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
1109 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1110 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
1111 tbButton
.iString
= (int) label
.wx_str();
1113 tbButton
.idCommand
= NewControlId();
1114 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
1116 wxLogLastError(wxT("TB_INSERTBUTTON"));
1120 m_hMenu
= bar
->GetHMenu();
1122 #else // !__WXWINCE__
1126 m_hMenu
= (WXHMENU
)::CreateMenu();
1130 wxLogLastError(wxT("CreateMenu"));
1134 size_t count
= GetMenuCount(), i
;
1135 wxMenuList::iterator it
;
1136 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1138 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
1139 (UINT_PTR
)(*it
)->GetHMenu(),
1140 m_titles
[i
].wx_str()) )
1142 wxLogLastError(wxT("AppendMenu"));
1148 #endif // __WXWINCE__/!__WXWINCE__
1151 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
1154 wxASSERT(menu
->GetHMenu());
1157 #if defined(__WXWINCE__)
1158 int totalMSWItems
= GetMenuCount();
1160 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1163 int i
; // For old C++ compatibility
1164 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1166 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1169 for(i
=0; i
<wxpos
; i
++)
1171 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1178 // ---------------------------------------------------------------------------
1179 // wxMenuBar functions to work with the top level submenus
1180 // ---------------------------------------------------------------------------
1182 // NB: we don't support owner drawn top level items for now, if we do these
1183 // functions would have to be changed to use wxMenuItem as well
1185 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1187 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1188 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1190 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1192 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1197 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1199 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1201 m_titles
[pos
] = label
;
1203 if ( !IsAttached() )
1207 //else: have to modify the existing menu
1209 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1212 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1213 if ( flagsOld
== 0xFFFFFFFF )
1215 wxLogLastError(wxT("GetMenuState"));
1220 if ( flagsOld
& MF_POPUP
)
1222 // HIBYTE contains the number of items in the submenu in this case
1224 id
= (UINT_PTR
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1234 info
.cbSize
= sizeof(info
);
1235 info
.fMask
= MIIM_TYPE
;
1236 info
.fType
= MFT_STRING
;
1237 info
.cch
= label
.length();
1238 info
.dwTypeData
= const_cast<wxChar
*>(label
.wx_str());
1239 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, &info
) )
1241 wxLogLastError(wxT("SetMenuItemInfo"));
1245 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1246 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1248 wxLogLastError(wxT("ModifyMenu"));
1255 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1257 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1258 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1260 return m_titles
[pos
];
1263 // ---------------------------------------------------------------------------
1264 // wxMenuBar construction
1265 // ---------------------------------------------------------------------------
1267 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1269 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1273 m_titles
[pos
] = title
;
1275 #if defined(WINCE_WITHOUT_COMMANDBAR)
1281 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1283 // can't use ModifyMenu() because it deletes the submenu it replaces
1284 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1286 wxLogLastError(wxT("RemoveMenu"));
1289 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1290 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1291 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1293 wxLogLastError(wxT("InsertMenu"));
1297 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1299 // need to rebuild accell table
1300 RebuildAccelTable();
1302 #endif // wxUSE_ACCEL
1311 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1313 // Find out which MSW item before which we'll be inserting before
1314 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1315 // If IsAttached() is false this won't be used anyway
1317 #if defined(WINCE_WITHOUT_COMMANDBAR)
1323 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1324 ? -1 // append the menu
1325 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1327 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1330 m_titles
.Insert(title
, pos
);
1334 #if defined(WINCE_WITHOUT_COMMANDBAR)
1338 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1339 tbButton
.iBitmap
= I_IMAGENONE
;
1340 tbButton
.fsState
= TBSTATE_ENABLED
;
1341 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1343 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1344 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1345 wxString label
= wxStripMenuCodes(title
);
1346 tbButton
.iString
= (int) label
.wx_str();
1348 tbButton
.idCommand
= NewControlId();
1349 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1351 wxLogLastError(wxT("TB_INSERTBUTTON"));
1354 wxUnusedVar(mswpos
);
1356 if ( !::InsertMenu(GetHmenu(), mswpos
,
1357 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1358 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1360 wxLogLastError(wxT("InsertMenu"));
1364 if ( menu
->HasAccels() )
1366 // need to rebuild accell table
1367 RebuildAccelTable();
1369 #endif // wxUSE_ACCEL
1378 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1380 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1381 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1383 if ( !wxMenuBarBase::Append(menu
, title
) )
1386 m_titles
.Add(title
);
1388 #if defined(WINCE_WITHOUT_COMMANDBAR)
1394 #if defined(WINCE_WITHOUT_COMMANDBAR)
1398 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1399 tbButton
.iBitmap
= I_IMAGENONE
;
1400 tbButton
.fsState
= TBSTATE_ENABLED
;
1401 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1403 size_t pos
= GetMenuCount();
1404 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1405 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1406 wxString label
= wxStripMenuCodes(title
);
1407 tbButton
.iString
= (int) label
.wx_str();
1409 tbButton
.idCommand
= NewControlId();
1410 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1412 wxLogLastError(wxT("TB_INSERTBUTTON"));
1416 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1417 (UINT_PTR
)submenu
, title
.wx_str()) )
1419 wxLogLastError(wxT("AppendMenu"));
1424 if ( menu
->HasAccels() )
1426 // need to rebuild accelerator table
1427 RebuildAccelTable();
1429 #endif // wxUSE_ACCEL
1438 wxMenu
*wxMenuBar::Remove(size_t pos
)
1440 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1444 #if defined(WINCE_WITHOUT_COMMANDBAR)
1450 #if defined(WINCE_WITHOUT_COMMANDBAR)
1453 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1455 wxLogLastError(wxT("TB_DELETEBUTTON"));
1459 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1461 wxLogLastError(wxT("RemoveMenu"));
1466 if ( menu
->HasAccels() )
1468 // need to rebuild accell table
1469 RebuildAccelTable();
1471 #endif // wxUSE_ACCEL
1477 m_titles
.RemoveAt(pos
);
1484 void wxMenuBar::RebuildAccelTable()
1486 // merge the accelerators of all menus into one accel table
1487 size_t nAccelCount
= 0;
1488 size_t i
, count
= GetMenuCount();
1489 wxMenuList::iterator it
;
1490 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1492 nAccelCount
+= (*it
)->GetAccelCount();
1497 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1500 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1502 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1505 SetAcceleratorTable(wxAcceleratorTable(nAccelCount
, accelEntries
));
1507 delete [] accelEntries
;
1511 #endif // wxUSE_ACCEL
1513 void wxMenuBar::Attach(wxFrame
*frame
)
1515 wxMenuBarBase::Attach(frame
);
1517 #if defined(WINCE_WITH_COMMANDBAR)
1521 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1526 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1528 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1535 RebuildAccelTable();
1536 #endif // wxUSE_ACCEL
1539 #if defined(WINCE_WITH_COMMANDBAR)
1540 bool wxMenuBar::AddAdornments(long style
)
1542 if (m_adornmentsAdded
|| !m_commandBar
)
1545 if (style
& wxCLOSE_BOX
)
1547 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1549 wxLogLastError(wxT("CommandBar_AddAdornments"));
1560 void wxMenuBar::Detach()
1562 wxMenuBarBase::Detach();
1565 #endif // wxUSE_MENUS