1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/menu.cpp
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
40 #include "wx/ownerdrw.h"
43 #include "wx/scopedarray.h"
45 #include "wx/msw/private.h"
46 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
54 #if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__)
58 #include "wx/msw/wince/missing.h"
62 // other standard headers
66 #include "wx/dynlib.h"
69 #ifndef MNS_CHECKORBMP
70 #define MNS_CHECKORBMP 0x04000000
73 #define MIM_STYLE 0x00000010
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 // the (popup) menu title has this special id
85 static const UINT idMenuTitle
= (UINT
)-3;
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
94 // make the given menu item default
95 void SetDefaultMenuItem(HMENU
WXUNUSED_IN_WINCE(hmenu
),
96 UINT
WXUNUSED_IN_WINCE(id
))
101 mii
.cbSize
= sizeof(MENUITEMINFO
);
102 mii
.fMask
= MIIM_STATE
;
103 mii
.fState
= MFS_DEFAULT
;
105 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
107 wxLogLastError(wxT("SetMenuItemInfo"));
113 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
117 info
.cbSize
= sizeof(info
);
118 info
.fMask
= MIIM_STATE
;
119 // MF_BYCOMMAND is zero so test MF_BYPOSITION
120 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
122 wxLogLastError(wxT("GetMenuItemInfo"));
126 #endif // __WXWINCE__
128 bool IsLessThanStdSize(const wxBitmap
& bmp
)
130 // FIXME: these +4 are chosen so that 16*16 bitmaps pass this test with
131 // default SM_CXMENUCHECK value but I have no idea what do we really
133 return bmp
.GetWidth() < ::GetSystemMetrics(SM_CXMENUCHECK
) + 4 &&
134 bmp
.GetHeight() < ::GetSystemMetrics(SM_CYMENUCHECK
) + 4;
137 } // anonymous namespace
139 // ============================================================================
141 // ============================================================================
143 #include "wx/listimpl.cpp"
145 WX_DEFINE_LIST( wxMenuInfoList
)
147 #if wxUSE_EXTENDED_RTTI
149 WX_DEFINE_FLAGS( wxMenuStyle
)
151 wxBEGIN_FLAGS( wxMenuStyle
)
152 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
153 wxEND_FLAGS( wxMenuStyle
)
155 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
157 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
159 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
161 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
164 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
165 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
166 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
167 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
168 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
169 wxEND_PROPERTIES_TABLE()
171 wxBEGIN_HANDLERS_TABLE(wxMenu
)
172 wxEND_HANDLERS_TABLE()
174 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
176 WX_DEFINE_FLAGS( wxMenuBarStyle
)
178 wxBEGIN_FLAGS( wxMenuBarStyle
)
179 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
180 wxEND_FLAGS( wxMenuBarStyle
)
182 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
183 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
188 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
190 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
192 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
193 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
194 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
195 wxEND_PROPERTIES_TABLE()
197 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
198 wxEND_HANDLERS_TABLE()
200 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
202 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
204 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
206 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
209 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
210 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
211 wxEND_PROPERTIES_TABLE()
213 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
214 wxEND_HANDLERS_TABLE()
216 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
219 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
220 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
221 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
224 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
226 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
227 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
228 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
230 wxMenuInfo
* info
= new wxMenuInfo() ;
231 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetMenuLabel(i
) ) ;
232 list
->Append( info
) ;
237 // ---------------------------------------------------------------------------
238 // wxMenu construction, adding and removing menu items
239 // ---------------------------------------------------------------------------
241 // Construct a menu with optional title (then use append)
245 m_startRadioGroup
= -1;
248 m_hMenu
= (WXHMENU
)CreatePopupMenu();
251 wxLogLastError(wxT("CreatePopupMenu"));
254 // if we have a title, insert it in the beginning of the menu
255 if ( !m_title
.empty() )
257 Append(idMenuTitle
, m_title
);
262 // The wxWindow destructor will take care of deleting the submenus.
265 // we should free Windows resources only if Windows doesn't do it for us
266 // which happens if we're attached to a menubar or a submenu of another
268 if ( !IsAttached() && !GetParent() )
270 if ( !::DestroyMenu(GetHmenu()) )
272 wxLogLastError(wxT("DestroyMenu"));
278 WX_CLEAR_ARRAY(m_accels
);
279 #endif // wxUSE_ACCEL
284 // this will take effect during the next call to Append()
288 void wxMenu::Attach(wxMenuBarBase
*menubar
)
290 wxMenuBase::Attach(menubar
);
297 int wxMenu::FindAccel(int id
) const
299 size_t n
, count
= m_accels
.GetCount();
300 for ( n
= 0; n
< count
; n
++ )
302 if ( m_accels
[n
]->m_command
== id
)
309 void wxMenu::UpdateAccel(wxMenuItem
*item
)
311 if ( item
->IsSubMenu() )
313 wxMenu
*submenu
= item
->GetSubMenu();
314 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
317 UpdateAccel(node
->GetData());
319 node
= node
->GetNext();
322 else if ( !item
->IsSeparator() )
324 // recurse upwards: we should only modify m_accels of the top level
325 // menus, not of the submenus as wxMenuBar doesn't look at them
326 // (alternative and arguable cleaner solution would be to recurse
327 // downwards in GetAccelCount() and CopyAccels())
330 GetParent()->UpdateAccel(item
);
334 // find the (new) accel for this item
335 wxAcceleratorEntry
*accel
= wxAcceleratorEntry::Create(item
->GetItemLabel());
337 accel
->m_command
= item
->GetId();
340 int n
= FindAccel(item
->GetId());
341 if ( n
== wxNOT_FOUND
)
343 // no old, add new if any
347 return; // skipping RebuildAccelTable() below
351 // replace old with new or just remove the old one if no new
356 m_accels
.RemoveAt(n
);
361 GetMenuBar()->RebuildAccelTable();
364 //else: it is a separator, they can't have accels, nothing to do
367 #endif // wxUSE_ACCEL
372 // helper of DoInsertOrAppend(): returns the HBITMAP to use in MENUITEMINFO
373 HBITMAP
GetHBitmapForMenu(wxMenuItem
*pItem
, bool checked
= true)
375 // Under versions of Windows older than Vista we can't pass HBITMAP
376 // directly as hbmpItem for 2 reasons:
377 // 1. We can't draw it with transparency then (this is not
378 // very important now but would be with themed menu bg)
379 // 2. Worse, Windows inverts the bitmap for the selected
380 // item and this looks downright ugly
382 // So we prefer to instead draw it ourselves in MSWOnDrawItem().by using
383 // HBMMENU_CALLBACK when inserting it
385 // However under Vista using HBMMENU_CALLBACK causes the entire menu to be
386 // drawn using the classic theme instead of the current one and it does
387 // handle transparency just fine so do use the real bitmap there
389 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
391 wxBitmap bmp
= pItem
->GetBitmap(checked
);
394 // we must use PARGB DIB for the menu bitmaps so ensure that we do
395 wxImage
img(bmp
.ConvertToImage());
396 if ( !img
.HasAlpha() )
399 pItem
->SetBitmap(img
, checked
);
402 return GetHbitmapOf(pItem
->GetBitmap(checked
));
404 //else: bitmap is not set
408 #endif // wxUSE_IMAGE
410 return HBMMENU_CALLBACK
;
413 } // anonymous namespace
415 // append a new item or submenu to the menu
416 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
420 #endif // wxUSE_ACCEL
422 // we should support disabling the item even prior to adding it to the menu
423 UINT flags
= pItem
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
;
425 // if "Break" has just been called, insert a menu break before this item
426 // (and don't forget to reset the flag)
428 flags
|= MF_MENUBREAK
;
432 if ( pItem
->IsSeparator() ) {
433 flags
|= MF_SEPARATOR
;
436 // id is the numeric id for normal menu items and HMENU for submenus as
437 // required by ::AppendMenu() API
439 wxMenu
*submenu
= pItem
->GetSubMenu();
440 if ( submenu
!= NULL
) {
441 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
443 submenu
->SetParent(this);
445 id
= (UINT_PTR
)submenu
->GetHMenu();
450 id
= pItem
->GetMSWId();
454 // prepare to insert the item in the menu
455 wxString itemText
= pItem
->GetItemLabel();
456 LPCTSTR pData
= NULL
;
457 if ( pos
== (size_t)-1 )
459 // append at the end (note that the item is already appended to
460 // internal data structures)
461 pos
= GetMenuItemCount() - 1;
464 // adjust position to account for the title, if any
465 if ( !m_title
.empty() )
466 pos
+= 2; // for the title itself and its separator
470 #if wxUSE_OWNER_DRAWN
471 // Under older systems mixing owner-drawn and non-owner-drawn items results
472 // in inconsistent margins, so we force this one to be owner-drawn if any
473 // other items already are. Later we might want to use a boolean in the
474 // wxMenu to avoid search. Also we might make this fix unnecessary by
475 // getting the correct margin using NONCLIENTMETRICS.
476 static const wxWinVersion winver
= wxGetWinVersion();
477 if ( winver
< wxWinVersion_XP
&&
478 !pItem
->IsOwnerDrawn() && !pItem
->IsSeparator() )
480 // Check if any other items are ownerdrawn, and make ownerdrawn if so
481 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
484 if (node
->GetData()->IsOwnerDrawn())
486 pItem
->SetOwnerDrawn(true);
489 node
= node
->GetNext();
492 #endif // wxUSE_OWNER_DRAWN
494 // check if we have something more than a simple text item
495 #if wxUSE_OWNER_DRAWN
496 if ( pItem
->IsOwnerDrawn() )
499 // MIIM_BITMAP only works under WinME/2000+ so we always use owner
500 // drawn item under the previous versions and we also have to use them
501 // in any case if the item has custom colours or font
502 bool mustUseOwnerDrawn
= winver
< wxWinVersion_98
||
503 pItem
->GetTextColour().Ok() ||
504 pItem
->GetBackgroundColour().Ok() ||
505 pItem
->GetFont().Ok();
506 if ( !mustUseOwnerDrawn
)
508 const wxBitmap
& bmpUnchecked
= pItem
->GetBitmap(false),
509 bmpChecked
= pItem
->GetBitmap(true);
510 if ( (bmpUnchecked
.Ok() && !IsLessThanStdSize(bmpUnchecked
)) ||
511 (bmpChecked
.Ok() && !IsLessThanStdSize(bmpChecked
)) )
513 mustUseOwnerDrawn
= true;
517 // use InsertMenuItem() if possible as it's guaranteed to look correct
518 // while our owner-drawn code is not
519 if ( !mustUseOwnerDrawn
)
521 WinStruct
<MENUITEMINFO
> mii
;
522 mii
.fMask
= MIIM_STRING
| MIIM_DATA
;
524 // don't set hbmpItem for the checkable items as it would
525 // be used for both checked and unchecked state
526 if ( pItem
->IsCheckable() )
528 mii
.fMask
|= MIIM_CHECKMARKS
;
529 mii
.hbmpChecked
= GetHBitmapForMenu(pItem
, true);
530 mii
.hbmpUnchecked
= GetHBitmapForMenu(pItem
, false);
532 else if ( pItem
->GetBitmap().IsOk() )
534 mii
.fMask
|= MIIM_BITMAP
;
535 mii
.hbmpItem
= GetHBitmapForMenu(pItem
);
538 mii
.cch
= itemText
.length();
539 mii
.dwTypeData
= const_cast<wxChar
*>(itemText
.wx_str());
541 if ( flags
& MF_POPUP
)
543 mii
.fMask
|= MIIM_SUBMENU
;
544 mii
.hSubMenu
= GetHmenuOf(pItem
->GetSubMenu());
548 mii
.fMask
|= MIIM_ID
;
552 mii
.dwItemData
= reinterpret_cast<ULONG_PTR
>(pItem
);
554 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
557 wxLogLastError(wxT("InsertMenuItem()"));
559 else // InsertMenuItem() ok
561 // we need to remove the extra indent which is reserved for
562 // the checkboxes by default as it looks ugly unless check
563 // boxes are used together with bitmaps and this is not the
565 WinStruct
<MENUINFO
> mi
;
567 // don't call SetMenuInfo() directly, this would prevent
568 // the app from starting up under Windows 95/NT 4
569 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
571 wxDynamicLibrary
dllUser(wxT("user32"));
572 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
573 if ( pfnSetMenuInfo
)
575 mi
.fMask
= MIM_STYLE
;
576 mi
.dwStyle
= MNS_CHECKORBMP
;
577 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
579 wxLogLastError(wxT("SetMenuInfo(MNS_NOCHECK)"));
583 // tell the item that it's not really owner-drawn but only
584 // needs to draw its bitmap, the rest is done by Windows
585 pItem
->SetOwnerDrawn(false);
592 // item draws itself, pass pointer to it in data parameter
593 flags
|= MF_OWNERDRAW
;
594 pData
= (LPCTSTR
)pItem
;
598 #endif // wxUSE_OWNER_DRAWN
600 // item is just a normal string (passed in data parameter)
604 itemText
= wxMenuItem::GetLabelText(itemText
);
607 pData
= (wxChar
*)itemText
.wx_str();
610 // item might have already been inserted by InsertMenuItem() above
613 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
615 wxLogLastError(wxT("InsertMenu[Item]()"));
622 // if we just appended the title, highlight it
623 if ( id
== idMenuTitle
)
625 // visually select the menu title
626 SetDefaultMenuItem(GetHmenu(), id
);
629 // if we're already attached to the menubar, we must update it
630 if ( IsAttached() && GetMenuBar()->IsAttached() )
632 GetMenuBar()->Refresh();
638 void wxMenu::EndRadioGroup()
640 // we're not inside a radio group any longer
641 m_startRadioGroup
= -1;
644 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
646 wxCHECK_MSG( item
, NULL
, wxT("NULL item in wxMenu::DoAppend") );
650 if ( item
->GetKind() == wxITEM_RADIO
)
652 int count
= GetMenuItemCount();
654 if ( m_startRadioGroup
== -1 )
656 // start a new radio group
657 m_startRadioGroup
= count
;
659 // for now it has just one element
660 item
->SetAsRadioGroupStart();
661 item
->SetRadioGroupEnd(m_startRadioGroup
);
663 // ensure that we have a checked item in the radio group
666 else // extend the current radio group
668 // we need to update its end item
669 item
->SetRadioGroupStart(m_startRadioGroup
);
670 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
674 node
->GetData()->SetRadioGroupEnd(count
);
678 wxFAIL_MSG( wxT("where is the radio group start item?") );
682 else // not a radio item
687 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
694 // check the item initially
701 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
703 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
709 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
711 // we need to find the item's position in the child list
713 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
714 for ( pos
= 0; node
; pos
++ )
716 if ( node
->GetData() == item
)
719 node
= node
->GetNext();
722 // DoRemove() (unlike Remove) can only be called for an existing item!
723 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
726 // remove the corresponding accel from the accel table
727 int n
= FindAccel(item
->GetId());
728 if ( n
!= wxNOT_FOUND
)
732 m_accels
.RemoveAt(n
);
734 //else: this item doesn't have an accel, nothing to do
735 #endif // wxUSE_ACCEL
737 // remove the item from the menu
738 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
740 wxLogLastError(wxT("RemoveMenu"));
743 if ( IsAttached() && GetMenuBar()->IsAttached() )
745 // otherwise, the change won't be visible
746 GetMenuBar()->Refresh();
749 // and from internal data structures
750 return wxMenuBase::DoRemove(item
);
753 // ---------------------------------------------------------------------------
754 // accelerator helpers
755 // ---------------------------------------------------------------------------
759 // create the wxAcceleratorEntries for our accels and put them into the provided
760 // array - return the number of accels we have
761 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
763 size_t count
= GetAccelCount();
764 for ( size_t n
= 0; n
< count
; n
++ )
766 *accels
++ = *m_accels
[n
];
772 wxAcceleratorTable
*wxMenu::CreateAccelTable() const
774 const size_t count
= m_accels
.size();
775 wxScopedArray
<wxAcceleratorEntry
> accels(new wxAcceleratorEntry
[count
]);
776 CopyAccels(accels
.get());
778 return new wxAcceleratorTable(count
, accels
.get());
781 #endif // wxUSE_ACCEL
783 // ---------------------------------------------------------------------------
785 // ---------------------------------------------------------------------------
787 void wxMenu::SetTitle(const wxString
& label
)
789 bool hasNoTitle
= m_title
.empty();
792 HMENU hMenu
= GetHmenu();
796 if ( !label
.empty() )
798 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
799 idMenuTitle
, m_title
.wx_str()) ||
800 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
802 wxLogLastError(wxT("InsertMenu"));
810 // remove the title and the separator after it
811 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
812 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
814 wxLogLastError(wxT("RemoveMenu"));
823 info
.cbSize
= sizeof(info
);
824 info
.fMask
= MIIM_TYPE
;
825 info
.fType
= MFT_STRING
;
826 info
.cch
= m_title
.length();
827 info
.dwTypeData
= const_cast<wxChar
*>(m_title
.wx_str());
828 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
830 wxLogLastError(wxT("SetMenuItemInfo"));
833 if ( !ModifyMenu(hMenu
, 0u,
834 MF_BYPOSITION
| MF_STRING
,
835 idMenuTitle
, m_title
.wx_str()) )
837 wxLogLastError(wxT("ModifyMenu"));
844 // put the title string in bold face
845 if ( !m_title
.empty() )
847 SetDefaultMenuItem(GetHmenu(), idMenuTitle
);
852 // ---------------------------------------------------------------------------
854 // ---------------------------------------------------------------------------
856 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
858 const int id
= (signed short)id_
;
860 // ignore commands from the menu title
861 if ( id
!= (int)idMenuTitle
)
863 // update the check item when it's clicked
864 wxMenuItem
* const item
= FindItem(id
);
865 if ( item
&& item
->IsCheckable() )
868 // get the status of the menu item: note that it has been just changed
869 // by Toggle() above so here we already get the new state of the item
870 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
871 SendEvent(id
, menuState
& MF_CHECKED
);
877 // ---------------------------------------------------------------------------
879 // ---------------------------------------------------------------------------
881 wxWindow
*wxMenu::GetWindow() const
883 if ( m_invokingWindow
!= NULL
)
884 return m_invokingWindow
;
885 else if ( GetMenuBar() != NULL
)
886 return GetMenuBar()->GetFrame();
891 // ---------------------------------------------------------------------------
893 // ---------------------------------------------------------------------------
895 void wxMenuBar::Init()
897 m_eventHandler
= this;
899 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
902 // Not using a combined wxToolBar/wxMenuBar? then use
903 // a commandbar in WinCE .NET just to implement the
905 #if defined(WINCE_WITH_COMMANDBAR)
907 m_adornmentsAdded
= false;
911 wxMenuBar::wxMenuBar()
916 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
921 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
925 m_titles
.Alloc(count
);
927 for ( size_t i
= 0; i
< count
; i
++ )
929 m_menus
.Append(menus
[i
]);
930 m_titles
.Add(titles
[i
]);
932 menus
[i
]->Attach(this);
936 wxMenuBar::~wxMenuBar()
938 // In Windows CE (not .NET), the menubar is always associated
939 // with a toolbar, which destroys the menu implicitly.
940 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
943 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
945 toolMenuBar
->SetMenuBar(NULL
);
948 // we should free Windows resources only if Windows doesn't do it for us
949 // which happens if we're attached to a frame
950 if (m_hMenu
&& !IsAttached())
952 #if defined(WINCE_WITH_COMMANDBAR)
953 ::DestroyWindow((HWND
) m_commandBar
);
954 m_commandBar
= (WXHWND
) NULL
;
956 ::DestroyMenu((HMENU
)m_hMenu
);
958 m_hMenu
= (WXHMENU
)NULL
;
963 // ---------------------------------------------------------------------------
965 // ---------------------------------------------------------------------------
967 void wxMenuBar::Refresh()
972 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
974 #if defined(WINCE_WITHOUT_COMMANDBAR)
977 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
979 #elif defined(WINCE_WITH_COMMANDBAR)
981 DrawMenuBar((HWND
) m_commandBar
);
983 DrawMenuBar(GetHwndOf(GetFrame()));
987 WXHMENU
wxMenuBar::Create()
989 // Note: this doesn't work at all on Smartphone,
990 // since you have to use resources.
991 // We'll have to find another way to add a menu
992 // by changing/adding menu items to an existing menu.
993 #if defined(WINCE_WITHOUT_COMMANDBAR)
997 wxToolMenuBar
* const bar
= static_cast<wxToolMenuBar
*>(GetToolBar());
1001 HWND hCommandBar
= GetHwndOf(bar
);
1003 // notify comctl32.dll about the version of the headers we use before using
1004 // any other TB_XXX messages
1005 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
1008 wxZeroMemory(tbButton
);
1009 tbButton
.iBitmap
= I_IMAGENONE
;
1010 tbButton
.fsState
= TBSTATE_ENABLED
;
1011 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
1012 TBSTYLE_NO_DROPDOWN_ARROW
|
1015 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
1017 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
1018 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1019 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
1020 tbButton
.iString
= (int) label
.wx_str();
1022 tbButton
.idCommand
= NewControlId();
1023 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
1025 wxLogLastError(wxT("TB_INSERTBUTTON"));
1029 m_hMenu
= bar
->GetHMenu();
1031 #else // !__WXWINCE__
1035 m_hMenu
= (WXHMENU
)::CreateMenu();
1039 wxLogLastError(wxT("CreateMenu"));
1043 size_t count
= GetMenuCount(), i
;
1044 wxMenuList::iterator it
;
1045 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1047 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
1048 (UINT_PTR
)(*it
)->GetHMenu(),
1049 m_titles
[i
].wx_str()) )
1051 wxLogLastError(wxT("AppendMenu"));
1057 #endif // __WXWINCE__/!__WXWINCE__
1060 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
1063 wxASSERT(menu
->GetHMenu());
1066 #if defined(__WXWINCE__)
1067 int totalMSWItems
= GetMenuCount();
1069 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1072 int i
; // For old C++ compatibility
1073 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1075 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1078 for(i
=0; i
<wxpos
; i
++)
1080 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1087 // ---------------------------------------------------------------------------
1088 // wxMenuBar functions to work with the top level submenus
1089 // ---------------------------------------------------------------------------
1091 // NB: we don't support owner drawn top level items for now, if we do these
1092 // functions would have to be changed to use wxMenuItem as well
1094 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1096 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1097 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1099 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1101 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1106 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1108 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1110 m_titles
[pos
] = label
;
1112 if ( !IsAttached() )
1116 //else: have to modify the existing menu
1118 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1121 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1122 if ( flagsOld
== 0xFFFFFFFF )
1124 wxLogLastError(wxT("GetMenuState"));
1129 if ( flagsOld
& MF_POPUP
)
1131 // HIBYTE contains the number of items in the submenu in this case
1133 id
= (UINT_PTR
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1143 info
.cbSize
= sizeof(info
);
1144 info
.fMask
= MIIM_TYPE
;
1145 info
.fType
= MFT_STRING
;
1146 info
.cch
= label
.length();
1147 info
.dwTypeData
= const_cast<wxChar
*>(label
.wx_str());
1148 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, &info
) )
1150 wxLogLastError(wxT("SetMenuItemInfo"));
1154 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1155 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1157 wxLogLastError(wxT("ModifyMenu"));
1164 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1166 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1167 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1169 return m_titles
[pos
];
1172 // ---------------------------------------------------------------------------
1173 // wxMenuBar construction
1174 // ---------------------------------------------------------------------------
1176 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1178 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1182 m_titles
[pos
] = title
;
1184 #if defined(WINCE_WITHOUT_COMMANDBAR)
1190 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1192 // can't use ModifyMenu() because it deletes the submenu it replaces
1193 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1195 wxLogLastError(wxT("RemoveMenu"));
1198 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1199 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1200 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1202 wxLogLastError(wxT("InsertMenu"));
1206 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1208 // need to rebuild accell table
1209 RebuildAccelTable();
1211 #endif // wxUSE_ACCEL
1220 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1222 // Find out which MSW item before which we'll be inserting before
1223 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1224 // If IsAttached() is false this won't be used anyway
1226 #if defined(WINCE_WITHOUT_COMMANDBAR)
1232 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1233 ? -1 // append the menu
1234 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1236 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1239 m_titles
.Insert(title
, pos
);
1243 #if defined(WINCE_WITHOUT_COMMANDBAR)
1247 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1248 tbButton
.iBitmap
= I_IMAGENONE
;
1249 tbButton
.fsState
= TBSTATE_ENABLED
;
1250 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1252 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1253 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1254 wxString label
= wxStripMenuCodes(title
);
1255 tbButton
.iString
= (int) label
.wx_str();
1257 tbButton
.idCommand
= NewControlId();
1258 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1260 wxLogLastError(wxT("TB_INSERTBUTTON"));
1263 wxUnusedVar(mswpos
);
1265 if ( !::InsertMenu(GetHmenu(), mswpos
,
1266 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1267 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1269 wxLogLastError(wxT("InsertMenu"));
1273 if ( menu
->HasAccels() )
1275 // need to rebuild accell table
1276 RebuildAccelTable();
1278 #endif // wxUSE_ACCEL
1287 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1289 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1290 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1292 if ( !wxMenuBarBase::Append(menu
, title
) )
1295 m_titles
.Add(title
);
1297 #if defined(WINCE_WITHOUT_COMMANDBAR)
1303 #if defined(WINCE_WITHOUT_COMMANDBAR)
1307 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1308 tbButton
.iBitmap
= I_IMAGENONE
;
1309 tbButton
.fsState
= TBSTATE_ENABLED
;
1310 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1312 size_t pos
= GetMenuCount();
1313 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1314 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1315 wxString label
= wxStripMenuCodes(title
);
1316 tbButton
.iString
= (int) label
.wx_str();
1318 tbButton
.idCommand
= NewControlId();
1319 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1321 wxLogLastError(wxT("TB_INSERTBUTTON"));
1325 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1326 (UINT_PTR
)submenu
, title
.wx_str()) )
1328 wxLogLastError(wxT("AppendMenu"));
1333 if ( menu
->HasAccels() )
1335 // need to rebuild accelerator table
1336 RebuildAccelTable();
1338 #endif // wxUSE_ACCEL
1347 wxMenu
*wxMenuBar::Remove(size_t pos
)
1349 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1353 #if defined(WINCE_WITHOUT_COMMANDBAR)
1359 #if defined(WINCE_WITHOUT_COMMANDBAR)
1362 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1364 wxLogLastError(wxT("TB_DELETEBUTTON"));
1368 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1370 wxLogLastError(wxT("RemoveMenu"));
1375 if ( menu
->HasAccels() )
1377 // need to rebuild accell table
1378 RebuildAccelTable();
1380 #endif // wxUSE_ACCEL
1386 m_titles
.RemoveAt(pos
);
1393 void wxMenuBar::RebuildAccelTable()
1395 // merge the accelerators of all menus into one accel table
1396 size_t nAccelCount
= 0;
1397 size_t i
, count
= GetMenuCount();
1398 wxMenuList::iterator it
;
1399 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1401 nAccelCount
+= (*it
)->GetAccelCount();
1406 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1409 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1411 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1414 SetAcceleratorTable(wxAcceleratorTable(nAccelCount
, accelEntries
));
1416 delete [] accelEntries
;
1420 #endif // wxUSE_ACCEL
1422 void wxMenuBar::Attach(wxFrame
*frame
)
1424 wxMenuBarBase::Attach(frame
);
1426 #if defined(WINCE_WITH_COMMANDBAR)
1430 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1435 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1437 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1444 RebuildAccelTable();
1445 #endif // wxUSE_ACCEL
1448 #if defined(WINCE_WITH_COMMANDBAR)
1449 bool wxMenuBar::AddAdornments(long style
)
1451 if (m_adornmentsAdded
|| !m_commandBar
)
1454 if (style
& wxCLOSE_BOX
)
1456 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1458 wxLogLastError(wxT("CommandBar_AddAdornments"));
1469 void wxMenuBar::Detach()
1471 wxMenuBarBase::Detach();
1474 #endif // wxUSE_MENUS