]> git.saurik.com Git - wxWidgets.git/blame - src/msw/menu.cpp
wxSpinCtrl
[wxWidgets.git] / src / msw / menu.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: menu.cpp
3// Purpose: wxMenu, wxMenuBar, wxMenuItem
4// Author: Julian Smart
5// Modified by: Vadim Zeitlin
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
c626a8b7 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
c2dcfdef
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
c626a8b7 21 #pragma implementation "menu.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
c626a8b7 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
c626a8b7
VZ
32 #include "wx/frame.h"
33 #include "wx/menu.h"
34 #include "wx/utils.h"
0c589ad0 35 #include "wx/intl.h"
2bda0e17
KB
36#endif
37
47d67540 38#if wxUSE_OWNER_DRAWN
c626a8b7 39 #include "wx/ownerdrw.h"
2bda0e17
KB
40#endif
41
42#include "wx/msw/private.h"
43#include "wx/msw/menu.h"
44#include "wx/menuitem.h"
45#include "wx/log.h"
46
47// other standard headers
2bda0e17
KB
48#include <string.h>
49
c626a8b7
VZ
50// ----------------------------------------------------------------------------
51// global variables
52// ----------------------------------------------------------------------------
53
54extern wxMenu *wxCurrentPopupMenu;
55
b8d3a4f1
VZ
56// ----------------------------------------------------------------------------
57// constants
58// ----------------------------------------------------------------------------
59
60// the (popup) menu title has this special id
61static const int idMenuTitle = -2;
62
63// ----------------------------------------------------------------------------
c626a8b7 64// macros
b8d3a4f1 65// ----------------------------------------------------------------------------
c626a8b7 66
2bda0e17 67#if !USE_SHARED_LIBRARY
c626a8b7
VZ
68 IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
69 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler)
2bda0e17
KB
70#endif
71
72// ============================================================================
73// implementation
74// ============================================================================
75
c2dcfdef
VZ
76// ---------------------------------------------------------------------------
77// wxMenu construction, adding and removing menu items
78// ---------------------------------------------------------------------------
2bda0e17
KB
79
80// Construct a menu with optional title (then use append)
33961d59 81void wxMenu::Init(const wxString& title, const wxFunction func )
c626a8b7 82{
b908d224 83 m_title = title;
c626a8b7
VZ
84 m_parent = NULL;
85 m_eventHandler = this;
86 m_pInvokingWindow = NULL;
87 m_doBreak = FALSE ;
88 m_noItems = 0;
89 m_menuBar = NULL;
90 m_hMenu = (WXHMENU) CreatePopupMenu();
91 m_savehMenu = 0 ;
92 m_topLevelMenu = this;
93 m_clientData = (void*) NULL;
94
95 if ( !!m_title )
96 {
97 Append(idMenuTitle, m_title) ;
98 AppendSeparator() ;
99 }
2bda0e17 100
c626a8b7 101 Callback(func);
2bda0e17
KB
102}
103
104// The wxWindow destructor will take care of deleting the submenus.
b8d3a4f1 105wxMenu::~wxMenu()
2bda0e17 106{
c2dcfdef
VZ
107 // free Windows resources
108 if ( m_hMenu )
109 {
110 ::DestroyMenu((HMENU)m_hMenu);
111 m_hMenu = 0;
112 }
c626a8b7 113
c2dcfdef 114 // delete submenus
c626a8b7 115 wxNode *node = m_menuItems.First();
c2dcfdef 116 while ( node )
c626a8b7
VZ
117 {
118 wxMenuItem *item = (wxMenuItem *)node->Data();
119
120 // Delete child menus.
121 // Beware: they must not be appended to children list!!!
122 // (because order of delete is significant)
c2dcfdef 123 if ( item->IsSubMenu() )
c626a8b7
VZ
124 item->DeleteSubMenu();
125
126 wxNode *next = node->Next();
127 delete item;
128 delete node;
129 node = next;
130 }
2bda0e17
KB
131}
132
b8d3a4f1 133void wxMenu::Break()
2bda0e17 134{
c2dcfdef 135 m_doBreak = TRUE;
2bda0e17
KB
136}
137
138// function appends a new item or submenu to the menu
139void wxMenu::Append(wxMenuItem *pItem)
140{
223d09f6 141 wxCHECK_RET( pItem != NULL, wxT("can't append NULL item to the menu") );
2bda0e17 142
d427503c 143#if wxUSE_ACCEL
42e69d6b
VZ
144 // check for accelerators: they are given after '\t'
145 wxString label = pItem->GetName();
223d09f6 146 int posTab = label.Find(wxT('\t'));
42e69d6b
VZ
147 if ( posTab != wxNOT_FOUND ) {
148 // parse the accelerator string
149 int keyCode = 0;
150 int accelFlags = wxACCEL_NORMAL;
151 wxString current;
152 for ( size_t n = (size_t)posTab + 1; n < label.Len(); n++ ) {
153 if ( (label[n] == '+') || (label[n] == '-') ) {
154 if ( current == _("ctrl") )
155 accelFlags |= wxACCEL_CTRL;
156 else if ( current == _("alt") )
157 accelFlags |= wxACCEL_ALT;
158 else if ( current == _("shift") )
159 accelFlags |= wxACCEL_SHIFT;
160 else {
223d09f6 161 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
42e69d6b
VZ
162 current.c_str());
163 }
164
165 current.Empty();
166 }
167 else {
168 current += wxTolower(label[n]);
169 }
170 }
171
172 if ( current.IsEmpty() ) {
223d09f6 173 wxLogDebug(wxT("No accel key found, accel string ignored."));
42e69d6b
VZ
174 }
175 else {
176 if ( current.Len() == 1 ) {
177 // it's a letter
9f104fbf 178 keyCode = wxToupper(current[0U]);
42e69d6b
VZ
179 }
180 else {
181 // it should be a function key
9f104fbf 182 if ( current[0U] == 'f' && isdigit(current[1U]) &&
42e69d6b 183 (current.Len() == 2 ||
9f104fbf 184 (current.Len() == 3 && isdigit(current[2U]))) ) {
42e69d6b 185 int n;
223d09f6 186 wxSscanf(current.c_str() + 1, wxT("%d"), &n);
42e69d6b
VZ
187
188 keyCode = VK_F1 + n - 1;
189 }
190 else {
223d09f6 191 wxLogDebug(wxT("Unrecognized accel key '%s', accel "
42e69d6b
VZ
192 "string ignored."), current.c_str());
193 }
194 }
195 }
196
197 if ( keyCode ) {
198 // do add an entry
199 m_accelKeyCodes.Add(keyCode);
200 m_accelFlags.Add(accelFlags);
201 m_accelIds.Add(pItem->GetId());
202 }
203 }
d427503c 204#endif // wxUSE_ACCEL
42e69d6b 205
c626a8b7 206 UINT flags = 0;
2bda0e17 207
c2dcfdef
VZ
208 // if "Break" has just been called, insert a menu break before this item
209 // (and don't forget to reset the flag)
c626a8b7
VZ
210 if ( m_doBreak ) {
211 flags |= MF_MENUBREAK;
212 m_doBreak = FALSE;
213 }
214
215 if ( pItem->IsSeparator() ) {
216 flags |= MF_SEPARATOR;
217 }
2bda0e17 218
c2dcfdef
VZ
219 // id is the numeric id for normal menu items and HMENU for submenus as
220 // required by ::AppendMenu() API
c626a8b7 221 UINT id;
c2dcfdef
VZ
222 wxMenu *submenu = pItem->GetSubMenu();
223 if ( submenu != NULL ) {
224 wxASSERT( submenu->GetHMenu() != (WXHMENU) NULL );
2bda0e17 225
c2dcfdef
VZ
226 id = (UINT)submenu->GetHMenu();
227 submenu->m_topLevelMenu = m_topLevelMenu;
228 submenu->m_parent = this;
229 submenu->m_savehMenu = (WXHMENU)id;
230 submenu->m_hMenu = 0;
2bda0e17 231
c626a8b7
VZ
232 flags |= MF_POPUP;
233 }
234 else {
235 id = pItem->GetId();
236 }
2bda0e17 237
837e5743 238 LPCTSTR pData;
2bda0e17 239
47d67540 240#if wxUSE_OWNER_DRAWN
c626a8b7
VZ
241 if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
242 // item draws itself, pass pointer to it in data parameter
243 flags |= MF_OWNERDRAW;
837e5743 244 pData = (LPCTSTR)pItem;
c626a8b7
VZ
245 }
246 else
2bda0e17 247#endif
c626a8b7
VZ
248 {
249 // menu is just a normal string (passed in data parameter)
250 flags |= MF_STRING;
42e69d6b 251 pData = label;
c626a8b7 252 }
2bda0e17 253
c50f1fb9 254 if ( !::AppendMenu(GetHmenu(), flags, id, pData) )
c626a8b7
VZ
255 {
256 wxLogLastError("AppendMenu");
257 }
c2dcfdef
VZ
258 else
259 {
42e69d6b
VZ
260#ifdef __WIN32__
261 if ( id == idMenuTitle )
262 {
263 // visually select the menu title
264 MENUITEMINFO mii;
265 mii.cbSize = sizeof(mii);
266 mii.fMask = MIIM_STATE;
267 mii.fState = MFS_DEFAULT;
268
c50f1fb9 269 if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id, FALSE, &mii) )
42e69d6b 270 {
223d09f6 271 wxLogLastError(wxT("SetMenuItemInfo"));
42e69d6b
VZ
272 }
273 }
274#endif // __WIN32__
275
c2dcfdef
VZ
276 m_menuItems.Append(pItem);
277 m_noItems++;
278 }
2bda0e17
KB
279}
280
b8d3a4f1 281void wxMenu::AppendSeparator()
2bda0e17 282{
c626a8b7 283 Append(new wxMenuItem(this, ID_SEPARATOR));
2bda0e17
KB
284}
285
286// Pullright item
c2dcfdef
VZ
287void wxMenu::Append(int id,
288 const wxString& label,
289 wxMenu *SubMenu,
290 const wxString& helpString)
2bda0e17 291{
8cd85069 292 Append(new wxMenuItem(this, id, label, helpString, FALSE, SubMenu));
2bda0e17
KB
293}
294
295// Ordinary menu item
c2dcfdef
VZ
296void wxMenu::Append(int id,
297 const wxString& label,
298 const wxString& helpString,
299 bool checkable)
2bda0e17 300{
c626a8b7 301 // 'checkable' parameter is useless for Windows.
8cd85069 302 Append(new wxMenuItem(this, id, label, helpString, checkable));
2bda0e17
KB
303}
304
c2dcfdef 305// delete item by id
2bda0e17
KB
306void wxMenu::Delete(int id)
307{
c626a8b7
VZ
308 wxMenuItem *item = NULL;
309 int pos;
310 wxNode *node;
311 for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++)
312 {
313 item = (wxMenuItem *)node->Data();
314 if ( item->GetId() == id )
315 break;
316 }
317
223d09f6 318 wxCHECK_RET( node, wxT("wxMenu::Delete(): item doesn't exist") );
c626a8b7 319
c50f1fb9 320 HMENU menu = GetHmenu();
c626a8b7
VZ
321
322 wxMenu *pSubMenu = item->GetSubMenu();
323 if ( pSubMenu != NULL ) {
324 RemoveMenu(menu, (UINT)pos, MF_BYPOSITION);
325 pSubMenu->m_hMenu = pSubMenu->m_savehMenu;
326 pSubMenu->m_savehMenu = 0;
327 pSubMenu->m_parent = NULL;
328 // RemoveChild(item->subMenu);
329 pSubMenu->m_topLevelMenu = NULL;
330 // TODO: Why isn't subMenu deleted here???
331 // Will put this in for now. Assuming this is supposed
332 // to delete the menu, not just remove it.
333 item->DeleteSubMenu();
334 }
335 else {
336 DeleteMenu(menu, (UINT)pos, MF_BYPOSITION);
337 }
338
339 m_menuItems.DeleteNode(node);
340 delete item;
2bda0e17
KB
341}
342
d427503c
VZ
343#if wxUSE_ACCEL
344
42e69d6b
VZ
345// ---------------------------------------------------------------------------
346// accelerator helpers
347// ---------------------------------------------------------------------------
348
349// create the wxAcceleratorEntries for our accels and put them into provided
350// array - return the number of accels we have
351size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
352{
353 size_t count = GetAccelCount();
354 for ( size_t n = 0; n < count; n++ )
355 {
356 (*accels++).Set(m_accelFlags[n], m_accelKeyCodes[n], m_accelIds[n]);
357 }
358
359 return count;
360}
361
d427503c
VZ
362#endif // wxUSE_ACCEL
363
c2dcfdef
VZ
364// ---------------------------------------------------------------------------
365// wxMenu functions implemented in wxMenuItem
366// ---------------------------------------------------------------------------
367
8cd85069 368void wxMenu::Enable(int id, bool Flag)
2bda0e17 369{
8cd85069 370 wxMenuItem *item = FindItemForId(id);
223d09f6 371 wxCHECK_RET( item != NULL, wxT("can't enable non-existing menu item") );
2bda0e17 372
c626a8b7 373 item->Enable(Flag);
2bda0e17
KB
374}
375
8cd85069 376bool wxMenu::IsEnabled(int id) const
2bda0e17 377{
8cd85069 378 wxMenuItem *item = FindItemForId(id);
223d09f6 379 wxCHECK_MSG( item != NULL, FALSE, wxT("invalid item id") );
2bda0e17 380
c626a8b7 381 return item->IsEnabled();
2bda0e17
KB
382}
383
8cd85069 384void wxMenu::Check(int id, bool Flag)
2bda0e17 385{
8cd85069 386 wxMenuItem *item = FindItemForId(id);
223d09f6 387 wxCHECK_RET( item != NULL, wxT("can't get status of non-existing menu item") );
2bda0e17 388
c626a8b7 389 item->Check(Flag);
2bda0e17
KB
390}
391
8cd85069 392bool wxMenu::IsChecked(int id) const
2bda0e17 393{
8cd85069 394 wxMenuItem *item = FindItemForId(id);
223d09f6 395 wxCHECK_MSG( item != NULL, FALSE, wxT("invalid item id") );
2bda0e17 396
c626a8b7 397 return item->IsChecked();
2bda0e17
KB
398}
399
c2dcfdef
VZ
400void wxMenu::SetLabel(int id, const wxString& label)
401{
402 wxMenuItem *item = FindItemForId(id) ;
223d09f6 403 wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") );
c2dcfdef
VZ
404
405 item->SetName(label);
406}
407
408wxString wxMenu::GetLabel(int id) const
409{
410 wxString label;
411 wxMenuItem *pItem = FindItemForId(id) ;
412 if (pItem)
413 label = pItem->GetName() ;
414 else
223d09f6 415 wxFAIL_MSG(wxT("wxMenu::GetLabel: item doesn't exist"));
c2dcfdef
VZ
416
417 return label;
418}
419
420void wxMenu::SetHelpString(int itemId, const wxString& helpString)
421{
422 wxMenuItem *item = FindItemForId (itemId);
423 if (item)
424 item->SetHelp(helpString);
425 else
223d09f6 426 wxFAIL_MSG(wxT("wxMenu::SetHelpString: item doesn't exist"));
c2dcfdef
VZ
427}
428
429wxString wxMenu::GetHelpString (int itemId) const
430{
431 wxString help;
432 wxMenuItem *item = FindItemForId (itemId);
433 if (item)
434 help = item->GetHelp();
435 else
223d09f6 436 wxFAIL_MSG(wxT("wxMenu::GetHelpString: item doesn't exist"));
c2dcfdef
VZ
437
438 return help;
439}
440
441// ---------------------------------------------------------------------------
442// wxMenu title
443// ---------------------------------------------------------------------------
444
2bda0e17
KB
445void wxMenu::SetTitle(const wxString& label)
446{
c626a8b7
VZ
447 bool hasNoTitle = m_title.IsEmpty();
448 m_title = label;
b8d3a4f1 449
c50f1fb9 450 HMENU hMenu = GetHmenu();
b8d3a4f1 451
c626a8b7 452 if ( hasNoTitle )
b8d3a4f1 453 {
c626a8b7
VZ
454 if ( !label.IsEmpty() )
455 {
456 if ( !InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
457 (unsigned)idMenuTitle, m_title) ||
458 !InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
459 {
223d09f6 460 wxLogLastError(wxT("InsertMenu"));
c626a8b7
VZ
461 }
462 }
b8d3a4f1
VZ
463 }
464 else
465 {
c626a8b7
VZ
466 if ( label.IsEmpty() )
467 {
468 // remove the title and the separator after it
469 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
470 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
471 {
472 wxLogLastError("RemoveMenu");
473 }
474 }
475 else
476 {
477 // modify the title
478 if ( !ModifyMenu(hMenu, 0u,
479 MF_BYPOSITION | MF_STRING,
480 (unsigned)idMenuTitle, m_title) )
481 {
482 wxLogLastError("ModifyMenu");
483 }
484 }
b8d3a4f1 485 }
b8d3a4f1 486
42e69d6b 487#ifdef __WIN32__
c626a8b7
VZ
488 // put the title string in bold face
489 if ( !m_title.IsEmpty() )
a3f4e9e8 490 {
c626a8b7
VZ
491 MENUITEMINFO mii;
492 mii.cbSize = sizeof(mii);
493 mii.fMask = MIIM_STATE;
494 mii.fState = MFS_DEFAULT;
495
496 if ( !SetMenuItemInfo(hMenu, (unsigned)idMenuTitle, FALSE, &mii) )
497 {
498 wxLogLastError("SetMenuItemInfo");
499 }
a3f4e9e8 500 }
750b78ba 501#endif
2bda0e17
KB
502}
503
f7387de5 504const wxString wxMenu::GetTitle() const
2bda0e17 505{
c626a8b7 506 return m_title;
2bda0e17
KB
507}
508
c2dcfdef
VZ
509// ---------------------------------------------------------------------------
510// event processing
511// ---------------------------------------------------------------------------
2bda0e17 512
debe6624 513bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id)
2bda0e17 514{
a3f4e9e8
VZ
515 // ignore commands from the menu title
516
517 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
518 if ( id != (WXWORD)idMenuTitle )
519 {
520 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED);
521 event.SetEventObject( this );
522 event.SetId( id );
523 event.SetInt( id );
524 ProcessCommand(event);
525 }
526
527 return TRUE;
2bda0e17
KB
528}
529
42e69d6b 530bool wxMenu::ProcessCommand(wxCommandEvent & event)
c2dcfdef
VZ
531{
532 bool processed = FALSE;
533
534 // Try a callback
535 if (m_callback)
536 {
537 (void)(*(m_callback))(*this, event);
538 processed = TRUE;
539 }
540
541 // Try the menu's event handler
542 if ( !processed && GetEventHandler())
543 {
544 processed = GetEventHandler()->ProcessEvent(event);
545 }
546
547 // Try the window the menu was popped up from (and up through the
548 // hierarchy)
549 wxWindow *win = GetInvokingWindow();
550 if ( !processed && win )
551 processed = win->GetEventHandler()->ProcessEvent(event);
42e69d6b
VZ
552
553 return processed;
c2dcfdef
VZ
554}
555
556// ---------------------------------------------------------------------------
557// Item search
558// ---------------------------------------------------------------------------
559
2bda0e17
KB
560// Finds the item id matching the given string, -1 if not found.
561int wxMenu::FindItem (const wxString& itemString) const
562{
c2dcfdef
VZ
563 wxString itemLabel = wxStripMenuCodes(itemString);
564 for ( wxNode *node = m_menuItems.First(); node; node = node->Next() )
2bda0e17 565 {
c2dcfdef
VZ
566 wxMenuItem *item = (wxMenuItem *)node->Data();
567 if ( item->IsSubMenu() )
c626a8b7
VZ
568 {
569 int ans = item->GetSubMenu()->FindItem(itemString);
c2dcfdef 570 if ( ans != wxNOT_FOUND )
c626a8b7
VZ
571 return ans;
572 }
c2dcfdef 573 else if ( !item->IsSeparator() )
c626a8b7 574 {
c2dcfdef
VZ
575 wxString label = wxStripMenuCodes(item->GetName());
576 if ( itemLabel == label )
c626a8b7
VZ
577 return item->GetId();
578 }
2bda0e17
KB
579 }
580
c626a8b7 581 return wxNOT_FOUND;
2bda0e17
KB
582}
583
debe6624 584wxMenuItem *wxMenu::FindItemForId(int itemId, wxMenu ** itemMenu) const
2bda0e17 585{
c2dcfdef 586 if ( itemMenu )
c626a8b7 587 *itemMenu = NULL;
c2dcfdef
VZ
588
589 wxMenuItem *item = NULL;
9a48c2ba 590 for ( wxNode *node = m_menuItems.First(); node && !item; node = node->Next() )
2bda0e17 591 {
c2dcfdef 592 item = (wxMenuItem *)node->Data();
c626a8b7 593
c2dcfdef 594 if ( item->GetId() == itemId )
c626a8b7
VZ
595 {
596 if (itemMenu)
c2dcfdef 597 *itemMenu = (wxMenu *)this;
c626a8b7 598 }
c2dcfdef 599 else if ( item->IsSubMenu() )
c626a8b7 600 {
c2dcfdef 601 item = item->GetSubMenu()->FindItemForId(itemId, itemMenu);
9a48c2ba
VZ
602 }
603 else
604 {
605 // don't exit the loop
606 item = NULL;
c626a8b7 607 }
2bda0e17
KB
608 }
609
c2dcfdef 610 return item;
2bda0e17
KB
611}
612
c2dcfdef
VZ
613// ---------------------------------------------------------------------------
614// other
615// ---------------------------------------------------------------------------
2bda0e17 616
c2dcfdef
VZ
617void wxMenu::Attach(wxMenuBar *menubar)
618{
619 // menu can be in at most one menubar because otherwise they would both
620 // delete the menu pointer
223d09f6 621 wxASSERT_MSG( !m_menuBar, wxT("menu belongs to 2 menubars, expect a crash") );
c2dcfdef
VZ
622
623 m_menuBar = menubar;
624 m_savehMenu = m_hMenu;
625 m_hMenu = 0;
626}
627
628void wxMenu::Detach()
629{
223d09f6 630 wxASSERT_MSG( m_menuBar, wxT("can't detach menu if it's not attached") );
c2dcfdef
VZ
631
632 m_hMenu = m_savehMenu;
633 m_savehMenu = 0;
634}
635
636// ---------------------------------------------------------------------------
2bda0e17 637// Menu Bar
c2dcfdef
VZ
638// ---------------------------------------------------------------------------
639
640void wxMenuBar::Init()
2bda0e17 641{
c626a8b7
VZ
642 m_eventHandler = this;
643 m_menuCount = 0;
644 m_menus = NULL;
645 m_titles = NULL;
646 m_menuBarFrame = NULL;
647 m_hMenu = 0;
cba2db0c 648}
2bda0e17 649
c2dcfdef
VZ
650wxMenuBar::wxMenuBar()
651{
652 Init();
653}
654
cba2db0c
JS
655wxMenuBar::wxMenuBar( long WXUNUSED(style) )
656{
c2dcfdef 657 Init();
2bda0e17
KB
658}
659
c2dcfdef 660wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[])
2bda0e17 661{
c2dcfdef
VZ
662 Init();
663
664 m_menuCount = count;
665 m_menus = menus;
666 m_titles = new wxString[count];
667
c626a8b7 668 int i;
c2dcfdef
VZ
669 for ( i = 0; i < count; i++ )
670 m_titles[i] = titles[i];
2bda0e17 671
c2dcfdef
VZ
672 for ( i = 0; i < count; i++ )
673 m_menus[i]->Attach(this);
2bda0e17
KB
674}
675
b8d3a4f1 676wxMenuBar::~wxMenuBar()
2bda0e17 677{
c2dcfdef
VZ
678 for ( int i = 0; i < m_menuCount; i++ )
679 {
680 delete m_menus[i];
681 }
2bda0e17 682
c2dcfdef
VZ
683 delete[] m_menus;
684 delete[] m_titles;
685}
2bda0e17 686
c2dcfdef
VZ
687// ---------------------------------------------------------------------------
688// wxMenuBar helpers
689// ---------------------------------------------------------------------------
690
691void wxMenuBar::Refresh()
692{
223d09f6 693 wxCHECK_RET( m_menuBarFrame, wxT("can't refresh a menubar withotu a frame") );
c2dcfdef
VZ
694
695 DrawMenuBar((HWND)m_menuBarFrame->GetHWND()) ;
696}
697
698WXHMENU wxMenuBar::Create()
699{
1cf27c63
UM
700 if (m_hMenu != 0 )
701 return m_hMenu;
702
223d09f6 703 wxCHECK_MSG( !m_hMenu, TRUE, wxT("menubar already created") );
c2dcfdef
VZ
704
705 m_hMenu = (WXHMENU)::CreateMenu();
2bda0e17 706
c2dcfdef 707 if ( !m_hMenu )
c626a8b7 708 {
c2dcfdef 709 wxLogLastError("CreateMenu");
c626a8b7 710 }
c2dcfdef 711 else
c626a8b7 712 {
c2dcfdef
VZ
713 for ( int i = 0; i < m_menuCount; i++ )
714 {
715 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
716 (UINT)m_menus[i]->GetHMenu(),
717 m_titles[i]) )
718 {
719 wxLogLastError("AppendMenu");
720 }
721 }
c626a8b7 722 }
c626a8b7 723
c2dcfdef 724 return m_hMenu;
2bda0e17
KB
725}
726
c2dcfdef
VZ
727// ---------------------------------------------------------------------------
728// wxMenuBar functions forwarded to wxMenuItem
729// ---------------------------------------------------------------------------
730
2bda0e17
KB
731// Must only be used AFTER menu has been attached to frame,
732// otherwise use individual menus to enable/disable items
8cd85069 733void wxMenuBar::Enable(int id, bool enable)
2bda0e17 734{
c626a8b7 735 wxMenu *itemMenu = NULL;
8cd85069 736 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
2bda0e17 737
223d09f6 738 wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") );
2bda0e17 739
c2dcfdef 740 item->Enable(enable);
2bda0e17
KB
741}
742
c626a8b7 743void wxMenuBar::EnableTop(int pos, bool enable)
2bda0e17 744{
c626a8b7 745 int flag = enable ? MF_ENABLED : MF_GRAYED;;
2bda0e17 746
c626a8b7 747 EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag);
2bda0e17
KB
748}
749
750// Must only be used AFTER menu has been attached to frame,
751// otherwise use individual menus
8cd85069 752void wxMenuBar::Check(int id, bool check)
c626a8b7
VZ
753{
754 wxMenu *itemMenu = NULL;
8cd85069 755 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
c626a8b7 756
223d09f6
KB
757 wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") );
758 wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") );
2bda0e17 759
c2dcfdef 760 item->Check(check);
c626a8b7
VZ
761}
762
8cd85069 763bool wxMenuBar::IsChecked(int id) const
c626a8b7
VZ
764{
765 wxMenu *itemMenu = NULL;
8cd85069 766 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
2bda0e17 767
223d09f6 768 wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsChecked(): no such item") );
2bda0e17 769
c50f1fb9 770 int flag = ::GetMenuState(GetHmenuOf(itemMenu), id, MF_BYCOMMAND);
2bda0e17 771
c626a8b7 772 return (flag & MF_CHECKED) != 0;
2bda0e17
KB
773}
774
8cd85069 775bool wxMenuBar::IsEnabled(int id) const
2bda0e17 776{
c626a8b7 777 wxMenu *itemMenu = NULL;
8cd85069 778 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
2bda0e17 779
223d09f6 780 wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsEnabled(): no such item") );
2bda0e17 781
c50f1fb9 782 int flag = ::GetMenuState(GetHmenuOf(itemMenu), id, MF_BYCOMMAND) ;
2bda0e17 783
1a5e297c
VZ
784 // don't "and" with MF_ENABLED because its value is 0
785 return (flag & MF_DISABLED) == 0;
2bda0e17
KB
786}
787
8cd85069 788void wxMenuBar::SetLabel(int id, const wxString& label)
2bda0e17 789{
c626a8b7 790 wxMenu *itemMenu = NULL;
8cd85069 791 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
2bda0e17 792
223d09f6 793 wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") );
2bda0e17 794
c2dcfdef 795 item->SetName(label);
2bda0e17
KB
796}
797
8cd85069 798wxString wxMenuBar::GetLabel(int id) const
2bda0e17 799{
c626a8b7 800 wxMenu *itemMenu = NULL;
8cd85069 801 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
2bda0e17 802
223d09f6 803 wxCHECK_MSG( item, wxT(""), wxT("wxMenuBar::GetLabel(): no such item") );
2bda0e17 804
c2dcfdef
VZ
805 return item->GetName();
806}
8cd85069 807
c2dcfdef
VZ
808void wxMenuBar::SetHelpString (int id, const wxString& helpString)
809{
810 wxMenu *itemMenu = NULL;
811 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
2bda0e17 812
223d09f6 813 wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") );
c2dcfdef
VZ
814
815 item->SetHelp(helpString);
2bda0e17
KB
816}
817
c2dcfdef
VZ
818wxString wxMenuBar::GetHelpString (int id) const
819{
820 wxMenu *itemMenu = NULL;
821 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
822
223d09f6 823 wxCHECK_MSG( item, wxT(""), wxT("wxMenuBar::GetHelpString(): no such item") );
c2dcfdef
VZ
824
825 return item->GetHelp();
826}
827
828// ---------------------------------------------------------------------------
829// wxMenuBar functions to work with the top level submenus
830// ---------------------------------------------------------------------------
831
832// NB: we don't support owner drawn top level items for now, if we do these
833// functions would have to be changed to use wxMenuItem as well
834
debe6624 835void wxMenuBar::SetLabelTop(int pos, const wxString& label)
2bda0e17 836{
8cd85069 837 UINT id;
c2dcfdef
VZ
838 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
839 if ( flagsOld == 0xFFFFFFFF )
c626a8b7 840 {
223d09f6 841 wxLogLastError(wxT("GetMenuState"));
c2dcfdef
VZ
842
843 return;
844 }
845
846 if ( flagsOld & MF_POPUP )
847 {
848 // HIBYTE contains the number of items in the submenu in this case
849 flagsOld &= 0xff ;
8cd85069 850 id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos) ;
c626a8b7
VZ
851 }
852 else
8cd85069
VZ
853 {
854 id = pos;
855 }
856
c50f1fb9 857 if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld,
c2dcfdef
VZ
858 id, label) == 0xFFFFFFFF )
859 {
860 wxLogLastError("ModifyMenu");
861 }
2bda0e17
KB
862}
863
debe6624 864wxString wxMenuBar::GetLabelTop(int pos) const
2bda0e17 865{
8cd85069
VZ
866 int len = ::GetMenuString((HMENU)m_hMenu, pos, NULL, 0, MF_BYCOMMAND);
867
868 len++; // for the NUL character
869 wxString label;
c50f1fb9 870 ::GetMenuString(GetHmenu(), pos, label.GetWriteBuf(len), len, MF_BYCOMMAND);
8cd85069
VZ
871 label.UngetWriteBuf();
872
873 return label;
2bda0e17
KB
874}
875
c2dcfdef
VZ
876// ---------------------------------------------------------------------------
877// wxMenuBar notifications
878// ---------------------------------------------------------------------------
879
debe6624 880bool wxMenuBar::OnDelete(wxMenu *a_menu, int pos)
2bda0e17 881{
c2dcfdef 882 if ( !m_menuBarFrame )
c626a8b7 883 return TRUE;
2bda0e17 884
c2dcfdef
VZ
885 if ( ::RemoveMenu((HMENU)m_hMenu, (UINT)pos, MF_BYPOSITION) )
886 {
887 // VZ: I'm not sure about what's going on here, so I leave an assert
223d09f6 888 wxASSERT_MSG( m_menus[pos] == a_menu, wxT("what is this parameter for??") );
2bda0e17 889
c2dcfdef
VZ
890 a_menu->Detach();
891
892 if ( m_menuBarFrame )
893 Refresh();
2bda0e17 894
c626a8b7
VZ
895 return TRUE;
896 }
c2dcfdef
VZ
897 else
898 {
899 wxLogLastError("RemoveMenu");
900 }
2bda0e17 901
c626a8b7 902 return FALSE;
2bda0e17
KB
903}
904
837e5743 905bool wxMenuBar::OnAppend(wxMenu *a_menu, const wxChar *title)
2bda0e17 906{
c2dcfdef
VZ
907 WXHMENU submenu = a_menu->GetHMenu();
908 if ( !submenu )
c626a8b7 909 return FALSE;
2bda0e17 910
c2dcfdef 911 if ( !m_menuBarFrame )
c626a8b7 912 return TRUE;
2bda0e17 913
c2dcfdef 914 a_menu->Attach(this);
2bda0e17 915
c50f1fb9 916 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
c2dcfdef
VZ
917 (UINT)submenu, title) )
918 {
223d09f6 919 wxLogLastError(wxT("AppendMenu"));
c2dcfdef 920 }
2bda0e17 921
c2dcfdef 922 Refresh();
2bda0e17 923
c626a8b7 924 return TRUE;
2bda0e17
KB
925}
926
c2dcfdef
VZ
927// ---------------------------------------------------------------------------
928// wxMenuBar construction
929// ---------------------------------------------------------------------------
1cf27c63
UM
930int wxMenuBar::FindMenu(const wxString& title)
931{
932 wxString menuTitle = wxStripMenuCodes(title);
933 for ( int i = 0; i < m_menuCount; i++ )
934 {
935 wxString title = wxStripMenuCodes(m_titles[i]);
936 if ( menuTitle == title )
937 return i;
938 }
939
940 return wxNOT_FOUND;
941
942}
943
944
945void wxMenuBar::ReplaceMenu(int pos, wxMenu * new_menu, const wxString& title)
946{
947 if (m_menuBarFrame) return;
948
949 if ( pos >= 0 && pos < m_menuCount )
950 {
951 wxMenu *old_menu = m_menus[pos];
952 m_menus[pos] = new_menu;
953 delete old_menu;
954 }
955
956}
957
958
959void wxMenuBar::Insert(int pos, wxMenu * menu, const wxString& title)
960{
961 if (m_menuBarFrame) return;
962 if ( pos < 0 && pos >= m_menuCount ) return;
963
964 m_menuCount ++;
965 wxMenu **new_menus = new wxMenu *[m_menuCount];
966 wxString *new_titles = new wxString[m_menuCount];
967 int i;
968
969 for (i = 0; i < pos; i++)
970 {
971 new_menus[i] = m_menus[i];
972 m_menus[i] = NULL;
973 new_titles[i] = m_titles[i];
223d09f6 974 m_titles[i] = wxT("");
1cf27c63
UM
975 }
976
977 new_menus[pos] = (wxMenu *)menu;
978 new_titles[i] = title;
979
980 for (i = pos+1; i < m_menuCount; i++)
981 {
982 new_menus[i] = m_menus[i-1];
983 m_menus[i-1] = NULL;
984 new_titles[i] = m_titles[i-1];
223d09f6 985 m_titles[i-1] = wxT("");
1cf27c63
UM
986 }
987 if (m_menus)
988 {
989 delete[]m_menus;
990 delete[]m_titles;
991 }
992 m_menus = new_menus;
993 m_titles = new_titles;
994
995 menu->SetParent(this);
996
997}
998
c2dcfdef 999
2bda0e17
KB
1000void wxMenuBar::Append (wxMenu * menu, const wxString& title)
1001{
c626a8b7
VZ
1002 if (!OnAppend(menu, title))
1003 return;
2bda0e17 1004
c626a8b7
VZ
1005 m_menuCount ++;
1006 wxMenu **new_menus = new wxMenu *[m_menuCount];
1007 wxString *new_titles = new wxString[m_menuCount];
1008 int i;
2bda0e17 1009
c626a8b7
VZ
1010 for (i = 0; i < m_menuCount - 1; i++)
1011 {
1012 new_menus[i] = m_menus[i];
1013 m_menus[i] = NULL;
1014 new_titles[i] = m_titles[i];
223d09f6 1015 m_titles[i] = wxT("");
c626a8b7
VZ
1016 }
1017 if (m_menus)
1018 {
1019 delete[]m_menus;
1020 delete[]m_titles;
1021 }
1022 m_menus = new_menus;
1023 m_titles = new_titles;
2bda0e17 1024
c626a8b7
VZ
1025 m_menus[m_menuCount - 1] = (wxMenu *)menu;
1026 m_titles[m_menuCount - 1] = title;
2bda0e17 1027
c2dcfdef 1028 menu->SetParent(this);
2bda0e17
KB
1029}
1030
debe6624 1031void wxMenuBar::Delete(wxMenu * menu, int i)
2bda0e17 1032{
c626a8b7
VZ
1033 int j;
1034 int ii = (int) i;
1035
1036 if (menu != 0) {
1037 for (ii = 0; ii < m_menuCount; ii++) {
1038 if (m_menus[ii] == menu)
1039 break;
1040 }
1041 if (ii >= m_menuCount)
1042 return;
1043 } else {
1044 if (ii < 0 || ii >= m_menuCount)
1045 return;
1046 menu = m_menus[ii];
1047 }
2bda0e17 1048
c626a8b7
VZ
1049 if (!OnDelete(menu, ii))
1050 return;
2bda0e17 1051
c626a8b7 1052 menu->SetParent(NULL);
2bda0e17 1053
c626a8b7
VZ
1054 -- m_menuCount;
1055 for (j = ii; j < m_menuCount; j++) {
1056 m_menus[j] = m_menus[j + 1];
1057 m_titles[j] = m_titles[j + 1];
1058 }
2bda0e17
KB
1059}
1060
42e69d6b
VZ
1061void wxMenuBar::Attach(wxFrame *frame)
1062{
223d09f6 1063 wxASSERT_MSG( !m_menuBarFrame, wxT("menubar already attached!") );
42e69d6b
VZ
1064
1065 m_menuBarFrame = frame;
1066
d427503c 1067#if wxUSE_ACCEL
5df1250b 1068 // create the accel table - we consider that the menubar construction is
42e69d6b
VZ
1069 // finished
1070 size_t nAccelCount = 0;
1071 int i;
1072 for ( i = 0; i < m_menuCount; i++ )
1073 {
1074 nAccelCount += m_menus[i]->GetAccelCount();
1075 }
1076
5df1250b 1077 if ( nAccelCount )
42e69d6b 1078 {
5df1250b 1079 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
42e69d6b 1080
5df1250b
VZ
1081 nAccelCount = 0;
1082 for ( i = 0; i < m_menuCount; i++ )
1083 {
1084 nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]);
1085 }
1086
1087 m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
42e69d6b 1088
5df1250b
VZ
1089 delete [] accelEntries;
1090 }
d427503c 1091#endif // wxUSE_ACCEL
42e69d6b
VZ
1092}
1093
1cf27c63
UM
1094void wxMenuBar::Detach()
1095{
1096// ::DestroyMenu((HMENU)m_hMenu);
1097 m_hMenu = NULL;
1098 m_menuBarFrame = NULL;
1099}
1100
1101
c2dcfdef
VZ
1102// ---------------------------------------------------------------------------
1103// wxMenuBar searching for menu items
1104// ---------------------------------------------------------------------------
1105
1106// Find the itemString in menuString, and return the item id or wxNOT_FOUND
1107int wxMenuBar::FindMenuItem(const wxString& menuString,
1108 const wxString& itemString) const
2bda0e17 1109{
c2dcfdef
VZ
1110 wxString menuLabel = wxStripMenuCodes(menuString);
1111 for ( int i = 0; i < m_menuCount; i++ )
2bda0e17 1112 {
c2dcfdef
VZ
1113 wxString title = wxStripMenuCodes(m_titles[i]);
1114 if ( menuString == title )
1115 return m_menus[i]->FindItem(itemString);
2bda0e17 1116 }
c2dcfdef
VZ
1117
1118 return wxNOT_FOUND;
2bda0e17
KB
1119}
1120
c2dcfdef 1121wxMenuItem *wxMenuBar::FindItemForId (int id, wxMenu **itemMenu) const
2bda0e17 1122{
c2dcfdef 1123 if ( itemMenu )
c626a8b7 1124 *itemMenu = NULL;
2bda0e17 1125
c626a8b7 1126 wxMenuItem *item = NULL;
c2dcfdef 1127 for ( int i = 0; !item && (i < m_menuCount); i++ )
c626a8b7 1128 {
c2dcfdef 1129 item = m_menus[i]->FindItemForId(id, itemMenu);
c626a8b7 1130 }
2bda0e17 1131
c2dcfdef 1132 return item;
2bda0e17
KB
1133}
1134
2bda0e17 1135
c626a8b7
VZ
1136// ----------------------------------------------------------------------------
1137// helper functions
1138// ----------------------------------------------------------------------------
1139
2bda0e17 1140wxWindow *wxMenu::GetWindow() const
c626a8b7
VZ
1141{
1142 if ( m_pInvokingWindow != NULL )
1143 return m_pInvokingWindow;
1144 else if ( m_menuBar != NULL)
c2dcfdef 1145 return m_menuBar->GetFrame();
c626a8b7
VZ
1146
1147 return NULL;
2bda0e17
KB
1148}
1149
1150WXHMENU wxMenu::GetHMenu() const
1151{
c626a8b7
VZ
1152 if ( m_hMenu != 0 )
1153 return m_hMenu;
1154 else if ( m_savehMenu != 0 )
1155 return m_savehMenu;
1156
223d09f6 1157 wxFAIL_MSG(wxT("wxMenu without HMENU"));
2bda0e17 1158
c626a8b7 1159 return 0;
2bda0e17
KB
1160}
1161
c626a8b7
VZ
1162// Update a menu and all submenus recursively. source is the object that has
1163// the update event handlers defined for it. If NULL, the menu or associated
1164// window will be used.
631f1bfe
JS
1165void wxMenu::UpdateUI(wxEvtHandler* source)
1166{
c626a8b7
VZ
1167 if (!source && GetInvokingWindow())
1168 source = GetInvokingWindow()->GetEventHandler();
1169 if (!source)
1170 source = GetEventHandler();
1171 if (!source)
1172 source = this;
1173
1174 wxNode* node = GetItems().First();
1175 while (node)
631f1bfe 1176 {
c626a8b7
VZ
1177 wxMenuItem* item = (wxMenuItem*) node->Data();
1178 if ( !item->IsSeparator() )
1179 {
1180 wxWindowID id = item->GetId();
1181 wxUpdateUIEvent event(id);
1182 event.SetEventObject( source );
1183
1184 if (source->ProcessEvent(event))
1185 {
1186 if (event.GetSetText())
1187 SetLabel(id, event.GetText());
1188 if (event.GetSetChecked())
1189 Check(id, event.GetChecked());
1190 if (event.GetSetEnabled())
1191 Enable(id, event.GetEnabled());
1192 }
1193
1194 if (item->GetSubMenu())
1195 item->GetSubMenu()->UpdateUI(source);
1196 }
1197 node = node->Next();
1198 }
631f1bfe 1199}