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 #if wxUSE_OWNER_DRAWN && defined(MIIM_BITMAP)
63 #include "wx/dynlib.h"
66 #ifndef MNS_CHECKORBMP
67 #define MNS_CHECKORBMP 0x04000000
70 #define MIM_STYLE 0x00000010
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 // the (popup) menu title has this special id
82 static const int idMenuTitle
= -3;
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // make the given menu item default
89 static void SetDefaultMenuItem(HMENU
WXUNUSED_IN_WINCE(hmenu
),
90 UINT
WXUNUSED_IN_WINCE(id
))
95 mii
.cbSize
= sizeof(MENUITEMINFO
);
96 mii
.fMask
= MIIM_STATE
;
97 mii
.fState
= MFS_DEFAULT
;
99 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
101 wxLogLastError(wxT("SetMenuItemInfo"));
107 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
111 info
.cbSize
= sizeof(info
);
112 info
.fMask
= MIIM_STATE
;
113 // MF_BYCOMMAND is zero so test MF_BYPOSITION
114 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
115 wxLogLastError(wxT("GetMenuItemInfo"));
120 // ============================================================================
122 // ============================================================================
124 #include "wx/listimpl.cpp"
126 WX_DEFINE_LIST( wxMenuInfoList
)
128 #if wxUSE_EXTENDED_RTTI
130 WX_DEFINE_FLAGS( wxMenuStyle
)
132 wxBEGIN_FLAGS( wxMenuStyle
)
133 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
134 wxEND_FLAGS( wxMenuStyle
)
136 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
138 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
140 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
142 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
145 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
146 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
147 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
148 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
149 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
150 wxEND_PROPERTIES_TABLE()
152 wxBEGIN_HANDLERS_TABLE(wxMenu
)
153 wxEND_HANDLERS_TABLE()
155 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
157 WX_DEFINE_FLAGS( wxMenuBarStyle
)
159 wxBEGIN_FLAGS( wxMenuBarStyle
)
160 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
161 wxEND_FLAGS( wxMenuBarStyle
)
163 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
164 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
169 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
171 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
173 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
174 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
175 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
176 wxEND_PROPERTIES_TABLE()
178 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
179 wxEND_HANDLERS_TABLE()
181 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
183 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
185 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
187 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
190 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
191 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
192 wxEND_PROPERTIES_TABLE()
194 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
195 wxEND_HANDLERS_TABLE()
197 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
200 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
201 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
202 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
205 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
207 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
208 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
209 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
211 wxMenuInfo
* info
= new wxMenuInfo() ;
212 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetLabelTop(i
) ) ;
213 list
->Append( info
) ;
218 // ---------------------------------------------------------------------------
219 // wxMenu construction, adding and removing menu items
220 // ---------------------------------------------------------------------------
222 // Construct a menu with optional title (then use append)
226 m_startRadioGroup
= -1;
229 m_hMenu
= (WXHMENU
)CreatePopupMenu();
232 wxLogLastError(wxT("CreatePopupMenu"));
235 // if we have a title, insert it in the beginning of the menu
236 if ( !m_title
.empty() )
238 Append(idMenuTitle
, m_title
);
243 // The wxWindow destructor will take care of deleting the submenus.
246 // we should free Windows resources only if Windows doesn't do it for us
247 // which happens if we're attached to a menubar or a submenu of another
249 if ( !IsAttached() && !GetParent() )
251 if ( !::DestroyMenu(GetHmenu()) )
253 wxLogLastError(wxT("DestroyMenu"));
259 WX_CLEAR_ARRAY(m_accels
);
260 #endif // wxUSE_ACCEL
265 // this will take effect during the next call to Append()
269 void wxMenu::Attach(wxMenuBarBase
*menubar
)
271 wxMenuBase::Attach(menubar
);
278 int wxMenu::FindAccel(int id
) const
280 size_t n
, count
= m_accels
.GetCount();
281 for ( n
= 0; n
< count
; n
++ )
283 if ( m_accels
[n
]->m_command
== id
)
290 void wxMenu::UpdateAccel(wxMenuItem
*item
)
292 if ( item
->IsSubMenu() )
294 wxMenu
*submenu
= item
->GetSubMenu();
295 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
298 UpdateAccel(node
->GetData());
300 node
= node
->GetNext();
303 else if ( !item
->IsSeparator() )
305 // recurse upwards: we should only modify m_accels of the top level
306 // menus, not of the submenus as wxMenuBar doesn't look at them
307 // (alternative and arguable cleaner solution would be to recurse
308 // downwards in GetAccelCount() and CopyAccels())
311 GetParent()->UpdateAccel(item
);
315 // find the (new) accel for this item
316 wxAcceleratorEntry
*accel
= wxAcceleratorEntry::Create(item
->GetText());
318 accel
->m_command
= item
->GetId();
321 int n
= FindAccel(item
->GetId());
322 if ( n
== wxNOT_FOUND
)
324 // no old, add new if any
328 return; // skipping RebuildAccelTable() below
332 // replace old with new or just remove the old one if no new
337 m_accels
.RemoveAt(n
);
342 GetMenuBar()->RebuildAccelTable();
345 //else: it is a separator, they can't have accels, nothing to do
348 #endif // wxUSE_ACCEL
350 // append a new item or submenu to the menu
351 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
355 #endif // wxUSE_ACCEL
359 // if "Break" has just been called, insert a menu break before this item
360 // (and don't forget to reset the flag)
362 flags
|= MF_MENUBREAK
;
366 if ( pItem
->IsSeparator() ) {
367 flags
|= MF_SEPARATOR
;
370 // id is the numeric id for normal menu items and HMENU for submenus as
371 // required by ::AppendMenu() API
373 wxMenu
*submenu
= pItem
->GetSubMenu();
374 if ( submenu
!= NULL
) {
375 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
377 submenu
->SetParent(this);
379 id
= (UINT
)submenu
->GetHMenu();
388 // prepare to insert the item in the menu
389 wxString itemText
= pItem
->GetText();
390 LPCTSTR pData
= NULL
;
391 if ( pos
== (size_t)-1 )
393 // append at the end (note that the item is already appended to
394 // internal data structures)
395 pos
= GetMenuItemCount() - 1;
398 // adjust position to account for the title, if any
399 if ( !m_title
.empty() )
400 pos
+= 2; // for the title itself and its separator
404 #if wxUSE_OWNER_DRAWN
405 // Currently, mixing owner-drawn and non-owner-drawn items results in
406 // inconsistent margins, so we force this to be owner-drawn if any other
407 // items already are. Later we might want to use a boolean in the wxMenu
408 // to avoid search. Also we might make this fix unnecessary by getting the correct
409 // margin using NONCLIENTMETRICS.
410 if ( !pItem
->IsOwnerDrawn() )
412 // Check if any other items are ownerdrawn, and make ownerdrawn if so
413 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
416 if (node
->GetData()->IsOwnerDrawn())
418 pItem
->SetOwnerDrawn(true);
421 node
= node
->GetNext();
426 // check if we have something more than a simple text item
427 #if wxUSE_OWNER_DRAWN
428 if ( pItem
->IsOwnerDrawn() )
430 // is the item owner-drawn just because of the bitmap?
431 if ( pItem
->GetBitmap().Ok() &&
432 !pItem
->GetTextColour().Ok() &&
433 !pItem
->GetBackgroundColour().Ok() &&
434 !pItem
->GetFont().Ok() &&
435 !pItem
->GetBitmap(true).Ok() )
437 // try to use InsertMenuItem() as it's guaranteed to look correct
438 // while our owner-drawn code is not
440 // first compile-time check
441 #if defined(MIIM_BITMAP) && (_WIN32_WINNT >= 0x0500)
442 WinStruct
<MENUITEMINFO
> mii
;
444 // now run-time one: MIIM_BITMAP only works under WinME/2000+
445 if ( wxGetWinVersion() >= wxWinVersion_98
)
447 mii
.fMask
= MIIM_STRING
| MIIM_DATA
| MIIM_BITMAP
;
448 mii
.cch
= itemText
.length();
449 mii
.dwTypeData
= wx_const_cast(wxChar
*, itemText
.c_str());
451 if (flags
& MF_POPUP
)
453 mii
.fMask
|= MIIM_SUBMENU
;
454 mii
.hSubMenu
= (HMENU
)pItem
->GetSubMenu()->GetHMenu();
458 mii
.fMask
|= MIIM_ID
;
462 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
463 // 1. we can't draw it with transparency then (this is not
464 // very important now but would be with themed menu bg)
465 // 2. worse, Windows inverts the bitmap for the selected
466 // item and this looks downright ugly
468 // so instead draw it ourselves in MSWOnDrawItem()
469 mii
.dwItemData
= wx_reinterpret_cast(ULONG_PTR
, pItem
);
470 mii
.hbmpItem
= HBMMENU_CALLBACK
;
472 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
475 wxLogLastError(wxT("InsertMenuItem()"));
477 else // InsertMenuItem() ok
479 // we need to remove the extra indent which is reserved for
480 // the checkboxes by default as it looks ugly unless check
481 // boxes are used together with bitmaps and this is not the
483 WinStruct
<MENUINFO
> mi
;
485 // don't call SetMenuInfo() directly, this would prevent
486 // the app from starting up under Windows 95/NT 4
487 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
489 wxDynamicLibrary
dllUser(_T("user32"));
490 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
491 if ( pfnSetMenuInfo
)
493 mi
.fMask
= MIM_STYLE
;
494 mi
.dwStyle
= MNS_CHECKORBMP
;
495 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
496 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
499 // tell the item that it's not really owner-drawn but only
500 // needs to draw its bitmap, the rest is done by Windows
501 pItem
->ResetOwnerDrawn();
504 #endif // MIIM_BITMAP
509 // item draws itself, pass pointer to it in data parameter
510 flags
|= MF_OWNERDRAW
;
511 pData
= (LPCTSTR
)pItem
;
515 #endif // wxUSE_OWNER_DRAWN
517 // item is just a normal string (passed in data parameter)
521 itemText
= wxMenuItem::GetLabelFromText(itemText
);
524 pData
= (wxChar
*)itemText
.c_str();
527 // item might have already been inserted by InsertMenuItem() above
530 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
532 wxLogLastError(wxT("InsertMenu[Item]()"));
539 // if we just appended the title, highlight it
540 if ( (int)id
== idMenuTitle
)
542 // visually select the menu title
543 SetDefaultMenuItem(GetHmenu(), id
);
546 // if we're already attached to the menubar, we must update it
547 if ( IsAttached() && GetMenuBar()->IsAttached() )
549 GetMenuBar()->Refresh();
555 void wxMenu::EndRadioGroup()
557 // we're not inside a radio group any longer
558 m_startRadioGroup
= -1;
561 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
563 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
567 if ( item
->GetKind() == wxITEM_RADIO
)
569 int count
= GetMenuItemCount();
571 if ( m_startRadioGroup
== -1 )
573 // start a new radio group
574 m_startRadioGroup
= count
;
576 // for now it has just one element
577 item
->SetAsRadioGroupStart();
578 item
->SetRadioGroupEnd(m_startRadioGroup
);
580 // ensure that we have a checked item in the radio group
583 else // extend the current radio group
585 // we need to update its end item
586 item
->SetRadioGroupStart(m_startRadioGroup
);
587 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
591 node
->GetData()->SetRadioGroupEnd(count
);
595 wxFAIL_MSG( _T("where is the radio group start item?") );
599 else // not a radio item
604 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
611 // check the item initially
618 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
620 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
626 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
628 // we need to find the item's position in the child list
630 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
631 for ( pos
= 0; node
; pos
++ )
633 if ( node
->GetData() == item
)
636 node
= node
->GetNext();
639 // DoRemove() (unlike Remove) can only be called for an existing item!
640 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
643 // remove the corresponding accel from the accel table
644 int n
= FindAccel(item
->GetId());
645 if ( n
!= wxNOT_FOUND
)
649 m_accels
.RemoveAt(n
);
651 //else: this item doesn't have an accel, nothing to do
652 #endif // wxUSE_ACCEL
654 // remove the item from the menu
655 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
657 wxLogLastError(wxT("RemoveMenu"));
660 if ( IsAttached() && GetMenuBar()->IsAttached() )
662 // otherwise, the change won't be visible
663 GetMenuBar()->Refresh();
666 // and from internal data structures
667 return wxMenuBase::DoRemove(item
);
670 // ---------------------------------------------------------------------------
671 // accelerator helpers
672 // ---------------------------------------------------------------------------
676 // create the wxAcceleratorEntries for our accels and put them into the provided
677 // array - return the number of accels we have
678 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
680 size_t count
= GetAccelCount();
681 for ( size_t n
= 0; n
< count
; n
++ )
683 *accels
++ = *m_accels
[n
];
689 #endif // wxUSE_ACCEL
691 // ---------------------------------------------------------------------------
693 // ---------------------------------------------------------------------------
695 void wxMenu::SetTitle(const wxString
& label
)
697 bool hasNoTitle
= m_title
.empty();
700 HMENU hMenu
= GetHmenu();
704 if ( !label
.empty() )
706 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
707 (unsigned)idMenuTitle
, m_title
) ||
708 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
710 wxLogLastError(wxT("InsertMenu"));
718 // remove the title and the separator after it
719 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
720 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
722 wxLogLastError(wxT("RemoveMenu"));
731 info
.cbSize
= sizeof(info
);
732 info
.fMask
= MIIM_TYPE
;
733 info
.fType
= MFT_STRING
;
734 info
.cch
= m_title
.length();
735 info
.dwTypeData
= (LPTSTR
) m_title
.c_str();
736 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
738 wxLogLastError(wxT("SetMenuItemInfo"));
741 if ( !ModifyMenu(hMenu
, 0u,
742 MF_BYPOSITION
| MF_STRING
,
743 (unsigned)idMenuTitle
, m_title
) )
745 wxLogLastError(wxT("ModifyMenu"));
752 // put the title string in bold face
753 if ( !m_title
.empty() )
755 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
760 // ---------------------------------------------------------------------------
762 // ---------------------------------------------------------------------------
764 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
766 // ignore commands from the menu title
767 if ( id
!= (WXWORD
)idMenuTitle
)
769 // get the checked status of the command: notice that menuState is the
770 // old state of the menu, so the test for MF_CHECKED must be inverted
771 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
772 SendEvent(id
, !(menuState
& MF_CHECKED
));
778 // ---------------------------------------------------------------------------
780 // ---------------------------------------------------------------------------
782 wxWindow
*wxMenu::GetWindow() const
784 if ( m_invokingWindow
!= NULL
)
785 return m_invokingWindow
;
786 else if ( GetMenuBar() != NULL
)
787 return GetMenuBar()->GetFrame();
792 // ---------------------------------------------------------------------------
794 // ---------------------------------------------------------------------------
796 void wxMenuBar::Init()
798 m_eventHandler
= this;
800 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
803 // Not using a combined wxToolBar/wxMenuBar? then use
804 // a commandbar in WinCE .NET just to implement the
806 #if defined(WINCE_WITH_COMMANDBAR)
808 m_adornmentsAdded
= false;
812 wxMenuBar::wxMenuBar()
817 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
822 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
826 m_titles
.Alloc(count
);
828 for ( size_t i
= 0; i
< count
; i
++ )
830 m_menus
.Append(menus
[i
]);
831 m_titles
.Add(titles
[i
]);
833 menus
[i
]->Attach(this);
837 wxMenuBar::~wxMenuBar()
839 // In Windows CE (not .NET), the menubar is always associated
840 // with a toolbar, which destroys the menu implicitly.
841 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
844 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
846 toolMenuBar
->SetMenuBar(NULL
);
849 // we should free Windows resources only if Windows doesn't do it for us
850 // which happens if we're attached to a frame
851 if (m_hMenu
&& !IsAttached())
853 #if defined(WINCE_WITH_COMMANDBAR)
854 ::DestroyWindow((HWND
) m_commandBar
);
855 m_commandBar
= (WXHWND
) NULL
;
857 ::DestroyMenu((HMENU
)m_hMenu
);
859 m_hMenu
= (WXHMENU
)NULL
;
864 // ---------------------------------------------------------------------------
866 // ---------------------------------------------------------------------------
868 void wxMenuBar::Refresh()
873 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
875 #if defined(WINCE_WITHOUT_COMMANDBAR)
878 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
880 #elif defined(WINCE_WITH_COMMANDBAR)
882 DrawMenuBar((HWND
) m_commandBar
);
884 DrawMenuBar(GetHwndOf(GetFrame()));
888 WXHMENU
wxMenuBar::Create()
890 // Note: this doesn't work at all on Smartphone,
891 // since you have to use resources.
892 // We'll have to find another way to add a menu
893 // by changing/adding menu items to an existing menu.
894 #if defined(WINCE_WITHOUT_COMMANDBAR)
901 HWND hCommandBar
= (HWND
) GetToolBar()->GetHWND();
902 HMENU hMenu
= (HMENU
)::SendMessage(hCommandBar
, SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
904 // hMenu may be zero on Windows Mobile 5. So add the menus anyway.
908 memset(&tbButton
, 0, sizeof(TBBUTTON
));
909 tbButton
.iBitmap
= I_IMAGENONE
;
910 tbButton
.fsState
= TBSTATE_ENABLED
;
911 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
914 for (i
= 0; i
< GetMenuCount(); i
++)
916 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu() ;
917 tbButton
.dwData
= (DWORD
)hPopupMenu
;
918 wxString label
= wxStripMenuCodes(GetLabelTop(i
));
919 tbButton
.iString
= (int) label
.c_str();
923 tbButton
.idCommand
= NewControlId();
924 if (!::SendMessage(hCommandBar
, TB_INSERTBUTTON
, position
, (LPARAM
)&tbButton
))
926 wxLogLastError(wxT("TB_INSERTBUTTON"));
930 m_hMenu
= (WXHMENU
) hMenu
;
936 m_hMenu
= (WXHMENU
)::CreateMenu();
940 wxLogLastError(wxT("CreateMenu"));
944 size_t count
= GetMenuCount(), i
;
945 wxMenuList::iterator it
;
946 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
948 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
949 (UINT
)(*it
)->GetHMenu(),
952 wxLogLastError(wxT("AppendMenu"));
961 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
964 wxASSERT(menu
->GetHMenu());
967 #if defined(__WXWINCE__)
968 int totalMSWItems
= GetMenuCount();
970 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
973 int i
; // For old C++ compatibility
974 for(i
=wxpos
; i
<totalMSWItems
; i
++)
976 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
979 for(i
=0; i
<wxpos
; i
++)
981 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
988 // ---------------------------------------------------------------------------
989 // wxMenuBar functions to work with the top level submenus
990 // ---------------------------------------------------------------------------
992 // NB: we don't support owner drawn top level items for now, if we do these
993 // functions would have to be changed to use wxMenuItem as well
995 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
997 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
998 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1000 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1002 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1007 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
1009 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1011 m_titles
[pos
] = label
;
1013 if ( !IsAttached() )
1017 //else: have to modify the existing menu
1019 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1022 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1023 if ( flagsOld
== 0xFFFFFFFF )
1025 wxLogLastError(wxT("GetMenuState"));
1030 if ( flagsOld
& MF_POPUP
)
1032 // HIBYTE contains the number of items in the submenu in this case
1034 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1044 info
.cbSize
= sizeof(info
);
1045 info
.fMask
= MIIM_TYPE
;
1046 info
.fType
= MFT_STRING
;
1047 info
.cch
= label
.length();
1048 info
.dwTypeData
= (LPTSTR
) label
.c_str();
1049 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, & info
) )
1051 wxLogLastError(wxT("SetMenuItemInfo"));
1055 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1056 id
, label
) == (int)0xFFFFFFFF )
1058 wxLogLastError(wxT("ModifyMenu"));
1065 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1067 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1068 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
1070 return wxMenuItem::GetLabelFromText(m_titles
[pos
]);
1073 // ---------------------------------------------------------------------------
1074 // wxMenuBar construction
1075 // ---------------------------------------------------------------------------
1077 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1079 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1083 m_titles
[pos
] = title
;
1085 #if defined(WINCE_WITHOUT_COMMANDBAR)
1091 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1093 // can't use ModifyMenu() because it deletes the submenu it replaces
1094 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1096 wxLogLastError(wxT("RemoveMenu"));
1099 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1100 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1101 (UINT
)GetHmenuOf(menu
), title
) )
1103 wxLogLastError(wxT("InsertMenu"));
1107 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1109 // need to rebuild accell table
1110 RebuildAccelTable();
1112 #endif // wxUSE_ACCEL
1121 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1123 // Find out which MSW item before which we'll be inserting before
1124 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1125 // If IsAttached() is false this won't be used anyway
1127 #if defined(WINCE_WITHOUT_COMMANDBAR)
1133 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1134 ? -1 // append the menu
1135 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1137 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1140 m_titles
.Insert(title
, pos
);
1144 #if defined(WINCE_WITHOUT_COMMANDBAR)
1148 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1149 tbButton
.iBitmap
= I_IMAGENONE
;
1150 tbButton
.fsState
= TBSTATE_ENABLED
;
1151 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1153 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1154 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1155 wxString label
= wxStripMenuCodes(title
);
1156 tbButton
.iString
= (int) label
.c_str();
1158 tbButton
.idCommand
= NewControlId();
1159 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1161 wxLogLastError(wxT("TB_INSERTBUTTON"));
1164 wxUnusedVar(mswpos
);
1166 if ( !::InsertMenu(GetHmenu(), mswpos
,
1167 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1168 (UINT
)GetHmenuOf(menu
), title
) )
1170 wxLogLastError(wxT("InsertMenu"));
1174 if ( menu
->HasAccels() )
1176 // need to rebuild accell table
1177 RebuildAccelTable();
1179 #endif // wxUSE_ACCEL
1188 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1190 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1191 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1193 if ( !wxMenuBarBase::Append(menu
, title
) )
1196 m_titles
.Add(title
);
1198 #if defined(WINCE_WITHOUT_COMMANDBAR)
1204 #if defined(WINCE_WITHOUT_COMMANDBAR)
1208 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1209 tbButton
.iBitmap
= I_IMAGENONE
;
1210 tbButton
.fsState
= TBSTATE_ENABLED
;
1211 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1213 size_t pos
= GetMenuCount();
1214 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1215 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1216 wxString label
= wxStripMenuCodes(title
);
1217 tbButton
.iString
= (int) label
.c_str();
1219 tbButton
.idCommand
= NewControlId();
1220 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1222 wxLogLastError(wxT("TB_INSERTBUTTON"));
1226 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1227 (UINT
)submenu
, title
) )
1229 wxLogLastError(wxT("AppendMenu"));
1234 if ( menu
->HasAccels() )
1236 // need to rebuild accelerator table
1237 RebuildAccelTable();
1239 #endif // wxUSE_ACCEL
1248 wxMenu
*wxMenuBar::Remove(size_t pos
)
1250 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1254 #if defined(WINCE_WITHOUT_COMMANDBAR)
1260 #if defined(WINCE_WITHOUT_COMMANDBAR)
1263 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1265 wxLogLastError(wxT("TB_DELETEBUTTON"));
1269 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1271 wxLogLastError(wxT("RemoveMenu"));
1276 if ( menu
->HasAccels() )
1278 // need to rebuild accell table
1279 RebuildAccelTable();
1281 #endif // wxUSE_ACCEL
1287 m_titles
.RemoveAt(pos
);
1294 void wxMenuBar::RebuildAccelTable()
1296 // merge the accelerators of all menus into one accel table
1297 size_t nAccelCount
= 0;
1298 size_t i
, count
= GetMenuCount();
1299 wxMenuList::iterator it
;
1300 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1302 nAccelCount
+= (*it
)->GetAccelCount();
1307 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1310 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1312 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1315 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1317 delete [] accelEntries
;
1321 #endif // wxUSE_ACCEL
1323 void wxMenuBar::Attach(wxFrame
*frame
)
1325 wxMenuBarBase::Attach(frame
);
1327 #if defined(WINCE_WITH_COMMANDBAR)
1331 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1336 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1338 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1345 RebuildAccelTable();
1346 #endif // wxUSE_ACCEL
1349 #if defined(WINCE_WITH_COMMANDBAR)
1350 bool wxMenuBar::AddAdornments(long style
)
1352 if (m_adornmentsAdded
|| !m_commandBar
)
1355 if (style
& wxCLOSE_BOX
)
1357 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1358 wxLogLastError(wxT("CommandBar_AddAdornments"));
1366 void wxMenuBar::Detach()
1368 wxMenuBarBase::Detach();
1371 #endif // wxUSE_MENUS