]> git.saurik.com Git - wxWidgets.git/blame - src/msw/menu.cpp
Removed helpwxht.h/cpp (old wxHelpControllerHtml class)
[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
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{
302 if ( m_startRadioGroup == -1 )
303 {
304 // nothing to do
305 return;
306 }
307
308 wxMenuItemList::Node *nodeStart = GetMenuItems().Item(m_startRadioGroup);
309 wxCHECK_RET( nodeStart, _T("where is the radio group start item?") );
310
311 int endRadioGroup = GetMenuItemCount();
2bda0e17 312
0472ece7
VZ
313 wxMenuItemList::Node *node = nodeStart;
314 for ( int n = m_startRadioGroup; n < endRadioGroup && node; n++ )
315 {
316 wxMenuItem *item = (wxMenuItem *)node->GetData();
317 item->SetRadioGroup(m_startRadioGroup, endRadioGroup - 1);
318
319 node = node->GetNext();
717a57c2 320 }
0472ece7
VZ
321
322 nodeStart->GetData()->Check(TRUE);
323
324 // we're not inside a radio group any longer
325 m_startRadioGroup = -1;
2bda0e17
KB
326}
327
717a57c2 328bool wxMenu::DoAppend(wxMenuItem *item)
2bda0e17 329{
0472ece7
VZ
330 wxCHECK_MSG( item, FALSE, _T("NULL item in wxMenu::DoAppend") );
331
546bfbea 332 if ( item->GetKind() == wxITEM_RADIO )
0472ece7
VZ
333 {
334 if ( m_startRadioGroup == -1 )
335 {
336 // start a new radio group
337 m_startRadioGroup = GetMenuItemCount();
338 }
339 }
340 else // not a radio item
341 {
342 EndRadioGroup();
343 }
344
717a57c2 345 return wxMenuBase::DoAppend(item) && DoInsertOrAppend(item);
2bda0e17
KB
346}
347
717a57c2 348bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
2bda0e17 349{
717a57c2 350 return wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos);
2bda0e17
KB
351}
352
717a57c2 353wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
2bda0e17 354{
717a57c2
VZ
355 // we need to find the items position in the child list
356 size_t pos;
357 wxMenuItemList::Node *node = GetMenuItems().GetFirst();
358 for ( pos = 0; node; pos++ )
c626a8b7 359 {
717a57c2 360 if ( node->GetData() == item )
c626a8b7 361 break;
717a57c2
VZ
362
363 node = node->GetNext();
c626a8b7
VZ
364 }
365
717a57c2
VZ
366 // DoRemove() (unlike Remove) can only be called for existing item!
367 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
c626a8b7 368
717a57c2
VZ
369#if wxUSE_ACCEL
370 // remove the corresponding accel from the accel table
371 int n = FindAccel(item->GetId());
372 if ( n != wxNOT_FOUND )
373 {
374 delete m_accels[n];
c626a8b7 375
b54e41c5 376 m_accels.RemoveAt(n);
c626a8b7 377 }
717a57c2
VZ
378 //else: this item doesn't have an accel, nothing to do
379#endif // wxUSE_ACCEL
380
381 // remove the item from the menu
382 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
383 {
f6bcfd97 384 wxLogLastError(wxT("RemoveMenu"));
c626a8b7
VZ
385 }
386
4bc1afd5 387 if ( IsAttached() && m_menuBar->IsAttached() )
717a57c2
VZ
388 {
389 // otherwise, the chane won't be visible
390 m_menuBar->Refresh();
391 }
2bda0e17 392
717a57c2
VZ
393 // and from internal data structures
394 return wxMenuBase::DoRemove(item);
395}
d427503c 396
42e69d6b
VZ
397// ---------------------------------------------------------------------------
398// accelerator helpers
399// ---------------------------------------------------------------------------
400
717a57c2
VZ
401#if wxUSE_ACCEL
402
42e69d6b
VZ
403// create the wxAcceleratorEntries for our accels and put them into provided
404// array - return the number of accels we have
405size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
406{
407 size_t count = GetAccelCount();
408 for ( size_t n = 0; n < count; n++ )
409 {
974e8d94 410 *accels++ = *m_accels[n];
42e69d6b
VZ
411 }
412
413 return count;
414}
415
d427503c
VZ
416#endif // wxUSE_ACCEL
417
c2dcfdef 418// ---------------------------------------------------------------------------
717a57c2 419// set wxMenu title
c2dcfdef
VZ
420// ---------------------------------------------------------------------------
421
2bda0e17
KB
422void wxMenu::SetTitle(const wxString& label)
423{
c626a8b7
VZ
424 bool hasNoTitle = m_title.IsEmpty();
425 m_title = label;
b8d3a4f1 426
c50f1fb9 427 HMENU hMenu = GetHmenu();
b8d3a4f1 428
c626a8b7 429 if ( hasNoTitle )
b8d3a4f1 430 {
c626a8b7
VZ
431 if ( !label.IsEmpty() )
432 {
717a57c2
VZ
433 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
434 (unsigned)idMenuTitle, m_title) ||
435 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
c626a8b7 436 {
f6bcfd97 437 wxLogLastError(wxT("InsertMenu"));
c626a8b7
VZ
438 }
439 }
b8d3a4f1
VZ
440 }
441 else
442 {
c626a8b7
VZ
443 if ( label.IsEmpty() )
444 {
445 // remove the title and the separator after it
446 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
447 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
448 {
f6bcfd97 449 wxLogLastError(wxT("RemoveMenu"));
c626a8b7
VZ
450 }
451 }
452 else
453 {
454 // modify the title
455 if ( !ModifyMenu(hMenu, 0u,
717a57c2
VZ
456 MF_BYPOSITION | MF_STRING,
457 (unsigned)idMenuTitle, m_title) )
c626a8b7 458 {
f6bcfd97 459 wxLogLastError(wxT("ModifyMenu"));
c626a8b7
VZ
460 }
461 }
b8d3a4f1 462 }
b8d3a4f1 463
42e69d6b 464#ifdef __WIN32__
c626a8b7
VZ
465 // put the title string in bold face
466 if ( !m_title.IsEmpty() )
a3f4e9e8 467 {
0472ece7 468 SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle);
a3f4e9e8 469 }
717a57c2 470#endif // Win32
2bda0e17
KB
471}
472
c2dcfdef
VZ
473// ---------------------------------------------------------------------------
474// event processing
475// ---------------------------------------------------------------------------
2bda0e17 476
debe6624 477bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id)
2bda0e17 478{
a3f4e9e8
VZ
479 // ignore commands from the menu title
480
481 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
482 if ( id != (WXWORD)idMenuTitle )
483 {
3ca6a5f0
BP
484 // VZ: previosuly, the command int was set to id too which was quite
485 // useless anyhow (as it could be retrieved using GetId()) and
486 // uncompatible with wxGTK, so now we use the command int instead
487 // to pass the checked status
1e6feb95 488 SendEvent(id, ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND) & MF_CHECKED);
a3f4e9e8
VZ
489 }
490
491 return TRUE;
2bda0e17
KB
492}
493
c2dcfdef
VZ
494// ---------------------------------------------------------------------------
495// other
496// ---------------------------------------------------------------------------
2bda0e17 497
717a57c2
VZ
498wxWindow *wxMenu::GetWindow() const
499{
500 if ( m_invokingWindow != NULL )
501 return m_invokingWindow;
502 else if ( m_menuBar != NULL)
503 return m_menuBar->GetFrame();
504
505 return NULL;
c2dcfdef
VZ
506}
507
508// ---------------------------------------------------------------------------
2bda0e17 509// Menu Bar
c2dcfdef
VZ
510// ---------------------------------------------------------------------------
511
512void wxMenuBar::Init()
2bda0e17 513{
c626a8b7 514 m_eventHandler = this;
c626a8b7 515 m_hMenu = 0;
cba2db0c 516}
2bda0e17 517
c2dcfdef
VZ
518wxMenuBar::wxMenuBar()
519{
520 Init();
521}
522
cba2db0c
JS
523wxMenuBar::wxMenuBar( long WXUNUSED(style) )
524{
c2dcfdef 525 Init();
2bda0e17
KB
526}
527
c2dcfdef 528wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[])
2bda0e17 529{
c2dcfdef
VZ
530 Init();
531
a8cfd0cb 532 m_titles.Alloc(count);
c2dcfdef 533
a8cfd0cb
VZ
534 for ( int i = 0; i < count; i++ )
535 {
536 m_menus.Append(menus[i]);
537 m_titles.Add(titles[i]);
2bda0e17 538
a8cfd0cb
VZ
539 menus[i]->Attach(this);
540 }
2bda0e17
KB
541}
542
b8d3a4f1 543wxMenuBar::~wxMenuBar()
2bda0e17 544{
c2dcfdef 545}
2bda0e17 546
c2dcfdef
VZ
547// ---------------------------------------------------------------------------
548// wxMenuBar helpers
549// ---------------------------------------------------------------------------
550
551void wxMenuBar::Refresh()
552{
065de612 553 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
c2dcfdef 554
1e6feb95 555 DrawMenuBar(GetHwndOf(GetFrame()));
c2dcfdef
VZ
556}
557
558WXHMENU wxMenuBar::Create()
559{
3ca6a5f0 560 if ( m_hMenu != 0 )
717a57c2 561 return m_hMenu;
1cf27c63 562
c2dcfdef 563 m_hMenu = (WXHMENU)::CreateMenu();
2bda0e17 564
c2dcfdef 565 if ( !m_hMenu )
c626a8b7 566 {
f6bcfd97 567 wxLogLastError(wxT("CreateMenu"));
c626a8b7 568 }
c2dcfdef 569 else
c626a8b7 570 {
a8cfd0cb
VZ
571 size_t count = GetMenuCount();
572 for ( size_t i = 0; i < count; i++ )
c2dcfdef
VZ
573 {
574 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
575 (UINT)m_menus[i]->GetHMenu(),
576 m_titles[i]) )
577 {
f6bcfd97 578 wxLogLastError(wxT("AppendMenu"));
c2dcfdef
VZ
579 }
580 }
c626a8b7 581 }
c626a8b7 582
c2dcfdef 583 return m_hMenu;
2bda0e17
KB
584}
585
c2dcfdef 586// ---------------------------------------------------------------------------
3dfac970 587// wxMenuBar functions to work with the top level submenus
c2dcfdef
VZ
588// ---------------------------------------------------------------------------
589
3dfac970
VZ
590// NB: we don't support owner drawn top level items for now, if we do these
591// functions would have to be changed to use wxMenuItem as well
2bda0e17 592
a8cfd0cb 593void wxMenuBar::EnableTop(size_t pos, bool enable)
2bda0e17 594{
717a57c2
VZ
595 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
596
597 int flag = enable ? MF_ENABLED : MF_GRAYED;
2bda0e17 598
c626a8b7 599 EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag);
adc6fb16
VZ
600
601 Refresh();
2bda0e17
KB
602}
603
a8cfd0cb 604void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
2bda0e17 605{
717a57c2
VZ
606 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
607
608 m_titles[pos] = label;
609
610 if ( !IsAttached() )
611 {
612 return;
613 }
614 //else: have to modify the existing menu
615
8cd85069 616 UINT id;
c2dcfdef
VZ
617 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
618 if ( flagsOld == 0xFFFFFFFF )
c626a8b7 619 {
223d09f6 620 wxLogLastError(wxT("GetMenuState"));
c2dcfdef
VZ
621
622 return;
623 }
624
625 if ( flagsOld & MF_POPUP )
626 {
627 // HIBYTE contains the number of items in the submenu in this case
ad9bb75f
VZ
628 flagsOld &= 0xff;
629 id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos);
c626a8b7
VZ
630 }
631 else
8cd85069
VZ
632 {
633 id = pos;
634 }
635
c50f1fb9 636 if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld,
a17e237f 637 id, label) == (int)0xFFFFFFFF )
c2dcfdef 638 {
f6bcfd97 639 wxLogLastError(wxT("ModifyMenu"));
c2dcfdef 640 }
717a57c2
VZ
641
642 Refresh();
2bda0e17
KB
643}
644
a8cfd0cb 645wxString wxMenuBar::GetLabelTop(size_t pos) const
2bda0e17 646{
717a57c2
VZ
647 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
648 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
8cd85069 649
717a57c2 650 return m_titles[pos];
2bda0e17
KB
651}
652
ad9bb75f
VZ
653// ---------------------------------------------------------------------------
654// wxMenuBar construction
655// ---------------------------------------------------------------------------
656
a8cfd0cb 657wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 658{
ad9bb75f
VZ
659 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
660 if ( !menuOld )
661 return FALSE;
662 m_titles[pos] = title;
a8cfd0cb 663
ad9bb75f 664 if ( IsAttached() )
a8cfd0cb 665 {
ad9bb75f
VZ
666 // can't use ModifyMenu() because it deletes the submenu it replaces
667 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
a8cfd0cb 668 {
f6bcfd97 669 wxLogLastError(wxT("RemoveMenu"));
a8cfd0cb 670 }
1cf27c63 671
ad9bb75f
VZ
672 if ( !::InsertMenu(GetHmenu(), (UINT)pos,
673 MF_BYPOSITION | MF_POPUP | MF_STRING,
674 (UINT)GetHmenuOf(menu), title) )
675 {
f6bcfd97 676 wxLogLastError(wxT("InsertMenu"));
ad9bb75f
VZ
677 }
678
717a57c2
VZ
679#if wxUSE_ACCEL
680 if ( menuOld->HasAccels() || menu->HasAccels() )
681 {
682 // need to rebuild accell table
683 RebuildAccelTable();
684 }
685#endif // wxUSE_ACCEL
686
ad9bb75f 687 Refresh();
a8cfd0cb 688 }
ad9bb75f
VZ
689
690 return menuOld;
1cf27c63
UM
691}
692
a8cfd0cb 693bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 694{
ad9bb75f 695 if ( !wxMenuBarBase::Insert(pos, menu, title) )
a8cfd0cb 696 return FALSE;
1cf27c63 697
ad9bb75f
VZ
698 m_titles.Insert(title, pos);
699
ad9bb75f
VZ
700 if ( IsAttached() )
701 {
702 if ( !::InsertMenu(GetHmenu(), pos,
703 MF_BYPOSITION | MF_POPUP | MF_STRING,
704 (UINT)GetHmenuOf(menu), title) )
705 {
f6bcfd97 706 wxLogLastError(wxT("InsertMenu"));
ad9bb75f
VZ
707 }
708
717a57c2
VZ
709#if wxUSE_ACCEL
710 if ( menu->HasAccels() )
711 {
712 // need to rebuild accell table
713 RebuildAccelTable();
714 }
715#endif // wxUSE_ACCEL
716
ad9bb75f 717 Refresh();
a8cfd0cb 718 }
ad9bb75f
VZ
719
720 return TRUE;
1cf27c63
UM
721}
722
ad9bb75f 723bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
2bda0e17 724{
ad9bb75f
VZ
725 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
726 wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") );
2bda0e17 727
717a57c2
VZ
728 if ( !wxMenuBarBase::Append(menu, title) )
729 return FALSE;
730
717a57c2
VZ
731 m_titles.Add(title);
732
ad9bb75f
VZ
733 if ( IsAttached() )
734 {
735 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
736 (UINT)submenu, title) )
737 {
738 wxLogLastError(wxT("AppendMenu"));
739 }
740
717a57c2
VZ
741#if wxUSE_ACCEL
742 if ( menu->HasAccels() )
743 {
744 // need to rebuild accell table
745 RebuildAccelTable();
746 }
747#endif // wxUSE_ACCEL
748
ad9bb75f
VZ
749 Refresh();
750 }
2bda0e17 751
a8cfd0cb 752 return TRUE;
2bda0e17
KB
753}
754
a8cfd0cb 755wxMenu *wxMenuBar::Remove(size_t pos)
2bda0e17 756{
a8cfd0cb
VZ
757 wxMenu *menu = wxMenuBarBase::Remove(pos);
758 if ( !menu )
759 return NULL;
c626a8b7 760
ad9bb75f
VZ
761 if ( IsAttached() )
762 {
763 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
764 {
f6bcfd97 765 wxLogLastError(wxT("RemoveMenu"));
ad9bb75f 766 }
2bda0e17 767
717a57c2
VZ
768#if wxUSE_ACCEL
769 if ( menu->HasAccels() )
770 {
771 // need to rebuild accell table
772 RebuildAccelTable();
773 }
774#endif // wxUSE_ACCEL
775
ad9bb75f
VZ
776 Refresh();
777 }
2bda0e17 778
a8cfd0cb 779 m_titles.Remove(pos);
2bda0e17 780
a8cfd0cb 781 return menu;
2bda0e17
KB
782}
783
d427503c 784#if wxUSE_ACCEL
717a57c2
VZ
785
786void wxMenuBar::RebuildAccelTable()
787{
788 // merge the accelerators of all menus into one accel table
42e69d6b 789 size_t nAccelCount = 0;
a8cfd0cb
VZ
790 size_t i, count = GetMenuCount();
791 for ( i = 0; i < count; i++ )
42e69d6b
VZ
792 {
793 nAccelCount += m_menus[i]->GetAccelCount();
794 }
795
5df1250b 796 if ( nAccelCount )
42e69d6b 797 {
5df1250b 798 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
42e69d6b 799
5df1250b 800 nAccelCount = 0;
a8cfd0cb 801 for ( i = 0; i < count; i++ )
5df1250b
VZ
802 {
803 nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]);
804 }
805
806 m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
42e69d6b 807
5df1250b
VZ
808 delete [] accelEntries;
809 }
717a57c2
VZ
810}
811
812#endif // wxUSE_ACCEL
813
814void wxMenuBar::Attach(wxFrame *frame)
815{
1e6feb95 816 wxMenuBarBase::Attach(frame);
717a57c2 817
717a57c2
VZ
818#if wxUSE_ACCEL
819 RebuildAccelTable();
d427503c 820#endif // wxUSE_ACCEL
42e69d6b
VZ
821}
822
1cf27c63
UM
823void wxMenuBar::Detach()
824{
a17e237f 825 m_hMenu = (WXHMENU)NULL;
2bda0e17 826
1e6feb95 827 wxMenuBarBase::Detach();
2bda0e17
KB
828}
829
1e6feb95 830#endif // wxUSE_MENUS