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();
411 ResetMaxAccelWidth();
414 //else: it is a separator, they can't have accels, nothing to do
417 #endif // wxUSE_ACCEL
422 // helper of DoInsertOrAppend(): returns the HBITMAP to use in MENUITEMINFO
423 HBITMAP
GetHBitmapForMenu(wxMenuItem
*pItem
, bool checked
= true)
425 // Under versions of Windows older than Vista we can't pass HBITMAP
426 // directly as hbmpItem for 2 reasons:
427 // 1. We can't draw it with transparency then (this is not
428 // very important now but would be with themed menu bg)
429 // 2. Worse, Windows inverts the bitmap for the selected
430 // item and this looks downright ugly
432 // So we prefer to instead draw it ourselves in MSWOnDrawItem().by using
433 // HBMMENU_CALLBACK when inserting it
435 // However under Vista using HBMMENU_CALLBACK causes the entire menu to be
436 // drawn using the classic theme instead of the current one and it does
437 // handle transparency just fine so do use the real bitmap there
439 if ( wxGetWinVersion() >= wxWinVersion_Vista
)
442 wxBitmap bmp
= pItem
->GetBitmap(checked
);
445 // we must use PARGB DIB for the menu bitmaps so ensure that we do
446 wxImage
img(bmp
.ConvertToImage());
447 if ( !img
.HasAlpha() )
450 pItem
->SetBitmap(img
, checked
);
453 return GetHbitmapOf(pItem
->GetBitmap(checked
));
455 #endif // wxUSE_OWNER_DRAW
456 //else: bitmap is not set
460 #endif // wxUSE_IMAGE
462 return HBMMENU_CALLBACK
;
465 } // anonymous namespace
467 bool wxMenu::MSWGetRadioGroupRange(int pos
, int *start
, int *end
) const
469 return m_radioData
&& m_radioData
->GetGroupRange(pos
, start
, end
);
472 // append a new item or submenu to the menu
473 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
477 #endif // wxUSE_ACCEL
479 // we should support disabling the item even prior to adding it to the menu
480 UINT flags
= pItem
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
;
482 // if "Break" has just been called, insert a menu break before this item
483 // (and don't forget to reset the flag)
485 flags
|= MF_MENUBREAK
;
489 if ( pItem
->IsSeparator() ) {
490 flags
|= MF_SEPARATOR
;
493 // id is the numeric id for normal menu items and HMENU for submenus as
494 // required by ::AppendMenu() API
496 wxMenu
*submenu
= pItem
->GetSubMenu();
497 if ( submenu
!= NULL
) {
498 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
500 submenu
->SetParent(this);
502 id
= (UINT_PTR
)submenu
->GetHMenu();
507 id
= pItem
->GetMSWId();
511 // prepare to insert the item in the menu
512 wxString itemText
= pItem
->GetItemLabel();
513 LPCTSTR pData
= NULL
;
514 if ( pos
== (size_t)-1 )
516 // append at the end (note that the item is already appended to
517 // internal data structures)
518 pos
= GetMenuItemCount() - 1;
521 // Update radio groups data if we're inserting a new radio item.
523 // NB: If we supported inserting non-radio items in the middle of existing
524 // radio groups to break them into two subgroups, we'd need to update
525 // m_radioData in this case too but currently this is not supported.
526 bool checkInitially
= false;
527 if ( pItem
->GetKind() == wxITEM_RADIO
)
530 m_radioData
= new wxMenuRadioItemsData
;
532 if ( m_radioData
->UpdateOnInsert(pos
) )
533 checkInitially
= true;
536 // adjust position to account for the title of a popup menu, if any
537 if ( !GetMenuBar() && !m_title
.empty() )
538 pos
+= 2; // for the title itself and its separator
542 #if wxUSE_OWNER_DRAWN
543 // Under older systems mixing owner-drawn and non-owner-drawn items results
544 // in inconsistent margins, so we force this one to be owner-drawn if any
545 // other items already are.
547 pItem
->SetOwnerDrawn(true);
548 #endif // wxUSE_OWNER_DRAWN
550 // check if we have something more than a simple text item
551 #if wxUSE_OWNER_DRAWN
552 if ( pItem
->IsOwnerDrawn() )
556 if ( !m_ownerDrawn
&& !pItem
->IsSeparator() )
558 // MIIM_BITMAP only works under WinME/2000+ so we always use owner
559 // drawn item under the previous versions and we also have to use
560 // them in any case if the item has custom colours or font
561 static const wxWinVersion winver
= wxGetWinVersion();
562 bool mustUseOwnerDrawn
= winver
< wxWinVersion_98
||
563 pItem
->GetTextColour().IsOk() ||
564 pItem
->GetBackgroundColour().IsOk() ||
565 pItem
->GetFont().IsOk();
567 if ( !mustUseOwnerDrawn
)
569 const wxBitmap
& bmpUnchecked
= pItem
->GetBitmap(false),
570 bmpChecked
= pItem
->GetBitmap(true);
572 if ( (bmpUnchecked
.IsOk() && IsGreaterThanStdSize(bmpUnchecked
)) ||
573 (bmpChecked
.IsOk() && IsGreaterThanStdSize(bmpChecked
)) )
575 mustUseOwnerDrawn
= true;
579 // use InsertMenuItem() if possible as it's guaranteed to look
580 // correct while our owner-drawn code is not
581 if ( !mustUseOwnerDrawn
)
583 WinStruct
<MENUITEMINFO
> mii
;
584 mii
.fMask
= MIIM_STRING
| MIIM_DATA
;
586 // don't set hbmpItem for the checkable items as it would
587 // be used for both checked and unchecked state
588 if ( pItem
->IsCheckable() )
590 mii
.fMask
|= MIIM_CHECKMARKS
;
591 mii
.hbmpChecked
= GetHBitmapForMenu(pItem
, true);
592 mii
.hbmpUnchecked
= GetHBitmapForMenu(pItem
, false);
594 else if ( pItem
->GetBitmap().IsOk() )
596 mii
.fMask
|= MIIM_BITMAP
;
597 mii
.hbmpItem
= GetHBitmapForMenu(pItem
);
600 mii
.cch
= itemText
.length();
601 mii
.dwTypeData
= const_cast<wxChar
*>(itemText
.wx_str());
603 if ( flags
& MF_POPUP
)
605 mii
.fMask
|= MIIM_SUBMENU
;
606 mii
.hSubMenu
= GetHmenuOf(pItem
->GetSubMenu());
610 mii
.fMask
|= MIIM_ID
;
614 mii
.dwItemData
= reinterpret_cast<ULONG_PTR
>(pItem
);
616 ok
= ::InsertMenuItem(GetHmenu(), pos
, TRUE
/* by pos */, &mii
);
619 wxLogLastError(wxT("InsertMenuItem()"));
621 else // InsertMenuItem() ok
623 // we need to remove the extra indent which is reserved for
624 // the checkboxes by default as it looks ugly unless check
625 // boxes are used together with bitmaps and this is not the
627 WinStruct
<MENUINFO
> mi
;
629 // don't call SetMenuInfo() directly, this would prevent
630 // the app from starting up under Windows 95/NT 4
631 typedef BOOL (WINAPI
*SetMenuInfo_t
)(HMENU
, MENUINFO
*);
633 wxDynamicLibrary
dllUser(wxT("user32"));
634 wxDYNLIB_FUNCTION(SetMenuInfo_t
, SetMenuInfo
, dllUser
);
635 if ( pfnSetMenuInfo
)
637 mi
.fMask
= MIM_STYLE
;
638 mi
.dwStyle
= MNS_CHECKORBMP
;
639 if ( !(*pfnSetMenuInfo
)(GetHmenu(), &mi
) )
641 wxLogLastError(wxT("SetMenuInfo(MNS_NOCHECK)"));
645 // tell the item that it's not really owner-drawn but only
646 // needs to draw its bitmap, the rest is done by Windows
647 pItem
->SetOwnerDrawn(false);
655 // item draws itself, pass pointer to it in data parameter
656 flags
|= MF_OWNERDRAW
;
657 pData
= (LPCTSTR
)pItem
;
659 bool updateAllMargins
= false;
661 // get size of bitmap always return valid value (0 for invalid bitmap),
662 // so we don't needed check if bitmap is valid ;)
663 int uncheckedW
= pItem
->GetBitmap(false).GetWidth();
664 int checkedW
= pItem
->GetBitmap(true).GetWidth();
666 if ( m_maxBitmapWidth
< uncheckedW
)
668 m_maxBitmapWidth
= uncheckedW
;
669 updateAllMargins
= true;
672 if ( m_maxBitmapWidth
< checkedW
)
674 m_maxBitmapWidth
= checkedW
;
675 updateAllMargins
= true;
678 // make other item ownerdrawn and update margin width for equals alignment
679 if ( !m_ownerDrawn
|| updateAllMargins
)
681 // we must use position in SetOwnerDrawnMenuItem because
682 // all separators have the same id
684 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
687 wxMenuItem
* item
= node
->GetData();
689 if ( !item
->IsOwnerDrawn())
691 item
->SetOwnerDrawn(true);
692 SetOwnerDrawnMenuItem(GetHmenu(), pos
,
693 reinterpret_cast<ULONG_PTR
>(item
), TRUE
);
696 item
->SetMarginWidth(m_maxBitmapWidth
);
698 node
= node
->GetNext();
702 // set menu as ownerdrawn
705 ResetMaxAccelWidth();
707 // only update our margin for equals alignment to other item
708 else if ( !updateAllMargins
)
710 pItem
->SetMarginWidth(m_maxBitmapWidth
);
715 #endif // wxUSE_OWNER_DRAWN
717 // item is just a normal string (passed in data parameter)
721 itemText
= wxMenuItem::GetLabelText(itemText
);
724 pData
= (wxChar
*)itemText
.wx_str();
727 // item might have already been inserted by InsertMenuItem() above
730 if ( !::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
) )
732 wxLogLastError(wxT("InsertMenu[Item]()"));
739 // Check the item if it should be initially checked.
740 if ( checkInitially
)
743 // if we just appended the title, highlight it
744 if ( id
== (UINT_PTR
)idMenuTitle
)
746 // visually select the menu title
747 SetDefaultMenuItem(GetHmenu(), id
);
750 // if we're already attached to the menubar, we must update it
751 if ( IsAttached() && GetMenuBar()->IsAttached() )
753 GetMenuBar()->Refresh();
759 wxMenuItem
* wxMenu::DoAppend(wxMenuItem
*item
)
761 return wxMenuBase::DoAppend(item
) && DoInsertOrAppend(item
) ? item
: NULL
;
764 wxMenuItem
* wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
766 if (wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
))
772 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
774 // we need to find the item's position in the child list
776 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
777 for ( pos
= 0; node
; pos
++ )
779 if ( node
->GetData() == item
)
782 node
= node
->GetNext();
785 // DoRemove() (unlike Remove) can only be called for an existing item!
786 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
789 // remove the corresponding accel from the accel table
790 int n
= FindAccel(item
->GetId());
791 if ( n
!= wxNOT_FOUND
)
795 m_accels
.RemoveAt(n
);
798 ResetMaxAccelWidth();
801 //else: this item doesn't have an accel, nothing to do
802 #endif // wxUSE_ACCEL
804 // remove the item from the menu
805 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
807 wxLogLastError(wxT("RemoveMenu"));
810 if ( IsAttached() && GetMenuBar()->IsAttached() )
812 // otherwise, the change won't be visible
813 GetMenuBar()->Refresh();
816 // and from internal data structures
817 return wxMenuBase::DoRemove(item
);
820 // ---------------------------------------------------------------------------
821 // accelerator helpers
822 // ---------------------------------------------------------------------------
826 // create the wxAcceleratorEntries for our accels and put them into the provided
827 // array - return the number of accels we have
828 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
830 size_t count
= GetAccelCount();
831 for ( size_t n
= 0; n
< count
; n
++ )
833 *accels
++ = *m_accels
[n
];
839 wxAcceleratorTable
*wxMenu::CreateAccelTable() const
841 const size_t count
= m_accels
.size();
842 wxScopedArray
<wxAcceleratorEntry
> accels(new wxAcceleratorEntry
[count
]);
843 CopyAccels(accels
.get());
845 return new wxAcceleratorTable(count
, accels
.get());
848 #endif // wxUSE_ACCEL
850 // ---------------------------------------------------------------------------
851 // ownerdrawn helpers
852 // ---------------------------------------------------------------------------
854 #if wxUSE_OWNER_DRAWN
856 void wxMenu::CalculateMaxAccelWidth()
858 wxASSERT_MSG( m_maxAccelWidth
== -1, wxT("it's really needed?") );
860 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
863 wxMenuItem
* item
= node
->GetData();
865 if ( item
->IsOwnerDrawn() )
867 int width
= item
->MeasureAccelWidth();
868 if (width
> m_maxAccelWidth
)
869 m_maxAccelWidth
= width
;
872 node
= node
->GetNext();
876 #endif // wxUSE_OWNER_DRAWN
878 // ---------------------------------------------------------------------------
880 // ---------------------------------------------------------------------------
882 void wxMenu::SetTitle(const wxString
& label
)
884 bool hasNoTitle
= m_title
.empty();
887 HMENU hMenu
= GetHmenu();
891 if ( !label
.empty() )
893 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
894 (UINT_PTR
)idMenuTitle
, m_title
.wx_str()) ||
895 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
897 wxLogLastError(wxT("InsertMenu"));
905 // remove the title and the separator after it
906 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
907 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
909 wxLogLastError(wxT("RemoveMenu"));
918 info
.cbSize
= sizeof(info
);
919 info
.fMask
= MIIM_TYPE
;
920 info
.fType
= MFT_STRING
;
921 info
.cch
= m_title
.length();
922 info
.dwTypeData
= const_cast<wxChar
*>(m_title
.wx_str());
923 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
925 wxLogLastError(wxT("SetMenuItemInfo"));
928 if ( !ModifyMenu(hMenu
, 0u,
929 MF_BYPOSITION
| MF_STRING
,
930 (UINT_PTR
)idMenuTitle
, m_title
.wx_str()) )
932 wxLogLastError(wxT("ModifyMenu"));
939 // put the title string in bold face
940 if ( !m_title
.empty() )
942 SetDefaultMenuItem(GetHmenu(), (UINT_PTR
)idMenuTitle
);
947 // ---------------------------------------------------------------------------
949 // ---------------------------------------------------------------------------
951 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id_
)
953 const int id
= (signed short)id_
;
955 // ignore commands from the menu title
956 if ( id
!= idMenuTitle
)
958 // Default value for uncheckable items.
961 // update the check item when it's clicked
962 wxMenuItem
* const item
= FindItem(id
);
963 if ( item
&& item
->IsCheckable() )
967 // Get the status of the menu item: note that it has been just changed
968 // by Toggle() above so here we already get the new state of the item.
970 // Also notice that we must pass unsigned id_ and not sign-extended id
971 // to ::GetMenuState() as this is what it expects.
972 UINT menuState
= ::GetMenuState(GetHmenu(), id_
, MF_BYCOMMAND
);
973 checked
= (menuState
& MF_CHECKED
) != 0;
976 SendEvent(id
, checked
);
982 // get the menu with given handle (recursively)
984 wxMenu
* wxMenu::MSWGetMenu(WXHMENU hMenu
)
987 if ( GetHMenu() == hMenu
)
990 // recursively query submenus
991 for ( size_t n
= 0 ; n
< GetMenuItemCount(); ++n
)
993 wxMenuItem
* item
= FindItemByPosition(n
);
994 wxMenu
* submenu
= item
->GetSubMenu();
997 submenu
= submenu
->MSWGetMenu(hMenu
);
1006 #endif // wxUSE_OWNER_DRAW
1008 // ---------------------------------------------------------------------------
1010 // ---------------------------------------------------------------------------
1012 void wxMenuBar::Init()
1014 m_eventHandler
= this;
1016 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
1019 // Not using a combined wxToolBar/wxMenuBar? then use
1020 // a commandbar in WinCE .NET just to implement the
1022 #if defined(WINCE_WITH_COMMANDBAR)
1023 m_commandBar
= NULL
;
1024 m_adornmentsAdded
= false;
1028 wxMenuBar::wxMenuBar()
1033 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
1038 wxMenuBar::wxMenuBar(size_t count
, wxMenu
*menus
[], const wxString titles
[], long WXUNUSED(style
))
1042 for ( size_t i
= 0; i
< count
; i
++ )
1044 // We just want to store the menu title in the menu itself, not to
1045 // show it as a dummy item in the menu itself as we do with the popup
1046 // menu titles in overridden wxMenu::SetTitle().
1047 menus
[i
]->wxMenuBase::SetTitle(titles
[i
]);
1048 m_menus
.Append(menus
[i
]);
1050 menus
[i
]->Attach(this);
1054 wxMenuBar::~wxMenuBar()
1056 // In Windows CE (not .NET), the menubar is always associated
1057 // with a toolbar, which destroys the menu implicitly.
1058 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
1061 wxToolMenuBar
* toolMenuBar
= wxDynamicCast(GetToolBar(), wxToolMenuBar
);
1063 toolMenuBar
->SetMenuBar(NULL
);
1066 // we should free Windows resources only if Windows doesn't do it for us
1067 // which happens if we're attached to a frame
1068 if (m_hMenu
&& !IsAttached())
1070 #if defined(WINCE_WITH_COMMANDBAR)
1071 ::DestroyWindow((HWND
) m_commandBar
);
1072 m_commandBar
= (WXHWND
) NULL
;
1074 ::DestroyMenu((HMENU
)m_hMenu
);
1076 m_hMenu
= (WXHMENU
)NULL
;
1081 // ---------------------------------------------------------------------------
1082 // wxMenuBar helpers
1083 // ---------------------------------------------------------------------------
1085 void wxMenuBar::Refresh()
1090 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
1092 #if defined(WINCE_WITHOUT_COMMANDBAR)
1095 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
1097 #elif defined(WINCE_WITH_COMMANDBAR)
1099 DrawMenuBar((HWND
) m_commandBar
);
1101 DrawMenuBar(GetHwndOf(GetFrame()));
1105 WXHMENU
wxMenuBar::Create()
1107 // Note: this doesn't work at all on Smartphone,
1108 // since you have to use resources.
1109 // We'll have to find another way to add a menu
1110 // by changing/adding menu items to an existing menu.
1111 #if defined(WINCE_WITHOUT_COMMANDBAR)
1115 wxToolMenuBar
* const bar
= static_cast<wxToolMenuBar
*>(GetToolBar());
1119 HWND hCommandBar
= GetHwndOf(bar
);
1121 // notify comctl32.dll about the version of the headers we use before using
1122 // any other TB_XXX messages
1123 SendMessage(hCommandBar
, TB_BUTTONSTRUCTSIZE
, sizeof(TBBUTTON
), 0);
1126 wxZeroMemory(tbButton
);
1127 tbButton
.iBitmap
= I_IMAGENONE
;
1128 tbButton
.fsState
= TBSTATE_ENABLED
;
1129 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
|
1130 TBSTYLE_NO_DROPDOWN_ARROW
|
1133 for ( unsigned i
= 0; i
< GetMenuCount(); i
++ )
1135 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu();
1136 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1137 wxString label
= wxStripMenuCodes(GetMenuLabel(i
));
1138 tbButton
.iString
= (int) label
.wx_str();
1140 tbButton
.idCommand
= NewControlId();
1141 if ( !::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
) )
1143 wxLogLastError(wxT("TB_INSERTBUTTON"));
1147 m_hMenu
= bar
->GetHMenu();
1149 #else // !__WXWINCE__
1153 m_hMenu
= (WXHMENU
)::CreateMenu();
1157 wxLogLastError(wxT("CreateMenu"));
1161 for ( wxMenuList::iterator it
= m_menus
.begin();
1162 it
!= m_menus
.end();
1165 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
1166 (UINT_PTR
)(*it
)->GetHMenu(),
1167 (*it
)->GetTitle().wx_str()) )
1169 wxLogLastError(wxT("AppendMenu"));
1175 #endif // __WXWINCE__/!__WXWINCE__
1178 int wxMenuBar::MSWPositionForWxMenu(wxMenu
*menu
, int wxpos
)
1181 wxASSERT(menu
->GetHMenu());
1184 #if defined(__WXWINCE__)
1185 int totalMSWItems
= GetMenuCount();
1187 int totalMSWItems
= GetMenuItemCount((HMENU
)m_hMenu
);
1190 int i
; // For old C++ compatibility
1191 for(i
=wxpos
; i
<totalMSWItems
; i
++)
1193 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1196 for(i
=0; i
<wxpos
; i
++)
1198 if(GetSubMenu((HMENU
)m_hMenu
,i
)==(HMENU
)menu
->GetHMenu())
1205 // ---------------------------------------------------------------------------
1206 // wxMenuBar functions to work with the top level submenus
1207 // ---------------------------------------------------------------------------
1209 // NB: we don't support owner drawn top level items for now, if we do these
1210 // functions would have to be changed to use wxMenuItem as well
1212 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
1214 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
1215 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1217 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
1219 EnableMenuItem((HMENU
)m_hMenu
, MSWPositionForWxMenu(GetMenu(pos
),pos
), MF_BYPOSITION
| flag
);
1224 void wxMenuBar::SetMenuLabel(size_t pos
, const wxString
& label
)
1226 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
1228 m_menus
[pos
]->wxMenuBase::SetTitle(label
);
1230 if ( !IsAttached() )
1234 //else: have to modify the existing menu
1236 int mswpos
= MSWPositionForWxMenu(GetMenu(pos
),pos
);
1239 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, mswpos
, MF_BYPOSITION
);
1240 if ( flagsOld
== 0xFFFFFFFF )
1242 wxLogLastError(wxT("GetMenuState"));
1247 if ( flagsOld
& MF_POPUP
)
1249 // HIBYTE contains the number of items in the submenu in this case
1251 id
= (UINT_PTR
)::GetSubMenu((HMENU
)m_hMenu
, mswpos
);
1261 info
.cbSize
= sizeof(info
);
1262 info
.fMask
= MIIM_TYPE
;
1263 info
.fType
= MFT_STRING
;
1264 info
.cch
= label
.length();
1265 info
.dwTypeData
= const_cast<wxChar
*>(label
.wx_str());
1266 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, &info
) )
1268 wxLogLastError(wxT("SetMenuItemInfo"));
1272 if ( ::ModifyMenu(GetHmenu(), mswpos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
1273 id
, label
.wx_str()) == (int)0xFFFFFFFF )
1275 wxLogLastError(wxT("ModifyMenu"));
1282 wxString
wxMenuBar::GetMenuLabel(size_t pos
) const
1284 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
1285 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
1287 return m_menus
[pos
]->GetTitle();
1290 // ---------------------------------------------------------------------------
1291 // wxMenuBar construction
1292 // ---------------------------------------------------------------------------
1294 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1296 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
1300 menu
->wxMenuBase::SetTitle(title
);
1302 #if defined(WINCE_WITHOUT_COMMANDBAR)
1308 int mswpos
= MSWPositionForWxMenu(menuOld
,pos
);
1310 // can't use ModifyMenu() because it deletes the submenu it replaces
1311 if ( !::RemoveMenu(GetHmenu(), (UINT
)mswpos
, MF_BYPOSITION
) )
1313 wxLogLastError(wxT("RemoveMenu"));
1316 if ( !::InsertMenu(GetHmenu(), (UINT
)mswpos
,
1317 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1318 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1320 wxLogLastError(wxT("InsertMenu"));
1324 if ( menuOld
->HasAccels() || menu
->HasAccels() )
1326 // need to rebuild accell table
1327 RebuildAccelTable();
1329 #endif // wxUSE_ACCEL
1338 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
1340 // Find out which MSW item before which we'll be inserting before
1341 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1342 // If IsAttached() is false this won't be used anyway
1344 #if defined(WINCE_WITHOUT_COMMANDBAR)
1350 int mswpos
= (!isAttached
|| (pos
== m_menus
.GetCount()))
1351 ? -1 // append the menu
1352 : MSWPositionForWxMenu(GetMenu(pos
),pos
);
1354 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
1357 menu
->wxMenuBase::SetTitle(title
);
1361 #if defined(WINCE_WITHOUT_COMMANDBAR)
1365 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1366 tbButton
.iBitmap
= I_IMAGENONE
;
1367 tbButton
.fsState
= TBSTATE_ENABLED
;
1368 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1370 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1371 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1372 wxString label
= wxStripMenuCodes(title
);
1373 tbButton
.iString
= (int) label
.wx_str();
1375 tbButton
.idCommand
= NewControlId();
1376 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1378 wxLogLastError(wxT("TB_INSERTBUTTON"));
1381 wxUnusedVar(mswpos
);
1383 if ( !::InsertMenu(GetHmenu(), mswpos
,
1384 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
1385 (UINT_PTR
)GetHmenuOf(menu
), title
.wx_str()) )
1387 wxLogLastError(wxT("InsertMenu"));
1391 if ( menu
->HasAccels() )
1393 // need to rebuild accell table
1394 RebuildAccelTable();
1396 #endif // wxUSE_ACCEL
1405 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
1407 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
1408 wxCHECK_MSG( submenu
, false, wxT("can't append invalid menu to menubar") );
1410 if ( !wxMenuBarBase::Append(menu
, title
) )
1413 menu
->wxMenuBase::SetTitle(title
);
1415 #if defined(WINCE_WITHOUT_COMMANDBAR)
1421 #if defined(WINCE_WITHOUT_COMMANDBAR)
1425 memset(&tbButton
, 0, sizeof(TBBUTTON
));
1426 tbButton
.iBitmap
= I_IMAGENONE
;
1427 tbButton
.fsState
= TBSTATE_ENABLED
;
1428 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
1430 size_t pos
= GetMenuCount();
1431 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
1432 tbButton
.dwData
= (DWORD
)hPopupMenu
;
1433 wxString label
= wxStripMenuCodes(title
);
1434 tbButton
.iString
= (int) label
.wx_str();
1436 tbButton
.idCommand
= NewControlId();
1437 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
1439 wxLogLastError(wxT("TB_INSERTBUTTON"));
1443 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
1444 (UINT_PTR
)submenu
, title
.wx_str()) )
1446 wxLogLastError(wxT("AppendMenu"));
1451 if ( menu
->HasAccels() )
1453 // need to rebuild accelerator table
1454 RebuildAccelTable();
1456 #endif // wxUSE_ACCEL
1465 wxMenu
*wxMenuBar::Remove(size_t pos
)
1467 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
1471 #if defined(WINCE_WITHOUT_COMMANDBAR)
1477 #if defined(WINCE_WITHOUT_COMMANDBAR)
1480 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
1482 wxLogLastError(wxT("TB_DELETEBUTTON"));
1486 if ( !::RemoveMenu(GetHmenu(), (UINT
)MSWPositionForWxMenu(menu
,pos
), MF_BYPOSITION
) )
1488 wxLogLastError(wxT("RemoveMenu"));
1493 if ( menu
->HasAccels() )
1495 // need to rebuild accell table
1496 RebuildAccelTable();
1498 #endif // wxUSE_ACCEL
1509 void wxMenuBar::RebuildAccelTable()
1511 // merge the accelerators of all menus into one accel table
1512 size_t nAccelCount
= 0;
1513 size_t i
, count
= GetMenuCount();
1514 wxMenuList::iterator it
;
1515 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1517 nAccelCount
+= (*it
)->GetAccelCount();
1522 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1525 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1527 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1530 SetAcceleratorTable(wxAcceleratorTable(nAccelCount
, accelEntries
));
1532 delete [] accelEntries
;
1536 #endif // wxUSE_ACCEL
1538 void wxMenuBar::Attach(wxFrame
*frame
)
1540 wxMenuBarBase::Attach(frame
);
1542 #if defined(WINCE_WITH_COMMANDBAR)
1546 m_commandBar
= (WXHWND
) CommandBar_Create(wxGetInstance(), (HWND
) frame
->GetHWND(), NewControlId());
1551 if (!CommandBar_InsertMenubarEx((HWND
) m_commandBar
, NULL
, (LPTSTR
) m_hMenu
, 0))
1553 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1560 RebuildAccelTable();
1561 #endif // wxUSE_ACCEL
1564 #if defined(WINCE_WITH_COMMANDBAR)
1565 bool wxMenuBar::AddAdornments(long style
)
1567 if (m_adornmentsAdded
|| !m_commandBar
)
1570 if (style
& wxCLOSE_BOX
)
1572 if (!CommandBar_AddAdornments((HWND
) m_commandBar
, 0, 0))
1574 wxLogLastError(wxT("CommandBar_AddAdornments"));
1585 void wxMenuBar::Detach()
1587 wxMenuBarBase::Detach();
1590 // get the menu with given handle (recursively)
1591 wxMenu
* wxMenuBar::MSWGetMenu(WXHMENU hMenu
)
1593 wxCHECK_MSG( GetHMenu() != hMenu
, NULL
,
1594 wxT("wxMenuBar::MSWGetMenu(): menu handle is wxMenuBar, not wxMenu") );
1596 #if wxUSE_OWNER_DRAW
1598 for ( size_t n
= 0 ; n
< GetMenuCount(); ++n
)
1600 wxMenu
* menu
= GetMenu(n
)->MSWGetMenu(hMenu
);
1610 #endif // wxUSE_MENUS