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 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // the (popup) menu title has this special id
74 static const int idMenuTitle
= -3;
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 // make the given menu item default
81 static void SetDefaultMenuItem(HMENU hmenu
, UINT id
)
86 mii
.cbSize
= sizeof(MENUITEMINFO
);
87 mii
.fMask
= MIIM_STATE
;
88 mii
.fState
= MFS_DEFAULT
;
90 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
92 wxLogLastError(wxT("SetMenuItemInfo"));
101 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
105 info
.cbSize
= sizeof(info
);
106 info
.fMask
= MIIM_STATE
;
107 // MF_BYCOMMAND is zero so test MF_BYPOSITION
108 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
109 wxLogLastError(wxT("GetMenuItemInfo"));
114 // ============================================================================
116 // ============================================================================
118 #include <wx/listimpl.cpp>
120 WX_DEFINE_LIST( wxMenuInfoList
) ;
122 #if wxUSE_EXTENDED_RTTI
124 WX_DEFINE_FLAGS( wxMenuStyle
)
126 wxBEGIN_FLAGS( wxMenuStyle
)
127 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
128 wxEND_FLAGS( wxMenuStyle
)
130 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
132 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
134 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
136 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
139 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
140 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
141 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
142 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
143 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
144 wxEND_PROPERTIES_TABLE()
146 wxBEGIN_HANDLERS_TABLE(wxMenu
)
147 wxEND_HANDLERS_TABLE()
149 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
151 WX_DEFINE_FLAGS( wxMenuBarStyle
)
153 wxBEGIN_FLAGS( wxMenuBarStyle
)
154 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
155 wxEND_FLAGS( wxMenuBarStyle
)
157 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
158 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
163 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
165 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
167 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
168 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
169 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
170 wxEND_PROPERTIES_TABLE()
172 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
173 wxEND_HANDLERS_TABLE()
175 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
177 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
179 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
181 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
184 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
185 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
186 wxEND_PROPERTIES_TABLE()
188 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
189 wxEND_HANDLERS_TABLE()
191 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
194 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
195 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
196 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
199 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
201 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
202 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
203 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
205 wxMenuInfo
* info
= new wxMenuInfo() ;
206 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetLabelTop(i
) ) ;
207 list
->Append( info
) ;
212 // ---------------------------------------------------------------------------
213 // wxMenu construction, adding and removing menu items
214 // ---------------------------------------------------------------------------
216 // Construct a menu with optional title (then use append)
220 m_startRadioGroup
= -1;
223 m_hMenu
= (WXHMENU
)CreatePopupMenu();
226 wxLogLastError(wxT("CreatePopupMenu"));
229 // if we have a title, insert it in the beginning of the menu
230 if ( !m_title
.empty() )
232 Append(idMenuTitle
, m_title
);
237 // The wxWindow destructor will take care of deleting the submenus.
240 // we should free Windows resources only if Windows doesn't do it for us
241 // which happens if we're attached to a menubar or a submenu of another
243 if ( !IsAttached() && !GetParent() )
245 if ( !::DestroyMenu(GetHmenu()) )
247 wxLogLastError(wxT("DestroyMenu"));
253 WX_CLEAR_ARRAY(m_accels
);
254 #endif // wxUSE_ACCEL
259 // this will take effect during the next call to Append()
263 void wxMenu::Attach(wxMenuBarBase
*menubar
)
265 wxMenuBase::Attach(menubar
);
272 int wxMenu::FindAccel(int id
) const
274 size_t n
, count
= m_accels
.GetCount();
275 for ( n
= 0; n
< count
; n
++ )
277 if ( m_accels
[n
]->m_command
== id
)
284 void wxMenu::UpdateAccel(wxMenuItem
*item
)
286 if ( item
->IsSubMenu() )
288 wxMenu
*submenu
= item
->GetSubMenu();
289 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
292 UpdateAccel(node
->GetData());
294 node
= node
->GetNext();
297 else if ( !item
->IsSeparator() )
299 // find the (new) accel for this item
300 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
302 accel
->m_command
= item
->GetId();
305 int n
= FindAccel(item
->GetId());
306 if ( n
== wxNOT_FOUND
)
308 // no old, add new if any
312 return; // skipping RebuildAccelTable() below
316 // replace old with new or just remove the old one if no new
321 m_accels
.RemoveAt(n
);
326 GetMenuBar()->RebuildAccelTable();
329 //else: it is a separator, they can't have accels, nothing to do
332 #endif // wxUSE_ACCEL
334 // append a new item or submenu to the menu
335 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
339 #endif // wxUSE_ACCEL
343 // if "Break" has just been called, insert a menu break before this item
344 // (and don't forget to reset the flag)
346 flags
|= MF_MENUBREAK
;
350 if ( pItem
->IsSeparator() ) {
351 flags
|= MF_SEPARATOR
;
354 // id is the numeric id for normal menu items and HMENU for submenus as
355 // required by ::AppendMenu() API
357 wxMenu
*submenu
= pItem
->GetSubMenu();
358 if ( submenu
!= NULL
) {
359 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
361 submenu
->SetParent(this);
363 id
= (UINT
)submenu
->GetHMenu();
372 // prepare to insert the item in the menu
373 wxString itemText
= pItem
->GetText();
374 LPCTSTR pData
= NULL
;
375 if ( pos
== (size_t)-1 )
378 pos
= ::GetMenuItemCount(GetHmenu());
383 // check if we have something more than a simple text item
384 #if wxUSE_OWNER_DRAWN
385 if ( pItem
->IsOwnerDrawn() )
387 // is the item owner-drawn just because of the bitmap?
388 if ( pItem
->GetBitmap().Ok() &&
389 !pItem
->GetTextColour().Ok() &&
390 !pItem
->GetBackgroundColour().Ok() &&
391 !pItem
->GetFont().Ok() )
393 // try to use InsertMenuItem() as it's guaranteed to look correctly
394 // while our owner-drawning code is not
396 // first compile-time check
398 WinStruct
<MENUITEMINFO
> mii
;
400 // now run-time one: MIIM_BITMAP only works under WinME/2000+
401 if ( wxGetWinVersion() >= wxWinVersion_98
)
403 mii
.fMask
= MIIM_ID
| MIIM_STRING
| MIIM_DATA
| MIIM_BITMAP
;
405 mii
.cch
= itemText
.length();
406 mii
.dwTypeData
= wx_const_cast(wxChar
*, itemText
.c_str());
408 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
409 // 1. we can't draw it with transparency then (this is not
410 // very important now but would be with themed menu bg)
411 // 2. worse, Windows inverses the bitmap for the selected
412 // item and this looks downright ugly
414 // so instead draw it ourselves in MSWOnDrawItem()
415 mii
.dwItemData
= wx_reinterpret_cast(ULONG_PTR
, pItem
);
416 mii
.hbmpItem
= HBMMENU_CALLBACK
;
418 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
421 wxLogLastError(wxT("InsertMenuItem()"));
423 else // InsertMenuItem() ok
425 // we need to remove the extra indent which is reserved for
426 // the checkboxes by default as it looks ugly unless check
427 // boxes are used together with bitmaps and this is not the
429 WinStruct
<MENUINFO
> mi
;
431 mi
.fMask
= MIM_STYLE
;
432 mi
.dwStyle
= MNS_CHECKORBMP
;
433 if ( !::SetMenuInfo(GetHmenu(), &mi
) )
434 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
436 // tell the item that it's not really owner-drawn but only
437 // needs to draw its bitmap, the rest is done by Windows
438 pItem
->ResetOwnerDrawn();
441 #endif // MIIM_BITMAP
446 // item draws itself, pass pointer to it in data parameter
447 flags
|= MF_OWNERDRAW
;
448 pData
= (LPCTSTR
)pItem
;
452 #endif // wxUSE_OWNER_DRAWN
454 // menu is just a normal string (passed in data parameter)
458 itemText
= wxMenuItem::GetLabelFromText(itemText
);
461 pData
= (wxChar
*)itemText
.c_str();
464 // item might have been already inserted by InsertMenuItem() above
467 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
469 wxLogLastError(wxT("InsertMenu[Item]()"));
476 // if we just appended the title, highlight it
477 if ( (int)id
== idMenuTitle
)
479 // visually select the menu title
480 SetDefaultMenuItem(GetHmenu(), id
);
483 // if we're already attached to the menubar, we must update it
484 if ( IsAttached() && GetMenuBar()->IsAttached() )
486 GetMenuBar()->Refresh();
492 void wxMenu::EndRadioGroup()
494 // we're not inside a radio group any longer
495 m_startRadioGroup
= -1;
498 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
500 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
504 if ( item
->GetKind() == wxITEM_RADIO
)
506 int count
= GetMenuItemCount();
508 if ( m_startRadioGroup
== -1 )
510 // start a new radio group
511 m_startRadioGroup
= count
;
513 // for now it has just one element
514 item
->SetAsRadioGroupStart();
515 item
->SetRadioGroupEnd(m_startRadioGroup
);
517 // ensure that we have a checked item in the radio group
520 else // extend the current radio group
522 // we need to update its end item
523 item
->SetRadioGroupStart(m_startRadioGroup
);
524 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
528 node
->GetData()->SetRadioGroupEnd(count
);
532 wxFAIL_MSG( _T("where is the radio group start item?") );
536 else // not a radio item
541 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
548 // check the item initially
555 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
557 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
563 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
565 // we need to find the items position in the child list
567 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
568 for ( pos
= 0; node
; pos
++ )
570 if ( node
->GetData() == item
)
573 node
= node
->GetNext();
576 // DoRemove() (unlike Remove) can only be called for existing item!
577 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
580 // remove the corresponding accel from the accel table
581 int n
= FindAccel(item
->GetId());
582 if ( n
!= wxNOT_FOUND
)
586 m_accels
.RemoveAt(n
);
588 //else: this item doesn't have an accel, nothing to do
589 #endif // wxUSE_ACCEL
591 // remove the item from the menu
592 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
594 wxLogLastError(wxT("RemoveMenu"));
597 if ( IsAttached() && GetMenuBar()->IsAttached() )
599 // otherwise, the chane won't be visible
600 GetMenuBar()->Refresh();
603 // and from internal data structures
604 return wxMenuBase::DoRemove(item
);
607 // ---------------------------------------------------------------------------
608 // accelerator helpers
609 // ---------------------------------------------------------------------------
613 // create the wxAcceleratorEntries for our accels and put them into provided
614 // array - return the number of accels we have
615 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
617 size_t count
= GetAccelCount();
618 for ( size_t n
= 0; n
< count
; n
++ )
620 *accels
++ = *m_accels
[n
];
626 #endif // wxUSE_ACCEL
628 // ---------------------------------------------------------------------------
630 // ---------------------------------------------------------------------------
632 void wxMenu::SetTitle(const wxString
& label
)
634 bool hasNoTitle
= m_title
.empty();
637 HMENU hMenu
= GetHmenu();
641 if ( !label
.empty() )
643 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
644 (unsigned)idMenuTitle
, m_title
) ||
645 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
647 wxLogLastError(wxT("InsertMenu"));
655 // remove the title and the separator after it
656 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
657 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
659 wxLogLastError(wxT("RemoveMenu"));
668 info
.cbSize
= sizeof(info
);
669 info
.fMask
= MIIM_TYPE
;
670 info
.fType
= MFT_STRING
;
671 info
.cch
= m_title
.Length();
672 info
.dwTypeData
= (LPTSTR
) m_title
.c_str();
673 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
675 wxLogLastError(wxT("SetMenuItemInfo"));
678 if ( !ModifyMenu(hMenu
, 0u,
679 MF_BYPOSITION
| MF_STRING
,
680 (unsigned)idMenuTitle
, m_title
) )
682 wxLogLastError(wxT("ModifyMenu"));
689 // put the title string in bold face
690 if ( !m_title
.empty() )
692 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
697 // ---------------------------------------------------------------------------
699 // ---------------------------------------------------------------------------
701 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
703 // ignore commands from the menu title
704 if ( id
!= (WXWORD
)idMenuTitle
)
706 // get the checked status of the command: notice that menuState is the
707 // old state of the menu, so the test for MF_CHECKED must be inverted
708 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
709 SendEvent(id
, !(menuState
& MF_CHECKED
));
715 // ---------------------------------------------------------------------------
717 // ---------------------------------------------------------------------------
719 wxWindow
*wxMenu::GetWindow() const
721 if ( m_invokingWindow
!= NULL
)
722 return m_invokingWindow
;
723 else if ( GetMenuBar() != NULL
)
724 return GetMenuBar()->GetFrame();
729 // ---------------------------------------------------------------------------
731 // ---------------------------------------------------------------------------
733 void wxMenuBar::Init()
735 m_eventHandler
= this;
737 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
740 // Not using a combined wxToolBar/wxMenuBar? then use
741 // a commandbar in WinCE .NET just to implement the
743 #if defined(WINCE_WITH_COMMANDBAR)
745 m_adornmentsAdded
= false;
749 wxMenuBar::wxMenuBar()
754 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
759 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
763 m_titles
.Alloc(count
);
765 for ( size_t i
= 0; i
< count
; i
++ )
767 m_menus
.Append(menus
[i
]);
768 m_titles
.Add(titles
[i
]);
770 menus
[i
]->Attach(this);
774 wxMenuBar::~wxMenuBar()
776 // In Windows CE (not .NET), the menubar is always associated
777 // with a toolbar, which destroys the menu implicitly.
778 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
781 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
783 toolMenuBar
->SetMenuBar(NULL
);
786 // we should free Windows resources only if Windows doesn't do it for us
787 // which happens if we're attached to a frame
788 if (m_hMenu
&& !IsAttached())
790 #if defined(WINCE_WITH_COMMANDBAR)
791 ::DestroyWindow((HWND
) m_commandBar
);
792 m_commandBar
= (WXHWND
) NULL
;
794 ::DestroyMenu((HMENU
)m_hMenu
);
796 m_hMenu
= (WXHMENU
)NULL
;
801 // ---------------------------------------------------------------------------
803 // ---------------------------------------------------------------------------
805 void wxMenuBar::Refresh()
807 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
809 #if defined(WINCE_WITHOUT_COMMANDBAR)
812 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
814 #elif defined(WINCE_WITH_COMMANDBAR)
816 DrawMenuBar((HWND
) m_commandBar
);
818 DrawMenuBar(GetHwndOf(GetFrame()));
822 WXHMENU
wxMenuBar::Create()
824 // Note: this totally doesn't work on Smartphone,
825 // since you have to use resources.
826 // We'll have to find another way to add a menu
827 // by changing/adding menu items to an existing menu.
828 #if defined(WINCE_WITHOUT_COMMANDBAR)
835 HWND hCommandBar
= (HWND
) GetToolBar()->GetHWND();
836 HMENU hMenu
= (HMENU
)::SendMessage(hCommandBar
, SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
840 memset(&tbButton
, 0, sizeof(TBBUTTON
));
841 tbButton
.iBitmap
= I_IMAGENONE
;
842 tbButton
.fsState
= TBSTATE_ENABLED
;
843 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
846 for (i
= 0; i
< GetMenuCount(); i
++)
848 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu() ;
849 tbButton
.dwData
= (DWORD
)hPopupMenu
;
850 wxString label
= wxStripMenuCodes(GetLabelTop(i
));
851 tbButton
.iString
= (int) label
.c_str();
855 tbButton
.idCommand
= NewControlId();
856 if (!::SendMessage(hCommandBar
, TB_INSERTBUTTON
, position
, (LPARAM
)&tbButton
))
858 wxLogLastError(wxT("TB_INSERTBUTTON"));
862 m_hMenu
= (WXHMENU
) hMenu
;
868 m_hMenu
= (WXHMENU
)::CreateMenu();
872 wxLogLastError(wxT("CreateMenu"));
876 size_t count
= GetMenuCount(), i
;
877 wxMenuList::iterator it
;
878 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
880 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
881 (UINT
)(*it
)->GetHMenu(),
884 wxLogLastError(wxT("AppendMenu"));
893 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
896 wxASSERT(menu
->GetHMenu());
899 #if defined(__WXWINCE__)
900 int totalMSWItems
= GetMenuCount();
902 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
905 int i
; // For old C++ compatibility
906 for(i
=wxpos
; i
<totalMSWItems
; i
++)
908 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
911 for(i
=0; i
<wxpos
; i
++)
913 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
920 // ---------------------------------------------------------------------------
921 // wxMenuBar functions to work with the top level submenus
922 // ---------------------------------------------------------------------------
924 // NB: we don't support owner drawn top level items for now, if we do these
925 // functions would have to be changed to use wxMenuItem as well
927 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
929 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
930 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
932 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
934 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
939 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
941 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
943 m_titles
[pos
] = label
;
949 //else: have to modify the existing menu
951 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
954 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
955 if ( flagsOld
== 0xFFFFFFFF )
957 wxLogLastError(wxT("GetMenuState"));
962 if ( flagsOld
& MF_POPUP
)
964 // HIBYTE contains the number of items in the submenu in this case
966 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
976 info
.cbSize
= sizeof(info
);
977 info
.fMask
= MIIM_TYPE
;
978 info
.fType
= MFT_STRING
;
979 info
.cch
= label
.Length();
980 info
.dwTypeData
= (LPTSTR
) label
.c_str();
981 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, & info
) )
983 wxLogLastError(wxT("SetMenuItemInfo"));
987 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
988 id
, label
) == (int)0xFFFFFFFF )
990 wxLogLastError(wxT("ModifyMenu"));
997 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
999 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1000 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
1002 return wxMenuItem::GetLabelFromText(m_titles
[pos
]);
1005 // ---------------------------------------------------------------------------
1006 // wxMenuBar construction
1007 // ---------------------------------------------------------------------------
1009 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1011 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1015 m_titles
[pos
] = title
;
1019 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1021 // can't use ModifyMenu() because it deletes the submenu it replaces
1022 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1024 wxLogLastError(wxT("RemoveMenu"));
1027 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1028 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1029 (UINT
)GetHmenuOf(menu
), title
) )
1031 wxLogLastError(wxT("InsertMenu"));
1035 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1037 // need to rebuild accell table
1038 RebuildAccelTable();
1040 #endif // wxUSE_ACCEL
1048 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1050 // Find out which MSW item before which we'll be inserting before
1051 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1052 // If IsAttached() is false this won't be used anyway
1053 int mswpos
= (!IsAttached() || (pos
== m_menus
.GetCount()))
1054 ? -1 // append the menu
1055 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1057 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1060 m_titles
.Insert(title
, pos
);
1064 #if defined(WINCE_WITHOUT_COMMANDAR)
1068 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1069 tbButton
.iBitmap
= I_IMAGENONE
;
1070 tbButton
.fsState
= TBSTATE_ENABLED
;
1071 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1073 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1074 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1075 wxString label
= wxStripMenuCodes(title
);
1076 tbButton
.iString
= (int) label
.c_str();
1078 tbButton
.idCommand
= NewControlId();
1079 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1081 wxLogLastError(wxT("TB_INSERTBUTTON"));
1085 if ( !::InsertMenu(GetHmenu(), mswpos
,
1086 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1087 (UINT
)GetHmenuOf(menu
), title
) )
1089 wxLogLastError(wxT("InsertMenu"));
1093 if ( menu
->HasAccels() )
1095 // need to rebuild accell table
1096 RebuildAccelTable();
1098 #endif // wxUSE_ACCEL
1106 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1108 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1109 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1111 if ( !wxMenuBarBase::Append(menu
, title
) )
1114 m_titles
.Add(title
);
1118 #if defined(WINCE_WITHOUT_COMMANDAR)
1122 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1123 tbButton
.iBitmap
= I_IMAGENONE
;
1124 tbButton
.fsState
= TBSTATE_ENABLED
;
1125 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1127 size_t pos
= GetMenuCount();
1128 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1129 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1130 wxString label
= wxStripMenuCodes(title
);
1131 tbButton
.iString
= (int) label
.c_str();
1133 tbButton
.idCommand
= NewControlId();
1134 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1136 wxLogLastError(wxT("TB_INSERTBUTTON"));
1140 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1141 (UINT
)submenu
, title
) )
1143 wxLogLastError(wxT("AppendMenu"));
1148 if ( menu
->HasAccels() )
1150 // need to rebuild accelerator table
1151 RebuildAccelTable();
1153 #endif // wxUSE_ACCEL
1161 wxMenu
*wxMenuBar::Remove(size_t pos
)
1163 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1169 #if defined(WINCE_WITHOUT_COMMANDAR)
1172 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1174 wxLogLastError(wxT("TB_DELETEBUTTON"));
1178 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1180 wxLogLastError(wxT("RemoveMenu"));
1185 if ( menu
->HasAccels() )
1187 // need to rebuild accell table
1188 RebuildAccelTable();
1190 #endif // wxUSE_ACCEL
1196 m_titles
.RemoveAt(pos
);
1203 void wxMenuBar::RebuildAccelTable()
1205 // merge the accelerators of all menus into one accel table
1206 size_t nAccelCount
= 0;
1207 size_t i
, count
= GetMenuCount();
1208 wxMenuList::iterator it
;
1209 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1211 nAccelCount
+= (*it
)->GetAccelCount();
1216 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1219 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1221 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1224 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1226 delete [] accelEntries
;
1230 #endif // wxUSE_ACCEL
1232 void wxMenuBar::Attach(wxFrame
*frame
)
1234 wxMenuBarBase::Attach(frame
);
1236 #if defined(WINCE_WITH_COMMANDBAR)
1240 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1245 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1247 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1254 RebuildAccelTable();
1255 #endif // wxUSE_ACCEL
1258 #if defined(WINCE_WITH_COMMANDBAR)
1259 bool wxMenuBar::AddAdornments(long style
)
1261 if (m_adornmentsAdded
|| !m_commandBar
)
1264 if (style
& wxCLOSE_BOX
)
1266 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1267 wxLogLastError(wxT("CommandBar_AddAdornments"));
1275 void wxMenuBar::Detach()
1277 wxMenuBarBase::Detach();
1280 #endif // wxUSE_MENUS