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 #ifndef MNS_CHECKORBMP
66 #define MNS_CHECKORBMP 0x04000000
69 #define MIM_STYLE 0x00000010
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 // the (popup) menu title has this special id
81 static const int idMenuTitle
= -3;
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 // make the given menu item default
88 static void SetDefaultMenuItem(HMENU hmenu
, UINT id
)
93 mii
.cbSize
= sizeof(MENUITEMINFO
);
94 mii
.fMask
= MIIM_STATE
;
95 mii
.fState
= MFS_DEFAULT
;
97 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
99 wxLogLastError(wxT("SetMenuItemInfo"));
108 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
112 info
.cbSize
= sizeof(info
);
113 info
.fMask
= MIIM_STATE
;
114 // MF_BYCOMMAND is zero so test MF_BYPOSITION
115 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
116 wxLogLastError(wxT("GetMenuItemInfo"));
121 // ============================================================================
123 // ============================================================================
125 #include <wx/listimpl.cpp>
127 WX_DEFINE_LIST( wxMenuInfoList
) ;
129 #if wxUSE_EXTENDED_RTTI
131 WX_DEFINE_FLAGS( wxMenuStyle
)
133 wxBEGIN_FLAGS( wxMenuStyle
)
134 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
135 wxEND_FLAGS( wxMenuStyle
)
137 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
139 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
141 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
143 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
146 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
147 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
148 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
149 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
150 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
151 wxEND_PROPERTIES_TABLE()
153 wxBEGIN_HANDLERS_TABLE(wxMenu
)
154 wxEND_HANDLERS_TABLE()
156 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
158 WX_DEFINE_FLAGS( wxMenuBarStyle
)
160 wxBEGIN_FLAGS( wxMenuBarStyle
)
161 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
162 wxEND_FLAGS( wxMenuBarStyle
)
164 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
165 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
170 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
172 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
174 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
175 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
176 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
177 wxEND_PROPERTIES_TABLE()
179 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
180 wxEND_HANDLERS_TABLE()
182 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
184 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
186 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
188 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
191 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
192 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
193 wxEND_PROPERTIES_TABLE()
195 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
196 wxEND_HANDLERS_TABLE()
198 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
201 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
202 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
203 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
206 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
208 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
209 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
210 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
212 wxMenuInfo
* info
= new wxMenuInfo() ;
213 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetLabelTop(i
) ) ;
214 list
->Append( info
) ;
219 // ---------------------------------------------------------------------------
220 // wxMenu construction, adding and removing menu items
221 // ---------------------------------------------------------------------------
223 // Construct a menu with optional title (then use append)
227 m_startRadioGroup
= -1;
230 m_hMenu
= (WXHMENU
)CreatePopupMenu();
233 wxLogLastError(wxT("CreatePopupMenu"));
236 // if we have a title, insert it in the beginning of the menu
237 if ( !m_title
.empty() )
239 Append(idMenuTitle
, m_title
);
244 // The wxWindow destructor will take care of deleting the submenus.
247 // we should free Windows resources only if Windows doesn't do it for us
248 // which happens if we're attached to a menubar or a submenu of another
250 if ( !IsAttached() && !GetParent() )
252 if ( !::DestroyMenu(GetHmenu()) )
254 wxLogLastError(wxT("DestroyMenu"));
260 WX_CLEAR_ARRAY(m_accels
);
261 #endif // wxUSE_ACCEL
266 // this will take effect during the next call to Append()
270 void wxMenu::Attach(wxMenuBarBase
*menubar
)
272 wxMenuBase::Attach(menubar
);
279 int wxMenu::FindAccel(int id
) const
281 size_t n
, count
= m_accels
.GetCount();
282 for ( n
= 0; n
< count
; n
++ )
284 if ( m_accels
[n
]->m_command
== id
)
291 void wxMenu::UpdateAccel(wxMenuItem
*item
)
293 if ( item
->IsSubMenu() )
295 wxMenu
*submenu
= item
->GetSubMenu();
296 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
299 UpdateAccel(node
->GetData());
301 node
= node
->GetNext();
304 else if ( !item
->IsSeparator() )
306 // find the (new) accel for this item
307 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
309 accel
->m_command
= item
->GetId();
312 int n
= FindAccel(item
->GetId());
313 if ( n
== wxNOT_FOUND
)
315 // no old, add new if any
319 return; // skipping RebuildAccelTable() below
323 // replace old with new or just remove the old one if no new
328 m_accels
.RemoveAt(n
);
333 GetMenuBar()->RebuildAccelTable();
336 //else: it is a separator, they can't have accels, nothing to do
339 #endif // wxUSE_ACCEL
341 // append a new item or submenu to the menu
342 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
346 #endif // wxUSE_ACCEL
350 // if "Break" has just been called, insert a menu break before this item
351 // (and don't forget to reset the flag)
353 flags
|= MF_MENUBREAK
;
357 if ( pItem
->IsSeparator() ) {
358 flags
|= MF_SEPARATOR
;
361 // id is the numeric id for normal menu items and HMENU for submenus as
362 // required by ::AppendMenu() API
364 wxMenu
*submenu
= pItem
->GetSubMenu();
365 if ( submenu
!= NULL
) {
366 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
368 submenu
->SetParent(this);
370 id
= (UINT
)submenu
->GetHMenu();
379 // prepare to insert the item in the menu
380 wxString itemText
= pItem
->GetText();
381 LPCTSTR pData
= NULL
;
382 if ( pos
== (size_t)-1 )
385 pos
= ::GetMenuItemCount(GetHmenu());
390 // check if we have something more than a simple text item
391 #if wxUSE_OWNER_DRAWN
392 if ( pItem
->IsOwnerDrawn() )
394 // is the item owner-drawn just because of the bitmap?
395 if ( pItem
->GetBitmap().Ok() &&
396 !pItem
->GetTextColour().Ok() &&
397 !pItem
->GetBackgroundColour().Ok() &&
398 !pItem
->GetFont().Ok() )
400 // try to use InsertMenuItem() as it's guaranteed to look correctly
401 // while our owner-drawning code is not
403 // first compile-time check
405 WinStruct
<MENUITEMINFO
> mii
;
407 // now run-time one: MIIM_BITMAP only works under WinME/2000+
408 if ( wxGetWinVersion() >= wxWinVersion_98
)
410 mii
.fMask
= MIIM_ID
| MIIM_STRING
| MIIM_DATA
| MIIM_BITMAP
;
412 mii
.cch
= itemText
.length();
413 mii
.dwTypeData
= wx_const_cast(wxChar
*, itemText
.c_str());
415 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
416 // 1. we can't draw it with transparency then (this is not
417 // very important now but would be with themed menu bg)
418 // 2. worse, Windows inverses the bitmap for the selected
419 // item and this looks downright ugly
421 // so instead draw it ourselves in MSWOnDrawItem()
422 mii
.dwItemData
= wx_reinterpret_cast(ULONG_PTR
, pItem
);
423 mii
.hbmpItem
= HBMMENU_CALLBACK
;
425 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
428 wxLogLastError(wxT("InsertMenuItem()"));
430 else // InsertMenuItem() ok
432 // we need to remove the extra indent which is reserved for
433 // the checkboxes by default as it looks ugly unless check
434 // boxes are used together with bitmaps and this is not the
436 WinStruct
<MENUINFO
> mi
;
438 mi
.fMask
= MIM_STYLE
;
439 mi
.dwStyle
= MNS_CHECKORBMP
;
440 if ( !::SetMenuInfo(GetHmenu(), &mi
) )
441 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
443 // tell the item that it's not really owner-drawn but only
444 // needs to draw its bitmap, the rest is done by Windows
445 pItem
->ResetOwnerDrawn();
448 #endif // MIIM_BITMAP
453 // item draws itself, pass pointer to it in data parameter
454 flags
|= MF_OWNERDRAW
;
455 pData
= (LPCTSTR
)pItem
;
459 #endif // wxUSE_OWNER_DRAWN
461 // menu is just a normal string (passed in data parameter)
465 itemText
= wxMenuItem::GetLabelFromText(itemText
);
468 pData
= (wxChar
*)itemText
.c_str();
471 // item might have been already inserted by InsertMenuItem() above
474 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
476 wxLogLastError(wxT("InsertMenu[Item]()"));
483 // if we just appended the title, highlight it
484 if ( (int)id
== idMenuTitle
)
486 // visually select the menu title
487 SetDefaultMenuItem(GetHmenu(), id
);
490 // if we're already attached to the menubar, we must update it
491 if ( IsAttached() && GetMenuBar()->IsAttached() )
493 GetMenuBar()->Refresh();
499 void wxMenu::EndRadioGroup()
501 // we're not inside a radio group any longer
502 m_startRadioGroup
= -1;
505 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
507 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
511 if ( item
->GetKind() == wxITEM_RADIO
)
513 int count
= GetMenuItemCount();
515 if ( m_startRadioGroup
== -1 )
517 // start a new radio group
518 m_startRadioGroup
= count
;
520 // for now it has just one element
521 item
->SetAsRadioGroupStart();
522 item
->SetRadioGroupEnd(m_startRadioGroup
);
524 // ensure that we have a checked item in the radio group
527 else // extend the current radio group
529 // we need to update its end item
530 item
->SetRadioGroupStart(m_startRadioGroup
);
531 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
535 node
->GetData()->SetRadioGroupEnd(count
);
539 wxFAIL_MSG( _T("where is the radio group start item?") );
543 else // not a radio item
548 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
555 // check the item initially
562 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
564 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
570 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
572 // we need to find the items position in the child list
574 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
575 for ( pos
= 0; node
; pos
++ )
577 if ( node
->GetData() == item
)
580 node
= node
->GetNext();
583 // DoRemove() (unlike Remove) can only be called for existing item!
584 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
587 // remove the corresponding accel from the accel table
588 int n
= FindAccel(item
->GetId());
589 if ( n
!= wxNOT_FOUND
)
593 m_accels
.RemoveAt(n
);
595 //else: this item doesn't have an accel, nothing to do
596 #endif // wxUSE_ACCEL
598 // remove the item from the menu
599 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
601 wxLogLastError(wxT("RemoveMenu"));
604 if ( IsAttached() && GetMenuBar()->IsAttached() )
606 // otherwise, the chane won't be visible
607 GetMenuBar()->Refresh();
610 // and from internal data structures
611 return wxMenuBase::DoRemove(item
);
614 // ---------------------------------------------------------------------------
615 // accelerator helpers
616 // ---------------------------------------------------------------------------
620 // create the wxAcceleratorEntries for our accels and put them into provided
621 // array - return the number of accels we have
622 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
624 size_t count
= GetAccelCount();
625 for ( size_t n
= 0; n
< count
; n
++ )
627 *accels
++ = *m_accels
[n
];
633 #endif // wxUSE_ACCEL
635 // ---------------------------------------------------------------------------
637 // ---------------------------------------------------------------------------
639 void wxMenu::SetTitle(const wxString
& label
)
641 bool hasNoTitle
= m_title
.empty();
644 HMENU hMenu
= GetHmenu();
648 if ( !label
.empty() )
650 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
651 (unsigned)idMenuTitle
, m_title
) ||
652 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
654 wxLogLastError(wxT("InsertMenu"));
662 // remove the title and the separator after it
663 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
664 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
666 wxLogLastError(wxT("RemoveMenu"));
675 info
.cbSize
= sizeof(info
);
676 info
.fMask
= MIIM_TYPE
;
677 info
.fType
= MFT_STRING
;
678 info
.cch
= m_title
.Length();
679 info
.dwTypeData
= (LPTSTR
) m_title
.c_str();
680 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
682 wxLogLastError(wxT("SetMenuItemInfo"));
685 if ( !ModifyMenu(hMenu
, 0u,
686 MF_BYPOSITION
| MF_STRING
,
687 (unsigned)idMenuTitle
, m_title
) )
689 wxLogLastError(wxT("ModifyMenu"));
696 // put the title string in bold face
697 if ( !m_title
.empty() )
699 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
704 // ---------------------------------------------------------------------------
706 // ---------------------------------------------------------------------------
708 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
710 // ignore commands from the menu title
711 if ( id
!= (WXWORD
)idMenuTitle
)
713 // get the checked status of the command: notice that menuState is the
714 // old state of the menu, so the test for MF_CHECKED must be inverted
715 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
716 SendEvent(id
, !(menuState
& MF_CHECKED
));
722 // ---------------------------------------------------------------------------
724 // ---------------------------------------------------------------------------
726 wxWindow
*wxMenu::GetWindow() const
728 if ( m_invokingWindow
!= NULL
)
729 return m_invokingWindow
;
730 else if ( GetMenuBar() != NULL
)
731 return GetMenuBar()->GetFrame();
736 // ---------------------------------------------------------------------------
738 // ---------------------------------------------------------------------------
740 void wxMenuBar::Init()
742 m_eventHandler
= this;
744 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
747 // Not using a combined wxToolBar/wxMenuBar? then use
748 // a commandbar in WinCE .NET just to implement the
750 #if defined(WINCE_WITH_COMMANDBAR)
752 m_adornmentsAdded
= false;
756 wxMenuBar::wxMenuBar()
761 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
766 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
770 m_titles
.Alloc(count
);
772 for ( size_t i
= 0; i
< count
; i
++ )
774 m_menus
.Append(menus
[i
]);
775 m_titles
.Add(titles
[i
]);
777 menus
[i
]->Attach(this);
781 wxMenuBar::~wxMenuBar()
783 // In Windows CE (not .NET), the menubar is always associated
784 // with a toolbar, which destroys the menu implicitly.
785 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
788 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
790 toolMenuBar
->SetMenuBar(NULL
);
793 // we should free Windows resources only if Windows doesn't do it for us
794 // which happens if we're attached to a frame
795 if (m_hMenu
&& !IsAttached())
797 #if defined(WINCE_WITH_COMMANDBAR)
798 ::DestroyWindow((HWND
) m_commandBar
);
799 m_commandBar
= (WXHWND
) NULL
;
801 ::DestroyMenu((HMENU
)m_hMenu
);
803 m_hMenu
= (WXHMENU
)NULL
;
808 // ---------------------------------------------------------------------------
810 // ---------------------------------------------------------------------------
812 void wxMenuBar::Refresh()
814 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
816 #if defined(WINCE_WITHOUT_COMMANDBAR)
819 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
821 #elif defined(WINCE_WITH_COMMANDBAR)
823 DrawMenuBar((HWND
) m_commandBar
);
825 DrawMenuBar(GetHwndOf(GetFrame()));
829 WXHMENU
wxMenuBar::Create()
831 // Note: this totally doesn't work on Smartphone,
832 // since you have to use resources.
833 // We'll have to find another way to add a menu
834 // by changing/adding menu items to an existing menu.
835 #if defined(WINCE_WITHOUT_COMMANDBAR)
842 HWND hCommandBar
= (HWND
) GetToolBar()->GetHWND();
843 HMENU hMenu
= (HMENU
)::SendMessage(hCommandBar
, SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
847 memset(&tbButton
, 0, sizeof(TBBUTTON
));
848 tbButton
.iBitmap
= I_IMAGENONE
;
849 tbButton
.fsState
= TBSTATE_ENABLED
;
850 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
853 for (i
= 0; i
< GetMenuCount(); i
++)
855 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu() ;
856 tbButton
.dwData
= (DWORD
)hPopupMenu
;
857 wxString label
= wxStripMenuCodes(GetLabelTop(i
));
858 tbButton
.iString
= (int) label
.c_str();
862 tbButton
.idCommand
= NewControlId();
863 if (!::SendMessage(hCommandBar
, TB_INSERTBUTTON
, position
, (LPARAM
)&tbButton
))
865 wxLogLastError(wxT("TB_INSERTBUTTON"));
869 m_hMenu
= (WXHMENU
) hMenu
;
875 m_hMenu
= (WXHMENU
)::CreateMenu();
879 wxLogLastError(wxT("CreateMenu"));
883 size_t count
= GetMenuCount(), i
;
884 wxMenuList::iterator it
;
885 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
887 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
888 (UINT
)(*it
)->GetHMenu(),
891 wxLogLastError(wxT("AppendMenu"));
900 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
903 wxASSERT(menu
->GetHMenu());
906 #if defined(__WXWINCE__)
907 int totalMSWItems
= GetMenuCount();
909 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
912 int i
; // For old C++ compatibility
913 for(i
=wxpos
; i
<totalMSWItems
; i
++)
915 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
918 for(i
=0; i
<wxpos
; i
++)
920 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
927 // ---------------------------------------------------------------------------
928 // wxMenuBar functions to work with the top level submenus
929 // ---------------------------------------------------------------------------
931 // NB: we don't support owner drawn top level items for now, if we do these
932 // functions would have to be changed to use wxMenuItem as well
934 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
936 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
937 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
939 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
941 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
946 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
948 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
950 m_titles
[pos
] = label
;
956 //else: have to modify the existing menu
958 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
961 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
962 if ( flagsOld
== 0xFFFFFFFF )
964 wxLogLastError(wxT("GetMenuState"));
969 if ( flagsOld
& MF_POPUP
)
971 // HIBYTE contains the number of items in the submenu in this case
973 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
983 info
.cbSize
= sizeof(info
);
984 info
.fMask
= MIIM_TYPE
;
985 info
.fType
= MFT_STRING
;
986 info
.cch
= label
.Length();
987 info
.dwTypeData
= (LPTSTR
) label
.c_str();
988 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, & info
) )
990 wxLogLastError(wxT("SetMenuItemInfo"));
994 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
995 id
, label
) == (int)0xFFFFFFFF )
997 wxLogLastError(wxT("ModifyMenu"));
1004 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1006 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1007 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
1009 return wxMenuItem::GetLabelFromText(m_titles
[pos
]);
1012 // ---------------------------------------------------------------------------
1013 // wxMenuBar construction
1014 // ---------------------------------------------------------------------------
1016 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1018 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1022 m_titles
[pos
] = title
;
1026 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1028 // can't use ModifyMenu() because it deletes the submenu it replaces
1029 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1031 wxLogLastError(wxT("RemoveMenu"));
1034 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1035 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1036 (UINT
)GetHmenuOf(menu
), title
) )
1038 wxLogLastError(wxT("InsertMenu"));
1042 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1044 // need to rebuild accell table
1045 RebuildAccelTable();
1047 #endif // wxUSE_ACCEL
1055 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1057 // Find out which MSW item before which we'll be inserting before
1058 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1059 // If IsAttached() is false this won't be used anyway
1060 int mswpos
= (!IsAttached() || (pos
== m_menus
.GetCount()))
1061 ? -1 // append the menu
1062 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1064 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1067 m_titles
.Insert(title
, pos
);
1071 #if defined(WINCE_WITHOUT_COMMANDAR)
1075 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1076 tbButton
.iBitmap
= I_IMAGENONE
;
1077 tbButton
.fsState
= TBSTATE_ENABLED
;
1078 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1080 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1081 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1082 wxString label
= wxStripMenuCodes(title
);
1083 tbButton
.iString
= (int) label
.c_str();
1085 tbButton
.idCommand
= NewControlId();
1086 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1088 wxLogLastError(wxT("TB_INSERTBUTTON"));
1092 if ( !::InsertMenu(GetHmenu(), mswpos
,
1093 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1094 (UINT
)GetHmenuOf(menu
), title
) )
1096 wxLogLastError(wxT("InsertMenu"));
1100 if ( menu
->HasAccels() )
1102 // need to rebuild accell table
1103 RebuildAccelTable();
1105 #endif // wxUSE_ACCEL
1113 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1115 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1116 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1118 if ( !wxMenuBarBase::Append(menu
, title
) )
1121 m_titles
.Add(title
);
1125 #if defined(WINCE_WITHOUT_COMMANDAR)
1129 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1130 tbButton
.iBitmap
= I_IMAGENONE
;
1131 tbButton
.fsState
= TBSTATE_ENABLED
;
1132 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1134 size_t pos
= GetMenuCount();
1135 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1136 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1137 wxString label
= wxStripMenuCodes(title
);
1138 tbButton
.iString
= (int) label
.c_str();
1140 tbButton
.idCommand
= NewControlId();
1141 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1143 wxLogLastError(wxT("TB_INSERTBUTTON"));
1147 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1148 (UINT
)submenu
, title
) )
1150 wxLogLastError(wxT("AppendMenu"));
1155 if ( menu
->HasAccels() )
1157 // need to rebuild accelerator table
1158 RebuildAccelTable();
1160 #endif // wxUSE_ACCEL
1168 wxMenu
*wxMenuBar::Remove(size_t pos
)
1170 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1176 #if defined(WINCE_WITHOUT_COMMANDAR)
1179 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1181 wxLogLastError(wxT("TB_DELETEBUTTON"));
1185 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1187 wxLogLastError(wxT("RemoveMenu"));
1192 if ( menu
->HasAccels() )
1194 // need to rebuild accell table
1195 RebuildAccelTable();
1197 #endif // wxUSE_ACCEL
1203 m_titles
.RemoveAt(pos
);
1210 void wxMenuBar::RebuildAccelTable()
1212 // merge the accelerators of all menus into one accel table
1213 size_t nAccelCount
= 0;
1214 size_t i
, count
= GetMenuCount();
1215 wxMenuList::iterator it
;
1216 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1218 nAccelCount
+= (*it
)->GetAccelCount();
1223 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1226 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1228 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1231 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1233 delete [] accelEntries
;
1237 #endif // wxUSE_ACCEL
1239 void wxMenuBar::Attach(wxFrame
*frame
)
1241 wxMenuBarBase::Attach(frame
);
1243 #if defined(WINCE_WITH_COMMANDBAR)
1247 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1252 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1254 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1261 RebuildAccelTable();
1262 #endif // wxUSE_ACCEL
1265 #if defined(WINCE_WITH_COMMANDBAR)
1266 bool wxMenuBar::AddAdornments(long style
)
1268 if (m_adornmentsAdded
|| !m_commandBar
)
1271 if (style
& wxCLOSE_BOX
)
1273 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1274 wxLogLastError(wxT("CommandBar_AddAdornments"));
1282 void wxMenuBar::Detach()
1284 wxMenuBarBase::Detach();
1287 #endif // wxUSE_MENUS