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
, wxEvtHandler
)
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("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("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 // find the (new) accel for this item
139 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
141 accel
->m_command
= item
->GetId();
144 int n
= FindAccel(item
->GetId());
145 if ( n
== wxNOT_FOUND
)
147 // no old, add new if any
151 return; // skipping RebuildAccelTable() below
155 // replace old with new or just remove the old one if no new
165 m_menuBar
->RebuildAccelTable();
169 #endif // wxUSE_ACCEL
171 // append a new item or submenu to the menu
172 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
176 #endif // wxUSE_ACCEL
180 // if "Break" has just been called, insert a menu break before this item
181 // (and don't forget to reset the flag)
183 flags
|= MF_MENUBREAK
;
187 if ( pItem
->IsSeparator() ) {
188 flags
|= MF_SEPARATOR
;
191 // id is the numeric id for normal menu items and HMENU for submenus as
192 // required by ::AppendMenu() API
194 wxMenu
*submenu
= pItem
->GetSubMenu();
195 if ( submenu
!= NULL
) {
196 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
198 submenu
->SetParent(this);
200 id
= (UINT
)submenu
->GetHMenu();
210 #if wxUSE_OWNER_DRAWN
211 if ( pItem
->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
212 // item draws itself, pass pointer to it in data parameter
213 flags
|= MF_OWNERDRAW
;
214 pData
= (LPCTSTR
)pItem
;
219 // menu is just a normal string (passed in data parameter)
222 pData
= (char*)pItem
->GetText().c_str();
226 if ( pos
== (size_t)-1 )
228 ok
= ::AppendMenu(GetHmenu(), flags
, id
, pData
);
232 ok
= ::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
);
237 wxLogLastError("Insert or AppendMenu");
243 // if we just appended the title, highlight it
245 if ( (int)id
== idMenuTitle
)
247 // visually select the menu title
249 mii
.cbSize
= sizeof(mii
);
250 mii
.fMask
= MIIM_STATE
;
251 mii
.fState
= MFS_DEFAULT
;
253 if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id
, FALSE
, &mii
) )
255 wxLogLastError(wxT("SetMenuItemInfo"));
260 // if we're already attached to the menubar, we must update it
263 m_menuBar
->Refresh();
270 bool wxMenu::DoAppend(wxMenuItem
*item
)
272 return wxMenuBase::DoAppend(item
) && DoInsertOrAppend(item
);
275 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
277 return wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
);
280 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
282 // we need to find the items position in the child list
284 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
285 for ( pos
= 0; node
; pos
++ )
287 if ( node
->GetData() == item
)
290 node
= node
->GetNext();
293 // DoRemove() (unlike Remove) can only be called for existing item!
294 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
297 // remove the corresponding accel from the accel table
298 int n
= FindAccel(item
->GetId());
299 if ( n
!= wxNOT_FOUND
)
305 //else: this item doesn't have an accel, nothing to do
306 #endif // wxUSE_ACCEL
308 // remove the item from the menu
309 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
311 wxLogLastError("RemoveMenu");
316 // otherwise, the chane won't be visible
317 m_menuBar
->Refresh();
320 // and from internal data structures
321 return wxMenuBase::DoRemove(item
);
324 // ---------------------------------------------------------------------------
325 // accelerator helpers
326 // ---------------------------------------------------------------------------
330 // create the wxAcceleratorEntries for our accels and put them into provided
331 // array - return the number of accels we have
332 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
334 size_t count
= GetAccelCount();
335 for ( size_t n
= 0; n
< count
; n
++ )
337 *accels
++ = *m_accels
[n
];
343 #endif // wxUSE_ACCEL
345 // ---------------------------------------------------------------------------
347 // ---------------------------------------------------------------------------
349 void wxMenu::SetTitle(const wxString
& label
)
351 bool hasNoTitle
= m_title
.IsEmpty();
354 HMENU hMenu
= GetHmenu();
358 if ( !label
.IsEmpty() )
360 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
361 (unsigned)idMenuTitle
, m_title
) ||
362 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
364 wxLogLastError("InsertMenu");
370 if ( label
.IsEmpty() )
372 // remove the title and the separator after it
373 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
374 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
376 wxLogLastError("RemoveMenu");
382 if ( !ModifyMenu(hMenu
, 0u,
383 MF_BYPOSITION
| MF_STRING
,
384 (unsigned)idMenuTitle
, m_title
) )
386 wxLogLastError("ModifyMenu");
392 // put the title string in bold face
393 if ( !m_title
.IsEmpty() )
396 mii
.cbSize
= sizeof(mii
);
397 mii
.fMask
= MIIM_STATE
;
398 mii
.fState
= MFS_DEFAULT
;
400 if ( !SetMenuItemInfo(hMenu
, (unsigned)idMenuTitle
, FALSE
, &mii
) )
402 wxLogLastError("SetMenuItemInfo");
408 // ---------------------------------------------------------------------------
410 // ---------------------------------------------------------------------------
412 bool wxMenu::MSWCommand(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 wxUSE_MENU_CALLBACK
437 (void)(*(m_callback
))(*this, event
);
440 #endif // wxUSE_MENU_CALLBACK
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()
533 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
535 DrawMenuBar(GetHwndOf(m_menuBarFrame
));
538 WXHMENU
wxMenuBar::Create()
543 wxCHECK_MSG( !m_hMenu
, TRUE
, wxT("menubar already created") );
545 m_hMenu
= (WXHMENU
)::CreateMenu();
549 wxLogLastError("CreateMenu");
553 size_t count
= GetMenuCount();
554 for ( size_t i
= 0; i
< count
; i
++ )
556 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
557 (UINT
)m_menus
[i
]->GetHMenu(),
560 wxLogLastError("AppendMenu");
568 // ---------------------------------------------------------------------------
569 // wxMenuBar functions to work with the top level submenus
570 // ---------------------------------------------------------------------------
572 // NB: we don't support owner drawn top level items for now, if we do these
573 // functions would have to be changed to use wxMenuItem as well
575 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
577 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
579 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
581 EnableMenuItem((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
| flag
);
586 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
588 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
590 m_titles
[pos
] = label
;
596 //else: have to modify the existing menu
599 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
);
600 if ( flagsOld
== 0xFFFFFFFF )
602 wxLogLastError(wxT("GetMenuState"));
607 if ( flagsOld
& MF_POPUP
)
609 // HIBYTE contains the number of items in the submenu in this case
611 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, pos
);
618 if ( ::ModifyMenu(GetHmenu(), pos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
619 id
, label
) == (int)0xFFFFFFFF )
621 wxLogLastError("ModifyMenu");
627 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
629 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
630 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
632 return m_titles
[pos
];
635 int wxMenuBar::FindMenu(const wxString
& title
)
637 wxString menuTitle
= wxStripMenuCodes(title
);
639 size_t count
= GetMenuCount();
640 for ( size_t i
= 0; i
< count
; i
++ )
642 wxString title
= wxStripMenuCodes(m_titles
[i
]);
643 if ( menuTitle
== title
)
651 // ---------------------------------------------------------------------------
652 // wxMenuBar construction
653 // ---------------------------------------------------------------------------
655 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
657 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
660 m_titles
[pos
] = title
;
664 // can't use ModifyMenu() because it deletes the submenu it replaces
665 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
667 wxLogLastError("RemoveMenu");
670 if ( !::InsertMenu(GetHmenu(), (UINT
)pos
,
671 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
672 (UINT
)GetHmenuOf(menu
), title
) )
674 wxLogLastError("InsertMenu");
678 if ( menuOld
->HasAccels() || menu
->HasAccels() )
680 // need to rebuild accell table
683 #endif // wxUSE_ACCEL
691 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
693 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
696 m_titles
.Insert(title
, pos
);
702 if ( !::InsertMenu(GetHmenu(), pos
,
703 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
704 (UINT
)GetHmenuOf(menu
), title
) )
706 wxLogLastError("InsertMenu");
710 if ( menu
->HasAccels() )
712 // need to rebuild accell table
715 #endif // wxUSE_ACCEL
723 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
725 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
726 wxCHECK_MSG( submenu
, FALSE
, wxT("can't append invalid menu to menubar") );
728 if ( !wxMenuBarBase::Append(menu
, title
) )
737 if ( !::AppendMenu(GetHmenu(), MF_POPUP
| MF_STRING
,
738 (UINT
)submenu
, title
) )
740 wxLogLastError(wxT("AppendMenu"));
744 if ( menu
->HasAccels() )
746 // need to rebuild accell table
749 #endif // wxUSE_ACCEL
757 wxMenu
*wxMenuBar::Remove(size_t pos
)
759 wxMenu
*menu
= wxMenuBarBase::Remove(pos
);
765 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
767 wxLogLastError("RemoveMenu");
773 if ( menu
->HasAccels() )
775 // need to rebuild accell table
778 #endif // wxUSE_ACCEL
783 m_titles
.Remove(pos
);
790 void wxMenuBar::RebuildAccelTable()
792 // merge the accelerators of all menus into one accel table
793 size_t nAccelCount
= 0;
794 size_t i
, count
= GetMenuCount();
795 for ( i
= 0; i
< count
; i
++ )
797 nAccelCount
+= m_menus
[i
]->GetAccelCount();
802 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
805 for ( i
= 0; i
< count
; i
++ )
807 nAccelCount
+= m_menus
[i
]->CopyAccels(&accelEntries
[nAccelCount
]);
810 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
812 delete [] accelEntries
;
816 #endif // wxUSE_ACCEL
818 void wxMenuBar::Attach(wxFrame
*frame
)
820 // wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
822 m_menuBarFrame
= frame
;
826 #endif // wxUSE_ACCEL
829 void wxMenuBar::Detach()
831 // ::DestroyMenu((HMENU)m_hMenu);
832 m_hMenu
= (WXHMENU
)NULL
;
833 m_menuBarFrame
= NULL
;
837 // ---------------------------------------------------------------------------
838 // wxMenuBar searching for menu items
839 // ---------------------------------------------------------------------------
841 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
842 int wxMenuBar::FindMenuItem(const wxString
& menuString
,
843 const wxString
& itemString
) const
845 wxString menuLabel
= wxStripMenuCodes(menuString
);
846 size_t count
= GetMenuCount();
847 for ( size_t i
= 0; i
< count
; i
++ )
849 wxString title
= wxStripMenuCodes(m_titles
[i
]);
850 if ( menuString
== title
)
851 return m_menus
[i
]->FindItem(itemString
);
857 wxMenuItem
*wxMenuBar::FindItem(int id
, wxMenu
**itemMenu
) const
862 wxMenuItem
*item
= NULL
;
863 size_t count
= GetMenuCount();
864 for ( size_t i
= 0; !item
&& (i
< count
); i
++ )
866 item
= m_menus
[i
]->FindItem(id
, itemMenu
);