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 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
50 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
52 // ============================================================================
54 // ============================================================================
56 // ---------------------------------------------------------------------------
57 // wxMenu construction, adding and removing menu items
58 // ---------------------------------------------------------------------------
60 // Construct a menu with optional title (then use append)
66 m_hMenu
= (WXHMENU
)0; // CreatePopupMenu();
69 wxLogLastError("CreatePopupMenu");
72 // if we have a title, insert it in the beginning of the menu
75 Append(idMenuTitle
, m_title
);
80 // The wxWindow destructor will take care of deleting the submenus.
83 // we should free Windows resources only if Windows doesn't do it for us
84 // which happens if we're attached to a menubar or a submenu of another
86 if ( !IsAttached() && !GetParent() )
89 if ( !::DestroyMenu(GetHmenu()) )
91 wxLogLastError("DestroyMenu");
98 WX_CLEAR_ARRAY(m_accels
);
104 // this will take effect during the next call to Append()
110 int wxMenu::FindAccel(int id
) const
112 size_t n
, count
= m_accels
.GetCount();
113 for ( n
= 0; n
< count
; n
++ )
115 if ( m_accels
[n
]->m_command
== id
)
122 void wxMenu::UpdateAccel(wxMenuItem
*item
)
124 // find the (new) accel for this item
125 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
127 accel
->m_command
= item
->GetId();
130 int n
= FindAccel(item
->GetId());
131 if ( n
== wxNOT_FOUND
)
133 // no old, add new if any
137 return; // skipping RebuildAccelTable() below
141 // replace old with new or just remove the old one if no new
151 m_menuBar
->RebuildAccelTable();
155 #endif // wxUSE_ACCEL
157 // append a new item or submenu to the menu
158 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
162 #endif // wxUSE_ACCEL
167 // if "Break" has just been called, insert a menu break before this item
168 // (and don't forget to reset the flag)
170 flags |= MF_MENUBREAK;
174 if ( pItem->IsSeparator() ) {
175 flags |= MF_SEPARATOR;
178 // id is the numeric id for normal menu items and HMENU for submenus as
179 // required by ::AppendMenu() API
181 wxMenu *submenu = pItem->GetSubMenu();
182 if ( submenu != NULL ) {
183 wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") );
185 submenu->SetParent(this);
187 id = (UINT)submenu->GetHMenu();
197 #if wxUSE_OWNER_DRAWN
198 if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
199 // item draws itself, pass pointer to it in data parameter
200 flags |= MF_OWNERDRAW;
201 pData = (LPCTSTR)pItem;
206 // menu is just a normal string (passed in data parameter)
209 pData = (char*)pItem->GetText().c_str();
213 if ( pos == (size_t)-1 )
215 ok = ::AppendMenu(GetHmenu(), flags, id, pData);
219 ok = ::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData);
224 wxLogLastError("Insert or AppendMenu");
230 // if we just appended the title, highlight it
232 if ( (int)id == idMenuTitle )
234 // visually select the menu title
236 mii.cbSize = sizeof(mii);
237 mii.fMask = MIIM_STATE;
238 mii.fState = MFS_DEFAULT;
240 if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id, FALSE, &mii) )
242 wxLogLastError(wxT("SetMenuItemInfo"));
247 // if we're already attached to the menubar, we must update it
250 m_menuBar->Refresh();
259 bool wxMenu::DoAppend(wxMenuItem
*item
)
261 return wxMenuBase::DoAppend(item
) && DoInsertOrAppend(item
);
264 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
266 return wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
);
269 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
271 // we need to find the items position in the child list
273 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
274 for ( pos
= 0; node
; pos
++ )
276 if ( node
->GetData() == item
)
279 node
= node
->GetNext();
282 // DoRemove() (unlike Remove) can only be called for existing item!
283 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
286 // remove the corresponding accel from the accel table
287 int n
= FindAccel(item
->GetId());
288 if ( n
!= wxNOT_FOUND
)
294 //else: this item doesn't have an accel, nothing to do
295 #endif // wxUSE_ACCEL
297 // remove the item from the menu
298 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
300 wxLogLastError("RemoveMenu");
305 // otherwise, the chane won't be visible
306 m_menuBar
->Refresh();
309 // and from internal data structures
310 return wxMenuBase::DoRemove(item
);
313 // ---------------------------------------------------------------------------
314 // accelerator helpers
315 // ---------------------------------------------------------------------------
319 // create the wxAcceleratorEntries for our accels and put them into provided
320 // array - return the number of accels we have
321 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
323 size_t count
= GetAccelCount();
324 for ( size_t n
= 0; n
< count
; n
++ )
326 *accels
++ = *m_accels
[n
];
332 #endif // wxUSE_ACCEL
334 // ---------------------------------------------------------------------------
336 // ---------------------------------------------------------------------------
338 void wxMenu::SetTitle(const wxString
& label
)
340 bool hasNoTitle
= m_title
.IsEmpty();
343 HMENU hMenu
= GetHmenu();
347 if ( !label
.IsEmpty() )
350 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
351 (unsigned)idMenuTitle, m_title) ||
352 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
354 wxLogLastError("InsertMenu");
361 if ( label
.IsEmpty() )
364 // remove the title and the separator after it
365 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
366 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
368 wxLogLastError("RemoveMenu");
376 if ( !ModifyMenu(hMenu, 0u,
377 MF_BYPOSITION | MF_STRING,
378 (unsigned)idMenuTitle, m_title) )
380 wxLogLastError("ModifyMenu");
387 // put the title string in bold face
388 if ( !m_title.IsEmpty() )
391 mii.cbSize = sizeof(mii);
392 mii.fMask = MIIM_STATE;
393 mii.fState = MFS_DEFAULT;
395 if ( !SetMenuItemInfo(hMenu, (unsigned)idMenuTitle, FALSE, &mii) )
397 wxLogLastError("SetMenuItemInfo");
404 // ---------------------------------------------------------------------------
406 // ---------------------------------------------------------------------------
408 bool wxMenu::OS2Command(WXUINT
WXUNUSED(param
), WXWORD id
)
410 // ignore commands from the menu title
412 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
413 if ( id
!= (WXWORD
)idMenuTitle
)
415 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
);
416 event
.SetEventObject( this );
419 ProcessCommand(event
);
425 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
427 bool processed
= FALSE
;
429 #if WXWIN_COMPATIBILITY
433 (void)(*(m_callback
))(*this, event
);
436 #endif // WXWIN_COMPATIBILITY
438 // Try the menu's event handler
439 if ( !processed
&& GetEventHandler())
441 processed
= GetEventHandler()->ProcessEvent(event
);
444 // Try the window the menu was popped up from (and up through the
446 wxWindow
*win
= GetInvokingWindow();
447 if ( !processed
&& win
)
448 processed
= win
->GetEventHandler()->ProcessEvent(event
);
453 // ---------------------------------------------------------------------------
455 // ---------------------------------------------------------------------------
457 void wxMenu::Attach(wxMenuBar
*menubar
)
459 // menu can be in at most one menubar because otherwise they would both
460 // delete the menu pointer
461 wxASSERT_MSG( !m_menuBar
, wxT("menu belongs to 2 menubars, expect a crash") );
466 void wxMenu::Detach()
468 wxASSERT_MSG( m_menuBar
, wxT("can't detach menu if it's not attached") );
473 wxWindow
*wxMenu::GetWindow() const
475 if ( m_invokingWindow
!= NULL
)
476 return m_invokingWindow
;
477 else if ( m_menuBar
!= NULL
)
478 return m_menuBar
->GetFrame();
483 // ---------------------------------------------------------------------------
485 // ---------------------------------------------------------------------------
487 void wxMenuBar::Init()
489 m_eventHandler
= this;
490 m_menuBarFrame
= NULL
;
494 wxMenuBar::wxMenuBar()
499 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
504 wxMenuBar::wxMenuBar(int count
, wxMenu
*menus
[], const wxString titles
[])
508 m_titles
.Alloc(count
);
510 for ( int i
= 0; i
< count
; i
++ )
512 m_menus
.Append(menus
[i
]);
513 m_titles
.Add(titles
[i
]);
515 menus
[i
]->Attach(this);
519 wxMenuBar::~wxMenuBar()
523 // ---------------------------------------------------------------------------
525 // ---------------------------------------------------------------------------
527 void wxMenuBar::Refresh(
528 bool WXUNUSED(bEraseBackground
)
529 , const wxRect
* WXUNUSED(pRect
)
532 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
534 // DrawMenuBar(GetHwndOf(m_menuBarFrame));
537 WXHMENU
wxMenuBar::Create()
542 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");
572 // ---------------------------------------------------------------------------
573 // wxMenuBar functions to work with the top level submenus
574 // ---------------------------------------------------------------------------
576 // NB: we don't support owner drawn top level items for now, if we do these
577 // functions would have to be changed to use wxMenuItem as well
579 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
581 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
583 // int flag = enable ? MF_ENABLED : MF_GRAYED;
585 // EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag);
590 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
592 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
594 m_titles
[pos
] = label
;
600 //else: have to modify the existing menu
605 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
606 if ( flagsOld == 0xFFFFFFFF )
608 wxLogLastError(wxT("GetMenuState"));
613 if ( flagsOld & MF_POPUP )
615 // HIBYTE contains the number of items in the submenu in this case
617 id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos);
624 if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld,
625 id, label) == (int)0xFFFFFFFF )
627 wxLogLastError("ModifyMenu");
633 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
635 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
636 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
638 return m_titles
[pos
];
641 int wxMenuBar::FindMenu(const wxString
& title
)
643 wxString menuTitle
= wxStripMenuCodes(title
);
645 size_t count
= GetMenuCount();
646 for ( size_t i
= 0; i
< count
; i
++ )
648 wxString title
= wxStripMenuCodes(m_titles
[i
]);
649 if ( menuTitle
== title
)
657 // ---------------------------------------------------------------------------
658 // wxMenuBar construction
659 // ---------------------------------------------------------------------------
661 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
663 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
666 m_titles
[pos
] = title
;
671 // can't use ModifyMenu() because it deletes the submenu it replaces
672 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
674 wxLogLastError("RemoveMenu");
677 if ( !::InsertMenu(GetHmenu(), (UINT)pos,
678 MF_BYPOSITION | MF_POPUP | MF_STRING,
679 (UINT)GetHmenuOf(menu), title) )
681 wxLogLastError("InsertMenu");
685 if ( menuOld->HasAccels() || menu->HasAccels() )
687 // need to rebuild accell table
690 #endif // wxUSE_ACCEL
698 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
700 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
703 m_titles
.Insert(title
, pos
);
710 if ( !::InsertMenu(GetHmenu(), pos,
711 MF_BYPOSITION | MF_POPUP | MF_STRING,
712 (UINT)GetHmenuOf(menu), title) )
714 wxLogLastError("InsertMenu");
718 if ( menu->HasAccels() )
720 // need to rebuild accell table
723 #endif // wxUSE_ACCEL
731 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
733 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
734 wxCHECK_MSG( submenu
, FALSE
, wxT("can't append invalid menu to menubar") );
736 if ( !wxMenuBarBase::Append(menu
, title
) )
746 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
747 (UINT)submenu, title) )
749 wxLogLastError(wxT("AppendMenu"));
753 if ( menu->HasAccels() )
755 // need to rebuild accell table
758 #endif // wxUSE_ACCEL
766 wxMenu
*wxMenuBar::Remove(size_t pos
)
768 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
775 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
777 wxLogLastError("RemoveMenu");
783 if ( menu->HasAccels() )
785 // need to rebuild accell table
788 #endif // wxUSE_ACCEL
793 m_titles.Remove(pos);
800 void wxMenuBar::RebuildAccelTable()
802 // merge the accelerators of all menus into one accel table
803 size_t nAccelCount
= 0;
804 size_t i
, count
= GetMenuCount();
805 for ( i
= 0; i
< count
; i
++ )
807 nAccelCount
+= m_menus
[i
]->GetAccelCount();
812 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
815 for ( i
= 0; i
< count
; i
++ )
817 nAccelCount
+= m_menus
[i
]->CopyAccels(&accelEntries
[nAccelCount
]);
820 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
822 delete [] accelEntries
;
826 #endif // wxUSE_ACCEL
828 void wxMenuBar::Attach(wxFrame
*frame
)
830 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
832 m_menuBarFrame
= frame
;
836 #endif // wxUSE_ACCEL
839 void wxMenuBar::Detach()
841 // ::DestroyMenu((HMENU)m_hMenu);
842 m_hMenu
= (WXHMENU
)NULL
;
843 m_menuBarFrame
= NULL
;
847 // ---------------------------------------------------------------------------
848 // wxMenuBar searching for menu items
849 // ---------------------------------------------------------------------------
851 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
852 int wxMenuBar::FindMenuItem(const wxString
& menuString
,
853 const wxString
& itemString
) const
855 wxString menuLabel
= wxStripMenuCodes(menuString
);
856 size_t count
= GetMenuCount();
857 for ( size_t i
= 0; i
< count
; i
++ )
859 wxString title
= wxStripMenuCodes(m_titles
[i
]);
860 if ( menuString
== title
)
861 return m_menus
[i
]->FindItem(itemString
);
867 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
**itemMenu
) const
872 wxMenuItem
*item
= NULL
;
873 size_t count
= GetMenuCount();
874 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
876 item
= m_menus
[i
]->FindItem(id
, itemMenu
);