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 #if !USE_SHARED_LIBRARY
66 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
67 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
70 // ============================================================================
72 // ============================================================================
74 // ---------------------------------------------------------------------------
75 // wxMenu construction, adding and removing menu items
76 // ---------------------------------------------------------------------------
78 // Construct a menu with optional title (then use append)
84 m_hMenu
= (WXHMENU
)CreatePopupMenu();
87 wxLogLastError("CreatePopupMenu");
90 // if we have a title, insert it in the beginning of the menu
93 Append(idMenuTitle
, m_title
);
98 // The wxWindow destructor will take care of deleting the submenus.
101 // we should free Windows resources only if Windows doesn't do it for us
102 // which happens if we're attached to a menubar or a submenu of another
104 if ( !IsAttached() && !GetParent() )
106 if ( !::DestroyMenu(GetHmenu()) )
108 wxLogLastError("DestroyMenu");
114 WX_CLEAR_ARRAY(m_accels
);
115 #endif // wxUSE_ACCEL
120 // this will take effect during the next call to Append()
126 int wxMenu::FindAccel(int id
) const
128 size_t n
, count
= m_accels
.GetCount();
129 for ( n
= 0; n
< count
; n
++ )
131 if ( m_accels
[n
]->m_command
== id
)
138 void wxMenu::UpdateAccel(wxMenuItem
*item
)
140 // find the (new) accel for this item
141 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
143 accel
->m_command
= item
->GetId();
146 int n
= FindAccel(item
->GetId());
147 if ( n
== wxNOT_FOUND
)
149 // no old, add new if any
153 return; // skipping RebuildAccelTable() below
157 // replace old with new or just remove the old one if no new
167 m_menuBar
->RebuildAccelTable();
171 #endif // wxUSE_ACCEL
173 // append a new item or submenu to the menu
174 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
178 #endif // wxUSE_ACCEL
182 // if "Break" has just been called, insert a menu break before this item
183 // (and don't forget to reset the flag)
185 flags
|= MF_MENUBREAK
;
189 if ( pItem
->IsSeparator() ) {
190 flags
|= MF_SEPARATOR
;
193 // id is the numeric id for normal menu items and HMENU for submenus as
194 // required by ::AppendMenu() API
196 wxMenu
*submenu
= pItem
->GetSubMenu();
197 if ( submenu
!= NULL
) {
198 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
200 submenu
->SetParent(this);
202 id
= (UINT
)submenu
->GetHMenu();
212 #if wxUSE_OWNER_DRAWN
213 if ( pItem
->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
214 // item draws itself, pass pointer to it in data parameter
215 flags
|= MF_OWNERDRAW
;
216 pData
= (LPCTSTR
)pItem
;
221 // menu is just a normal string (passed in data parameter)
224 pData
= (char*)pItem
->GetText().c_str();
228 if ( pos
== (size_t)-1 )
230 ok
= ::AppendMenu(GetHmenu(), flags
, id
, pData
);
234 ok
= ::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
);
239 wxLogLastError("Insert or AppendMenu");
245 // if we just appended the title, highlight it
247 if ( (int)id
== idMenuTitle
)
249 // visually select the menu title
251 mii
.cbSize
= sizeof(mii
);
252 mii
.fMask
= MIIM_STATE
;
253 mii
.fState
= MFS_DEFAULT
;
255 if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id
, FALSE
, &mii
) )
257 wxLogLastError(wxT("SetMenuItemInfo"));
262 // if we're already attached to the menubar, we must update it
265 m_menuBar
->Refresh();
272 bool wxMenu::DoAppend(wxMenuItem
*item
)
274 return wxMenuBase::DoAppend(item
) && DoInsertOrAppend(item
);
277 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
279 return wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
);
282 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
284 // we need to find the items position in the child list
286 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
287 for ( pos
= 0; node
; pos
++ )
289 if ( node
->GetData() == item
)
292 node
= node
->GetNext();
295 // DoRemove() (unlike Remove) can only be called for existing item!
296 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
299 // remove the corresponding accel from the accel table
300 int n
= FindAccel(item
->GetId());
301 if ( n
!= wxNOT_FOUND
)
307 //else: this item doesn't have an accel, nothing to do
308 #endif // wxUSE_ACCEL
310 // remove the item from the menu
311 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
313 wxLogLastError("RemoveMenu");
318 // otherwise, the chane won't be visible
319 m_menuBar
->Refresh();
322 // and from internal data structures
323 return wxMenuBase::DoRemove(item
);
326 // ---------------------------------------------------------------------------
327 // accelerator helpers
328 // ---------------------------------------------------------------------------
332 // create the wxAcceleratorEntries for our accels and put them into provided
333 // array - return the number of accels we have
334 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
336 size_t count
= GetAccelCount();
337 for ( size_t n
= 0; n
< count
; n
++ )
339 *accels
++ = *m_accels
[n
];
345 #endif // wxUSE_ACCEL
347 // ---------------------------------------------------------------------------
349 // ---------------------------------------------------------------------------
351 void wxMenu::SetTitle(const wxString
& label
)
353 bool hasNoTitle
= m_title
.IsEmpty();
356 HMENU hMenu
= GetHmenu();
360 if ( !label
.IsEmpty() )
362 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
363 (unsigned)idMenuTitle
, m_title
) ||
364 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
366 wxLogLastError("InsertMenu");
372 if ( label
.IsEmpty() )
374 // remove the title and the separator after it
375 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
376 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
378 wxLogLastError("RemoveMenu");
384 if ( !ModifyMenu(hMenu
, 0u,
385 MF_BYPOSITION
| MF_STRING
,
386 (unsigned)idMenuTitle
, m_title
) )
388 wxLogLastError("ModifyMenu");
394 // put the title string in bold face
395 if ( !m_title
.IsEmpty() )
398 mii
.cbSize
= sizeof(mii
);
399 mii
.fMask
= MIIM_STATE
;
400 mii
.fState
= MFS_DEFAULT
;
402 if ( !SetMenuItemInfo(hMenu
, (unsigned)idMenuTitle
, FALSE
, &mii
) )
404 wxLogLastError("SetMenuItemInfo");
410 // ---------------------------------------------------------------------------
412 // ---------------------------------------------------------------------------
414 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
416 // ignore commands from the menu title
418 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
419 if ( id
!= (WXWORD
)idMenuTitle
)
421 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
);
422 event
.SetEventObject( this );
425 ProcessCommand(event
);
431 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
433 bool processed
= FALSE
;
435 #if WXWIN_COMPATIBILITY
439 (void)(*(m_callback
))(*this, event
);
442 #endif WXWIN_COMPATIBILITY
444 // Try the menu's event handler
445 if ( !processed
&& GetEventHandler())
447 processed
= GetEventHandler()->ProcessEvent(event
);
450 // Try the window the menu was popped up from (and up through the
452 wxWindow
*win
= GetInvokingWindow();
453 if ( !processed
&& win
)
454 processed
= win
->GetEventHandler()->ProcessEvent(event
);
459 // ---------------------------------------------------------------------------
461 // ---------------------------------------------------------------------------
463 void wxMenu::Attach(wxMenuBar
*menubar
)
465 // menu can be in at most one menubar because otherwise they would both
466 // delete the menu pointer
467 wxASSERT_MSG( !m_menuBar
, wxT("menu belongs to 2 menubars, expect a crash") );
472 void wxMenu::Detach()
474 wxASSERT_MSG( m_menuBar
, wxT("can't detach menu if it's not attached") );
479 wxWindow
*wxMenu::GetWindow() const
481 if ( m_invokingWindow
!= NULL
)
482 return m_invokingWindow
;
483 else if ( m_menuBar
!= NULL
)
484 return m_menuBar
->GetFrame();
489 // ---------------------------------------------------------------------------
491 // ---------------------------------------------------------------------------
493 void wxMenuBar::Init()
495 m_eventHandler
= this;
496 m_menuBarFrame
= NULL
;
500 wxMenuBar::wxMenuBar()
505 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
510 wxMenuBar::wxMenuBar(int count
, wxMenu
*menus
[], const wxString titles
[])
514 m_titles
.Alloc(count
);
516 for ( int i
= 0; i
< count
; i
++ )
518 m_menus
.Append(menus
[i
]);
519 m_titles
.Add(titles
[i
]);
521 menus
[i
]->Attach(this);
525 wxMenuBar::~wxMenuBar()
529 // ---------------------------------------------------------------------------
531 // ---------------------------------------------------------------------------
533 void wxMenuBar::Refresh()
535 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
537 DrawMenuBar(GetHwndOf(m_menuBarFrame
));
540 WXHMENU
wxMenuBar::Create()
545 wxCHECK_MSG( !m_hMenu
, TRUE
, wxT("menubar already created") );
547 m_hMenu
= (WXHMENU
)::CreateMenu();
551 wxLogLastError("CreateMenu");
555 size_t count
= GetMenuCount();
556 for ( size_t i
= 0; i
< count
; i
++ )
558 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
559 (UINT
)m_menus
[i
]->GetHMenu(),
562 wxLogLastError("AppendMenu");
570 // ---------------------------------------------------------------------------
571 // wxMenuBar functions to work with the top level submenus
572 // ---------------------------------------------------------------------------
574 // NB: we don't support owner drawn top level items for now, if we do these
575 // functions would have to be changed to use wxMenuItem as well
577 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
579 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
581 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
583 EnableMenuItem((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
| flag
);
588 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
590 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
592 m_titles
[pos
] = label
;
598 //else: have to modify the existing menu
601 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
);
602 if ( flagsOld
== 0xFFFFFFFF )
604 wxLogLastError(wxT("GetMenuState"));
609 if ( flagsOld
& MF_POPUP
)
611 // HIBYTE contains the number of items in the submenu in this case
613 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, pos
);
620 if ( ::ModifyMenu(GetHmenu(), pos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
621 id
, label
) == (int)0xFFFFFFFF )
623 wxLogLastError("ModifyMenu");
629 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
631 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
632 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
634 return m_titles
[pos
];
637 int wxMenuBar::FindMenu(const wxString
& title
)
639 wxString menuTitle
= wxStripMenuCodes(title
);
641 size_t count
= GetMenuCount();
642 for ( size_t i
= 0; i
< count
; i
++ )
644 wxString title
= wxStripMenuCodes(m_titles
[i
]);
645 if ( menuTitle
== title
)
653 // ---------------------------------------------------------------------------
654 // wxMenuBar construction
655 // ---------------------------------------------------------------------------
657 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
659 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
662 m_titles
[pos
] = title
;
666 // can't use ModifyMenu() because it deletes the submenu it replaces
667 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
669 wxLogLastError("RemoveMenu");
672 if ( !::InsertMenu(GetHmenu(), (UINT
)pos
,
673 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
674 (UINT
)GetHmenuOf(menu
), title
) )
676 wxLogLastError("InsertMenu");
680 if ( menuOld
->HasAccels() || menu
->HasAccels() )
682 // need to rebuild accell table
685 #endif // wxUSE_ACCEL
693 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
695 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
698 m_titles
.Insert(title
, pos
);
704 if ( !::InsertMenu(GetHmenu(), pos
,
705 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
706 (UINT
)GetHmenuOf(menu
), title
) )
708 wxLogLastError("InsertMenu");
712 if ( menu
->HasAccels() )
714 // need to rebuild accell table
717 #endif // wxUSE_ACCEL
725 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
727 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
728 wxCHECK_MSG( submenu
, FALSE
, wxT("can't append invalid menu to menubar") );
730 if ( !wxMenuBarBase::Append(menu
, title
) )
739 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
740 (UINT
)submenu
, title
) )
742 wxLogLastError(wxT("AppendMenu"));
746 if ( menu
->HasAccels() )
748 // need to rebuild accell table
751 #endif // wxUSE_ACCEL
759 wxMenu
*wxMenuBar::Remove(size_t pos
)
761 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
767 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
769 wxLogLastError("RemoveMenu");
775 if ( menu
->HasAccels() )
777 // need to rebuild accell table
780 #endif // wxUSE_ACCEL
785 m_titles
.Remove(pos
);
792 void wxMenuBar::RebuildAccelTable()
794 // merge the accelerators of all menus into one accel table
795 size_t nAccelCount
= 0;
796 size_t i
, count
= GetMenuCount();
797 for ( i
= 0; i
< count
; i
++ )
799 nAccelCount
+= m_menus
[i
]->GetAccelCount();
804 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
807 for ( i
= 0; i
< count
; i
++ )
809 nAccelCount
+= m_menus
[i
]->CopyAccels(&accelEntries
[nAccelCount
]);
812 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
814 delete [] accelEntries
;
818 #endif // wxUSE_ACCEL
820 void wxMenuBar::Attach(wxFrame
*frame
)
822 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
824 m_menuBarFrame
= frame
;
828 #endif // wxUSE_ACCEL
831 void wxMenuBar::Detach()
833 // ::DestroyMenu((HMENU)m_hMenu);
834 m_hMenu
= (WXHMENU
)NULL
;
835 m_menuBarFrame
= NULL
;
839 // ---------------------------------------------------------------------------
840 // wxMenuBar searching for menu items
841 // ---------------------------------------------------------------------------
843 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
844 int wxMenuBar::FindMenuItem(const wxString
& menuString
,
845 const wxString
& itemString
) const
847 wxString menuLabel
= wxStripMenuCodes(menuString
);
848 size_t count
= GetMenuCount();
849 for ( size_t i
= 0; i
< count
; i
++ )
851 wxString title
= wxStripMenuCodes(m_titles
[i
]);
852 if ( menuString
== title
)
853 return m_menus
[i
]->FindItem(itemString
);
859 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
**itemMenu
) const
864 wxMenuItem
*item
= NULL
;
865 size_t count
= GetMenuCount();
866 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
868 item
= m_menus
[i
]->FindItem(id
, itemMenu
);