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() &&
412 !pItem
->GetBitmap(true).Ok() )
414 // try to use InsertMenuItem() as it's guaranteed to look correctly
415 // while our owner-drawning code is not
417 // first compile-time check
419 WinStruct
<MENUITEMINFO
> mii
;
421 // now run-time one: MIIM_BITMAP only works under WinME/2000+
422 if ( wxGetWinVersion() >= wxWinVersion_98
)
424 mii
.fMask
= MIIM_STRING
| MIIM_DATA
| MIIM_BITMAP
;
425 mii
.cch
= itemText
.length();
426 mii
.dwTypeData
= wx_const_cast(wxChar
*, itemText
.c_str());
428 if (flags
& MF_POPUP
)
430 mii
.fMask
|= MIIM_SUBMENU
;
431 mii
.hSubMenu
= (HMENU
)pItem
->GetSubMenu()->GetHMenu();
435 mii
.fMask
|= MIIM_ID
;
439 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
440 // 1. we can't draw it with transparency then (this is not
441 // very important now but would be with themed menu bg)
442 // 2. worse, Windows inverses the bitmap for the selected
443 // item and this looks downright ugly
445 // so instead draw it ourselves in MSWOnDrawItem()
446 mii
.dwItemData
= wx_reinterpret_cast(ULONG_PTR
, pItem
);
447 mii
.hbmpItem
= HBMMENU_CALLBACK
;
449 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
452 wxLogLastError(wxT("InsertMenuItem()"));
454 else // InsertMenuItem() ok
456 // we need to remove the extra indent which is reserved for
457 // the checkboxes by default as it looks ugly unless check
458 // boxes are used together with bitmaps and this is not the
460 WinStruct
<MENUINFO
> mi
;
462 // don't call SetMenuInfo() directly, this would prevent
463 // the app from starting up under Windows 95/NT 4
464 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
466 wxDynamicLibrary
dllUser(_T("user32"));
467 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
468 if ( pfnSetMenuInfo
)
470 mi
.fMask
= MIM_STYLE
;
471 mi
.dwStyle
= MNS_CHECKORBMP
;
472 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
473 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
476 // tell the item that it's not really owner-drawn but only
477 // needs to draw its bitmap, the rest is done by Windows
478 pItem
->ResetOwnerDrawn();
481 #endif // MIIM_BITMAP
486 // item draws itself, pass pointer to it in data parameter
487 flags
|= MF_OWNERDRAW
;
488 pData
= (LPCTSTR
)pItem
;
492 #endif // wxUSE_OWNER_DRAWN
494 // menu is just a normal string (passed in data parameter)
498 itemText
= wxMenuItem::GetLabelFromText(itemText
);
501 pData
= (wxChar
*)itemText
.c_str();
504 // item might have been already inserted by InsertMenuItem() above
507 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
509 wxLogLastError(wxT("InsertMenu[Item]()"));
516 // if we just appended the title, highlight it
517 if ( (int)id
== idMenuTitle
)
519 // visually select the menu title
520 SetDefaultMenuItem(GetHmenu(), id
);
523 // if we're already attached to the menubar, we must update it
524 if ( IsAttached() && GetMenuBar()->IsAttached() )
526 GetMenuBar()->Refresh();
532 void wxMenu::EndRadioGroup()
534 // we're not inside a radio group any longer
535 m_startRadioGroup
= -1;
538 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
540 wxCHECK_MSG( item
, NULL
, _T("NULL item in wxMenu::DoAppend") );
544 if ( item
->GetKind() == wxITEM_RADIO
)
546 int count
= GetMenuItemCount();
548 if ( m_startRadioGroup
== -1 )
550 // start a new radio group
551 m_startRadioGroup
= count
;
553 // for now it has just one element
554 item
->SetAsRadioGroupStart();
555 item
->SetRadioGroupEnd(m_startRadioGroup
);
557 // ensure that we have a checked item in the radio group
560 else // extend the current radio group
562 // we need to update its end item
563 item
->SetRadioGroupStart(m_startRadioGroup
);
564 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
568 node
->GetData()->SetRadioGroupEnd(count
);
572 wxFAIL_MSG( _T("where is the radio group start item?") );
576 else // not a radio item
581 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
588 // check the item initially
595 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
597 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
603 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
605 // we need to find the items position in the child list
607 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
608 for ( pos
= 0; node
; pos
++ )
610 if ( node
->GetData() == item
)
613 node
= node
->GetNext();
616 // DoRemove() (unlike Remove) can only be called for existing item!
617 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
620 // remove the corresponding accel from the accel table
621 int n
= FindAccel(item
->GetId());
622 if ( n
!= wxNOT_FOUND
)
626 m_accels
.RemoveAt(n
);
628 //else: this item doesn't have an accel, nothing to do
629 #endif // wxUSE_ACCEL
631 // remove the item from the menu
632 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
634 wxLogLastError(wxT("RemoveMenu"));
637 if ( IsAttached() && GetMenuBar()->IsAttached() )
639 // otherwise, the chane won't be visible
640 GetMenuBar()->Refresh();
643 // and from internal data structures
644 return wxMenuBase::DoRemove(item
);
647 // ---------------------------------------------------------------------------
648 // accelerator helpers
649 // ---------------------------------------------------------------------------
653 // create the wxAcceleratorEntries for our accels and put them into provided
654 // array - return the number of accels we have
655 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
657 size_t count
= GetAccelCount();
658 for ( size_t n
= 0; n
< count
; n
++ )
660 *accels
++ = *m_accels
[n
];
666 #endif // wxUSE_ACCEL
668 // ---------------------------------------------------------------------------
670 // ---------------------------------------------------------------------------
672 void wxMenu::SetTitle(const wxString
& label
)
674 bool hasNoTitle
= m_title
.empty();
677 HMENU hMenu
= GetHmenu();
681 if ( !label
.empty() )
683 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
684 (unsigned)idMenuTitle
, m_title
) ||
685 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
687 wxLogLastError(wxT("InsertMenu"));
695 // remove the title and the separator after it
696 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
697 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
699 wxLogLastError(wxT("RemoveMenu"));
708 info
.cbSize
= sizeof(info
);
709 info
.fMask
= MIIM_TYPE
;
710 info
.fType
= MFT_STRING
;
711 info
.cch
= m_title
.length();
712 info
.dwTypeData
= (LPTSTR
) m_title
.c_str();
713 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
715 wxLogLastError(wxT("SetMenuItemInfo"));
718 if ( !ModifyMenu(hMenu
, 0u,
719 MF_BYPOSITION
| MF_STRING
,
720 (unsigned)idMenuTitle
, m_title
) )
722 wxLogLastError(wxT("ModifyMenu"));
729 // put the title string in bold face
730 if ( !m_title
.empty() )
732 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
737 // ---------------------------------------------------------------------------
739 // ---------------------------------------------------------------------------
741 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
743 // ignore commands from the menu title
744 if ( id
!= (WXWORD
)idMenuTitle
)
746 // get the checked status of the command: notice that menuState is the
747 // old state of the menu, so the test for MF_CHECKED must be inverted
748 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
749 SendEvent(id
, !(menuState
& MF_CHECKED
));
755 // ---------------------------------------------------------------------------
757 // ---------------------------------------------------------------------------
759 wxWindow
*wxMenu::GetWindow() const
761 if ( m_invokingWindow
!= NULL
)
762 return m_invokingWindow
;
763 else if ( GetMenuBar() != NULL
)
764 return GetMenuBar()->GetFrame();
769 // ---------------------------------------------------------------------------
771 // ---------------------------------------------------------------------------
773 void wxMenuBar::Init()
775 m_eventHandler
= this;
777 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
780 // Not using a combined wxToolBar/wxMenuBar? then use
781 // a commandbar in WinCE .NET just to implement the
783 #if defined(WINCE_WITH_COMMANDBAR)
785 m_adornmentsAdded
= false;
789 wxMenuBar::wxMenuBar()
794 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
799 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
803 m_titles
.Alloc(count
);
805 for ( size_t i
= 0; i
< count
; i
++ )
807 m_menus
.Append(menus
[i
]);
808 m_titles
.Add(titles
[i
]);
810 menus
[i
]->Attach(this);
814 wxMenuBar::~wxMenuBar()
816 // In Windows CE (not .NET), the menubar is always associated
817 // with a toolbar, which destroys the menu implicitly.
818 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
821 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
823 toolMenuBar
->SetMenuBar(NULL
);
826 // we should free Windows resources only if Windows doesn't do it for us
827 // which happens if we're attached to a frame
828 if (m_hMenu
&& !IsAttached())
830 #if defined(WINCE_WITH_COMMANDBAR)
831 ::DestroyWindow((HWND
) m_commandBar
);
832 m_commandBar
= (WXHWND
) NULL
;
834 ::DestroyMenu((HMENU
)m_hMenu
);
836 m_hMenu
= (WXHMENU
)NULL
;
841 // ---------------------------------------------------------------------------
843 // ---------------------------------------------------------------------------
845 void wxMenuBar::Refresh()
847 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
849 #if defined(WINCE_WITHOUT_COMMANDBAR)
852 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
854 #elif defined(WINCE_WITH_COMMANDBAR)
856 DrawMenuBar((HWND
) m_commandBar
);
858 DrawMenuBar(GetHwndOf(GetFrame()));
862 WXHMENU
wxMenuBar::Create()
864 // Note: this totally doesn't work on Smartphone,
865 // since you have to use resources.
866 // We'll have to find another way to add a menu
867 // by changing/adding menu items to an existing menu.
868 #if defined(WINCE_WITHOUT_COMMANDBAR)
875 HWND hCommandBar
= (HWND
) GetToolBar()->GetHWND();
876 HMENU hMenu
= (HMENU
)::SendMessage(hCommandBar
, SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
880 memset(&tbButton
, 0, sizeof(TBBUTTON
));
881 tbButton
.iBitmap
= I_IMAGENONE
;
882 tbButton
.fsState
= TBSTATE_ENABLED
;
883 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
886 for (i
= 0; i
< GetMenuCount(); i
++)
888 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu() ;
889 tbButton
.dwData
= (DWORD
)hPopupMenu
;
890 wxString label
= wxStripMenuCodes(GetLabelTop(i
));
891 tbButton
.iString
= (int) label
.c_str();
895 tbButton
.idCommand
= NewControlId();
896 if (!::SendMessage(hCommandBar
, TB_INSERTBUTTON
, position
, (LPARAM
)&tbButton
))
898 wxLogLastError(wxT("TB_INSERTBUTTON"));
902 m_hMenu
= (WXHMENU
) hMenu
;
908 m_hMenu
= (WXHMENU
)::CreateMenu();
912 wxLogLastError(wxT("CreateMenu"));
916 size_t count
= GetMenuCount(), i
;
917 wxMenuList::iterator it
;
918 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
920 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
921 (UINT
)(*it
)->GetHMenu(),
924 wxLogLastError(wxT("AppendMenu"));
933 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
936 wxASSERT(menu
->GetHMenu());
939 #if defined(__WXWINCE__)
940 int totalMSWItems
= GetMenuCount();
942 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
945 int i
; // For old C++ compatibility
946 for(i
=wxpos
; i
<totalMSWItems
; i
++)
948 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
951 for(i
=0; i
<wxpos
; i
++)
953 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
960 // ---------------------------------------------------------------------------
961 // wxMenuBar functions to work with the top level submenus
962 // ---------------------------------------------------------------------------
964 // NB: we don't support owner drawn top level items for now, if we do these
965 // functions would have to be changed to use wxMenuItem as well
967 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
969 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
970 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
972 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
974 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
979 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
981 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
983 m_titles
[pos
] = label
;
989 //else: have to modify the existing menu
991 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
994 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
995 if ( flagsOld
== 0xFFFFFFFF )
997 wxLogLastError(wxT("GetMenuState"));
1002 if ( flagsOld
& MF_POPUP
)
1004 // HIBYTE contains the number of items in the submenu in this case
1006 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1016 info
.cbSize
= sizeof(info
);
1017 info
.fMask
= MIIM_TYPE
;
1018 info
.fType
= MFT_STRING
;
1019 info
.cch
= label
.length();
1020 info
.dwTypeData
= (LPTSTR
) label
.c_str();
1021 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, & info
) )
1023 wxLogLastError(wxT("SetMenuItemInfo"));
1027 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1028 id
, label
) == (int)0xFFFFFFFF )
1030 wxLogLastError(wxT("ModifyMenu"));
1037 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
1039 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1040 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
1042 return wxMenuItem::GetLabelFromText(m_titles
[pos
]);
1045 // ---------------------------------------------------------------------------
1046 // wxMenuBar construction
1047 // ---------------------------------------------------------------------------
1049 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1051 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1055 m_titles
[pos
] = title
;
1057 #if defined(WINCE_WITHOUT_COMMANDBAR)
1063 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1065 // can't use ModifyMenu() because it deletes the submenu it replaces
1066 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1068 wxLogLastError(wxT("RemoveMenu"));
1071 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1072 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1073 (UINT
)GetHmenuOf(menu
), title
) )
1075 wxLogLastError(wxT("InsertMenu"));
1079 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1081 // need to rebuild accell table
1082 RebuildAccelTable();
1084 #endif // wxUSE_ACCEL
1093 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1095 // Find out which MSW item before which we'll be inserting before
1096 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1097 // If IsAttached() is false this won't be used anyway
1099 #if defined(WINCE_WITHOUT_COMMANDBAR)
1105 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1106 ? -1 // append the menu
1107 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1109 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1112 m_titles
.Insert(title
, pos
);
1116 #if defined(WINCE_WITHOUT_COMMANDBAR)
1120 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1121 tbButton
.iBitmap
= I_IMAGENONE
;
1122 tbButton
.fsState
= TBSTATE_ENABLED
;
1123 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1125 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1126 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1127 wxString label
= wxStripMenuCodes(title
);
1128 tbButton
.iString
= (int) label
.c_str();
1130 tbButton
.idCommand
= NewControlId();
1131 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1133 wxLogLastError(wxT("TB_INSERTBUTTON"));
1137 if ( !::InsertMenu(GetHmenu(), mswpos
,
1138 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1139 (UINT
)GetHmenuOf(menu
), title
) )
1141 wxLogLastError(wxT("InsertMenu"));
1145 if ( menu
->HasAccels() )
1147 // need to rebuild accell table
1148 RebuildAccelTable();
1150 #endif // wxUSE_ACCEL
1159 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1161 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1162 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1164 if ( !wxMenuBarBase::Append(menu
, title
) )
1167 m_titles
.Add(title
);
1169 #if defined(WINCE_WITHOUT_COMMANDBAR)
1175 #if defined(WINCE_WITHOUT_COMMANDBAR)
1179 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1180 tbButton
.iBitmap
= I_IMAGENONE
;
1181 tbButton
.fsState
= TBSTATE_ENABLED
;
1182 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1184 size_t pos
= GetMenuCount();
1185 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1186 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1187 wxString label
= wxStripMenuCodes(title
);
1188 tbButton
.iString
= (int) label
.c_str();
1190 tbButton
.idCommand
= NewControlId();
1191 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1193 wxLogLastError(wxT("TB_INSERTBUTTON"));
1197 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1198 (UINT
)submenu
, title
) )
1200 wxLogLastError(wxT("AppendMenu"));
1205 if ( menu
->HasAccels() )
1207 // need to rebuild accelerator table
1208 RebuildAccelTable();
1210 #endif // wxUSE_ACCEL
1219 wxMenu
*wxMenuBar::Remove(size_t pos
)
1221 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1225 #if defined(WINCE_WITHOUT_COMMANDBAR)
1231 #if defined(WINCE_WITHOUT_COMMANDBAR)
1234 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1236 wxLogLastError(wxT("TB_DELETEBUTTON"));
1240 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1242 wxLogLastError(wxT("RemoveMenu"));
1247 if ( menu
->HasAccels() )
1249 // need to rebuild accell table
1250 RebuildAccelTable();
1252 #endif // wxUSE_ACCEL
1258 m_titles
.RemoveAt(pos
);
1265 void wxMenuBar::RebuildAccelTable()
1267 // merge the accelerators of all menus into one accel table
1268 size_t nAccelCount
= 0;
1269 size_t i
, count
= GetMenuCount();
1270 wxMenuList::iterator it
;
1271 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1273 nAccelCount
+= (*it
)->GetAccelCount();
1278 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1281 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1283 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1286 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1288 delete [] accelEntries
;
1292 #endif // wxUSE_ACCEL
1294 void wxMenuBar::Attach(wxFrame
*frame
)
1296 wxMenuBarBase::Attach(frame
);
1298 #if defined(WINCE_WITH_COMMANDBAR)
1302 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1307 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1309 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1316 RebuildAccelTable();
1317 #endif // wxUSE_ACCEL
1320 #if defined(WINCE_WITH_COMMANDBAR)
1321 bool wxMenuBar::AddAdornments(long style
)
1323 if (m_adornmentsAdded
|| !m_commandBar
)
1326 if (style
& wxCLOSE_BOX
)
1328 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1329 wxLogLastError(wxT("CommandBar_AddAdornments"));
1337 void wxMenuBar::Detach()
1339 wxMenuBarBase::Detach();
1342 #endif // wxUSE_MENUS