1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "menu.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
40 #include "wx/ownerdrw.h"
43 #include "wx/msw/private.h"
45 // other standard headers
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 extern wxMenu
*wxCurrentPopupMenu
;
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // the (popup) menu title has this special id
59 static const int idMenuTitle
= -2;
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
66 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
68 // ============================================================================
70 // ============================================================================
72 // ---------------------------------------------------------------------------
73 // wxMenu construction, adding and removing menu items
74 // ---------------------------------------------------------------------------
76 // Construct a menu with optional title (then use append)
82 m_hMenu
= (WXHMENU
)CreatePopupMenu();
85 wxLogLastError(wxT("CreatePopupMenu"));
88 // if we have a title, insert it in the beginning of the menu
91 Append(idMenuTitle
, m_title
);
96 // The wxWindow destructor will take care of deleting the submenus.
99 // we should free Windows resources only if Windows doesn't do it for us
100 // which happens if we're attached to a menubar or a submenu of another
102 if ( !IsAttached() && !GetParent() )
104 if ( !::DestroyMenu(GetHmenu()) )
106 wxLogLastError(wxT("DestroyMenu"));
112 WX_CLEAR_ARRAY(m_accels
);
113 #endif // wxUSE_ACCEL
118 // this will take effect during the next call to Append()
124 int wxMenu::FindAccel(int id
) const
126 size_t n
, count
= m_accels
.GetCount();
127 for ( n
= 0; n
< count
; n
++ )
129 if ( m_accels
[n
]->m_command
== id
)
136 void wxMenu::UpdateAccel(wxMenuItem
*item
)
138 if ( item
->IsSubMenu() )
140 wxMenu
*submenu
= item
->GetSubMenu();
141 wxMenuItemList::Node
*node
= submenu
->GetMenuItems().GetFirst();
144 UpdateAccel(node
->GetData());
146 node
= node
->GetNext();
149 else if ( !item
->IsSeparator() )
151 // find the (new) accel for this item
152 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
154 accel
->m_command
= item
->GetId();
157 int n
= FindAccel(item
->GetId());
158 if ( n
== wxNOT_FOUND
)
160 // no old, add new if any
164 return; // skipping RebuildAccelTable() below
168 // replace old with new or just remove the old one if no new
178 m_menuBar
->RebuildAccelTable();
181 //else: it is a separator, they can't have accels, nothing to do
184 #endif // wxUSE_ACCEL
186 // append a new item or submenu to the menu
187 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
191 #endif // wxUSE_ACCEL
195 // if "Break" has just been called, insert a menu break before this item
196 // (and don't forget to reset the flag)
198 flags
|= MF_MENUBREAK
;
202 if ( pItem
->IsSeparator() ) {
203 flags
|= MF_SEPARATOR
;
206 // id is the numeric id for normal menu items and HMENU for submenus as
207 // required by ::AppendMenu() API
209 wxMenu
*submenu
= pItem
->GetSubMenu();
210 if ( submenu
!= NULL
) {
211 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
213 submenu
->SetParent(this);
215 id
= (UINT
)submenu
->GetHMenu();
225 #if wxUSE_OWNER_DRAWN
226 if ( pItem
->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
227 // item draws itself, pass pointer to it in data parameter
228 flags
|= MF_OWNERDRAW
;
229 pData
= (LPCTSTR
)pItem
;
234 // menu is just a normal string (passed in data parameter)
237 pData
= (wxChar
*)pItem
->GetText().c_str();
241 if ( pos
== (size_t)-1 )
243 ok
= ::AppendMenu(GetHmenu(), flags
, id
, pData
);
247 ok
= ::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
);
252 wxLogLastError(wxT("Insert or AppendMenu"));
258 // if we just appended the title, highlight it
260 if ( (int)id
== idMenuTitle
)
262 // visually select the menu title
264 mii
.cbSize
= sizeof(mii
);
265 mii
.fMask
= MIIM_STATE
;
266 mii
.fState
= MFS_DEFAULT
;
268 if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id
, FALSE
, &mii
) )
270 wxLogLastError(wxT("SetMenuItemInfo"));
275 // if we're already attached to the menubar, we must update it
278 m_menuBar
->Refresh();
285 bool wxMenu::DoAppend(wxMenuItem
*item
)
287 return wxMenuBase::DoAppend(item
) && DoInsertOrAppend(item
);
290 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
292 return wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
);
295 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
297 // we need to find the items position in the child list
299 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
300 for ( pos
= 0; node
; pos
++ )
302 if ( node
->GetData() == item
)
305 node
= node
->GetNext();
308 // DoRemove() (unlike Remove) can only be called for existing item!
309 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
312 // remove the corresponding accel from the accel table
313 int n
= FindAccel(item
->GetId());
314 if ( n
!= wxNOT_FOUND
)
320 //else: this item doesn't have an accel, nothing to do
321 #endif // wxUSE_ACCEL
323 // remove the item from the menu
324 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
326 wxLogLastError(wxT("RemoveMenu"));
331 // otherwise, the chane won't be visible
332 m_menuBar
->Refresh();
335 // and from internal data structures
336 return wxMenuBase::DoRemove(item
);
339 // ---------------------------------------------------------------------------
340 // accelerator helpers
341 // ---------------------------------------------------------------------------
345 // create the wxAcceleratorEntries for our accels and put them into provided
346 // array - return the number of accels we have
347 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
349 size_t count
= GetAccelCount();
350 for ( size_t n
= 0; n
< count
; n
++ )
352 *accels
++ = *m_accels
[n
];
358 #endif // wxUSE_ACCEL
360 // ---------------------------------------------------------------------------
362 // ---------------------------------------------------------------------------
364 void wxMenu::SetTitle(const wxString
& label
)
366 bool hasNoTitle
= m_title
.IsEmpty();
369 HMENU hMenu
= GetHmenu();
373 if ( !label
.IsEmpty() )
375 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
376 (unsigned)idMenuTitle
, m_title
) ||
377 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
379 wxLogLastError(wxT("InsertMenu"));
385 if ( label
.IsEmpty() )
387 // remove the title and the separator after it
388 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
389 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
391 wxLogLastError(wxT("RemoveMenu"));
397 if ( !ModifyMenu(hMenu
, 0u,
398 MF_BYPOSITION
| MF_STRING
,
399 (unsigned)idMenuTitle
, m_title
) )
401 wxLogLastError(wxT("ModifyMenu"));
407 // put the title string in bold face
408 if ( !m_title
.IsEmpty() )
411 mii
.cbSize
= sizeof(mii
);
412 mii
.fMask
= MIIM_STATE
;
413 mii
.fState
= MFS_DEFAULT
;
415 if ( !SetMenuItemInfo(hMenu
, (unsigned)idMenuTitle
, FALSE
, &mii
) )
417 wxLogLastError(wxT("SetMenuItemInfo"));
423 // ---------------------------------------------------------------------------
425 // ---------------------------------------------------------------------------
427 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
429 // ignore commands from the menu title
431 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
432 if ( id
!= (WXWORD
)idMenuTitle
)
434 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
);
435 event
.SetEventObject( this );
438 // VZ: previosuly, the command int was set to id too which was quite
439 // useless anyhow (as it could be retrieved using GetId()) and
440 // uncompatible with wxGTK, so now we use the command int instead
441 // to pass the checked status
442 event
.SetInt(::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
) & MF_CHECKED
);
444 ProcessCommand(event
);
450 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
452 bool processed
= FALSE
;
454 #if wxUSE_MENU_CALLBACK
458 (void)(*(m_callback
))(*this, event
);
461 #endif // wxUSE_MENU_CALLBACK
463 // Try the menu's event handler
464 if ( !processed
&& GetEventHandler())
466 processed
= GetEventHandler()->ProcessEvent(event
);
469 // Try the window the menu was popped up from (and up through the
471 wxWindow
*win
= GetInvokingWindow();
472 if ( !processed
&& win
)
473 processed
= win
->GetEventHandler()->ProcessEvent(event
);
478 // ---------------------------------------------------------------------------
480 // ---------------------------------------------------------------------------
482 void wxMenu::Attach(wxMenuBar
*menubar
)
484 // menu can be in at most one menubar because otherwise they would both
485 // delete the menu pointer
486 wxASSERT_MSG( !m_menuBar
, wxT("menu belongs to 2 menubars, expect a crash") );
491 void wxMenu::Detach()
493 wxASSERT_MSG( m_menuBar
, wxT("can't detach menu if it's not attached") );
498 wxWindow
*wxMenu::GetWindow() const
500 if ( m_invokingWindow
!= NULL
)
501 return m_invokingWindow
;
502 else if ( m_menuBar
!= NULL
)
503 return m_menuBar
->GetFrame();
508 // ---------------------------------------------------------------------------
510 // ---------------------------------------------------------------------------
512 void wxMenuBar::Init()
514 m_eventHandler
= this;
515 m_menuBarFrame
= NULL
;
519 wxMenuBar::wxMenuBar()
524 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
529 wxMenuBar::wxMenuBar(int count
, wxMenu
*menus
[], const wxString titles
[])
533 m_titles
.Alloc(count
);
535 for ( int i
= 0; i
< count
; i
++ )
537 m_menus
.Append(menus
[i
]);
538 m_titles
.Add(titles
[i
]);
540 menus
[i
]->Attach(this);
544 wxMenuBar::~wxMenuBar()
548 // ---------------------------------------------------------------------------
550 // ---------------------------------------------------------------------------
552 void wxMenuBar::Refresh()
554 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
556 DrawMenuBar(GetHwndOf(m_menuBarFrame
));
559 WXHMENU
wxMenuBar::Create()
564 m_hMenu
= (WXHMENU
)::CreateMenu();
568 wxLogLastError(wxT("CreateMenu"));
572 size_t count
= GetMenuCount();
573 for ( size_t i
= 0; i
< count
; i
++ )
575 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
576 (UINT
)m_menus
[i
]->GetHMenu(),
579 wxLogLastError(wxT("AppendMenu"));
587 // ---------------------------------------------------------------------------
588 // wxMenuBar functions to work with the top level submenus
589 // ---------------------------------------------------------------------------
591 // NB: we don't support owner drawn top level items for now, if we do these
592 // functions would have to be changed to use wxMenuItem as well
594 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
596 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
598 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
600 EnableMenuItem((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
| flag
);
605 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
607 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
609 m_titles
[pos
] = label
;
615 //else: have to modify the existing menu
618 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
);
619 if ( flagsOld
== 0xFFFFFFFF )
621 wxLogLastError(wxT("GetMenuState"));
626 if ( flagsOld
& MF_POPUP
)
628 // HIBYTE contains the number of items in the submenu in this case
630 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, pos
);
637 if ( ::ModifyMenu(GetHmenu(), pos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
638 id
, label
) == (int)0xFFFFFFFF )
640 wxLogLastError(wxT("ModifyMenu"));
646 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
648 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
649 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
651 return m_titles
[pos
];
654 // ---------------------------------------------------------------------------
655 // wxMenuBar construction
656 // ---------------------------------------------------------------------------
658 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
660 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
663 m_titles
[pos
] = title
;
667 // can't use ModifyMenu() because it deletes the submenu it replaces
668 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
670 wxLogLastError(wxT("RemoveMenu"));
673 if ( !::InsertMenu(GetHmenu(), (UINT
)pos
,
674 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
675 (UINT
)GetHmenuOf(menu
), title
) )
677 wxLogLastError(wxT("InsertMenu"));
681 if ( menuOld
->HasAccels() || menu
->HasAccels() )
683 // need to rebuild accell table
686 #endif // wxUSE_ACCEL
694 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
696 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
699 m_titles
.Insert(title
, pos
);
705 if ( !::InsertMenu(GetHmenu(), pos
,
706 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
707 (UINT
)GetHmenuOf(menu
), title
) )
709 wxLogLastError(wxT("InsertMenu"));
713 if ( menu
->HasAccels() )
715 // need to rebuild accell table
718 #endif // wxUSE_ACCEL
726 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
728 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
729 wxCHECK_MSG( submenu
, FALSE
, wxT("can't append invalid menu to menubar") );
731 if ( !wxMenuBarBase::Append(menu
, title
) )
740 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
741 (UINT
)submenu
, title
) )
743 wxLogLastError(wxT("AppendMenu"));
747 if ( menu
->HasAccels() )
749 // need to rebuild accell table
752 #endif // wxUSE_ACCEL
760 wxMenu
*wxMenuBar::Remove(size_t pos
)
762 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
768 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
770 wxLogLastError(wxT("RemoveMenu"));
776 if ( menu
->HasAccels() )
778 // need to rebuild accell table
781 #endif // wxUSE_ACCEL
786 m_titles
.Remove(pos
);
793 void wxMenuBar::RebuildAccelTable()
795 // merge the accelerators of all menus into one accel table
796 size_t nAccelCount
= 0;
797 size_t i
, count
= GetMenuCount();
798 for ( i
= 0; i
< count
; i
++ )
800 nAccelCount
+= m_menus
[i
]->GetAccelCount();
805 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
808 for ( i
= 0; i
< count
; i
++ )
810 nAccelCount
+= m_menus
[i
]->CopyAccels(&accelEntries
[nAccelCount
]);
813 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
815 delete [] accelEntries
;
819 #endif // wxUSE_ACCEL
821 void wxMenuBar::Attach(wxFrame
*frame
)
823 // wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
825 m_menuBarFrame
= frame
;
829 #endif // wxUSE_ACCEL
832 void wxMenuBar::Detach()
834 // ::DestroyMenu((HMENU)m_hMenu);
835 m_hMenu
= (WXHMENU
)NULL
;
836 m_menuBarFrame
= NULL
;
840 // ---------------------------------------------------------------------------
841 // wxMenuBar searching for menu items
842 // ---------------------------------------------------------------------------
844 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
845 int wxMenuBar::FindMenuItem(const wxString
& menuString
,
846 const wxString
& itemString
) const
848 wxString menuLabel
= wxStripMenuCodes(menuString
);
849 size_t count
= GetMenuCount();
850 for ( size_t i
= 0; i
< count
; i
++ )
852 wxString title
= wxStripMenuCodes(m_titles
[i
]);
853 if ( menuLabel
== title
)
854 return m_menus
[i
]->FindItem(itemString
);
860 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
**itemMenu
) const
865 wxMenuItem
*item
= NULL
;
866 size_t count
= GetMenuCount();
867 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
869 item
= m_menus
[i
]->FindItem(id
, itemMenu
);