1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "menu.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
28 #include "wx/ownerdrw.h"
31 #include "wx/os2/private.h"
33 // other standard headers
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 extern wxMenu
*wxCurrentPopupMenu
;
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // the (popup) menu title has this special id
47 static const int idMenuTitle
= -2;
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
54 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
56 // ============================================================================
58 // ============================================================================
60 // ---------------------------------------------------------------------------
61 // wxMenu construction, adding and removing menu items
62 // ---------------------------------------------------------------------------
64 // Construct a menu with optional title (then use append)
70 m_hMenu
= (WXHMENU
)0; // CreatePopupMenu();
73 wxLogLastError("CreatePopupMenu");
76 // if we have a title, insert it in the beginning of the menu
79 Append(idMenuTitle
, m_title
);
84 // The wxWindow destructor will take care of deleting the submenus.
87 // we should free Windows resources only if Windows doesn't do it for us
88 // which happens if we're attached to a menubar or a submenu of another
90 if ( !IsAttached() && !GetParent() )
93 if ( !::DestroyMenu(GetHmenu()) )
95 wxLogLastError("DestroyMenu");
102 WX_CLEAR_ARRAY(m_accels
);
103 #endif // wxUSE_ACCEL
108 // this will take effect during the next call to Append()
114 int wxMenu::FindAccel(int id
) const
116 size_t n
, count
= m_accels
.GetCount();
117 for ( n
= 0; n
< count
; n
++ )
119 if ( m_accels
[n
]->m_command
== id
)
126 void wxMenu::UpdateAccel(wxMenuItem
*item
)
128 // find the (new) accel for this item
129 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
131 accel
->m_command
= item
->GetId();
134 int n
= FindAccel(item
->GetId());
135 if ( n
== wxNOT_FOUND
)
137 // no old, add new if any
141 return; // skipping RebuildAccelTable() below
145 // replace old with new or just remove the old one if no new
155 m_menuBar
->RebuildAccelTable();
159 #endif // wxUSE_ACCEL
161 // append a new item or submenu to the menu
162 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
166 #endif // wxUSE_ACCEL
171 // if "Break" has just been called, insert a menu break before this item
172 // (and don't forget to reset the flag)
174 flags |= MF_MENUBREAK;
178 if ( pItem->IsSeparator() ) {
179 flags |= MF_SEPARATOR;
182 // id is the numeric id for normal menu items and HMENU for submenus as
183 // required by ::AppendMenu() API
185 wxMenu *submenu = pItem->GetSubMenu();
186 if ( submenu != NULL ) {
187 wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") );
189 submenu->SetParent(this);
191 id = (UINT)submenu->GetHMenu();
201 #if wxUSE_OWNER_DRAWN
202 if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
203 // item draws itself, pass pointer to it in data parameter
204 flags |= MF_OWNERDRAW;
205 pData = (LPCTSTR)pItem;
210 // menu is just a normal string (passed in data parameter)
213 pData = (char*)pItem->GetText().c_str();
217 if ( pos == (size_t)-1 )
219 ok = ::AppendMenu(GetHmenu(), flags, id, pData);
223 ok = ::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData);
228 wxLogLastError("Insert or AppendMenu");
234 // if we just appended the title, highlight it
236 if ( (int)id == idMenuTitle )
238 // visually select the menu title
240 mii.cbSize = sizeof(mii);
241 mii.fMask = MIIM_STATE;
242 mii.fState = MFS_DEFAULT;
244 if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id, FALSE, &mii) )
246 wxLogLastError(wxT("SetMenuItemInfo"));
251 // if we're already attached to the menubar, we must update it
254 m_menuBar->Refresh();
263 bool wxMenu::DoAppend(wxMenuItem
*item
)
265 return wxMenuBase::DoAppend(item
) && DoInsertOrAppend(item
);
268 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
270 return wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
);
273 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
275 // we need to find the items position in the child list
277 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
278 for ( pos
= 0; node
; pos
++ )
280 if ( node
->GetData() == item
)
283 node
= node
->GetNext();
286 // DoRemove() (unlike Remove) can only be called for existing item!
287 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
290 // remove the corresponding accel from the accel table
291 int n
= FindAccel(item
->GetId());
292 if ( n
!= wxNOT_FOUND
)
298 //else: this item doesn't have an accel, nothing to do
299 #endif // wxUSE_ACCEL
301 // remove the item from the menu
302 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
304 wxLogLastError("RemoveMenu");
309 // otherwise, the chane won't be visible
310 m_menuBar
->Refresh();
313 // and from internal data structures
314 return wxMenuBase::DoRemove(item
);
317 // ---------------------------------------------------------------------------
318 // accelerator helpers
319 // ---------------------------------------------------------------------------
323 // create the wxAcceleratorEntries for our accels and put them into provided
324 // array - return the number of accels we have
325 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
327 size_t count
= GetAccelCount();
328 for ( size_t n
= 0; n
< count
; n
++ )
330 *accels
++ = *m_accels
[n
];
336 #endif // wxUSE_ACCEL
338 // ---------------------------------------------------------------------------
340 // ---------------------------------------------------------------------------
342 void wxMenu::SetTitle(const wxString
& label
)
344 bool hasNoTitle
= m_title
.IsEmpty();
347 HMENU hMenu
= GetHmenu();
351 if ( !label
.IsEmpty() )
354 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
355 (unsigned)idMenuTitle, m_title) ||
356 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
358 wxLogLastError("InsertMenu");
365 if ( label
.IsEmpty() )
368 // remove the title and the separator after it
369 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
370 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
372 wxLogLastError("RemoveMenu");
380 if ( !ModifyMenu(hMenu, 0u,
381 MF_BYPOSITION | MF_STRING,
382 (unsigned)idMenuTitle, m_title) )
384 wxLogLastError("ModifyMenu");
391 // put the title string in bold face
392 if ( !m_title.IsEmpty() )
395 mii.cbSize = sizeof(mii);
396 mii.fMask = MIIM_STATE;
397 mii.fState = MFS_DEFAULT;
399 if ( !SetMenuItemInfo(hMenu, (unsigned)idMenuTitle, FALSE, &mii) )
401 wxLogLastError("SetMenuItemInfo");
408 // ---------------------------------------------------------------------------
410 // ---------------------------------------------------------------------------
412 bool wxMenu::OS2Command(WXUINT
WXUNUSED(param
), WXWORD id
)
414 // ignore commands from the menu title
416 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
417 if ( id
!= (WXWORD
)idMenuTitle
)
419 wxCommandEvent
event(wxEVT_COMMAND_MENU_SELECTED
);
420 event
.SetEventObject( this );
423 ProcessCommand(event
);
429 bool wxMenu::ProcessCommand(wxCommandEvent
& event
)
431 bool processed
= FALSE
;
433 #if WXWIN_COMPATIBILITY
437 (void)(*(m_callback
))(*this, event
);
440 #endif // WXWIN_COMPATIBILITY
442 // Try the menu's event handler
443 if ( !processed
&& GetEventHandler())
445 processed
= GetEventHandler()->ProcessEvent(event
);
448 // Try the window the menu was popped up from (and up through the
450 wxWindow
*win
= GetInvokingWindow();
451 if ( !processed
&& win
)
452 processed
= win
->GetEventHandler()->ProcessEvent(event
);
457 // ---------------------------------------------------------------------------
459 // ---------------------------------------------------------------------------
461 void wxMenu::Attach(wxMenuBar
*menubar
)
463 // menu can be in at most one menubar because otherwise they would both
464 // delete the menu pointer
465 wxASSERT_MSG( !m_menuBar
, wxT("menu belongs to 2 menubars, expect a crash") );
470 void wxMenu::Detach()
472 wxASSERT_MSG( m_menuBar
, wxT("can't detach menu if it's not attached") );
477 wxWindow
*wxMenu::GetWindow() const
479 if ( m_invokingWindow
!= NULL
)
480 return m_invokingWindow
;
481 else if ( m_menuBar
!= NULL
)
482 return m_menuBar
->GetFrame();
487 // ---------------------------------------------------------------------------
489 // ---------------------------------------------------------------------------
491 void wxMenuBar::Init()
493 m_eventHandler
= this;
494 m_menuBarFrame
= NULL
;
498 wxMenuBar::wxMenuBar()
503 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
508 wxMenuBar::wxMenuBar(int count
, wxMenu
*menus
[], const wxString titles
[])
512 m_titles
.Alloc(count
);
514 for ( int i
= 0; i
< count
; i
++ )
516 m_menus
.Append(menus
[i
]);
517 m_titles
.Add(titles
[i
]);
519 menus
[i
]->Attach(this);
523 wxMenuBar::~wxMenuBar()
527 // ---------------------------------------------------------------------------
529 // ---------------------------------------------------------------------------
531 void wxMenuBar::Refresh(
532 bool WXUNUSED(bEraseBackground
)
533 , const wxRect
* WXUNUSED(pRect
)
536 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
538 // DrawMenuBar(GetHwndOf(m_menuBarFrame));
541 WXHMENU
wxMenuBar::Create()
546 wxCHECK_MSG( !m_hMenu
, TRUE
, wxT("menubar already created") );
551 m_hMenu = (WXHMENU)::CreateMenu();
555 wxLogLastError("CreateMenu");
559 size_t count = GetMenuCount();
560 for ( size_t i = 0; i < count; i++ )
562 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
563 (UINT)m_menus[i]->GetHMenu(),
566 wxLogLastError("AppendMenu");
576 // ---------------------------------------------------------------------------
577 // wxMenuBar functions to work with the top level submenus
578 // ---------------------------------------------------------------------------
580 // NB: we don't support owner drawn top level items for now, if we do these
581 // functions would have to be changed to use wxMenuItem as well
583 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
585 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
587 // int flag = enable ? MF_ENABLED : MF_GRAYED;
589 // EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag);
594 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
596 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
598 m_titles
[pos
] = label
;
604 //else: have to modify the existing menu
609 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
610 if ( flagsOld == 0xFFFFFFFF )
612 wxLogLastError(wxT("GetMenuState"));
617 if ( flagsOld & MF_POPUP )
619 // HIBYTE contains the number of items in the submenu in this case
621 id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos);
628 if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld,
629 id, label) == (int)0xFFFFFFFF )
631 wxLogLastError("ModifyMenu");
637 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
639 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
640 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
642 return m_titles
[pos
];
645 int wxMenuBar::FindMenu(const wxString
& title
)
647 wxString menuTitle
= wxStripMenuCodes(title
);
649 size_t count
= GetMenuCount();
650 for ( size_t i
= 0; i
< count
; i
++ )
652 wxString title
= wxStripMenuCodes(m_titles
[i
]);
653 if ( menuTitle
== title
)
661 // ---------------------------------------------------------------------------
662 // wxMenuBar construction
663 // ---------------------------------------------------------------------------
665 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
667 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
670 m_titles
[pos
] = title
;
675 // can't use ModifyMenu() because it deletes the submenu it replaces
676 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
678 wxLogLastError("RemoveMenu");
681 if ( !::InsertMenu(GetHmenu(), (UINT)pos,
682 MF_BYPOSITION | MF_POPUP | MF_STRING,
683 (UINT)GetHmenuOf(menu), title) )
685 wxLogLastError("InsertMenu");
689 if ( menuOld->HasAccels() || menu->HasAccels() )
691 // need to rebuild accell table
694 #endif // wxUSE_ACCEL
702 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
704 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
707 m_titles
.Insert(title
, pos
);
714 if ( !::InsertMenu(GetHmenu(), pos,
715 MF_BYPOSITION | MF_POPUP | MF_STRING,
716 (UINT)GetHmenuOf(menu), title) )
718 wxLogLastError("InsertMenu");
722 if ( menu->HasAccels() )
724 // need to rebuild accell table
727 #endif // wxUSE_ACCEL
735 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
737 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
738 wxCHECK_MSG( submenu
, FALSE
, wxT("can't append invalid menu to menubar") );
740 if ( !wxMenuBarBase::Append(menu
, title
) )
750 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
751 (UINT)submenu, title) )
753 wxLogLastError(wxT("AppendMenu"));
757 if ( menu->HasAccels() )
759 // need to rebuild accell table
762 #endif // wxUSE_ACCEL
770 wxMenu
*wxMenuBar::Remove(size_t pos
)
772 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
779 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
781 wxLogLastError("RemoveMenu");
787 if ( menu->HasAccels() )
789 // need to rebuild accell table
792 #endif // wxUSE_ACCEL
797 m_titles.Remove(pos);
804 void wxMenuBar::RebuildAccelTable()
806 // merge the accelerators of all menus into one accel table
807 size_t nAccelCount
= 0;
808 size_t i
, count
= GetMenuCount();
809 for ( i
= 0; i
< count
; i
++ )
811 nAccelCount
+= m_menus
[i
]->GetAccelCount();
816 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
819 for ( i
= 0; i
< count
; i
++ )
821 nAccelCount
+= m_menus
[i
]->CopyAccels(&accelEntries
[nAccelCount
]);
824 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
826 delete [] accelEntries
;
830 #endif // wxUSE_ACCEL
832 void wxMenuBar::Attach(wxFrame
*frame
)
834 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
836 m_menuBarFrame
= frame
;
840 #endif // wxUSE_ACCEL
843 void wxMenuBar::Detach()
845 // ::DestroyMenu((HMENU)m_hMenu);
846 m_hMenu
= (WXHMENU
)NULL
;
847 m_menuBarFrame
= NULL
;
851 // ---------------------------------------------------------------------------
852 // wxMenuBar searching for menu items
853 // ---------------------------------------------------------------------------
855 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
856 int wxMenuBar::FindMenuItem(const wxString
& menuString
,
857 const wxString
& itemString
) const
859 wxString menuLabel
= wxStripMenuCodes(menuString
);
860 size_t count
= GetMenuCount();
861 for ( size_t i
= 0; i
< count
; i
++ )
863 wxString title
= wxStripMenuCodes(m_titles
[i
]);
864 if ( menuString
== title
)
865 return m_menus
[i
]->FindItem(itemString
);
871 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
**itemMenu
) const
876 wxMenuItem
*item
= NULL
;
877 size_t count
= GetMenuCount();
878 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
880 item
= m_menus
[i
]->FindItem(id
, itemMenu
);