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/msw/private.h"
43 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
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 UINT idMenuTitle
= (UINT
)-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
375 // we should support disabling the item even prior to adding it to the menu
376 UINT flags
= pItem
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
;
378 // if "Break" has just been called, insert a menu break before this item
379 // (and don't forget to reset the flag)
381 flags
|= MF_MENUBREAK
;
385 if ( pItem
->IsSeparator() ) {
386 flags
|= MF_SEPARATOR
;
389 // id is the numeric id for normal menu items and HMENU for submenus as
390 // required by ::AppendMenu() API
392 wxMenu
*submenu
= pItem
->GetSubMenu();
393 if ( submenu
!= NULL
) {
394 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
396 submenu
->SetParent(this);
398 id
= (UINT_PTR
)submenu
->GetHMenu();
403 id
= pItem
->GetMSWId();
407 // prepare to insert the item in the menu
408 wxString itemText
= pItem
->GetItemLabel();
409 LPCTSTR pData
= NULL
;
410 if ( pos
== (size_t)-1 )
412 // append at the end (note that the item is already appended to
413 // internal data structures)
414 pos
= GetMenuItemCount() - 1;
417 // adjust position to account for the title, if any
418 if ( !m_title
.empty() )
419 pos
+= 2; // for the title itself and its separator
423 #if wxUSE_OWNER_DRAWN
424 // Currently, mixing owner-drawn and non-owner-drawn items results in
425 // inconsistent margins, so we force this to be owner-drawn if any other
426 // items already are. Later we might want to use a boolean in the wxMenu
427 // to avoid search. Also we might make this fix unnecessary by getting the correct
428 // margin using NONCLIENTMETRICS.
429 if ( !pItem
->IsOwnerDrawn() && !pItem
->IsSeparator() )
431 // Check if any other items are ownerdrawn, and make ownerdrawn if so
432 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
435 if (node
->GetData()->IsOwnerDrawn())
437 pItem
->SetOwnerDrawn(true);
440 node
= node
->GetNext();
445 // check if we have something more than a simple text item
446 #if wxUSE_OWNER_DRAWN
447 if ( pItem
->IsOwnerDrawn() )
449 // is the item owner-drawn just because of the [checked] bitmap?
450 if ( (pItem
->GetBitmap(false).Ok() || pItem
->GetBitmap(true).Ok()) &&
451 !pItem
->GetTextColour().Ok() &&
452 !pItem
->GetBackgroundColour().Ok() &&
453 !pItem
->GetFont().Ok() )
455 // try to use InsertMenuItem() as it's guaranteed to look correct
456 // while our owner-drawn code is not
458 // DMC at march 2007 doesn't have HBITMAP hbmpItem tagMENUITEMINFOA /W
459 // MIIM_BITMAP only works under WinME/2000+
460 WinStruct
<MENUITEMINFO
> mii
;
461 if ( wxGetWinVersion() >= wxWinVersion_98
)
463 mii
.fMask
= MIIM_STRING
| MIIM_DATA
| MIIM_BITMAP
;
464 if ( pItem
->IsCheckable() )
466 // need to set checked/unchecked bitmaps as otherwise our
467 // MSWOnDrawItem() item is not called
468 mii
.fMask
|= MIIM_CHECKMARKS
;
471 mii
.cch
= itemText
.length();
472 mii
.dwTypeData
= wx_const_cast(wxChar
*, itemText
.wx_str());
474 if (flags
& MF_POPUP
)
476 mii
.fMask
|= MIIM_SUBMENU
;
477 mii
.hSubMenu
= (HMENU
)pItem
->GetSubMenu()->GetHMenu();
481 mii
.fMask
|= MIIM_ID
;
485 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
486 // 1. we can't draw it with transparency then (this is not
487 // very important now but would be with themed menu bg)
488 // 2. worse, Windows inverts the bitmap for the selected
489 // item and this looks downright ugly
491 // so instead draw it ourselves in MSWOnDrawItem()
492 mii
.dwItemData
= wx_reinterpret_cast(ULONG_PTR
, pItem
);
493 if ( pItem
->IsCheckable() )
496 mii
.hbmpUnchecked
= HBMMENU_CALLBACK
;
498 mii
.hbmpItem
= HBMMENU_CALLBACK
;
500 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
503 wxLogLastError(wxT("InsertMenuItem()"));
505 else // InsertMenuItem() ok
507 // we need to remove the extra indent which is reserved for
508 // the checkboxes by default as it looks ugly unless check
509 // boxes are used together with bitmaps and this is not the
511 WinStruct
<MENUINFO
> mi
;
513 // don't call SetMenuInfo() directly, this would prevent
514 // the app from starting up under Windows 95/NT 4
515 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
517 wxDynamicLibrary
dllUser(_T("user32"));
518 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
519 if ( pfnSetMenuInfo
)
521 mi
.fMask
= MIM_STYLE
;
522 mi
.dwStyle
= MNS_CHECKORBMP
;
523 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
524 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
527 // tell the item that it's not really owner-drawn but only
528 // needs to draw its bitmap, the rest is done by Windows
529 pItem
->ResetOwnerDrawn();
537 // item draws itself, pass pointer to it in data parameter
538 flags
|= MF_OWNERDRAW
;
539 pData
= (LPCTSTR
)pItem
;
543 #endif // wxUSE_OWNER_DRAWN
545 // item is just a normal string (passed in data parameter)
549 itemText
= wxMenuItem::GetLabelText(itemText
);
552 pData
= (wxChar
*)itemText
.wx_str();
555 // item might have already been inserted by InsertMenuItem() above
558 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
560 wxLogLastError(wxT("InsertMenu[Item]()"));
567 // if we just appended the title, highlight it
568 if ( id
== idMenuTitle
)
570 // visually select the menu title
571 SetDefaultMenuItem(GetHmenu(), id
);
574 // if we're already attached to the menubar, we must update it
575 if ( IsAttached() && GetMenuBar()->IsAttached() )
577 GetMenuBar()->Refresh();
583 void wxMenu::EndRadioGroup()
585 // we're not inside a radio group any longer
586 m_startRadioGroup
= -1;
589 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
591 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
595 if ( item
->GetKind() == wxITEM_RADIO
)
597 int count
= GetMenuItemCount();
599 if ( m_startRadioGroup
== -1 )
601 // start a new radio group
602 m_startRadioGroup
= count
;
604 // for now it has just one element
605 item
->SetAsRadioGroupStart();
606 item
->SetRadioGroupEnd(m_startRadioGroup
);
608 // ensure that we have a checked item in the radio group
611 else // extend the current radio group
613 // we need to update its end item
614 item
->SetRadioGroupStart(m_startRadioGroup
);
615 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
619 node
->GetData()->SetRadioGroupEnd(count
);
623 wxFAIL_MSG( _T("where is the radio group start item?") );
627 else // not a radio item
632 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
639 // check the item initially
646 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
648 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
654 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
656 // we need to find the item's position in the child list
658 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
659 for ( pos
= 0; node
; pos
++ )
661 if ( node
->GetData() == item
)
664 node
= node
->GetNext();
667 // DoRemove() (unlike Remove) can only be called for an existing item!
668 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
671 // remove the corresponding accel from the accel table
672 int n
= FindAccel(item
->GetId());
673 if ( n
!= wxNOT_FOUND
)
677 m_accels
.RemoveAt(n
);
679 //else: this item doesn't have an accel, nothing to do
680 #endif // wxUSE_ACCEL
682 // remove the item from the menu
683 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
685 wxLogLastError(wxT("RemoveMenu"));
688 if ( IsAttached() && GetMenuBar()->IsAttached() )
690 // otherwise, the change won't be visible
691 GetMenuBar()->Refresh();
694 // and from internal data structures
695 return wxMenuBase::DoRemove(item
);
698 // ---------------------------------------------------------------------------
699 // accelerator helpers
700 // ---------------------------------------------------------------------------
704 // create the wxAcceleratorEntries for our accels and put them into the provided
705 // array - return the number of accels we have
706 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
708 size_t count
= GetAccelCount();
709 for ( size_t n
= 0; n
< count
; n
++ )
711 *accels
++ = *m_accels
[n
];
717 #endif // wxUSE_ACCEL
719 // ---------------------------------------------------------------------------
721 // ---------------------------------------------------------------------------
723 void wxMenu::SetTitle(const wxString
& label
)
725 bool hasNoTitle
= m_title
.empty();
728 HMENU hMenu
= GetHmenu();
732 if ( !label
.empty() )
734 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
735 idMenuTitle
, m_title
.wx_str()) ||
736 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
738 wxLogLastError(wxT("InsertMenu"));
746 // remove the title and the separator after it
747 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
748 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
750 wxLogLastError(wxT("RemoveMenu"));
759 info
.cbSize
= sizeof(info
);
760 info
.fMask
= MIIM_TYPE
;
761 info
.fType
= MFT_STRING
;
762 info
.cch
= m_title
.length();
763 info
.dwTypeData
= wx_const_cast(wxChar
*, m_title
.wx_str());
764 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
766 wxLogLastError(wxT("SetMenuItemInfo"));
769 if ( !ModifyMenu(hMenu
, 0u,
770 MF_BYPOSITION
| MF_STRING
,
771 idMenuTitle
, m_title
.wx_str()) )
773 wxLogLastError(wxT("ModifyMenu"));
780 // put the title string in bold face
781 if ( !m_title
.empty() )
783 SetDefaultMenuItem(GetHmenu(), idMenuTitle
);
788 // ---------------------------------------------------------------------------
790 // ---------------------------------------------------------------------------
792 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
794 const int id
= (signed short)id_
;
796 // ignore commands from the menu title
797 if ( id
!= (int)idMenuTitle
)
799 // update the check item when it's clicked
800 wxMenuItem
* const item
= FindItem(id
);
801 if ( item
&& item
->IsCheckable() )
804 // get the status of the menu item: note that it has been just changed
805 // by Toggle() above so here we already get the new state of the item
806 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
807 SendEvent(id
, menuState
& MF_CHECKED
);
813 // ---------------------------------------------------------------------------
815 // ---------------------------------------------------------------------------
817 wxWindow
*wxMenu::GetWindow() const
819 if ( m_invokingWindow
!= NULL
)
820 return m_invokingWindow
;
821 else if ( GetMenuBar() != NULL
)
822 return GetMenuBar()->GetFrame();
827 // ---------------------------------------------------------------------------
829 // ---------------------------------------------------------------------------
831 void wxMenuBar::Init()
833 m_eventHandler
= this;
835 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
838 // Not using a combined wxToolBar/wxMenuBar? then use
839 // a commandbar in WinCE .NET just to implement the
841 #if defined(WINCE_WITH_COMMANDBAR)
843 m_adornmentsAdded
= false;
847 wxMenuBar::wxMenuBar()
852 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
857 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
861 m_titles
.Alloc(count
);
863 for ( size_t i
= 0; i
< count
; i
++ )
865 m_menus
.Append(menus
[i
]);
866 m_titles
.Add(titles
[i
]);
868 menus
[i
]->Attach(this);
872 wxMenuBar::~wxMenuBar()
874 // In Windows CE (not .NET), the menubar is always associated
875 // with a toolbar, which destroys the menu implicitly.
876 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
879 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
881 toolMenuBar
->SetMenuBar(NULL
);
884 // we should free Windows resources only if Windows doesn't do it for us
885 // which happens if we're attached to a frame
886 if (m_hMenu
&& !IsAttached())
888 #if defined(WINCE_WITH_COMMANDBAR)
889 ::DestroyWindow((HWND
) m_commandBar
);
890 m_commandBar
= (WXHWND
) NULL
;
892 ::DestroyMenu((HMENU
)m_hMenu
);
894 m_hMenu
= (WXHMENU
)NULL
;
899 // ---------------------------------------------------------------------------
901 // ---------------------------------------------------------------------------
903 void wxMenuBar::Refresh()
908 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
910 #if defined(WINCE_WITHOUT_COMMANDBAR)
913 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
915 #elif defined(WINCE_WITH_COMMANDBAR)
917 DrawMenuBar((HWND
) m_commandBar
);
919 DrawMenuBar(GetHwndOf(GetFrame()));
923 WXHMENU
wxMenuBar::Create()
925 // Note: this doesn't work at all on Smartphone,
926 // since you have to use resources.
927 // We'll have to find another way to add a menu
928 // by changing/adding menu items to an existing menu.
929 #if defined(WINCE_WITHOUT_COMMANDBAR)
933 wxToolMenuBar
* const bar
= wx_static_cast(wxToolMenuBar
*, GetToolBar());
937 HWND hCommandBar
= GetHwndOf(bar
);
939 // notify comctl32.dll about the version of the headers we use before using
940 // any other TB_XXX messages
941 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
944 wxZeroMemory(tbButton
);
945 tbButton
.iBitmap
= I_IMAGENONE
;
946 tbButton
.fsState
= TBSTATE_ENABLED
;
947 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
948 TBSTYLE_NO_DROPDOWN_ARROW
|
951 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
953 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
954 tbButton
.dwData
= (DWORD
)hPopupMenu
;
955 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
956 tbButton
.iString
= (int) label
.wx_str();
958 tbButton
.idCommand
= NewControlId();
959 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
961 wxLogLastError(wxT("TB_INSERTBUTTON"));
965 m_hMenu
= bar
->GetHMenu();
967 #else // !__WXWINCE__
971 m_hMenu
= (WXHMENU
)::CreateMenu();
975 wxLogLastError(wxT("CreateMenu"));
979 size_t count
= GetMenuCount(), i
;
980 wxMenuList::iterator it
;
981 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
983 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
984 (UINT_PTR
)(*it
)->GetHMenu(),
985 m_titles
[i
].wx_str()) )
987 wxLogLastError(wxT("AppendMenu"));
993 #endif // __WXWINCE__/!__WXWINCE__
996 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
999 wxASSERT(menu
->GetHMenu());
1002 #if defined(__WXWINCE__)
1003 int totalMSWItems
= GetMenuCount();
1005 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1008 int i
; // For old C++ compatibility
1009 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1011 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1014 for(i
=0; i
<wxpos
; i
++)
1016 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1023 // ---------------------------------------------------------------------------
1024 // wxMenuBar functions to work with the top level submenus
1025 // ---------------------------------------------------------------------------
1027 // NB: we don't support owner drawn top level items for now, if we do these
1028 // functions would have to be changed to use wxMenuItem as well
1030 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1032 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1033 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1035 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1037 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1042 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1044 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1046 m_titles
[pos
] = label
;
1048 if ( !IsAttached() )
1052 //else: have to modify the existing menu
1054 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1057 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1058 if ( flagsOld
== 0xFFFFFFFF )
1060 wxLogLastError(wxT("GetMenuState"));
1065 if ( flagsOld
& MF_POPUP
)
1067 // HIBYTE contains the number of items in the submenu in this case
1069 id
= (UINT_PTR
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1079 info
.cbSize
= sizeof(info
);
1080 info
.fMask
= MIIM_TYPE
;
1081 info
.fType
= MFT_STRING
;
1082 info
.cch
= label
.length();
1083 info
.dwTypeData
= wx_const_cast(wxChar
*, label
.wx_str());
1084 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, &info
) )
1086 wxLogLastError(wxT("SetMenuItemInfo"));
1090 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1091 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1093 wxLogLastError(wxT("ModifyMenu"));
1100 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1102 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1103 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1105 return m_titles
[pos
];
1108 // ---------------------------------------------------------------------------
1109 // wxMenuBar construction
1110 // ---------------------------------------------------------------------------
1112 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1114 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1118 m_titles
[pos
] = title
;
1120 #if defined(WINCE_WITHOUT_COMMANDBAR)
1126 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1128 // can't use ModifyMenu() because it deletes the submenu it replaces
1129 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1131 wxLogLastError(wxT("RemoveMenu"));
1134 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1135 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1136 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1138 wxLogLastError(wxT("InsertMenu"));
1142 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1144 // need to rebuild accell table
1145 RebuildAccelTable();
1147 #endif // wxUSE_ACCEL
1156 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1158 // Find out which MSW item before which we'll be inserting before
1159 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1160 // If IsAttached() is false this won't be used anyway
1162 #if defined(WINCE_WITHOUT_COMMANDBAR)
1168 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1169 ? -1 // append the menu
1170 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1172 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1175 m_titles
.Insert(title
, pos
);
1179 #if defined(WINCE_WITHOUT_COMMANDBAR)
1183 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1184 tbButton
.iBitmap
= I_IMAGENONE
;
1185 tbButton
.fsState
= TBSTATE_ENABLED
;
1186 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1188 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1189 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1190 wxString label
= wxStripMenuCodes(title
);
1191 tbButton
.iString
= (int) label
.wx_str();
1193 tbButton
.idCommand
= NewControlId();
1194 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1196 wxLogLastError(wxT("TB_INSERTBUTTON"));
1199 wxUnusedVar(mswpos
);
1201 if ( !::InsertMenu(GetHmenu(), mswpos
,
1202 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1203 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1205 wxLogLastError(wxT("InsertMenu"));
1209 if ( menu
->HasAccels() )
1211 // need to rebuild accell table
1212 RebuildAccelTable();
1214 #endif // wxUSE_ACCEL
1223 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1225 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1226 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1228 if ( !wxMenuBarBase::Append(menu
, title
) )
1231 m_titles
.Add(title
);
1233 #if defined(WINCE_WITHOUT_COMMANDBAR)
1239 #if defined(WINCE_WITHOUT_COMMANDBAR)
1243 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1244 tbButton
.iBitmap
= I_IMAGENONE
;
1245 tbButton
.fsState
= TBSTATE_ENABLED
;
1246 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1248 size_t pos
= GetMenuCount();
1249 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1250 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1251 wxString label
= wxStripMenuCodes(title
);
1252 tbButton
.iString
= (int) label
.wx_str();
1254 tbButton
.idCommand
= NewControlId();
1255 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1257 wxLogLastError(wxT("TB_INSERTBUTTON"));
1261 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1262 (UINT_PTR
)submenu
, title
.wx_str()) )
1264 wxLogLastError(wxT("AppendMenu"));
1269 if ( menu
->HasAccels() )
1271 // need to rebuild accelerator table
1272 RebuildAccelTable();
1274 #endif // wxUSE_ACCEL
1283 wxMenu
*wxMenuBar::Remove(size_t pos
)
1285 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1289 #if defined(WINCE_WITHOUT_COMMANDBAR)
1295 #if defined(WINCE_WITHOUT_COMMANDBAR)
1298 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1300 wxLogLastError(wxT("TB_DELETEBUTTON"));
1304 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1306 wxLogLastError(wxT("RemoveMenu"));
1311 if ( menu
->HasAccels() )
1313 // need to rebuild accell table
1314 RebuildAccelTable();
1316 #endif // wxUSE_ACCEL
1322 m_titles
.RemoveAt(pos
);
1329 void wxMenuBar::RebuildAccelTable()
1331 // merge the accelerators of all menus into one accel table
1332 size_t nAccelCount
= 0;
1333 size_t i
, count
= GetMenuCount();
1334 wxMenuList::iterator it
;
1335 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1337 nAccelCount
+= (*it
)->GetAccelCount();
1342 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1345 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1347 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1350 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1352 delete [] accelEntries
;
1356 #endif // wxUSE_ACCEL
1358 void wxMenuBar::Attach(wxFrame
*frame
)
1360 wxMenuBarBase::Attach(frame
);
1362 #if defined(WINCE_WITH_COMMANDBAR)
1366 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1371 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1373 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1380 RebuildAccelTable();
1381 #endif // wxUSE_ACCEL
1384 #if defined(WINCE_WITH_COMMANDBAR)
1385 bool wxMenuBar::AddAdornments(long style
)
1387 if (m_adornmentsAdded
|| !m_commandBar
)
1390 if (style
& wxCLOSE_BOX
)
1392 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1393 wxLogLastError(wxT("CommandBar_AddAdornments"));
1401 void wxMenuBar::Detach()
1403 wxMenuBarBase::Detach();
1406 #endif // wxUSE_MENUS