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