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)
268 #if wxUSE_OWNER_DRAWN
269 m_ownerDrawn
= false;
270 m_maxBitmapWidth
= 0;
271 m_maxAccelWidth
= -1;
272 #endif // wxUSE_OWNER_DRAWN
275 m_hMenu
= (WXHMENU
)CreatePopupMenu();
278 wxLogLastError(wxT("CreatePopupMenu"));
281 // if we have a title, insert it in the beginning of the menu
282 if ( !m_title
.empty() )
284 const wxString title
= m_title
;
285 m_title
.clear(); // so that SetTitle() knows there was no title before
290 // The wxWindow destructor will take care of deleting the submenus.
293 // we should free Windows resources only if Windows doesn't do it for us
294 // which happens if we're attached to a menubar or a submenu of another
296 if ( !IsAttached() && !GetParent() )
298 if ( !::DestroyMenu(GetHmenu()) )
300 wxLogLastError(wxT("DestroyMenu"));
306 WX_CLEAR_ARRAY(m_accels
);
307 #endif // wxUSE_ACCEL
314 // this will take effect during the next call to Append()
320 int wxMenu::FindAccel(int id
) const
322 size_t n
, count
= m_accels
.GetCount();
323 for ( n
= 0; n
< count
; n
++ )
325 if ( m_accels
[n
]->m_command
== id
)
332 void wxMenu::UpdateAccel(wxMenuItem
*item
)
334 if ( item
->IsSubMenu() )
336 wxMenu
*submenu
= item
->GetSubMenu();
337 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
340 UpdateAccel(node
->GetData());
342 node
= node
->GetNext();
345 else if ( !item
->IsSeparator() )
347 // recurse upwards: we should only modify m_accels of the top level
348 // menus, not of the submenus as wxMenuBar doesn't look at them
349 // (alternative and arguable cleaner solution would be to recurse
350 // downwards in GetAccelCount() and CopyAccels())
353 GetParent()->UpdateAccel(item
);
357 // find the (new) accel for this item
358 wxAcceleratorEntry
*accel
= wxAcceleratorEntry::Create(item
->GetItemLabel());
360 accel
->m_command
= item
->GetId();
363 int n
= FindAccel(item
->GetId());
364 if ( n
== wxNOT_FOUND
)
366 // no old, add new if any
370 return; // skipping RebuildAccelTable() below
374 // replace old with new or just remove the old one if no new
379 m_accels
.RemoveAt(n
);
384 GetMenuBar()->RebuildAccelTable();
387 ResetMaxAccelWidth();
389 //else: it is a separator, they can't have accels, nothing to do
392 #endif // wxUSE_ACCEL
397 // helper of DoInsertOrAppend(): returns the HBITMAP to use in MENUITEMINFO
398 HBITMAP
GetHBitmapForMenu(wxMenuItem
*pItem
, bool checked
= true)
400 // Under versions of Windows older than Vista we can't pass HBITMAP
401 // directly as hbmpItem for 2 reasons:
402 // 1. We can't draw it with transparency then (this is not
403 // very important now but would be with themed menu bg)
404 // 2. Worse, Windows inverts the bitmap for the selected
405 // item and this looks downright ugly
407 // So we prefer to instead draw it ourselves in MSWOnDrawItem().by using
408 // HBMMENU_CALLBACK when inserting it
410 // However under Vista using HBMMENU_CALLBACK causes the entire menu to be
411 // drawn using the classic theme instead of the current one and it does
412 // handle transparency just fine so do use the real bitmap there
414 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
416 wxBitmap bmp
= pItem
->GetBitmap(checked
);
419 // we must use PARGB DIB for the menu bitmaps so ensure that we do
420 wxImage
img(bmp
.ConvertToImage());
421 if ( !img
.HasAlpha() )
424 pItem
->SetBitmap(img
, checked
);
427 return GetHbitmapOf(pItem
->GetBitmap(checked
));
429 //else: bitmap is not set
433 #endif // wxUSE_IMAGE
435 return HBMMENU_CALLBACK
;
438 } // anonymous namespace
440 bool wxMenu::MSWGetRadioGroupRange(int pos
, int *start
, int *end
) const
442 return m_radioData
&& m_radioData
->GetGroupRange(pos
, start
, end
);
445 // append a new item or submenu to the menu
446 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
450 #endif // wxUSE_ACCEL
452 // we should support disabling the item even prior to adding it to the menu
453 UINT flags
= pItem
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
;
455 // if "Break" has just been called, insert a menu break before this item
456 // (and don't forget to reset the flag)
458 flags
|= MF_MENUBREAK
;
462 if ( pItem
->IsSeparator() ) {
463 flags
|= MF_SEPARATOR
;
466 // id is the numeric id for normal menu items and HMENU for submenus as
467 // required by ::AppendMenu() API
469 wxMenu
*submenu
= pItem
->GetSubMenu();
470 if ( submenu
!= NULL
) {
471 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
473 submenu
->SetParent(this);
475 id
= (UINT_PTR
)submenu
->GetHMenu();
480 id
= pItem
->GetMSWId();
484 // prepare to insert the item in the menu
485 wxString itemText
= pItem
->GetItemLabel();
486 LPCTSTR pData
= NULL
;
487 if ( pos
== (size_t)-1 )
489 // append at the end (note that the item is already appended to
490 // internal data structures)
491 pos
= GetMenuItemCount() - 1;
494 // Update radio groups data if we're inserting a new radio item.
496 // NB: If we supported inserting non-radio items in the middle of existing
497 // radio groups to break them into two subgroups, we'd need to update
498 // m_radioData in this case too but currently this is not supported.
499 bool checkInitially
= false;
500 if ( pItem
->GetKind() == wxITEM_RADIO
)
503 m_radioData
= new wxMenuRadioItemsData
;
505 if ( m_radioData
->UpdateOnInsert(pos
) )
506 checkInitially
= true;
509 // adjust position to account for the title of a popup menu, if any
510 if ( !GetMenuBar() && !m_title
.empty() )
511 pos
+= 2; // for the title itself and its separator
515 #if wxUSE_OWNER_DRAWN
516 // Under older systems mixing owner-drawn and non-owner-drawn items results
517 // in inconsistent margins, so we force this one to be owner-drawn if any
518 // other items already are.
520 pItem
->SetOwnerDrawn(true);
521 #endif // wxUSE_OWNER_DRAWN
523 // check if we have something more than a simple text item
524 #if wxUSE_OWNER_DRAWN
525 if ( pItem
->IsOwnerDrawn() )
529 if ( !m_ownerDrawn
&& !pItem
->IsSeparator() )
531 // MIIM_BITMAP only works under WinME/2000+ so we always use owner
532 // drawn item under the previous versions and we also have to use
533 // them in any case if the item has custom colours or font
534 static const wxWinVersion winver
= wxGetWinVersion();
535 bool mustUseOwnerDrawn
= winver
< wxWinVersion_98
||
536 pItem
->GetTextColour().IsOk() ||
537 pItem
->GetBackgroundColour().IsOk() ||
538 pItem
->GetFont().IsOk();
540 if ( !mustUseOwnerDrawn
)
542 const wxBitmap
& bmpUnchecked
= pItem
->GetBitmap(false),
543 bmpChecked
= pItem
->GetBitmap(true);
545 if ( (bmpUnchecked
.IsOk() && IsGreaterThanStdSize(bmpUnchecked
)) ||
546 (bmpChecked
.IsOk() && IsGreaterThanStdSize(bmpChecked
)) )
548 mustUseOwnerDrawn
= true;
552 // use InsertMenuItem() if possible as it's guaranteed to look
553 // correct while our owner-drawn code is not
554 if ( !mustUseOwnerDrawn
)
556 WinStruct
<MENUITEMINFO
> mii
;
557 mii
.fMask
= MIIM_STRING
| MIIM_DATA
;
559 // don't set hbmpItem for the checkable items as it would
560 // be used for both checked and unchecked state
561 if ( pItem
->IsCheckable() )
563 mii
.fMask
|= MIIM_CHECKMARKS
;
564 mii
.hbmpChecked
= GetHBitmapForMenu(pItem
, true);
565 mii
.hbmpUnchecked
= GetHBitmapForMenu(pItem
, false);
567 else if ( pItem
->GetBitmap().IsOk() )
569 mii
.fMask
|= MIIM_BITMAP
;
570 mii
.hbmpItem
= GetHBitmapForMenu(pItem
);
573 mii
.cch
= itemText
.length();
574 mii
.dwTypeData
= const_cast<wxChar
*>(itemText
.wx_str());
576 if ( flags
& MF_POPUP
)
578 mii
.fMask
|= MIIM_SUBMENU
;
579 mii
.hSubMenu
= GetHmenuOf(pItem
->GetSubMenu());
583 mii
.fMask
|= MIIM_ID
;
587 mii
.dwItemData
= reinterpret_cast<ULONG_PTR
>(pItem
);
589 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
592 wxLogLastError(wxT("InsertMenuItem()"));
594 else // InsertMenuItem() ok
596 // we need to remove the extra indent which is reserved for
597 // the checkboxes by default as it looks ugly unless check
598 // boxes are used together with bitmaps and this is not the
600 WinStruct
<MENUINFO
> mi
;
602 // don't call SetMenuInfo() directly, this would prevent
603 // the app from starting up under Windows 95/NT 4
604 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
606 wxDynamicLibrary
dllUser(wxT("user32"));
607 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
608 if ( pfnSetMenuInfo
)
610 mi
.fMask
= MIM_STYLE
;
611 mi
.dwStyle
= MNS_CHECKORBMP
;
612 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
614 wxLogLastError(wxT("SetMenuInfo(MNS_NOCHECK)"));
618 // tell the item that it's not really owner-drawn but only
619 // needs to draw its bitmap, the rest is done by Windows
620 pItem
->SetOwnerDrawn(false);
628 // item draws itself, pass pointer to it in data parameter
629 flags
|= MF_OWNERDRAW
;
630 pData
= (LPCTSTR
)pItem
;
632 bool updateAllMargins
= false;
634 // get size of bitmap always return valid value (0 for invalid bitmap),
635 // so we don't needed check if bitmap is valid ;)
636 int uncheckedW
= pItem
->GetBitmap(false).GetWidth();
637 int checkedW
= pItem
->GetBitmap(true).GetWidth();
639 if ( m_maxBitmapWidth
< uncheckedW
)
641 m_maxBitmapWidth
= uncheckedW
;
642 updateAllMargins
= true;
645 if ( m_maxBitmapWidth
< checkedW
)
647 m_maxBitmapWidth
= checkedW
;
648 updateAllMargins
= true;
651 // make other item ownerdrawn and update margin width for equals alignment
652 if ( !m_ownerDrawn
|| updateAllMargins
)
654 // we must use position in SetOwnerDrawnMenuItem because
655 // all separators have the same id
657 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
660 wxMenuItem
* item
= node
->GetData();
662 if ( !item
->IsOwnerDrawn())
664 item
->SetOwnerDrawn(true);
665 SetOwnerDrawnMenuItem(GetHmenu(), pos
,
666 reinterpret_cast<ULONG_PTR
>(item
), TRUE
);
669 item
->SetMarginWidth(m_maxBitmapWidth
);
671 node
= node
->GetNext();
675 // set menu as ownerdrawn
678 ResetMaxAccelWidth();
680 // only update our margin for equals alignment to other item
681 else if ( !updateAllMargins
)
683 pItem
->SetMarginWidth(m_maxBitmapWidth
);
688 #endif // wxUSE_OWNER_DRAWN
690 // item is just a normal string (passed in data parameter)
694 itemText
= wxMenuItem::GetLabelText(itemText
);
697 pData
= (wxChar
*)itemText
.wx_str();
700 // item might have already been inserted by InsertMenuItem() above
703 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
705 wxLogLastError(wxT("InsertMenu[Item]()"));
712 // Check the item if it should be initially checked.
713 if ( checkInitially
)
716 // if we just appended the title, highlight it
717 if ( id
== (UINT_PTR
)idMenuTitle
)
719 // visually select the menu title
720 SetDefaultMenuItem(GetHmenu(), id
);
723 // if we're already attached to the menubar, we must update it
724 if ( IsAttached() && GetMenuBar()->IsAttached() )
726 GetMenuBar()->Refresh();
732 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
734 return wxMenuBase::DoAppend(item
) && DoInsertOrAppend(item
) ? item
: NULL
;
737 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
739 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
745 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
747 // we need to find the item's position in the child list
749 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
750 for ( pos
= 0; node
; pos
++ )
752 if ( node
->GetData() == item
)
755 node
= node
->GetNext();
758 // DoRemove() (unlike Remove) can only be called for an existing item!
759 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
762 // remove the corresponding accel from the accel table
763 int n
= FindAccel(item
->GetId());
764 if ( n
!= wxNOT_FOUND
)
768 m_accels
.RemoveAt(n
);
770 ResetMaxAccelWidth();
772 //else: this item doesn't have an accel, nothing to do
773 #endif // wxUSE_ACCEL
775 // remove the item from the menu
776 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
778 wxLogLastError(wxT("RemoveMenu"));
781 if ( IsAttached() && GetMenuBar()->IsAttached() )
783 // otherwise, the change won't be visible
784 GetMenuBar()->Refresh();
787 // and from internal data structures
788 return wxMenuBase::DoRemove(item
);
791 // ---------------------------------------------------------------------------
792 // accelerator helpers
793 // ---------------------------------------------------------------------------
797 // create the wxAcceleratorEntries for our accels and put them into the provided
798 // array - return the number of accels we have
799 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
801 size_t count
= GetAccelCount();
802 for ( size_t n
= 0; n
< count
; n
++ )
804 *accels
++ = *m_accels
[n
];
810 wxAcceleratorTable
*wxMenu::CreateAccelTable() const
812 const size_t count
= m_accels
.size();
813 wxScopedArray
<wxAcceleratorEntry
> accels(new wxAcceleratorEntry
[count
]);
814 CopyAccels(accels
.get());
816 return new wxAcceleratorTable(count
, accels
.get());
819 #endif // wxUSE_ACCEL
821 // ---------------------------------------------------------------------------
822 // ownerdrawn helpers
823 // ---------------------------------------------------------------------------
825 #if wxUSE_OWNER_DRAWN
827 void wxMenu::CalculateMaxAccelWidth()
829 wxASSERT_MSG( m_maxAccelWidth
== -1, wxT("it's really needed?") );
831 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
834 wxMenuItem
* item
= node
->GetData();
836 if ( item
->IsOwnerDrawn() )
838 int width
= item
->MeasureAccelWidth();
839 if (width
> m_maxAccelWidth
)
840 m_maxAccelWidth
= width
;
843 node
= node
->GetNext();
847 #endif // wxUSE_OWNER_DRAWN
849 // ---------------------------------------------------------------------------
851 // ---------------------------------------------------------------------------
853 void wxMenu::SetTitle(const wxString
& label
)
855 bool hasNoTitle
= m_title
.empty();
858 HMENU hMenu
= GetHmenu();
862 if ( !label
.empty() )
864 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
865 (UINT_PTR
)idMenuTitle
, m_title
.wx_str()) ||
866 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
868 wxLogLastError(wxT("InsertMenu"));
876 // remove the title and the separator after it
877 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
878 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
880 wxLogLastError(wxT("RemoveMenu"));
889 info
.cbSize
= sizeof(info
);
890 info
.fMask
= MIIM_TYPE
;
891 info
.fType
= MFT_STRING
;
892 info
.cch
= m_title
.length();
893 info
.dwTypeData
= const_cast<wxChar
*>(m_title
.wx_str());
894 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
896 wxLogLastError(wxT("SetMenuItemInfo"));
899 if ( !ModifyMenu(hMenu
, 0u,
900 MF_BYPOSITION
| MF_STRING
,
901 (UINT_PTR
)idMenuTitle
, m_title
.wx_str()) )
903 wxLogLastError(wxT("ModifyMenu"));
910 // put the title string in bold face
911 if ( !m_title
.empty() )
913 SetDefaultMenuItem(GetHmenu(), (UINT_PTR
)idMenuTitle
);
918 // ---------------------------------------------------------------------------
920 // ---------------------------------------------------------------------------
922 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
924 const int id
= (signed short)id_
;
926 // ignore commands from the menu title
927 if ( id
!= idMenuTitle
)
929 // update the check item when it's clicked
930 wxMenuItem
* const item
= FindItem(id
);
931 if ( item
&& item
->IsCheckable() )
934 // get the status of the menu item: note that it has been just changed
935 // by Toggle() above so here we already get the new state of the item
936 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
);
937 SendEvent(id
, menuState
& MF_CHECKED
);
943 // get the menu with given handle (recursively)
944 wxMenu
* wxMenu::MSWGetMenu(WXHMENU hMenu
)
947 if ( GetHMenu() == hMenu
)
950 // recursively query submenus
951 for ( size_t n
= 0 ; n
< GetMenuItemCount(); ++n
)
953 wxMenuItem
* item
= FindItemByPosition(n
);
954 wxMenu
* submenu
= item
->GetSubMenu();
957 submenu
= submenu
->MSWGetMenu(hMenu
);
967 // ---------------------------------------------------------------------------
969 // ---------------------------------------------------------------------------
971 void wxMenuBar::Init()
973 m_eventHandler
= this;
975 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
978 // Not using a combined wxToolBar/wxMenuBar? then use
979 // a commandbar in WinCE .NET just to implement the
981 #if defined(WINCE_WITH_COMMANDBAR)
983 m_adornmentsAdded
= false;
987 wxMenuBar::wxMenuBar()
992 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
997 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
1001 for ( size_t i
= 0; i
< count
; i
++ )
1003 // We just want to store the menu title in the menu itself, not to
1004 // show it as a dummy item in the menu itself as we do with the popup
1005 // menu titles in overridden wxMenu::SetTitle().
1006 menus
[i
]->wxMenuBase::SetTitle(titles
[i
]);
1007 m_menus
.Append(menus
[i
]);
1009 menus
[i
]->Attach(this);
1013 wxMenuBar::~wxMenuBar()
1015 // In Windows CE (not .NET), the menubar is always associated
1016 // with a toolbar, which destroys the menu implicitly.
1017 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
1020 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
1022 toolMenuBar
->SetMenuBar(NULL
);
1025 // we should free Windows resources only if Windows doesn't do it for us
1026 // which happens if we're attached to a frame
1027 if (m_hMenu
&& !IsAttached())
1029 #if defined(WINCE_WITH_COMMANDBAR)
1030 ::DestroyWindow((HWND
) m_commandBar
);
1031 m_commandBar
= (WXHWND
) NULL
;
1033 ::DestroyMenu((HMENU
)m_hMenu
);
1035 m_hMenu
= (WXHMENU
)NULL
;
1040 // ---------------------------------------------------------------------------
1041 // wxMenuBar helpers
1042 // ---------------------------------------------------------------------------
1044 void wxMenuBar::Refresh()
1049 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
1051 #if defined(WINCE_WITHOUT_COMMANDBAR)
1054 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
1056 #elif defined(WINCE_WITH_COMMANDBAR)
1058 DrawMenuBar((HWND
) m_commandBar
);
1060 DrawMenuBar(GetHwndOf(GetFrame()));
1064 WXHMENU
wxMenuBar::Create()
1066 // Note: this doesn't work at all on Smartphone,
1067 // since you have to use resources.
1068 // We'll have to find another way to add a menu
1069 // by changing/adding menu items to an existing menu.
1070 #if defined(WINCE_WITHOUT_COMMANDBAR)
1074 wxToolMenuBar
* const bar
= static_cast<wxToolMenuBar
*>(GetToolBar());
1078 HWND hCommandBar
= GetHwndOf(bar
);
1080 // notify comctl32.dll about the version of the headers we use before using
1081 // any other TB_XXX messages
1082 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
1085 wxZeroMemory(tbButton
);
1086 tbButton
.iBitmap
= I_IMAGENONE
;
1087 tbButton
.fsState
= TBSTATE_ENABLED
;
1088 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
1089 TBSTYLE_NO_DROPDOWN_ARROW
|
1092 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
1094 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
1095 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1096 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
1097 tbButton
.iString
= (int) label
.wx_str();
1099 tbButton
.idCommand
= NewControlId();
1100 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
1102 wxLogLastError(wxT("TB_INSERTBUTTON"));
1106 m_hMenu
= bar
->GetHMenu();
1108 #else // !__WXWINCE__
1112 m_hMenu
= (WXHMENU
)::CreateMenu();
1116 wxLogLastError(wxT("CreateMenu"));
1120 for ( wxMenuList::iterator it
= m_menus
.begin();
1121 it
!= m_menus
.end();
1124 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
1125 (UINT_PTR
)(*it
)->GetHMenu(),
1126 (*it
)->GetTitle().wx_str()) )
1128 wxLogLastError(wxT("AppendMenu"));
1134 #endif // __WXWINCE__/!__WXWINCE__
1137 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
1140 wxASSERT(menu
->GetHMenu());
1143 #if defined(__WXWINCE__)
1144 int totalMSWItems
= GetMenuCount();
1146 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1149 int i
; // For old C++ compatibility
1150 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1152 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1155 for(i
=0; i
<wxpos
; i
++)
1157 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1164 // ---------------------------------------------------------------------------
1165 // wxMenuBar functions to work with the top level submenus
1166 // ---------------------------------------------------------------------------
1168 // NB: we don't support owner drawn top level items for now, if we do these
1169 // functions would have to be changed to use wxMenuItem as well
1171 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1173 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1174 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1176 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1178 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1183 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1185 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1187 m_menus
[pos
]->wxMenuBase::SetTitle(label
);
1189 if ( !IsAttached() )
1193 //else: have to modify the existing menu
1195 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1198 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1199 if ( flagsOld
== 0xFFFFFFFF )
1201 wxLogLastError(wxT("GetMenuState"));
1206 if ( flagsOld
& MF_POPUP
)
1208 // HIBYTE contains the number of items in the submenu in this case
1210 id
= (UINT_PTR
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1220 info
.cbSize
= sizeof(info
);
1221 info
.fMask
= MIIM_TYPE
;
1222 info
.fType
= MFT_STRING
;
1223 info
.cch
= label
.length();
1224 info
.dwTypeData
= const_cast<wxChar
*>(label
.wx_str());
1225 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, &info
) )
1227 wxLogLastError(wxT("SetMenuItemInfo"));
1231 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1232 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1234 wxLogLastError(wxT("ModifyMenu"));
1241 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1243 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1244 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1246 return m_menus
[pos
]->GetTitle();
1249 // ---------------------------------------------------------------------------
1250 // wxMenuBar construction
1251 // ---------------------------------------------------------------------------
1253 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1255 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1259 menu
->wxMenuBase::SetTitle(title
);
1261 #if defined(WINCE_WITHOUT_COMMANDBAR)
1267 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1269 // can't use ModifyMenu() because it deletes the submenu it replaces
1270 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1272 wxLogLastError(wxT("RemoveMenu"));
1275 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1276 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1277 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1279 wxLogLastError(wxT("InsertMenu"));
1283 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1285 // need to rebuild accell table
1286 RebuildAccelTable();
1288 #endif // wxUSE_ACCEL
1297 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1299 // Find out which MSW item before which we'll be inserting before
1300 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1301 // If IsAttached() is false this won't be used anyway
1303 #if defined(WINCE_WITHOUT_COMMANDBAR)
1309 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1310 ? -1 // append the menu
1311 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1313 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1316 menu
->wxMenuBase::SetTitle(title
);
1320 #if defined(WINCE_WITHOUT_COMMANDBAR)
1324 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1325 tbButton
.iBitmap
= I_IMAGENONE
;
1326 tbButton
.fsState
= TBSTATE_ENABLED
;
1327 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1329 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1330 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1331 wxString label
= wxStripMenuCodes(title
);
1332 tbButton
.iString
= (int) label
.wx_str();
1334 tbButton
.idCommand
= NewControlId();
1335 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1337 wxLogLastError(wxT("TB_INSERTBUTTON"));
1340 wxUnusedVar(mswpos
);
1342 if ( !::InsertMenu(GetHmenu(), mswpos
,
1343 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1344 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1346 wxLogLastError(wxT("InsertMenu"));
1350 if ( menu
->HasAccels() )
1352 // need to rebuild accell table
1353 RebuildAccelTable();
1355 #endif // wxUSE_ACCEL
1364 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1366 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1367 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1369 if ( !wxMenuBarBase::Append(menu
, title
) )
1372 menu
->wxMenuBase::SetTitle(title
);
1374 #if defined(WINCE_WITHOUT_COMMANDBAR)
1380 #if defined(WINCE_WITHOUT_COMMANDBAR)
1384 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1385 tbButton
.iBitmap
= I_IMAGENONE
;
1386 tbButton
.fsState
= TBSTATE_ENABLED
;
1387 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1389 size_t pos
= GetMenuCount();
1390 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1391 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1392 wxString label
= wxStripMenuCodes(title
);
1393 tbButton
.iString
= (int) label
.wx_str();
1395 tbButton
.idCommand
= NewControlId();
1396 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1398 wxLogLastError(wxT("TB_INSERTBUTTON"));
1402 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1403 (UINT_PTR
)submenu
, title
.wx_str()) )
1405 wxLogLastError(wxT("AppendMenu"));
1410 if ( menu
->HasAccels() )
1412 // need to rebuild accelerator table
1413 RebuildAccelTable();
1415 #endif // wxUSE_ACCEL
1424 wxMenu
*wxMenuBar::Remove(size_t pos
)
1426 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1430 #if defined(WINCE_WITHOUT_COMMANDBAR)
1436 #if defined(WINCE_WITHOUT_COMMANDBAR)
1439 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1441 wxLogLastError(wxT("TB_DELETEBUTTON"));
1445 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1447 wxLogLastError(wxT("RemoveMenu"));
1452 if ( menu
->HasAccels() )
1454 // need to rebuild accell table
1455 RebuildAccelTable();
1457 #endif // wxUSE_ACCEL
1468 void wxMenuBar::RebuildAccelTable()
1470 // merge the accelerators of all menus into one accel table
1471 size_t nAccelCount
= 0;
1472 size_t i
, count
= GetMenuCount();
1473 wxMenuList::iterator it
;
1474 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1476 nAccelCount
+= (*it
)->GetAccelCount();
1481 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1484 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1486 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1489 SetAcceleratorTable(wxAcceleratorTable(nAccelCount
, accelEntries
));
1491 delete [] accelEntries
;
1495 #endif // wxUSE_ACCEL
1497 void wxMenuBar::Attach(wxFrame
*frame
)
1499 wxMenuBarBase::Attach(frame
);
1501 #if defined(WINCE_WITH_COMMANDBAR)
1505 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1510 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1512 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1519 RebuildAccelTable();
1520 #endif // wxUSE_ACCEL
1523 #if defined(WINCE_WITH_COMMANDBAR)
1524 bool wxMenuBar::AddAdornments(long style
)
1526 if (m_adornmentsAdded
|| !m_commandBar
)
1529 if (style
& wxCLOSE_BOX
)
1531 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1533 wxLogLastError(wxT("CommandBar_AddAdornments"));
1544 void wxMenuBar::Detach()
1546 wxMenuBarBase::Detach();
1549 // get the menu with given handle (recursively)
1550 wxMenu
* wxMenuBar::MSWGetMenu(WXHMENU hMenu
)
1552 wxCHECK_MSG( GetHMenu() != hMenu
, NULL
,
1553 wxT("wxMenuBar::MSWGetMenu(): menu handle is wxMenuBar, not wxMenu") );
1556 for ( size_t n
= 0 ; n
< GetMenuCount(); ++n
)
1558 wxMenu
* menu
= GetMenu(n
)->MSWGetMenu(hMenu
);
1567 #endif // wxUSE_MENUS