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 // find the (new) accel for this item
309 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
311 accel
->m_command
= item
->GetId();
314 int n
= FindAccel(item
->GetId());
315 if ( n
== wxNOT_FOUND
)
317 // no old, add new if any
321 return; // skipping RebuildAccelTable() below
325 // replace old with new or just remove the old one if no new
330 m_accels
.RemoveAt(n
);
335 GetMenuBar()->RebuildAccelTable();
338 //else: it is a separator, they can't have accels, nothing to do
341 #endif // wxUSE_ACCEL
343 // append a new item or submenu to the menu
344 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
348 #endif // wxUSE_ACCEL
352 // if "Break" has just been called, insert a menu break before this item
353 // (and don't forget to reset the flag)
355 flags
|= MF_MENUBREAK
;
359 if ( pItem
->IsSeparator() ) {
360 flags
|= MF_SEPARATOR
;
363 // id is the numeric id for normal menu items and HMENU for submenus as
364 // required by ::AppendMenu() API
366 wxMenu
*submenu
= pItem
->GetSubMenu();
367 if ( submenu
!= NULL
) {
368 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
370 submenu
->SetParent(this);
372 id
= (UINT
)submenu
->GetHMenu();
381 // prepare to insert the item in the menu
382 wxString itemText
= pItem
->GetText();
383 LPCTSTR pData
= NULL
;
384 if ( pos
== (size_t)-1 )
386 // append at the end (note that the item is already appended to
387 // internal data structures)
388 pos
= GetMenuItemCount() - 1;
391 // adjust position to account for the title, if any
392 if ( !m_title
.empty() )
393 pos
+= 2; // for the title itself and its separator
397 // check if we have something more than a simple text item
398 #if wxUSE_OWNER_DRAWN
399 if ( pItem
->IsOwnerDrawn() )
401 // is the item owner-drawn just because of the bitmap?
402 if ( pItem
->GetBitmap().Ok() &&
403 !pItem
->GetTextColour().Ok() &&
404 !pItem
->GetBackgroundColour().Ok() &&
405 !pItem
->GetFont().Ok() )
407 // try to use InsertMenuItem() as it's guaranteed to look correctly
408 // while our owner-drawning code is not
410 // first compile-time check
412 WinStruct
<MENUITEMINFO
> mii
;
414 // now run-time one: MIIM_BITMAP only works under WinME/2000+
415 if ( wxGetWinVersion() >= wxWinVersion_98
)
417 mii
.fMask
= MIIM_ID
| MIIM_STRING
| MIIM_DATA
| MIIM_BITMAP
;
419 mii
.cch
= itemText
.length();
420 mii
.dwTypeData
= wx_const_cast(wxChar
*, itemText
.c_str());
422 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
423 // 1. we can't draw it with transparency then (this is not
424 // very important now but would be with themed menu bg)
425 // 2. worse, Windows inverses the bitmap for the selected
426 // item and this looks downright ugly
428 // so instead draw it ourselves in MSWOnDrawItem()
429 mii
.dwItemData
= wx_reinterpret_cast(ULONG_PTR
, pItem
);
430 mii
.hbmpItem
= HBMMENU_CALLBACK
;
432 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
435 wxLogLastError(wxT("InsertMenuItem()"));
437 else // InsertMenuItem() ok
439 // we need to remove the extra indent which is reserved for
440 // the checkboxes by default as it looks ugly unless check
441 // boxes are used together with bitmaps and this is not the
443 WinStruct
<MENUINFO
> mi
;
445 // don't call SetMenuInfo() directly, this would prevent
446 // the app from starting up under Windows 95/NT 4
447 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
449 wxDynamicLibrary
dllUser(_T("user32"));
450 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
451 if ( pfnSetMenuInfo
)
453 mi
.fMask
= MIM_STYLE
;
454 mi
.dwStyle
= MNS_CHECKORBMP
;
455 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
456 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
459 // tell the item that it's not really owner-drawn but only
460 // needs to draw its bitmap, the rest is done by Windows
461 pItem
->ResetOwnerDrawn();
464 #endif // MIIM_BITMAP
469 // item draws itself, pass pointer to it in data parameter
470 flags
|= MF_OWNERDRAW
;
471 pData
= (LPCTSTR
)pItem
;
475 #endif // wxUSE_OWNER_DRAWN
477 // menu is just a normal string (passed in data parameter)
481 itemText
= wxMenuItem::GetLabelFromText(itemText
);
484 pData
= (wxChar
*)itemText
.c_str();
487 // item might have been already inserted by InsertMenuItem() above
490 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
492 wxLogLastError(wxT("InsertMenu[Item]()"));
499 // if we just appended the title, highlight it
500 if ( (int)id
== idMenuTitle
)
502 // visually select the menu title
503 SetDefaultMenuItem(GetHmenu(), id
);
506 // if we're already attached to the menubar, we must update it
507 if ( IsAttached() && GetMenuBar()->IsAttached() )
509 GetMenuBar()->Refresh();
515 void wxMenu::EndRadioGroup()
517 // we're not inside a radio group any longer
518 m_startRadioGroup
= -1;
521 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
523 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
527 if ( item
->GetKind() == wxITEM_RADIO
)
529 int count
= GetMenuItemCount();
531 if ( m_startRadioGroup
== -1 )
533 // start a new radio group
534 m_startRadioGroup
= count
;
536 // for now it has just one element
537 item
->SetAsRadioGroupStart();
538 item
->SetRadioGroupEnd(m_startRadioGroup
);
540 // ensure that we have a checked item in the radio group
543 else // extend the current radio group
545 // we need to update its end item
546 item
->SetRadioGroupStart(m_startRadioGroup
);
547 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
551 node
->GetData()->SetRadioGroupEnd(count
);
555 wxFAIL_MSG( _T("where is the radio group start item?") );
559 else // not a radio item
564 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
571 // check the item initially
578 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
580 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
586 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
588 // we need to find the items position in the child list
590 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
591 for ( pos
= 0; node
; pos
++ )
593 if ( node
->GetData() == item
)
596 node
= node
->GetNext();
599 // DoRemove() (unlike Remove) can only be called for existing item!
600 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
603 // remove the corresponding accel from the accel table
604 int n
= FindAccel(item
->GetId());
605 if ( n
!= wxNOT_FOUND
)
609 m_accels
.RemoveAt(n
);
611 //else: this item doesn't have an accel, nothing to do
612 #endif // wxUSE_ACCEL
614 // remove the item from the menu
615 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
617 wxLogLastError(wxT("RemoveMenu"));
620 if ( IsAttached() && GetMenuBar()->IsAttached() )
622 // otherwise, the chane won't be visible
623 GetMenuBar()->Refresh();
626 // and from internal data structures
627 return wxMenuBase::DoRemove(item
);
630 // ---------------------------------------------------------------------------
631 // accelerator helpers
632 // ---------------------------------------------------------------------------
636 // create the wxAcceleratorEntries for our accels and put them into provided
637 // array - return the number of accels we have
638 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
640 size_t count
= GetAccelCount();
641 for ( size_t n
= 0; n
< count
; n
++ )
643 *accels
++ = *m_accels
[n
];
649 #endif // wxUSE_ACCEL
651 // ---------------------------------------------------------------------------
653 // ---------------------------------------------------------------------------
655 void wxMenu::SetTitle(const wxString
& label
)
657 bool hasNoTitle
= m_title
.empty();
660 HMENU hMenu
= GetHmenu();
664 if ( !label
.empty() )
666 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
667 (unsigned)idMenuTitle
, m_title
) ||
668 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
670 wxLogLastError(wxT("InsertMenu"));
678 // remove the title and the separator after it
679 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
680 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
682 wxLogLastError(wxT("RemoveMenu"));
691 info
.cbSize
= sizeof(info
);
692 info
.fMask
= MIIM_TYPE
;
693 info
.fType
= MFT_STRING
;
694 info
.cch
= m_title
.Length();
695 info
.dwTypeData
= (LPTSTR
) m_title
.c_str();
696 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
698 wxLogLastError(wxT("SetMenuItemInfo"));
701 if ( !ModifyMenu(hMenu
, 0u,
702 MF_BYPOSITION
| MF_STRING
,
703 (unsigned)idMenuTitle
, m_title
) )
705 wxLogLastError(wxT("ModifyMenu"));
712 // put the title string in bold face
713 if ( !m_title
.empty() )
715 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
720 // ---------------------------------------------------------------------------
722 // ---------------------------------------------------------------------------
724 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
726 // ignore commands from the menu title
727 if ( id
!= (WXWORD
)idMenuTitle
)
729 // get the checked status of the command: notice that menuState is the
730 // old state of the menu, so the test for MF_CHECKED must be inverted
731 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
732 SendEvent(id
, !(menuState
& MF_CHECKED
));
738 // ---------------------------------------------------------------------------
740 // ---------------------------------------------------------------------------
742 wxWindow
*wxMenu::GetWindow() const
744 if ( m_invokingWindow
!= NULL
)
745 return m_invokingWindow
;
746 else if ( GetMenuBar() != NULL
)
747 return GetMenuBar()->GetFrame();
752 // ---------------------------------------------------------------------------
754 // ---------------------------------------------------------------------------
756 void wxMenuBar::Init()
758 m_eventHandler
= this;
760 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
763 // Not using a combined wxToolBar/wxMenuBar? then use
764 // a commandbar in WinCE .NET just to implement the
766 #if defined(WINCE_WITH_COMMANDBAR)
768 m_adornmentsAdded
= false;
772 wxMenuBar::wxMenuBar()
777 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
782 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
786 m_titles
.Alloc(count
);
788 for ( size_t i
= 0; i
< count
; i
++ )
790 m_menus
.Append(menus
[i
]);
791 m_titles
.Add(titles
[i
]);
793 menus
[i
]->Attach(this);
797 wxMenuBar::~wxMenuBar()
799 // In Windows CE (not .NET), the menubar is always associated
800 // with a toolbar, which destroys the menu implicitly.
801 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
804 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
806 toolMenuBar
->SetMenuBar(NULL
);
809 // we should free Windows resources only if Windows doesn't do it for us
810 // which happens if we're attached to a frame
811 if (m_hMenu
&& !IsAttached())
813 #if defined(WINCE_WITH_COMMANDBAR)
814 ::DestroyWindow((HWND
) m_commandBar
);
815 m_commandBar
= (WXHWND
) NULL
;
817 ::DestroyMenu((HMENU
)m_hMenu
);
819 m_hMenu
= (WXHMENU
)NULL
;
824 // ---------------------------------------------------------------------------
826 // ---------------------------------------------------------------------------
828 void wxMenuBar::Refresh()
830 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
832 #if defined(WINCE_WITHOUT_COMMANDBAR)
835 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
837 #elif defined(WINCE_WITH_COMMANDBAR)
839 DrawMenuBar((HWND
) m_commandBar
);
841 DrawMenuBar(GetHwndOf(GetFrame()));
845 WXHMENU
wxMenuBar::Create()
847 // Note: this totally doesn't work on Smartphone,
848 // since you have to use resources.
849 // We'll have to find another way to add a menu
850 // by changing/adding menu items to an existing menu.
851 #if defined(WINCE_WITHOUT_COMMANDBAR)
858 HWND hCommandBar
= (HWND
) GetToolBar()->GetHWND();
859 HMENU hMenu
= (HMENU
)::SendMessage(hCommandBar
, SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
863 memset(&tbButton
, 0, sizeof(TBBUTTON
));
864 tbButton
.iBitmap
= I_IMAGENONE
;
865 tbButton
.fsState
= TBSTATE_ENABLED
;
866 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
869 for (i
= 0; i
< GetMenuCount(); i
++)
871 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu() ;
872 tbButton
.dwData
= (DWORD
)hPopupMenu
;
873 wxString label
= wxStripMenuCodes(GetLabelTop(i
));
874 tbButton
.iString
= (int) label
.c_str();
878 tbButton
.idCommand
= NewControlId();
879 if (!::SendMessage(hCommandBar
, TB_INSERTBUTTON
, position
, (LPARAM
)&tbButton
))
881 wxLogLastError(wxT("TB_INSERTBUTTON"));
885 m_hMenu
= (WXHMENU
) hMenu
;
891 m_hMenu
= (WXHMENU
)::CreateMenu();
895 wxLogLastError(wxT("CreateMenu"));
899 size_t count
= GetMenuCount(), i
;
900 wxMenuList::iterator it
;
901 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
903 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
904 (UINT
)(*it
)->GetHMenu(),
907 wxLogLastError(wxT("AppendMenu"));
916 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
919 wxASSERT(menu
->GetHMenu());
922 #if defined(__WXWINCE__)
923 int totalMSWItems
= GetMenuCount();
925 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
928 int i
; // For old C++ compatibility
929 for(i
=wxpos
; i
<totalMSWItems
; i
++)
931 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
934 for(i
=0; i
<wxpos
; i
++)
936 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
943 // ---------------------------------------------------------------------------
944 // wxMenuBar functions to work with the top level submenus
945 // ---------------------------------------------------------------------------
947 // NB: we don't support owner drawn top level items for now, if we do these
948 // functions would have to be changed to use wxMenuItem as well
950 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
952 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
953 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
955 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
957 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
962 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
964 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
966 m_titles
[pos
] = label
;
972 //else: have to modify the existing menu
974 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
977 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
978 if ( flagsOld
== 0xFFFFFFFF )
980 wxLogLastError(wxT("GetMenuState"));
985 if ( flagsOld
& MF_POPUP
)
987 // HIBYTE contains the number of items in the submenu in this case
989 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
999 info
.cbSize
= sizeof(info
);
1000 info
.fMask
= MIIM_TYPE
;
1001 info
.fType
= MFT_STRING
;
1002 info
.cch
= label
.Length();
1003 info
.dwTypeData
= (LPTSTR
) label
.c_str();
1004 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, & info
) )
1006 wxLogLastError(wxT("SetMenuItemInfo"));
1010 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1011 id
, label
) == (int)0xFFFFFFFF )
1013 wxLogLastError(wxT("ModifyMenu"));
1020 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1022 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1023 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
1025 return wxMenuItem::GetLabelFromText(m_titles
[pos
]);
1028 // ---------------------------------------------------------------------------
1029 // wxMenuBar construction
1030 // ---------------------------------------------------------------------------
1032 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1034 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1038 m_titles
[pos
] = title
;
1042 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1044 // can't use ModifyMenu() because it deletes the submenu it replaces
1045 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1047 wxLogLastError(wxT("RemoveMenu"));
1050 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1051 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1052 (UINT
)GetHmenuOf(menu
), title
) )
1054 wxLogLastError(wxT("InsertMenu"));
1058 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1060 // need to rebuild accell table
1061 RebuildAccelTable();
1063 #endif // wxUSE_ACCEL
1071 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1073 // Find out which MSW item before which we'll be inserting before
1074 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1075 // If IsAttached() is false this won't be used anyway
1076 int mswpos
= (!IsAttached() || (pos
== m_menus
.GetCount()))
1077 ? -1 // append the menu
1078 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1080 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1083 m_titles
.Insert(title
, pos
);
1087 #if defined(WINCE_WITHOUT_COMMANDAR)
1091 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1092 tbButton
.iBitmap
= I_IMAGENONE
;
1093 tbButton
.fsState
= TBSTATE_ENABLED
;
1094 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1096 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1097 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1098 wxString label
= wxStripMenuCodes(title
);
1099 tbButton
.iString
= (int) label
.c_str();
1101 tbButton
.idCommand
= NewControlId();
1102 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1104 wxLogLastError(wxT("TB_INSERTBUTTON"));
1108 if ( !::InsertMenu(GetHmenu(), mswpos
,
1109 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1110 (UINT
)GetHmenuOf(menu
), title
) )
1112 wxLogLastError(wxT("InsertMenu"));
1116 if ( menu
->HasAccels() )
1118 // need to rebuild accell table
1119 RebuildAccelTable();
1121 #endif // wxUSE_ACCEL
1129 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1131 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1132 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1134 if ( !wxMenuBarBase::Append(menu
, title
) )
1137 m_titles
.Add(title
);
1141 #if defined(WINCE_WITHOUT_COMMANDAR)
1145 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1146 tbButton
.iBitmap
= I_IMAGENONE
;
1147 tbButton
.fsState
= TBSTATE_ENABLED
;
1148 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1150 size_t pos
= GetMenuCount();
1151 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1152 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1153 wxString label
= wxStripMenuCodes(title
);
1154 tbButton
.iString
= (int) label
.c_str();
1156 tbButton
.idCommand
= NewControlId();
1157 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1159 wxLogLastError(wxT("TB_INSERTBUTTON"));
1163 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1164 (UINT
)submenu
, title
) )
1166 wxLogLastError(wxT("AppendMenu"));
1171 if ( menu
->HasAccels() )
1173 // need to rebuild accelerator table
1174 RebuildAccelTable();
1176 #endif // wxUSE_ACCEL
1184 wxMenu
*wxMenuBar::Remove(size_t pos
)
1186 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1192 #if defined(WINCE_WITHOUT_COMMANDAR)
1195 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1197 wxLogLastError(wxT("TB_DELETEBUTTON"));
1201 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1203 wxLogLastError(wxT("RemoveMenu"));
1208 if ( menu
->HasAccels() )
1210 // need to rebuild accell table
1211 RebuildAccelTable();
1213 #endif // wxUSE_ACCEL
1219 m_titles
.RemoveAt(pos
);
1226 void wxMenuBar::RebuildAccelTable()
1228 // merge the accelerators of all menus into one accel table
1229 size_t nAccelCount
= 0;
1230 size_t i
, count
= GetMenuCount();
1231 wxMenuList::iterator it
;
1232 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1234 nAccelCount
+= (*it
)->GetAccelCount();
1239 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1242 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1244 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1247 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1249 delete [] accelEntries
;
1253 #endif // wxUSE_ACCEL
1255 void wxMenuBar::Attach(wxFrame
*frame
)
1257 wxMenuBarBase::Attach(frame
);
1259 #if defined(WINCE_WITH_COMMANDBAR)
1263 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1268 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1270 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1277 RebuildAccelTable();
1278 #endif // wxUSE_ACCEL
1281 #if defined(WINCE_WITH_COMMANDBAR)
1282 bool wxMenuBar::AddAdornments(long style
)
1284 if (m_adornmentsAdded
|| !m_commandBar
)
1287 if (style
& wxCLOSE_BOX
)
1289 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1290 wxLogLastError(wxT("CommandBar_AddAdornments"));
1298 void wxMenuBar::Detach()
1300 wxMenuBarBase::Detach();
1303 #endif // wxUSE_MENUS