1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/menu.cpp
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ===========================================================================
13 // ===========================================================================
15 // ---------------------------------------------------------------------------
17 // ---------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
39 #include "wx/ownerdrw.h"
42 #include "wx/scopedarray.h"
43 #include "wx/vector.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 int idMenuTitle
= wxID_NONE
;
87 // ----------------------------------------------------------------------------
88 // private helper classes and functions
89 // ----------------------------------------------------------------------------
91 // Contains the data about the radio items groups in the given menu.
92 class wxMenuRadioItemsData
95 wxMenuRadioItemsData() { }
97 // Default copy ctor, assignment operator and dtor are all ok.
99 // Find the start and end of the group containing the given position or
100 // return false if it's not inside any range.
101 bool GetGroupRange(int pos
, int *start
, int *end
) const
103 // We use a simple linear search here because there are not that many
104 // items in a menu and hence even fewer radio items ranges anyhow, so
105 // normally there is no need to do anything fancy (like keeping the
106 // array sorted and using binary search).
107 for ( Ranges::const_iterator it
= m_ranges
.begin();
108 it
!= m_ranges
.end();
111 const Range
& r
= *it
;
113 if ( r
.start
<= pos
&& pos
<= r
.end
)
127 // Take into account the new radio item about to be added at the given
130 // Returns true if this item starts a new radio group, false if it extends
132 bool UpdateOnInsert(int pos
)
134 bool inExistingGroup
= false;
136 for ( Ranges::iterator it
= m_ranges
.begin();
137 it
!= m_ranges
.end();
144 // Item is inserted before this range, update its indices.
148 else if ( pos
<= r
.end
+ 1 )
150 // Item is inserted in the middle of this range or immediately
151 // after it in which case it extends this range so make it span
152 // one more item in any case.
155 inExistingGroup
= true;
157 //else: Item is inserted after this range, nothing to do for it.
160 if ( inExistingGroup
)
163 // Make a new range for the group this item will belong to.
167 m_ranges
.push_back(r
);
173 // Contains the inclusive positions of the range start and end.
180 typedef wxVector
<Range
> Ranges
;
187 // make the given menu item default
188 void SetDefaultMenuItem(HMENU
WXUNUSED_IN_WINCE(hmenu
),
189 UINT
WXUNUSED_IN_WINCE(id
))
192 WinStruct
<MENUITEMINFO
> mii
;
193 mii
.fMask
= MIIM_STATE
;
194 mii
.fState
= MFS_DEFAULT
;
196 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
198 wxLogLastError(wxT("SetMenuItemInfo"));
200 #endif // !__WXWINCE__
203 // make the given menu item owner-drawn
204 void SetOwnerDrawnMenuItem(HMENU
WXUNUSED_IN_WINCE(hmenu
),
205 UINT
WXUNUSED_IN_WINCE(id
),
206 ULONG_PTR
WXUNUSED_IN_WINCE(data
),
207 BOOL
WXUNUSED_IN_WINCE(byPositon
= FALSE
))
210 WinStruct
<MENUITEMINFO
> mii
;
211 mii
.fMask
= MIIM_FTYPE
| MIIM_DATA
;
212 mii
.fType
= MFT_OWNERDRAW
;
213 mii
.dwItemData
= data
;
215 if ( reinterpret_cast<wxMenuItem
*>(data
)->IsSeparator() )
216 mii
.fType
|= MFT_SEPARATOR
;
218 if ( !::SetMenuItemInfo(hmenu
, id
, byPositon
, &mii
) )
220 wxLogLastError(wxT("SetMenuItemInfo"));
222 #endif // !__WXWINCE__
226 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
228 WinStruct
<MENUITEMINFO
> info
;
229 info
.fMask
= MIIM_STATE
;
230 // MF_BYCOMMAND is zero so test MF_BYPOSITION
231 if ( !::GetMenuItemInfo(hMenu
, id
, flags
& MF_BYPOSITION
? TRUE
: FALSE
, & info
) )
233 wxLogLastError(wxT("GetMenuItemInfo"));
237 #endif // __WXWINCE__
239 inline bool IsGreaterThanStdSize(const wxBitmap
& bmp
)
241 return bmp
.GetWidth() > ::GetSystemMetrics(SM_CXMENUCHECK
) ||
242 bmp
.GetHeight() > ::GetSystemMetrics(SM_CYMENUCHECK
);
245 } // anonymous namespace
247 // ============================================================================
249 // ============================================================================
251 // ---------------------------------------------------------------------------
252 // wxMenu construction, adding and removing menu items
253 // ---------------------------------------------------------------------------
255 // Construct a menu with optional title (then use append)
256 void wxMenu::InitNoCreate()
261 #if wxUSE_OWNER_DRAWN
262 m_ownerDrawn
= false;
263 m_maxBitmapWidth
= 0;
264 m_maxAccelWidth
= -1;
265 #endif // wxUSE_OWNER_DRAWN
273 m_hMenu
= (WXHMENU
)CreatePopupMenu();
276 wxLogLastError(wxT("CreatePopupMenu"));
279 // if we have a title, insert it in the beginning of the menu
280 if ( !m_title
.empty() )
282 const wxString title
= m_title
;
283 m_title
.clear(); // so that SetTitle() knows there was no title before
288 wxMenu::wxMenu(WXHMENU hMenu
)
294 // Ensure that our internal idea of how many items we have corresponds to
295 // the real number of items in the menu.
297 // We could also retrieve the real labels of the items here but it doesn't
298 // seem to be worth the trouble.
299 const int numExistingItems
= ::GetMenuItemCount(m_hMenu
);
300 for ( int n
= 0; n
< numExistingItems
; n
++ )
302 wxMenuBase::DoAppend(wxMenuItem::New(this, wxID_SEPARATOR
));
306 // The wxWindow destructor will take care of deleting the submenus.
309 // we should free Windows resources only if Windows doesn't do it for us
310 // which happens if we're attached to a menubar or a submenu of another
312 if ( !IsAttached() && !GetParent() )
314 if ( !::DestroyMenu(GetHmenu()) )
316 wxLogLastError(wxT("DestroyMenu"));
322 WX_CLEAR_ARRAY(m_accels
);
323 #endif // wxUSE_ACCEL
330 // this will take effect during the next call to Append()
336 int wxMenu::FindAccel(int id
) const
338 size_t n
, count
= m_accels
.GetCount();
339 for ( n
= 0; n
< count
; n
++ )
341 if ( m_accels
[n
]->m_command
== id
)
348 void wxMenu::UpdateAccel(wxMenuItem
*item
)
350 if ( item
->IsSubMenu() )
352 wxMenu
*submenu
= item
->GetSubMenu();
353 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
356 UpdateAccel(node
->GetData());
358 node
= node
->GetNext();
361 else if ( !item
->IsSeparator() )
363 // recurse upwards: we should only modify m_accels of the top level
364 // menus, not of the submenus as wxMenuBar doesn't look at them
365 // (alternative and arguable cleaner solution would be to recurse
366 // downwards in GetAccelCount() and CopyAccels())
369 GetParent()->UpdateAccel(item
);
373 // find the (new) accel for this item
374 wxAcceleratorEntry
*accel
= wxAcceleratorEntry::Create(item
->GetItemLabel());
376 accel
->m_command
= item
->GetId();
379 int n
= FindAccel(item
->GetId());
380 if ( n
== wxNOT_FOUND
)
382 // no old, add new if any
386 return; // skipping RebuildAccelTable() below
390 // replace old with new or just remove the old one if no new
395 m_accels
.RemoveAt(n
);
400 GetMenuBar()->RebuildAccelTable();
403 #if wxUSE_OWNER_DRAWN
404 ResetMaxAccelWidth();
407 //else: it is a separator, they can't have accels, nothing to do
410 #endif // wxUSE_ACCEL
415 // helper of DoInsertOrAppend(): returns the HBITMAP to use in MENUITEMINFO
416 HBITMAP
GetHBitmapForMenu(wxMenuItem
*pItem
, bool checked
= true)
418 // Under versions of Windows older than Vista we can't pass HBITMAP
419 // directly as hbmpItem for 2 reasons:
420 // 1. We can't draw it with transparency then (this is not
421 // very important now but would be with themed menu bg)
422 // 2. Worse, Windows inverts the bitmap for the selected
423 // item and this looks downright ugly
425 // So we prefer to instead draw it ourselves in MSWOnDrawItem().by using
426 // HBMMENU_CALLBACK when inserting it
428 // However under Vista using HBMMENU_CALLBACK causes the entire menu to be
429 // drawn using the classic theme instead of the current one and it does
430 // handle transparency just fine so do use the real bitmap there
432 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
434 #if wxUSE_OWNER_DRAWN
435 wxBitmap bmp
= pItem
->GetBitmap(checked
);
438 // we must use PARGB DIB for the menu bitmaps so ensure that we do
439 wxImage
img(bmp
.ConvertToImage());
440 if ( !img
.HasAlpha() )
443 pItem
->SetBitmap(img
, checked
);
446 return GetHbitmapOf(pItem
->GetBitmap(checked
));
448 #endif // wxUSE_OWNER_DRAWN
449 //else: bitmap is not set
453 #endif // wxUSE_IMAGE
455 return HBMMENU_CALLBACK
;
458 } // anonymous namespace
460 bool wxMenu::MSWGetRadioGroupRange(int pos
, int *start
, int *end
) const
462 return m_radioData
&& m_radioData
->GetGroupRange(pos
, start
, end
);
465 // append a new item or submenu to the menu
466 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
470 #endif // wxUSE_ACCEL
472 // we should support disabling the item even prior to adding it to the menu
473 UINT flags
= pItem
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
;
475 // if "Break" has just been called, insert a menu break before this item
476 // (and don't forget to reset the flag)
478 flags
|= MF_MENUBREAK
;
482 if ( pItem
->IsSeparator() ) {
483 flags
|= MF_SEPARATOR
;
486 // id is the numeric id for normal menu items and HMENU for submenus as
487 // required by ::AppendMenu() API
489 wxMenu
*submenu
= pItem
->GetSubMenu();
490 if ( submenu
!= NULL
) {
491 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
493 submenu
->SetParent(this);
495 id
= (UINT_PTR
)submenu
->GetHMenu();
500 id
= pItem
->GetMSWId();
504 // prepare to insert the item in the menu
505 wxString itemText
= pItem
->GetItemLabel();
506 LPCTSTR pData
= NULL
;
507 if ( pos
== (size_t)-1 )
509 // append at the end (note that the item is already appended to
510 // internal data structures)
511 pos
= GetMenuItemCount() - 1;
514 // Update radio groups data if we're inserting a new radio item.
516 // NB: If we supported inserting non-radio items in the middle of existing
517 // radio groups to break them into two subgroups, we'd need to update
518 // m_radioData in this case too but currently this is not supported.
519 bool checkInitially
= false;
520 if ( pItem
->GetKind() == wxITEM_RADIO
)
523 m_radioData
= new wxMenuRadioItemsData
;
525 if ( m_radioData
->UpdateOnInsert(pos
) )
526 checkInitially
= true;
529 // adjust position to account for the title of a popup menu, if any
530 if ( !GetMenuBar() && !m_title
.empty() )
531 pos
+= 2; // for the title itself and its separator
535 #if wxUSE_OWNER_DRAWN
536 // Under older systems mixing owner-drawn and non-owner-drawn items results
537 // in inconsistent margins, so we force this one to be owner-drawn if any
538 // other items already are.
540 pItem
->SetOwnerDrawn(true);
541 #endif // wxUSE_OWNER_DRAWN
543 // check if we have something more than a simple text item
544 #if wxUSE_OWNER_DRAWN
545 bool makeItemOwnerDrawn
= false;
546 if ( pItem
->IsOwnerDrawn() )
550 if ( !m_ownerDrawn
&& !pItem
->IsSeparator() )
552 // MIIM_BITMAP only works under WinME/2000+ so we always use owner
553 // drawn item under the previous versions and we also have to use
554 // them in any case if the item has custom colours or font
555 static const wxWinVersion winver
= wxGetWinVersion();
556 bool mustUseOwnerDrawn
= winver
< wxWinVersion_98
||
557 pItem
->GetTextColour().IsOk() ||
558 pItem
->GetBackgroundColour().IsOk() ||
559 pItem
->GetFont().IsOk();
561 if ( !mustUseOwnerDrawn
)
563 const wxBitmap
& bmpUnchecked
= pItem
->GetBitmap(false),
564 bmpChecked
= pItem
->GetBitmap(true);
566 if ( (bmpUnchecked
.IsOk() && IsGreaterThanStdSize(bmpUnchecked
)) ||
567 (bmpChecked
.IsOk() && IsGreaterThanStdSize(bmpChecked
)) )
569 mustUseOwnerDrawn
= true;
573 // use InsertMenuItem() if possible as it's guaranteed to look
574 // correct while our owner-drawn code is not
575 if ( !mustUseOwnerDrawn
)
577 WinStruct
<MENUITEMINFO
> mii
;
578 mii
.fMask
= MIIM_STRING
| MIIM_DATA
;
580 // don't set hbmpItem for the checkable items as it would
581 // be used for both checked and unchecked state
582 if ( pItem
->IsCheckable() )
584 mii
.fMask
|= MIIM_CHECKMARKS
;
585 mii
.hbmpChecked
= GetHBitmapForMenu(pItem
, true);
586 mii
.hbmpUnchecked
= GetHBitmapForMenu(pItem
, false);
588 else if ( pItem
->GetBitmap().IsOk() )
590 mii
.fMask
|= MIIM_BITMAP
;
591 mii
.hbmpItem
= GetHBitmapForMenu(pItem
);
594 mii
.cch
= itemText
.length();
595 mii
.dwTypeData
= wxMSW_CONV_LPTSTR(itemText
);
597 if ( flags
& MF_POPUP
)
599 mii
.fMask
|= MIIM_SUBMENU
;
600 mii
.hSubMenu
= GetHmenuOf(pItem
->GetSubMenu());
604 mii
.fMask
|= MIIM_ID
;
608 mii
.dwItemData
= reinterpret_cast<ULONG_PTR
>(pItem
);
610 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
613 wxLogLastError(wxT("InsertMenuItem()"));
615 else // InsertMenuItem() ok
617 // we need to remove the extra indent which is reserved for
618 // the checkboxes by default as it looks ugly unless check
619 // boxes are used together with bitmaps and this is not the
621 WinStruct
<MENUINFO
> mi
;
623 // don't call SetMenuInfo() directly, this would prevent
624 // the app from starting up under Windows 95/NT 4
625 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
627 wxDynamicLibrary
dllUser(wxT("user32"));
628 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
629 if ( pfnSetMenuInfo
)
631 mi
.fMask
= MIM_STYLE
;
632 mi
.dwStyle
= MNS_CHECKORBMP
;
633 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
635 wxLogLastError(wxT("SetMenuInfo(MNS_NOCHECK)"));
639 // tell the item that it's not really owner-drawn but only
640 // needs to draw its bitmap, the rest is done by Windows
641 pItem
->SetOwnerDrawn(false);
649 // item draws itself, pass pointer to it in data parameter
650 flags
|= MF_OWNERDRAW
;
651 pData
= (LPCTSTR
)pItem
;
653 bool updateAllMargins
= false;
655 // get size of bitmap always return valid value (0 for invalid bitmap),
656 // so we don't needed check if bitmap is valid ;)
657 int uncheckedW
= pItem
->GetBitmap(false).GetWidth();
658 int checkedW
= pItem
->GetBitmap(true).GetWidth();
660 if ( m_maxBitmapWidth
< uncheckedW
)
662 m_maxBitmapWidth
= uncheckedW
;
663 updateAllMargins
= true;
666 if ( m_maxBitmapWidth
< checkedW
)
668 m_maxBitmapWidth
= checkedW
;
669 updateAllMargins
= true;
672 // make other item ownerdrawn and update margin width for equals alignment
673 if ( !m_ownerDrawn
|| updateAllMargins
)
675 // we must use position in SetOwnerDrawnMenuItem because
676 // all separators have the same id
678 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
681 wxMenuItem
* item
= node
->GetData();
683 if ( !item
->IsOwnerDrawn())
685 item
->SetOwnerDrawn(true);
686 SetOwnerDrawnMenuItem(GetHmenu(), pos
,
687 reinterpret_cast<ULONG_PTR
>(item
), TRUE
);
690 item
->SetMarginWidth(m_maxBitmapWidth
);
692 node
= node
->GetNext();
696 // set menu as ownerdrawn
699 // also ensure that the new item itself is made owner drawn
700 makeItemOwnerDrawn
= true;
702 ResetMaxAccelWidth();
704 // only update our margin for equals alignment to other item
705 else if ( !updateAllMargins
)
707 pItem
->SetMarginWidth(m_maxBitmapWidth
);
712 #endif // wxUSE_OWNER_DRAWN
714 // item is just a normal string (passed in data parameter)
718 itemText
= wxMenuItem::GetLabelText(itemText
);
721 pData
= itemText
.t_str();
724 // item might have already been inserted by InsertMenuItem() above
727 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
729 wxLogLastError(wxT("InsertMenu[Item]()"));
734 #if wxUSE_OWNER_DRAWN
735 if ( makeItemOwnerDrawn
)
737 SetOwnerDrawnMenuItem(GetHmenu(), pos
,
738 reinterpret_cast<ULONG_PTR
>(pItem
), TRUE
);
744 // Check the item if it should be initially checked.
745 if ( checkInitially
)
748 // if we just appended the title, highlight it
749 if ( id
== (UINT_PTR
)idMenuTitle
)
751 // visually select the menu title
752 SetDefaultMenuItem(GetHmenu(), id
);
755 // if we're already attached to the menubar, we must update it
756 if ( IsAttached() && GetMenuBar()->IsAttached() )
758 GetMenuBar()->Refresh();
764 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
766 return wxMenuBase::DoAppend(item
) && DoInsertOrAppend(item
) ? item
: NULL
;
769 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
771 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
777 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
779 // we need to find the item's position in the child list
781 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
782 for ( pos
= 0; node
; pos
++ )
784 if ( node
->GetData() == item
)
787 node
= node
->GetNext();
790 // DoRemove() (unlike Remove) can only be called for an existing item!
791 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
794 // remove the corresponding accel from the accel table
795 int n
= FindAccel(item
->GetId());
796 if ( n
!= wxNOT_FOUND
)
800 m_accels
.RemoveAt(n
);
802 #if wxUSE_OWNER_DRAWN
803 ResetMaxAccelWidth();
806 //else: this item doesn't have an accel, nothing to do
807 #endif // wxUSE_ACCEL
809 // remove the item from the menu
810 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
812 wxLogLastError(wxT("RemoveMenu"));
815 if ( IsAttached() && GetMenuBar()->IsAttached() )
817 // otherwise, the change won't be visible
818 GetMenuBar()->Refresh();
821 // and from internal data structures
822 return wxMenuBase::DoRemove(item
);
825 // ---------------------------------------------------------------------------
826 // accelerator helpers
827 // ---------------------------------------------------------------------------
831 // create the wxAcceleratorEntries for our accels and put them into the provided
832 // array - return the number of accels we have
833 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
835 size_t count
= GetAccelCount();
836 for ( size_t n
= 0; n
< count
; n
++ )
838 *accels
++ = *m_accels
[n
];
844 wxAcceleratorTable
*wxMenu::CreateAccelTable() const
846 const size_t count
= m_accels
.size();
847 wxScopedArray
<wxAcceleratorEntry
> accels(new wxAcceleratorEntry
[count
]);
848 CopyAccels(accels
.get());
850 return new wxAcceleratorTable(count
, accels
.get());
853 #endif // wxUSE_ACCEL
855 // ---------------------------------------------------------------------------
856 // ownerdrawn helpers
857 // ---------------------------------------------------------------------------
859 #if wxUSE_OWNER_DRAWN
861 void wxMenu::CalculateMaxAccelWidth()
863 wxASSERT_MSG( m_maxAccelWidth
== -1, wxT("it's really needed?") );
865 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
868 wxMenuItem
* item
= node
->GetData();
870 if ( item
->IsOwnerDrawn() )
872 int width
= item
->MeasureAccelWidth();
873 if (width
> m_maxAccelWidth
)
874 m_maxAccelWidth
= width
;
877 node
= node
->GetNext();
881 #endif // wxUSE_OWNER_DRAWN
883 // ---------------------------------------------------------------------------
885 // ---------------------------------------------------------------------------
887 void wxMenu::SetTitle(const wxString
& label
)
889 bool hasNoTitle
= m_title
.empty();
892 HMENU hMenu
= GetHmenu();
896 if ( !label
.empty() )
898 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
899 (UINT_PTR
)idMenuTitle
, m_title
.t_str()) ||
900 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
902 wxLogLastError(wxT("InsertMenu"));
910 // remove the title and the separator after it
911 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
912 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
914 wxLogLastError(wxT("RemoveMenu"));
921 WinStruct
<MENUITEMINFO
> info
;
922 info
.fMask
= MIIM_TYPE
;
923 info
.fType
= MFT_STRING
;
924 info
.cch
= m_title
.length();
925 info
.dwTypeData
= wxMSW_CONV_LPTSTR(m_title
);
926 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
928 wxLogLastError(wxT("SetMenuItemInfo"));
931 if ( !ModifyMenu(hMenu
, 0u,
932 MF_BYPOSITION
| MF_STRING
,
933 (UINT_PTR
)idMenuTitle
, m_title
.t_str()) )
935 wxLogLastError(wxT("ModifyMenu"));
942 // put the title string in bold face
943 if ( !m_title
.empty() )
945 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
950 // ---------------------------------------------------------------------------
952 // ---------------------------------------------------------------------------
954 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
956 const int id
= (signed short)id_
;
958 // ignore commands from the menu title
959 if ( id
!= idMenuTitle
)
961 // Default value for uncheckable items.
964 // update the check item when it's clicked
965 wxMenuItem
* const item
= FindItem(id
);
966 if ( item
&& item
->IsCheckable() )
970 // Get the status of the menu item: note that it has been just changed
971 // by Toggle() above so here we already get the new state of the item.
973 // Also notice that we must pass unsigned id_ and not sign-extended id
974 // to ::GetMenuState() as this is what it expects.
975 UINT menuState
= ::GetMenuState(GetHmenu(), id_
, MF_BYCOMMAND
);
976 checked
= (menuState
& MF_CHECKED
) != 0;
979 SendEvent(id
, checked
);
985 // get the menu with given handle (recursively)
986 #if wxUSE_OWNER_DRAWN
987 wxMenu
* wxMenu::MSWGetMenu(WXHMENU hMenu
)
990 if ( GetHMenu() == hMenu
)
993 // recursively query submenus
994 for ( size_t n
= 0 ; n
< GetMenuItemCount(); ++n
)
996 wxMenuItem
* item
= FindItemByPosition(n
);
997 wxMenu
* submenu
= item
->GetSubMenu();
1000 submenu
= submenu
->MSWGetMenu(hMenu
);
1009 #endif // wxUSE_OWNER_DRAWN
1011 // ---------------------------------------------------------------------------
1013 // ---------------------------------------------------------------------------
1015 void wxMenuBar::Init()
1017 m_eventHandler
= this;
1019 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
1022 // Not using a combined wxToolBar/wxMenuBar? then use
1023 // a commandbar in WinCE .NET just to implement the
1025 #if defined(WINCE_WITH_COMMANDBAR)
1026 m_commandBar
= NULL
;
1027 m_adornmentsAdded
= false;
1031 wxMenuBar::wxMenuBar()
1036 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
1041 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
1045 for ( size_t i
= 0; i
< count
; i
++ )
1047 // We just want to store the menu title in the menu itself, not to
1048 // show it as a dummy item in the menu itself as we do with the popup
1049 // menu titles in overridden wxMenu::SetTitle().
1050 menus
[i
]->wxMenuBase::SetTitle(titles
[i
]);
1051 m_menus
.Append(menus
[i
]);
1053 menus
[i
]->Attach(this);
1057 wxMenuBar::~wxMenuBar()
1059 // In Windows CE (not .NET), the menubar is always associated
1060 // with a toolbar, which destroys the menu implicitly.
1061 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
1064 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
1066 toolMenuBar
->SetMenuBar(NULL
);
1069 // we should free Windows resources only if Windows doesn't do it for us
1070 // which happens if we're attached to a frame
1071 if (m_hMenu
&& !IsAttached())
1073 #if defined(WINCE_WITH_COMMANDBAR)
1074 ::DestroyWindow((HWND
) m_commandBar
);
1075 m_commandBar
= (WXHWND
) NULL
;
1077 ::DestroyMenu((HMENU
)m_hMenu
);
1079 m_hMenu
= (WXHMENU
)NULL
;
1084 // ---------------------------------------------------------------------------
1085 // wxMenuBar helpers
1086 // ---------------------------------------------------------------------------
1088 void wxMenuBar::Refresh()
1093 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
1095 #if defined(WINCE_WITHOUT_COMMANDBAR)
1098 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
1100 #elif defined(WINCE_WITH_COMMANDBAR)
1102 DrawMenuBar((HWND
) m_commandBar
);
1104 DrawMenuBar(GetHwndOf(GetFrame()));
1108 WXHMENU
wxMenuBar::Create()
1110 // Note: this doesn't work at all on Smartphone,
1111 // since you have to use resources.
1112 // We'll have to find another way to add a menu
1113 // by changing/adding menu items to an existing menu.
1114 #if defined(WINCE_WITHOUT_COMMANDBAR)
1118 wxToolMenuBar
* const bar
= static_cast<wxToolMenuBar
*>(GetToolBar());
1122 HWND hCommandBar
= GetHwndOf(bar
);
1124 // notify comctl32.dll about the version of the headers we use before using
1125 // any other TB_XXX messages
1126 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
1129 wxZeroMemory(tbButton
);
1130 tbButton
.iBitmap
= I_IMAGENONE
;
1131 tbButton
.fsState
= TBSTATE_ENABLED
;
1132 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
1133 TBSTYLE_NO_DROPDOWN_ARROW
|
1136 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
1138 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
1139 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1140 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
1141 tbButton
.iString
= (int) wxMSW_CONV_LPCTSTR(label
);
1143 tbButton
.idCommand
= NewControlId();
1144 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
1146 wxLogLastError(wxT("TB_INSERTBUTTON"));
1150 m_hMenu
= bar
->GetHMenu();
1152 #else // !__WXWINCE__
1156 m_hMenu
= (WXHMENU
)::CreateMenu();
1160 wxLogLastError(wxT("CreateMenu"));
1164 for ( wxMenuList::iterator it
= m_menus
.begin();
1165 it
!= m_menus
.end();
1168 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
1169 (UINT_PTR
)(*it
)->GetHMenu(),
1170 (*it
)->GetTitle().t_str()) )
1172 wxLogLastError(wxT("AppendMenu"));
1178 #endif // __WXWINCE__/!__WXWINCE__
1181 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
1184 wxASSERT(menu
->GetHMenu());
1187 #if defined(__WXWINCE__)
1188 int totalMSWItems
= GetMenuCount();
1190 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1193 int i
; // For old C++ compatibility
1194 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1196 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1199 for(i
=0; i
<wxpos
; i
++)
1201 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1208 // ---------------------------------------------------------------------------
1209 // wxMenuBar functions to work with the top level submenus
1210 // ---------------------------------------------------------------------------
1212 // NB: we don't support owner drawn top level items for now, if we do these
1213 // functions would have to be changed to use wxMenuItem as well
1215 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1217 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1218 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1220 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1222 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1227 bool wxMenuBar::IsEnabledTop(size_t pos
) const
1229 wxCHECK_MSG( pos
< GetMenuCount(), false, wxS("invalid menu index") );
1230 WinStruct
<MENUITEMINFO
> mii
;
1231 mii
.fMask
= MIIM_STATE
;
1232 if ( !::GetMenuItemInfo(GetHmenu(), pos
, TRUE
, &mii
) )
1234 wxLogLastError(wxS("GetMenuItemInfo(menubar)"));
1237 return !(mii
.fState
& MFS_GRAYED
);
1240 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1242 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1244 m_menus
[pos
]->wxMenuBase::SetTitle(label
);
1246 if ( !IsAttached() )
1250 //else: have to modify the existing menu
1252 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1255 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1256 if ( flagsOld
== 0xFFFFFFFF )
1258 wxLogLastError(wxT("GetMenuState"));
1263 if ( flagsOld
& MF_POPUP
)
1265 // HIBYTE contains the number of items in the submenu in this case
1267 id
= (UINT_PTR
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1275 WinStruct
<MENUITEMINFO
> info
;
1276 info
.fMask
= MIIM_TYPE
;
1277 info
.fType
= MFT_STRING
;
1278 info
.cch
= label
.length();
1279 info
.dwTypeData
= wxMSW_CONV_LPTSTR(label
);
1280 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, &info
) )
1282 wxLogLastError(wxT("SetMenuItemInfo"));
1286 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1287 id
, label
.t_str()) == (int)0xFFFFFFFF )
1289 wxLogLastError(wxT("ModifyMenu"));
1296 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1298 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1299 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1301 return m_menus
[pos
]->GetTitle();
1304 // ---------------------------------------------------------------------------
1305 // wxMenuBar construction
1306 // ---------------------------------------------------------------------------
1308 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1310 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1314 menu
->wxMenuBase::SetTitle(title
);
1316 #if defined(WINCE_WITHOUT_COMMANDBAR)
1322 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1324 // can't use ModifyMenu() because it deletes the submenu it replaces
1325 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1327 wxLogLastError(wxT("RemoveMenu"));
1330 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1331 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1332 (UINT_PTR
)GetHmenuOf(menu
), title
.t_str()) )
1334 wxLogLastError(wxT("InsertMenu"));
1338 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1340 // need to rebuild accell table
1341 RebuildAccelTable();
1343 #endif // wxUSE_ACCEL
1352 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1354 // Find out which MSW item before which we'll be inserting before
1355 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1356 // If IsAttached() is false this won't be used anyway
1358 #if defined(WINCE_WITHOUT_COMMANDBAR)
1364 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1365 ? -1 // append the menu
1366 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1368 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1371 menu
->wxMenuBase::SetTitle(title
);
1375 #if defined(WINCE_WITHOUT_COMMANDBAR)
1379 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1380 tbButton
.iBitmap
= I_IMAGENONE
;
1381 tbButton
.fsState
= TBSTATE_ENABLED
;
1382 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1384 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1385 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1386 wxString label
= wxStripMenuCodes(title
);
1387 tbButton
.iString
= (int) wxMSW_CONV_LPCTSTR(label
);
1389 tbButton
.idCommand
= NewControlId();
1390 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1392 wxLogLastError(wxT("TB_INSERTBUTTON"));
1395 wxUnusedVar(mswpos
);
1397 if ( !::InsertMenu(GetHmenu(), mswpos
,
1398 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1399 (UINT_PTR
)GetHmenuOf(menu
), title
.t_str()) )
1401 wxLogLastError(wxT("InsertMenu"));
1405 if ( menu
->HasAccels() )
1407 // need to rebuild accell table
1408 RebuildAccelTable();
1410 #endif // wxUSE_ACCEL
1419 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1421 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1422 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1424 if ( !wxMenuBarBase::Append(menu
, title
) )
1427 menu
->wxMenuBase::SetTitle(title
);
1429 #if defined(WINCE_WITHOUT_COMMANDBAR)
1435 #if defined(WINCE_WITHOUT_COMMANDBAR)
1439 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1440 tbButton
.iBitmap
= I_IMAGENONE
;
1441 tbButton
.fsState
= TBSTATE_ENABLED
;
1442 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1444 size_t pos
= GetMenuCount();
1445 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1446 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1447 wxString label
= wxStripMenuCodes(title
);
1448 tbButton
.iString
= (int) wxMSW_CONV_LPCTSTR(label
);
1450 tbButton
.idCommand
= NewControlId();
1451 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1453 wxLogLastError(wxT("TB_INSERTBUTTON"));
1457 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1458 (UINT_PTR
)submenu
, title
.t_str()) )
1460 wxLogLastError(wxT("AppendMenu"));
1465 if ( menu
->HasAccels() )
1467 // need to rebuild accelerator table
1468 RebuildAccelTable();
1470 #endif // wxUSE_ACCEL
1479 wxMenu
*wxMenuBar::Remove(size_t pos
)
1481 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1485 #if defined(WINCE_WITHOUT_COMMANDBAR)
1491 #if defined(WINCE_WITHOUT_COMMANDBAR)
1494 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1496 wxLogLastError(wxT("TB_DELETEBUTTON"));
1500 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1502 wxLogLastError(wxT("RemoveMenu"));
1507 if ( menu
->HasAccels() )
1509 // need to rebuild accell table
1510 RebuildAccelTable();
1512 #endif // wxUSE_ACCEL
1523 void wxMenuBar::RebuildAccelTable()
1525 // merge the accelerators of all menus into one accel table
1526 size_t nAccelCount
= 0;
1527 size_t i
, count
= GetMenuCount();
1528 wxMenuList::iterator it
;
1529 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1531 nAccelCount
+= (*it
)->GetAccelCount();
1536 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1539 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1541 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1544 SetAcceleratorTable(wxAcceleratorTable(nAccelCount
, accelEntries
));
1546 delete [] accelEntries
;
1548 else // No (more) accelerators.
1550 SetAcceleratorTable(wxAcceleratorTable());
1554 #endif // wxUSE_ACCEL
1556 void wxMenuBar::Attach(wxFrame
*frame
)
1558 wxMenuBarBase::Attach(frame
);
1560 #if defined(WINCE_WITH_COMMANDBAR)
1564 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1569 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1571 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1578 RebuildAccelTable();
1579 #endif // wxUSE_ACCEL
1582 #if defined(WINCE_WITH_COMMANDBAR)
1583 bool wxMenuBar::AddAdornments(long style
)
1585 if (m_adornmentsAdded
|| !m_commandBar
)
1588 if (style
& wxCLOSE_BOX
)
1590 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1592 wxLogLastError(wxT("CommandBar_AddAdornments"));
1603 void wxMenuBar::Detach()
1605 wxMenuBarBase::Detach();
1608 // get the menu with given handle (recursively)
1609 wxMenu
* wxMenuBar::MSWGetMenu(WXHMENU hMenu
)
1611 wxCHECK_MSG( GetHMenu() != hMenu
, NULL
,
1612 wxT("wxMenuBar::MSWGetMenu(): menu handle is wxMenuBar, not wxMenu") );
1614 #if wxUSE_OWNER_DRAWN
1616 for ( size_t n
= 0 ; n
< GetMenuCount(); ++n
)
1618 wxMenu
* menu
= GetMenu(n
)->MSWGetMenu(hMenu
);
1628 #endif // wxUSE_MENUS