]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/menu.cpp
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"
42 #include "wx/ownerdrw.h"
45 #include "wx/msw/private.h"
47 // other standard headers
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 extern wxMenu
*wxCurrentPopupMenu
;
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // the (popup) menu title has this special id
61 static const int idMenuTitle
= -2;
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // make the given menu item default
68 static void SetDefaultMenuItem(HMENU hmenu
, UINT id
)
72 mii
.cbSize
= sizeof(MENUITEMINFO
);
73 mii
.fMask
= MIIM_STATE
;
74 mii
.fState
= MFS_DEFAULT
;
76 if ( !::SetMenuItemInfo(hmenu
, id
, FALSE
, &mii
) )
78 wxLogLastError(wxT("SetMenuItemInfo"));
82 // ============================================================================
84 // ============================================================================
86 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
87 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxWindow
)
89 // ---------------------------------------------------------------------------
90 // wxMenu construction, adding and removing menu items
91 // ---------------------------------------------------------------------------
93 // Construct a menu with optional title (then use append)
97 m_startRadioGroup
= -1;
100 m_hMenu
= (WXHMENU
)CreatePopupMenu();
103 wxLogLastError(wxT("CreatePopupMenu"));
106 // if we have a title, insert it in the beginning of the menu
109 Append(idMenuTitle
, m_title
);
114 // The wxWindow destructor will take care of deleting the submenus.
117 // we should free Windows resources only if Windows doesn't do it for us
118 // which happens if we're attached to a menubar or a submenu of another
120 if ( !IsAttached() && !GetParent() )
122 if ( !::DestroyMenu(GetHmenu()) )
124 wxLogLastError(wxT("DestroyMenu"));
130 WX_CLEAR_ARRAY(m_accels
);
131 #endif // wxUSE_ACCEL
136 // this will take effect during the next call to Append()
140 void wxMenu::Attach(wxMenuBarBase
*menubar
)
142 wxMenuBase::Attach(menubar
);
149 int wxMenu::FindAccel(int id
) const
151 size_t n
, count
= m_accels
.GetCount();
152 for ( n
= 0; n
< count
; n
++ )
154 if ( m_accels
[n
]->m_command
== id
)
161 void wxMenu::UpdateAccel(wxMenuItem
*item
)
163 if ( item
->IsSubMenu() )
165 wxMenu
*submenu
= item
->GetSubMenu();
166 wxMenuItemList::Node
*node
= submenu
->GetMenuItems().GetFirst();
169 UpdateAccel(node
->GetData());
171 node
= node
->GetNext();
174 else if ( !item
->IsSeparator() )
176 // find the (new) accel for this item
177 wxAcceleratorEntry
*accel
= wxGetAccelFromString(item
->GetText());
179 accel
->m_command
= item
->GetId();
182 int n
= FindAccel(item
->GetId());
183 if ( n
== wxNOT_FOUND
)
185 // no old, add new if any
189 return; // skipping RebuildAccelTable() below
193 // replace old with new or just remove the old one if no new
198 m_accels
.RemoveAt(n
);
203 m_menuBar
->RebuildAccelTable();
206 //else: it is a separator, they can't have accels, nothing to do
209 #endif // wxUSE_ACCEL
211 // append a new item or submenu to the menu
212 bool wxMenu::DoInsertOrAppend(wxMenuItem
*pItem
, size_t pos
)
216 #endif // wxUSE_ACCEL
220 // if "Break" has just been called, insert a menu break before this item
221 // (and don't forget to reset the flag)
223 flags
|= MF_MENUBREAK
;
227 if ( pItem
->IsSeparator() ) {
228 flags
|= MF_SEPARATOR
;
231 // id is the numeric id for normal menu items and HMENU for submenus as
232 // required by ::AppendMenu() API
234 wxMenu
*submenu
= pItem
->GetSubMenu();
235 if ( submenu
!= NULL
) {
236 wxASSERT_MSG( submenu
->GetHMenu(), wxT("invalid submenu") );
238 submenu
->SetParent(this);
240 id
= (UINT
)submenu
->GetHMenu();
250 #if wxUSE_OWNER_DRAWN
251 if ( pItem
->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
252 // item draws itself, pass pointer to it in data parameter
253 flags
|= MF_OWNERDRAW
;
254 pData
= (LPCTSTR
)pItem
;
259 // menu is just a normal string (passed in data parameter)
262 pData
= (wxChar
*)pItem
->GetText().c_str();
266 if ( pos
== (size_t)-1 )
268 ok
= ::AppendMenu(GetHmenu(), flags
, id
, pData
);
272 ok
= ::InsertMenu(GetHmenu(), pos
, flags
| MF_BYPOSITION
, id
, pData
);
277 wxLogLastError(wxT("Insert or AppendMenu"));
282 // if we just appended the title, highlight it
284 if ( (int)id
== idMenuTitle
)
286 // visually select the menu title
287 SetDefaultMenuItem(GetHmenu(), id
);
291 // if we're already attached to the menubar, we must update it
292 if ( IsAttached() && m_menuBar
->IsAttached() )
294 m_menuBar
->Refresh();
300 void wxMenu::EndRadioGroup()
302 // we're not inside a radio group any longer
303 m_startRadioGroup
= -1;
306 bool wxMenu::DoAppend(wxMenuItem
*item
)
308 wxCHECK_MSG( item
, FALSE
, _T("NULL item in wxMenu::DoAppend") );
312 if ( item
->GetKind() == wxITEM_RADIO
)
314 int count
= GetMenuItemCount();
316 if ( m_startRadioGroup
== -1 )
318 // start a new radio group
319 m_startRadioGroup
= count
;
321 // for now it has just one element
322 item
->SetAsRadioGroupStart();
323 item
->SetRadioGroupEnd(m_startRadioGroup
);
325 // ensure that we have a checked item in the radio group
328 else // extend the current radio group
330 // we need to update its end item
331 item
->SetRadioGroupStart(m_startRadioGroup
);
332 wxMenuItemList::Node
*node
= GetMenuItems().Item(m_startRadioGroup
);
336 node
->GetData()->SetRadioGroupEnd(count
);
340 wxFAIL_MSG( _T("where is the radio group start item?") );
344 else // not a radio item
349 if ( !wxMenuBase::DoAppend(item
) || !DoInsertOrAppend(item
) )
356 // check the item initially
363 bool wxMenu::DoInsert(size_t pos
, wxMenuItem
*item
)
365 return wxMenuBase::DoInsert(pos
, item
) && DoInsertOrAppend(item
, pos
);
368 wxMenuItem
*wxMenu::DoRemove(wxMenuItem
*item
)
370 // we need to find the items position in the child list
372 wxMenuItemList::Node
*node
= GetMenuItems().GetFirst();
373 for ( pos
= 0; node
; pos
++ )
375 if ( node
->GetData() == item
)
378 node
= node
->GetNext();
381 // DoRemove() (unlike Remove) can only be called for existing item!
382 wxCHECK_MSG( node
, NULL
, wxT("bug in wxMenu::Remove logic") );
385 // remove the corresponding accel from the accel table
386 int n
= FindAccel(item
->GetId());
387 if ( n
!= wxNOT_FOUND
)
391 m_accels
.RemoveAt(n
);
393 //else: this item doesn't have an accel, nothing to do
394 #endif // wxUSE_ACCEL
396 // remove the item from the menu
397 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
399 wxLogLastError(wxT("RemoveMenu"));
402 if ( IsAttached() && m_menuBar
->IsAttached() )
404 // otherwise, the chane won't be visible
405 m_menuBar
->Refresh();
408 // and from internal data structures
409 return wxMenuBase::DoRemove(item
);
412 // ---------------------------------------------------------------------------
413 // accelerator helpers
414 // ---------------------------------------------------------------------------
418 // create the wxAcceleratorEntries for our accels and put them into provided
419 // array - return the number of accels we have
420 size_t wxMenu::CopyAccels(wxAcceleratorEntry
*accels
) const
422 size_t count
= GetAccelCount();
423 for ( size_t n
= 0; n
< count
; n
++ )
425 *accels
++ = *m_accels
[n
];
431 #endif // wxUSE_ACCEL
433 // ---------------------------------------------------------------------------
435 // ---------------------------------------------------------------------------
437 void wxMenu::SetTitle(const wxString
& label
)
439 bool hasNoTitle
= m_title
.IsEmpty();
442 HMENU hMenu
= GetHmenu();
446 if ( !label
.IsEmpty() )
448 if ( !::InsertMenu(hMenu
, 0u, MF_BYPOSITION
| MF_STRING
,
449 (unsigned)idMenuTitle
, m_title
) ||
450 !::InsertMenu(hMenu
, 1u, MF_BYPOSITION
, (unsigned)-1, NULL
) )
452 wxLogLastError(wxT("InsertMenu"));
458 if ( label
.IsEmpty() )
460 // remove the title and the separator after it
461 if ( !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) ||
462 !RemoveMenu(hMenu
, 0, MF_BYPOSITION
) )
464 wxLogLastError(wxT("RemoveMenu"));
470 if ( !ModifyMenu(hMenu
, 0u,
471 MF_BYPOSITION
| MF_STRING
,
472 (unsigned)idMenuTitle
, m_title
) )
474 wxLogLastError(wxT("ModifyMenu"));
480 // put the title string in bold face
481 if ( !m_title
.IsEmpty() )
483 SetDefaultMenuItem(GetHmenu(), (UINT
)idMenuTitle
);
488 // ---------------------------------------------------------------------------
490 // ---------------------------------------------------------------------------
492 bool wxMenu::MSWCommand(WXUINT
WXUNUSED(param
), WXWORD id
)
494 // ignore commands from the menu title
496 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
497 if ( id
!= (WXWORD
)idMenuTitle
)
499 // VZ: previosuly, the command int was set to id too which was quite
500 // useless anyhow (as it could be retrieved using GetId()) and
501 // uncompatible with wxGTK, so now we use the command int instead
502 // to pass the checked status
503 SendEvent(id
, ::GetMenuState(GetHmenu(), id
, MF_BYCOMMAND
) & MF_CHECKED
);
509 // ---------------------------------------------------------------------------
511 // ---------------------------------------------------------------------------
513 wxWindow
*wxMenu::GetWindow() const
515 if ( m_invokingWindow
!= NULL
)
516 return m_invokingWindow
;
517 else if ( m_menuBar
!= NULL
)
518 return m_menuBar
->GetFrame();
523 // ---------------------------------------------------------------------------
525 // ---------------------------------------------------------------------------
527 void wxMenuBar::Init()
529 m_eventHandler
= this;
533 wxMenuBar::wxMenuBar()
538 wxMenuBar::wxMenuBar( long WXUNUSED(style
) )
543 wxMenuBar::wxMenuBar(int count
, wxMenu
*menus
[], const wxString titles
[])
547 m_titles
.Alloc(count
);
549 for ( int i
= 0; i
< count
; i
++ )
551 m_menus
.Append(menus
[i
]);
552 m_titles
.Add(titles
[i
]);
554 menus
[i
]->Attach(this);
558 wxMenuBar::~wxMenuBar()
562 // ---------------------------------------------------------------------------
564 // ---------------------------------------------------------------------------
566 void wxMenuBar::Refresh()
568 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
570 DrawMenuBar(GetHwndOf(GetFrame()));
573 WXHMENU
wxMenuBar::Create()
578 m_hMenu
= (WXHMENU
)::CreateMenu();
582 wxLogLastError(wxT("CreateMenu"));
586 size_t count
= GetMenuCount();
587 for ( size_t i
= 0; i
< count
; i
++ )
589 if ( !::AppendMenu((HMENU
)m_hMenu
, MF_POPUP
| MF_STRING
,
590 (UINT
)m_menus
[i
]->GetHMenu(),
593 wxLogLastError(wxT("AppendMenu"));
601 // ---------------------------------------------------------------------------
602 // wxMenuBar functions to work with the top level submenus
603 // ---------------------------------------------------------------------------
605 // NB: we don't support owner drawn top level items for now, if we do these
606 // functions would have to be changed to use wxMenuItem as well
608 void wxMenuBar::EnableTop(size_t pos
, bool enable
)
610 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
612 int flag
= enable
? MF_ENABLED
: MF_GRAYED
;
614 EnableMenuItem((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
| flag
);
619 void wxMenuBar::SetLabelTop(size_t pos
, const wxString
& label
)
621 wxCHECK_RET( pos
< GetMenuCount(), wxT("invalid menu index") );
623 m_titles
[pos
] = label
;
629 //else: have to modify the existing menu
632 UINT flagsOld
= ::GetMenuState((HMENU
)m_hMenu
, pos
, MF_BYPOSITION
);
633 if ( flagsOld
== 0xFFFFFFFF )
635 wxLogLastError(wxT("GetMenuState"));
640 if ( flagsOld
& MF_POPUP
)
642 // HIBYTE contains the number of items in the submenu in this case
644 id
= (UINT
)::GetSubMenu((HMENU
)m_hMenu
, pos
);
651 if ( ::ModifyMenu(GetHmenu(), pos
, MF_BYPOSITION
| MF_STRING
| flagsOld
,
652 id
, label
) == (int)0xFFFFFFFF )
654 wxLogLastError(wxT("ModifyMenu"));
660 wxString
wxMenuBar::GetLabelTop(size_t pos
) const
662 wxCHECK_MSG( pos
< GetMenuCount(), wxEmptyString
,
663 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
665 return m_titles
[pos
];
668 // ---------------------------------------------------------------------------
669 // wxMenuBar construction
670 // ---------------------------------------------------------------------------
672 wxMenu
*wxMenuBar::Replace(size_t pos
, wxMenu
*menu
, const wxString
& title
)
674 wxMenu
*menuOld
= wxMenuBarBase::Replace(pos
, menu
, title
);
677 m_titles
[pos
] = title
;
681 // can't use ModifyMenu() because it deletes the submenu it replaces
682 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
684 wxLogLastError(wxT("RemoveMenu"));
687 if ( !::InsertMenu(GetHmenu(), (UINT
)pos
,
688 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
689 (UINT
)GetHmenuOf(menu
), title
) )
691 wxLogLastError(wxT("InsertMenu"));
695 if ( menuOld
->HasAccels() || menu
->HasAccels() )
697 // need to rebuild accell table
700 #endif // wxUSE_ACCEL
708 bool wxMenuBar::Insert(size_t pos
, wxMenu
*menu
, const wxString
& title
)
710 if ( !wxMenuBarBase::Insert(pos
, menu
, title
) )
713 m_titles
.Insert(title
, pos
);
717 if ( !::InsertMenu(GetHmenu(), pos
,
718 MF_BYPOSITION
| MF_POPUP
| MF_STRING
,
719 (UINT
)GetHmenuOf(menu
), title
) )
721 wxLogLastError(wxT("InsertMenu"));
725 if ( menu
->HasAccels() )
727 // need to rebuild accell table
730 #endif // wxUSE_ACCEL
738 bool wxMenuBar::Append(wxMenu
*menu
, const wxString
& title
)
740 WXHMENU submenu
= menu
? menu
->GetHMenu() : 0;
741 wxCHECK_MSG( submenu
, FALSE
, wxT("can't append invalid menu to menubar") );
743 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
);
778 if ( !::RemoveMenu(GetHmenu(), (UINT
)pos
, MF_BYPOSITION
) )
780 wxLogLastError(wxT("RemoveMenu"));
784 if ( menu
->HasAccels() )
786 // need to rebuild accell table
789 #endif // wxUSE_ACCEL
794 m_titles
.Remove(pos
);
801 void wxMenuBar::RebuildAccelTable()
803 // merge the accelerators of all menus into one accel table
804 size_t nAccelCount
= 0;
805 size_t i
, count
= GetMenuCount();
806 for ( i
= 0; i
< count
; i
++ )
808 nAccelCount
+= m_menus
[i
]->GetAccelCount();
813 wxAcceleratorEntry
*accelEntries
= new wxAcceleratorEntry
[nAccelCount
];
816 for ( i
= 0; i
< count
; i
++ )
818 nAccelCount
+= m_menus
[i
]->CopyAccels(&accelEntries
[nAccelCount
]);
821 m_accelTable
= wxAcceleratorTable(nAccelCount
, accelEntries
);
823 delete [] accelEntries
;
827 #endif // wxUSE_ACCEL
829 void wxMenuBar::Attach(wxFrame
*frame
)
831 wxMenuBarBase::Attach(frame
);
835 #endif // wxUSE_ACCEL
838 void wxMenuBar::Detach()
840 m_hMenu
= (WXHMENU
)NULL
;
842 wxMenuBarBase::Detach();
845 #endif // wxUSE_MENUS