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"
39 #include "wx/ownerdrw.h"
42 #include "wx/ptr_scpd.h"
44 #include "wx/msw/private.h"
45 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
53 #if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__)
57 #include "wx/msw/wince/missing.h"
61 // other standard headers
64 //VC6 needs these defining, though they are in winuser.h
66 #define MIIM_STRING 0x00000040
67 #define MIIM_BITMAP 0x00000080
68 #define MIIM_FTYPE 0x00000100
69 #define HBMMENU_CALLBACK ((HBITMAP) -1)
70 typedef struct tagMENUINFO
77 DWORD dwContextHelpID
;
79 } MENUINFO
, FAR
*LPMENUINFO
;
83 #include "wx/dynlib.h"
86 #ifndef MNS_CHECKORBMP
87 #define MNS_CHECKORBMP 0x04000000
90 #define MIM_STYLE 0x00000010
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 // the (popup) menu title has this special id
102 static const UINT idMenuTitle
= (UINT
)-3;
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
111 // make the given menu item default
112 void SetDefaultMenuItem(HMENU
WXUNUSED_IN_WINCE(hmenu
),
113 UINT
WXUNUSED_IN_WINCE(id
))
118 mii
.cbSize
= sizeof(MENUITEMINFO
);
119 mii
.fMask
= MIIM_STATE
;
120 mii
.fState
= MFS_DEFAULT
;
122 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
124 wxLogLastError(wxT("SetMenuItemInfo"));
130 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
134 info
.cbSize
= sizeof(info
);
135 info
.fMask
= MIIM_STATE
;
136 // MF_BYCOMMAND is zero so test MF_BYPOSITION
137 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
138 wxLogLastError(wxT("GetMenuItemInfo"));
141 #endif // __WXWINCE__
143 bool IsLessThanStdSize(const wxBitmap
& bmp
)
145 // FIXME: these +4 are chosen so that 16*16 bitmaps pass this test with
146 // default SM_CXMENUCHECK value but I have no idea what do we really
148 return bmp
.GetWidth() < ::GetSystemMetrics(SM_CXMENUCHECK
) + 4 &&
149 bmp
.GetHeight() < ::GetSystemMetrics(SM_CYMENUCHECK
) + 4;
152 } // anonymous namespace
154 // ============================================================================
156 // ============================================================================
158 #include "wx/listimpl.cpp"
160 WX_DEFINE_LIST( wxMenuInfoList
)
162 #if wxUSE_EXTENDED_RTTI
164 WX_DEFINE_FLAGS( wxMenuStyle
)
166 wxBEGIN_FLAGS( wxMenuStyle
)
167 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
168 wxEND_FLAGS( wxMenuStyle
)
170 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
172 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
174 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
176 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
179 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
180 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
181 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
182 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
183 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
184 wxEND_PROPERTIES_TABLE()
186 wxBEGIN_HANDLERS_TABLE(wxMenu
)
187 wxEND_HANDLERS_TABLE()
189 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
191 WX_DEFINE_FLAGS( wxMenuBarStyle
)
193 wxBEGIN_FLAGS( wxMenuBarStyle
)
194 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
195 wxEND_FLAGS( wxMenuBarStyle
)
197 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
198 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
203 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
205 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
207 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
208 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
209 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
210 wxEND_PROPERTIES_TABLE()
212 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
213 wxEND_HANDLERS_TABLE()
215 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
217 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
219 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
221 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
224 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
225 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
226 wxEND_PROPERTIES_TABLE()
228 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
229 wxEND_HANDLERS_TABLE()
231 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
234 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
235 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
236 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
239 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
241 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
242 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
243 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
245 wxMenuInfo
* info
= new wxMenuInfo() ;
246 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetMenuLabel(i
) ) ;
247 list
->Append( info
) ;
252 // ---------------------------------------------------------------------------
253 // wxMenu construction, adding and removing menu items
254 // ---------------------------------------------------------------------------
256 // Construct a menu with optional title (then use append)
260 m_startRadioGroup
= -1;
263 m_hMenu
= (WXHMENU
)CreatePopupMenu();
266 wxLogLastError(wxT("CreatePopupMenu"));
269 // if we have a title, insert it in the beginning of the menu
270 if ( !m_title
.empty() )
272 Append(idMenuTitle
, m_title
);
277 // The wxWindow destructor will take care of deleting the submenus.
280 // we should free Windows resources only if Windows doesn't do it for us
281 // which happens if we're attached to a menubar or a submenu of another
283 if ( !IsAttached() && !GetParent() )
285 if ( !::DestroyMenu(GetHmenu()) )
287 wxLogLastError(wxT("DestroyMenu"));
293 WX_CLEAR_ARRAY(m_accels
);
294 #endif // wxUSE_ACCEL
299 // this will take effect during the next call to Append()
303 void wxMenu::Attach(wxMenuBarBase
*menubar
)
305 wxMenuBase::Attach(menubar
);
312 int wxMenu::FindAccel(int id
) const
314 size_t n
, count
= m_accels
.GetCount();
315 for ( n
= 0; n
< count
; n
++ )
317 if ( m_accels
[n
]->m_command
== id
)
324 void wxMenu::UpdateAccel(wxMenuItem
*item
)
326 if ( item
->IsSubMenu() )
328 wxMenu
*submenu
= item
->GetSubMenu();
329 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
332 UpdateAccel(node
->GetData());
334 node
= node
->GetNext();
337 else if ( !item
->IsSeparator() )
339 // recurse upwards: we should only modify m_accels of the top level
340 // menus, not of the submenus as wxMenuBar doesn't look at them
341 // (alternative and arguable cleaner solution would be to recurse
342 // downwards in GetAccelCount() and CopyAccels())
345 GetParent()->UpdateAccel(item
);
349 // find the (new) accel for this item
350 wxAcceleratorEntry
*accel
= wxAcceleratorEntry::Create(item
->GetItemLabel());
352 accel
->m_command
= item
->GetId();
355 int n
= FindAccel(item
->GetId());
356 if ( n
== wxNOT_FOUND
)
358 // no old, add new if any
362 return; // skipping RebuildAccelTable() below
366 // replace old with new or just remove the old one if no new
371 m_accels
.RemoveAt(n
);
376 GetMenuBar()->RebuildAccelTable();
379 //else: it is a separator, they can't have accels, nothing to do
382 #endif // wxUSE_ACCEL
384 // append a new item or submenu to the menu
385 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
389 #endif // wxUSE_ACCEL
391 // we should support disabling the item even prior to adding it to the menu
392 UINT flags
= pItem
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
;
394 // if "Break" has just been called, insert a menu break before this item
395 // (and don't forget to reset the flag)
397 flags
|= MF_MENUBREAK
;
401 if ( pItem
->IsSeparator() ) {
402 flags
|= MF_SEPARATOR
;
405 // id is the numeric id for normal menu items and HMENU for submenus as
406 // required by ::AppendMenu() API
408 wxMenu
*submenu
= pItem
->GetSubMenu();
409 if ( submenu
!= NULL
) {
410 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
412 submenu
->SetParent(this);
414 id
= (UINT_PTR
)submenu
->GetHMenu();
419 id
= pItem
->GetMSWId();
423 // prepare to insert the item in the menu
424 wxString itemText
= pItem
->GetItemLabel();
425 LPCTSTR pData
= NULL
;
426 if ( pos
== (size_t)-1 )
428 // append at the end (note that the item is already appended to
429 // internal data structures)
430 pos
= GetMenuItemCount() - 1;
433 // adjust position to account for the title, if any
434 if ( !m_title
.empty() )
435 pos
+= 2; // for the title itself and its separator
439 #if wxUSE_OWNER_DRAWN
440 // Currently, mixing owner-drawn and non-owner-drawn items results in
441 // inconsistent margins, so we force this to be owner-drawn if any other
442 // items already are. Later we might want to use a boolean in the wxMenu
443 // to avoid search. Also we might make this fix unnecessary by getting the correct
444 // margin using NONCLIENTMETRICS.
445 if ( !pItem
->IsOwnerDrawn() && !pItem
->IsSeparator() )
447 // Check if any other items are ownerdrawn, and make ownerdrawn if so
448 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
451 if (node
->GetData()->IsOwnerDrawn())
453 pItem
->SetOwnerDrawn(true);
456 node
= node
->GetNext();
461 // check if we have something more than a simple text item
462 #if wxUSE_OWNER_DRAWN
463 if ( pItem
->IsOwnerDrawn() )
466 // if the item is owner-drawn just because of the [checked] bitmap and
467 // the bitmap uses standard menu bitmap size we can avoid making it
468 // owner-drawn and use built-in support for menu bitmaps instead
469 bool mustUseOwnerDrawn
= pItem
->GetTextColour().Ok() ||
470 pItem
->GetBackgroundColour().Ok() ||
471 pItem
->GetFont().Ok();
472 if ( !mustUseOwnerDrawn
)
474 const wxBitmap
& bmpUnchecked
= pItem
->GetBitmap(false),
475 bmpChecked
= pItem
->GetBitmap(true);
476 if ( (bmpUnchecked
.Ok() && !IsLessThanStdSize(bmpUnchecked
)) ||
477 (bmpChecked
.Ok() && !IsLessThanStdSize(bmpChecked
)) )
479 mustUseOwnerDrawn
= true;
483 // MIIM_BITMAP only works under WinME/2000+
484 if ( !mustUseOwnerDrawn
&& wxGetWinVersion() >= wxWinVersion_98
)
486 // use InsertMenuItem() as it's guaranteed to look correct while
487 // our owner-drawn code is not
488 WinStruct
<MENUITEMINFO
> mii
;
489 mii
.fMask
= MIIM_STRING
| MIIM_DATA
| MIIM_BITMAP
;
490 if ( pItem
->IsCheckable() )
492 // need to set checked/unchecked bitmaps as otherwise our
493 // MSWOnDrawItem() item is not called
494 mii
.fMask
|= MIIM_CHECKMARKS
;
497 mii
.cch
= itemText
.length();
498 mii
.dwTypeData
= const_cast<wxChar
*>(itemText
.wx_str());
500 if (flags
& MF_POPUP
)
502 mii
.fMask
|= MIIM_SUBMENU
;
503 mii
.hSubMenu
= (HMENU
)pItem
->GetSubMenu()->GetHMenu();
507 mii
.fMask
|= MIIM_ID
;
511 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
512 // 1. we can't draw it with transparency then (this is not
513 // very important now but would be with themed menu bg)
514 // 2. worse, Windows inverts the bitmap for the selected
515 // item and this looks downright ugly
517 // so instead draw it ourselves in MSWOnDrawItem()
518 mii
.dwItemData
= reinterpret_cast<ULONG_PTR
>(pItem
);
519 if ( pItem
->IsCheckable() )
522 mii
.hbmpUnchecked
= HBMMENU_CALLBACK
;
524 mii
.hbmpItem
= HBMMENU_CALLBACK
;
526 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
529 wxLogLastError(wxT("InsertMenuItem()"));
531 else // InsertMenuItem() ok
533 // we need to remove the extra indent which is reserved for
534 // the checkboxes by default as it looks ugly unless check
535 // boxes are used together with bitmaps and this is not the
537 WinStruct
<MENUINFO
> mi
;
539 // don't call SetMenuInfo() directly, this would prevent
540 // the app from starting up under Windows 95/NT 4
541 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
543 wxDynamicLibrary
dllUser(_T("user32"));
544 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
545 if ( pfnSetMenuInfo
)
547 mi
.fMask
= MIM_STYLE
;
548 mi
.dwStyle
= MNS_CHECKORBMP
;
549 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
550 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
553 // tell the item that it's not really owner-drawn but only
554 // needs to draw its bitmap, the rest is done by Windows
555 pItem
->ResetOwnerDrawn();
562 // item draws itself, pass pointer to it in data parameter
563 flags
|= MF_OWNERDRAW
;
564 pData
= (LPCTSTR
)pItem
;
568 #endif // wxUSE_OWNER_DRAWN
570 // item is just a normal string (passed in data parameter)
574 itemText
= wxMenuItem::GetLabelText(itemText
);
577 pData
= (wxChar
*)itemText
.wx_str();
580 // item might have already been inserted by InsertMenuItem() above
583 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
585 wxLogLastError(wxT("InsertMenu[Item]()"));
592 // if we just appended the title, highlight it
593 if ( id
== idMenuTitle
)
595 // visually select the menu title
596 SetDefaultMenuItem(GetHmenu(), id
);
599 // if we're already attached to the menubar, we must update it
600 if ( IsAttached() && GetMenuBar()->IsAttached() )
602 GetMenuBar()->Refresh();
608 void wxMenu::EndRadioGroup()
610 // we're not inside a radio group any longer
611 m_startRadioGroup
= -1;
614 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
616 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
620 if ( item
->GetKind() == wxITEM_RADIO
)
622 int count
= GetMenuItemCount();
624 if ( m_startRadioGroup
== -1 )
626 // start a new radio group
627 m_startRadioGroup
= count
;
629 // for now it has just one element
630 item
->SetAsRadioGroupStart();
631 item
->SetRadioGroupEnd(m_startRadioGroup
);
633 // ensure that we have a checked item in the radio group
636 else // extend the current radio group
638 // we need to update its end item
639 item
->SetRadioGroupStart(m_startRadioGroup
);
640 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
644 node
->GetData()->SetRadioGroupEnd(count
);
648 wxFAIL_MSG( _T("where is the radio group start item?") );
652 else // not a radio item
657 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
664 // check the item initially
671 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
673 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
679 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
681 // we need to find the item's position in the child list
683 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
684 for ( pos
= 0; node
; pos
++ )
686 if ( node
->GetData() == item
)
689 node
= node
->GetNext();
692 // DoRemove() (unlike Remove) can only be called for an existing item!
693 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
696 // remove the corresponding accel from the accel table
697 int n
= FindAccel(item
->GetId());
698 if ( n
!= wxNOT_FOUND
)
702 m_accels
.RemoveAt(n
);
704 //else: this item doesn't have an accel, nothing to do
705 #endif // wxUSE_ACCEL
707 // remove the item from the menu
708 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
710 wxLogLastError(wxT("RemoveMenu"));
713 if ( IsAttached() && GetMenuBar()->IsAttached() )
715 // otherwise, the change won't be visible
716 GetMenuBar()->Refresh();
719 // and from internal data structures
720 return wxMenuBase::DoRemove(item
);
723 // ---------------------------------------------------------------------------
724 // accelerator helpers
725 // ---------------------------------------------------------------------------
729 // create the wxAcceleratorEntries for our accels and put them into the provided
730 // array - return the number of accels we have
731 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
733 size_t count
= GetAccelCount();
734 for ( size_t n
= 0; n
< count
; n
++ )
736 *accels
++ = *m_accels
[n
];
742 wxAcceleratorTable
*wxMenu::CreateAccelTable() const
744 const size_t count
= m_accels
.size();
745 wxScopedArray
<wxAcceleratorEntry
> accels(new wxAcceleratorEntry
[count
]);
746 CopyAccels(accels
.get());
748 return new wxAcceleratorTable(count
, accels
.get());
751 #endif // wxUSE_ACCEL
753 // ---------------------------------------------------------------------------
755 // ---------------------------------------------------------------------------
757 void wxMenu::SetTitle(const wxString
& label
)
759 bool hasNoTitle
= m_title
.empty();
762 HMENU hMenu
= GetHmenu();
766 if ( !label
.empty() )
768 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
769 idMenuTitle
, m_title
.wx_str()) ||
770 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
772 wxLogLastError(wxT("InsertMenu"));
780 // remove the title and the separator after it
781 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
782 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
784 wxLogLastError(wxT("RemoveMenu"));
793 info
.cbSize
= sizeof(info
);
794 info
.fMask
= MIIM_TYPE
;
795 info
.fType
= MFT_STRING
;
796 info
.cch
= m_title
.length();
797 info
.dwTypeData
= const_cast<wxChar
*>(m_title
.wx_str());
798 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
800 wxLogLastError(wxT("SetMenuItemInfo"));
803 if ( !ModifyMenu(hMenu
, 0u,
804 MF_BYPOSITION
| MF_STRING
,
805 idMenuTitle
, m_title
.wx_str()) )
807 wxLogLastError(wxT("ModifyMenu"));
814 // put the title string in bold face
815 if ( !m_title
.empty() )
817 SetDefaultMenuItem(GetHmenu(), idMenuTitle
);
822 // ---------------------------------------------------------------------------
824 // ---------------------------------------------------------------------------
826 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
828 const int id
= (signed short)id_
;
830 // ignore commands from the menu title
831 if ( id
!= (int)idMenuTitle
)
833 // update the check item when it's clicked
834 wxMenuItem
* const item
= FindItem(id
);
835 if ( item
&& item
->IsCheckable() )
838 // get the status of the menu item: note that it has been just changed
839 // by Toggle() above so here we already get the new state of the item
840 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
841 SendEvent(id
, menuState
& MF_CHECKED
);
847 // ---------------------------------------------------------------------------
849 // ---------------------------------------------------------------------------
851 wxWindow
*wxMenu::GetWindow() const
853 if ( m_invokingWindow
!= NULL
)
854 return m_invokingWindow
;
855 else if ( GetMenuBar() != NULL
)
856 return GetMenuBar()->GetFrame();
861 // ---------------------------------------------------------------------------
863 // ---------------------------------------------------------------------------
865 void wxMenuBar::Init()
867 m_eventHandler
= this;
869 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
872 // Not using a combined wxToolBar/wxMenuBar? then use
873 // a commandbar in WinCE .NET just to implement the
875 #if defined(WINCE_WITH_COMMANDBAR)
877 m_adornmentsAdded
= false;
881 wxMenuBar::wxMenuBar()
886 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
891 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
895 m_titles
.Alloc(count
);
897 for ( size_t i
= 0; i
< count
; i
++ )
899 m_menus
.Append(menus
[i
]);
900 m_titles
.Add(titles
[i
]);
902 menus
[i
]->Attach(this);
906 wxMenuBar::~wxMenuBar()
908 // In Windows CE (not .NET), the menubar is always associated
909 // with a toolbar, which destroys the menu implicitly.
910 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
913 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
915 toolMenuBar
->SetMenuBar(NULL
);
918 // we should free Windows resources only if Windows doesn't do it for us
919 // which happens if we're attached to a frame
920 if (m_hMenu
&& !IsAttached())
922 #if defined(WINCE_WITH_COMMANDBAR)
923 ::DestroyWindow((HWND
) m_commandBar
);
924 m_commandBar
= (WXHWND
) NULL
;
926 ::DestroyMenu((HMENU
)m_hMenu
);
928 m_hMenu
= (WXHMENU
)NULL
;
933 // ---------------------------------------------------------------------------
935 // ---------------------------------------------------------------------------
937 void wxMenuBar::Refresh()
942 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
944 #if defined(WINCE_WITHOUT_COMMANDBAR)
947 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
949 #elif defined(WINCE_WITH_COMMANDBAR)
951 DrawMenuBar((HWND
) m_commandBar
);
953 DrawMenuBar(GetHwndOf(GetFrame()));
957 WXHMENU
wxMenuBar::Create()
959 // Note: this doesn't work at all on Smartphone,
960 // since you have to use resources.
961 // We'll have to find another way to add a menu
962 // by changing/adding menu items to an existing menu.
963 #if defined(WINCE_WITHOUT_COMMANDBAR)
967 wxToolMenuBar
* const bar
= static_cast<wxToolMenuBar
*>(GetToolBar());
971 HWND hCommandBar
= GetHwndOf(bar
);
973 // notify comctl32.dll about the version of the headers we use before using
974 // any other TB_XXX messages
975 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
978 wxZeroMemory(tbButton
);
979 tbButton
.iBitmap
= I_IMAGENONE
;
980 tbButton
.fsState
= TBSTATE_ENABLED
;
981 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
982 TBSTYLE_NO_DROPDOWN_ARROW
|
985 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
987 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
988 tbButton
.dwData
= (DWORD
)hPopupMenu
;
989 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
990 tbButton
.iString
= (int) label
.wx_str();
992 tbButton
.idCommand
= NewControlId();
993 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
995 wxLogLastError(wxT("TB_INSERTBUTTON"));
999 m_hMenu
= bar
->GetHMenu();
1001 #else // !__WXWINCE__
1005 m_hMenu
= (WXHMENU
)::CreateMenu();
1009 wxLogLastError(wxT("CreateMenu"));
1013 size_t count
= GetMenuCount(), i
;
1014 wxMenuList::iterator it
;
1015 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1017 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
1018 (UINT_PTR
)(*it
)->GetHMenu(),
1019 m_titles
[i
].wx_str()) )
1021 wxLogLastError(wxT("AppendMenu"));
1027 #endif // __WXWINCE__/!__WXWINCE__
1030 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
1033 wxASSERT(menu
->GetHMenu());
1036 #if defined(__WXWINCE__)
1037 int totalMSWItems
= GetMenuCount();
1039 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1042 int i
; // For old C++ compatibility
1043 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1045 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1048 for(i
=0; i
<wxpos
; i
++)
1050 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1057 // ---------------------------------------------------------------------------
1058 // wxMenuBar functions to work with the top level submenus
1059 // ---------------------------------------------------------------------------
1061 // NB: we don't support owner drawn top level items for now, if we do these
1062 // functions would have to be changed to use wxMenuItem as well
1064 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1066 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1067 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1069 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1071 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1076 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1078 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1080 m_titles
[pos
] = label
;
1082 if ( !IsAttached() )
1086 //else: have to modify the existing menu
1088 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1091 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1092 if ( flagsOld
== 0xFFFFFFFF )
1094 wxLogLastError(wxT("GetMenuState"));
1099 if ( flagsOld
& MF_POPUP
)
1101 // HIBYTE contains the number of items in the submenu in this case
1103 id
= (UINT_PTR
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1113 info
.cbSize
= sizeof(info
);
1114 info
.fMask
= MIIM_TYPE
;
1115 info
.fType
= MFT_STRING
;
1116 info
.cch
= label
.length();
1117 info
.dwTypeData
= const_cast<wxChar
*>(label
.wx_str());
1118 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, &info
) )
1120 wxLogLastError(wxT("SetMenuItemInfo"));
1124 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1125 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1127 wxLogLastError(wxT("ModifyMenu"));
1134 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1136 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1137 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1139 return m_titles
[pos
];
1142 // ---------------------------------------------------------------------------
1143 // wxMenuBar construction
1144 // ---------------------------------------------------------------------------
1146 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1148 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1152 m_titles
[pos
] = title
;
1154 #if defined(WINCE_WITHOUT_COMMANDBAR)
1160 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1162 // can't use ModifyMenu() because it deletes the submenu it replaces
1163 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1165 wxLogLastError(wxT("RemoveMenu"));
1168 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1169 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1170 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1172 wxLogLastError(wxT("InsertMenu"));
1176 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1178 // need to rebuild accell table
1179 RebuildAccelTable();
1181 #endif // wxUSE_ACCEL
1190 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1192 // Find out which MSW item before which we'll be inserting before
1193 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1194 // If IsAttached() is false this won't be used anyway
1196 #if defined(WINCE_WITHOUT_COMMANDBAR)
1202 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1203 ? -1 // append the menu
1204 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1206 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1209 m_titles
.Insert(title
, pos
);
1213 #if defined(WINCE_WITHOUT_COMMANDBAR)
1217 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1218 tbButton
.iBitmap
= I_IMAGENONE
;
1219 tbButton
.fsState
= TBSTATE_ENABLED
;
1220 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1222 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1223 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1224 wxString label
= wxStripMenuCodes(title
);
1225 tbButton
.iString
= (int) label
.wx_str();
1227 tbButton
.idCommand
= NewControlId();
1228 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1230 wxLogLastError(wxT("TB_INSERTBUTTON"));
1233 wxUnusedVar(mswpos
);
1235 if ( !::InsertMenu(GetHmenu(), mswpos
,
1236 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1237 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1239 wxLogLastError(wxT("InsertMenu"));
1243 if ( menu
->HasAccels() )
1245 // need to rebuild accell table
1246 RebuildAccelTable();
1248 #endif // wxUSE_ACCEL
1257 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1259 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1260 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1262 if ( !wxMenuBarBase::Append(menu
, title
) )
1265 m_titles
.Add(title
);
1267 #if defined(WINCE_WITHOUT_COMMANDBAR)
1273 #if defined(WINCE_WITHOUT_COMMANDBAR)
1277 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1278 tbButton
.iBitmap
= I_IMAGENONE
;
1279 tbButton
.fsState
= TBSTATE_ENABLED
;
1280 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1282 size_t pos
= GetMenuCount();
1283 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1284 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1285 wxString label
= wxStripMenuCodes(title
);
1286 tbButton
.iString
= (int) label
.wx_str();
1288 tbButton
.idCommand
= NewControlId();
1289 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1291 wxLogLastError(wxT("TB_INSERTBUTTON"));
1295 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1296 (UINT_PTR
)submenu
, title
.wx_str()) )
1298 wxLogLastError(wxT("AppendMenu"));
1303 if ( menu
->HasAccels() )
1305 // need to rebuild accelerator table
1306 RebuildAccelTable();
1308 #endif // wxUSE_ACCEL
1317 wxMenu
*wxMenuBar::Remove(size_t pos
)
1319 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1323 #if defined(WINCE_WITHOUT_COMMANDBAR)
1329 #if defined(WINCE_WITHOUT_COMMANDBAR)
1332 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1334 wxLogLastError(wxT("TB_DELETEBUTTON"));
1338 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1340 wxLogLastError(wxT("RemoveMenu"));
1345 if ( menu
->HasAccels() )
1347 // need to rebuild accell table
1348 RebuildAccelTable();
1350 #endif // wxUSE_ACCEL
1356 m_titles
.RemoveAt(pos
);
1363 void wxMenuBar::RebuildAccelTable()
1365 // merge the accelerators of all menus into one accel table
1366 size_t nAccelCount
= 0;
1367 size_t i
, count
= GetMenuCount();
1368 wxMenuList::iterator it
;
1369 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1371 nAccelCount
+= (*it
)->GetAccelCount();
1376 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1379 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1381 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1384 SetAcceleratorTable(wxAcceleratorTable(nAccelCount
, accelEntries
));
1386 delete [] accelEntries
;
1390 #endif // wxUSE_ACCEL
1392 void wxMenuBar::Attach(wxFrame
*frame
)
1394 wxMenuBarBase::Attach(frame
);
1396 #if defined(WINCE_WITH_COMMANDBAR)
1400 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1405 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1407 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1414 RebuildAccelTable();
1415 #endif // wxUSE_ACCEL
1418 #if defined(WINCE_WITH_COMMANDBAR)
1419 bool wxMenuBar::AddAdornments(long style
)
1421 if (m_adornmentsAdded
|| !m_commandBar
)
1424 if (style
& wxCLOSE_BOX
)
1426 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1427 wxLogLastError(wxT("CommandBar_AddAdornments"));
1435 void wxMenuBar::Detach()
1437 wxMenuBarBase::Detach();
1440 #endif // wxUSE_MENUS