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"));
109 #endif // !__WXWINCE__
112 // make the given menu item owner-drawn
113 void SetOwnerDrawnMenuItem(HMENU
WXUNUSED_IN_WINCE(hmenu
),
114 UINT
WXUNUSED_IN_WINCE(id
),
115 ULONG_PTR
WXUNUSED_IN_WINCE(data
),
116 BOOL
WXUNUSED_IN_WINCE(byPositon
= FALSE
))
121 mii
.cbSize
= sizeof(MENUITEMINFO
);
122 mii
.fMask
= MIIM_FTYPE
| MIIM_DATA
;
123 mii
.fType
= MFT_OWNERDRAW
;
124 mii
.dwItemData
= data
;
126 if ( reinterpret_cast<wxMenuItem
*>(data
)->IsSeparator() )
127 mii
.fType
|= MFT_SEPARATOR
;
129 if ( !::SetMenuItemInfo(hmenu
, id
, byPositon
, &mii
) )
131 wxLogLastError(wxT("SetMenuItemInfo"));
133 #endif // !__WXWINCE__
137 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
141 info
.cbSize
= sizeof(info
);
142 info
.fMask
= MIIM_STATE
;
143 // MF_BYCOMMAND is zero so test MF_BYPOSITION
144 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
146 wxLogLastError(wxT("GetMenuItemInfo"));
150 #endif // __WXWINCE__
152 inline bool IsLessThanStdSize(const wxBitmap
& bmp
)
154 // FIXME: these +4 are chosen so that 16*16 bitmaps pass this test with
155 // default SM_CXMENUCHECK value but I have no idea what do we really
157 return bmp
.GetWidth() < ::GetSystemMetrics(SM_CXMENUCHECK
) + 4 &&
158 bmp
.GetHeight() < ::GetSystemMetrics(SM_CYMENUCHECK
) + 4;
161 } // anonymous namespace
163 // ============================================================================
165 // ============================================================================
167 #include "wx/listimpl.cpp"
169 WX_DEFINE_LIST( wxMenuInfoList
)
171 #if wxUSE_EXTENDED_RTTI
173 WX_DEFINE_FLAGS( wxMenuStyle
)
175 wxBEGIN_FLAGS( wxMenuStyle
)
176 wxFLAGS_MEMBER(wxMENU_TEAROFF
)
177 wxEND_FLAGS( wxMenuStyle
)
179 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu
, wxEvtHandler
,"wx/menu.h")
181 wxCOLLECTION_TYPE_INFO( wxMenuItem
* , wxMenuItemList
) ;
183 template<> void wxCollectionToVariantArray( wxMenuItemList
const &theList
, wxxVariantArray
&value
)
185 wxListCollectionToVariantArray
<wxMenuItemList::compatibility_iterator
>( theList
, value
) ;
188 wxBEGIN_PROPERTIES_TABLE(wxMenu
)
189 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_MENU_SELECTED
, wxCommandEvent
)
190 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
191 wxREADONLY_PROPERTY_FLAGS( MenuStyle
, wxMenuStyle
, long , GetStyle
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
192 wxPROPERTY_COLLECTION( MenuItems
, wxMenuItemList
, wxMenuItem
* , Append
, GetMenuItems
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
193 wxEND_PROPERTIES_TABLE()
195 wxBEGIN_HANDLERS_TABLE(wxMenu
)
196 wxEND_HANDLERS_TABLE()
198 wxDIRECT_CONSTRUCTOR_2( wxMenu
, wxString
, Title
, long , MenuStyle
)
200 WX_DEFINE_FLAGS( wxMenuBarStyle
)
202 wxBEGIN_FLAGS( wxMenuBarStyle
)
203 wxFLAGS_MEMBER(wxMB_DOCKABLE
)
204 wxEND_FLAGS( wxMenuBarStyle
)
206 // the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
207 bool wxMenuBarStreamingCallback( const wxObject
*WXUNUSED(object
), wxWriter
* , wxPersister
* , wxxVariantArray
& )
212 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar
, wxWindow
,"wx/menu.h",wxMenuBarStreamingCallback
)
214 IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo
, wxObject
, "wx/menu.h" )
216 wxBEGIN_PROPERTIES_TABLE(wxMenuInfo
)
217 wxREADONLY_PROPERTY( Menu
, wxMenu
* , GetMenu
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
218 wxREADONLY_PROPERTY( Title
, wxString
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
219 wxEND_PROPERTIES_TABLE()
221 wxBEGIN_HANDLERS_TABLE(wxMenuInfo
)
222 wxEND_HANDLERS_TABLE()
224 wxCONSTRUCTOR_2( wxMenuInfo
, wxMenu
* , Menu
, wxString
, Title
)
226 wxCOLLECTION_TYPE_INFO( wxMenuInfo
* , wxMenuInfoList
) ;
228 template<> void wxCollectionToVariantArray( wxMenuInfoList
const &theList
, wxxVariantArray
&value
)
230 wxListCollectionToVariantArray
<wxMenuInfoList::compatibility_iterator
>( theList
, value
) ;
233 wxBEGIN_PROPERTIES_TABLE(wxMenuBar
)
234 wxPROPERTY_COLLECTION( MenuInfos
, wxMenuInfoList
, wxMenuInfo
* , Append
, GetMenuInfos
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
235 wxEND_PROPERTIES_TABLE()
237 wxBEGIN_HANDLERS_TABLE(wxMenuBar
)
238 wxEND_HANDLERS_TABLE()
240 wxCONSTRUCTOR_DUMMY( wxMenuBar
)
243 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
244 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
245 IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo
, wxObject
)
248 const wxMenuInfoList
& wxMenuBar::GetMenuInfos() const
250 wxMenuInfoList
* list
= const_cast< wxMenuInfoList
* >( &m_menuInfos
) ;
251 WX_CLEAR_LIST( wxMenuInfoList
, *list
) ;
252 for( size_t i
= 0 ; i
< GetMenuCount() ; ++i
)
254 wxMenuInfo
* info
= new wxMenuInfo() ;
255 info
->Create( const_cast<wxMenuBar
*>(this)->GetMenu(i
) , GetMenuLabel(i
) ) ;
256 list
->Append( info
) ;
261 // ---------------------------------------------------------------------------
262 // wxMenu construction, adding and removing menu items
263 // ---------------------------------------------------------------------------
265 // Construct a menu with optional title (then use append)
269 m_startRadioGroup
= -1;
271 #if wxUSE_OWNER_DRAWN
272 m_ownerDrawn
= false;
273 m_maxBitmapWidth
= 0;
274 m_maxAccelWidth
= -1;
275 #endif // wxUSE_OWNER_DRAWN
278 m_hMenu
= (WXHMENU
)CreatePopupMenu();
281 wxLogLastError(wxT("CreatePopupMenu"));
284 // if we have a title, insert it in the beginning of the menu
285 if ( !m_title
.empty() )
287 Append(idMenuTitle
, m_title
);
292 // The wxWindow destructor will take care of deleting the submenus.
295 // we should free Windows resources only if Windows doesn't do it for us
296 // which happens if we're attached to a menubar or a submenu of another
298 if ( !IsAttached() && !GetParent() )
300 if ( !::DestroyMenu(GetHmenu()) )
302 wxLogLastError(wxT("DestroyMenu"));
308 WX_CLEAR_ARRAY(m_accels
);
309 #endif // wxUSE_ACCEL
314 // this will take effect during the next call to Append()
318 void wxMenu::Attach(wxMenuBarBase
*menubar
)
320 wxMenuBase::Attach(menubar
);
327 int wxMenu::FindAccel(int id
) const
329 size_t n
, count
= m_accels
.GetCount();
330 for ( n
= 0; n
< count
; n
++ )
332 if ( m_accels
[n
]->m_command
== id
)
339 void wxMenu::UpdateAccel(wxMenuItem
*item
)
341 if ( item
->IsSubMenu() )
343 wxMenu
*submenu
= item
->GetSubMenu();
344 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
347 UpdateAccel(node
->GetData());
349 node
= node
->GetNext();
352 else if ( !item
->IsSeparator() )
354 // recurse upwards: we should only modify m_accels of the top level
355 // menus, not of the submenus as wxMenuBar doesn't look at them
356 // (alternative and arguable cleaner solution would be to recurse
357 // downwards in GetAccelCount() and CopyAccels())
360 GetParent()->UpdateAccel(item
);
364 // find the (new) accel for this item
365 wxAcceleratorEntry
*accel
= wxAcceleratorEntry::Create(item
->GetItemLabel());
367 accel
->m_command
= item
->GetId();
370 int n
= FindAccel(item
->GetId());
371 if ( n
== wxNOT_FOUND
)
373 // no old, add new if any
377 return; // skipping RebuildAccelTable() below
381 // replace old with new or just remove the old one if no new
386 m_accels
.RemoveAt(n
);
391 GetMenuBar()->RebuildAccelTable();
394 ResetMaxAccelWidth();
396 //else: it is a separator, they can't have accels, nothing to do
399 #endif // wxUSE_ACCEL
404 // helper of DoInsertOrAppend(): returns the HBITMAP to use in MENUITEMINFO
405 HBITMAP
GetHBitmapForMenu(wxMenuItem
*pItem
, bool checked
= true)
407 // Under versions of Windows older than Vista we can't pass HBITMAP
408 // 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 inverts the bitmap for the selected
412 // item and this looks downright ugly
414 // So we prefer to instead draw it ourselves in MSWOnDrawItem().by using
415 // HBMMENU_CALLBACK when inserting it
417 // However under Vista using HBMMENU_CALLBACK causes the entire menu to be
418 // drawn using the classic theme instead of the current one and it does
419 // handle transparency just fine so do use the real bitmap there
421 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
423 wxBitmap bmp
= pItem
->GetBitmap(checked
);
426 // we must use PARGB DIB for the menu bitmaps so ensure that we do
427 wxImage
img(bmp
.ConvertToImage());
428 if ( !img
.HasAlpha() )
431 pItem
->SetBitmap(img
, checked
);
434 return GetHbitmapOf(pItem
->GetBitmap(checked
));
436 //else: bitmap is not set
440 #endif // wxUSE_IMAGE
442 return HBMMENU_CALLBACK
;
445 } // anonymous namespace
447 // append a new item or submenu to the menu
448 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
452 #endif // wxUSE_ACCEL
454 // we should support disabling the item even prior to adding it to the menu
455 UINT flags
= pItem
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
;
457 // if "Break" has just been called, insert a menu break before this item
458 // (and don't forget to reset the flag)
460 flags
|= MF_MENUBREAK
;
464 if ( pItem
->IsSeparator() ) {
465 flags
|= MF_SEPARATOR
;
468 // id is the numeric id for normal menu items and HMENU for submenus as
469 // required by ::AppendMenu() API
471 wxMenu
*submenu
= pItem
->GetSubMenu();
472 if ( submenu
!= NULL
) {
473 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
475 submenu
->SetParent(this);
477 id
= (UINT_PTR
)submenu
->GetHMenu();
482 id
= pItem
->GetMSWId();
486 // prepare to insert the item in the menu
487 wxString itemText
= pItem
->GetItemLabel();
488 LPCTSTR pData
= NULL
;
489 if ( pos
== (size_t)-1 )
491 // append at the end (note that the item is already appended to
492 // internal data structures)
493 pos
= GetMenuItemCount() - 1;
496 // adjust position to account for the title, if any
497 if ( !m_title
.empty() )
498 pos
+= 2; // for the title itself and its separator
502 #if wxUSE_OWNER_DRAWN
503 // Under older systems mixing owner-drawn and non-owner-drawn items results
504 // in inconsistent margins, so we force this one to be owner-drawn if any
505 // other items already are.
507 pItem
->SetOwnerDrawn(true);
508 #endif // wxUSE_OWNER_DRAWN
510 // check if we have something more than a simple text item
511 #if wxUSE_OWNER_DRAWN
512 if ( pItem
->IsOwnerDrawn() )
516 if ( !m_ownerDrawn
&& !pItem
->IsSeparator() )
518 // MIIM_BITMAP only works under WinME/2000+ so we always use owner
519 // drawn item under the previous versions and we also have to use
520 // them in any case if the item has custom colours or font
521 static const wxWinVersion winver
= wxGetWinVersion();
522 bool mustUseOwnerDrawn
= winver
< wxWinVersion_98
||
523 pItem
->GetTextColour().Ok() ||
524 pItem
->GetBackgroundColour().Ok() ||
525 pItem
->GetFont().Ok();
527 if ( !mustUseOwnerDrawn
)
529 const wxBitmap
& bmpUnchecked
= pItem
->GetBitmap(false),
530 bmpChecked
= pItem
->GetBitmap(true);
532 if ( (bmpUnchecked
.Ok() && !IsLessThanStdSize(bmpUnchecked
)) ||
533 (bmpChecked
.Ok() && !IsLessThanStdSize(bmpChecked
)) )
535 mustUseOwnerDrawn
= true;
539 // use InsertMenuItem() if possible as it's guaranteed to look
540 // correct while our owner-drawn code is not
541 if ( !mustUseOwnerDrawn
)
543 WinStruct
<MENUITEMINFO
> mii
;
544 mii
.fMask
= MIIM_STRING
| MIIM_DATA
;
546 // don't set hbmpItem for the checkable items as it would
547 // be used for both checked and unchecked state
548 if ( pItem
->IsCheckable() )
550 mii
.fMask
|= MIIM_CHECKMARKS
;
551 mii
.hbmpChecked
= GetHBitmapForMenu(pItem
, true);
552 mii
.hbmpUnchecked
= GetHBitmapForMenu(pItem
, false);
554 else if ( pItem
->GetBitmap().IsOk() )
556 mii
.fMask
|= MIIM_BITMAP
;
557 mii
.hbmpItem
= GetHBitmapForMenu(pItem
);
560 mii
.cch
= itemText
.length();
561 mii
.dwTypeData
= const_cast<wxChar
*>(itemText
.wx_str());
563 if ( flags
& MF_POPUP
)
565 mii
.fMask
|= MIIM_SUBMENU
;
566 mii
.hSubMenu
= GetHmenuOf(pItem
->GetSubMenu());
570 mii
.fMask
|= MIIM_ID
;
574 mii
.dwItemData
= reinterpret_cast<ULONG_PTR
>(pItem
);
576 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
579 wxLogLastError(wxT("InsertMenuItem()"));
581 else // InsertMenuItem() ok
583 // we need to remove the extra indent which is reserved for
584 // the checkboxes by default as it looks ugly unless check
585 // boxes are used together with bitmaps and this is not the
587 WinStruct
<MENUINFO
> mi
;
589 // don't call SetMenuInfo() directly, this would prevent
590 // the app from starting up under Windows 95/NT 4
591 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
593 wxDynamicLibrary
dllUser(wxT("user32"));
594 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
595 if ( pfnSetMenuInfo
)
597 mi
.fMask
= MIM_STYLE
;
598 mi
.dwStyle
= MNS_CHECKORBMP
;
599 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
601 wxLogLastError(wxT("SetMenuInfo(MNS_NOCHECK)"));
605 // tell the item that it's not really owner-drawn but only
606 // needs to draw its bitmap, the rest is done by Windows
607 pItem
->SetOwnerDrawn(false);
615 // item draws itself, pass pointer to it in data parameter
616 flags
|= MF_OWNERDRAW
;
617 pData
= (LPCTSTR
)pItem
;
619 bool updateAllMargins
= false;
621 // get size of bitmap always return valid value (0 for invalid bitmap),
622 // so we don't needed check if bitmap is valid ;)
623 int uncheckedW
= pItem
->GetBitmap(false).GetWidth();
624 int checkedW
= pItem
->GetBitmap(true).GetWidth();
626 if ( m_maxBitmapWidth
< uncheckedW
)
628 m_maxBitmapWidth
= uncheckedW
;
629 updateAllMargins
= true;
632 if ( m_maxBitmapWidth
< checkedW
)
634 m_maxBitmapWidth
= checkedW
;
635 updateAllMargins
= true;
638 // make other item ownerdrawn and update margin width for equals alignment
639 if ( !m_ownerDrawn
|| updateAllMargins
)
641 // we must use position in SetOwnerDrawnMenuItem because
642 // all separators have the same id
644 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
647 wxMenuItem
* item
= node
->GetData();
649 if ( !item
->IsOwnerDrawn())
651 item
->SetOwnerDrawn(true);
652 SetOwnerDrawnMenuItem(GetHmenu(), pos
,
653 reinterpret_cast<ULONG_PTR
>(item
), TRUE
);
656 item
->SetMarginWidth(m_maxBitmapWidth
);
658 node
= node
->GetNext();
662 // set menu as ownerdrawn
665 ResetMaxAccelWidth();
667 // only update our margin for equals alignment to other item
668 else if ( !updateAllMargins
)
670 pItem
->SetMarginWidth(m_maxBitmapWidth
);
675 #endif // wxUSE_OWNER_DRAWN
677 // item is just a normal string (passed in data parameter)
681 itemText
= wxMenuItem::GetLabelText(itemText
);
684 pData
= (wxChar
*)itemText
.wx_str();
687 // item might have already been inserted by InsertMenuItem() above
690 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
692 wxLogLastError(wxT("InsertMenu[Item]()"));
699 // if we just appended the title, highlight it
700 if ( id
== idMenuTitle
)
702 // visually select the menu title
703 SetDefaultMenuItem(GetHmenu(), id
);
706 // if we're already attached to the menubar, we must update it
707 if ( IsAttached() && GetMenuBar()->IsAttached() )
709 GetMenuBar()->Refresh();
715 void wxMenu::EndRadioGroup()
717 // we're not inside a radio group any longer
718 m_startRadioGroup
= -1;
721 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
723 wxCHECK_MSG( item
, NULL
, wxT("NULL item in wxMenu::DoAppend") );
727 if ( item
->GetKind() == wxITEM_RADIO
)
729 int count
= GetMenuItemCount();
731 if ( m_startRadioGroup
== -1 )
733 // start a new radio group
734 m_startRadioGroup
= count
;
736 // for now it has just one element
737 item
->SetAsRadioGroupStart();
738 item
->SetRadioGroupEnd(m_startRadioGroup
);
740 // ensure that we have a checked item in the radio group
743 else // extend the current radio group
745 // we need to update its end item
746 item
->SetRadioGroupStart(m_startRadioGroup
);
747 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
751 node
->GetData()->SetRadioGroupEnd(count
);
755 wxFAIL_MSG( wxT("where is the radio group start item?") );
759 else // not a radio item
764 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
771 // check the item initially
778 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
780 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
786 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
788 // we need to find the item's position in the child list
790 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
791 for ( pos
= 0; node
; pos
++ )
793 if ( node
->GetData() == item
)
796 node
= node
->GetNext();
799 // DoRemove() (unlike Remove) can only be called for an existing item!
800 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
803 // remove the corresponding accel from the accel table
804 int n
= FindAccel(item
->GetId());
805 if ( n
!= wxNOT_FOUND
)
809 m_accels
.RemoveAt(n
);
811 ResetMaxAccelWidth();
813 //else: this item doesn't have an accel, nothing to do
814 #endif // wxUSE_ACCEL
816 // remove the item from the menu
817 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
819 wxLogLastError(wxT("RemoveMenu"));
822 if ( IsAttached() && GetMenuBar()->IsAttached() )
824 // otherwise, the change won't be visible
825 GetMenuBar()->Refresh();
828 // and from internal data structures
829 return wxMenuBase::DoRemove(item
);
832 // ---------------------------------------------------------------------------
833 // accelerator helpers
834 // ---------------------------------------------------------------------------
838 // create the wxAcceleratorEntries for our accels and put them into the provided
839 // array - return the number of accels we have
840 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
842 size_t count
= GetAccelCount();
843 for ( size_t n
= 0; n
< count
; n
++ )
845 *accels
++ = *m_accels
[n
];
851 wxAcceleratorTable
*wxMenu::CreateAccelTable() const
853 const size_t count
= m_accels
.size();
854 wxScopedArray
<wxAcceleratorEntry
> accels(new wxAcceleratorEntry
[count
]);
855 CopyAccels(accels
.get());
857 return new wxAcceleratorTable(count
, accels
.get());
860 #endif // wxUSE_ACCEL
862 // ---------------------------------------------------------------------------
863 // ownerdrawn helpers
864 // ---------------------------------------------------------------------------
866 #if wxUSE_OWNER_DRAWN
868 void wxMenu::CalculateMaxAccelWidth()
870 wxASSERT_MSG( m_maxAccelWidth
== -1, wxT("it's really needed?") );
872 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
875 wxMenuItem
* item
= node
->GetData();
877 if ( item
->IsOwnerDrawn() )
879 int width
= item
->MeasureAccelWidth();
880 if (width
> m_maxAccelWidth
)
881 m_maxAccelWidth
= width
;
884 node
= node
->GetNext();
888 #endif // wxUSE_OWNER_DRAWN
890 // ---------------------------------------------------------------------------
892 // ---------------------------------------------------------------------------
894 void wxMenu::SetTitle(const wxString
& label
)
896 bool hasNoTitle
= m_title
.empty();
899 HMENU hMenu
= GetHmenu();
903 if ( !label
.empty() )
905 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
906 idMenuTitle
, m_title
.wx_str()) ||
907 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
909 wxLogLastError(wxT("InsertMenu"));
917 // remove the title and the separator after it
918 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
919 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
921 wxLogLastError(wxT("RemoveMenu"));
930 info
.cbSize
= sizeof(info
);
931 info
.fMask
= MIIM_TYPE
;
932 info
.fType
= MFT_STRING
;
933 info
.cch
= m_title
.length();
934 info
.dwTypeData
= const_cast<wxChar
*>(m_title
.wx_str());
935 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
937 wxLogLastError(wxT("SetMenuItemInfo"));
940 if ( !ModifyMenu(hMenu
, 0u,
941 MF_BYPOSITION
| MF_STRING
,
942 idMenuTitle
, m_title
.wx_str()) )
944 wxLogLastError(wxT("ModifyMenu"));
951 // put the title string in bold face
952 if ( !m_title
.empty() )
954 SetDefaultMenuItem(GetHmenu(), idMenuTitle
);
959 // ---------------------------------------------------------------------------
961 // ---------------------------------------------------------------------------
963 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
965 const int id
= (signed short)id_
;
967 // ignore commands from the menu title
968 if ( id
!= (int)idMenuTitle
)
970 // update the check item when it's clicked
971 wxMenuItem
* const item
= FindItem(id
);
972 if ( item
&& item
->IsCheckable() )
975 // get the status of the menu item: note that it has been just changed
976 // by Toggle() above so here we already get the new state of the item
977 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
978 SendEvent(id
, menuState
& MF_CHECKED
);
984 // ---------------------------------------------------------------------------
986 // ---------------------------------------------------------------------------
988 wxWindow
*wxMenu::GetWindow() const
990 if ( m_invokingWindow
!= NULL
)
991 return m_invokingWindow
;
992 else if ( GetMenuBar() != NULL
)
993 return GetMenuBar()->GetFrame();
998 // ---------------------------------------------------------------------------
1000 // ---------------------------------------------------------------------------
1002 void wxMenuBar::Init()
1004 m_eventHandler
= this;
1006 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
1009 // Not using a combined wxToolBar/wxMenuBar? then use
1010 // a commandbar in WinCE .NET just to implement the
1012 #if defined(WINCE_WITH_COMMANDBAR)
1013 m_commandBar
= NULL
;
1014 m_adornmentsAdded
= false;
1018 wxMenuBar::wxMenuBar()
1023 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
1028 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
1032 m_titles
.Alloc(count
);
1034 for ( size_t i
= 0; i
< count
; i
++ )
1036 m_menus
.Append(menus
[i
]);
1037 m_titles
.Add(titles
[i
]);
1039 menus
[i
]->Attach(this);
1043 wxMenuBar::~wxMenuBar()
1045 // In Windows CE (not .NET), the menubar is always associated
1046 // with a toolbar, which destroys the menu implicitly.
1047 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
1050 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
1052 toolMenuBar
->SetMenuBar(NULL
);
1055 // we should free Windows resources only if Windows doesn't do it for us
1056 // which happens if we're attached to a frame
1057 if (m_hMenu
&& !IsAttached())
1059 #if defined(WINCE_WITH_COMMANDBAR)
1060 ::DestroyWindow((HWND
) m_commandBar
);
1061 m_commandBar
= (WXHWND
) NULL
;
1063 ::DestroyMenu((HMENU
)m_hMenu
);
1065 m_hMenu
= (WXHMENU
)NULL
;
1070 // ---------------------------------------------------------------------------
1071 // wxMenuBar helpers
1072 // ---------------------------------------------------------------------------
1074 void wxMenuBar::Refresh()
1079 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
1081 #if defined(WINCE_WITHOUT_COMMANDBAR)
1084 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
1086 #elif defined(WINCE_WITH_COMMANDBAR)
1088 DrawMenuBar((HWND
) m_commandBar
);
1090 DrawMenuBar(GetHwndOf(GetFrame()));
1094 WXHMENU
wxMenuBar::Create()
1096 // Note: this doesn't work at all on Smartphone,
1097 // since you have to use resources.
1098 // We'll have to find another way to add a menu
1099 // by changing/adding menu items to an existing menu.
1100 #if defined(WINCE_WITHOUT_COMMANDBAR)
1104 wxToolMenuBar
* const bar
= static_cast<wxToolMenuBar
*>(GetToolBar());
1108 HWND hCommandBar
= GetHwndOf(bar
);
1110 // notify comctl32.dll about the version of the headers we use before using
1111 // any other TB_XXX messages
1112 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
1115 wxZeroMemory(tbButton
);
1116 tbButton
.iBitmap
= I_IMAGENONE
;
1117 tbButton
.fsState
= TBSTATE_ENABLED
;
1118 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
1119 TBSTYLE_NO_DROPDOWN_ARROW
|
1122 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
1124 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
1125 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1126 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
1127 tbButton
.iString
= (int) label
.wx_str();
1129 tbButton
.idCommand
= NewControlId();
1130 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
1132 wxLogLastError(wxT("TB_INSERTBUTTON"));
1136 m_hMenu
= bar
->GetHMenu();
1138 #else // !__WXWINCE__
1142 m_hMenu
= (WXHMENU
)::CreateMenu();
1146 wxLogLastError(wxT("CreateMenu"));
1150 size_t count
= GetMenuCount(), i
;
1151 wxMenuList::iterator it
;
1152 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1154 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
1155 (UINT_PTR
)(*it
)->GetHMenu(),
1156 m_titles
[i
].wx_str()) )
1158 wxLogLastError(wxT("AppendMenu"));
1164 #endif // __WXWINCE__/!__WXWINCE__
1167 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
1170 wxASSERT(menu
->GetHMenu());
1173 #if defined(__WXWINCE__)
1174 int totalMSWItems
= GetMenuCount();
1176 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1179 int i
; // For old C++ compatibility
1180 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1182 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1185 for(i
=0; i
<wxpos
; i
++)
1187 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1194 // ---------------------------------------------------------------------------
1195 // wxMenuBar functions to work with the top level submenus
1196 // ---------------------------------------------------------------------------
1198 // NB: we don't support owner drawn top level items for now, if we do these
1199 // functions would have to be changed to use wxMenuItem as well
1201 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1203 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1204 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1206 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1208 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1213 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1215 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1217 m_titles
[pos
] = label
;
1219 if ( !IsAttached() )
1223 //else: have to modify the existing menu
1225 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1228 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1229 if ( flagsOld
== 0xFFFFFFFF )
1231 wxLogLastError(wxT("GetMenuState"));
1236 if ( flagsOld
& MF_POPUP
)
1238 // HIBYTE contains the number of items in the submenu in this case
1240 id
= (UINT_PTR
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1250 info
.cbSize
= sizeof(info
);
1251 info
.fMask
= MIIM_TYPE
;
1252 info
.fType
= MFT_STRING
;
1253 info
.cch
= label
.length();
1254 info
.dwTypeData
= const_cast<wxChar
*>(label
.wx_str());
1255 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, &info
) )
1257 wxLogLastError(wxT("SetMenuItemInfo"));
1261 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1262 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1264 wxLogLastError(wxT("ModifyMenu"));
1271 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1273 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1274 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1276 return m_titles
[pos
];
1279 // ---------------------------------------------------------------------------
1280 // wxMenuBar construction
1281 // ---------------------------------------------------------------------------
1283 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1285 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1289 m_titles
[pos
] = title
;
1291 #if defined(WINCE_WITHOUT_COMMANDBAR)
1297 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1299 // can't use ModifyMenu() because it deletes the submenu it replaces
1300 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1302 wxLogLastError(wxT("RemoveMenu"));
1305 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1306 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1307 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1309 wxLogLastError(wxT("InsertMenu"));
1313 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1315 // need to rebuild accell table
1316 RebuildAccelTable();
1318 #endif // wxUSE_ACCEL
1327 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1329 // Find out which MSW item before which we'll be inserting before
1330 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1331 // If IsAttached() is false this won't be used anyway
1333 #if defined(WINCE_WITHOUT_COMMANDBAR)
1339 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1340 ? -1 // append the menu
1341 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1343 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1346 m_titles
.Insert(title
, pos
);
1350 #if defined(WINCE_WITHOUT_COMMANDBAR)
1354 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1355 tbButton
.iBitmap
= I_IMAGENONE
;
1356 tbButton
.fsState
= TBSTATE_ENABLED
;
1357 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1359 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1360 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1361 wxString label
= wxStripMenuCodes(title
);
1362 tbButton
.iString
= (int) label
.wx_str();
1364 tbButton
.idCommand
= NewControlId();
1365 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1367 wxLogLastError(wxT("TB_INSERTBUTTON"));
1370 wxUnusedVar(mswpos
);
1372 if ( !::InsertMenu(GetHmenu(), mswpos
,
1373 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1374 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1376 wxLogLastError(wxT("InsertMenu"));
1380 if ( menu
->HasAccels() )
1382 // need to rebuild accell table
1383 RebuildAccelTable();
1385 #endif // wxUSE_ACCEL
1394 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1396 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1397 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1399 if ( !wxMenuBarBase::Append(menu
, title
) )
1402 m_titles
.Add(title
);
1404 #if defined(WINCE_WITHOUT_COMMANDBAR)
1410 #if defined(WINCE_WITHOUT_COMMANDBAR)
1414 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1415 tbButton
.iBitmap
= I_IMAGENONE
;
1416 tbButton
.fsState
= TBSTATE_ENABLED
;
1417 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1419 size_t pos
= GetMenuCount();
1420 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1421 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1422 wxString label
= wxStripMenuCodes(title
);
1423 tbButton
.iString
= (int) label
.wx_str();
1425 tbButton
.idCommand
= NewControlId();
1426 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1428 wxLogLastError(wxT("TB_INSERTBUTTON"));
1432 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1433 (UINT_PTR
)submenu
, title
.wx_str()) )
1435 wxLogLastError(wxT("AppendMenu"));
1440 if ( menu
->HasAccels() )
1442 // need to rebuild accelerator table
1443 RebuildAccelTable();
1445 #endif // wxUSE_ACCEL
1454 wxMenu
*wxMenuBar::Remove(size_t pos
)
1456 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1460 #if defined(WINCE_WITHOUT_COMMANDBAR)
1466 #if defined(WINCE_WITHOUT_COMMANDBAR)
1469 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1471 wxLogLastError(wxT("TB_DELETEBUTTON"));
1475 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1477 wxLogLastError(wxT("RemoveMenu"));
1482 if ( menu
->HasAccels() )
1484 // need to rebuild accell table
1485 RebuildAccelTable();
1487 #endif // wxUSE_ACCEL
1493 m_titles
.RemoveAt(pos
);
1500 void wxMenuBar::RebuildAccelTable()
1502 // merge the accelerators of all menus into one accel table
1503 size_t nAccelCount
= 0;
1504 size_t i
, count
= GetMenuCount();
1505 wxMenuList::iterator it
;
1506 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1508 nAccelCount
+= (*it
)->GetAccelCount();
1513 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1516 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1518 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1521 SetAcceleratorTable(wxAcceleratorTable(nAccelCount
, accelEntries
));
1523 delete [] accelEntries
;
1527 #endif // wxUSE_ACCEL
1529 void wxMenuBar::Attach(wxFrame
*frame
)
1531 wxMenuBarBase::Attach(frame
);
1533 #if defined(WINCE_WITH_COMMANDBAR)
1537 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1542 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1544 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1551 RebuildAccelTable();
1552 #endif // wxUSE_ACCEL
1555 #if defined(WINCE_WITH_COMMANDBAR)
1556 bool wxMenuBar::AddAdornments(long style
)
1558 if (m_adornmentsAdded
|| !m_commandBar
)
1561 if (style
& wxCLOSE_BOX
)
1563 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1565 wxLogLastError(wxT("CommandBar_AddAdornments"));
1576 void wxMenuBar::Detach()
1578 wxMenuBarBase::Detach();
1581 #endif // wxUSE_MENUS