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