]> git.saurik.com Git - wxWidgets.git/blame - src/msw/menu.cpp
Added inline setters for wxTreeEvent so we don't need to add new
[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$
6c9a19aa
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
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
1e6feb95
VZ
31#if wxUSE_MENUS
32
2bda0e17 33#ifndef WX_PRECOMP
c626a8b7
VZ
34 #include "wx/frame.h"
35 #include "wx/menu.h"
36 #include "wx/utils.h"
0c589ad0 37 #include "wx/intl.h"
717a57c2 38 #include "wx/log.h"
2bda0e17
KB
39#endif
40
47d67540 41#if wxUSE_OWNER_DRAWN
c626a8b7 42 #include "wx/ownerdrw.h"
2bda0e17
KB
43#endif
44
45#include "wx/msw/private.h"
2bda0e17
KB
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// ----------------------------------------------------------------------------
0472ece7 64// private functions
b8d3a4f1 65// ----------------------------------------------------------------------------
c626a8b7 66
0472ece7
VZ
67// make the given menu item default
68static void SetDefaultMenuItem(HMENU hmenu, UINT id)
69{
70 MENUITEMINFO mii;
71 wxZeroMemory(mii);
72 mii.cbSize = sizeof(MENUITEMINFO);
73 mii.fMask = MIIM_STATE;
74 mii.fState = MFS_DEFAULT;
75
76 if ( !::SetMenuItemInfo(hmenu, id, FALSE, &mii) )
77 {
78 wxLogLastError(wxT("SetMenuItemInfo"));
79 }
80}
2bda0e17
KB
81
82// ============================================================================
83// implementation
84// ============================================================================
85
0472ece7
VZ
86IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
87IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow)
88
c2dcfdef
VZ
89// ---------------------------------------------------------------------------
90// wxMenu construction, adding and removing menu items
91// ---------------------------------------------------------------------------
2bda0e17
KB
92
93// Construct a menu with optional title (then use append)
717a57c2 94void wxMenu::Init()
c626a8b7 95{
ad9bb75f 96 m_doBreak = FALSE;
0472ece7 97 m_startRadioGroup = -1;
c626a8b7 98
717a57c2
VZ
99 // create the menu
100 m_hMenu = (WXHMENU)CreatePopupMenu();
101 if ( !m_hMenu )
102 {
f6bcfd97 103 wxLogLastError(wxT("CreatePopupMenu"));
717a57c2
VZ
104 }
105
106 // if we have a title, insert it in the beginning of the menu
c626a8b7
VZ
107 if ( !!m_title )
108 {
ad9bb75f
VZ
109 Append(idMenuTitle, m_title);
110 AppendSeparator();
c626a8b7 111 }
2bda0e17
KB
112}
113
114// The wxWindow destructor will take care of deleting the submenus.
b8d3a4f1 115wxMenu::~wxMenu()
2bda0e17 116{
717a57c2
VZ
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
119 // menu
120 if ( !IsAttached() && !GetParent() )
c2dcfdef 121 {
ad9bb75f
VZ
122 if ( !::DestroyMenu(GetHmenu()) )
123 {
f6bcfd97 124 wxLogLastError(wxT("DestroyMenu"));
ad9bb75f 125 }
c2dcfdef 126 }
c626a8b7 127
ad9bb75f
VZ
128#if wxUSE_ACCEL
129 // delete accels
130 WX_CLEAR_ARRAY(m_accels);
131#endif // wxUSE_ACCEL
2bda0e17
KB
132}
133
b8d3a4f1 134void wxMenu::Break()
2bda0e17 135{
717a57c2 136 // this will take effect during the next call to Append()
c2dcfdef 137 m_doBreak = TRUE;
2bda0e17
KB
138}
139
0472ece7
VZ
140void wxMenu::Attach(wxMenuBarBase *menubar)
141{
142 wxMenuBase::Attach(menubar);
143
144 EndRadioGroup();
145}
146
717a57c2
VZ
147#if wxUSE_ACCEL
148
149int wxMenu::FindAccel(int id) const
150{
151 size_t n, count = m_accels.GetCount();
152 for ( n = 0; n < count; n++ )
153 {
154 if ( m_accels[n]->m_command == id )
155 return n;
156 }
157
158 return wxNOT_FOUND;
159}
160
161void wxMenu::UpdateAccel(wxMenuItem *item)
2bda0e17 162{
f6bcfd97 163 if ( item->IsSubMenu() )
717a57c2 164 {
f6bcfd97
BP
165 wxMenu *submenu = item->GetSubMenu();
166 wxMenuItemList::Node *node = submenu->GetMenuItems().GetFirst();
167 while ( node )
168 {
169 UpdateAccel(node->GetData());
170
171 node = node->GetNext();
172 }
717a57c2 173 }
f6bcfd97 174 else if ( !item->IsSeparator() )
717a57c2 175 {
f6bcfd97
BP
176 // find the (new) accel for this item
177 wxAcceleratorEntry *accel = wxGetAccelFromString(item->GetText());
717a57c2 178 if ( accel )
f6bcfd97
BP
179 accel->m_command = item->GetId();
180
181 // find the old one
182 int n = FindAccel(item->GetId());
183 if ( n == wxNOT_FOUND )
184 {
185 // no old, add new if any
186 if ( accel )
187 m_accels.Add(accel);
188 else
189 return; // skipping RebuildAccelTable() below
190 }
717a57c2 191 else
f6bcfd97
BP
192 {
193 // replace old with new or just remove the old one if no new
194 delete m_accels[n];
195 if ( accel )
196 m_accels[n] = accel;
197 else
b54e41c5 198 m_accels.RemoveAt(n);
f6bcfd97 199 }
717a57c2 200
f6bcfd97
BP
201 if ( IsAttached() )
202 {
203 m_menuBar->RebuildAccelTable();
204 }
42e69d6b 205 }
f6bcfd97 206 //else: it is a separator, they can't have accels, nothing to do
717a57c2
VZ
207}
208
209#endif // wxUSE_ACCEL
210
211// append a new item or submenu to the menu
212bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos)
213{
214#if wxUSE_ACCEL
215 UpdateAccel(pItem);
d427503c 216#endif // wxUSE_ACCEL
42e69d6b 217
c626a8b7 218 UINT flags = 0;
2bda0e17 219
c2dcfdef
VZ
220 // if "Break" has just been called, insert a menu break before this item
221 // (and don't forget to reset the flag)
c626a8b7
VZ
222 if ( m_doBreak ) {
223 flags |= MF_MENUBREAK;
224 m_doBreak = FALSE;
225 }
226
227 if ( pItem->IsSeparator() ) {
228 flags |= MF_SEPARATOR;
229 }
2bda0e17 230
c2dcfdef
VZ
231 // id is the numeric id for normal menu items and HMENU for submenus as
232 // required by ::AppendMenu() API
c626a8b7 233 UINT id;
c2dcfdef
VZ
234 wxMenu *submenu = pItem->GetSubMenu();
235 if ( submenu != NULL ) {
717a57c2
VZ
236 wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") );
237
238 submenu->SetParent(this);
2bda0e17 239
c2dcfdef 240 id = (UINT)submenu->GetHMenu();
2bda0e17 241
c626a8b7
VZ
242 flags |= MF_POPUP;
243 }
244 else {
245 id = pItem->GetId();
246 }
2bda0e17 247
837e5743 248 LPCTSTR pData;
2bda0e17 249
47d67540 250#if wxUSE_OWNER_DRAWN
c626a8b7
VZ
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;
837e5743 254 pData = (LPCTSTR)pItem;
c626a8b7
VZ
255 }
256 else
2bda0e17 257#endif
c626a8b7
VZ
258 {
259 // menu is just a normal string (passed in data parameter)
260 flags |= MF_STRING;
8fb3a512 261
f5166ed4 262 pData = (wxChar*)pItem->GetText().c_str();
c626a8b7 263 }
2bda0e17 264
717a57c2
VZ
265 BOOL ok;
266 if ( pos == (size_t)-1 )
267 {
268 ok = ::AppendMenu(GetHmenu(), flags, id, pData);
269 }
270 else
271 {
272 ok = ::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData);
273 }
274
275 if ( !ok )
c626a8b7 276 {
f6bcfd97 277 wxLogLastError(wxT("Insert or AppendMenu"));
717a57c2
VZ
278
279 return FALSE;
c626a8b7 280 }
42e69d6b 281
0472ece7
VZ
282 // if we just appended the title, highlight it
283#ifdef __WIN32__
284 if ( (int)id == idMenuTitle )
285 {
286 // visually select the menu title
287 SetDefaultMenuItem(GetHmenu(), id);
288 }
42e69d6b
VZ
289#endif // __WIN32__
290
0472ece7
VZ
291 // if we're already attached to the menubar, we must update it
292 if ( IsAttached() && m_menuBar->IsAttached() )
293 {
294 m_menuBar->Refresh();
295 }
296
297 return TRUE;
298}
299
300void wxMenu::EndRadioGroup()
301{
0472ece7
VZ
302 // we're not inside a radio group any longer
303 m_startRadioGroup = -1;
2bda0e17
KB
304}
305
717a57c2 306bool wxMenu::DoAppend(wxMenuItem *item)
2bda0e17 307{
0472ece7
VZ
308 wxCHECK_MSG( item, FALSE, _T("NULL item in wxMenu::DoAppend") );
309
be15b995
VZ
310 bool check = FALSE;
311
546bfbea 312 if ( item->GetKind() == wxITEM_RADIO )
0472ece7 313 {
be15b995
VZ
314 int count = GetMenuItemCount();
315
0472ece7
VZ
316 if ( m_startRadioGroup == -1 )
317 {
318 // start a new radio group
be15b995
VZ
319 m_startRadioGroup = count;
320
321 // for now it has just one element
322 item->SetAsRadioGroupStart();
323 item->SetRadioGroupEnd(m_startRadioGroup);
324
325 // ensure that we have a checked item in the radio group
326 check = TRUE;
327 }
328 else // extend the current radio group
329 {
330 // we need to update its end item
331 item->SetRadioGroupStart(m_startRadioGroup);
332 wxMenuItemList::Node *node = GetMenuItems().Item(m_startRadioGroup);
333
334 if ( node )
335 {
336 node->GetData()->SetRadioGroupEnd(count);
337 }
338 else
339 {
340 wxFAIL_MSG( _T("where is the radio group start item?") );
341 }
0472ece7
VZ
342 }
343 }
344 else // not a radio item
345 {
346 EndRadioGroup();
347 }
348
be15b995
VZ
349 if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) )
350 {
351 return FALSE;
352 }
353
354 if ( check )
355 {
356 // check the item initially
357 item->Check(TRUE);
358 }
359
360 return TRUE;
2bda0e17
KB
361}
362
717a57c2 363bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
2bda0e17 364{
717a57c2 365 return wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos);
2bda0e17
KB
366}
367
717a57c2 368wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
2bda0e17 369{
717a57c2
VZ
370 // we need to find the items position in the child list
371 size_t pos;
372 wxMenuItemList::Node *node = GetMenuItems().GetFirst();
373 for ( pos = 0; node; pos++ )
c626a8b7 374 {
717a57c2 375 if ( node->GetData() == item )
c626a8b7 376 break;
717a57c2
VZ
377
378 node = node->GetNext();
c626a8b7
VZ
379 }
380
717a57c2
VZ
381 // DoRemove() (unlike Remove) can only be called for existing item!
382 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
c626a8b7 383
717a57c2
VZ
384#if wxUSE_ACCEL
385 // remove the corresponding accel from the accel table
386 int n = FindAccel(item->GetId());
387 if ( n != wxNOT_FOUND )
388 {
389 delete m_accels[n];
c626a8b7 390
b54e41c5 391 m_accels.RemoveAt(n);
c626a8b7 392 }
717a57c2
VZ
393 //else: this item doesn't have an accel, nothing to do
394#endif // wxUSE_ACCEL
395
396 // remove the item from the menu
397 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
398 {
f6bcfd97 399 wxLogLastError(wxT("RemoveMenu"));
c626a8b7
VZ
400 }
401
4bc1afd5 402 if ( IsAttached() && m_menuBar->IsAttached() )
717a57c2
VZ
403 {
404 // otherwise, the chane won't be visible
405 m_menuBar->Refresh();
406 }
2bda0e17 407
717a57c2
VZ
408 // and from internal data structures
409 return wxMenuBase::DoRemove(item);
410}
d427503c 411
42e69d6b
VZ
412// ---------------------------------------------------------------------------
413// accelerator helpers
414// ---------------------------------------------------------------------------
415
717a57c2
VZ
416#if wxUSE_ACCEL
417
42e69d6b
VZ
418// create the wxAcceleratorEntries for our accels and put them into provided
419// array - return the number of accels we have
420size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
421{
422 size_t count = GetAccelCount();
423 for ( size_t n = 0; n < count; n++ )
424 {
974e8d94 425 *accels++ = *m_accels[n];
42e69d6b
VZ
426 }
427
428 return count;
429}
430
d427503c
VZ
431#endif // wxUSE_ACCEL
432
c2dcfdef 433// ---------------------------------------------------------------------------
717a57c2 434// set wxMenu title
c2dcfdef
VZ
435// ---------------------------------------------------------------------------
436
2bda0e17
KB
437void wxMenu::SetTitle(const wxString& label)
438{
c626a8b7
VZ
439 bool hasNoTitle = m_title.IsEmpty();
440 m_title = label;
b8d3a4f1 441
c50f1fb9 442 HMENU hMenu = GetHmenu();
b8d3a4f1 443
c626a8b7 444 if ( hasNoTitle )
b8d3a4f1 445 {
c626a8b7
VZ
446 if ( !label.IsEmpty() )
447 {
717a57c2
VZ
448 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
449 (unsigned)idMenuTitle, m_title) ||
450 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
c626a8b7 451 {
f6bcfd97 452 wxLogLastError(wxT("InsertMenu"));
c626a8b7
VZ
453 }
454 }
b8d3a4f1
VZ
455 }
456 else
457 {
c626a8b7
VZ
458 if ( label.IsEmpty() )
459 {
460 // remove the title and the separator after it
461 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
462 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
463 {
f6bcfd97 464 wxLogLastError(wxT("RemoveMenu"));
c626a8b7
VZ
465 }
466 }
467 else
468 {
469 // modify the title
470 if ( !ModifyMenu(hMenu, 0u,
717a57c2
VZ
471 MF_BYPOSITION | MF_STRING,
472 (unsigned)idMenuTitle, m_title) )
c626a8b7 473 {
f6bcfd97 474 wxLogLastError(wxT("ModifyMenu"));
c626a8b7
VZ
475 }
476 }
b8d3a4f1 477 }
b8d3a4f1 478
42e69d6b 479#ifdef __WIN32__
c626a8b7
VZ
480 // put the title string in bold face
481 if ( !m_title.IsEmpty() )
a3f4e9e8 482 {
0472ece7 483 SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle);
a3f4e9e8 484 }
717a57c2 485#endif // Win32
2bda0e17
KB
486}
487
c2dcfdef
VZ
488// ---------------------------------------------------------------------------
489// event processing
490// ---------------------------------------------------------------------------
2bda0e17 491
debe6624 492bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id)
2bda0e17 493{
a3f4e9e8
VZ
494 // ignore commands from the menu title
495
496 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
497 if ( id != (WXWORD)idMenuTitle )
498 {
3ca6a5f0
BP
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
1e6feb95 503 SendEvent(id, ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND) & MF_CHECKED);
a3f4e9e8
VZ
504 }
505
506 return TRUE;
2bda0e17
KB
507}
508
c2dcfdef
VZ
509// ---------------------------------------------------------------------------
510// other
511// ---------------------------------------------------------------------------
2bda0e17 512
717a57c2
VZ
513wxWindow *wxMenu::GetWindow() const
514{
515 if ( m_invokingWindow != NULL )
516 return m_invokingWindow;
517 else if ( m_menuBar != NULL)
518 return m_menuBar->GetFrame();
519
520 return NULL;
c2dcfdef
VZ
521}
522
523// ---------------------------------------------------------------------------
2bda0e17 524// Menu Bar
c2dcfdef
VZ
525// ---------------------------------------------------------------------------
526
527void wxMenuBar::Init()
2bda0e17 528{
c626a8b7 529 m_eventHandler = this;
c626a8b7 530 m_hMenu = 0;
cba2db0c 531}
2bda0e17 532
c2dcfdef
VZ
533wxMenuBar::wxMenuBar()
534{
535 Init();
536}
537
cba2db0c
JS
538wxMenuBar::wxMenuBar( long WXUNUSED(style) )
539{
c2dcfdef 540 Init();
2bda0e17
KB
541}
542
c2dcfdef 543wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[])
2bda0e17 544{
c2dcfdef
VZ
545 Init();
546
a8cfd0cb 547 m_titles.Alloc(count);
c2dcfdef 548
a8cfd0cb
VZ
549 for ( int i = 0; i < count; i++ )
550 {
551 m_menus.Append(menus[i]);
552 m_titles.Add(titles[i]);
2bda0e17 553
a8cfd0cb
VZ
554 menus[i]->Attach(this);
555 }
2bda0e17
KB
556}
557
b8d3a4f1 558wxMenuBar::~wxMenuBar()
2bda0e17 559{
7a0363dd
JS
560 // we should free Windows resources only if Windows doesn't do it for us
561 // which happens if we're attached to a frame
562 if (m_hMenu && !IsAttached())
563 {
564 ::DestroyMenu((HMENU)m_hMenu);
565 m_hMenu = (WXHMENU)NULL;
566 }
c2dcfdef 567}
2bda0e17 568
c2dcfdef
VZ
569// ---------------------------------------------------------------------------
570// wxMenuBar helpers
571// ---------------------------------------------------------------------------
572
573void wxMenuBar::Refresh()
574{
065de612 575 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
c2dcfdef 576
1e6feb95 577 DrawMenuBar(GetHwndOf(GetFrame()));
c2dcfdef
VZ
578}
579
580WXHMENU wxMenuBar::Create()
581{
3ca6a5f0 582 if ( m_hMenu != 0 )
717a57c2 583 return m_hMenu;
1cf27c63 584
c2dcfdef 585 m_hMenu = (WXHMENU)::CreateMenu();
2bda0e17 586
c2dcfdef 587 if ( !m_hMenu )
c626a8b7 588 {
f6bcfd97 589 wxLogLastError(wxT("CreateMenu"));
c626a8b7 590 }
c2dcfdef 591 else
c626a8b7 592 {
a8cfd0cb
VZ
593 size_t count = GetMenuCount();
594 for ( size_t i = 0; i < count; i++ )
c2dcfdef
VZ
595 {
596 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
597 (UINT)m_menus[i]->GetHMenu(),
598 m_titles[i]) )
599 {
f6bcfd97 600 wxLogLastError(wxT("AppendMenu"));
c2dcfdef
VZ
601 }
602 }
c626a8b7 603 }
c626a8b7 604
c2dcfdef 605 return m_hMenu;
2bda0e17
KB
606}
607
c2dcfdef 608// ---------------------------------------------------------------------------
3dfac970 609// wxMenuBar functions to work with the top level submenus
c2dcfdef
VZ
610// ---------------------------------------------------------------------------
611
3dfac970
VZ
612// NB: we don't support owner drawn top level items for now, if we do these
613// functions would have to be changed to use wxMenuItem as well
2bda0e17 614
a8cfd0cb 615void wxMenuBar::EnableTop(size_t pos, bool enable)
2bda0e17 616{
717a57c2
VZ
617 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
618
619 int flag = enable ? MF_ENABLED : MF_GRAYED;
2bda0e17 620
c626a8b7 621 EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag);
adc6fb16
VZ
622
623 Refresh();
2bda0e17
KB
624}
625
a8cfd0cb 626void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
2bda0e17 627{
717a57c2
VZ
628 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
629
630 m_titles[pos] = label;
631
632 if ( !IsAttached() )
633 {
634 return;
635 }
636 //else: have to modify the existing menu
637
8cd85069 638 UINT id;
c2dcfdef
VZ
639 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
640 if ( flagsOld == 0xFFFFFFFF )
c626a8b7 641 {
223d09f6 642 wxLogLastError(wxT("GetMenuState"));
c2dcfdef
VZ
643
644 return;
645 }
646
647 if ( flagsOld & MF_POPUP )
648 {
649 // HIBYTE contains the number of items in the submenu in this case
ad9bb75f
VZ
650 flagsOld &= 0xff;
651 id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos);
c626a8b7
VZ
652 }
653 else
8cd85069
VZ
654 {
655 id = pos;
656 }
657
c50f1fb9 658 if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld,
a17e237f 659 id, label) == (int)0xFFFFFFFF )
c2dcfdef 660 {
f6bcfd97 661 wxLogLastError(wxT("ModifyMenu"));
c2dcfdef 662 }
717a57c2
VZ
663
664 Refresh();
2bda0e17
KB
665}
666
a8cfd0cb 667wxString wxMenuBar::GetLabelTop(size_t pos) const
2bda0e17 668{
717a57c2
VZ
669 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
670 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
8cd85069 671
717a57c2 672 return m_titles[pos];
2bda0e17
KB
673}
674
ad9bb75f
VZ
675// ---------------------------------------------------------------------------
676// wxMenuBar construction
677// ---------------------------------------------------------------------------
678
a8cfd0cb 679wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 680{
ad9bb75f
VZ
681 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
682 if ( !menuOld )
f7f50f49
VZ
683 return NULL;
684
ad9bb75f 685 m_titles[pos] = title;
a8cfd0cb 686
ad9bb75f 687 if ( IsAttached() )
a8cfd0cb 688 {
ad9bb75f
VZ
689 // can't use ModifyMenu() because it deletes the submenu it replaces
690 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
a8cfd0cb 691 {
f6bcfd97 692 wxLogLastError(wxT("RemoveMenu"));
a8cfd0cb 693 }
1cf27c63 694
ad9bb75f
VZ
695 if ( !::InsertMenu(GetHmenu(), (UINT)pos,
696 MF_BYPOSITION | MF_POPUP | MF_STRING,
697 (UINT)GetHmenuOf(menu), title) )
698 {
f6bcfd97 699 wxLogLastError(wxT("InsertMenu"));
ad9bb75f
VZ
700 }
701
717a57c2
VZ
702#if wxUSE_ACCEL
703 if ( menuOld->HasAccels() || menu->HasAccels() )
704 {
705 // need to rebuild accell table
706 RebuildAccelTable();
707 }
708#endif // wxUSE_ACCEL
709
ad9bb75f 710 Refresh();
a8cfd0cb 711 }
ad9bb75f
VZ
712
713 return menuOld;
1cf27c63
UM
714}
715
a8cfd0cb 716bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 717{
ad9bb75f 718 if ( !wxMenuBarBase::Insert(pos, menu, title) )
a8cfd0cb 719 return FALSE;
1cf27c63 720
ad9bb75f
VZ
721 m_titles.Insert(title, pos);
722
ad9bb75f
VZ
723 if ( IsAttached() )
724 {
725 if ( !::InsertMenu(GetHmenu(), pos,
726 MF_BYPOSITION | MF_POPUP | MF_STRING,
727 (UINT)GetHmenuOf(menu), title) )
728 {
f6bcfd97 729 wxLogLastError(wxT("InsertMenu"));
ad9bb75f
VZ
730 }
731
717a57c2
VZ
732#if wxUSE_ACCEL
733 if ( menu->HasAccels() )
734 {
735 // need to rebuild accell table
736 RebuildAccelTable();
737 }
738#endif // wxUSE_ACCEL
739
ad9bb75f 740 Refresh();
a8cfd0cb 741 }
ad9bb75f
VZ
742
743 return TRUE;
1cf27c63
UM
744}
745
ad9bb75f 746bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
2bda0e17 747{
ad9bb75f
VZ
748 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
749 wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") );
2bda0e17 750
717a57c2
VZ
751 if ( !wxMenuBarBase::Append(menu, title) )
752 return FALSE;
753
717a57c2
VZ
754 m_titles.Add(title);
755
ad9bb75f
VZ
756 if ( IsAttached() )
757 {
758 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
759 (UINT)submenu, title) )
760 {
761 wxLogLastError(wxT("AppendMenu"));
762 }
763
717a57c2
VZ
764#if wxUSE_ACCEL
765 if ( menu->HasAccels() )
766 {
767 // need to rebuild accell table
768 RebuildAccelTable();
769 }
770#endif // wxUSE_ACCEL
771
ad9bb75f
VZ
772 Refresh();
773 }
2bda0e17 774
a8cfd0cb 775 return TRUE;
2bda0e17
KB
776}
777
a8cfd0cb 778wxMenu *wxMenuBar::Remove(size_t pos)
2bda0e17 779{
a8cfd0cb
VZ
780 wxMenu *menu = wxMenuBarBase::Remove(pos);
781 if ( !menu )
782 return NULL;
c626a8b7 783
ad9bb75f
VZ
784 if ( IsAttached() )
785 {
786 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
787 {
f6bcfd97 788 wxLogLastError(wxT("RemoveMenu"));
ad9bb75f 789 }
2bda0e17 790
717a57c2
VZ
791#if wxUSE_ACCEL
792 if ( menu->HasAccels() )
793 {
794 // need to rebuild accell table
795 RebuildAccelTable();
796 }
797#endif // wxUSE_ACCEL
798
ad9bb75f
VZ
799 Refresh();
800 }
2bda0e17 801
a8cfd0cb 802 m_titles.Remove(pos);
2bda0e17 803
a8cfd0cb 804 return menu;
2bda0e17
KB
805}
806
d427503c 807#if wxUSE_ACCEL
717a57c2
VZ
808
809void wxMenuBar::RebuildAccelTable()
810{
811 // merge the accelerators of all menus into one accel table
42e69d6b 812 size_t nAccelCount = 0;
a8cfd0cb
VZ
813 size_t i, count = GetMenuCount();
814 for ( i = 0; i < count; i++ )
42e69d6b
VZ
815 {
816 nAccelCount += m_menus[i]->GetAccelCount();
817 }
818
5df1250b 819 if ( nAccelCount )
42e69d6b 820 {
5df1250b 821 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
42e69d6b 822
5df1250b 823 nAccelCount = 0;
a8cfd0cb 824 for ( i = 0; i < count; i++ )
5df1250b
VZ
825 {
826 nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]);
827 }
828
829 m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
42e69d6b 830
5df1250b
VZ
831 delete [] accelEntries;
832 }
717a57c2
VZ
833}
834
835#endif // wxUSE_ACCEL
836
837void wxMenuBar::Attach(wxFrame *frame)
838{
1e6feb95 839 wxMenuBarBase::Attach(frame);
717a57c2 840
717a57c2
VZ
841#if wxUSE_ACCEL
842 RebuildAccelTable();
d427503c 843#endif // wxUSE_ACCEL
42e69d6b
VZ
844}
845
1cf27c63
UM
846void wxMenuBar::Detach()
847{
1e6feb95 848 wxMenuBarBase::Detach();
2bda0e17
KB
849}
850
1e6feb95 851#endif // wxUSE_MENUS