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/scopedarray.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
65 #include "wx/dynlib.h"
68 #ifndef MNS_CHECKORBMP
69 #define MNS_CHECKORBMP 0x04000000
72 #define MIM_STYLE 0x00000010
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 // the (popup) menu title has this special id
84 static const UINT idMenuTitle
= (UINT
)-3;
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
93 // make the given menu item default
94 void SetDefaultMenuItem(HMENU
WXUNUSED_IN_WINCE(hmenu
),
95 UINT
WXUNUSED_IN_WINCE(id
))
100 mii
.cbSize
= sizeof(MENUITEMINFO
);
101 mii
.fMask
= MIIM_STATE
;
102 mii
.fState
= MFS_DEFAULT
;
104 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
106 wxLogLastError(wxT("SetMenuItemInfo"));
112 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
116 info
.cbSize
= sizeof(info
);
117 info
.fMask
= MIIM_STATE
;
118 // MF_BYCOMMAND is zero so test MF_BYPOSITION
119 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
120 wxLogLastError(wxT("GetMenuItemInfo"));
123 #endif // __WXWINCE__
125 bool IsLessThanStdSize(const wxBitmap
& bmp
)
127 // FIXME: these +4 are chosen so that 16*16 bitmaps pass this test with
128 // default SM_CXMENUCHECK value but I have no idea what do we really
130 return bmp
.GetWidth() < ::GetSystemMetrics(SM_CXMENUCHECK
) + 4 &&
131 bmp
.GetHeight() < ::GetSystemMetrics(SM_CYMENUCHECK
) + 4;
134 } // anonymous namespace
136 // ============================================================================
138 // ============================================================================
140 #include "wx/listimpl.cpp"
142 WX_DEFINE_LIST( wxMenuInfoList
)
144 #if wxUSE_EXTENDED_RTTI
146 WX_DEFINE_FLAGS( wxMenuStyle
)
148 wxBEGIN_FLAGS( wxMenuStyle
)
149 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
150 wxEND_FLAGS( wxMenuStyle
)
152 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
154 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
156 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
158 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
161 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
162 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
163 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
164 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
165 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
166 wxEND_PROPERTIES_TABLE()
168 wxBEGIN_HANDLERS_TABLE(wxMenu
)
169 wxEND_HANDLERS_TABLE()
171 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
173 WX_DEFINE_FLAGS( wxMenuBarStyle
)
175 wxBEGIN_FLAGS( wxMenuBarStyle
)
176 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
177 wxEND_FLAGS( wxMenuBarStyle
)
179 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
180 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
185 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
187 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
189 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
190 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
191 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
192 wxEND_PROPERTIES_TABLE()
194 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
195 wxEND_HANDLERS_TABLE()
197 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
199 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
201 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
203 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
206 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
207 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
208 wxEND_PROPERTIES_TABLE()
210 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
211 wxEND_HANDLERS_TABLE()
213 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
216 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
217 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
218 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
221 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
223 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
224 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
225 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
227 wxMenuInfo
* info
= new wxMenuInfo() ;
228 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetMenuLabel(i
) ) ;
229 list
->Append( info
) ;
234 // ---------------------------------------------------------------------------
235 // wxMenu construction, adding and removing menu items
236 // ---------------------------------------------------------------------------
238 // Construct a menu with optional title (then use append)
242 m_startRadioGroup
= -1;
245 m_hMenu
= (WXHMENU
)CreatePopupMenu();
248 wxLogLastError(wxT("CreatePopupMenu"));
251 // if we have a title, insert it in the beginning of the menu
252 if ( !m_title
.empty() )
254 Append(idMenuTitle
, m_title
);
259 // The wxWindow destructor will take care of deleting the submenus.
262 // we should free Windows resources only if Windows doesn't do it for us
263 // which happens if we're attached to a menubar or a submenu of another
265 if ( !IsAttached() && !GetParent() )
267 if ( !::DestroyMenu(GetHmenu()) )
269 wxLogLastError(wxT("DestroyMenu"));
275 WX_CLEAR_ARRAY(m_accels
);
276 #endif // wxUSE_ACCEL
281 // this will take effect during the next call to Append()
285 void wxMenu::Attach(wxMenuBarBase
*menubar
)
287 wxMenuBase::Attach(menubar
);
294 int wxMenu::FindAccel(int id
) const
296 size_t n
, count
= m_accels
.GetCount();
297 for ( n
= 0; n
< count
; n
++ )
299 if ( m_accels
[n
]->m_command
== id
)
306 void wxMenu::UpdateAccel(wxMenuItem
*item
)
308 if ( item
->IsSubMenu() )
310 wxMenu
*submenu
= item
->GetSubMenu();
311 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
314 UpdateAccel(node
->GetData());
316 node
= node
->GetNext();
319 else if ( !item
->IsSeparator() )
321 // recurse upwards: we should only modify m_accels of the top level
322 // menus, not of the submenus as wxMenuBar doesn't look at them
323 // (alternative and arguable cleaner solution would be to recurse
324 // downwards in GetAccelCount() and CopyAccels())
327 GetParent()->UpdateAccel(item
);
331 // find the (new) accel for this item
332 wxAcceleratorEntry
*accel
= wxAcceleratorEntry::Create(item
->GetItemLabel());
334 accel
->m_command
= item
->GetId();
337 int n
= FindAccel(item
->GetId());
338 if ( n
== wxNOT_FOUND
)
340 // no old, add new if any
344 return; // skipping RebuildAccelTable() below
348 // replace old with new or just remove the old one if no new
353 m_accels
.RemoveAt(n
);
358 GetMenuBar()->RebuildAccelTable();
361 //else: it is a separator, they can't have accels, nothing to do
364 #endif // wxUSE_ACCEL
366 // append a new item or submenu to the menu
367 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
371 #endif // wxUSE_ACCEL
373 // we should support disabling the item even prior to adding it to the menu
374 UINT flags
= pItem
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
;
376 // if "Break" has just been called, insert a menu break before this item
377 // (and don't forget to reset the flag)
379 flags
|= MF_MENUBREAK
;
383 if ( pItem
->IsSeparator() ) {
384 flags
|= MF_SEPARATOR
;
387 // id is the numeric id for normal menu items and HMENU for submenus as
388 // required by ::AppendMenu() API
390 wxMenu
*submenu
= pItem
->GetSubMenu();
391 if ( submenu
!= NULL
) {
392 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
394 submenu
->SetParent(this);
396 id
= (UINT_PTR
)submenu
->GetHMenu();
401 id
= pItem
->GetMSWId();
405 // prepare to insert the item in the menu
406 wxString itemText
= pItem
->GetItemLabel();
407 LPCTSTR pData
= NULL
;
408 if ( pos
== (size_t)-1 )
410 // append at the end (note that the item is already appended to
411 // internal data structures)
412 pos
= GetMenuItemCount() - 1;
415 // adjust position to account for the title, if any
416 if ( !m_title
.empty() )
417 pos
+= 2; // for the title itself and its separator
421 #if wxUSE_OWNER_DRAWN
422 // Currently, mixing owner-drawn and non-owner-drawn items results in
423 // inconsistent margins, so we force this to be owner-drawn if any other
424 // items already are. Later we might want to use a boolean in the wxMenu
425 // to avoid search. Also we might make this fix unnecessary by getting the correct
426 // margin using NONCLIENTMETRICS.
427 if ( !pItem
->IsOwnerDrawn() && !pItem
->IsSeparator() )
429 // Check if any other items are ownerdrawn, and make ownerdrawn if so
430 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
433 if (node
->GetData()->IsOwnerDrawn())
435 pItem
->SetOwnerDrawn(true);
438 node
= node
->GetNext();
443 // check if we have something more than a simple text item
444 #if wxUSE_OWNER_DRAWN
445 if ( pItem
->IsOwnerDrawn() )
448 // if the item is owner-drawn just because of the [checked] bitmap and
449 // the bitmap uses standard menu bitmap size we can avoid making it
450 // owner-drawn and use built-in support for menu bitmaps instead
451 bool mustUseOwnerDrawn
= pItem
->GetTextColour().Ok() ||
452 pItem
->GetBackgroundColour().Ok() ||
453 pItem
->GetFont().Ok();
454 if ( !mustUseOwnerDrawn
)
456 const wxBitmap
& bmpUnchecked
= pItem
->GetBitmap(false),
457 bmpChecked
= pItem
->GetBitmap(true);
458 if ( (bmpUnchecked
.Ok() && !IsLessThanStdSize(bmpUnchecked
)) ||
459 (bmpChecked
.Ok() && !IsLessThanStdSize(bmpChecked
)) )
461 mustUseOwnerDrawn
= true;
465 // MIIM_BITMAP only works under WinME/2000+
466 if ( !mustUseOwnerDrawn
&& wxGetWinVersion() >= wxWinVersion_98
)
468 // use InsertMenuItem() as it's guaranteed to look correct while
469 // our owner-drawn code is not
470 WinStruct
<MENUITEMINFO
> mii
;
471 mii
.fMask
= MIIM_STRING
| MIIM_DATA
| MIIM_BITMAP
;
472 if ( pItem
->IsCheckable() )
474 // need to set checked/unchecked bitmaps as otherwise our
475 // MSWOnDrawItem() item is not called
476 mii
.fMask
|= MIIM_CHECKMARKS
;
479 mii
.cch
= itemText
.length();
480 mii
.dwTypeData
= const_cast<wxChar
*>(itemText
.wx_str());
482 if (flags
& MF_POPUP
)
484 mii
.fMask
|= MIIM_SUBMENU
;
485 mii
.hSubMenu
= (HMENU
)pItem
->GetSubMenu()->GetHMenu();
489 mii
.fMask
|= MIIM_ID
;
493 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
494 // 1. we can't draw it with transparency then (this is not
495 // very important now but would be with themed menu bg)
496 // 2. worse, Windows inverts the bitmap for the selected
497 // item and this looks downright ugly
499 // so instead draw it ourselves in MSWOnDrawItem()
500 mii
.dwItemData
= reinterpret_cast<ULONG_PTR
>(pItem
);
501 if ( pItem
->IsCheckable() )
504 mii
.hbmpUnchecked
= HBMMENU_CALLBACK
;
506 mii
.hbmpItem
= HBMMENU_CALLBACK
;
508 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
511 wxLogLastError(wxT("InsertMenuItem()"));
513 else // InsertMenuItem() ok
515 // we need to remove the extra indent which is reserved for
516 // the checkboxes by default as it looks ugly unless check
517 // boxes are used together with bitmaps and this is not the
519 WinStruct
<MENUINFO
> mi
;
521 // don't call SetMenuInfo() directly, this would prevent
522 // the app from starting up under Windows 95/NT 4
523 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
525 wxDynamicLibrary
dllUser(_T("user32"));
526 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
527 if ( pfnSetMenuInfo
)
529 mi
.fMask
= MIM_STYLE
;
530 mi
.dwStyle
= MNS_CHECKORBMP
;
531 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
532 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
535 // tell the item that it's not really owner-drawn but only
536 // needs to draw its bitmap, the rest is done by Windows
537 pItem
->ResetOwnerDrawn();
544 // item draws itself, pass pointer to it in data parameter
545 flags
|= MF_OWNERDRAW
;
546 pData
= (LPCTSTR
)pItem
;
550 #endif // wxUSE_OWNER_DRAWN
552 // item is just a normal string (passed in data parameter)
556 itemText
= wxMenuItem::GetLabelText(itemText
);
559 pData
= (wxChar
*)itemText
.wx_str();
562 // item might have already been inserted by InsertMenuItem() above
565 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
567 wxLogLastError(wxT("InsertMenu[Item]()"));
574 // if we just appended the title, highlight it
575 if ( id
== idMenuTitle
)
577 // visually select the menu title
578 SetDefaultMenuItem(GetHmenu(), id
);
581 // if we're already attached to the menubar, we must update it
582 if ( IsAttached() && GetMenuBar()->IsAttached() )
584 GetMenuBar()->Refresh();
590 void wxMenu::EndRadioGroup()
592 // we're not inside a radio group any longer
593 m_startRadioGroup
= -1;
596 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
598 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
602 if ( item
->GetKind() == wxITEM_RADIO
)
604 int count
= GetMenuItemCount();
606 if ( m_startRadioGroup
== -1 )
608 // start a new radio group
609 m_startRadioGroup
= count
;
611 // for now it has just one element
612 item
->SetAsRadioGroupStart();
613 item
->SetRadioGroupEnd(m_startRadioGroup
);
615 // ensure that we have a checked item in the radio group
618 else // extend the current radio group
620 // we need to update its end item
621 item
->SetRadioGroupStart(m_startRadioGroup
);
622 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
626 node
->GetData()->SetRadioGroupEnd(count
);
630 wxFAIL_MSG( _T("where is the radio group start item?") );
634 else // not a radio item
639 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
646 // check the item initially
653 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
655 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
661 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
663 // we need to find the item's position in the child list
665 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
666 for ( pos
= 0; node
; pos
++ )
668 if ( node
->GetData() == item
)
671 node
= node
->GetNext();
674 // DoRemove() (unlike Remove) can only be called for an existing item!
675 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
678 // remove the corresponding accel from the accel table
679 int n
= FindAccel(item
->GetId());
680 if ( n
!= wxNOT_FOUND
)
684 m_accels
.RemoveAt(n
);
686 //else: this item doesn't have an accel, nothing to do
687 #endif // wxUSE_ACCEL
689 // remove the item from the menu
690 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
692 wxLogLastError(wxT("RemoveMenu"));
695 if ( IsAttached() && GetMenuBar()->IsAttached() )
697 // otherwise, the change won't be visible
698 GetMenuBar()->Refresh();
701 // and from internal data structures
702 return wxMenuBase::DoRemove(item
);
705 // ---------------------------------------------------------------------------
706 // accelerator helpers
707 // ---------------------------------------------------------------------------
711 // create the wxAcceleratorEntries for our accels and put them into the provided
712 // array - return the number of accels we have
713 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
715 size_t count
= GetAccelCount();
716 for ( size_t n
= 0; n
< count
; n
++ )
718 *accels
++ = *m_accels
[n
];
724 wxAcceleratorTable
*wxMenu::CreateAccelTable() const
726 const size_t count
= m_accels
.size();
727 wxScopedArray
<wxAcceleratorEntry
> accels(new wxAcceleratorEntry
[count
]);
728 CopyAccels(accels
.get());
730 return new wxAcceleratorTable(count
, accels
.get());
733 #endif // wxUSE_ACCEL
735 // ---------------------------------------------------------------------------
737 // ---------------------------------------------------------------------------
739 void wxMenu::SetTitle(const wxString
& label
)
741 bool hasNoTitle
= m_title
.empty();
744 HMENU hMenu
= GetHmenu();
748 if ( !label
.empty() )
750 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
751 idMenuTitle
, m_title
.wx_str()) ||
752 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
754 wxLogLastError(wxT("InsertMenu"));
762 // remove the title and the separator after it
763 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
764 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
766 wxLogLastError(wxT("RemoveMenu"));
775 info
.cbSize
= sizeof(info
);
776 info
.fMask
= MIIM_TYPE
;
777 info
.fType
= MFT_STRING
;
778 info
.cch
= m_title
.length();
779 info
.dwTypeData
= const_cast<wxChar
*>(m_title
.wx_str());
780 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
782 wxLogLastError(wxT("SetMenuItemInfo"));
785 if ( !ModifyMenu(hMenu
, 0u,
786 MF_BYPOSITION
| MF_STRING
,
787 idMenuTitle
, m_title
.wx_str()) )
789 wxLogLastError(wxT("ModifyMenu"));
796 // put the title string in bold face
797 if ( !m_title
.empty() )
799 SetDefaultMenuItem(GetHmenu(), idMenuTitle
);
804 // ---------------------------------------------------------------------------
806 // ---------------------------------------------------------------------------
808 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
810 const int id
= (signed short)id_
;
812 // ignore commands from the menu title
813 if ( id
!= (int)idMenuTitle
)
815 // update the check item when it's clicked
816 wxMenuItem
* const item
= FindItem(id
);
817 if ( item
&& item
->IsCheckable() )
820 // get the status of the menu item: note that it has been just changed
821 // by Toggle() above so here we already get the new state of the item
822 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
823 SendEvent(id
, menuState
& MF_CHECKED
);
829 // ---------------------------------------------------------------------------
831 // ---------------------------------------------------------------------------
833 wxWindow
*wxMenu::GetWindow() const
835 if ( m_invokingWindow
!= NULL
)
836 return m_invokingWindow
;
837 else if ( GetMenuBar() != NULL
)
838 return GetMenuBar()->GetFrame();
843 // ---------------------------------------------------------------------------
845 // ---------------------------------------------------------------------------
847 void wxMenuBar::Init()
849 m_eventHandler
= this;
851 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
854 // Not using a combined wxToolBar/wxMenuBar? then use
855 // a commandbar in WinCE .NET just to implement the
857 #if defined(WINCE_WITH_COMMANDBAR)
859 m_adornmentsAdded
= false;
863 wxMenuBar::wxMenuBar()
868 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
873 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
877 m_titles
.Alloc(count
);
879 for ( size_t i
= 0; i
< count
; i
++ )
881 m_menus
.Append(menus
[i
]);
882 m_titles
.Add(titles
[i
]);
884 menus
[i
]->Attach(this);
888 wxMenuBar::~wxMenuBar()
890 // In Windows CE (not .NET), the menubar is always associated
891 // with a toolbar, which destroys the menu implicitly.
892 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
895 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
897 toolMenuBar
->SetMenuBar(NULL
);
900 // we should free Windows resources only if Windows doesn't do it for us
901 // which happens if we're attached to a frame
902 if (m_hMenu
&& !IsAttached())
904 #if defined(WINCE_WITH_COMMANDBAR)
905 ::DestroyWindow((HWND
) m_commandBar
);
906 m_commandBar
= (WXHWND
) NULL
;
908 ::DestroyMenu((HMENU
)m_hMenu
);
910 m_hMenu
= (WXHMENU
)NULL
;
915 // ---------------------------------------------------------------------------
917 // ---------------------------------------------------------------------------
919 void wxMenuBar::Refresh()
924 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
926 #if defined(WINCE_WITHOUT_COMMANDBAR)
929 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
931 #elif defined(WINCE_WITH_COMMANDBAR)
933 DrawMenuBar((HWND
) m_commandBar
);
935 DrawMenuBar(GetHwndOf(GetFrame()));
939 WXHMENU
wxMenuBar::Create()
941 // Note: this doesn't work at all on Smartphone,
942 // since you have to use resources.
943 // We'll have to find another way to add a menu
944 // by changing/adding menu items to an existing menu.
945 #if defined(WINCE_WITHOUT_COMMANDBAR)
949 wxToolMenuBar
* const bar
= static_cast<wxToolMenuBar
*>(GetToolBar());
953 HWND hCommandBar
= GetHwndOf(bar
);
955 // notify comctl32.dll about the version of the headers we use before using
956 // any other TB_XXX messages
957 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
960 wxZeroMemory(tbButton
);
961 tbButton
.iBitmap
= I_IMAGENONE
;
962 tbButton
.fsState
= TBSTATE_ENABLED
;
963 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
964 TBSTYLE_NO_DROPDOWN_ARROW
|
967 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
969 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
970 tbButton
.dwData
= (DWORD
)hPopupMenu
;
971 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
972 tbButton
.iString
= (int) label
.wx_str();
974 tbButton
.idCommand
= NewControlId();
975 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
977 wxLogLastError(wxT("TB_INSERTBUTTON"));
981 m_hMenu
= bar
->GetHMenu();
983 #else // !__WXWINCE__
987 m_hMenu
= (WXHMENU
)::CreateMenu();
991 wxLogLastError(wxT("CreateMenu"));
995 size_t count
= GetMenuCount(), i
;
996 wxMenuList::iterator it
;
997 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
999 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
1000 (UINT_PTR
)(*it
)->GetHMenu(),
1001 m_titles
[i
].wx_str()) )
1003 wxLogLastError(wxT("AppendMenu"));
1009 #endif // __WXWINCE__/!__WXWINCE__
1012 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
1015 wxASSERT(menu
->GetHMenu());
1018 #if defined(__WXWINCE__)
1019 int totalMSWItems
= GetMenuCount();
1021 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1024 int i
; // For old C++ compatibility
1025 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1027 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1030 for(i
=0; i
<wxpos
; i
++)
1032 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1039 // ---------------------------------------------------------------------------
1040 // wxMenuBar functions to work with the top level submenus
1041 // ---------------------------------------------------------------------------
1043 // NB: we don't support owner drawn top level items for now, if we do these
1044 // functions would have to be changed to use wxMenuItem as well
1046 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1048 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1049 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1051 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1053 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1058 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1060 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1062 m_titles
[pos
] = label
;
1064 if ( !IsAttached() )
1068 //else: have to modify the existing menu
1070 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1073 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1074 if ( flagsOld
== 0xFFFFFFFF )
1076 wxLogLastError(wxT("GetMenuState"));
1081 if ( flagsOld
& MF_POPUP
)
1083 // HIBYTE contains the number of items in the submenu in this case
1085 id
= (UINT_PTR
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1095 info
.cbSize
= sizeof(info
);
1096 info
.fMask
= MIIM_TYPE
;
1097 info
.fType
= MFT_STRING
;
1098 info
.cch
= label
.length();
1099 info
.dwTypeData
= const_cast<wxChar
*>(label
.wx_str());
1100 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, &info
) )
1102 wxLogLastError(wxT("SetMenuItemInfo"));
1106 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1107 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1109 wxLogLastError(wxT("ModifyMenu"));
1116 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1118 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1119 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1121 return m_titles
[pos
];
1124 // ---------------------------------------------------------------------------
1125 // wxMenuBar construction
1126 // ---------------------------------------------------------------------------
1128 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1130 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1134 m_titles
[pos
] = title
;
1136 #if defined(WINCE_WITHOUT_COMMANDBAR)
1142 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1144 // can't use ModifyMenu() because it deletes the submenu it replaces
1145 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1147 wxLogLastError(wxT("RemoveMenu"));
1150 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1151 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1152 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1154 wxLogLastError(wxT("InsertMenu"));
1158 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1160 // need to rebuild accell table
1161 RebuildAccelTable();
1163 #endif // wxUSE_ACCEL
1172 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1174 // Find out which MSW item before which we'll be inserting before
1175 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1176 // If IsAttached() is false this won't be used anyway
1178 #if defined(WINCE_WITHOUT_COMMANDBAR)
1184 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1185 ? -1 // append the menu
1186 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1188 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1191 m_titles
.Insert(title
, pos
);
1195 #if defined(WINCE_WITHOUT_COMMANDBAR)
1199 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1200 tbButton
.iBitmap
= I_IMAGENONE
;
1201 tbButton
.fsState
= TBSTATE_ENABLED
;
1202 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1204 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1205 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1206 wxString label
= wxStripMenuCodes(title
);
1207 tbButton
.iString
= (int) label
.wx_str();
1209 tbButton
.idCommand
= NewControlId();
1210 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1212 wxLogLastError(wxT("TB_INSERTBUTTON"));
1215 wxUnusedVar(mswpos
);
1217 if ( !::InsertMenu(GetHmenu(), mswpos
,
1218 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1219 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1221 wxLogLastError(wxT("InsertMenu"));
1225 if ( menu
->HasAccels() )
1227 // need to rebuild accell table
1228 RebuildAccelTable();
1230 #endif // wxUSE_ACCEL
1239 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1241 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1242 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1244 if ( !wxMenuBarBase::Append(menu
, title
) )
1247 m_titles
.Add(title
);
1249 #if defined(WINCE_WITHOUT_COMMANDBAR)
1255 #if defined(WINCE_WITHOUT_COMMANDBAR)
1259 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1260 tbButton
.iBitmap
= I_IMAGENONE
;
1261 tbButton
.fsState
= TBSTATE_ENABLED
;
1262 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1264 size_t pos
= GetMenuCount();
1265 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1266 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1267 wxString label
= wxStripMenuCodes(title
);
1268 tbButton
.iString
= (int) label
.wx_str();
1270 tbButton
.idCommand
= NewControlId();
1271 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1273 wxLogLastError(wxT("TB_INSERTBUTTON"));
1277 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1278 (UINT_PTR
)submenu
, title
.wx_str()) )
1280 wxLogLastError(wxT("AppendMenu"));
1285 if ( menu
->HasAccels() )
1287 // need to rebuild accelerator table
1288 RebuildAccelTable();
1290 #endif // wxUSE_ACCEL
1299 wxMenu
*wxMenuBar::Remove(size_t pos
)
1301 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1305 #if defined(WINCE_WITHOUT_COMMANDBAR)
1311 #if defined(WINCE_WITHOUT_COMMANDBAR)
1314 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1316 wxLogLastError(wxT("TB_DELETEBUTTON"));
1320 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1322 wxLogLastError(wxT("RemoveMenu"));
1327 if ( menu
->HasAccels() )
1329 // need to rebuild accell table
1330 RebuildAccelTable();
1332 #endif // wxUSE_ACCEL
1338 m_titles
.RemoveAt(pos
);
1345 void wxMenuBar::RebuildAccelTable()
1347 // merge the accelerators of all menus into one accel table
1348 size_t nAccelCount
= 0;
1349 size_t i
, count
= GetMenuCount();
1350 wxMenuList::iterator it
;
1351 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1353 nAccelCount
+= (*it
)->GetAccelCount();
1358 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1361 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1363 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1366 SetAcceleratorTable(wxAcceleratorTable(nAccelCount
, accelEntries
));
1368 delete [] accelEntries
;
1372 #endif // wxUSE_ACCEL
1374 void wxMenuBar::Attach(wxFrame
*frame
)
1376 wxMenuBarBase::Attach(frame
);
1378 #if defined(WINCE_WITH_COMMANDBAR)
1382 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1387 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1389 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1396 RebuildAccelTable();
1397 #endif // wxUSE_ACCEL
1400 #if defined(WINCE_WITH_COMMANDBAR)
1401 bool wxMenuBar::AddAdornments(long style
)
1403 if (m_adornmentsAdded
|| !m_commandBar
)
1406 if (style
& wxCLOSE_BOX
)
1408 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1409 wxLogLastError(wxT("CommandBar_AddAdornments"));
1417 void wxMenuBar::Detach()
1419 wxMenuBarBase::Detach();
1422 #endif // wxUSE_MENUS