1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/menu.cpp
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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
38 #include "wx/ownerdrw.h"
41 #include "wx/msw/private.h"
50 #if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__)
54 #include "wx/msw/wince/missing.h"
58 // other standard headers
61 #if wxUSE_OWNER_DRAWN && defined(MIIM_BITMAP)
62 #include "wx/dynlib.h"
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
WXUNUSED_IN_WINCE(hmenu
),
89 UINT
WXUNUSED_IN_WINCE(id
))
94 mii
.cbSize
= sizeof(MENUITEMINFO
);
95 mii
.fMask
= MIIM_STATE
;
96 mii
.fState
= MFS_DEFAULT
;
98 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
100 wxLogLastError(wxT("SetMenuItemInfo"));
106 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
110 info
.cbSize
= sizeof(info
);
111 info
.fMask
= MIIM_STATE
;
112 // MF_BYCOMMAND is zero so test MF_BYPOSITION
113 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
114 wxLogLastError(wxT("GetMenuItemInfo"));
119 // ============================================================================
121 // ============================================================================
123 #include "wx/listimpl.cpp"
125 WX_DEFINE_LIST( wxMenuInfoList
)
127 #if wxUSE_EXTENDED_RTTI
129 WX_DEFINE_FLAGS( wxMenuStyle
)
131 wxBEGIN_FLAGS( wxMenuStyle
)
132 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
133 wxEND_FLAGS( wxMenuStyle
)
135 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
137 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
139 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
141 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
144 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
145 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
146 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
147 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
148 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
149 wxEND_PROPERTIES_TABLE()
151 wxBEGIN_HANDLERS_TABLE(wxMenu
)
152 wxEND_HANDLERS_TABLE()
154 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
156 WX_DEFINE_FLAGS( wxMenuBarStyle
)
158 wxBEGIN_FLAGS( wxMenuBarStyle
)
159 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
160 wxEND_FLAGS( wxMenuBarStyle
)
162 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
163 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
168 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
170 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
172 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
173 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
174 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
175 wxEND_PROPERTIES_TABLE()
177 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
178 wxEND_HANDLERS_TABLE()
180 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
182 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
184 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
186 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
189 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
190 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
191 wxEND_PROPERTIES_TABLE()
193 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
194 wxEND_HANDLERS_TABLE()
196 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
199 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
200 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
201 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
204 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
206 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
207 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
208 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
210 wxMenuInfo
* info
= new wxMenuInfo() ;
211 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetLabelTop(i
) ) ;
212 list
->Append( info
) ;
217 // ---------------------------------------------------------------------------
218 // wxMenu construction, adding and removing menu items
219 // ---------------------------------------------------------------------------
221 // Construct a menu with optional title (then use append)
225 m_startRadioGroup
= -1;
228 m_hMenu
= (WXHMENU
)CreatePopupMenu();
231 wxLogLastError(wxT("CreatePopupMenu"));
234 // if we have a title, insert it in the beginning of the menu
235 if ( !m_title
.empty() )
237 Append(idMenuTitle
, m_title
);
242 // The wxWindow destructor will take care of deleting the submenus.
245 // we should free Windows resources only if Windows doesn't do it for us
246 // which happens if we're attached to a menubar or a submenu of another
248 if ( !IsAttached() && !GetParent() )
250 if ( !::DestroyMenu(GetHmenu()) )
252 wxLogLastError(wxT("DestroyMenu"));
258 WX_CLEAR_ARRAY(m_accels
);
259 #endif // wxUSE_ACCEL
264 // this will take effect during the next call to Append()
268 void wxMenu::Attach(wxMenuBarBase
*menubar
)
270 wxMenuBase::Attach(menubar
);
277 int wxMenu::FindAccel(int id
) const
279 size_t n
, count
= m_accels
.GetCount();
280 for ( n
= 0; n
< count
; n
++ )
282 if ( m_accels
[n
]->m_command
== id
)
289 void wxMenu::UpdateAccel(wxMenuItem
*item
)
291 if ( item
->IsSubMenu() )
293 wxMenu
*submenu
= item
->GetSubMenu();
294 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
297 UpdateAccel(node
->GetData());
299 node
= node
->GetNext();
302 else if ( !item
->IsSeparator() )
304 // recurse upwards: we should only modify m_accels of the top level
305 // menus, not of the submenus as wxMenuBar doesn't look at them
306 // (alternative and arguable cleaner solution would be to recurse
307 // downwards in GetAccelCount() and CopyAccels())
310 GetParent()->UpdateAccel(item
);
314 // find the (new) accel for this item
315 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
317 accel
->m_command
= item
->GetId();
320 int n
= FindAccel(item
->GetId());
321 if ( n
== wxNOT_FOUND
)
323 // no old, add new if any
327 return; // skipping RebuildAccelTable() below
331 // replace old with new or just remove the old one if no new
336 m_accels
.RemoveAt(n
);
341 GetMenuBar()->RebuildAccelTable();
344 //else: it is a separator, they can't have accels, nothing to do
347 #endif // wxUSE_ACCEL
349 // append a new item or submenu to the menu
350 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
354 #endif // wxUSE_ACCEL
358 // if "Break" has just been called, insert a menu break before this item
359 // (and don't forget to reset the flag)
361 flags
|= MF_MENUBREAK
;
365 if ( pItem
->IsSeparator() ) {
366 flags
|= MF_SEPARATOR
;
369 // id is the numeric id for normal menu items and HMENU for submenus as
370 // required by ::AppendMenu() API
372 wxMenu
*submenu
= pItem
->GetSubMenu();
373 if ( submenu
!= NULL
) {
374 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
376 submenu
->SetParent(this);
378 id
= (UINT
)submenu
->GetHMenu();
387 // prepare to insert the item in the menu
388 wxString itemText
= pItem
->GetText();
389 LPCTSTR pData
= NULL
;
390 if ( pos
== (size_t)-1 )
392 // append at the end (note that the item is already appended to
393 // internal data structures)
394 pos
= GetMenuItemCount() - 1;
397 // adjust position to account for the title, if any
398 if ( !m_title
.empty() )
399 pos
+= 2; // for the title itself and its separator
403 // check if we have something more than a simple text item
404 #if wxUSE_OWNER_DRAWN
405 if ( pItem
->IsOwnerDrawn() )
407 // is the item owner-drawn just because of the bitmap?
408 if ( pItem
->GetBitmap().Ok() &&
409 !pItem
->GetTextColour().Ok() &&
410 !pItem
->GetBackgroundColour().Ok() &&
411 !pItem
->GetFont().Ok() )
413 // try to use InsertMenuItem() as it's guaranteed to look correctly
414 // while our owner-drawning code is not
416 // first compile-time check
418 WinStruct
<MENUITEMINFO
> mii
;
420 // now run-time one: MIIM_BITMAP only works under WinME/2000+
421 if ( wxGetWinVersion() >= wxWinVersion_98
)
423 mii
.fMask
= MIIM_ID
| MIIM_STRING
| MIIM_DATA
| MIIM_BITMAP
;
425 mii
.cch
= itemText
.length();
426 mii
.dwTypeData
= wx_const_cast(wxChar
*, itemText
.c_str());
428 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
429 // 1. we can't draw it with transparency then (this is not
430 // very important now but would be with themed menu bg)
431 // 2. worse, Windows inverses the bitmap for the selected
432 // item and this looks downright ugly
434 // so instead draw it ourselves in MSWOnDrawItem()
435 mii
.dwItemData
= wx_reinterpret_cast(ULONG_PTR
, pItem
);
436 mii
.hbmpItem
= HBMMENU_CALLBACK
;
438 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
441 wxLogLastError(wxT("InsertMenuItem()"));
443 else // InsertMenuItem() ok
445 // we need to remove the extra indent which is reserved for
446 // the checkboxes by default as it looks ugly unless check
447 // boxes are used together with bitmaps and this is not the
449 WinStruct
<MENUINFO
> mi
;
451 // don't call SetMenuInfo() directly, this would prevent
452 // the app from starting up under Windows 95/NT 4
453 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
455 wxDynamicLibrary
dllUser(_T("user32"));
456 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
457 if ( pfnSetMenuInfo
)
459 mi
.fMask
= MIM_STYLE
;
460 mi
.dwStyle
= MNS_CHECKORBMP
;
461 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
462 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
465 // tell the item that it's not really owner-drawn but only
466 // needs to draw its bitmap, the rest is done by Windows
467 pItem
->ResetOwnerDrawn();
470 #endif // MIIM_BITMAP
475 // item draws itself, pass pointer to it in data parameter
476 flags
|= MF_OWNERDRAW
;
477 pData
= (LPCTSTR
)pItem
;
481 #endif // wxUSE_OWNER_DRAWN
483 // menu is just a normal string (passed in data parameter)
487 itemText
= wxMenuItem::GetLabelFromText(itemText
);
490 pData
= (wxChar
*)itemText
.c_str();
493 // item might have been already inserted by InsertMenuItem() above
496 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
498 wxLogLastError(wxT("InsertMenu[Item]()"));
505 // if we just appended the title, highlight it
506 if ( (int)id
== idMenuTitle
)
508 // visually select the menu title
509 SetDefaultMenuItem(GetHmenu(), id
);
512 // if we're already attached to the menubar, we must update it
513 if ( IsAttached() && GetMenuBar()->IsAttached() )
515 GetMenuBar()->Refresh();
521 void wxMenu::EndRadioGroup()
523 // we're not inside a radio group any longer
524 m_startRadioGroup
= -1;
527 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
529 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
533 if ( item
->GetKind() == wxITEM_RADIO
)
535 int count
= GetMenuItemCount();
537 if ( m_startRadioGroup
== -1 )
539 // start a new radio group
540 m_startRadioGroup
= count
;
542 // for now it has just one element
543 item
->SetAsRadioGroupStart();
544 item
->SetRadioGroupEnd(m_startRadioGroup
);
546 // ensure that we have a checked item in the radio group
549 else // extend the current radio group
551 // we need to update its end item
552 item
->SetRadioGroupStart(m_startRadioGroup
);
553 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
557 node
->GetData()->SetRadioGroupEnd(count
);
561 wxFAIL_MSG( _T("where is the radio group start item?") );
565 else // not a radio item
570 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
577 // check the item initially
584 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
586 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
592 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
594 // we need to find the items position in the child list
596 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
597 for ( pos
= 0; node
; pos
++ )
599 if ( node
->GetData() == item
)
602 node
= node
->GetNext();
605 // DoRemove() (unlike Remove) can only be called for existing item!
606 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
609 // remove the corresponding accel from the accel table
610 int n
= FindAccel(item
->GetId());
611 if ( n
!= wxNOT_FOUND
)
615 m_accels
.RemoveAt(n
);
617 //else: this item doesn't have an accel, nothing to do
618 #endif // wxUSE_ACCEL
620 // remove the item from the menu
621 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
623 wxLogLastError(wxT("RemoveMenu"));
626 if ( IsAttached() && GetMenuBar()->IsAttached() )
628 // otherwise, the chane won't be visible
629 GetMenuBar()->Refresh();
632 // and from internal data structures
633 return wxMenuBase::DoRemove(item
);
636 // ---------------------------------------------------------------------------
637 // accelerator helpers
638 // ---------------------------------------------------------------------------
642 // create the wxAcceleratorEntries for our accels and put them into provided
643 // array - return the number of accels we have
644 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
646 size_t count
= GetAccelCount();
647 for ( size_t n
= 0; n
< count
; n
++ )
649 *accels
++ = *m_accels
[n
];
655 #endif // wxUSE_ACCEL
657 // ---------------------------------------------------------------------------
659 // ---------------------------------------------------------------------------
661 void wxMenu::SetTitle(const wxString
& label
)
663 bool hasNoTitle
= m_title
.empty();
666 HMENU hMenu
= GetHmenu();
670 if ( !label
.empty() )
672 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
673 (unsigned)idMenuTitle
, m_title
) ||
674 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
676 wxLogLastError(wxT("InsertMenu"));
684 // remove the title and the separator after it
685 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
686 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
688 wxLogLastError(wxT("RemoveMenu"));
697 info
.cbSize
= sizeof(info
);
698 info
.fMask
= MIIM_TYPE
;
699 info
.fType
= MFT_STRING
;
700 info
.cch
= m_title
.length();
701 info
.dwTypeData
= (LPTSTR
) m_title
.c_str();
702 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
704 wxLogLastError(wxT("SetMenuItemInfo"));
707 if ( !ModifyMenu(hMenu
, 0u,
708 MF_BYPOSITION
| MF_STRING
,
709 (unsigned)idMenuTitle
, m_title
) )
711 wxLogLastError(wxT("ModifyMenu"));
718 // put the title string in bold face
719 if ( !m_title
.empty() )
721 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
726 // ---------------------------------------------------------------------------
728 // ---------------------------------------------------------------------------
730 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
732 // ignore commands from the menu title
733 if ( id
!= (WXWORD
)idMenuTitle
)
735 // get the checked status of the command: notice that menuState is the
736 // old state of the menu, so the test for MF_CHECKED must be inverted
737 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
738 SendEvent(id
, !(menuState
& MF_CHECKED
));
744 // ---------------------------------------------------------------------------
746 // ---------------------------------------------------------------------------
748 wxWindow
*wxMenu::GetWindow() const
750 if ( m_invokingWindow
!= NULL
)
751 return m_invokingWindow
;
752 else if ( GetMenuBar() != NULL
)
753 return GetMenuBar()->GetFrame();
758 // ---------------------------------------------------------------------------
760 // ---------------------------------------------------------------------------
762 void wxMenuBar::Init()
764 m_eventHandler
= this;
766 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
769 // Not using a combined wxToolBar/wxMenuBar? then use
770 // a commandbar in WinCE .NET just to implement the
772 #if defined(WINCE_WITH_COMMANDBAR)
774 m_adornmentsAdded
= false;
778 wxMenuBar::wxMenuBar()
783 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
788 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
792 m_titles
.Alloc(count
);
794 for ( size_t i
= 0; i
< count
; i
++ )
796 m_menus
.Append(menus
[i
]);
797 m_titles
.Add(titles
[i
]);
799 menus
[i
]->Attach(this);
803 wxMenuBar::~wxMenuBar()
805 // In Windows CE (not .NET), the menubar is always associated
806 // with a toolbar, which destroys the menu implicitly.
807 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
810 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
812 toolMenuBar
->SetMenuBar(NULL
);
815 // we should free Windows resources only if Windows doesn't do it for us
816 // which happens if we're attached to a frame
817 if (m_hMenu
&& !IsAttached())
819 #if defined(WINCE_WITH_COMMANDBAR)
820 ::DestroyWindow((HWND
) m_commandBar
);
821 m_commandBar
= (WXHWND
) NULL
;
823 ::DestroyMenu((HMENU
)m_hMenu
);
825 m_hMenu
= (WXHMENU
)NULL
;
830 // ---------------------------------------------------------------------------
832 // ---------------------------------------------------------------------------
834 void wxMenuBar::Refresh()
836 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
838 #if defined(WINCE_WITHOUT_COMMANDBAR)
841 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
843 #elif defined(WINCE_WITH_COMMANDBAR)
845 DrawMenuBar((HWND
) m_commandBar
);
847 DrawMenuBar(GetHwndOf(GetFrame()));
851 WXHMENU
wxMenuBar::Create()
853 // Note: this totally doesn't work on Smartphone,
854 // since you have to use resources.
855 // We'll have to find another way to add a menu
856 // by changing/adding menu items to an existing menu.
857 #if defined(WINCE_WITHOUT_COMMANDBAR)
864 HWND hCommandBar
= (HWND
) GetToolBar()->GetHWND();
865 HMENU hMenu
= (HMENU
)::SendMessage(hCommandBar
, SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
869 memset(&tbButton
, 0, sizeof(TBBUTTON
));
870 tbButton
.iBitmap
= I_IMAGENONE
;
871 tbButton
.fsState
= TBSTATE_ENABLED
;
872 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
875 for (i
= 0; i
< GetMenuCount(); i
++)
877 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu() ;
878 tbButton
.dwData
= (DWORD
)hPopupMenu
;
879 wxString label
= wxStripMenuCodes(GetLabelTop(i
));
880 tbButton
.iString
= (int) label
.c_str();
884 tbButton
.idCommand
= NewControlId();
885 if (!::SendMessage(hCommandBar
, TB_INSERTBUTTON
, position
, (LPARAM
)&tbButton
))
887 wxLogLastError(wxT("TB_INSERTBUTTON"));
891 m_hMenu
= (WXHMENU
) hMenu
;
897 m_hMenu
= (WXHMENU
)::CreateMenu();
901 wxLogLastError(wxT("CreateMenu"));
905 size_t count
= GetMenuCount(), i
;
906 wxMenuList::iterator it
;
907 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
909 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
910 (UINT
)(*it
)->GetHMenu(),
913 wxLogLastError(wxT("AppendMenu"));
922 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
925 wxASSERT(menu
->GetHMenu());
928 #if defined(__WXWINCE__)
929 int totalMSWItems
= GetMenuCount();
931 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
934 int i
; // For old C++ compatibility
935 for(i
=wxpos
; i
<totalMSWItems
; i
++)
937 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
940 for(i
=0; i
<wxpos
; i
++)
942 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
949 // ---------------------------------------------------------------------------
950 // wxMenuBar functions to work with the top level submenus
951 // ---------------------------------------------------------------------------
953 // NB: we don't support owner drawn top level items for now, if we do these
954 // functions would have to be changed to use wxMenuItem as well
956 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
958 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
959 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
961 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
963 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
968 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
970 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
972 m_titles
[pos
] = label
;
978 //else: have to modify the existing menu
980 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
983 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
984 if ( flagsOld
== 0xFFFFFFFF )
986 wxLogLastError(wxT("GetMenuState"));
991 if ( flagsOld
& MF_POPUP
)
993 // HIBYTE contains the number of items in the submenu in this case
995 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1005 info
.cbSize
= sizeof(info
);
1006 info
.fMask
= MIIM_TYPE
;
1007 info
.fType
= MFT_STRING
;
1008 info
.cch
= label
.length();
1009 info
.dwTypeData
= (LPTSTR
) label
.c_str();
1010 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, & info
) )
1012 wxLogLastError(wxT("SetMenuItemInfo"));
1016 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1017 id
, label
) == (int)0xFFFFFFFF )
1019 wxLogLastError(wxT("ModifyMenu"));
1026 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1028 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1029 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
1031 return wxMenuItem::GetLabelFromText(m_titles
[pos
]);
1034 // ---------------------------------------------------------------------------
1035 // wxMenuBar construction
1036 // ---------------------------------------------------------------------------
1038 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1040 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1044 m_titles
[pos
] = title
;
1048 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1050 // can't use ModifyMenu() because it deletes the submenu it replaces
1051 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1053 wxLogLastError(wxT("RemoveMenu"));
1056 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1057 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1058 (UINT
)GetHmenuOf(menu
), title
) )
1060 wxLogLastError(wxT("InsertMenu"));
1064 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1066 // need to rebuild accell table
1067 RebuildAccelTable();
1069 #endif // wxUSE_ACCEL
1077 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1079 // Find out which MSW item before which we'll be inserting before
1080 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1081 // If IsAttached() is false this won't be used anyway
1082 int mswpos
= (!IsAttached() || (pos
== m_menus
.GetCount()))
1083 ? -1 // append the menu
1084 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1086 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1089 m_titles
.Insert(title
, pos
);
1093 #if defined(WINCE_WITHOUT_COMMANDAR)
1097 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1098 tbButton
.iBitmap
= I_IMAGENONE
;
1099 tbButton
.fsState
= TBSTATE_ENABLED
;
1100 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1102 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1103 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1104 wxString label
= wxStripMenuCodes(title
);
1105 tbButton
.iString
= (int) label
.c_str();
1107 tbButton
.idCommand
= NewControlId();
1108 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1110 wxLogLastError(wxT("TB_INSERTBUTTON"));
1114 if ( !::InsertMenu(GetHmenu(), mswpos
,
1115 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1116 (UINT
)GetHmenuOf(menu
), title
) )
1118 wxLogLastError(wxT("InsertMenu"));
1122 if ( menu
->HasAccels() )
1124 // need to rebuild accell table
1125 RebuildAccelTable();
1127 #endif // wxUSE_ACCEL
1135 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1137 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1138 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1140 if ( !wxMenuBarBase::Append(menu
, title
) )
1143 m_titles
.Add(title
);
1147 #if defined(WINCE_WITHOUT_COMMANDAR)
1151 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1152 tbButton
.iBitmap
= I_IMAGENONE
;
1153 tbButton
.fsState
= TBSTATE_ENABLED
;
1154 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1156 size_t pos
= GetMenuCount();
1157 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1158 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1159 wxString label
= wxStripMenuCodes(title
);
1160 tbButton
.iString
= (int) label
.c_str();
1162 tbButton
.idCommand
= NewControlId();
1163 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1165 wxLogLastError(wxT("TB_INSERTBUTTON"));
1169 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1170 (UINT
)submenu
, title
) )
1172 wxLogLastError(wxT("AppendMenu"));
1177 if ( menu
->HasAccels() )
1179 // need to rebuild accelerator table
1180 RebuildAccelTable();
1182 #endif // wxUSE_ACCEL
1190 wxMenu
*wxMenuBar::Remove(size_t pos
)
1192 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1198 #if defined(WINCE_WITHOUT_COMMANDAR)
1201 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1203 wxLogLastError(wxT("TB_DELETEBUTTON"));
1207 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1209 wxLogLastError(wxT("RemoveMenu"));
1214 if ( menu
->HasAccels() )
1216 // need to rebuild accell table
1217 RebuildAccelTable();
1219 #endif // wxUSE_ACCEL
1225 m_titles
.RemoveAt(pos
);
1232 void wxMenuBar::RebuildAccelTable()
1234 // merge the accelerators of all menus into one accel table
1235 size_t nAccelCount
= 0;
1236 size_t i
, count
= GetMenuCount();
1237 wxMenuList::iterator it
;
1238 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1240 nAccelCount
+= (*it
)->GetAccelCount();
1245 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1248 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1250 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1253 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1255 delete [] accelEntries
;
1259 #endif // wxUSE_ACCEL
1261 void wxMenuBar::Attach(wxFrame
*frame
)
1263 wxMenuBarBase::Attach(frame
);
1265 #if defined(WINCE_WITH_COMMANDBAR)
1269 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1274 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1276 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1283 RebuildAccelTable();
1284 #endif // wxUSE_ACCEL
1287 #if defined(WINCE_WITH_COMMANDBAR)
1288 bool wxMenuBar::AddAdornments(long style
)
1290 if (m_adornmentsAdded
|| !m_commandBar
)
1293 if (style
& wxCLOSE_BOX
)
1295 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1296 wxLogLastError(wxT("CommandBar_AddAdornments"));
1304 void wxMenuBar::Detach()
1306 wxMenuBarBase::Detach();
1309 #endif // wxUSE_MENUS