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 hmenu
, UINT id
)
97 mii
.cbSize
= sizeof(MENUITEMINFO
);
98 mii
.fMask
= MIIM_STATE
;
99 mii
.fState
= MFS_DEFAULT
;
101 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
103 wxLogLastError(wxT("SetMenuItemInfo"));
112 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
116 info
.cbSize
= sizeof(info
);
117 info
.fMask
= MIIM_STATE
;
118 // MF_BYCOMMAND is zero so test MF_BYPOSITION
119 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
120 wxLogLastError(wxT("GetMenuItemInfo"));
125 // ============================================================================
127 // ============================================================================
129 #include <wx/listimpl.cpp>
131 WX_DEFINE_LIST( wxMenuInfoList
) ;
133 #if wxUSE_EXTENDED_RTTI
135 WX_DEFINE_FLAGS( wxMenuStyle
)
137 wxBEGIN_FLAGS( wxMenuStyle
)
138 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
139 wxEND_FLAGS( wxMenuStyle
)
141 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
143 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
145 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
147 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
150 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
151 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
152 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
153 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
154 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
155 wxEND_PROPERTIES_TABLE()
157 wxBEGIN_HANDLERS_TABLE(wxMenu
)
158 wxEND_HANDLERS_TABLE()
160 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
162 WX_DEFINE_FLAGS( wxMenuBarStyle
)
164 wxBEGIN_FLAGS( wxMenuBarStyle
)
165 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
166 wxEND_FLAGS( wxMenuBarStyle
)
168 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
169 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
174 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
176 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
178 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
179 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
180 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
181 wxEND_PROPERTIES_TABLE()
183 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
184 wxEND_HANDLERS_TABLE()
186 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
188 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
190 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
192 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
195 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
196 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
197 wxEND_PROPERTIES_TABLE()
199 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
200 wxEND_HANDLERS_TABLE()
202 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
205 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
206 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
207 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
210 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
212 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
213 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
214 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
216 wxMenuInfo
* info
= new wxMenuInfo() ;
217 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetLabelTop(i
) ) ;
218 list
->Append( info
) ;
223 // ---------------------------------------------------------------------------
224 // wxMenu construction, adding and removing menu items
225 // ---------------------------------------------------------------------------
227 // Construct a menu with optional title (then use append)
231 m_startRadioGroup
= -1;
234 m_hMenu
= (WXHMENU
)CreatePopupMenu();
237 wxLogLastError(wxT("CreatePopupMenu"));
240 // if we have a title, insert it in the beginning of the menu
241 if ( !m_title
.empty() )
243 Append(idMenuTitle
, m_title
);
248 // The wxWindow destructor will take care of deleting the submenus.
251 // we should free Windows resources only if Windows doesn't do it for us
252 // which happens if we're attached to a menubar or a submenu of another
254 if ( !IsAttached() && !GetParent() )
256 if ( !::DestroyMenu(GetHmenu()) )
258 wxLogLastError(wxT("DestroyMenu"));
264 WX_CLEAR_ARRAY(m_accels
);
265 #endif // wxUSE_ACCEL
270 // this will take effect during the next call to Append()
274 void wxMenu::Attach(wxMenuBarBase
*menubar
)
276 wxMenuBase::Attach(menubar
);
283 int wxMenu::FindAccel(int id
) const
285 size_t n
, count
= m_accels
.GetCount();
286 for ( n
= 0; n
< count
; n
++ )
288 if ( m_accels
[n
]->m_command
== id
)
295 void wxMenu::UpdateAccel(wxMenuItem
*item
)
297 if ( item
->IsSubMenu() )
299 wxMenu
*submenu
= item
->GetSubMenu();
300 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
303 UpdateAccel(node
->GetData());
305 node
= node
->GetNext();
308 else if ( !item
->IsSeparator() )
310 // find the (new) accel for this item
311 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
313 accel
->m_command
= item
->GetId();
316 int n
= FindAccel(item
->GetId());
317 if ( n
== wxNOT_FOUND
)
319 // no old, add new if any
323 return; // skipping RebuildAccelTable() below
327 // replace old with new or just remove the old one if no new
332 m_accels
.RemoveAt(n
);
337 GetMenuBar()->RebuildAccelTable();
340 //else: it is a separator, they can't have accels, nothing to do
343 #endif // wxUSE_ACCEL
345 // append a new item or submenu to the menu
346 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
350 #endif // wxUSE_ACCEL
354 // if "Break" has just been called, insert a menu break before this item
355 // (and don't forget to reset the flag)
357 flags
|= MF_MENUBREAK
;
361 if ( pItem
->IsSeparator() ) {
362 flags
|= MF_SEPARATOR
;
365 // id is the numeric id for normal menu items and HMENU for submenus as
366 // required by ::AppendMenu() API
368 wxMenu
*submenu
= pItem
->GetSubMenu();
369 if ( submenu
!= NULL
) {
370 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
372 submenu
->SetParent(this);
374 id
= (UINT
)submenu
->GetHMenu();
383 // prepare to insert the item in the menu
384 wxString itemText
= pItem
->GetText();
385 LPCTSTR pData
= NULL
;
386 if ( pos
== (size_t)-1 )
389 pos
= ::GetMenuItemCount(GetHmenu());
394 // check if we have something more than a simple text item
395 #if wxUSE_OWNER_DRAWN
396 if ( pItem
->IsOwnerDrawn() )
398 // is the item owner-drawn just because of the bitmap?
399 if ( pItem
->GetBitmap().Ok() &&
400 !pItem
->GetTextColour().Ok() &&
401 !pItem
->GetBackgroundColour().Ok() &&
402 !pItem
->GetFont().Ok() )
404 // try to use InsertMenuItem() as it's guaranteed to look correctly
405 // while our owner-drawning code is not
407 // first compile-time check
409 WinStruct
<MENUITEMINFO
> mii
;
411 // now run-time one: MIIM_BITMAP only works under WinME/2000+
412 if ( wxGetWinVersion() >= wxWinVersion_98
)
414 mii
.fMask
= MIIM_ID
| MIIM_STRING
| MIIM_DATA
| MIIM_BITMAP
;
416 mii
.cch
= itemText
.length();
417 mii
.dwTypeData
= wx_const_cast(wxChar
*, itemText
.c_str());
419 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
420 // 1. we can't draw it with transparency then (this is not
421 // very important now but would be with themed menu bg)
422 // 2. worse, Windows inverses the bitmap for the selected
423 // item and this looks downright ugly
425 // so instead draw it ourselves in MSWOnDrawItem()
426 mii
.dwItemData
= wx_reinterpret_cast(ULONG_PTR
, pItem
);
427 mii
.hbmpItem
= HBMMENU_CALLBACK
;
429 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
432 wxLogLastError(wxT("InsertMenuItem()"));
434 else // InsertMenuItem() ok
436 // we need to remove the extra indent which is reserved for
437 // the checkboxes by default as it looks ugly unless check
438 // boxes are used together with bitmaps and this is not the
440 WinStruct
<MENUINFO
> mi
;
442 // don't call SetMenuInfo() directly, this would prevent
443 // the app from starting up under Windows 95/NT 4
444 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
446 wxDynamicLibrary
dllUser(_T("user32"));
447 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
448 if ( pfnSetMenuInfo
)
450 mi
.fMask
= MIM_STYLE
;
451 mi
.dwStyle
= MNS_CHECKORBMP
;
452 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
453 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
456 // tell the item that it's not really owner-drawn but only
457 // needs to draw its bitmap, the rest is done by Windows
458 pItem
->ResetOwnerDrawn();
461 #endif // MIIM_BITMAP
466 // item draws itself, pass pointer to it in data parameter
467 flags
|= MF_OWNERDRAW
;
468 pData
= (LPCTSTR
)pItem
;
472 #endif // wxUSE_OWNER_DRAWN
474 // menu is just a normal string (passed in data parameter)
478 itemText
= wxMenuItem::GetLabelFromText(itemText
);
481 pData
= (wxChar
*)itemText
.c_str();
484 // item might have been already inserted by InsertMenuItem() above
487 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
489 wxLogLastError(wxT("InsertMenu[Item]()"));
496 // if we just appended the title, highlight it
497 if ( (int)id
== idMenuTitle
)
499 // visually select the menu title
500 SetDefaultMenuItem(GetHmenu(), id
);
503 // if we're already attached to the menubar, we must update it
504 if ( IsAttached() && GetMenuBar()->IsAttached() )
506 GetMenuBar()->Refresh();
512 void wxMenu::EndRadioGroup()
514 // we're not inside a radio group any longer
515 m_startRadioGroup
= -1;
518 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
520 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
524 if ( item
->GetKind() == wxITEM_RADIO
)
526 int count
= GetMenuItemCount();
528 if ( m_startRadioGroup
== -1 )
530 // start a new radio group
531 m_startRadioGroup
= count
;
533 // for now it has just one element
534 item
->SetAsRadioGroupStart();
535 item
->SetRadioGroupEnd(m_startRadioGroup
);
537 // ensure that we have a checked item in the radio group
540 else // extend the current radio group
542 // we need to update its end item
543 item
->SetRadioGroupStart(m_startRadioGroup
);
544 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
548 node
->GetData()->SetRadioGroupEnd(count
);
552 wxFAIL_MSG( _T("where is the radio group start item?") );
556 else // not a radio item
561 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
568 // check the item initially
575 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
577 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
583 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
585 // we need to find the items position in the child list
587 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
588 for ( pos
= 0; node
; pos
++ )
590 if ( node
->GetData() == item
)
593 node
= node
->GetNext();
596 // DoRemove() (unlike Remove) can only be called for existing item!
597 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
600 // remove the corresponding accel from the accel table
601 int n
= FindAccel(item
->GetId());
602 if ( n
!= wxNOT_FOUND
)
606 m_accels
.RemoveAt(n
);
608 //else: this item doesn't have an accel, nothing to do
609 #endif // wxUSE_ACCEL
611 // remove the item from the menu
612 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
614 wxLogLastError(wxT("RemoveMenu"));
617 if ( IsAttached() && GetMenuBar()->IsAttached() )
619 // otherwise, the chane won't be visible
620 GetMenuBar()->Refresh();
623 // and from internal data structures
624 return wxMenuBase::DoRemove(item
);
627 // ---------------------------------------------------------------------------
628 // accelerator helpers
629 // ---------------------------------------------------------------------------
633 // create the wxAcceleratorEntries for our accels and put them into provided
634 // array - return the number of accels we have
635 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
637 size_t count
= GetAccelCount();
638 for ( size_t n
= 0; n
< count
; n
++ )
640 *accels
++ = *m_accels
[n
];
646 #endif // wxUSE_ACCEL
648 // ---------------------------------------------------------------------------
650 // ---------------------------------------------------------------------------
652 void wxMenu::SetTitle(const wxString
& label
)
654 bool hasNoTitle
= m_title
.empty();
657 HMENU hMenu
= GetHmenu();
661 if ( !label
.empty() )
663 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
664 (unsigned)idMenuTitle
, m_title
) ||
665 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
667 wxLogLastError(wxT("InsertMenu"));
675 // remove the title and the separator after it
676 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
677 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
679 wxLogLastError(wxT("RemoveMenu"));
688 info
.cbSize
= sizeof(info
);
689 info
.fMask
= MIIM_TYPE
;
690 info
.fType
= MFT_STRING
;
691 info
.cch
= m_title
.Length();
692 info
.dwTypeData
= (LPTSTR
) m_title
.c_str();
693 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
695 wxLogLastError(wxT("SetMenuItemInfo"));
698 if ( !ModifyMenu(hMenu
, 0u,
699 MF_BYPOSITION
| MF_STRING
,
700 (unsigned)idMenuTitle
, m_title
) )
702 wxLogLastError(wxT("ModifyMenu"));
709 // put the title string in bold face
710 if ( !m_title
.empty() )
712 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
717 // ---------------------------------------------------------------------------
719 // ---------------------------------------------------------------------------
721 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
723 // ignore commands from the menu title
724 if ( id
!= (WXWORD
)idMenuTitle
)
726 // get the checked status of the command: notice that menuState is the
727 // old state of the menu, so the test for MF_CHECKED must be inverted
728 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
729 SendEvent(id
, !(menuState
& MF_CHECKED
));
735 // ---------------------------------------------------------------------------
737 // ---------------------------------------------------------------------------
739 wxWindow
*wxMenu::GetWindow() const
741 if ( m_invokingWindow
!= NULL
)
742 return m_invokingWindow
;
743 else if ( GetMenuBar() != NULL
)
744 return GetMenuBar()->GetFrame();
749 // ---------------------------------------------------------------------------
751 // ---------------------------------------------------------------------------
753 void wxMenuBar::Init()
755 m_eventHandler
= this;
757 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
760 // Not using a combined wxToolBar/wxMenuBar? then use
761 // a commandbar in WinCE .NET just to implement the
763 #if defined(WINCE_WITH_COMMANDBAR)
765 m_adornmentsAdded
= false;
769 wxMenuBar::wxMenuBar()
774 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
779 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
783 m_titles
.Alloc(count
);
785 for ( size_t i
= 0; i
< count
; i
++ )
787 m_menus
.Append(menus
[i
]);
788 m_titles
.Add(titles
[i
]);
790 menus
[i
]->Attach(this);
794 wxMenuBar::~wxMenuBar()
796 // In Windows CE (not .NET), the menubar is always associated
797 // with a toolbar, which destroys the menu implicitly.
798 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
801 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
803 toolMenuBar
->SetMenuBar(NULL
);
806 // we should free Windows resources only if Windows doesn't do it for us
807 // which happens if we're attached to a frame
808 if (m_hMenu
&& !IsAttached())
810 #if defined(WINCE_WITH_COMMANDBAR)
811 ::DestroyWindow((HWND
) m_commandBar
);
812 m_commandBar
= (WXHWND
) NULL
;
814 ::DestroyMenu((HMENU
)m_hMenu
);
816 m_hMenu
= (WXHMENU
)NULL
;
821 // ---------------------------------------------------------------------------
823 // ---------------------------------------------------------------------------
825 void wxMenuBar::Refresh()
827 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
829 #if defined(WINCE_WITHOUT_COMMANDBAR)
832 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
834 #elif defined(WINCE_WITH_COMMANDBAR)
836 DrawMenuBar((HWND
) m_commandBar
);
838 DrawMenuBar(GetHwndOf(GetFrame()));
842 WXHMENU
wxMenuBar::Create()
844 // Note: this totally doesn't work on Smartphone,
845 // since you have to use resources.
846 // We'll have to find another way to add a menu
847 // by changing/adding menu items to an existing menu.
848 #if defined(WINCE_WITHOUT_COMMANDBAR)
855 HWND hCommandBar
= (HWND
) GetToolBar()->GetHWND();
856 HMENU hMenu
= (HMENU
)::SendMessage(hCommandBar
, SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
860 memset(&tbButton
, 0, sizeof(TBBUTTON
));
861 tbButton
.iBitmap
= I_IMAGENONE
;
862 tbButton
.fsState
= TBSTATE_ENABLED
;
863 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
866 for (i
= 0; i
< GetMenuCount(); i
++)
868 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu() ;
869 tbButton
.dwData
= (DWORD
)hPopupMenu
;
870 wxString label
= wxStripMenuCodes(GetLabelTop(i
));
871 tbButton
.iString
= (int) label
.c_str();
875 tbButton
.idCommand
= NewControlId();
876 if (!::SendMessage(hCommandBar
, TB_INSERTBUTTON
, position
, (LPARAM
)&tbButton
))
878 wxLogLastError(wxT("TB_INSERTBUTTON"));
882 m_hMenu
= (WXHMENU
) hMenu
;
888 m_hMenu
= (WXHMENU
)::CreateMenu();
892 wxLogLastError(wxT("CreateMenu"));
896 size_t count
= GetMenuCount(), i
;
897 wxMenuList::iterator it
;
898 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
900 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
901 (UINT
)(*it
)->GetHMenu(),
904 wxLogLastError(wxT("AppendMenu"));
913 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
916 wxASSERT(menu
->GetHMenu());
919 #if defined(__WXWINCE__)
920 int totalMSWItems
= GetMenuCount();
922 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
925 int i
; // For old C++ compatibility
926 for(i
=wxpos
; i
<totalMSWItems
; i
++)
928 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
931 for(i
=0; i
<wxpos
; i
++)
933 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
940 // ---------------------------------------------------------------------------
941 // wxMenuBar functions to work with the top level submenus
942 // ---------------------------------------------------------------------------
944 // NB: we don't support owner drawn top level items for now, if we do these
945 // functions would have to be changed to use wxMenuItem as well
947 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
949 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
950 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
952 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
954 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
959 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
961 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
963 m_titles
[pos
] = label
;
969 //else: have to modify the existing menu
971 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
974 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
975 if ( flagsOld
== 0xFFFFFFFF )
977 wxLogLastError(wxT("GetMenuState"));
982 if ( flagsOld
& MF_POPUP
)
984 // HIBYTE contains the number of items in the submenu in this case
986 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
996 info
.cbSize
= sizeof(info
);
997 info
.fMask
= MIIM_TYPE
;
998 info
.fType
= MFT_STRING
;
999 info
.cch
= label
.Length();
1000 info
.dwTypeData
= (LPTSTR
) label
.c_str();
1001 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, & info
) )
1003 wxLogLastError(wxT("SetMenuItemInfo"));
1007 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1008 id
, label
) == (int)0xFFFFFFFF )
1010 wxLogLastError(wxT("ModifyMenu"));
1017 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1019 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1020 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
1022 return wxMenuItem::GetLabelFromText(m_titles
[pos
]);
1025 // ---------------------------------------------------------------------------
1026 // wxMenuBar construction
1027 // ---------------------------------------------------------------------------
1029 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1031 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1035 m_titles
[pos
] = title
;
1039 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1041 // can't use ModifyMenu() because it deletes the submenu it replaces
1042 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1044 wxLogLastError(wxT("RemoveMenu"));
1047 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1048 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1049 (UINT
)GetHmenuOf(menu
), title
) )
1051 wxLogLastError(wxT("InsertMenu"));
1055 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1057 // need to rebuild accell table
1058 RebuildAccelTable();
1060 #endif // wxUSE_ACCEL
1068 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1070 // Find out which MSW item before which we'll be inserting before
1071 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1072 // If IsAttached() is false this won't be used anyway
1073 int mswpos
= (!IsAttached() || (pos
== m_menus
.GetCount()))
1074 ? -1 // append the menu
1075 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1077 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1080 m_titles
.Insert(title
, pos
);
1084 #if defined(WINCE_WITHOUT_COMMANDAR)
1088 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1089 tbButton
.iBitmap
= I_IMAGENONE
;
1090 tbButton
.fsState
= TBSTATE_ENABLED
;
1091 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1093 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1094 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1095 wxString label
= wxStripMenuCodes(title
);
1096 tbButton
.iString
= (int) label
.c_str();
1098 tbButton
.idCommand
= NewControlId();
1099 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1101 wxLogLastError(wxT("TB_INSERTBUTTON"));
1105 if ( !::InsertMenu(GetHmenu(), mswpos
,
1106 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1107 (UINT
)GetHmenuOf(menu
), title
) )
1109 wxLogLastError(wxT("InsertMenu"));
1113 if ( menu
->HasAccels() )
1115 // need to rebuild accell table
1116 RebuildAccelTable();
1118 #endif // wxUSE_ACCEL
1126 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1128 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1129 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1131 if ( !wxMenuBarBase::Append(menu
, title
) )
1134 m_titles
.Add(title
);
1138 #if defined(WINCE_WITHOUT_COMMANDAR)
1142 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1143 tbButton
.iBitmap
= I_IMAGENONE
;
1144 tbButton
.fsState
= TBSTATE_ENABLED
;
1145 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1147 size_t pos
= GetMenuCount();
1148 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1149 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1150 wxString label
= wxStripMenuCodes(title
);
1151 tbButton
.iString
= (int) label
.c_str();
1153 tbButton
.idCommand
= NewControlId();
1154 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1156 wxLogLastError(wxT("TB_INSERTBUTTON"));
1160 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1161 (UINT
)submenu
, title
) )
1163 wxLogLastError(wxT("AppendMenu"));
1168 if ( menu
->HasAccels() )
1170 // need to rebuild accelerator table
1171 RebuildAccelTable();
1173 #endif // wxUSE_ACCEL
1181 wxMenu
*wxMenuBar::Remove(size_t pos
)
1183 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1189 #if defined(WINCE_WITHOUT_COMMANDAR)
1192 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1194 wxLogLastError(wxT("TB_DELETEBUTTON"));
1198 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1200 wxLogLastError(wxT("RemoveMenu"));
1205 if ( menu
->HasAccels() )
1207 // need to rebuild accell table
1208 RebuildAccelTable();
1210 #endif // wxUSE_ACCEL
1216 m_titles
.RemoveAt(pos
);
1223 void wxMenuBar::RebuildAccelTable()
1225 // merge the accelerators of all menus into one accel table
1226 size_t nAccelCount
= 0;
1227 size_t i
, count
= GetMenuCount();
1228 wxMenuList::iterator it
;
1229 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1231 nAccelCount
+= (*it
)->GetAccelCount();
1236 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1239 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1241 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1244 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1246 delete [] accelEntries
;
1250 #endif // wxUSE_ACCEL
1252 void wxMenuBar::Attach(wxFrame
*frame
)
1254 wxMenuBarBase::Attach(frame
);
1256 #if defined(WINCE_WITH_COMMANDBAR)
1260 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1265 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1267 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1274 RebuildAccelTable();
1275 #endif // wxUSE_ACCEL
1278 #if defined(WINCE_WITH_COMMANDBAR)
1279 bool wxMenuBar::AddAdornments(long style
)
1281 if (m_adornmentsAdded
|| !m_commandBar
)
1284 if (style
& wxCLOSE_BOX
)
1286 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1287 wxLogLastError(wxT("CommandBar_AddAdornments"));
1295 void wxMenuBar::Detach()
1297 wxMenuBarBase::Detach();
1300 #endif // wxUSE_MENUS