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"
40 #include "wx/ownerdrw.h"
43 #include "wx/scopedarray.h"
45 #include "wx/msw/private.h"
46 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
54 #if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__)
58 #include "wx/msw/wince/missing.h"
62 // other standard headers
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 UINT idMenuTitle
= (UINT
)-3;
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
94 // make the given menu item default
95 void SetDefaultMenuItem(HMENU
WXUNUSED_IN_WINCE(hmenu
),
96 UINT
WXUNUSED_IN_WINCE(id
))
101 mii
.cbSize
= sizeof(MENUITEMINFO
);
102 mii
.fMask
= MIIM_STATE
;
103 mii
.fState
= MFS_DEFAULT
;
105 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
107 wxLogLastError(wxT("SetMenuItemInfo"));
113 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
117 info
.cbSize
= sizeof(info
);
118 info
.fMask
= MIIM_STATE
;
119 // MF_BYCOMMAND is zero so test MF_BYPOSITION
120 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
122 wxLogLastError(wxT("GetMenuItemInfo"));
126 #endif // __WXWINCE__
128 bool IsLessThanStdSize(const wxBitmap
& bmp
)
130 // FIXME: these +4 are chosen so that 16*16 bitmaps pass this test with
131 // default SM_CXMENUCHECK value but I have no idea what do we really
133 return bmp
.GetWidth() < ::GetSystemMetrics(SM_CXMENUCHECK
) + 4 &&
134 bmp
.GetHeight() < ::GetSystemMetrics(SM_CYMENUCHECK
) + 4;
137 } // anonymous namespace
139 // ============================================================================
141 // ============================================================================
143 #include "wx/listimpl.cpp"
145 WX_DEFINE_LIST( wxMenuInfoList
)
147 #if wxUSE_EXTENDED_RTTI
149 WX_DEFINE_FLAGS( wxMenuStyle
)
151 wxBEGIN_FLAGS( wxMenuStyle
)
152 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
153 wxEND_FLAGS( wxMenuStyle
)
155 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
157 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
159 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
161 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
164 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
165 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
166 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
167 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
168 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
169 wxEND_PROPERTIES_TABLE()
171 wxBEGIN_HANDLERS_TABLE(wxMenu
)
172 wxEND_HANDLERS_TABLE()
174 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
176 WX_DEFINE_FLAGS( wxMenuBarStyle
)
178 wxBEGIN_FLAGS( wxMenuBarStyle
)
179 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
180 wxEND_FLAGS( wxMenuBarStyle
)
182 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
183 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
188 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
190 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
192 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
193 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
194 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
195 wxEND_PROPERTIES_TABLE()
197 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
198 wxEND_HANDLERS_TABLE()
200 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
202 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
204 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
206 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
209 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
210 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
211 wxEND_PROPERTIES_TABLE()
213 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
214 wxEND_HANDLERS_TABLE()
216 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
219 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
220 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
221 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
224 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
226 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
227 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
228 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
230 wxMenuInfo
* info
= new wxMenuInfo() ;
231 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetMenuLabel(i
) ) ;
232 list
->Append( info
) ;
237 // ---------------------------------------------------------------------------
238 // wxMenu construction, adding and removing menu items
239 // ---------------------------------------------------------------------------
241 // Construct a menu with optional title (then use append)
245 m_startRadioGroup
= -1;
248 m_hMenu
= (WXHMENU
)CreatePopupMenu();
251 wxLogLastError(wxT("CreatePopupMenu"));
254 // if we have a title, insert it in the beginning of the menu
255 if ( !m_title
.empty() )
257 Append(idMenuTitle
, m_title
);
262 // The wxWindow destructor will take care of deleting the submenus.
265 // we should free Windows resources only if Windows doesn't do it for us
266 // which happens if we're attached to a menubar or a submenu of another
268 if ( !IsAttached() && !GetParent() )
270 if ( !::DestroyMenu(GetHmenu()) )
272 wxLogLastError(wxT("DestroyMenu"));
278 WX_CLEAR_ARRAY(m_accels
);
279 #endif // wxUSE_ACCEL
284 // this will take effect during the next call to Append()
288 void wxMenu::Attach(wxMenuBarBase
*menubar
)
290 wxMenuBase::Attach(menubar
);
297 int wxMenu::FindAccel(int id
) const
299 size_t n
, count
= m_accels
.GetCount();
300 for ( n
= 0; n
< count
; n
++ )
302 if ( m_accels
[n
]->m_command
== id
)
309 void wxMenu::UpdateAccel(wxMenuItem
*item
)
311 if ( item
->IsSubMenu() )
313 wxMenu
*submenu
= item
->GetSubMenu();
314 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
317 UpdateAccel(node
->GetData());
319 node
= node
->GetNext();
322 else if ( !item
->IsSeparator() )
324 // recurse upwards: we should only modify m_accels of the top level
325 // menus, not of the submenus as wxMenuBar doesn't look at them
326 // (alternative and arguable cleaner solution would be to recurse
327 // downwards in GetAccelCount() and CopyAccels())
330 GetParent()->UpdateAccel(item
);
334 // find the (new) accel for this item
335 wxAcceleratorEntry
*accel
= wxAcceleratorEntry::Create(item
->GetItemLabel());
337 accel
->m_command
= item
->GetId();
340 int n
= FindAccel(item
->GetId());
341 if ( n
== wxNOT_FOUND
)
343 // no old, add new if any
347 return; // skipping RebuildAccelTable() below
351 // replace old with new or just remove the old one if no new
356 m_accels
.RemoveAt(n
);
361 GetMenuBar()->RebuildAccelTable();
364 //else: it is a separator, they can't have accels, nothing to do
367 #endif // wxUSE_ACCEL
372 // helper of DoInsertOrAppend(): returns the HBITMAP to use in MENUITEMINFO
373 HBITMAP
GetHBitmapForMenu(wxMenuItem
*pItem
, bool checked
= true)
375 // Under versions of Windows older than Vista we can't pass HBITMAP
376 // directly as hbmpItem for 2 reasons:
377 // 1. We can't draw it with transparency then (this is not
378 // very important now but would be with themed menu bg)
379 // 2. Worse, Windows inverts the bitmap for the selected
380 // item and this looks downright ugly
382 // So we prefer to instead draw it ourselves in MSWOnDrawItem().by using
383 // HBMMENU_CALLBACK when inserting it
385 // However under Vista using HBMMENU_CALLBACK causes the entire menu to be
386 // drawn using the classic theme instead of the current one and it does
387 // handle transparency just fine so do use the real bitmap there
389 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
391 wxBitmap bmp
= pItem
->GetBitmap(checked
);
394 // we must use PARGB DIB for the menu bitmaps so ensure that we do
395 wxImage
img(bmp
.ConvertToImage());
396 if ( !img
.HasAlpha() )
399 pItem
->SetBitmap(img
, checked
);
402 return GetHbitmapOf(pItem
->GetBitmap(checked
));
405 #endif // wxUSE_IMAGE
407 return HBMMENU_CALLBACK
;
410 } // anonymous namespace
412 // append a new item or submenu to the menu
413 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
417 #endif // wxUSE_ACCEL
419 // we should support disabling the item even prior to adding it to the menu
420 UINT flags
= pItem
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
;
422 // if "Break" has just been called, insert a menu break before this item
423 // (and don't forget to reset the flag)
425 flags
|= MF_MENUBREAK
;
429 if ( pItem
->IsSeparator() ) {
430 flags
|= MF_SEPARATOR
;
433 // id is the numeric id for normal menu items and HMENU for submenus as
434 // required by ::AppendMenu() API
436 wxMenu
*submenu
= pItem
->GetSubMenu();
437 if ( submenu
!= NULL
) {
438 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
440 submenu
->SetParent(this);
442 id
= (UINT_PTR
)submenu
->GetHMenu();
447 id
= pItem
->GetMSWId();
451 // prepare to insert the item in the menu
452 wxString itemText
= pItem
->GetItemLabel();
453 LPCTSTR pData
= NULL
;
454 if ( pos
== (size_t)-1 )
456 // append at the end (note that the item is already appended to
457 // internal data structures)
458 pos
= GetMenuItemCount() - 1;
461 // adjust position to account for the title, if any
462 if ( !m_title
.empty() )
463 pos
+= 2; // for the title itself and its separator
467 #if wxUSE_OWNER_DRAWN
468 // Under older systems mixing owner-drawn and non-owner-drawn items results
469 // in inconsistent margins, so we force this one to be owner-drawn if any
470 // other items already are. Later we might want to use a boolean in the
471 // wxMenu to avoid search. Also we might make this fix unnecessary by
472 // getting the correct margin using NONCLIENTMETRICS.
473 static const wxWinVersion winver
= wxGetWinVersion();
474 if ( winver
< wxWinVersion_XP
&&
475 !pItem
->IsOwnerDrawn() && !pItem
->IsSeparator() )
477 // Check if any other items are ownerdrawn, and make ownerdrawn if so
478 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
481 if (node
->GetData()->IsOwnerDrawn())
483 pItem
->SetOwnerDrawn(true);
486 node
= node
->GetNext();
489 #endif // wxUSE_OWNER_DRAWN
491 // check if we have something more than a simple text item
492 #if wxUSE_OWNER_DRAWN
493 if ( pItem
->IsOwnerDrawn() )
496 // MIIM_BITMAP only works under WinME/2000+ so we always use owner
497 // drawn item under the previous versions and we also have to use them
498 // in any case if the item has custom colours or font
499 bool mustUseOwnerDrawn
= winver
< wxWinVersion_98
||
500 pItem
->GetTextColour().Ok() ||
501 pItem
->GetBackgroundColour().Ok() ||
502 pItem
->GetFont().Ok();
503 if ( !mustUseOwnerDrawn
)
505 const wxBitmap
& bmpUnchecked
= pItem
->GetBitmap(false),
506 bmpChecked
= pItem
->GetBitmap(true);
507 if ( (bmpUnchecked
.Ok() && !IsLessThanStdSize(bmpUnchecked
)) ||
508 (bmpChecked
.Ok() && !IsLessThanStdSize(bmpChecked
)) )
510 mustUseOwnerDrawn
= true;
514 // use InsertMenuItem() if possible as it's guaranteed to look correct
515 // while our owner-drawn code is not
516 if ( !mustUseOwnerDrawn
)
518 WinStruct
<MENUITEMINFO
> mii
;
519 mii
.fMask
= MIIM_STRING
| MIIM_DATA
;
521 if ( pItem
->GetBitmap().IsOk() )
523 mii
.fMask
|= MIIM_BITMAP
;
524 mii
.hbmpItem
= GetHBitmapForMenu(pItem
);
527 if ( pItem
->IsCheckable() )
529 mii
.fMask
|= MIIM_CHECKMARKS
;
530 mii
.hbmpChecked
= GetHBitmapForMenu(pItem
, true);
531 mii
.hbmpUnchecked
= GetHBitmapForMenu(pItem
, false);
534 mii
.cch
= itemText
.length();
535 mii
.dwTypeData
= const_cast<wxChar
*>(itemText
.wx_str());
537 if ( flags
& MF_POPUP
)
539 mii
.fMask
|= MIIM_SUBMENU
;
540 mii
.hSubMenu
= GetHmenuOf(pItem
->GetSubMenu());
544 mii
.fMask
|= MIIM_ID
;
548 mii
.dwItemData
= reinterpret_cast<ULONG_PTR
>(pItem
);
550 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
553 wxLogLastError(wxT("InsertMenuItem()"));
555 else // InsertMenuItem() ok
557 // we need to remove the extra indent which is reserved for
558 // the checkboxes by default as it looks ugly unless check
559 // boxes are used together with bitmaps and this is not the
561 WinStruct
<MENUINFO
> mi
;
563 // don't call SetMenuInfo() directly, this would prevent
564 // the app from starting up under Windows 95/NT 4
565 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
567 wxDynamicLibrary
dllUser(wxT("user32"));
568 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
569 if ( pfnSetMenuInfo
)
571 mi
.fMask
= MIM_STYLE
;
572 mi
.dwStyle
= MNS_CHECKORBMP
;
573 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
575 wxLogLastError(wxT("SetMenuInfo(MNS_NOCHECK)"));
579 // tell the item that it's not really owner-drawn but only
580 // needs to draw its bitmap, the rest is done by Windows
581 pItem
->ResetOwnerDrawn();
588 // item draws itself, pass pointer to it in data parameter
589 flags
|= MF_OWNERDRAW
;
590 pData
= (LPCTSTR
)pItem
;
594 #endif // wxUSE_OWNER_DRAWN
596 // item is just a normal string (passed in data parameter)
600 itemText
= wxMenuItem::GetLabelText(itemText
);
603 pData
= (wxChar
*)itemText
.wx_str();
606 // item might have already been inserted by InsertMenuItem() above
609 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
611 wxLogLastError(wxT("InsertMenu[Item]()"));
618 // if we just appended the title, highlight it
619 if ( id
== idMenuTitle
)
621 // visually select the menu title
622 SetDefaultMenuItem(GetHmenu(), id
);
625 // if we're already attached to the menubar, we must update it
626 if ( IsAttached() && GetMenuBar()->IsAttached() )
628 GetMenuBar()->Refresh();
634 void wxMenu::EndRadioGroup()
636 // we're not inside a radio group any longer
637 m_startRadioGroup
= -1;
640 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
642 wxCHECK_MSG( item
, NULL
, wxT("NULL item in wxMenu::DoAppend") );
646 if ( item
->GetKind() == wxITEM_RADIO
)
648 int count
= GetMenuItemCount();
650 if ( m_startRadioGroup
== -1 )
652 // start a new radio group
653 m_startRadioGroup
= count
;
655 // for now it has just one element
656 item
->SetAsRadioGroupStart();
657 item
->SetRadioGroupEnd(m_startRadioGroup
);
659 // ensure that we have a checked item in the radio group
662 else // extend the current radio group
664 // we need to update its end item
665 item
->SetRadioGroupStart(m_startRadioGroup
);
666 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
670 node
->GetData()->SetRadioGroupEnd(count
);
674 wxFAIL_MSG( wxT("where is the radio group start item?") );
678 else // not a radio item
683 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
690 // check the item initially
697 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
699 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
705 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
707 // we need to find the item's position in the child list
709 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
710 for ( pos
= 0; node
; pos
++ )
712 if ( node
->GetData() == item
)
715 node
= node
->GetNext();
718 // DoRemove() (unlike Remove) can only be called for an existing item!
719 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
722 // remove the corresponding accel from the accel table
723 int n
= FindAccel(item
->GetId());
724 if ( n
!= wxNOT_FOUND
)
728 m_accels
.RemoveAt(n
);
730 //else: this item doesn't have an accel, nothing to do
731 #endif // wxUSE_ACCEL
733 // remove the item from the menu
734 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
736 wxLogLastError(wxT("RemoveMenu"));
739 if ( IsAttached() && GetMenuBar()->IsAttached() )
741 // otherwise, the change won't be visible
742 GetMenuBar()->Refresh();
745 // and from internal data structures
746 return wxMenuBase::DoRemove(item
);
749 // ---------------------------------------------------------------------------
750 // accelerator helpers
751 // ---------------------------------------------------------------------------
755 // create the wxAcceleratorEntries for our accels and put them into the provided
756 // array - return the number of accels we have
757 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
759 size_t count
= GetAccelCount();
760 for ( size_t n
= 0; n
< count
; n
++ )
762 *accels
++ = *m_accels
[n
];
768 wxAcceleratorTable
*wxMenu::CreateAccelTable() const
770 const size_t count
= m_accels
.size();
771 wxScopedArray
<wxAcceleratorEntry
> accels(new wxAcceleratorEntry
[count
]);
772 CopyAccels(accels
.get());
774 return new wxAcceleratorTable(count
, accels
.get());
777 #endif // wxUSE_ACCEL
779 // ---------------------------------------------------------------------------
781 // ---------------------------------------------------------------------------
783 void wxMenu::SetTitle(const wxString
& label
)
785 bool hasNoTitle
= m_title
.empty();
788 HMENU hMenu
= GetHmenu();
792 if ( !label
.empty() )
794 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
795 idMenuTitle
, m_title
.wx_str()) ||
796 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
798 wxLogLastError(wxT("InsertMenu"));
806 // remove the title and the separator after it
807 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
808 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
810 wxLogLastError(wxT("RemoveMenu"));
819 info
.cbSize
= sizeof(info
);
820 info
.fMask
= MIIM_TYPE
;
821 info
.fType
= MFT_STRING
;
822 info
.cch
= m_title
.length();
823 info
.dwTypeData
= const_cast<wxChar
*>(m_title
.wx_str());
824 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
826 wxLogLastError(wxT("SetMenuItemInfo"));
829 if ( !ModifyMenu(hMenu
, 0u,
830 MF_BYPOSITION
| MF_STRING
,
831 idMenuTitle
, m_title
.wx_str()) )
833 wxLogLastError(wxT("ModifyMenu"));
840 // put the title string in bold face
841 if ( !m_title
.empty() )
843 SetDefaultMenuItem(GetHmenu(), idMenuTitle
);
848 // ---------------------------------------------------------------------------
850 // ---------------------------------------------------------------------------
852 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
854 const int id
= (signed short)id_
;
856 // ignore commands from the menu title
857 if ( id
!= (int)idMenuTitle
)
859 // update the check item when it's clicked
860 wxMenuItem
* const item
= FindItem(id
);
861 if ( item
&& item
->IsCheckable() )
864 // get the status of the menu item: note that it has been just changed
865 // by Toggle() above so here we already get the new state of the item
866 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
867 SendEvent(id
, menuState
& MF_CHECKED
);
873 // ---------------------------------------------------------------------------
875 // ---------------------------------------------------------------------------
877 wxWindow
*wxMenu::GetWindow() const
879 if ( m_invokingWindow
!= NULL
)
880 return m_invokingWindow
;
881 else if ( GetMenuBar() != NULL
)
882 return GetMenuBar()->GetFrame();
887 // ---------------------------------------------------------------------------
889 // ---------------------------------------------------------------------------
891 void wxMenuBar::Init()
893 m_eventHandler
= this;
895 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
898 // Not using a combined wxToolBar/wxMenuBar? then use
899 // a commandbar in WinCE .NET just to implement the
901 #if defined(WINCE_WITH_COMMANDBAR)
903 m_adornmentsAdded
= false;
907 wxMenuBar::wxMenuBar()
912 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
917 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
921 m_titles
.Alloc(count
);
923 for ( size_t i
= 0; i
< count
; i
++ )
925 m_menus
.Append(menus
[i
]);
926 m_titles
.Add(titles
[i
]);
928 menus
[i
]->Attach(this);
932 wxMenuBar::~wxMenuBar()
934 // In Windows CE (not .NET), the menubar is always associated
935 // with a toolbar, which destroys the menu implicitly.
936 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
939 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
941 toolMenuBar
->SetMenuBar(NULL
);
944 // we should free Windows resources only if Windows doesn't do it for us
945 // which happens if we're attached to a frame
946 if (m_hMenu
&& !IsAttached())
948 #if defined(WINCE_WITH_COMMANDBAR)
949 ::DestroyWindow((HWND
) m_commandBar
);
950 m_commandBar
= (WXHWND
) NULL
;
952 ::DestroyMenu((HMENU
)m_hMenu
);
954 m_hMenu
= (WXHMENU
)NULL
;
959 // ---------------------------------------------------------------------------
961 // ---------------------------------------------------------------------------
963 void wxMenuBar::Refresh()
968 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
970 #if defined(WINCE_WITHOUT_COMMANDBAR)
973 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
975 #elif defined(WINCE_WITH_COMMANDBAR)
977 DrawMenuBar((HWND
) m_commandBar
);
979 DrawMenuBar(GetHwndOf(GetFrame()));
983 WXHMENU
wxMenuBar::Create()
985 // Note: this doesn't work at all on Smartphone,
986 // since you have to use resources.
987 // We'll have to find another way to add a menu
988 // by changing/adding menu items to an existing menu.
989 #if defined(WINCE_WITHOUT_COMMANDBAR)
993 wxToolMenuBar
* const bar
= static_cast<wxToolMenuBar
*>(GetToolBar());
997 HWND hCommandBar
= GetHwndOf(bar
);
999 // notify comctl32.dll about the version of the headers we use before using
1000 // any other TB_XXX messages
1001 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
1004 wxZeroMemory(tbButton
);
1005 tbButton
.iBitmap
= I_IMAGENONE
;
1006 tbButton
.fsState
= TBSTATE_ENABLED
;
1007 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
1008 TBSTYLE_NO_DROPDOWN_ARROW
|
1011 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
1013 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
1014 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1015 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
1016 tbButton
.iString
= (int) label
.wx_str();
1018 tbButton
.idCommand
= NewControlId();
1019 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
1021 wxLogLastError(wxT("TB_INSERTBUTTON"));
1025 m_hMenu
= bar
->GetHMenu();
1027 #else // !__WXWINCE__
1031 m_hMenu
= (WXHMENU
)::CreateMenu();
1035 wxLogLastError(wxT("CreateMenu"));
1039 size_t count
= GetMenuCount(), i
;
1040 wxMenuList::iterator it
;
1041 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1043 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
1044 (UINT_PTR
)(*it
)->GetHMenu(),
1045 m_titles
[i
].wx_str()) )
1047 wxLogLastError(wxT("AppendMenu"));
1053 #endif // __WXWINCE__/!__WXWINCE__
1056 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
1059 wxASSERT(menu
->GetHMenu());
1062 #if defined(__WXWINCE__)
1063 int totalMSWItems
= GetMenuCount();
1065 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1068 int i
; // For old C++ compatibility
1069 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1071 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1074 for(i
=0; i
<wxpos
; i
++)
1076 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1083 // ---------------------------------------------------------------------------
1084 // wxMenuBar functions to work with the top level submenus
1085 // ---------------------------------------------------------------------------
1087 // NB: we don't support owner drawn top level items for now, if we do these
1088 // functions would have to be changed to use wxMenuItem as well
1090 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1092 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1093 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1095 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1097 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1102 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1104 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1106 m_titles
[pos
] = label
;
1108 if ( !IsAttached() )
1112 //else: have to modify the existing menu
1114 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1117 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1118 if ( flagsOld
== 0xFFFFFFFF )
1120 wxLogLastError(wxT("GetMenuState"));
1125 if ( flagsOld
& MF_POPUP
)
1127 // HIBYTE contains the number of items in the submenu in this case
1129 id
= (UINT_PTR
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1139 info
.cbSize
= sizeof(info
);
1140 info
.fMask
= MIIM_TYPE
;
1141 info
.fType
= MFT_STRING
;
1142 info
.cch
= label
.length();
1143 info
.dwTypeData
= const_cast<wxChar
*>(label
.wx_str());
1144 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, &info
) )
1146 wxLogLastError(wxT("SetMenuItemInfo"));
1150 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1151 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1153 wxLogLastError(wxT("ModifyMenu"));
1160 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1162 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1163 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1165 return m_titles
[pos
];
1168 // ---------------------------------------------------------------------------
1169 // wxMenuBar construction
1170 // ---------------------------------------------------------------------------
1172 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1174 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1178 m_titles
[pos
] = title
;
1180 #if defined(WINCE_WITHOUT_COMMANDBAR)
1186 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1188 // can't use ModifyMenu() because it deletes the submenu it replaces
1189 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1191 wxLogLastError(wxT("RemoveMenu"));
1194 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1195 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1196 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1198 wxLogLastError(wxT("InsertMenu"));
1202 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1204 // need to rebuild accell table
1205 RebuildAccelTable();
1207 #endif // wxUSE_ACCEL
1216 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1218 // Find out which MSW item before which we'll be inserting before
1219 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1220 // If IsAttached() is false this won't be used anyway
1222 #if defined(WINCE_WITHOUT_COMMANDBAR)
1228 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1229 ? -1 // append the menu
1230 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1232 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1235 m_titles
.Insert(title
, pos
);
1239 #if defined(WINCE_WITHOUT_COMMANDBAR)
1243 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1244 tbButton
.iBitmap
= I_IMAGENONE
;
1245 tbButton
.fsState
= TBSTATE_ENABLED
;
1246 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1248 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1249 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1250 wxString label
= wxStripMenuCodes(title
);
1251 tbButton
.iString
= (int) label
.wx_str();
1253 tbButton
.idCommand
= NewControlId();
1254 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1256 wxLogLastError(wxT("TB_INSERTBUTTON"));
1259 wxUnusedVar(mswpos
);
1261 if ( !::InsertMenu(GetHmenu(), mswpos
,
1262 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1263 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1265 wxLogLastError(wxT("InsertMenu"));
1269 if ( menu
->HasAccels() )
1271 // need to rebuild accell table
1272 RebuildAccelTable();
1274 #endif // wxUSE_ACCEL
1283 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1285 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1286 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1288 if ( !wxMenuBarBase::Append(menu
, title
) )
1291 m_titles
.Add(title
);
1293 #if defined(WINCE_WITHOUT_COMMANDBAR)
1299 #if defined(WINCE_WITHOUT_COMMANDBAR)
1303 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1304 tbButton
.iBitmap
= I_IMAGENONE
;
1305 tbButton
.fsState
= TBSTATE_ENABLED
;
1306 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1308 size_t pos
= GetMenuCount();
1309 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1310 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1311 wxString label
= wxStripMenuCodes(title
);
1312 tbButton
.iString
= (int) label
.wx_str();
1314 tbButton
.idCommand
= NewControlId();
1315 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1317 wxLogLastError(wxT("TB_INSERTBUTTON"));
1321 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1322 (UINT_PTR
)submenu
, title
.wx_str()) )
1324 wxLogLastError(wxT("AppendMenu"));
1329 if ( menu
->HasAccels() )
1331 // need to rebuild accelerator table
1332 RebuildAccelTable();
1334 #endif // wxUSE_ACCEL
1343 wxMenu
*wxMenuBar::Remove(size_t pos
)
1345 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1349 #if defined(WINCE_WITHOUT_COMMANDBAR)
1355 #if defined(WINCE_WITHOUT_COMMANDBAR)
1358 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1360 wxLogLastError(wxT("TB_DELETEBUTTON"));
1364 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1366 wxLogLastError(wxT("RemoveMenu"));
1371 if ( menu
->HasAccels() )
1373 // need to rebuild accell table
1374 RebuildAccelTable();
1376 #endif // wxUSE_ACCEL
1382 m_titles
.RemoveAt(pos
);
1389 void wxMenuBar::RebuildAccelTable()
1391 // merge the accelerators of all menus into one accel table
1392 size_t nAccelCount
= 0;
1393 size_t i
, count
= GetMenuCount();
1394 wxMenuList::iterator it
;
1395 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1397 nAccelCount
+= (*it
)->GetAccelCount();
1402 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1405 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1407 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1410 SetAcceleratorTable(wxAcceleratorTable(nAccelCount
, accelEntries
));
1412 delete [] accelEntries
;
1416 #endif // wxUSE_ACCEL
1418 void wxMenuBar::Attach(wxFrame
*frame
)
1420 wxMenuBarBase::Attach(frame
);
1422 #if defined(WINCE_WITH_COMMANDBAR)
1426 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1431 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1433 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1440 RebuildAccelTable();
1441 #endif // wxUSE_ACCEL
1444 #if defined(WINCE_WITH_COMMANDBAR)
1445 bool wxMenuBar::AddAdornments(long style
)
1447 if (m_adornmentsAdded
|| !m_commandBar
)
1450 if (style
& wxCLOSE_BOX
)
1452 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1454 wxLogLastError(wxT("CommandBar_AddAdornments"));
1465 void wxMenuBar::Detach()
1467 wxMenuBarBase::Detach();
1470 #endif // wxUSE_MENUS