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"
58 #include "wx/msw/wince/missing.h"
62 // other standard headers
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 extern wxMenu
*wxCurrentPopupMenu
;
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // the (popup) menu title has this special id
76 static const int idMenuTitle
= -3;
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 // make the given menu item default
83 static void SetDefaultMenuItem(HMENU hmenu
, UINT id
)
88 mii
.cbSize
= sizeof(MENUITEMINFO
);
89 mii
.fMask
= MIIM_STATE
;
90 mii
.fState
= MFS_DEFAULT
;
92 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
94 wxLogLastError(wxT("SetMenuItemInfo"));
100 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
104 info
.cbSize
= sizeof(info
);
105 info
.fMask
= MIIM_STATE
;
106 if ( !GetMenuItemInfo(hMenu
, id
, flags
& MF_BYCOMMAND
? FALSE
: TRUE
, & info
) )
107 wxLogLastError(wxT("GetMenuItemInfo"));
112 // ============================================================================
114 // ============================================================================
116 #include <wx/listimpl.cpp>
118 WX_DEFINE_LIST( wxMenuInfoList
) ;
120 #if wxUSE_EXTENDED_RTTI
122 WX_DEFINE_FLAGS( wxMenuStyle
)
124 wxBEGIN_FLAGS( wxMenuStyle
)
125 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
126 wxEND_FLAGS( wxMenuStyle
)
128 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
130 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
132 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
134 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
137 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
138 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
139 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
140 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
141 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
142 wxEND_PROPERTIES_TABLE()
144 wxBEGIN_HANDLERS_TABLE(wxMenu
)
145 wxEND_HANDLERS_TABLE()
147 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
149 WX_DEFINE_FLAGS( wxMenuBarStyle
)
151 wxBEGIN_FLAGS( wxMenuBarStyle
)
152 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
153 wxEND_FLAGS( wxMenuBarStyle
)
155 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
156 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
161 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
163 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
165 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
166 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
167 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
168 wxEND_PROPERTIES_TABLE()
170 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
171 wxEND_HANDLERS_TABLE()
173 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
175 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
177 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
179 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
182 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
183 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
184 wxEND_PROPERTIES_TABLE()
186 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
187 wxEND_HANDLERS_TABLE()
189 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
192 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
193 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
194 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
197 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
199 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
200 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
201 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
203 wxMenuInfo
* info
= new wxMenuInfo() ;
204 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetLabelTop(i
) ) ;
205 list
->Append( info
) ;
210 // ---------------------------------------------------------------------------
211 // wxMenu construction, adding and removing menu items
212 // ---------------------------------------------------------------------------
214 // Construct a menu with optional title (then use append)
218 m_startRadioGroup
= -1;
221 m_hMenu
= (WXHMENU
)CreatePopupMenu();
224 wxLogLastError(wxT("CreatePopupMenu"));
227 // if we have a title, insert it in the beginning of the menu
230 Append(idMenuTitle
, m_title
);
235 // The wxWindow destructor will take care of deleting the submenus.
238 // we should free Windows resources only if Windows doesn't do it for us
239 // which happens if we're attached to a menubar or a submenu of another
241 if ( !IsAttached() && !GetParent() )
243 if ( !::DestroyMenu(GetHmenu()) )
245 wxLogLastError(wxT("DestroyMenu"));
251 WX_CLEAR_ARRAY(m_accels
);
252 #endif // wxUSE_ACCEL
257 // this will take effect during the next call to Append()
261 void wxMenu::Attach(wxMenuBarBase
*menubar
)
263 wxMenuBase::Attach(menubar
);
270 int wxMenu::FindAccel(int id
) const
272 size_t n
, count
= m_accels
.GetCount();
273 for ( n
= 0; n
< count
; n
++ )
275 if ( m_accels
[n
]->m_command
== id
)
282 void wxMenu::UpdateAccel(wxMenuItem
*item
)
284 if ( item
->IsSubMenu() )
286 wxMenu
*submenu
= item
->GetSubMenu();
287 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
290 UpdateAccel(node
->GetData());
292 node
= node
->GetNext();
295 else if ( !item
->IsSeparator() )
297 // find the (new) accel for this item
298 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
300 accel
->m_command
= item
->GetId();
303 int n
= FindAccel(item
->GetId());
304 if ( n
== wxNOT_FOUND
)
306 // no old, add new if any
310 return; // skipping RebuildAccelTable() below
314 // replace old with new or just remove the old one if no new
319 m_accels
.RemoveAt(n
);
324 GetMenuBar()->RebuildAccelTable();
327 //else: it is a separator, they can't have accels, nothing to do
330 #endif // wxUSE_ACCEL
332 // append a new item or submenu to the menu
333 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
337 #endif // wxUSE_ACCEL
341 // if "Break" has just been called, insert a menu break before this item
342 // (and don't forget to reset the flag)
344 flags
|= MF_MENUBREAK
;
348 if ( pItem
->IsSeparator() ) {
349 flags
|= MF_SEPARATOR
;
352 // id is the numeric id for normal menu items and HMENU for submenus as
353 // required by ::AppendMenu() API
355 wxMenu
*submenu
= pItem
->GetSubMenu();
356 if ( submenu
!= NULL
) {
357 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
359 submenu
->SetParent(this);
361 id
= (UINT
)submenu
->GetHMenu();
370 wxString strippedString
;
375 #if wxUSE_OWNER_DRAWN
376 if ( pItem
->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
377 // item draws itself, pass pointer to it in data parameter
378 flags
|= MF_OWNERDRAW
;
379 pData
= (LPCTSTR
)pItem
;
384 // menu is just a normal string (passed in data parameter)
388 strippedString
= wxStripMenuCodes(pItem
->GetText());
389 pData
= (wxChar
*)strippedString
.c_str();
391 pData
= (wxChar
*)pItem
->GetText().c_str();
396 if ( pos
== (size_t)-1 )
398 ok
= ::AppendMenu(GetHmenu(), flags
, id
, pData
);
402 ok
= ::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
);
407 wxLogLastError(wxT("Insert or AppendMenu"));
412 // if we just appended the title, highlight it
414 if ( (int)id
== idMenuTitle
)
416 // visually select the menu title
417 SetDefaultMenuItem(GetHmenu(), id
);
421 // if we're already attached to the menubar, we must update it
422 if ( IsAttached() && GetMenuBar()->IsAttached() )
424 GetMenuBar()->Refresh();
430 void wxMenu::EndRadioGroup()
432 // we're not inside a radio group any longer
433 m_startRadioGroup
= -1;
436 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
438 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
442 if ( item
->GetKind() == wxITEM_RADIO
)
444 int count
= GetMenuItemCount();
446 if ( m_startRadioGroup
== -1 )
448 // start a new radio group
449 m_startRadioGroup
= count
;
451 // for now it has just one element
452 item
->SetAsRadioGroupStart();
453 item
->SetRadioGroupEnd(m_startRadioGroup
);
455 // ensure that we have a checked item in the radio group
458 else // extend the current radio group
460 // we need to update its end item
461 item
->SetRadioGroupStart(m_startRadioGroup
);
462 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
466 node
->GetData()->SetRadioGroupEnd(count
);
470 wxFAIL_MSG( _T("where is the radio group start item?") );
474 else // not a radio item
479 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
486 // check the item initially
493 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
495 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
501 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
503 // we need to find the items position in the child list
505 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
506 for ( pos
= 0; node
; pos
++ )
508 if ( node
->GetData() == item
)
511 node
= node
->GetNext();
514 // DoRemove() (unlike Remove) can only be called for existing item!
515 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
518 // remove the corresponding accel from the accel table
519 int n
= FindAccel(item
->GetId());
520 if ( n
!= wxNOT_FOUND
)
524 m_accels
.RemoveAt(n
);
526 //else: this item doesn't have an accel, nothing to do
527 #endif // wxUSE_ACCEL
529 // remove the item from the menu
530 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
532 wxLogLastError(wxT("RemoveMenu"));
535 if ( IsAttached() && GetMenuBar()->IsAttached() )
537 // otherwise, the chane won't be visible
538 GetMenuBar()->Refresh();
541 // and from internal data structures
542 return wxMenuBase::DoRemove(item
);
545 // ---------------------------------------------------------------------------
546 // accelerator helpers
547 // ---------------------------------------------------------------------------
551 // create the wxAcceleratorEntries for our accels and put them into provided
552 // array - return the number of accels we have
553 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
555 size_t count
= GetAccelCount();
556 for ( size_t n
= 0; n
< count
; n
++ )
558 *accels
++ = *m_accels
[n
];
564 #endif // wxUSE_ACCEL
566 // ---------------------------------------------------------------------------
568 // ---------------------------------------------------------------------------
570 void wxMenu::SetTitle(const wxString
& label
)
572 bool hasNoTitle
= m_title
.IsEmpty();
575 HMENU hMenu
= GetHmenu();
579 if ( !label
.IsEmpty() )
581 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
582 (unsigned)idMenuTitle
, m_title
) ||
583 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
585 wxLogLastError(wxT("InsertMenu"));
591 if ( label
.IsEmpty() )
593 // remove the title and the separator after it
594 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
595 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
597 wxLogLastError(wxT("RemoveMenu"));
606 info
.cbSize
= sizeof(info
);
607 info
.fMask
= MIIM_TYPE
;
608 info
.fType
= MFT_STRING
;
609 info
.cch
= m_title
.Length();
610 info
.dwTypeData
= (LPTSTR
) m_title
.c_str();
611 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
613 wxLogLastError(wxT("SetMenuItemInfo"));
616 if ( !ModifyMenu(hMenu
, 0u,
617 MF_BYPOSITION
| MF_STRING
,
618 (unsigned)idMenuTitle
, m_title
) )
620 wxLogLastError(wxT("ModifyMenu"));
627 // put the title string in bold face
628 if ( !m_title
.IsEmpty() )
630 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
635 // ---------------------------------------------------------------------------
637 // ---------------------------------------------------------------------------
639 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
641 // ignore commands from the menu title
643 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
644 if ( id
!= (WXWORD
)idMenuTitle
)
646 // VZ: previosuly, the command int was set to id too which was quite
647 // useless anyhow (as it could be retrieved using GetId()) and
648 // uncompatible with wxGTK, so now we use the command int instead
649 // to pass the checked status
650 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
) ;
651 SendEvent(id
, menuState
& MF_CHECKED
);
657 // ---------------------------------------------------------------------------
659 // ---------------------------------------------------------------------------
661 wxWindow
*wxMenu::GetWindow() const
663 if ( m_invokingWindow
!= NULL
)
664 return m_invokingWindow
;
665 else if ( GetMenuBar() != NULL
)
666 return GetMenuBar()->GetFrame();
671 // ---------------------------------------------------------------------------
673 // ---------------------------------------------------------------------------
675 void wxMenuBar::Init()
677 m_eventHandler
= this;
679 #if wxUSE_TOOLBAR && defined(__WXWINCE__) && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__))
682 // Not using a combined wxToolBar/wxMenuBar? then use
683 // a commandbar in WinCE .NET just to implement the
685 #if defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__))
687 m_adornmentsAdded
= false;
691 wxMenuBar::wxMenuBar()
696 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
701 wxMenuBar::wxMenuBar(int count
, wxMenu
*menus
[], const wxString titles
[])
705 m_titles
.Alloc(count
);
707 for ( int i
= 0; i
< count
; i
++ )
709 m_menus
.Append(menus
[i
]);
710 m_titles
.Add(titles
[i
]);
712 menus
[i
]->Attach(this);
716 wxMenuBar::~wxMenuBar()
718 // In Windows CE (not .NET), the menubar is always associated
719 // with a toolbar, which destroys the menu implicitly.
720 #if defined(__WXWINCE__) && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__))
722 GetToolBar()->SetMenuBar(NULL
);
724 // we should free Windows resources only if Windows doesn't do it for us
725 // which happens if we're attached to a frame
726 if (m_hMenu
&& !IsAttached())
728 #if defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__))
729 ::DestroyWindow((HWND
) m_commandBar
);
730 m_commandBar
= (WXHWND
) NULL
;
732 ::DestroyMenu((HMENU
)m_hMenu
);
734 m_hMenu
= (WXHMENU
)NULL
;
739 // ---------------------------------------------------------------------------
741 // ---------------------------------------------------------------------------
743 void wxMenuBar::Refresh()
745 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
747 #if defined(__WXWINCE__) && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__))
750 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
752 #elif defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__))
754 DrawMenuBar((HWND
) m_commandBar
);
756 DrawMenuBar(GetHwndOf(GetFrame()));
760 WXHMENU
wxMenuBar::Create()
762 // Note: this totally doesn't work on Smartphone,
763 // since you have to use resources.
764 // We'll have to find another way to add a menu
765 // by changing/adding menu items to an existing menu.
766 #if defined(__WXWINCE__) && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__))
773 HWND hCommandBar
= (HWND
) GetToolBar()->GetHWND();
774 HMENU hMenu
= (HMENU
)::SendMessage(hCommandBar
, SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
778 memset(&tbButton
, 0, sizeof(TBBUTTON
));
779 tbButton
.iBitmap
= I_IMAGENONE
;
780 tbButton
.fsState
= TBSTATE_ENABLED
;
781 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
784 for (i
= 0; i
< GetMenuCount(); i
++)
786 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu() ;
787 tbButton
.dwData
= (DWORD
)hPopupMenu
;
788 wxString label
= wxStripMenuCodes(GetLabelTop(i
));
789 tbButton
.iString
= (int) label
.c_str();
793 tbButton
.idCommand
= NewControlId();
794 if (!::SendMessage(hCommandBar
, TB_INSERTBUTTON
, position
, (LPARAM
)&tbButton
))
796 wxLogLastError(wxT("TB_INSERTBUTTON"));
800 m_hMenu
= (WXHMENU
) hMenu
;
806 m_hMenu
= (WXHMENU
)::CreateMenu();
810 wxLogLastError(wxT("CreateMenu"));
814 size_t count
= GetMenuCount(), i
;
815 wxMenuList::iterator it
;
816 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
818 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
819 (UINT
)(*it
)->GetHMenu(),
822 wxLogLastError(wxT("AppendMenu"));
831 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
834 wxASSERT(menu
->GetHMenu());
837 #if defined(__WXWINCE__)
838 int totalMSWItems
= GetMenuCount();
840 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
843 int i
; // For old C++ compatibility
844 for(i
=wxpos
; i
<totalMSWItems
; i
++)
846 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
849 for(i
=0; i
<wxpos
; i
++)
851 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
858 // ---------------------------------------------------------------------------
859 // wxMenuBar functions to work with the top level submenus
860 // ---------------------------------------------------------------------------
862 // NB: we don't support owner drawn top level items for now, if we do these
863 // functions would have to be changed to use wxMenuItem as well
865 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
867 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
868 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
870 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
872 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
877 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
879 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
881 m_titles
[pos
] = label
;
887 //else: have to modify the existing menu
889 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
892 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
893 if ( flagsOld
== 0xFFFFFFFF )
895 wxLogLastError(wxT("GetMenuState"));
900 if ( flagsOld
& MF_POPUP
)
902 // HIBYTE contains the number of items in the submenu in this case
904 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
914 info
.cbSize
= sizeof(info
);
915 info
.fMask
= MIIM_TYPE
;
916 info
.fType
= MFT_STRING
;
917 info
.cch
= label
.Length();
918 info
.dwTypeData
= (LPTSTR
) label
.c_str();
919 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, & info
) )
921 wxLogLastError(wxT("SetMenuItemInfo"));
925 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
926 id
, label
) == (int)0xFFFFFFFF )
928 wxLogLastError(wxT("ModifyMenu"));
935 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
937 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
938 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
940 return wxMenuItem::GetLabelFromText(m_titles
[pos
]);
943 // ---------------------------------------------------------------------------
944 // wxMenuBar construction
945 // ---------------------------------------------------------------------------
947 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
949 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
953 m_titles
[pos
] = title
;
957 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
959 // can't use ModifyMenu() because it deletes the submenu it replaces
960 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
962 wxLogLastError(wxT("RemoveMenu"));
965 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
966 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
967 (UINT
)GetHmenuOf(menu
), title
) )
969 wxLogLastError(wxT("InsertMenu"));
973 if ( menuOld
->HasAccels() || menu
->HasAccels() )
975 // need to rebuild accell table
978 #endif // wxUSE_ACCEL
986 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
988 // Find out which MSW item before which we'll be inserting before
989 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
990 // If IsAttached() is false this won't be used anyway
991 int mswpos
= (!IsAttached() || (pos
== m_menus
.GetCount()))
992 ? -1 // append the menu
993 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
995 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
998 m_titles
.Insert(title
, pos
);
1002 #if defined(__WXWINCE__) && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__))
1006 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1007 tbButton
.iBitmap
= I_IMAGENONE
;
1008 tbButton
.fsState
= TBSTATE_ENABLED
;
1009 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1011 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1012 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1013 wxString label
= wxStripMenuCodes(title
);
1014 tbButton
.iString
= (int) label
.c_str();
1016 tbButton
.idCommand
= NewControlId();
1017 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1019 wxLogLastError(wxT("TB_INSERTBUTTON"));
1023 if ( !::InsertMenu(GetHmenu(), mswpos
,
1024 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1025 (UINT
)GetHmenuOf(menu
), title
) )
1027 wxLogLastError(wxT("InsertMenu"));
1031 if ( menu
->HasAccels() )
1033 // need to rebuild accell table
1034 RebuildAccelTable();
1036 #endif // wxUSE_ACCEL
1044 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1046 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1047 wxCHECK_MSG( submenu
, FALSE
, wxT("can't append invalid menu to menubar") );
1049 if ( !wxMenuBarBase::Append(menu
, title
) )
1052 m_titles
.Add(title
);
1056 #if defined(__WXWINCE__) && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__))
1060 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1061 tbButton
.iBitmap
= I_IMAGENONE
;
1062 tbButton
.fsState
= TBSTATE_ENABLED
;
1063 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1065 size_t pos
= GetMenuCount();
1066 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1067 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1068 wxString label
= wxStripMenuCodes(title
);
1069 tbButton
.iString
= (int) label
.c_str();
1071 tbButton
.idCommand
= NewControlId();
1072 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1074 wxLogLastError(wxT("TB_INSERTBUTTON"));
1078 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1079 (UINT
)submenu
, title
) )
1081 wxLogLastError(wxT("AppendMenu"));
1086 if ( menu
->HasAccels() )
1088 // need to rebuild accelerator table
1089 RebuildAccelTable();
1091 #endif // wxUSE_ACCEL
1099 wxMenu
*wxMenuBar::Remove(size_t pos
)
1101 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1107 #if defined(__WXWINCE__) && (_WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__))
1110 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1112 wxLogLastError(wxT("TB_DELETEBUTTON"));
1116 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1118 wxLogLastError(wxT("RemoveMenu"));
1123 if ( menu
->HasAccels() )
1125 // need to rebuild accell table
1126 RebuildAccelTable();
1128 #endif // wxUSE_ACCEL
1134 m_titles
.RemoveAt(pos
);
1141 void wxMenuBar::RebuildAccelTable()
1143 // merge the accelerators of all menus into one accel table
1144 size_t nAccelCount
= 0;
1145 size_t i
, count
= GetMenuCount();
1146 wxMenuList::iterator it
;
1147 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1149 nAccelCount
+= (*it
)->GetAccelCount();
1154 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1157 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1159 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1162 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1164 delete [] accelEntries
;
1168 #endif // wxUSE_ACCEL
1170 void wxMenuBar::Attach(wxFrame
*frame
)
1172 wxMenuBarBase::Attach(frame
);
1174 #if defined(__WXWINCE__)
1177 #if _WIN32_WCE < 400 || defined(__POCKETPC__) || defined(__SMARTPHONE__)
1179 // No idea why this was here, but it seems to be obsolete.
1180 // Remove after testing with other WinCE combinations - April 2004
1184 HWND hCommandBar
= (HWND
) GetToolBar()->GetHWND();
1185 if (!CommandBar_InsertMenubarEx(hCommandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1187 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1193 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1198 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1200 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1207 // __WXWINCE__ && _WIN32_WCE >= 400
1210 RebuildAccelTable();
1211 #endif // wxUSE_ACCEL
1214 #if defined(__WXWINCE__) && (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__))
1215 bool wxMenuBar::AddAdornments(long style
)
1217 if (m_adornmentsAdded
|| !m_commandBar
)
1220 if (style
& wxCLOSE_BOX
)
1222 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1223 wxLogLastError(wxT("CommandBar_AddAdornments"));
1231 void wxMenuBar::Detach()
1233 wxMenuBarBase::Detach();
1236 #endif // wxUSE_MENUS