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"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
40 #include "wx/ownerdrw.h"
43 #include "wx/msw/private.h"
51 #if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__)
55 #include "wx/msw/wince/missing.h"
59 // other standard headers
62 //VC6 needs these defining, though they are in winuser.h
64 #define MIIM_STRING 0x00000040
65 #define MIIM_BITMAP 0x00000080
66 #define MIIM_FTYPE 0x00000100
67 #define HBMMENU_CALLBACK ((HBITMAP) -1)
68 typedef struct tagMENUINFO
75 DWORD dwContextHelpID
;
77 } MENUINFO
, FAR
*LPMENUINFO
;
81 #include "wx/dynlib.h"
84 #ifndef MNS_CHECKORBMP
85 #define MNS_CHECKORBMP 0x04000000
88 #define MIM_STYLE 0x00000010
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 // the (popup) menu title has this special id
100 static const int idMenuTitle
= -3;
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 // make the given menu item default
107 static void SetDefaultMenuItem(HMENU
WXUNUSED_IN_WINCE(hmenu
),
108 UINT
WXUNUSED_IN_WINCE(id
))
113 mii
.cbSize
= sizeof(MENUITEMINFO
);
114 mii
.fMask
= MIIM_STATE
;
115 mii
.fState
= MFS_DEFAULT
;
117 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
119 wxLogLastError(wxT("SetMenuItemInfo"));
125 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
129 info
.cbSize
= sizeof(info
);
130 info
.fMask
= MIIM_STATE
;
131 // MF_BYCOMMAND is zero so test MF_BYPOSITION
132 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
133 wxLogLastError(wxT("GetMenuItemInfo"));
138 // ============================================================================
140 // ============================================================================
142 #include "wx/listimpl.cpp"
144 WX_DEFINE_LIST( wxMenuInfoList
)
146 #if wxUSE_EXTENDED_RTTI
148 WX_DEFINE_FLAGS( wxMenuStyle
)
150 wxBEGIN_FLAGS( wxMenuStyle
)
151 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
152 wxEND_FLAGS( wxMenuStyle
)
154 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
156 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
158 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
160 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
163 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
164 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
165 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
166 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
167 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
168 wxEND_PROPERTIES_TABLE()
170 wxBEGIN_HANDLERS_TABLE(wxMenu
)
171 wxEND_HANDLERS_TABLE()
173 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
175 WX_DEFINE_FLAGS( wxMenuBarStyle
)
177 wxBEGIN_FLAGS( wxMenuBarStyle
)
178 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
179 wxEND_FLAGS( wxMenuBarStyle
)
181 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
182 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
187 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
189 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
191 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
192 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
193 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
194 wxEND_PROPERTIES_TABLE()
196 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
197 wxEND_HANDLERS_TABLE()
199 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
201 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
203 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
205 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
208 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
209 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
210 wxEND_PROPERTIES_TABLE()
212 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
213 wxEND_HANDLERS_TABLE()
215 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
218 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
219 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
220 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
223 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
225 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
226 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
227 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
229 wxMenuInfo
* info
= new wxMenuInfo() ;
230 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetMenuLabel(i
) ) ;
231 list
->Append( info
) ;
236 // ---------------------------------------------------------------------------
237 // wxMenu construction, adding and removing menu items
238 // ---------------------------------------------------------------------------
240 // Construct a menu with optional title (then use append)
244 m_startRadioGroup
= -1;
247 m_hMenu
= (WXHMENU
)CreatePopupMenu();
250 wxLogLastError(wxT("CreatePopupMenu"));
253 // if we have a title, insert it in the beginning of the menu
254 if ( !m_title
.empty() )
256 Append(idMenuTitle
, m_title
);
261 // The wxWindow destructor will take care of deleting the submenus.
264 if ( !::DestroyMenu(GetHmenu()) )
266 wxLogLastError(wxT("DestroyMenu"));
271 WX_CLEAR_ARRAY(m_accels
);
272 #endif // wxUSE_ACCEL
277 // this will take effect during the next call to Append()
281 void wxMenu::Attach(wxMenuBarBase
*menubar
)
283 wxMenuBase::Attach(menubar
);
290 int wxMenu::FindAccel(int id
) const
292 size_t n
, count
= m_accels
.GetCount();
293 for ( n
= 0; n
< count
; n
++ )
295 if ( m_accels
[n
]->m_command
== id
)
302 void wxMenu::UpdateAccel(wxMenuItem
*item
)
304 if ( item
->IsSubMenu() )
306 wxMenu
*submenu
= item
->GetSubMenu();
307 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
310 UpdateAccel(node
->GetData());
312 node
= node
->GetNext();
315 else if ( !item
->IsSeparator() )
317 // recurse upwards: we should only modify m_accels of the top level
318 // menus, not of the submenus as wxMenuBar doesn't look at them
319 // (alternative and arguable cleaner solution would be to recurse
320 // downwards in GetAccelCount() and CopyAccels())
323 GetParent()->UpdateAccel(item
);
327 // find the (new) accel for this item
328 wxAcceleratorEntry
*accel
= wxAcceleratorEntry::Create(item
->GetItemLabel());
330 accel
->m_command
= item
->GetId();
333 int n
= FindAccel(item
->GetId());
334 if ( n
== wxNOT_FOUND
)
336 // no old, add new if any
340 return; // skipping RebuildAccelTable() below
344 // replace old with new or just remove the old one if no new
349 m_accels
.RemoveAt(n
);
354 GetMenuBar()->RebuildAccelTable();
357 //else: it is a separator, they can't have accels, nothing to do
360 #endif // wxUSE_ACCEL
362 // append a new item or submenu to the menu
363 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
367 #endif // wxUSE_ACCEL
371 // if "Break" has just been called, insert a menu break before this item
372 // (and don't forget to reset the flag)
374 flags
|= MF_MENUBREAK
;
378 if ( pItem
->IsSeparator() ) {
379 flags
|= MF_SEPARATOR
;
382 // id is the numeric id for normal menu items and HMENU for submenus as
383 // required by ::AppendMenu() API
385 wxMenu
*submenu
= pItem
->GetSubMenu();
386 if ( submenu
!= NULL
) {
387 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
389 submenu
->SetParent(this);
391 id
= (UINT
)submenu
->GetHMenu();
400 // prepare to insert the item in the menu
401 wxString itemText
= pItem
->GetItemLabel();
402 LPCTSTR pData
= NULL
;
403 if ( pos
== (size_t)-1 )
405 // append at the end (note that the item is already appended to
406 // internal data structures)
407 pos
= GetMenuItemCount() - 1;
410 // adjust position to account for the title, if any
411 if ( !m_title
.empty() )
412 pos
+= 2; // for the title itself and its separator
416 #if wxUSE_OWNER_DRAWN
417 // Currently, mixing owner-drawn and non-owner-drawn items results in
418 // inconsistent margins, so we force this to be owner-drawn if any other
419 // items already are. Later we might want to use a boolean in the wxMenu
420 // to avoid search. Also we might make this fix unnecessary by getting the correct
421 // margin using NONCLIENTMETRICS.
422 if ( !pItem
->IsOwnerDrawn() && !pItem
->IsSeparator() )
424 // Check if any other items are ownerdrawn, and make ownerdrawn if so
425 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
428 if (node
->GetData()->IsOwnerDrawn())
430 pItem
->SetOwnerDrawn(true);
433 node
= node
->GetNext();
438 // check if we have something more than a simple text item
439 #if wxUSE_OWNER_DRAWN
440 if ( pItem
->IsOwnerDrawn() )
442 // is the item owner-drawn just because of the [checked] bitmap?
443 if ( (pItem
->GetBitmap(false).Ok() || pItem
->GetBitmap(true).Ok()) &&
444 !pItem
->GetTextColour().Ok() &&
445 !pItem
->GetBackgroundColour().Ok() &&
446 !pItem
->GetFont().Ok() )
448 // try to use InsertMenuItem() as it's guaranteed to look correct
449 // while our owner-drawn code is not
451 // DMC at march 2007 doesn't have HBITMAP hbmpItem tagMENUITEMINFOA /W
452 // MIIM_BITMAP only works under WinME/2000+
453 WinStruct
<MENUITEMINFO
> mii
;
454 if ( wxGetWinVersion() >= wxWinVersion_98
)
456 mii
.fMask
= MIIM_STRING
| MIIM_DATA
| MIIM_BITMAP
;
457 if ( pItem
->IsCheckable() )
459 // need to set checked/unchecked bitmaps as otherwise our
460 // MSWOnDrawItem() item is not called
461 mii
.fMask
|= MIIM_CHECKMARKS
;
464 mii
.cch
= itemText
.length();
465 mii
.dwTypeData
= wx_const_cast(wxChar
*, itemText
.wx_str());
467 if (flags
& MF_POPUP
)
469 mii
.fMask
|= MIIM_SUBMENU
;
470 mii
.hSubMenu
= (HMENU
)pItem
->GetSubMenu()->GetHMenu();
474 mii
.fMask
|= MIIM_ID
;
478 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
479 // 1. we can't draw it with transparency then (this is not
480 // very important now but would be with themed menu bg)
481 // 2. worse, Windows inverts the bitmap for the selected
482 // item and this looks downright ugly
484 // so instead draw it ourselves in MSWOnDrawItem()
485 mii
.dwItemData
= wx_reinterpret_cast(ULONG_PTR
, pItem
);
486 if ( pItem
->IsCheckable() )
489 mii
.hbmpUnchecked
= HBMMENU_CALLBACK
;
491 mii
.hbmpItem
= HBMMENU_CALLBACK
;
493 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
496 wxLogLastError(wxT("InsertMenuItem()"));
498 else // InsertMenuItem() ok
500 // we need to remove the extra indent which is reserved for
501 // the checkboxes by default as it looks ugly unless check
502 // boxes are used together with bitmaps and this is not the
504 WinStruct
<MENUINFO
> mi
;
506 // don't call SetMenuInfo() directly, this would prevent
507 // the app from starting up under Windows 95/NT 4
508 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
510 wxDynamicLibrary
dllUser(_T("user32"));
511 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
512 if ( pfnSetMenuInfo
)
514 mi
.fMask
= MIM_STYLE
;
515 mi
.dwStyle
= MNS_CHECKORBMP
;
516 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
517 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
520 // tell the item that it's not really owner-drawn but only
521 // needs to draw its bitmap, the rest is done by Windows
522 pItem
->ResetOwnerDrawn();
530 // item draws itself, pass pointer to it in data parameter
531 flags
|= MF_OWNERDRAW
;
532 pData
= (LPCTSTR
)pItem
;
536 #endif // wxUSE_OWNER_DRAWN
538 // item is just a normal string (passed in data parameter)
542 itemText
= wxMenuItem::GetLabelText(itemText
);
545 pData
= (wxChar
*)itemText
.wx_str();
548 // item might have already been inserted by InsertMenuItem() above
551 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
553 wxLogLastError(wxT("InsertMenu[Item]()"));
560 // if we just appended the title, highlight it
561 if ( (int)id
== idMenuTitle
)
563 // visually select the menu title
564 SetDefaultMenuItem(GetHmenu(), id
);
567 // if we're already attached to the menubar, we must update it
568 if ( IsAttached() && GetMenuBar()->IsAttached() )
570 GetMenuBar()->Refresh();
576 void wxMenu::EndRadioGroup()
578 // we're not inside a radio group any longer
579 m_startRadioGroup
= -1;
582 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
584 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
588 if ( item
->GetKind() == wxITEM_RADIO
)
590 int count
= GetMenuItemCount();
592 if ( m_startRadioGroup
== -1 )
594 // start a new radio group
595 m_startRadioGroup
= count
;
597 // for now it has just one element
598 item
->SetAsRadioGroupStart();
599 item
->SetRadioGroupEnd(m_startRadioGroup
);
601 // ensure that we have a checked item in the radio group
604 else // extend the current radio group
606 // we need to update its end item
607 item
->SetRadioGroupStart(m_startRadioGroup
);
608 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
612 node
->GetData()->SetRadioGroupEnd(count
);
616 wxFAIL_MSG( _T("where is the radio group start item?") );
620 else // not a radio item
625 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
632 // check the item initially
639 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
641 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
647 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
649 // we need to find the item's position in the child list
651 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
652 for ( pos
= 0; node
; pos
++ )
654 if ( node
->GetData() == item
)
657 node
= node
->GetNext();
660 // DoRemove() (unlike Remove) can only be called for an existing item!
661 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
664 // remove the corresponding accel from the accel table
665 int n
= FindAccel(item
->GetId());
666 if ( n
!= wxNOT_FOUND
)
670 m_accels
.RemoveAt(n
);
672 //else: this item doesn't have an accel, nothing to do
673 #endif // wxUSE_ACCEL
675 // remove the item from the menu
676 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
678 wxLogLastError(wxT("RemoveMenu"));
681 if ( IsAttached() && GetMenuBar()->IsAttached() )
683 // otherwise, the change won't be visible
684 GetMenuBar()->Refresh();
687 // and from internal data structures
688 return wxMenuBase::DoRemove(item
);
691 // ---------------------------------------------------------------------------
692 // accelerator helpers
693 // ---------------------------------------------------------------------------
697 // create the wxAcceleratorEntries for our accels and put them into the provided
698 // array - return the number of accels we have
699 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
701 size_t count
= GetAccelCount();
702 for ( size_t n
= 0; n
< count
; n
++ )
704 *accels
++ = *m_accels
[n
];
710 #endif // wxUSE_ACCEL
712 // ---------------------------------------------------------------------------
714 // ---------------------------------------------------------------------------
716 void wxMenu::SetTitle(const wxString
& label
)
718 bool hasNoTitle
= m_title
.empty();
721 HMENU hMenu
= GetHmenu();
725 if ( !label
.empty() )
727 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
728 (unsigned)idMenuTitle
, m_title
.wx_str()) ||
729 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
731 wxLogLastError(wxT("InsertMenu"));
739 // remove the title and the separator after it
740 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
741 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
743 wxLogLastError(wxT("RemoveMenu"));
752 info
.cbSize
= sizeof(info
);
753 info
.fMask
= MIIM_TYPE
;
754 info
.fType
= MFT_STRING
;
755 info
.cch
= m_title
.length();
756 info
.dwTypeData
= wx_const_cast(wxChar
*, m_title
.wx_str());
757 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
759 wxLogLastError(wxT("SetMenuItemInfo"));
762 if ( !ModifyMenu(hMenu
, 0u,
763 MF_BYPOSITION
| MF_STRING
,
764 (unsigned)idMenuTitle
, m_title
.wx_str()) )
766 wxLogLastError(wxT("ModifyMenu"));
773 // put the title string in bold face
774 if ( !m_title
.empty() )
776 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
781 // ---------------------------------------------------------------------------
783 // ---------------------------------------------------------------------------
785 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
787 const int id
= (signed short)id_
;
789 // ignore commands from the menu title
790 if ( id
!= idMenuTitle
)
792 // update the check item when it's clicked
793 wxMenuItem
* const item
= FindItem(id
);
794 if ( item
&& item
->IsCheckable() )
797 // get the status of the menu item: note that it has been just changed
798 // by Toggle() above so here we already get the new state of the item
799 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
800 SendEvent(id
, menuState
& MF_CHECKED
);
806 // ---------------------------------------------------------------------------
808 // ---------------------------------------------------------------------------
810 wxWindow
*wxMenu::GetWindow() const
812 if ( m_invokingWindow
!= NULL
)
813 return m_invokingWindow
;
814 else if ( GetMenuBar() != NULL
)
815 return GetMenuBar()->GetFrame();
820 // ---------------------------------------------------------------------------
822 // ---------------------------------------------------------------------------
824 void wxMenuBar::Init()
826 m_eventHandler
= this;
828 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
831 // Not using a combined wxToolBar/wxMenuBar? then use
832 // a commandbar in WinCE .NET just to implement the
834 #if defined(WINCE_WITH_COMMANDBAR)
836 m_adornmentsAdded
= false;
840 wxMenuBar::wxMenuBar()
845 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
850 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
854 m_titles
.Alloc(count
);
856 for ( size_t i
= 0; i
< count
; i
++ )
858 m_menus
.Append(menus
[i
]);
859 m_titles
.Add(titles
[i
]);
861 menus
[i
]->Attach(this);
865 wxMenuBar::~wxMenuBar()
867 // In Windows CE (not .NET), the menubar is always associated
868 // with a toolbar, which destroys the menu implicitly.
869 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
872 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
874 toolMenuBar
->SetMenuBar(NULL
);
880 #if defined(WINCE_WITH_COMMANDBAR)
881 ::DestroyWindow((HWND
) m_commandBar
);
882 m_commandBar
= (WXHWND
) NULL
;
884 ::DestroyMenu((HMENU
)m_hMenu
);
886 m_hMenu
= (WXHMENU
)NULL
;
891 // ---------------------------------------------------------------------------
893 // ---------------------------------------------------------------------------
895 void wxMenuBar::Refresh()
900 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
902 #if defined(WINCE_WITHOUT_COMMANDBAR)
905 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
907 #elif defined(WINCE_WITH_COMMANDBAR)
909 DrawMenuBar((HWND
) m_commandBar
);
911 DrawMenuBar(GetHwndOf(GetFrame()));
915 WXHMENU
wxMenuBar::Create()
917 // Note: this doesn't work at all on Smartphone,
918 // since you have to use resources.
919 // We'll have to find another way to add a menu
920 // by changing/adding menu items to an existing menu.
921 #if defined(WINCE_WITHOUT_COMMANDBAR)
925 wxToolMenuBar
* const bar
= wx_static_cast(wxToolMenuBar
*, GetToolBar());
929 HWND hCommandBar
= GetHwndOf(bar
);
931 // notify comctl32.dll about the version of the headers we use before using
932 // any other TB_XXX messages
933 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
936 wxZeroMemory(tbButton
);
937 tbButton
.iBitmap
= I_IMAGENONE
;
938 tbButton
.fsState
= TBSTATE_ENABLED
;
939 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
940 TBSTYLE_NO_DROPDOWN_ARROW
|
943 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
945 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
946 tbButton
.dwData
= (DWORD
)hPopupMenu
;
947 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
948 tbButton
.iString
= (int) label
.wx_str();
950 tbButton
.idCommand
= NewControlId();
951 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
953 wxLogLastError(wxT("TB_INSERTBUTTON"));
957 m_hMenu
= bar
->GetHMenu();
959 #else // !__WXWINCE__
963 m_hMenu
= (WXHMENU
)::CreateMenu();
967 wxLogLastError(wxT("CreateMenu"));
971 size_t count
= GetMenuCount(), i
;
972 wxMenuList::iterator it
;
973 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
975 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
976 (UINT
)(*it
)->GetHMenu(),
977 m_titles
[i
].wx_str()) )
979 wxLogLastError(wxT("AppendMenu"));
985 #endif // __WXWINCE__/!__WXWINCE__
988 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
991 wxASSERT(menu
->GetHMenu());
994 #if defined(__WXWINCE__)
995 int totalMSWItems
= GetMenuCount();
997 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1000 int i
; // For old C++ compatibility
1001 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1003 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1006 for(i
=0; i
<wxpos
; i
++)
1008 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1015 // ---------------------------------------------------------------------------
1016 // wxMenuBar functions to work with the top level submenus
1017 // ---------------------------------------------------------------------------
1019 // NB: we don't support owner drawn top level items for now, if we do these
1020 // functions would have to be changed to use wxMenuItem as well
1022 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1024 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1025 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1027 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1029 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1034 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1036 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1038 m_titles
[pos
] = label
;
1040 if ( !IsAttached() )
1044 //else: have to modify the existing menu
1046 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1049 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1050 if ( flagsOld
== 0xFFFFFFFF )
1052 wxLogLastError(wxT("GetMenuState"));
1057 if ( flagsOld
& MF_POPUP
)
1059 // HIBYTE contains the number of items in the submenu in this case
1061 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1071 info
.cbSize
= sizeof(info
);
1072 info
.fMask
= MIIM_TYPE
;
1073 info
.fType
= MFT_STRING
;
1074 info
.cch
= label
.length();
1075 info
.dwTypeData
= wx_const_cast(wxChar
*, label
.wx_str());
1076 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, & info
) )
1078 wxLogLastError(wxT("SetMenuItemInfo"));
1082 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1083 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1085 wxLogLastError(wxT("ModifyMenu"));
1092 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1094 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1095 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1097 return m_titles
[pos
];
1100 // ---------------------------------------------------------------------------
1101 // wxMenuBar construction
1102 // ---------------------------------------------------------------------------
1104 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1106 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1110 m_titles
[pos
] = title
;
1112 #if defined(WINCE_WITHOUT_COMMANDBAR)
1118 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1120 // can't use ModifyMenu() because it deletes the submenu it replaces
1121 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1123 wxLogLastError(wxT("RemoveMenu"));
1126 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1127 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1128 (UINT
)GetHmenuOf(menu
), title
.wx_str()) )
1130 wxLogLastError(wxT("InsertMenu"));
1134 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1136 // need to rebuild accell table
1137 RebuildAccelTable();
1139 #endif // wxUSE_ACCEL
1148 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1150 // Find out which MSW item before which we'll be inserting before
1151 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1152 // If IsAttached() is false this won't be used anyway
1154 #if defined(WINCE_WITHOUT_COMMANDBAR)
1160 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1161 ? -1 // append the menu
1162 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1164 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1167 m_titles
.Insert(title
, pos
);
1171 #if defined(WINCE_WITHOUT_COMMANDBAR)
1175 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1176 tbButton
.iBitmap
= I_IMAGENONE
;
1177 tbButton
.fsState
= TBSTATE_ENABLED
;
1178 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1180 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1181 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1182 wxString label
= wxStripMenuCodes(title
);
1183 tbButton
.iString
= (int) label
.wx_str();
1185 tbButton
.idCommand
= NewControlId();
1186 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1188 wxLogLastError(wxT("TB_INSERTBUTTON"));
1191 wxUnusedVar(mswpos
);
1193 if ( !::InsertMenu(GetHmenu(), mswpos
,
1194 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1195 (UINT
)GetHmenuOf(menu
), title
.wx_str()) )
1197 wxLogLastError(wxT("InsertMenu"));
1201 if ( menu
->HasAccels() )
1203 // need to rebuild accell table
1204 RebuildAccelTable();
1206 #endif // wxUSE_ACCEL
1215 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1217 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1218 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1220 if ( !wxMenuBarBase::Append(menu
, title
) )
1223 m_titles
.Add(title
);
1225 #if defined(WINCE_WITHOUT_COMMANDBAR)
1231 #if defined(WINCE_WITHOUT_COMMANDBAR)
1235 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1236 tbButton
.iBitmap
= I_IMAGENONE
;
1237 tbButton
.fsState
= TBSTATE_ENABLED
;
1238 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1240 size_t pos
= GetMenuCount();
1241 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1242 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1243 wxString label
= wxStripMenuCodes(title
);
1244 tbButton
.iString
= (int) label
.wx_str();
1246 tbButton
.idCommand
= NewControlId();
1247 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1249 wxLogLastError(wxT("TB_INSERTBUTTON"));
1253 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1254 (UINT
)submenu
, title
.wx_str()) )
1256 wxLogLastError(wxT("AppendMenu"));
1261 if ( menu
->HasAccels() )
1263 // need to rebuild accelerator table
1264 RebuildAccelTable();
1266 #endif // wxUSE_ACCEL
1275 wxMenu
*wxMenuBar::Remove(size_t pos
)
1277 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1281 #if defined(WINCE_WITHOUT_COMMANDBAR)
1287 #if defined(WINCE_WITHOUT_COMMANDBAR)
1290 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1292 wxLogLastError(wxT("TB_DELETEBUTTON"));
1296 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1298 wxLogLastError(wxT("RemoveMenu"));
1303 if ( menu
->HasAccels() )
1305 // need to rebuild accell table
1306 RebuildAccelTable();
1308 #endif // wxUSE_ACCEL
1314 m_titles
.RemoveAt(pos
);
1321 void wxMenuBar::RebuildAccelTable()
1323 // merge the accelerators of all menus into one accel table
1324 size_t nAccelCount
= 0;
1325 size_t i
, count
= GetMenuCount();
1326 wxMenuList::iterator it
;
1327 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1329 nAccelCount
+= (*it
)->GetAccelCount();
1334 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1337 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1339 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1342 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1344 delete [] accelEntries
;
1348 #endif // wxUSE_ACCEL
1350 void wxMenuBar::Attach(wxFrame
*frame
)
1352 wxMenuBarBase::Attach(frame
);
1354 #if defined(WINCE_WITH_COMMANDBAR)
1358 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1363 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1365 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1372 RebuildAccelTable();
1373 #endif // wxUSE_ACCEL
1376 #if defined(WINCE_WITH_COMMANDBAR)
1377 bool wxMenuBar::AddAdornments(long style
)
1379 if (m_adornmentsAdded
|| !m_commandBar
)
1382 if (style
& wxCLOSE_BOX
)
1384 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1385 wxLogLastError(wxT("CommandBar_AddAdornments"));
1393 void wxMenuBar::Detach()
1395 wxMenuBarBase::Detach();
1398 #endif // wxUSE_MENUS