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"
44 #include "wx/vector.h"
46 #include "wx/msw/private.h"
47 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
55 #if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__)
59 #include "wx/msw/wince/missing.h"
63 // other standard headers
67 #include "wx/dynlib.h"
70 #ifndef MNS_CHECKORBMP
71 #define MNS_CHECKORBMP 0x04000000
74 #define MIM_STYLE 0x00000010
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 // the (popup) menu title has this special id
86 static const int idMenuTitle
= wxID_NONE
;
88 // ----------------------------------------------------------------------------
89 // private helper classes and functions
90 // ----------------------------------------------------------------------------
92 // Contains the data about the radio items groups in the given menu.
93 class wxMenuRadioItemsData
96 wxMenuRadioItemsData() { }
98 // Default copy ctor, assignment operator and dtor are all ok.
100 // Find the start and end of the group containing the given position or
101 // return false if it's not inside any range.
102 bool GetGroupRange(int pos
, int *start
, int *end
) const
104 // We use a simple linear search here because there are not that many
105 // items in a menu and hence even fewer radio items ranges anyhow, so
106 // normally there is no need to do anything fancy (like keeping the
107 // array sorted and using binary search).
108 for ( Ranges::const_iterator it
= m_ranges
.begin();
109 it
!= m_ranges
.end();
112 const Range
& r
= *it
;
114 if ( r
.start
<= pos
&& pos
<= r
.end
)
128 // Take into account the new radio item about to be added at the given
131 // Returns true if this item starts a new radio group, false if it extends
133 bool UpdateOnInsert(int pos
)
135 bool inExistingGroup
= false;
137 for ( Ranges::iterator it
= m_ranges
.begin();
138 it
!= m_ranges
.end();
145 // Item is inserted before this range, update its indices.
149 else if ( pos
<= r
.end
+ 1 )
151 // Item is inserted in the middle of this range or immediately
152 // after it in which case it extends this range so make it span
153 // one more item in any case.
156 inExistingGroup
= true;
158 //else: Item is inserted after this range, nothing to do for it.
161 if ( inExistingGroup
)
164 // Make a new range for the group this item will belong to.
168 m_ranges
.push_back(r
);
174 // Contains the inclusive positions of the range start and end.
181 typedef wxVector
<Range
> Ranges
;
188 // make the given menu item default
189 void SetDefaultMenuItem(HMENU
WXUNUSED_IN_WINCE(hmenu
),
190 UINT
WXUNUSED_IN_WINCE(id
))
195 mii
.cbSize
= sizeof(MENUITEMINFO
);
196 mii
.fMask
= MIIM_STATE
;
197 mii
.fState
= MFS_DEFAULT
;
199 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
201 wxLogLastError(wxT("SetMenuItemInfo"));
203 #endif // !__WXWINCE__
206 // make the given menu item owner-drawn
207 void SetOwnerDrawnMenuItem(HMENU
WXUNUSED_IN_WINCE(hmenu
),
208 UINT
WXUNUSED_IN_WINCE(id
),
209 ULONG_PTR
WXUNUSED_IN_WINCE(data
),
210 BOOL
WXUNUSED_IN_WINCE(byPositon
= FALSE
))
215 mii
.cbSize
= sizeof(MENUITEMINFO
);
216 mii
.fMask
= MIIM_FTYPE
| MIIM_DATA
;
217 mii
.fType
= MFT_OWNERDRAW
;
218 mii
.dwItemData
= data
;
220 if ( reinterpret_cast<wxMenuItem
*>(data
)->IsSeparator() )
221 mii
.fType
|= MFT_SEPARATOR
;
223 if ( !::SetMenuItemInfo(hmenu
, id
, byPositon
, &mii
) )
225 wxLogLastError(wxT("SetMenuItemInfo"));
227 #endif // !__WXWINCE__
231 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
235 info
.cbSize
= sizeof(info
);
236 info
.fMask
= MIIM_STATE
;
237 // MF_BYCOMMAND is zero so test MF_BYPOSITION
238 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
240 wxLogLastError(wxT("GetMenuItemInfo"));
244 #endif // __WXWINCE__
246 inline bool IsGreaterThanStdSize(const wxBitmap
& bmp
)
248 return bmp
.GetWidth() > ::GetSystemMetrics(SM_CXMENUCHECK
) ||
249 bmp
.GetHeight() > ::GetSystemMetrics(SM_CYMENUCHECK
);
252 } // anonymous namespace
254 // ============================================================================
256 // ============================================================================
258 // ---------------------------------------------------------------------------
259 // wxMenu construction, adding and removing menu items
260 // ---------------------------------------------------------------------------
262 // Construct a menu with optional title (then use append)
263 void wxMenu::InitNoCreate()
268 #if wxUSE_OWNER_DRAWN
269 m_ownerDrawn
= false;
270 m_maxBitmapWidth
= 0;
271 m_maxAccelWidth
= -1;
272 #endif // wxUSE_OWNER_DRAWN
280 m_hMenu
= (WXHMENU
)CreatePopupMenu();
283 wxLogLastError(wxT("CreatePopupMenu"));
286 // if we have a title, insert it in the beginning of the menu
287 if ( !m_title
.empty() )
289 const wxString title
= m_title
;
290 m_title
.clear(); // so that SetTitle() knows there was no title before
295 wxMenu::wxMenu(WXHMENU hMenu
)
301 // Ensure that our internal idea of how many items we have corresponds to
302 // the real number of items in the menu.
304 // We could also retrieve the real labels of the items here but it doesn't
305 // seem to be worth the trouble.
306 const int numExistingItems
= ::GetMenuItemCount(m_hMenu
);
307 for ( int n
= 0; n
< numExistingItems
; n
++ )
309 wxMenuBase::DoAppend(wxMenuItem::New(this, wxID_SEPARATOR
));
313 // The wxWindow destructor will take care of deleting the submenus.
316 // we should free Windows resources only if Windows doesn't do it for us
317 // which happens if we're attached to a menubar or a submenu of another
319 if ( !IsAttached() && !GetParent() )
321 if ( !::DestroyMenu(GetHmenu()) )
323 wxLogLastError(wxT("DestroyMenu"));
329 WX_CLEAR_ARRAY(m_accels
);
330 #endif // wxUSE_ACCEL
337 // this will take effect during the next call to Append()
343 int wxMenu::FindAccel(int id
) const
345 size_t n
, count
= m_accels
.GetCount();
346 for ( n
= 0; n
< count
; n
++ )
348 if ( m_accels
[n
]->m_command
== id
)
355 void wxMenu::UpdateAccel(wxMenuItem
*item
)
357 if ( item
->IsSubMenu() )
359 wxMenu
*submenu
= item
->GetSubMenu();
360 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
363 UpdateAccel(node
->GetData());
365 node
= node
->GetNext();
368 else if ( !item
->IsSeparator() )
370 // recurse upwards: we should only modify m_accels of the top level
371 // menus, not of the submenus as wxMenuBar doesn't look at them
372 // (alternative and arguable cleaner solution would be to recurse
373 // downwards in GetAccelCount() and CopyAccels())
376 GetParent()->UpdateAccel(item
);
380 // find the (new) accel for this item
381 wxAcceleratorEntry
*accel
= wxAcceleratorEntry::Create(item
->GetItemLabel());
383 accel
->m_command
= item
->GetId();
386 int n
= FindAccel(item
->GetId());
387 if ( n
== wxNOT_FOUND
)
389 // no old, add new if any
393 return; // skipping RebuildAccelTable() below
397 // replace old with new or just remove the old one if no new
402 m_accels
.RemoveAt(n
);
407 GetMenuBar()->RebuildAccelTable();
410 ResetMaxAccelWidth();
412 //else: it is a separator, they can't have accels, nothing to do
415 #endif // wxUSE_ACCEL
420 // helper of DoInsertOrAppend(): returns the HBITMAP to use in MENUITEMINFO
421 HBITMAP
GetHBitmapForMenu(wxMenuItem
*pItem
, bool checked
= true)
423 // Under versions of Windows older than Vista we can't pass HBITMAP
424 // directly as hbmpItem for 2 reasons:
425 // 1. We can't draw it with transparency then (this is not
426 // very important now but would be with themed menu bg)
427 // 2. Worse, Windows inverts the bitmap for the selected
428 // item and this looks downright ugly
430 // So we prefer to instead draw it ourselves in MSWOnDrawItem().by using
431 // HBMMENU_CALLBACK when inserting it
433 // However under Vista using HBMMENU_CALLBACK causes the entire menu to be
434 // drawn using the classic theme instead of the current one and it does
435 // handle transparency just fine so do use the real bitmap there
437 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
439 wxBitmap bmp
= pItem
->GetBitmap(checked
);
442 // we must use PARGB DIB for the menu bitmaps so ensure that we do
443 wxImage
img(bmp
.ConvertToImage());
444 if ( !img
.HasAlpha() )
447 pItem
->SetBitmap(img
, checked
);
450 return GetHbitmapOf(pItem
->GetBitmap(checked
));
452 //else: bitmap is not set
456 #endif // wxUSE_IMAGE
458 return HBMMENU_CALLBACK
;
461 } // anonymous namespace
463 bool wxMenu::MSWGetRadioGroupRange(int pos
, int *start
, int *end
) const
465 return m_radioData
&& m_radioData
->GetGroupRange(pos
, start
, end
);
468 // append a new item or submenu to the menu
469 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
473 #endif // wxUSE_ACCEL
475 // we should support disabling the item even prior to adding it to the menu
476 UINT flags
= pItem
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
;
478 // if "Break" has just been called, insert a menu break before this item
479 // (and don't forget to reset the flag)
481 flags
|= MF_MENUBREAK
;
485 if ( pItem
->IsSeparator() ) {
486 flags
|= MF_SEPARATOR
;
489 // id is the numeric id for normal menu items and HMENU for submenus as
490 // required by ::AppendMenu() API
492 wxMenu
*submenu
= pItem
->GetSubMenu();
493 if ( submenu
!= NULL
) {
494 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
496 submenu
->SetParent(this);
498 id
= (UINT_PTR
)submenu
->GetHMenu();
503 id
= pItem
->GetMSWId();
507 // prepare to insert the item in the menu
508 wxString itemText
= pItem
->GetItemLabel();
509 LPCTSTR pData
= NULL
;
510 if ( pos
== (size_t)-1 )
512 // append at the end (note that the item is already appended to
513 // internal data structures)
514 pos
= GetMenuItemCount() - 1;
517 // Update radio groups data if we're inserting a new radio item.
519 // NB: If we supported inserting non-radio items in the middle of existing
520 // radio groups to break them into two subgroups, we'd need to update
521 // m_radioData in this case too but currently this is not supported.
522 bool checkInitially
= false;
523 if ( pItem
->GetKind() == wxITEM_RADIO
)
526 m_radioData
= new wxMenuRadioItemsData
;
528 if ( m_radioData
->UpdateOnInsert(pos
) )
529 checkInitially
= true;
532 // adjust position to account for the title of a popup menu, if any
533 if ( !GetMenuBar() && !m_title
.empty() )
534 pos
+= 2; // for the title itself and its separator
538 #if wxUSE_OWNER_DRAWN
539 // Under older systems mixing owner-drawn and non-owner-drawn items results
540 // in inconsistent margins, so we force this one to be owner-drawn if any
541 // other items already are.
543 pItem
->SetOwnerDrawn(true);
544 #endif // wxUSE_OWNER_DRAWN
546 // check if we have something more than a simple text item
547 #if wxUSE_OWNER_DRAWN
548 if ( pItem
->IsOwnerDrawn() )
552 if ( !m_ownerDrawn
&& !pItem
->IsSeparator() )
554 // MIIM_BITMAP only works under WinME/2000+ so we always use owner
555 // drawn item under the previous versions and we also have to use
556 // them in any case if the item has custom colours or font
557 static const wxWinVersion winver
= wxGetWinVersion();
558 bool mustUseOwnerDrawn
= winver
< wxWinVersion_98
||
559 pItem
->GetTextColour().IsOk() ||
560 pItem
->GetBackgroundColour().IsOk() ||
561 pItem
->GetFont().IsOk();
563 if ( !mustUseOwnerDrawn
)
565 const wxBitmap
& bmpUnchecked
= pItem
->GetBitmap(false),
566 bmpChecked
= pItem
->GetBitmap(true);
568 if ( (bmpUnchecked
.IsOk() && IsGreaterThanStdSize(bmpUnchecked
)) ||
569 (bmpChecked
.IsOk() && IsGreaterThanStdSize(bmpChecked
)) )
571 mustUseOwnerDrawn
= true;
575 // use InsertMenuItem() if possible as it's guaranteed to look
576 // correct while our owner-drawn code is not
577 if ( !mustUseOwnerDrawn
)
579 WinStruct
<MENUITEMINFO
> mii
;
580 mii
.fMask
= MIIM_STRING
| MIIM_DATA
;
582 // don't set hbmpItem for the checkable items as it would
583 // be used for both checked and unchecked state
584 if ( pItem
->IsCheckable() )
586 mii
.fMask
|= MIIM_CHECKMARKS
;
587 mii
.hbmpChecked
= GetHBitmapForMenu(pItem
, true);
588 mii
.hbmpUnchecked
= GetHBitmapForMenu(pItem
, false);
590 else if ( pItem
->GetBitmap().IsOk() )
592 mii
.fMask
|= MIIM_BITMAP
;
593 mii
.hbmpItem
= GetHBitmapForMenu(pItem
);
596 mii
.cch
= itemText
.length();
597 mii
.dwTypeData
= const_cast<wxChar
*>(itemText
.wx_str());
599 if ( flags
& MF_POPUP
)
601 mii
.fMask
|= MIIM_SUBMENU
;
602 mii
.hSubMenu
= GetHmenuOf(pItem
->GetSubMenu());
606 mii
.fMask
|= MIIM_ID
;
610 mii
.dwItemData
= reinterpret_cast<ULONG_PTR
>(pItem
);
612 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
615 wxLogLastError(wxT("InsertMenuItem()"));
617 else // InsertMenuItem() ok
619 // we need to remove the extra indent which is reserved for
620 // the checkboxes by default as it looks ugly unless check
621 // boxes are used together with bitmaps and this is not the
623 WinStruct
<MENUINFO
> mi
;
625 // don't call SetMenuInfo() directly, this would prevent
626 // the app from starting up under Windows 95/NT 4
627 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
629 wxDynamicLibrary
dllUser(wxT("user32"));
630 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
631 if ( pfnSetMenuInfo
)
633 mi
.fMask
= MIM_STYLE
;
634 mi
.dwStyle
= MNS_CHECKORBMP
;
635 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
637 wxLogLastError(wxT("SetMenuInfo(MNS_NOCHECK)"));
641 // tell the item that it's not really owner-drawn but only
642 // needs to draw its bitmap, the rest is done by Windows
643 pItem
->SetOwnerDrawn(false);
651 // item draws itself, pass pointer to it in data parameter
652 flags
|= MF_OWNERDRAW
;
653 pData
= (LPCTSTR
)pItem
;
655 bool updateAllMargins
= false;
657 // get size of bitmap always return valid value (0 for invalid bitmap),
658 // so we don't needed check if bitmap is valid ;)
659 int uncheckedW
= pItem
->GetBitmap(false).GetWidth();
660 int checkedW
= pItem
->GetBitmap(true).GetWidth();
662 if ( m_maxBitmapWidth
< uncheckedW
)
664 m_maxBitmapWidth
= uncheckedW
;
665 updateAllMargins
= true;
668 if ( m_maxBitmapWidth
< checkedW
)
670 m_maxBitmapWidth
= checkedW
;
671 updateAllMargins
= true;
674 // make other item ownerdrawn and update margin width for equals alignment
675 if ( !m_ownerDrawn
|| updateAllMargins
)
677 // we must use position in SetOwnerDrawnMenuItem because
678 // all separators have the same id
680 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
683 wxMenuItem
* item
= node
->GetData();
685 if ( !item
->IsOwnerDrawn())
687 item
->SetOwnerDrawn(true);
688 SetOwnerDrawnMenuItem(GetHmenu(), pos
,
689 reinterpret_cast<ULONG_PTR
>(item
), TRUE
);
692 item
->SetMarginWidth(m_maxBitmapWidth
);
694 node
= node
->GetNext();
698 // set menu as ownerdrawn
701 ResetMaxAccelWidth();
703 // only update our margin for equals alignment to other item
704 else if ( !updateAllMargins
)
706 pItem
->SetMarginWidth(m_maxBitmapWidth
);
711 #endif // wxUSE_OWNER_DRAWN
713 // item is just a normal string (passed in data parameter)
717 itemText
= wxMenuItem::GetLabelText(itemText
);
720 pData
= (wxChar
*)itemText
.wx_str();
723 // item might have already been inserted by InsertMenuItem() above
726 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
728 wxLogLastError(wxT("InsertMenu[Item]()"));
735 // Check the item if it should be initially checked.
736 if ( checkInitially
)
739 // if we just appended the title, highlight it
740 if ( id
== (UINT_PTR
)idMenuTitle
)
742 // visually select the menu title
743 SetDefaultMenuItem(GetHmenu(), id
);
746 // if we're already attached to the menubar, we must update it
747 if ( IsAttached() && GetMenuBar()->IsAttached() )
749 GetMenuBar()->Refresh();
755 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
757 return wxMenuBase::DoAppend(item
) && DoInsertOrAppend(item
) ? item
: NULL
;
760 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
762 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
768 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
770 // we need to find the item's position in the child list
772 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
773 for ( pos
= 0; node
; pos
++ )
775 if ( node
->GetData() == item
)
778 node
= node
->GetNext();
781 // DoRemove() (unlike Remove) can only be called for an existing item!
782 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
785 // remove the corresponding accel from the accel table
786 int n
= FindAccel(item
->GetId());
787 if ( n
!= wxNOT_FOUND
)
791 m_accels
.RemoveAt(n
);
793 ResetMaxAccelWidth();
795 //else: this item doesn't have an accel, nothing to do
796 #endif // wxUSE_ACCEL
798 // remove the item from the menu
799 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
801 wxLogLastError(wxT("RemoveMenu"));
804 if ( IsAttached() && GetMenuBar()->IsAttached() )
806 // otherwise, the change won't be visible
807 GetMenuBar()->Refresh();
810 // and from internal data structures
811 return wxMenuBase::DoRemove(item
);
814 // ---------------------------------------------------------------------------
815 // accelerator helpers
816 // ---------------------------------------------------------------------------
820 // create the wxAcceleratorEntries for our accels and put them into the provided
821 // array - return the number of accels we have
822 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
824 size_t count
= GetAccelCount();
825 for ( size_t n
= 0; n
< count
; n
++ )
827 *accels
++ = *m_accels
[n
];
833 wxAcceleratorTable
*wxMenu::CreateAccelTable() const
835 const size_t count
= m_accels
.size();
836 wxScopedArray
<wxAcceleratorEntry
> accels(new wxAcceleratorEntry
[count
]);
837 CopyAccels(accels
.get());
839 return new wxAcceleratorTable(count
, accels
.get());
842 #endif // wxUSE_ACCEL
844 // ---------------------------------------------------------------------------
845 // ownerdrawn helpers
846 // ---------------------------------------------------------------------------
848 #if wxUSE_OWNER_DRAWN
850 void wxMenu::CalculateMaxAccelWidth()
852 wxASSERT_MSG( m_maxAccelWidth
== -1, wxT("it's really needed?") );
854 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
857 wxMenuItem
* item
= node
->GetData();
859 if ( item
->IsOwnerDrawn() )
861 int width
= item
->MeasureAccelWidth();
862 if (width
> m_maxAccelWidth
)
863 m_maxAccelWidth
= width
;
866 node
= node
->GetNext();
870 #endif // wxUSE_OWNER_DRAWN
872 // ---------------------------------------------------------------------------
874 // ---------------------------------------------------------------------------
876 void wxMenu::SetTitle(const wxString
& label
)
878 bool hasNoTitle
= m_title
.empty();
881 HMENU hMenu
= GetHmenu();
885 if ( !label
.empty() )
887 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
888 (UINT_PTR
)idMenuTitle
, m_title
.wx_str()) ||
889 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
891 wxLogLastError(wxT("InsertMenu"));
899 // remove the title and the separator after it
900 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
901 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
903 wxLogLastError(wxT("RemoveMenu"));
912 info
.cbSize
= sizeof(info
);
913 info
.fMask
= MIIM_TYPE
;
914 info
.fType
= MFT_STRING
;
915 info
.cch
= m_title
.length();
916 info
.dwTypeData
= const_cast<wxChar
*>(m_title
.wx_str());
917 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
919 wxLogLastError(wxT("SetMenuItemInfo"));
922 if ( !ModifyMenu(hMenu
, 0u,
923 MF_BYPOSITION
| MF_STRING
,
924 (UINT_PTR
)idMenuTitle
, m_title
.wx_str()) )
926 wxLogLastError(wxT("ModifyMenu"));
933 // put the title string in bold face
934 if ( !m_title
.empty() )
936 SetDefaultMenuItem(GetHmenu(), (UINT_PTR
)idMenuTitle
);
941 // ---------------------------------------------------------------------------
943 // ---------------------------------------------------------------------------
945 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
947 const int id
= (signed short)id_
;
949 // ignore commands from the menu title
950 if ( id
!= idMenuTitle
)
952 // update the check item when it's clicked
953 wxMenuItem
* const item
= FindItem(id
);
954 if ( item
&& item
->IsCheckable() )
957 // get the status of the menu item: note that it has been just changed
958 // by Toggle() above so here we already get the new state of the item
959 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
960 SendEvent(id
, menuState
& MF_CHECKED
);
966 // get the menu with given handle (recursively)
967 wxMenu
* wxMenu::MSWGetMenu(WXHMENU hMenu
)
970 if ( GetHMenu() == hMenu
)
973 // recursively query submenus
974 for ( size_t n
= 0 ; n
< GetMenuItemCount(); ++n
)
976 wxMenuItem
* item
= FindItemByPosition(n
);
977 wxMenu
* submenu
= item
->GetSubMenu();
980 submenu
= submenu
->MSWGetMenu(hMenu
);
990 // ---------------------------------------------------------------------------
992 // ---------------------------------------------------------------------------
994 void wxMenuBar::Init()
996 m_eventHandler
= this;
998 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
1001 // Not using a combined wxToolBar/wxMenuBar? then use
1002 // a commandbar in WinCE .NET just to implement the
1004 #if defined(WINCE_WITH_COMMANDBAR)
1005 m_commandBar
= NULL
;
1006 m_adornmentsAdded
= false;
1010 wxMenuBar::wxMenuBar()
1015 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
1020 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
1024 for ( size_t i
= 0; i
< count
; i
++ )
1026 // We just want to store the menu title in the menu itself, not to
1027 // show it as a dummy item in the menu itself as we do with the popup
1028 // menu titles in overridden wxMenu::SetTitle().
1029 menus
[i
]->wxMenuBase::SetTitle(titles
[i
]);
1030 m_menus
.Append(menus
[i
]);
1032 menus
[i
]->Attach(this);
1036 wxMenuBar::~wxMenuBar()
1038 // In Windows CE (not .NET), the menubar is always associated
1039 // with a toolbar, which destroys the menu implicitly.
1040 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
1043 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
1045 toolMenuBar
->SetMenuBar(NULL
);
1048 // we should free Windows resources only if Windows doesn't do it for us
1049 // which happens if we're attached to a frame
1050 if (m_hMenu
&& !IsAttached())
1052 #if defined(WINCE_WITH_COMMANDBAR)
1053 ::DestroyWindow((HWND
) m_commandBar
);
1054 m_commandBar
= (WXHWND
) NULL
;
1056 ::DestroyMenu((HMENU
)m_hMenu
);
1058 m_hMenu
= (WXHMENU
)NULL
;
1063 // ---------------------------------------------------------------------------
1064 // wxMenuBar helpers
1065 // ---------------------------------------------------------------------------
1067 void wxMenuBar::Refresh()
1072 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
1074 #if defined(WINCE_WITHOUT_COMMANDBAR)
1077 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
1079 #elif defined(WINCE_WITH_COMMANDBAR)
1081 DrawMenuBar((HWND
) m_commandBar
);
1083 DrawMenuBar(GetHwndOf(GetFrame()));
1087 WXHMENU
wxMenuBar::Create()
1089 // Note: this doesn't work at all on Smartphone,
1090 // since you have to use resources.
1091 // We'll have to find another way to add a menu
1092 // by changing/adding menu items to an existing menu.
1093 #if defined(WINCE_WITHOUT_COMMANDBAR)
1097 wxToolMenuBar
* const bar
= static_cast<wxToolMenuBar
*>(GetToolBar());
1101 HWND hCommandBar
= GetHwndOf(bar
);
1103 // notify comctl32.dll about the version of the headers we use before using
1104 // any other TB_XXX messages
1105 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
1108 wxZeroMemory(tbButton
);
1109 tbButton
.iBitmap
= I_IMAGENONE
;
1110 tbButton
.fsState
= TBSTATE_ENABLED
;
1111 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
1112 TBSTYLE_NO_DROPDOWN_ARROW
|
1115 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
1117 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
1118 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1119 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
1120 tbButton
.iString
= (int) label
.wx_str();
1122 tbButton
.idCommand
= NewControlId();
1123 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
1125 wxLogLastError(wxT("TB_INSERTBUTTON"));
1129 m_hMenu
= bar
->GetHMenu();
1131 #else // !__WXWINCE__
1135 m_hMenu
= (WXHMENU
)::CreateMenu();
1139 wxLogLastError(wxT("CreateMenu"));
1143 for ( wxMenuList::iterator it
= m_menus
.begin();
1144 it
!= m_menus
.end();
1147 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
1148 (UINT_PTR
)(*it
)->GetHMenu(),
1149 (*it
)->GetTitle().wx_str()) )
1151 wxLogLastError(wxT("AppendMenu"));
1157 #endif // __WXWINCE__/!__WXWINCE__
1160 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
1163 wxASSERT(menu
->GetHMenu());
1166 #if defined(__WXWINCE__)
1167 int totalMSWItems
= GetMenuCount();
1169 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1172 int i
; // For old C++ compatibility
1173 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1175 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1178 for(i
=0; i
<wxpos
; i
++)
1180 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1187 // ---------------------------------------------------------------------------
1188 // wxMenuBar functions to work with the top level submenus
1189 // ---------------------------------------------------------------------------
1191 // NB: we don't support owner drawn top level items for now, if we do these
1192 // functions would have to be changed to use wxMenuItem as well
1194 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1196 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1197 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1199 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1201 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1206 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1208 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1210 m_menus
[pos
]->wxMenuBase::SetTitle(label
);
1212 if ( !IsAttached() )
1216 //else: have to modify the existing menu
1218 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1221 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1222 if ( flagsOld
== 0xFFFFFFFF )
1224 wxLogLastError(wxT("GetMenuState"));
1229 if ( flagsOld
& MF_POPUP
)
1231 // HIBYTE contains the number of items in the submenu in this case
1233 id
= (UINT_PTR
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1243 info
.cbSize
= sizeof(info
);
1244 info
.fMask
= MIIM_TYPE
;
1245 info
.fType
= MFT_STRING
;
1246 info
.cch
= label
.length();
1247 info
.dwTypeData
= const_cast<wxChar
*>(label
.wx_str());
1248 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, &info
) )
1250 wxLogLastError(wxT("SetMenuItemInfo"));
1254 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1255 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1257 wxLogLastError(wxT("ModifyMenu"));
1264 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1266 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1267 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1269 return m_menus
[pos
]->GetTitle();
1272 // ---------------------------------------------------------------------------
1273 // wxMenuBar construction
1274 // ---------------------------------------------------------------------------
1276 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1278 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1282 menu
->wxMenuBase::SetTitle(title
);
1284 #if defined(WINCE_WITHOUT_COMMANDBAR)
1290 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1292 // can't use ModifyMenu() because it deletes the submenu it replaces
1293 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1295 wxLogLastError(wxT("RemoveMenu"));
1298 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1299 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1300 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1302 wxLogLastError(wxT("InsertMenu"));
1306 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1308 // need to rebuild accell table
1309 RebuildAccelTable();
1311 #endif // wxUSE_ACCEL
1320 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1322 // Find out which MSW item before which we'll be inserting before
1323 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1324 // If IsAttached() is false this won't be used anyway
1326 #if defined(WINCE_WITHOUT_COMMANDBAR)
1332 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1333 ? -1 // append the menu
1334 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1336 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1339 menu
->wxMenuBase::SetTitle(title
);
1343 #if defined(WINCE_WITHOUT_COMMANDBAR)
1347 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1348 tbButton
.iBitmap
= I_IMAGENONE
;
1349 tbButton
.fsState
= TBSTATE_ENABLED
;
1350 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1352 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1353 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1354 wxString label
= wxStripMenuCodes(title
);
1355 tbButton
.iString
= (int) label
.wx_str();
1357 tbButton
.idCommand
= NewControlId();
1358 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1360 wxLogLastError(wxT("TB_INSERTBUTTON"));
1363 wxUnusedVar(mswpos
);
1365 if ( !::InsertMenu(GetHmenu(), mswpos
,
1366 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1367 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1369 wxLogLastError(wxT("InsertMenu"));
1373 if ( menu
->HasAccels() )
1375 // need to rebuild accell table
1376 RebuildAccelTable();
1378 #endif // wxUSE_ACCEL
1387 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1389 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1390 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1392 if ( !wxMenuBarBase::Append(menu
, title
) )
1395 menu
->wxMenuBase::SetTitle(title
);
1397 #if defined(WINCE_WITHOUT_COMMANDBAR)
1403 #if defined(WINCE_WITHOUT_COMMANDBAR)
1407 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1408 tbButton
.iBitmap
= I_IMAGENONE
;
1409 tbButton
.fsState
= TBSTATE_ENABLED
;
1410 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1412 size_t pos
= GetMenuCount();
1413 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1414 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1415 wxString label
= wxStripMenuCodes(title
);
1416 tbButton
.iString
= (int) label
.wx_str();
1418 tbButton
.idCommand
= NewControlId();
1419 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1421 wxLogLastError(wxT("TB_INSERTBUTTON"));
1425 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1426 (UINT_PTR
)submenu
, title
.wx_str()) )
1428 wxLogLastError(wxT("AppendMenu"));
1433 if ( menu
->HasAccels() )
1435 // need to rebuild accelerator table
1436 RebuildAccelTable();
1438 #endif // wxUSE_ACCEL
1447 wxMenu
*wxMenuBar::Remove(size_t pos
)
1449 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1453 #if defined(WINCE_WITHOUT_COMMANDBAR)
1459 #if defined(WINCE_WITHOUT_COMMANDBAR)
1462 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1464 wxLogLastError(wxT("TB_DELETEBUTTON"));
1468 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1470 wxLogLastError(wxT("RemoveMenu"));
1475 if ( menu
->HasAccels() )
1477 // need to rebuild accell table
1478 RebuildAccelTable();
1480 #endif // wxUSE_ACCEL
1491 void wxMenuBar::RebuildAccelTable()
1493 // merge the accelerators of all menus into one accel table
1494 size_t nAccelCount
= 0;
1495 size_t i
, count
= GetMenuCount();
1496 wxMenuList::iterator it
;
1497 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1499 nAccelCount
+= (*it
)->GetAccelCount();
1504 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1507 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1509 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1512 SetAcceleratorTable(wxAcceleratorTable(nAccelCount
, accelEntries
));
1514 delete [] accelEntries
;
1518 #endif // wxUSE_ACCEL
1520 void wxMenuBar::Attach(wxFrame
*frame
)
1522 wxMenuBarBase::Attach(frame
);
1524 #if defined(WINCE_WITH_COMMANDBAR)
1528 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1533 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1535 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1542 RebuildAccelTable();
1543 #endif // wxUSE_ACCEL
1546 #if defined(WINCE_WITH_COMMANDBAR)
1547 bool wxMenuBar::AddAdornments(long style
)
1549 if (m_adornmentsAdded
|| !m_commandBar
)
1552 if (style
& wxCLOSE_BOX
)
1554 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1556 wxLogLastError(wxT("CommandBar_AddAdornments"));
1567 void wxMenuBar::Detach()
1569 wxMenuBarBase::Detach();
1572 // get the menu with given handle (recursively)
1573 wxMenu
* wxMenuBar::MSWGetMenu(WXHMENU hMenu
)
1575 wxCHECK_MSG( GetHMenu() != hMenu
, NULL
,
1576 wxT("wxMenuBar::MSWGetMenu(): menu handle is wxMenuBar, not wxMenu") );
1579 for ( size_t n
= 0 ; n
< GetMenuCount(); ++n
)
1581 wxMenu
* menu
= GetMenu(n
)->MSWGetMenu(hMenu
);
1590 #endif // wxUSE_MENUS