1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
24 #include "wx/ownerdrw.h"
27 #include "wx/os2/private.h"
29 // other standard headers
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 extern wxMenu
*wxCurrentPopupMenu
;
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // the (popup) menu title has this special id
43 static const int idMenuTitle
= -2;
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 #if !USE_SHARED_LIBRARY
50 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
51 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
54 // ============================================================================
56 // ============================================================================
58 // ---------------------------------------------------------------------------
59 // wxMenu construction, adding and removing menu items
60 // ---------------------------------------------------------------------------
62 // Construct a menu with optional title (then use append)
68 m_hMenu
= (WXHMENU
)0; // CreatePopupMenu();
71 wxLogLastError("CreatePopupMenu");
74 // if we have a title, insert it in the beginning of the menu
77 Append(idMenuTitle
, m_title
);
82 // The wxWindow destructor will take care of deleting the submenus.
85 // we should free Windows resources only if Windows doesn't do it for us
86 // which happens if we're attached to a menubar or a submenu of another
88 if ( !IsAttached() && !GetParent() )
91 if ( !::DestroyMenu(GetHmenu()) )
93 wxLogLastError("DestroyMenu");
100 WX_CLEAR_ARRAY(m_accels
);
101 #endif // wxUSE_ACCEL
106 // this will take effect during the next call to Append()
112 int wxMenu::FindAccel(int id
) const
114 size_t n
, count
= m_accels
.GetCount();
115 for ( n
= 0; n
< count
; n
++ )
117 if ( m_accels
[n
]->m_command
== id
)
124 void wxMenu::UpdateAccel(wxMenuItem
*item
)
126 // find the (new) accel for this item
127 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
129 accel
->m_command
= item
->GetId();
132 int n
= FindAccel(item
->GetId());
133 if ( n
== wxNOT_FOUND
)
135 // no old, add new if any
139 return; // skipping RebuildAccelTable() below
143 // replace old with new or just remove the old one if no new
153 m_menuBar
->RebuildAccelTable();
157 #endif // wxUSE_ACCEL
159 // append a new item or submenu to the menu
160 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
164 #endif // wxUSE_ACCEL
169 // if "Break" has just been called, insert a menu break before this item
170 // (and don't forget to reset the flag)
172 flags |= MF_MENUBREAK;
176 if ( pItem->IsSeparator() ) {
177 flags |= MF_SEPARATOR;
180 // id is the numeric id for normal menu items and HMENU for submenus as
181 // required by ::AppendMenu() API
183 wxMenu *submenu = pItem->GetSubMenu();
184 if ( submenu != NULL ) {
185 wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") );
187 submenu->SetParent(this);
189 id = (UINT)submenu->GetHMenu();
199 #if wxUSE_OWNER_DRAWN
200 if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
201 // item draws itself, pass pointer to it in data parameter
202 flags |= MF_OWNERDRAW;
203 pData = (LPCTSTR)pItem;
208 // menu is just a normal string (passed in data parameter)
211 pData = (char*)pItem->GetText().c_str();
215 if ( pos == (size_t)-1 )
217 ok = ::AppendMenu(GetHmenu(), flags, id, pData);
221 ok = ::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData);
226 wxLogLastError("Insert or AppendMenu");
232 // if we just appended the title, highlight it
234 if ( (int)id == idMenuTitle )
236 // visually select the menu title
238 mii.cbSize = sizeof(mii);
239 mii.fMask = MIIM_STATE;
240 mii.fState = MFS_DEFAULT;
242 if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id, FALSE, &mii) )
244 wxLogLastError(wxT("SetMenuItemInfo"));
249 // if we're already attached to the menubar, we must update it
252 m_menuBar->Refresh();
261 bool wxMenu::DoAppend(wxMenuItem
*item
)
263 return wxMenuBase::DoAppend(item
) && DoInsertOrAppend(item
);
266 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
268 return wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
);
271 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
273 // we need to find the items position in the child list
275 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
276 for ( pos
= 0; node
; pos
++ )
278 if ( node
->GetData() == item
)
281 node
= node
->GetNext();
284 // DoRemove() (unlike Remove) can only be called for existing item!
285 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
288 // remove the corresponding accel from the accel table
289 int n
= FindAccel(item
->GetId());
290 if ( n
!= wxNOT_FOUND
)
296 //else: this item doesn't have an accel, nothing to do
297 #endif // wxUSE_ACCEL
299 // remove the item from the menu
300 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
302 wxLogLastError("RemoveMenu");
307 // otherwise, the chane won't be visible
308 m_menuBar
->Refresh();
311 // and from internal data structures
312 return wxMenuBase::DoRemove(item
);
315 // ---------------------------------------------------------------------------
316 // accelerator helpers
317 // ---------------------------------------------------------------------------
321 // create the wxAcceleratorEntries for our accels and put them into provided
322 // array - return the number of accels we have
323 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
325 size_t count
= GetAccelCount();
326 for ( size_t n
= 0; n
< count
; n
++ )
328 *accels
++ = *m_accels
[n
];
334 #endif // wxUSE_ACCEL
336 // ---------------------------------------------------------------------------
338 // ---------------------------------------------------------------------------
340 void wxMenu::SetTitle(const wxString
& label
)
342 bool hasNoTitle
= m_title
.IsEmpty();
345 HMENU hMenu
= GetHmenu();
349 if ( !label
.IsEmpty() )
352 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
353 (unsigned)idMenuTitle, m_title) ||
354 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
356 wxLogLastError("InsertMenu");
363 if ( label
.IsEmpty() )
366 // remove the title and the separator after it
367 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
368 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
370 wxLogLastError("RemoveMenu");
378 if ( !ModifyMenu(hMenu, 0u,
379 MF_BYPOSITION | MF_STRING,
380 (unsigned)idMenuTitle, m_title) )
382 wxLogLastError("ModifyMenu");
389 // put the title string in bold face
390 if ( !m_title.IsEmpty() )
393 mii.cbSize = sizeof(mii);
394 mii.fMask = MIIM_STATE;
395 mii.fState = MFS_DEFAULT;
397 if ( !SetMenuItemInfo(hMenu, (unsigned)idMenuTitle, FALSE, &mii) )
399 wxLogLastError("SetMenuItemInfo");
406 // ---------------------------------------------------------------------------
408 // ---------------------------------------------------------------------------
410 bool wxMenu::OS2Command(WXUINT
WXUNUSED(param
), WXWORD id
)
412 // ignore commands from the menu title
414 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
415 if ( id
!= (WXWORD
)idMenuTitle
)
417 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
);
418 event
.SetEventObject( this );
421 ProcessCommand(event
);
427 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
429 bool processed
= FALSE
;
431 #if WXWIN_COMPATIBILITY
435 (void)(*(m_callback
))(*this, event
);
438 #endif // WXWIN_COMPATIBILITY
440 // Try the menu's event handler
441 if ( !processed
&& GetEventHandler())
443 processed
= GetEventHandler()->ProcessEvent(event
);
446 // Try the window the menu was popped up from (and up through the
448 wxWindow
*win
= GetInvokingWindow();
449 if ( !processed
&& win
)
450 processed
= win
->GetEventHandler()->ProcessEvent(event
);
455 // ---------------------------------------------------------------------------
457 // ---------------------------------------------------------------------------
459 void wxMenu::Attach(wxMenuBar
*menubar
)
461 // menu can be in at most one menubar because otherwise they would both
462 // delete the menu pointer
463 wxASSERT_MSG( !m_menuBar
, wxT("menu belongs to 2 menubars, expect a crash") );
468 void wxMenu::Detach()
470 wxASSERT_MSG( m_menuBar
, wxT("can't detach menu if it's not attached") );
475 wxWindow
*wxMenu::GetWindow() const
477 if ( m_invokingWindow
!= NULL
)
478 return m_invokingWindow
;
479 else if ( m_menuBar
!= NULL
)
480 return m_menuBar
->GetFrame();
485 // ---------------------------------------------------------------------------
487 // ---------------------------------------------------------------------------
489 void wxMenuBar::Init()
491 m_eventHandler
= this;
492 m_menuBarFrame
= NULL
;
496 wxMenuBar::wxMenuBar()
501 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
506 wxMenuBar::wxMenuBar(int count
, wxMenu
*menus
[], const wxString titles
[])
510 m_titles
.Alloc(count
);
512 for ( int i
= 0; i
< count
; i
++ )
514 m_menus
.Append(menus
[i
]);
515 m_titles
.Add(titles
[i
]);
517 menus
[i
]->Attach(this);
521 wxMenuBar::~wxMenuBar()
525 // ---------------------------------------------------------------------------
527 // ---------------------------------------------------------------------------
529 void wxMenuBar::Refresh(
530 bool WXUNUSED(bEraseBackground
)
531 , const wxRect
* WXUNUSED(pRect
)
534 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
536 // DrawMenuBar(GetHwndOf(m_menuBarFrame));
539 WXHMENU
wxMenuBar::Create()
544 wxCHECK_MSG( !m_hMenu
, TRUE
, wxT("menubar already created") );
549 m_hMenu = (WXHMENU)::CreateMenu();
553 wxLogLastError("CreateMenu");
557 size_t count = GetMenuCount();
558 for ( size_t i = 0; i < count; i++ )
560 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
561 (UINT)m_menus[i]->GetHMenu(),
564 wxLogLastError("AppendMenu");
574 // ---------------------------------------------------------------------------
575 // wxMenuBar functions to work with the top level submenus
576 // ---------------------------------------------------------------------------
578 // NB: we don't support owner drawn top level items for now, if we do these
579 // functions would have to be changed to use wxMenuItem as well
581 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
583 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
585 // int flag = enable ? MF_ENABLED : MF_GRAYED;
587 // EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag);
592 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
594 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
596 m_titles
[pos
] = label
;
602 //else: have to modify the existing menu
607 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
608 if ( flagsOld == 0xFFFFFFFF )
610 wxLogLastError(wxT("GetMenuState"));
615 if ( flagsOld & MF_POPUP )
617 // HIBYTE contains the number of items in the submenu in this case
619 id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos);
626 if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld,
627 id, label) == (int)0xFFFFFFFF )
629 wxLogLastError("ModifyMenu");
635 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
637 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
638 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
640 return m_titles
[pos
];
643 int wxMenuBar::FindMenu(const wxString
& title
)
645 wxString menuTitle
= wxStripMenuCodes(title
);
647 size_t count
= GetMenuCount();
648 for ( size_t i
= 0; i
< count
; i
++ )
650 wxString title
= wxStripMenuCodes(m_titles
[i
]);
651 if ( menuTitle
== title
)
659 // ---------------------------------------------------------------------------
660 // wxMenuBar construction
661 // ---------------------------------------------------------------------------
663 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
665 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
668 m_titles
[pos
] = title
;
673 // can't use ModifyMenu() because it deletes the submenu it replaces
674 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
676 wxLogLastError("RemoveMenu");
679 if ( !::InsertMenu(GetHmenu(), (UINT)pos,
680 MF_BYPOSITION | MF_POPUP | MF_STRING,
681 (UINT)GetHmenuOf(menu), title) )
683 wxLogLastError("InsertMenu");
687 if ( menuOld->HasAccels() || menu->HasAccels() )
689 // need to rebuild accell table
692 #endif // wxUSE_ACCEL
700 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
702 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
705 m_titles
.Insert(title
, pos
);
712 if ( !::InsertMenu(GetHmenu(), pos,
713 MF_BYPOSITION | MF_POPUP | MF_STRING,
714 (UINT)GetHmenuOf(menu), title) )
716 wxLogLastError("InsertMenu");
720 if ( menu->HasAccels() )
722 // need to rebuild accell table
725 #endif // wxUSE_ACCEL
733 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
735 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
736 wxCHECK_MSG( submenu
, FALSE
, wxT("can't append invalid menu to menubar") );
738 if ( !wxMenuBarBase::Append(menu
, title
) )
748 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
749 (UINT)submenu, title) )
751 wxLogLastError(wxT("AppendMenu"));
755 if ( menu->HasAccels() )
757 // need to rebuild accell table
760 #endif // wxUSE_ACCEL
768 wxMenu
*wxMenuBar::Remove(size_t pos
)
770 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
777 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
779 wxLogLastError("RemoveMenu");
785 if ( menu->HasAccels() )
787 // need to rebuild accell table
790 #endif // wxUSE_ACCEL
795 m_titles.Remove(pos);
802 void wxMenuBar::RebuildAccelTable()
804 // merge the accelerators of all menus into one accel table
805 size_t nAccelCount
= 0;
806 size_t i
, count
= GetMenuCount();
807 for ( i
= 0; i
< count
; i
++ )
809 nAccelCount
+= m_menus
[i
]->GetAccelCount();
814 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
817 for ( i
= 0; i
< count
; i
++ )
819 nAccelCount
+= m_menus
[i
]->CopyAccels(&accelEntries
[nAccelCount
]);
822 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
824 delete [] accelEntries
;
828 #endif // wxUSE_ACCEL
830 void wxMenuBar::Attach(wxFrame
*frame
)
832 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
834 m_menuBarFrame
= frame
;
838 #endif // wxUSE_ACCEL
841 void wxMenuBar::Detach()
843 // ::DestroyMenu((HMENU)m_hMenu);
844 m_hMenu
= (WXHMENU
)NULL
;
845 m_menuBarFrame
= NULL
;
849 // ---------------------------------------------------------------------------
850 // wxMenuBar searching for menu items
851 // ---------------------------------------------------------------------------
853 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
854 int wxMenuBar::FindMenuItem(const wxString
& menuString
,
855 const wxString
& itemString
) const
857 wxString menuLabel
= wxStripMenuCodes(menuString
);
858 size_t count
= GetMenuCount();
859 for ( size_t i
= 0; i
< count
; i
++ )
861 wxString title
= wxStripMenuCodes(m_titles
[i
]);
862 if ( menuString
== title
)
863 return m_menus
[i
]->FindItem(itemString
);
869 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
**itemMenu
) const
874 wxMenuItem
*item
= NULL
;
875 size_t count
= GetMenuCount();
876 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
878 item
= m_menus
[i
]->FindItem(id
, itemMenu
);