1 /////////////////////////////////////////////////////////////////////////////
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 // ---------------------------------------------------------------------------
21 #pragma implementation "menu.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
42 #include "wx/ownerdrw.h"
45 #include "wx/msw/private.h"
55 #ifndef TBSTYLE_NO_DROPDOWN_ARROW
56 #define TBSTYLE_NO_DROPDOWN_ARROW 0x0080
61 // other standard headers
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 extern wxMenu
*wxCurrentPopupMenu
;
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 // the (popup) menu title has this special id
75 static const int idMenuTitle
= -2;
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 // make the given menu item default
82 static void SetDefaultMenuItem(HMENU hmenu
, UINT id
)
87 mii
.cbSize
= sizeof(MENUITEMINFO
);
88 mii
.fMask
= MIIM_STATE
;
89 mii
.fState
= MFS_DEFAULT
;
91 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
93 wxLogLastError(wxT("SetMenuItemInfo"));
99 UINT
GetMenuState(HMENU hMenu
, UINT id
, UINT flags
)
103 info
.cbSize
= sizeof(info
);
104 info
.fMask
= MIIM_STATE
;
105 if ( !GetMenuItemInfo(hMenu
, id
, flags
& MF_BYCOMMAND
? FALSE
: TRUE
, & info
) )
106 wxLogLastError(wxT("GetMenuItemInfo"));
111 // ============================================================================
113 // ============================================================================
115 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
116 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
118 // ---------------------------------------------------------------------------
119 // wxMenu construction, adding and removing menu items
120 // ---------------------------------------------------------------------------
122 // Construct a menu with optional title (then use append)
126 m_startRadioGroup
= -1;
129 m_hMenu
= (WXHMENU
)CreatePopupMenu();
132 wxLogLastError(wxT("CreatePopupMenu"));
135 // if we have a title, insert it in the beginning of the menu
138 Append(idMenuTitle
, m_title
);
143 // The wxWindow destructor will take care of deleting the submenus.
146 // we should free Windows resources only if Windows doesn't do it for us
147 // which happens if we're attached to a menubar or a submenu of another
149 if ( !IsAttached() && !GetParent() )
151 if ( !::DestroyMenu(GetHmenu()) )
153 wxLogLastError(wxT("DestroyMenu"));
159 WX_CLEAR_ARRAY(m_accels
);
160 #endif // wxUSE_ACCEL
165 // this will take effect during the next call to Append()
169 void wxMenu::Attach(wxMenuBarBase
*menubar
)
171 wxMenuBase::Attach(menubar
);
178 int wxMenu::FindAccel(int id
) const
180 size_t n
, count
= m_accels
.GetCount();
181 for ( n
= 0; n
< count
; n
++ )
183 if ( m_accels
[n
]->m_command
== id
)
190 void wxMenu::UpdateAccel(wxMenuItem
*item
)
192 if ( item
->IsSubMenu() )
194 wxMenu
*submenu
= item
->GetSubMenu();
195 wxMenuItemList::compatibility_iterator node
= submenu
->GetMenuItems().GetFirst();
198 UpdateAccel(node
->GetData());
200 node
= node
->GetNext();
203 else if ( !item
->IsSeparator() )
205 // find the (new) accel for this item
206 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
208 accel
->m_command
= item
->GetId();
211 int n
= FindAccel(item
->GetId());
212 if ( n
== wxNOT_FOUND
)
214 // no old, add new if any
218 return; // skipping RebuildAccelTable() below
222 // replace old with new or just remove the old one if no new
227 m_accels
.RemoveAt(n
);
232 m_menuBar
->RebuildAccelTable();
235 //else: it is a separator, they can't have accels, nothing to do
238 #endif // wxUSE_ACCEL
240 // append a new item or submenu to the menu
241 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
245 #endif // wxUSE_ACCEL
249 // if "Break" has just been called, insert a menu break before this item
250 // (and don't forget to reset the flag)
252 flags
|= MF_MENUBREAK
;
256 if ( pItem
->IsSeparator() ) {
257 flags
|= MF_SEPARATOR
;
260 // id is the numeric id for normal menu items and HMENU for submenus as
261 // required by ::AppendMenu() API
263 wxMenu
*submenu
= pItem
->GetSubMenu();
264 if ( submenu
!= NULL
) {
265 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
267 submenu
->SetParent(this);
269 id
= (UINT
)submenu
->GetHMenu();
278 wxString strippedString
;
283 #if wxUSE_OWNER_DRAWN
284 if ( pItem
->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
285 // item draws itself, pass pointer to it in data parameter
286 flags
|= MF_OWNERDRAW
;
287 pData
= (LPCTSTR
)pItem
;
292 // menu is just a normal string (passed in data parameter)
296 strippedString
= wxStripMenuCodes(pItem
->GetText());
297 pData
= (wxChar
*)strippedString
.c_str();
299 pData
= (wxChar
*)pItem
->GetText().c_str();
304 if ( pos
== (size_t)-1 )
306 ok
= ::AppendMenu(GetHmenu(), flags
, id
, pData
);
310 ok
= ::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
);
315 wxLogLastError(wxT("Insert or AppendMenu"));
320 // if we just appended the title, highlight it
322 if ( (int)id
== idMenuTitle
)
324 // visually select the menu title
325 SetDefaultMenuItem(GetHmenu(), id
);
329 // if we're already attached to the menubar, we must update it
330 if ( IsAttached() && m_menuBar
->IsAttached() )
332 m_menuBar
->Refresh();
338 void wxMenu::EndRadioGroup()
340 // we're not inside a radio group any longer
341 m_startRadioGroup
= -1;
344 bool wxMenu::DoAppend(wxMenuItem
*item
)
346 wxCHECK_MSG( item
, FALSE
, _T("NULL item in wxMenu::DoAppend") );
350 if ( item
->GetKind() == wxITEM_RADIO
)
352 int count
= GetMenuItemCount();
354 if ( m_startRadioGroup
== -1 )
356 // start a new radio group
357 m_startRadioGroup
= count
;
359 // for now it has just one element
360 item
->SetAsRadioGroupStart();
361 item
->SetRadioGroupEnd(m_startRadioGroup
);
363 // ensure that we have a checked item in the radio group
366 else // extend the current radio group
368 // we need to update its end item
369 item
->SetRadioGroupStart(m_startRadioGroup
);
370 wxMenuItemList::compatibility_iterator node
= GetMenuItems().Item(m_startRadioGroup
);
374 node
->GetData()->SetRadioGroupEnd(count
);
378 wxFAIL_MSG( _T("where is the radio group start item?") );
382 else // not a radio item
387 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
394 // check the item initially
401 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
403 return wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
);
406 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
408 // we need to find the items position in the child list
410 wxMenuItemList::compatibility_iterator node
= GetMenuItems().GetFirst();
411 for ( pos
= 0; node
; pos
++ )
413 if ( node
->GetData() == item
)
416 node
= node
->GetNext();
419 // DoRemove() (unlike Remove) can only be called for existing item!
420 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
423 // remove the corresponding accel from the accel table
424 int n
= FindAccel(item
->GetId());
425 if ( n
!= wxNOT_FOUND
)
429 m_accels
.RemoveAt(n
);
431 //else: this item doesn't have an accel, nothing to do
432 #endif // wxUSE_ACCEL
434 // remove the item from the menu
435 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
437 wxLogLastError(wxT("RemoveMenu"));
440 if ( IsAttached() && m_menuBar
->IsAttached() )
442 // otherwise, the chane won't be visible
443 m_menuBar
->Refresh();
446 // and from internal data structures
447 return wxMenuBase::DoRemove(item
);
450 // ---------------------------------------------------------------------------
451 // accelerator helpers
452 // ---------------------------------------------------------------------------
456 // create the wxAcceleratorEntries for our accels and put them into provided
457 // array - return the number of accels we have
458 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
460 size_t count
= GetAccelCount();
461 for ( size_t n
= 0; n
< count
; n
++ )
463 *accels
++ = *m_accels
[n
];
469 #endif // wxUSE_ACCEL
471 // ---------------------------------------------------------------------------
473 // ---------------------------------------------------------------------------
475 void wxMenu::SetTitle(const wxString
& label
)
477 bool hasNoTitle
= m_title
.IsEmpty();
480 HMENU hMenu
= GetHmenu();
484 if ( !label
.IsEmpty() )
486 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
487 (unsigned)idMenuTitle
, m_title
) ||
488 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
490 wxLogLastError(wxT("InsertMenu"));
496 if ( label
.IsEmpty() )
498 // remove the title and the separator after it
499 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
500 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
502 wxLogLastError(wxT("RemoveMenu"));
511 info
.cbSize
= sizeof(info
);
512 info
.fMask
= MIIM_TYPE
;
513 info
.fType
= MFT_STRING
;
514 info
.cch
= m_title
.Length();
515 info
.dwTypeData
= (LPTSTR
) m_title
.c_str();
516 if ( !SetMenuItemInfo(hMenu
, 0, TRUE
, & info
) )
518 wxLogLastError(wxT("SetMenuItemInfo"));
521 if ( !ModifyMenu(hMenu
, 0u,
522 MF_BYPOSITION
| MF_STRING
,
523 (unsigned)idMenuTitle
, m_title
) )
525 wxLogLastError(wxT("ModifyMenu"));
532 // put the title string in bold face
533 if ( !m_title
.IsEmpty() )
535 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
540 // ---------------------------------------------------------------------------
542 // ---------------------------------------------------------------------------
544 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
546 // ignore commands from the menu title
548 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
549 if ( id
!= (WXWORD
)idMenuTitle
)
551 // VZ: previosuly, the command int was set to id too which was quite
552 // useless anyhow (as it could be retrieved using GetId()) and
553 // uncompatible with wxGTK, so now we use the command int instead
554 // to pass the checked status
555 UINT menuState
= ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
) ;
556 SendEvent(id
, menuState
& MF_CHECKED
);
562 // ---------------------------------------------------------------------------
564 // ---------------------------------------------------------------------------
566 wxWindow
*wxMenu::GetWindow() const
568 if ( m_invokingWindow
!= NULL
)
569 return m_invokingWindow
;
570 else if ( m_menuBar
!= NULL
)
571 return m_menuBar
->GetFrame();
576 // ---------------------------------------------------------------------------
578 // ---------------------------------------------------------------------------
580 void wxMenuBar::Init()
582 m_eventHandler
= this;
589 wxMenuBar::wxMenuBar()
594 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
599 wxMenuBar::wxMenuBar(int count
, wxMenu
*menus
[], const wxString titles
[])
603 m_titles
.Alloc(count
);
605 for ( int i
= 0; i
< count
; i
++ )
607 m_menus
.Append(menus
[i
]);
608 m_titles
.Add(titles
[i
]);
610 menus
[i
]->Attach(this);
614 wxMenuBar::~wxMenuBar()
616 // In Windows CE, the menubar is always associated
617 // with a toolbar, which destroys the menu implicitly.
620 GetToolBar()->SetMenuBar(NULL
);
622 // we should free Windows resources only if Windows doesn't do it for us
623 // which happens if we're attached to a frame
624 if (m_hMenu
&& !IsAttached())
626 ::DestroyMenu((HMENU
)m_hMenu
);
627 m_hMenu
= (WXHMENU
)NULL
;
632 // ---------------------------------------------------------------------------
634 // ---------------------------------------------------------------------------
636 void wxMenuBar::Refresh()
638 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
643 CommandBar_DrawMenuBar((HWND
) GetToolBar()->GetHWND(), 0);
646 DrawMenuBar(GetHwndOf(GetFrame()));
650 WXHMENU
wxMenuBar::Create()
659 HWND hCommandBar
= (HWND
) GetToolBar()->GetHWND();
660 HMENU hMenu
= (HMENU
)::SendMessage(hCommandBar
, SHCMBM_GETMENU
, (WPARAM
)0, (LPARAM
)0);
664 memset(&tbButton
, 0, sizeof(TBBUTTON
));
665 tbButton
.iBitmap
= I_IMAGENONE
;
666 tbButton
.fsState
= TBSTATE_ENABLED
;
667 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
670 for (i
= 0; i
< GetMenuCount(); i
++)
672 HMENU hPopupMenu
= (HMENU
) GetMenu(i
)->GetHMenu() ;
673 tbButton
.dwData
= (DWORD
)hPopupMenu
;
674 wxString label
= wxStripMenuCodes(GetLabelTop(i
));
675 tbButton
.iString
= (int) label
.c_str();
677 tbButton
.idCommand
= NewControlId();
678 if (!::SendMessage(hCommandBar
, TB_INSERTBUTTON
, i
, (LPARAM
)&tbButton
))
680 wxLogLastError(wxT("TB_INSERTBUTTON"));
684 m_hMenu
= (WXHMENU
) hMenu
;
690 m_hMenu
= (WXHMENU
)::CreateMenu();
694 wxLogLastError(wxT("CreateMenu"));
698 size_t count
= GetMenuCount(), i
;
699 wxMenuList::iterator it
;
700 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
702 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
703 (UINT
)(*it
)->GetHMenu(),
706 wxLogLastError(wxT("AppendMenu"));
715 // ---------------------------------------------------------------------------
716 // wxMenuBar functions to work with the top level submenus
717 // ---------------------------------------------------------------------------
719 // NB: we don't support owner drawn top level items for now, if we do these
720 // functions would have to be changed to use wxMenuItem as well
722 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
724 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
726 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
728 EnableMenuItem((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
| flag
);
733 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
735 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
737 m_titles
[pos
] = label
;
743 //else: have to modify the existing menu
746 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
);
747 if ( flagsOld
== 0xFFFFFFFF )
749 wxLogLastError(wxT("GetMenuState"));
754 if ( flagsOld
& MF_POPUP
)
756 // HIBYTE contains the number of items in the submenu in this case
758 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, pos
);
768 info
.cbSize
= sizeof(info
);
769 info
.fMask
= MIIM_TYPE
;
770 info
.fType
= MFT_STRING
;
771 info
.cch
= label
.Length();
772 info
.dwTypeData
= (LPTSTR
) label
.c_str();
773 if ( !SetMenuItemInfo(GetHmenu(), id
, TRUE
, & info
) )
775 wxLogLastError(wxT("SetMenuItemInfo"));
779 if ( ::ModifyMenu(GetHmenu(), pos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
780 id
, label
) == (int)0xFFFFFFFF )
782 wxLogLastError(wxT("ModifyMenu"));
789 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
791 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
792 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
794 return m_titles
[pos
];
797 // ---------------------------------------------------------------------------
798 // wxMenuBar construction
799 // ---------------------------------------------------------------------------
801 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
803 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
807 m_titles
[pos
] = title
;
811 // can't use ModifyMenu() because it deletes the submenu it replaces
812 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
814 wxLogLastError(wxT("RemoveMenu"));
817 if ( !::InsertMenu(GetHmenu(), (UINT
)pos
,
818 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
819 (UINT
)GetHmenuOf(menu
), title
) )
821 wxLogLastError(wxT("InsertMenu"));
825 if ( menuOld
->HasAccels() || menu
->HasAccels() )
827 // need to rebuild accell table
830 #endif // wxUSE_ACCEL
838 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
840 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
843 m_titles
.Insert(title
, pos
);
851 memset(&tbButton
, 0, sizeof(TBBUTTON
));
852 tbButton
.iBitmap
= I_IMAGENONE
;
853 tbButton
.fsState
= TBSTATE_ENABLED
;
854 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
856 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
857 tbButton
.dwData
= (DWORD
)hPopupMenu
;
858 wxString label
= wxStripMenuCodes(title
);
859 tbButton
.iString
= (int) label
.c_str();
861 tbButton
.idCommand
= NewControlId();
862 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
864 wxLogLastError(wxT("TB_INSERTBUTTON"));
868 if ( !::InsertMenu(GetHmenu(), pos
,
869 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
870 (UINT
)GetHmenuOf(menu
), title
) )
872 wxLogLastError(wxT("InsertMenu"));
876 if ( menu
->HasAccels() )
878 // need to rebuild accell table
881 #endif // wxUSE_ACCEL
889 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
891 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
892 wxCHECK_MSG( submenu
, FALSE
, wxT("can't append invalid menu to menubar") );
894 if ( !wxMenuBarBase::Append(menu
, title
) )
905 memset(&tbButton
, 0, sizeof(TBBUTTON
));
906 tbButton
.iBitmap
= I_IMAGENONE
;
907 tbButton
.fsState
= TBSTATE_ENABLED
;
908 tbButton
.fsStyle
= TBSTYLE_DROPDOWN
| TBSTYLE_NO_DROPDOWN_ARROW
| TBSTYLE_AUTOSIZE
;
910 size_t pos
= GetMenuCount();
911 HMENU hPopupMenu
= (HMENU
) menu
->GetHMenu() ;
912 tbButton
.dwData
= (DWORD
)hPopupMenu
;
913 wxString label
= wxStripMenuCodes(title
);
914 tbButton
.iString
= (int) label
.c_str();
916 tbButton
.idCommand
= NewControlId();
917 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_INSERTBUTTON
, pos
, (LPARAM
)&tbButton
))
919 wxLogLastError(wxT("TB_INSERTBUTTON"));
923 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
924 (UINT
)submenu
, title
) )
926 wxLogLastError(wxT("AppendMenu"));
931 if ( menu
->HasAccels() )
933 // need to rebuild accelerator table
936 #endif // wxUSE_ACCEL
944 wxMenu
*wxMenuBar::Remove(size_t pos
)
946 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
955 if (!::SendMessage((HWND
) GetToolBar()->GetHWND(), TB_DELETEBUTTON
, (UINT
) pos
, (LPARAM
) 0))
957 wxLogLastError(wxT("TB_DELETEBUTTON"));
961 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
963 wxLogLastError(wxT("RemoveMenu"));
967 if ( menu
->HasAccels() )
969 // need to rebuild accell table
972 #endif // wxUSE_ACCEL
977 m_titles
.RemoveAt(pos
);
984 void wxMenuBar::RebuildAccelTable()
986 // merge the accelerators of all menus into one accel table
987 size_t nAccelCount
= 0;
988 size_t i
, count
= GetMenuCount();
989 wxMenuList::iterator it
;
990 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
992 nAccelCount
+= (*it
)->GetAccelCount();
997 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1000 for ( i
= 0, it
= m_menus
.begin(); i
< count
; i
++, it
++ )
1002 nAccelCount
+= (*it
)->CopyAccels(&accelEntries
[nAccelCount
]);
1005 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
1007 delete [] accelEntries
;
1011 #endif // wxUSE_ACCEL
1013 void wxMenuBar::Attach(wxFrame
*frame
)
1015 wxMenuBarBase::Attach(frame
);
1018 RebuildAccelTable();
1019 #endif // wxUSE_ACCEL
1022 void wxMenuBar::Detach()
1024 wxMenuBarBase::Detach();
1027 #endif // wxUSE_MENUS