1 /////////////////////////////////////////////////////////////////////////////
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "menu.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
42 #include "wx/ownerdrw.h"
45 #include "wx/msw/private.h"
54 #if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__)
58 #include "wx/msw/wince/missing.h"
62 // other standard headers
65 #if wxUSE_OWNER_DRAWN && defined(MIIM_BITMAP)
66 #include "wx/dynlib.h"
69 #ifndef MNS_CHECKORBMP
70 #define MNS_CHECKORBMP 0x04000000
73 #define MIM_STYLE 0x00000010
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 // the (popup) menu title has this special id
85 static const int idMenuTitle
= -3;
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 // make the given menu item default
92 static void SetDefaultMenuItem(HMENU
WXUNUSED_IN_WINCE(hmenu
),
93 UINT
WXUNUSED_IN_WINCE(id
))
98 mii
.cbSize
= sizeof(MENUITEMINFO
);
99 mii
.fMask
= MIIM_STATE
;
100 mii
.fState
= MFS_DEFAULT
;
102 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
104 wxLogLastError(wxT("SetMenuItemInfo"));
110 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
114 info
.cbSize
= sizeof(info
);
115 info
.fMask
= MIIM_STATE
;
116 // MF_BYCOMMAND is zero so test MF_BYPOSITION
117 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
118 wxLogLastError(wxT("GetMenuItemInfo"));
123 // ============================================================================
125 // ============================================================================
127 #include <wx/listimpl.cpp>
129 WX_DEFINE_LIST( wxMenuInfoList
) ;
131 #if wxUSE_EXTENDED_RTTI
133 WX_DEFINE_FLAGS( wxMenuStyle
)
135 wxBEGIN_FLAGS( wxMenuStyle
)
136 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
137 wxEND_FLAGS( wxMenuStyle
)
139 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
141 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
143 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
145 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
148 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
149 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
150 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
151 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
152 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
153 wxEND_PROPERTIES_TABLE()
155 wxBEGIN_HANDLERS_TABLE(wxMenu
)
156 wxEND_HANDLERS_TABLE()
158 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
160 WX_DEFINE_FLAGS( wxMenuBarStyle
)
162 wxBEGIN_FLAGS( wxMenuBarStyle
)
163 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
164 wxEND_FLAGS( wxMenuBarStyle
)
166 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
167 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
172 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
174 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
176 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
177 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
178 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
179 wxEND_PROPERTIES_TABLE()
181 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
182 wxEND_HANDLERS_TABLE()
184 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
186 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
188 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
190 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
193 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
194 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
195 wxEND_PROPERTIES_TABLE()
197 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
198 wxEND_HANDLERS_TABLE()
200 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
203 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
204 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
205 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
208 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
210 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
211 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
212 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
214 wxMenuInfo
* info
= new wxMenuInfo() ;
215 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetLabelTop(i
) ) ;
216 list
->Append( info
) ;
221 // ---------------------------------------------------------------------------
222 // wxMenu construction, adding and removing menu items
223 // ---------------------------------------------------------------------------
225 // Construct a menu with optional title (then use append)
229 m_startRadioGroup
= -1;
232 m_hMenu
= (WXHMENU
)CreatePopupMenu();
235 wxLogLastError(wxT("CreatePopupMenu"));
238 // if we have a title, insert it in the beginning of the menu
239 if ( !m_title
.empty() )
241 Append(idMenuTitle
, m_title
);
246 // The wxWindow destructor will take care of deleting the submenus.
249 // we should free Windows resources only if Windows doesn't do it for us
250 // which happens if we're attached to a menubar or a submenu of another
252 if ( !IsAttached() && !GetParent() )
254 if ( !::DestroyMenu(GetHmenu()) )
256 wxLogLastError(wxT("DestroyMenu"));
262 WX_CLEAR_ARRAY(m_accels
);
263 #endif // wxUSE_ACCEL
268 // this will take effect during the next call to Append()
272 void wxMenu::Attach(wxMenuBarBase
*menubar
)
274 wxMenuBase::Attach(menubar
);
281 int wxMenu::FindAccel(int id
) const
283 size_t n
, count
= m_accels
.GetCount();
284 for ( n
= 0; n
< count
; n
++ )
286 if ( m_accels
[n
]->m_command
== id
)
293 void wxMenu::UpdateAccel(wxMenuItem
*item
)
295 if ( item
->IsSubMenu() )
297 wxMenu
*submenu
= item
->GetSubMenu();
298 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
301 UpdateAccel(node
->GetData());
303 node
= node
->GetNext();
306 else if ( !item
->IsSeparator() )
308 // recurse upwards: we should only modify m_accels of the top level
309 // menus, not of the submenus as wxMenuBar doesn't look at them
310 // (alternative and arguable cleaner solution would be to recurse
311 // downwards in GetAccelCount() and CopyAccels())
314 GetParent()->UpdateAccel(item
);
318 // find the (new) accel for this item
319 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
321 accel
->m_command
= item
->GetId();
324 int n
= FindAccel(item
->GetId());
325 if ( n
== wxNOT_FOUND
)
327 // no old, add new if any
331 return; // skipping RebuildAccelTable() below
335 // replace old with new or just remove the old one if no new
340 m_accels
.RemoveAt(n
);
345 GetMenuBar()->RebuildAccelTable();
348 //else: it is a separator, they can't have accels, nothing to do
351 #endif // wxUSE_ACCEL
353 // append a new item or submenu to the menu
354 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
358 #endif // wxUSE_ACCEL
362 // if "Break" has just been called, insert a menu break before this item
363 // (and don't forget to reset the flag)
365 flags
|= MF_MENUBREAK
;
369 if ( pItem
->IsSeparator() ) {
370 flags
|= MF_SEPARATOR
;
373 // id is the numeric id for normal menu items and HMENU for submenus as
374 // required by ::AppendMenu() API
376 wxMenu
*submenu
= pItem
->GetSubMenu();
377 if ( submenu
!= NULL
) {
378 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
380 submenu
->SetParent(this);
382 id
= (UINT
)submenu
->GetHMenu();
391 // prepare to insert the item in the menu
392 wxString itemText
= pItem
->GetText();
393 LPCTSTR pData
= NULL
;
394 if ( pos
== (size_t)-1 )
396 // append at the end (note that the item is already appended to
397 // internal data structures)
398 pos
= GetMenuItemCount() - 1;
401 // adjust position to account for the title, if any
402 if ( !m_title
.empty() )
403 pos
+= 2; // for the title itself and its separator
407 // check if we have something more than a simple text item
408 #if wxUSE_OWNER_DRAWN
409 if ( pItem
->IsOwnerDrawn() )
411 // is the item owner-drawn just because of the bitmap?
412 if ( pItem
->GetBitmap().Ok() &&
413 !pItem
->GetTextColour().Ok() &&
414 !pItem
->GetBackgroundColour().Ok() &&
415 !pItem
->GetFont().Ok() )
417 // try to use InsertMenuItem() as it's guaranteed to look correctly
418 // while our owner-drawning code is not
420 // first compile-time check
422 WinStruct
<MENUITEMINFO
> mii
;
424 // now run-time one: MIIM_BITMAP only works under WinME/2000+
425 if ( wxGetWinVersion() >= wxWinVersion_98
)
427 mii
.fMask
= MIIM_ID
| MIIM_STRING
| MIIM_DATA
| MIIM_BITMAP
;
429 mii
.cch
= itemText
.length();
430 mii
.dwTypeData
= wx_const_cast(wxChar
*, itemText
.c_str());
432 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
433 // 1. we can't draw it with transparency then (this is not
434 // very important now but would be with themed menu bg)
435 // 2. worse, Windows inverses the bitmap for the selected
436 // item and this looks downright ugly
438 // so instead draw it ourselves in MSWOnDrawItem()
439 mii
.dwItemData
= wx_reinterpret_cast(ULONG_PTR
, pItem
);
440 mii
.hbmpItem
= HBMMENU_CALLBACK
;
442 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
445 wxLogLastError(wxT("InsertMenuItem()"));
447 else // InsertMenuItem() ok
449 // we need to remove the extra indent which is reserved for
450 // the checkboxes by default as it looks ugly unless check
451 // boxes are used together with bitmaps and this is not the
453 WinStruct
<MENUINFO
> mi
;
455 // don't call SetMenuInfo() directly, this would prevent
456 // the app from starting up under Windows 95/NT 4
457 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
459 wxDynamicLibrary
dllUser(_T("user32"));
460 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
461 if ( pfnSetMenuInfo
)
463 mi
.fMask
= MIM_STYLE
;
464 mi
.dwStyle
= MNS_CHECKORBMP
;
465 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
466 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
469 // tell the item that it's not really owner-drawn but only
470 // needs to draw its bitmap, the rest is done by Windows
471 pItem
->ResetOwnerDrawn();
474 #endif // MIIM_BITMAP
479 // item draws itself, pass pointer to it in data parameter
480 flags
|= MF_OWNERDRAW
;
481 pData
= (LPCTSTR
)pItem
;
485 #endif // wxUSE_OWNER_DRAWN
487 // menu is just a normal string (passed in data parameter)
491 itemText
= wxMenuItem::GetLabelFromText(itemText
);
494 pData
= (wxChar
*)itemText
.c_str();
497 // item might have been already inserted by InsertMenuItem() above
500 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
502 wxLogLastError(wxT("InsertMenu[Item]()"));
509 // if we just appended the title, highlight it
510 if ( (int)id
== idMenuTitle
)
512 // visually select the menu title
513 SetDefaultMenuItem(GetHmenu(), id
);
516 // if we're already attached to the menubar, we must update it
517 if ( IsAttached() && GetMenuBar()->IsAttached() )
519 GetMenuBar()->Refresh();
525 void wxMenu::EndRadioGroup()
527 // we're not inside a radio group any longer
528 m_startRadioGroup
= -1;
531 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
533 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
537 if ( item
->GetKind() == wxITEM_RADIO
)
539 int count
= GetMenuItemCount();
541 if ( m_startRadioGroup
== -1 )
543 // start a new radio group
544 m_startRadioGroup
= count
;
546 // for now it has just one element
547 item
->SetAsRadioGroupStart();
548 item
->SetRadioGroupEnd(m_startRadioGroup
);
550 // ensure that we have a checked item in the radio group
553 else // extend the current radio group
555 // we need to update its end item
556 item
->SetRadioGroupStart(m_startRadioGroup
);
557 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
561 node
->GetData()->SetRadioGroupEnd(count
);
565 wxFAIL_MSG( _T("where is the radio group start item?") );
569 else // not a radio item
574 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
581 // check the item initially
588 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
590 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
596 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
598 // we need to find the items position in the child list
600 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
601 for ( pos
= 0; node
; pos
++ )
603 if ( node
->GetData() == item
)
606 node
= node
->GetNext();
609 // DoRemove() (unlike Remove) can only be called for existing item!
610 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
613 // remove the corresponding accel from the accel table
614 int n
= FindAccel(item
->GetId());
615 if ( n
!= wxNOT_FOUND
)
619 m_accels
.RemoveAt(n
);
621 //else: this item doesn't have an accel, nothing to do
622 #endif // wxUSE_ACCEL
624 // remove the item from the menu
625 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
627 wxLogLastError(wxT("RemoveMenu"));
630 if ( IsAttached() && GetMenuBar()->IsAttached() )
632 // otherwise, the chane won't be visible
633 GetMenuBar()->Refresh();
636 // and from internal data structures
637 return wxMenuBase::DoRemove(item
);
640 // ---------------------------------------------------------------------------
641 // accelerator helpers
642 // ---------------------------------------------------------------------------
646 // create the wxAcceleratorEntries for our accels and put them into provided
647 // array - return the number of accels we have
648 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
650 size_t count
= GetAccelCount();
651 for ( size_t n
= 0; n
< count
; n
++ )
653 *accels
++ = *m_accels
[n
];
659 #endif // wxUSE_ACCEL
661 // ---------------------------------------------------------------------------
663 // ---------------------------------------------------------------------------
665 void wxMenu::SetTitle(const wxString
& label
)
667 bool hasNoTitle
= m_title
.empty();
670 HMENU hMenu
= GetHmenu();
674 if ( !label
.empty() )
676 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
677 (unsigned)idMenuTitle
, m_title
) ||
678 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
680 wxLogLastError(wxT("InsertMenu"));
688 // remove the title and the separator after it
689 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
690 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
692 wxLogLastError(wxT("RemoveMenu"));
701 info
.cbSize
= sizeof(info
);
702 info
.fMask
= MIIM_TYPE
;
703 info
.fType
= MFT_STRING
;
704 info
.cch
= m_title
.Length();
705 info
.dwTypeData
= (LPTSTR
) m_title
.c_str();
706 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
708 wxLogLastError(wxT("SetMenuItemInfo"));
711 if ( !ModifyMenu(hMenu
, 0u,
712 MF_BYPOSITION
| MF_STRING
,
713 (unsigned)idMenuTitle
, m_title
) )
715 wxLogLastError(wxT("ModifyMenu"));
722 // put the title string in bold face
723 if ( !m_title
.empty() )
725 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
730 // ---------------------------------------------------------------------------
732 // ---------------------------------------------------------------------------
734 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
736 // ignore commands from the menu title
737 if ( id
!= (WXWORD
)idMenuTitle
)
739 // get the checked status of the command: notice that menuState is the
740 // old state of the menu, so the test for MF_CHECKED must be inverted
741 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
742 SendEvent(id
, !(menuState
& MF_CHECKED
));
748 // ---------------------------------------------------------------------------
750 // ---------------------------------------------------------------------------
752 wxWindow
*wxMenu::GetWindow() const
754 if ( m_invokingWindow
!= NULL
)
755 return m_invokingWindow
;
756 else if ( GetMenuBar() != NULL
)
757 return GetMenuBar()->GetFrame();
762 // ---------------------------------------------------------------------------
764 // ---------------------------------------------------------------------------
766 void wxMenuBar::Init()
768 m_eventHandler
= this;
770 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
773 // Not using a combined wxToolBar/wxMenuBar? then use
774 // a commandbar in WinCE .NET just to implement the
776 #if defined(WINCE_WITH_COMMANDBAR)
778 m_adornmentsAdded
= false;
782 wxMenuBar::wxMenuBar()
787 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
792 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
796 m_titles
.Alloc(count
);
798 for ( size_t i
= 0; i
< count
; i
++ )
800 m_menus
.Append(menus
[i
]);
801 m_titles
.Add(titles
[i
]);
803 menus
[i
]->Attach(this);
807 wxMenuBar::~wxMenuBar()
809 // In Windows CE (not .NET), the menubar is always associated
810 // with a toolbar, which destroys the menu implicitly.
811 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
814 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
816 toolMenuBar
->SetMenuBar(NULL
);
819 // we should free Windows resources only if Windows doesn't do it for us
820 // which happens if we're attached to a frame
821 if (m_hMenu
&& !IsAttached())
823 #if defined(WINCE_WITH_COMMANDBAR)
824 ::DestroyWindow((HWND
) m_commandBar
);
825 m_commandBar
= (WXHWND
) NULL
;
827 ::DestroyMenu((HMENU
)m_hMenu
);
829 m_hMenu
= (WXHMENU
)NULL
;
834 // ---------------------------------------------------------------------------
836 // ---------------------------------------------------------------------------
838 void wxMenuBar::Refresh()
840 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
842 #if defined(WINCE_WITHOUT_COMMANDBAR)
845 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
847 #elif defined(WINCE_WITH_COMMANDBAR)
849 DrawMenuBar((HWND
) m_commandBar
);
851 DrawMenuBar(GetHwndOf(GetFrame()));
855 WXHMENU
wxMenuBar::Create()
857 // Note: this totally doesn't work on Smartphone,
858 // since you have to use resources.
859 // We'll have to find another way to add a menu
860 // by changing/adding menu items to an existing menu.
861 #if defined(WINCE_WITHOUT_COMMANDBAR)
868 HWND hCommandBar
= (HWND
) GetToolBar()->GetHWND();
869 HMENU hMenu
= (HMENU
)::SendMessage(hCommandBar
, SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
873 memset(&tbButton
, 0, sizeof(TBBUTTON
));
874 tbButton
.iBitmap
= I_IMAGENONE
;
875 tbButton
.fsState
= TBSTATE_ENABLED
;
876 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
879 for (i
= 0; i
< GetMenuCount(); i
++)
881 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu() ;
882 tbButton
.dwData
= (DWORD
)hPopupMenu
;
883 wxString label
= wxStripMenuCodes(GetLabelTop(i
));
884 tbButton
.iString
= (int) label
.c_str();
888 tbButton
.idCommand
= NewControlId();
889 if (!::SendMessage(hCommandBar
, TB_INSERTBUTTON
, position
, (LPARAM
)&tbButton
))
891 wxLogLastError(wxT("TB_INSERTBUTTON"));
895 m_hMenu
= (WXHMENU
) hMenu
;
901 m_hMenu
= (WXHMENU
)::CreateMenu();
905 wxLogLastError(wxT("CreateMenu"));
909 size_t count
= GetMenuCount(), i
;
910 wxMenuList::iterator it
;
911 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
913 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
914 (UINT
)(*it
)->GetHMenu(),
917 wxLogLastError(wxT("AppendMenu"));
926 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
929 wxASSERT(menu
->GetHMenu());
932 #if defined(__WXWINCE__)
933 int totalMSWItems
= GetMenuCount();
935 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
938 int i
; // For old C++ compatibility
939 for(i
=wxpos
; i
<totalMSWItems
; i
++)
941 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
944 for(i
=0; i
<wxpos
; i
++)
946 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
953 // ---------------------------------------------------------------------------
954 // wxMenuBar functions to work with the top level submenus
955 // ---------------------------------------------------------------------------
957 // NB: we don't support owner drawn top level items for now, if we do these
958 // functions would have to be changed to use wxMenuItem as well
960 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
962 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
963 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
965 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
967 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
972 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
974 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
976 m_titles
[pos
] = label
;
982 //else: have to modify the existing menu
984 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
987 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
988 if ( flagsOld
== 0xFFFFFFFF )
990 wxLogLastError(wxT("GetMenuState"));
995 if ( flagsOld
& MF_POPUP
)
997 // HIBYTE contains the number of items in the submenu in this case
999 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1009 info
.cbSize
= sizeof(info
);
1010 info
.fMask
= MIIM_TYPE
;
1011 info
.fType
= MFT_STRING
;
1012 info
.cch
= label
.Length();
1013 info
.dwTypeData
= (LPTSTR
) label
.c_str();
1014 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, & info
) )
1016 wxLogLastError(wxT("SetMenuItemInfo"));
1020 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1021 id
, label
) == (int)0xFFFFFFFF )
1023 wxLogLastError(wxT("ModifyMenu"));
1030 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1032 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1033 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
1035 return wxMenuItem::GetLabelFromText(m_titles
[pos
]);
1038 // ---------------------------------------------------------------------------
1039 // wxMenuBar construction
1040 // ---------------------------------------------------------------------------
1042 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1044 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1048 m_titles
[pos
] = title
;
1052 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1054 // can't use ModifyMenu() because it deletes the submenu it replaces
1055 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1057 wxLogLastError(wxT("RemoveMenu"));
1060 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1061 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1062 (UINT
)GetHmenuOf(menu
), title
) )
1064 wxLogLastError(wxT("InsertMenu"));
1068 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1070 // need to rebuild accell table
1071 RebuildAccelTable();
1073 #endif // wxUSE_ACCEL
1081 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1083 // Find out which MSW item before which we'll be inserting before
1084 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1085 // If IsAttached() is false this won't be used anyway
1086 int mswpos
= (!IsAttached() || (pos
== m_menus
.GetCount()))
1087 ? -1 // append the menu
1088 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1090 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1093 m_titles
.Insert(title
, pos
);
1097 #if defined(WINCE_WITHOUT_COMMANDAR)
1101 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1102 tbButton
.iBitmap
= I_IMAGENONE
;
1103 tbButton
.fsState
= TBSTATE_ENABLED
;
1104 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1106 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1107 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1108 wxString label
= wxStripMenuCodes(title
);
1109 tbButton
.iString
= (int) label
.c_str();
1111 tbButton
.idCommand
= NewControlId();
1112 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1114 wxLogLastError(wxT("TB_INSERTBUTTON"));
1118 if ( !::InsertMenu(GetHmenu(), mswpos
,
1119 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1120 (UINT
)GetHmenuOf(menu
), title
) )
1122 wxLogLastError(wxT("InsertMenu"));
1126 if ( menu
->HasAccels() )
1128 // need to rebuild accell table
1129 RebuildAccelTable();
1131 #endif // wxUSE_ACCEL
1139 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1141 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1142 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1144 if ( !wxMenuBarBase::Append(menu
, title
) )
1147 m_titles
.Add(title
);
1151 #if defined(WINCE_WITHOUT_COMMANDAR)
1155 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1156 tbButton
.iBitmap
= I_IMAGENONE
;
1157 tbButton
.fsState
= TBSTATE_ENABLED
;
1158 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1160 size_t pos
= GetMenuCount();
1161 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1162 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1163 wxString label
= wxStripMenuCodes(title
);
1164 tbButton
.iString
= (int) label
.c_str();
1166 tbButton
.idCommand
= NewControlId();
1167 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1169 wxLogLastError(wxT("TB_INSERTBUTTON"));
1173 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1174 (UINT
)submenu
, title
) )
1176 wxLogLastError(wxT("AppendMenu"));
1181 if ( menu
->HasAccels() )
1183 // need to rebuild accelerator table
1184 RebuildAccelTable();
1186 #endif // wxUSE_ACCEL
1194 wxMenu
*wxMenuBar::Remove(size_t pos
)
1196 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1202 #if defined(WINCE_WITHOUT_COMMANDAR)
1205 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1207 wxLogLastError(wxT("TB_DELETEBUTTON"));
1211 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1213 wxLogLastError(wxT("RemoveMenu"));
1218 if ( menu
->HasAccels() )
1220 // need to rebuild accell table
1221 RebuildAccelTable();
1223 #endif // wxUSE_ACCEL
1229 m_titles
.RemoveAt(pos
);
1236 void wxMenuBar::RebuildAccelTable()
1238 // merge the accelerators of all menus into one accel table
1239 size_t nAccelCount
= 0;
1240 size_t i
, count
= GetMenuCount();
1241 wxMenuList::iterator it
;
1242 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1244 nAccelCount
+= (*it
)->GetAccelCount();
1249 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1252 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1254 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1257 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1259 delete [] accelEntries
;
1263 #endif // wxUSE_ACCEL
1265 void wxMenuBar::Attach(wxFrame
*frame
)
1267 wxMenuBarBase::Attach(frame
);
1269 #if defined(WINCE_WITH_COMMANDBAR)
1273 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1278 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1280 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1287 RebuildAccelTable();
1288 #endif // wxUSE_ACCEL
1291 #if defined(WINCE_WITH_COMMANDBAR)
1292 bool wxMenuBar::AddAdornments(long style
)
1294 if (m_adornmentsAdded
|| !m_commandBar
)
1297 if (style
& wxCLOSE_BOX
)
1299 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1300 wxLogLastError(wxT("CommandBar_AddAdornments"));
1308 void wxMenuBar::Detach()
1310 wxMenuBarBase::Detach();
1313 #endif // wxUSE_MENUS