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 // we should free Windows resources only if Windows doesn't do it for us
265 // which happens if we're attached to a menubar or a submenu of another
267 if ( !IsAttached() && !GetParent() )
269 if ( !::DestroyMenu(GetHmenu()) )
271 wxLogLastError(wxT("DestroyMenu"));
277 WX_CLEAR_ARRAY(m_accels
);
278 #endif // wxUSE_ACCEL
283 // this will take effect during the next call to Append()
287 void wxMenu::Attach(wxMenuBarBase
*menubar
)
289 wxMenuBase::Attach(menubar
);
296 int wxMenu::FindAccel(int id
) const
298 size_t n
, count
= m_accels
.GetCount();
299 for ( n
= 0; n
< count
; n
++ )
301 if ( m_accels
[n
]->m_command
== id
)
308 void wxMenu::UpdateAccel(wxMenuItem
*item
)
310 if ( item
->IsSubMenu() )
312 wxMenu
*submenu
= item
->GetSubMenu();
313 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
316 UpdateAccel(node
->GetData());
318 node
= node
->GetNext();
321 else if ( !item
->IsSeparator() )
323 // recurse upwards: we should only modify m_accels of the top level
324 // menus, not of the submenus as wxMenuBar doesn't look at them
325 // (alternative and arguable cleaner solution would be to recurse
326 // downwards in GetAccelCount() and CopyAccels())
329 GetParent()->UpdateAccel(item
);
333 // find the (new) accel for this item
334 wxAcceleratorEntry
*accel
= wxAcceleratorEntry::Create(item
->GetItemLabel());
336 accel
->m_command
= item
->GetId();
339 int n
= FindAccel(item
->GetId());
340 if ( n
== wxNOT_FOUND
)
342 // no old, add new if any
346 return; // skipping RebuildAccelTable() below
350 // replace old with new or just remove the old one if no new
355 m_accels
.RemoveAt(n
);
360 GetMenuBar()->RebuildAccelTable();
363 //else: it is a separator, they can't have accels, nothing to do
366 #endif // wxUSE_ACCEL
368 // append a new item or submenu to the menu
369 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
373 #endif // wxUSE_ACCEL
377 // if "Break" has just been called, insert a menu break before this item
378 // (and don't forget to reset the flag)
380 flags
|= MF_MENUBREAK
;
384 if ( pItem
->IsSeparator() ) {
385 flags
|= MF_SEPARATOR
;
388 // id is the numeric id for normal menu items and HMENU for submenus as
389 // required by ::AppendMenu() API
391 wxMenu
*submenu
= pItem
->GetSubMenu();
392 if ( submenu
!= NULL
) {
393 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
395 submenu
->SetParent(this);
397 id
= (UINT
)submenu
->GetHMenu();
406 // prepare to insert the item in the menu
407 wxString itemText
= pItem
->GetItemLabel();
408 LPCTSTR pData
= NULL
;
409 if ( pos
== (size_t)-1 )
411 // append at the end (note that the item is already appended to
412 // internal data structures)
413 pos
= GetMenuItemCount() - 1;
416 // adjust position to account for the title, if any
417 if ( !m_title
.empty() )
418 pos
+= 2; // for the title itself and its separator
422 #if wxUSE_OWNER_DRAWN
423 // Currently, mixing owner-drawn and non-owner-drawn items results in
424 // inconsistent margins, so we force this to be owner-drawn if any other
425 // items already are. Later we might want to use a boolean in the wxMenu
426 // to avoid search. Also we might make this fix unnecessary by getting the correct
427 // margin using NONCLIENTMETRICS.
428 if ( !pItem
->IsOwnerDrawn() && !pItem
->IsSeparator() )
430 // Check if any other items are ownerdrawn, and make ownerdrawn if so
431 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
434 if (node
->GetData()->IsOwnerDrawn())
436 pItem
->SetOwnerDrawn(true);
439 node
= node
->GetNext();
444 // check if we have something more than a simple text item
445 #if wxUSE_OWNER_DRAWN
446 if ( pItem
->IsOwnerDrawn() )
448 // is the item owner-drawn just because of the [checked] bitmap?
449 if ( (pItem
->GetBitmap(false).Ok() || pItem
->GetBitmap(true).Ok()) &&
450 !pItem
->GetTextColour().Ok() &&
451 !pItem
->GetBackgroundColour().Ok() &&
452 !pItem
->GetFont().Ok() )
454 // try to use InsertMenuItem() as it's guaranteed to look correct
455 // while our owner-drawn code is not
457 // DMC at march 2007 doesn't have HBITMAP hbmpItem tagMENUITEMINFOA /W
458 // MIIM_BITMAP only works under WinME/2000+
459 WinStruct
<MENUITEMINFO
> mii
;
460 if ( wxGetWinVersion() >= wxWinVersion_98
)
462 mii
.fMask
= MIIM_STRING
| MIIM_DATA
| MIIM_BITMAP
;
463 if ( pItem
->IsCheckable() )
465 // need to set checked/unchecked bitmaps as otherwise our
466 // MSWOnDrawItem() item is not called
467 mii
.fMask
|= MIIM_CHECKMARKS
;
470 mii
.cch
= itemText
.length();
471 mii
.dwTypeData
= wx_const_cast(wxChar
*, itemText
.wx_str());
473 if (flags
& MF_POPUP
)
475 mii
.fMask
|= MIIM_SUBMENU
;
476 mii
.hSubMenu
= (HMENU
)pItem
->GetSubMenu()->GetHMenu();
480 mii
.fMask
|= MIIM_ID
;
484 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
485 // 1. we can't draw it with transparency then (this is not
486 // very important now but would be with themed menu bg)
487 // 2. worse, Windows inverts the bitmap for the selected
488 // item and this looks downright ugly
490 // so instead draw it ourselves in MSWOnDrawItem()
491 mii
.dwItemData
= wx_reinterpret_cast(ULONG_PTR
, pItem
);
492 if ( pItem
->IsCheckable() )
495 mii
.hbmpUnchecked
= HBMMENU_CALLBACK
;
497 mii
.hbmpItem
= HBMMENU_CALLBACK
;
499 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
502 wxLogLastError(wxT("InsertMenuItem()"));
504 else // InsertMenuItem() ok
506 // we need to remove the extra indent which is reserved for
507 // the checkboxes by default as it looks ugly unless check
508 // boxes are used together with bitmaps and this is not the
510 WinStruct
<MENUINFO
> mi
;
512 // don't call SetMenuInfo() directly, this would prevent
513 // the app from starting up under Windows 95/NT 4
514 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
516 wxDynamicLibrary
dllUser(_T("user32"));
517 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
518 if ( pfnSetMenuInfo
)
520 mi
.fMask
= MIM_STYLE
;
521 mi
.dwStyle
= MNS_CHECKORBMP
;
522 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
523 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
526 // tell the item that it's not really owner-drawn but only
527 // needs to draw its bitmap, the rest is done by Windows
528 pItem
->ResetOwnerDrawn();
536 // item draws itself, pass pointer to it in data parameter
537 flags
|= MF_OWNERDRAW
;
538 pData
= (LPCTSTR
)pItem
;
542 #endif // wxUSE_OWNER_DRAWN
544 // item is just a normal string (passed in data parameter)
548 itemText
= wxMenuItem::GetLabelText(itemText
);
551 pData
= (wxChar
*)itemText
.wx_str();
554 // item might have already been inserted by InsertMenuItem() above
557 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
559 wxLogLastError(wxT("InsertMenu[Item]()"));
566 // if we just appended the title, highlight it
567 if ( (int)id
== idMenuTitle
)
569 // visually select the menu title
570 SetDefaultMenuItem(GetHmenu(), id
);
573 // if we're already attached to the menubar, we must update it
574 if ( IsAttached() && GetMenuBar()->IsAttached() )
576 GetMenuBar()->Refresh();
582 void wxMenu::EndRadioGroup()
584 // we're not inside a radio group any longer
585 m_startRadioGroup
= -1;
588 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
590 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
594 if ( item
->GetKind() == wxITEM_RADIO
)
596 int count
= GetMenuItemCount();
598 if ( m_startRadioGroup
== -1 )
600 // start a new radio group
601 m_startRadioGroup
= count
;
603 // for now it has just one element
604 item
->SetAsRadioGroupStart();
605 item
->SetRadioGroupEnd(m_startRadioGroup
);
607 // ensure that we have a checked item in the radio group
610 else // extend the current radio group
612 // we need to update its end item
613 item
->SetRadioGroupStart(m_startRadioGroup
);
614 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
618 node
->GetData()->SetRadioGroupEnd(count
);
622 wxFAIL_MSG( _T("where is the radio group start item?") );
626 else // not a radio item
631 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
638 // check the item initially
645 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
647 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
653 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
655 // we need to find the item's position in the child list
657 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
658 for ( pos
= 0; node
; pos
++ )
660 if ( node
->GetData() == item
)
663 node
= node
->GetNext();
666 // DoRemove() (unlike Remove) can only be called for an existing item!
667 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
670 // remove the corresponding accel from the accel table
671 int n
= FindAccel(item
->GetId());
672 if ( n
!= wxNOT_FOUND
)
676 m_accels
.RemoveAt(n
);
678 //else: this item doesn't have an accel, nothing to do
679 #endif // wxUSE_ACCEL
681 // remove the item from the menu
682 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
684 wxLogLastError(wxT("RemoveMenu"));
687 if ( IsAttached() && GetMenuBar()->IsAttached() )
689 // otherwise, the change won't be visible
690 GetMenuBar()->Refresh();
693 // and from internal data structures
694 return wxMenuBase::DoRemove(item
);
697 // ---------------------------------------------------------------------------
698 // accelerator helpers
699 // ---------------------------------------------------------------------------
703 // create the wxAcceleratorEntries for our accels and put them into the provided
704 // array - return the number of accels we have
705 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
707 size_t count
= GetAccelCount();
708 for ( size_t n
= 0; n
< count
; n
++ )
710 *accels
++ = *m_accels
[n
];
716 #endif // wxUSE_ACCEL
718 // ---------------------------------------------------------------------------
720 // ---------------------------------------------------------------------------
722 void wxMenu::SetTitle(const wxString
& label
)
724 bool hasNoTitle
= m_title
.empty();
727 HMENU hMenu
= GetHmenu();
731 if ( !label
.empty() )
733 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
734 (unsigned)idMenuTitle
, m_title
.wx_str()) ||
735 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
737 wxLogLastError(wxT("InsertMenu"));
745 // remove the title and the separator after it
746 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
747 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
749 wxLogLastError(wxT("RemoveMenu"));
758 info
.cbSize
= sizeof(info
);
759 info
.fMask
= MIIM_TYPE
;
760 info
.fType
= MFT_STRING
;
761 info
.cch
= m_title
.length();
762 info
.dwTypeData
= wx_const_cast(wxChar
*, m_title
.wx_str());
763 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
765 wxLogLastError(wxT("SetMenuItemInfo"));
768 if ( !ModifyMenu(hMenu
, 0u,
769 MF_BYPOSITION
| MF_STRING
,
770 (unsigned)idMenuTitle
, m_title
.wx_str()) )
772 wxLogLastError(wxT("ModifyMenu"));
779 // put the title string in bold face
780 if ( !m_title
.empty() )
782 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
787 // ---------------------------------------------------------------------------
789 // ---------------------------------------------------------------------------
791 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
793 const int id
= (signed short)id_
;
795 // ignore commands from the menu title
796 if ( id
!= idMenuTitle
)
798 // update the check item when it's clicked
799 wxMenuItem
* const item
= FindItem(id
);
800 if ( item
&& item
->IsCheckable() )
803 // get the status of the menu item: note that it has been just changed
804 // by Toggle() above so here we already get the new state of the item
805 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
806 SendEvent(id
, menuState
& MF_CHECKED
);
812 // ---------------------------------------------------------------------------
814 // ---------------------------------------------------------------------------
816 wxWindow
*wxMenu::GetWindow() const
818 if ( m_invokingWindow
!= NULL
)
819 return m_invokingWindow
;
820 else if ( GetMenuBar() != NULL
)
821 return GetMenuBar()->GetFrame();
826 // ---------------------------------------------------------------------------
828 // ---------------------------------------------------------------------------
830 void wxMenuBar::Init()
832 m_eventHandler
= this;
834 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
837 // Not using a combined wxToolBar/wxMenuBar? then use
838 // a commandbar in WinCE .NET just to implement the
840 #if defined(WINCE_WITH_COMMANDBAR)
842 m_adornmentsAdded
= false;
846 wxMenuBar::wxMenuBar()
851 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
856 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
860 m_titles
.Alloc(count
);
862 for ( size_t i
= 0; i
< count
; i
++ )
864 m_menus
.Append(menus
[i
]);
865 m_titles
.Add(titles
[i
]);
867 menus
[i
]->Attach(this);
871 wxMenuBar::~wxMenuBar()
873 // In Windows CE (not .NET), the menubar is always associated
874 // with a toolbar, which destroys the menu implicitly.
875 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
878 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
880 toolMenuBar
->SetMenuBar(NULL
);
883 // we should free Windows resources only if Windows doesn't do it for us
884 // which happens if we're attached to a frame
885 if (m_hMenu
&& !IsAttached())
887 #if defined(WINCE_WITH_COMMANDBAR)
888 ::DestroyWindow((HWND
) m_commandBar
);
889 m_commandBar
= (WXHWND
) NULL
;
891 ::DestroyMenu((HMENU
)m_hMenu
);
893 m_hMenu
= (WXHMENU
)NULL
;
898 // ---------------------------------------------------------------------------
900 // ---------------------------------------------------------------------------
902 void wxMenuBar::Refresh()
907 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
909 #if defined(WINCE_WITHOUT_COMMANDBAR)
912 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
914 #elif defined(WINCE_WITH_COMMANDBAR)
916 DrawMenuBar((HWND
) m_commandBar
);
918 DrawMenuBar(GetHwndOf(GetFrame()));
922 WXHMENU
wxMenuBar::Create()
924 // Note: this doesn't work at all on Smartphone,
925 // since you have to use resources.
926 // We'll have to find another way to add a menu
927 // by changing/adding menu items to an existing menu.
928 #if defined(WINCE_WITHOUT_COMMANDBAR)
932 wxToolMenuBar
* const bar
= wx_static_cast(wxToolMenuBar
*, GetToolBar());
936 HWND hCommandBar
= GetHwndOf(bar
);
938 // notify comctl32.dll about the version of the headers we use before using
939 // any other TB_XXX messages
940 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
943 wxZeroMemory(tbButton
);
944 tbButton
.iBitmap
= I_IMAGENONE
;
945 tbButton
.fsState
= TBSTATE_ENABLED
;
946 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
947 TBSTYLE_NO_DROPDOWN_ARROW
|
950 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
952 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
953 tbButton
.dwData
= (DWORD
)hPopupMenu
;
954 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
955 tbButton
.iString
= (int) label
.wx_str();
957 tbButton
.idCommand
= NewControlId();
958 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
960 wxLogLastError(wxT("TB_INSERTBUTTON"));
964 m_hMenu
= bar
->GetHMenu();
966 #else // !__WXWINCE__
970 m_hMenu
= (WXHMENU
)::CreateMenu();
974 wxLogLastError(wxT("CreateMenu"));
978 size_t count
= GetMenuCount(), i
;
979 wxMenuList::iterator it
;
980 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
982 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
983 (UINT
)(*it
)->GetHMenu(),
984 m_titles
[i
].wx_str()) )
986 wxLogLastError(wxT("AppendMenu"));
992 #endif // __WXWINCE__/!__WXWINCE__
995 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
998 wxASSERT(menu
->GetHMenu());
1001 #if defined(__WXWINCE__)
1002 int totalMSWItems
= GetMenuCount();
1004 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1007 int i
; // For old C++ compatibility
1008 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1010 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1013 for(i
=0; i
<wxpos
; i
++)
1015 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1022 // ---------------------------------------------------------------------------
1023 // wxMenuBar functions to work with the top level submenus
1024 // ---------------------------------------------------------------------------
1026 // NB: we don't support owner drawn top level items for now, if we do these
1027 // functions would have to be changed to use wxMenuItem as well
1029 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1031 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1032 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1034 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1036 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1041 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1043 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1045 m_titles
[pos
] = label
;
1047 if ( !IsAttached() )
1051 //else: have to modify the existing menu
1053 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1056 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1057 if ( flagsOld
== 0xFFFFFFFF )
1059 wxLogLastError(wxT("GetMenuState"));
1064 if ( flagsOld
& MF_POPUP
)
1066 // HIBYTE contains the number of items in the submenu in this case
1068 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1078 info
.cbSize
= sizeof(info
);
1079 info
.fMask
= MIIM_TYPE
;
1080 info
.fType
= MFT_STRING
;
1081 info
.cch
= label
.length();
1082 info
.dwTypeData
= wx_const_cast(wxChar
*, label
.wx_str());
1083 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, & info
) )
1085 wxLogLastError(wxT("SetMenuItemInfo"));
1089 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1090 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1092 wxLogLastError(wxT("ModifyMenu"));
1099 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1101 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1102 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1104 return m_titles
[pos
];
1107 // ---------------------------------------------------------------------------
1108 // wxMenuBar construction
1109 // ---------------------------------------------------------------------------
1111 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1113 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1117 m_titles
[pos
] = title
;
1119 #if defined(WINCE_WITHOUT_COMMANDBAR)
1125 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1127 // can't use ModifyMenu() because it deletes the submenu it replaces
1128 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1130 wxLogLastError(wxT("RemoveMenu"));
1133 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1134 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1135 (UINT
)GetHmenuOf(menu
), title
.wx_str()) )
1137 wxLogLastError(wxT("InsertMenu"));
1141 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1143 // need to rebuild accell table
1144 RebuildAccelTable();
1146 #endif // wxUSE_ACCEL
1155 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1157 // Find out which MSW item before which we'll be inserting before
1158 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1159 // If IsAttached() is false this won't be used anyway
1161 #if defined(WINCE_WITHOUT_COMMANDBAR)
1167 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1168 ? -1 // append the menu
1169 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1171 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1174 m_titles
.Insert(title
, pos
);
1178 #if defined(WINCE_WITHOUT_COMMANDBAR)
1182 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1183 tbButton
.iBitmap
= I_IMAGENONE
;
1184 tbButton
.fsState
= TBSTATE_ENABLED
;
1185 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1187 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1188 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1189 wxString label
= wxStripMenuCodes(title
);
1190 tbButton
.iString
= (int) label
.wx_str();
1192 tbButton
.idCommand
= NewControlId();
1193 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1195 wxLogLastError(wxT("TB_INSERTBUTTON"));
1198 wxUnusedVar(mswpos
);
1200 if ( !::InsertMenu(GetHmenu(), mswpos
,
1201 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1202 (UINT
)GetHmenuOf(menu
), title
.wx_str()) )
1204 wxLogLastError(wxT("InsertMenu"));
1208 if ( menu
->HasAccels() )
1210 // need to rebuild accell table
1211 RebuildAccelTable();
1213 #endif // wxUSE_ACCEL
1222 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1224 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1225 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1227 if ( !wxMenuBarBase::Append(menu
, title
) )
1230 m_titles
.Add(title
);
1232 #if defined(WINCE_WITHOUT_COMMANDBAR)
1238 #if defined(WINCE_WITHOUT_COMMANDBAR)
1242 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1243 tbButton
.iBitmap
= I_IMAGENONE
;
1244 tbButton
.fsState
= TBSTATE_ENABLED
;
1245 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1247 size_t pos
= GetMenuCount();
1248 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1249 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1250 wxString label
= wxStripMenuCodes(title
);
1251 tbButton
.iString
= (int) label
.wx_str();
1253 tbButton
.idCommand
= NewControlId();
1254 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1256 wxLogLastError(wxT("TB_INSERTBUTTON"));
1260 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1261 (UINT
)submenu
, title
.wx_str()) )
1263 wxLogLastError(wxT("AppendMenu"));
1268 if ( menu
->HasAccels() )
1270 // need to rebuild accelerator table
1271 RebuildAccelTable();
1273 #endif // wxUSE_ACCEL
1282 wxMenu
*wxMenuBar::Remove(size_t pos
)
1284 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1288 #if defined(WINCE_WITHOUT_COMMANDBAR)
1294 #if defined(WINCE_WITHOUT_COMMANDBAR)
1297 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1299 wxLogLastError(wxT("TB_DELETEBUTTON"));
1303 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1305 wxLogLastError(wxT("RemoveMenu"));
1310 if ( menu
->HasAccels() )
1312 // need to rebuild accell table
1313 RebuildAccelTable();
1315 #endif // wxUSE_ACCEL
1321 m_titles
.RemoveAt(pos
);
1328 void wxMenuBar::RebuildAccelTable()
1330 // merge the accelerators of all menus into one accel table
1331 size_t nAccelCount
= 0;
1332 size_t i
, count
= GetMenuCount();
1333 wxMenuList::iterator it
;
1334 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1336 nAccelCount
+= (*it
)->GetAccelCount();
1341 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1344 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1346 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1349 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1351 delete [] accelEntries
;
1355 #endif // wxUSE_ACCEL
1357 void wxMenuBar::Attach(wxFrame
*frame
)
1359 wxMenuBarBase::Attach(frame
);
1361 #if defined(WINCE_WITH_COMMANDBAR)
1365 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1370 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1372 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1379 RebuildAccelTable();
1380 #endif // wxUSE_ACCEL
1383 #if defined(WINCE_WITH_COMMANDBAR)
1384 bool wxMenuBar::AddAdornments(long style
)
1386 if (m_adornmentsAdded
|| !m_commandBar
)
1389 if (style
& wxCLOSE_BOX
)
1391 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1392 wxLogLastError(wxT("CommandBar_AddAdornments"));
1400 void wxMenuBar::Detach()
1402 wxMenuBarBase::Detach();
1405 #endif // wxUSE_MENUS