]> git.saurik.com Git - wxWidgets.git/blame - src/msw/menu.cpp
added possibility to throw different objects and crash directly from the main frame...
[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 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
c2dcfdef
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 46
39d2f9a7
JS
47#ifdef __WXWINCE__
48#include <windows.h>
49#include <windowsx.h>
50#include <tchar.h>
51#include <ole2.h>
eae4425d 52#include <shellapi.h>
39d2f9a7 53#include <commctrl.h>
781a24e8 54#if (_WIN32_WCE < 400) && !defined(__HANDHELDPC__)
39d2f9a7 55#include <aygshell.h>
39d2f9a7
JS
56#endif
57
2d36b3d8
JS
58#include "wx/msw/wince/missing.h"
59
39d2f9a7
JS
60#endif
61
2bda0e17 62// other standard headers
2bda0e17
KB
63#include <string.h>
64
c626a8b7
VZ
65// ----------------------------------------------------------------------------
66// global variables
67// ----------------------------------------------------------------------------
68
69extern wxMenu *wxCurrentPopupMenu;
70
b8d3a4f1
VZ
71// ----------------------------------------------------------------------------
72// constants
73// ----------------------------------------------------------------------------
74
75// the (popup) menu title has this special id
c25f373e 76static const int idMenuTitle = -3;
b8d3a4f1
VZ
77
78// ----------------------------------------------------------------------------
0472ece7 79// private functions
b8d3a4f1 80// ----------------------------------------------------------------------------
c626a8b7 81
0472ece7
VZ
82// make the given menu item default
83static void SetDefaultMenuItem(HMENU hmenu, UINT id)
84{
4676948b 85#ifndef __WXWINCE__
0472ece7
VZ
86 MENUITEMINFO mii;
87 wxZeroMemory(mii);
88 mii.cbSize = sizeof(MENUITEMINFO);
89 mii.fMask = MIIM_STATE;
90 mii.fState = MFS_DEFAULT;
91
92 if ( !::SetMenuItemInfo(hmenu, id, FALSE, &mii) )
93 {
94 wxLogLastError(wxT("SetMenuItemInfo"));
95 }
665b71b1
WS
96#else
97 wxUnusedVar(hmenu);
98 wxUnusedVar(id);
4676948b
JS
99#endif
100}
101
102#ifdef __WXWINCE__
103UINT GetMenuState(HMENU hMenu, UINT id, UINT flags)
104{
105 MENUITEMINFO info;
106 wxZeroMemory(info);
107 info.cbSize = sizeof(info);
108 info.fMask = MIIM_STATE;
598ddd96 109 if ( !::GetMenuItemInfo(hMenu, id, flags & MF_BYCOMMAND ? FALSE : TRUE, & info) )
4676948b
JS
110 wxLogLastError(wxT("GetMenuItemInfo"));
111 return info.fState;
0472ece7 112}
4676948b 113#endif
2bda0e17
KB
114
115// ============================================================================
116// implementation
117// ============================================================================
118
e70b4f10
SC
119#include <wx/listimpl.cpp>
120
121WX_DEFINE_LIST( wxMenuInfoList ) ;
122
123#if wxUSE_EXTENDED_RTTI
124
125WX_DEFINE_FLAGS( wxMenuStyle )
126
3ff066a4
SC
127wxBEGIN_FLAGS( wxMenuStyle )
128 wxFLAGS_MEMBER(wxMENU_TEAROFF)
129wxEND_FLAGS( wxMenuStyle )
e70b4f10
SC
130
131IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu, wxEvtHandler,"wx/menu.h")
132
3ff066a4 133wxCOLLECTION_TYPE_INFO( wxMenuItem * , wxMenuItemList ) ;
e70b4f10
SC
134
135template<> void wxCollectionToVariantArray( wxMenuItemList const &theList, wxxVariantArray &value)
136{
137 wxListCollectionToVariantArray<wxMenuItemList::compatibility_iterator>( theList , value ) ;
138}
139
3ff066a4 140wxBEGIN_PROPERTIES_TABLE(wxMenu)
665b71b1 141 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_MENU_SELECTED , wxCommandEvent)
3ff066a4 142 wxPROPERTY( Title, wxString , SetTitle, GetTitle, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
af498247 143 wxREADONLY_PROPERTY_FLAGS( MenuStyle , wxMenuStyle , long , GetStyle , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4
SC
144 wxPROPERTY_COLLECTION( MenuItems , wxMenuItemList , wxMenuItem* , Append , GetMenuItems , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
145wxEND_PROPERTIES_TABLE()
e70b4f10 146
3ff066a4
SC
147wxBEGIN_HANDLERS_TABLE(wxMenu)
148wxEND_HANDLERS_TABLE()
e70b4f10 149
3ff066a4 150wxDIRECT_CONSTRUCTOR_2( wxMenu , wxString , Title , long , MenuStyle )
e70b4f10
SC
151
152WX_DEFINE_FLAGS( wxMenuBarStyle )
153
3ff066a4
SC
154wxBEGIN_FLAGS( wxMenuBarStyle )
155 wxFLAGS_MEMBER(wxMB_DOCKABLE)
156wxEND_FLAGS( wxMenuBarStyle )
e70b4f10
SC
157
158// the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
2341cf5f 159bool wxMenuBarStreamingCallback( const wxObject *WXUNUSED(object), wxWriter * , wxPersister * , wxxVariantArray & )
e70b4f10
SC
160{
161 return true ;
162}
163
164IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar, wxWindow ,"wx/menu.h",wxMenuBarStreamingCallback)
165
166IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo, wxObject , "wx/menu.h" )
167
3ff066a4 168wxBEGIN_PROPERTIES_TABLE(wxMenuInfo)
af498247 169 wxREADONLY_PROPERTY( Menu , wxMenu* , GetMenu , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
3ff066a4
SC
170 wxREADONLY_PROPERTY( Title , wxString , GetTitle , wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
171wxEND_PROPERTIES_TABLE()
e70b4f10 172
3ff066a4
SC
173wxBEGIN_HANDLERS_TABLE(wxMenuInfo)
174wxEND_HANDLERS_TABLE()
e70b4f10 175
598ddd96 176wxCONSTRUCTOR_2( wxMenuInfo , wxMenu* , Menu , wxString , Title )
e70b4f10 177
3ff066a4 178wxCOLLECTION_TYPE_INFO( wxMenuInfo * , wxMenuInfoList ) ;
e70b4f10
SC
179
180template<> void wxCollectionToVariantArray( wxMenuInfoList const &theList, wxxVariantArray &value)
181{
182 wxListCollectionToVariantArray<wxMenuInfoList::compatibility_iterator>( theList , value ) ;
183}
184
3ff066a4
SC
185wxBEGIN_PROPERTIES_TABLE(wxMenuBar)
186 wxPROPERTY_COLLECTION( MenuInfos , wxMenuInfoList , wxMenuInfo* , Append , GetMenuInfos , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
187wxEND_PROPERTIES_TABLE()
e70b4f10 188
3ff066a4
SC
189wxBEGIN_HANDLERS_TABLE(wxMenuBar)
190wxEND_HANDLERS_TABLE()
e70b4f10 191
3ff066a4 192wxCONSTRUCTOR_DUMMY( wxMenuBar )
e70b4f10
SC
193
194#else
0472ece7
VZ
195IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
196IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow)
e70b4f10
SC
197IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo, wxObject)
198#endif
0472ece7 199
e70b4f10
SC
200const wxMenuInfoList& wxMenuBar::GetMenuInfos() const
201{
202 wxMenuInfoList* list = const_cast< wxMenuInfoList* >( &m_menuInfos ) ;
203 WX_CLEAR_LIST( wxMenuInfoList , *list ) ;
204 for( size_t i = 0 ; i < GetMenuCount() ; ++i )
205 {
206 wxMenuInfo* info = new wxMenuInfo() ;
207 info->Create( const_cast<wxMenuBar*>(this)->GetMenu(i) , GetLabelTop(i) ) ;
208 list->Append( info ) ;
209 }
210 return m_menuInfos ;
211}
066f1b7a 212
c2dcfdef
VZ
213// ---------------------------------------------------------------------------
214// wxMenu construction, adding and removing menu items
215// ---------------------------------------------------------------------------
2bda0e17
KB
216
217// Construct a menu with optional title (then use append)
717a57c2 218void wxMenu::Init()
c626a8b7 219{
598ddd96 220 m_doBreak = false;
0472ece7 221 m_startRadioGroup = -1;
c626a8b7 222
717a57c2
VZ
223 // create the menu
224 m_hMenu = (WXHMENU)CreatePopupMenu();
225 if ( !m_hMenu )
226 {
f6bcfd97 227 wxLogLastError(wxT("CreatePopupMenu"));
717a57c2
VZ
228 }
229
230 // if we have a title, insert it in the beginning of the menu
598ddd96 231 if ( !m_title.IsEmpty() )
c626a8b7 232 {
ad9bb75f
VZ
233 Append(idMenuTitle, m_title);
234 AppendSeparator();
c626a8b7 235 }
2bda0e17
KB
236}
237
238// The wxWindow destructor will take care of deleting the submenus.
b8d3a4f1 239wxMenu::~wxMenu()
2bda0e17 240{
717a57c2
VZ
241 // we should free Windows resources only if Windows doesn't do it for us
242 // which happens if we're attached to a menubar or a submenu of another
243 // menu
244 if ( !IsAttached() && !GetParent() )
c2dcfdef 245 {
ad9bb75f
VZ
246 if ( !::DestroyMenu(GetHmenu()) )
247 {
f6bcfd97 248 wxLogLastError(wxT("DestroyMenu"));
ad9bb75f 249 }
c2dcfdef 250 }
c626a8b7 251
ad9bb75f
VZ
252#if wxUSE_ACCEL
253 // delete accels
254 WX_CLEAR_ARRAY(m_accels);
255#endif // wxUSE_ACCEL
2bda0e17
KB
256}
257
b8d3a4f1 258void wxMenu::Break()
2bda0e17 259{
717a57c2 260 // this will take effect during the next call to Append()
598ddd96 261 m_doBreak = true;
2bda0e17
KB
262}
263
0472ece7
VZ
264void wxMenu::Attach(wxMenuBarBase *menubar)
265{
266 wxMenuBase::Attach(menubar);
267
268 EndRadioGroup();
269}
270
717a57c2
VZ
271#if wxUSE_ACCEL
272
273int wxMenu::FindAccel(int id) const
274{
275 size_t n, count = m_accels.GetCount();
276 for ( n = 0; n < count; n++ )
277 {
278 if ( m_accels[n]->m_command == id )
279 return n;
280 }
281
282 return wxNOT_FOUND;
283}
284
285void wxMenu::UpdateAccel(wxMenuItem *item)
2bda0e17 286{
f6bcfd97 287 if ( item->IsSubMenu() )
717a57c2 288 {
f6bcfd97 289 wxMenu *submenu = item->GetSubMenu();
222ed1d6 290 wxMenuItemList::compatibility_iterator node = submenu->GetMenuItems().GetFirst();
f6bcfd97
BP
291 while ( node )
292 {
293 UpdateAccel(node->GetData());
294
295 node = node->GetNext();
296 }
717a57c2 297 }
f6bcfd97 298 else if ( !item->IsSeparator() )
717a57c2 299 {
f6bcfd97
BP
300 // find the (new) accel for this item
301 wxAcceleratorEntry *accel = wxGetAccelFromString(item->GetText());
717a57c2 302 if ( accel )
f6bcfd97
BP
303 accel->m_command = item->GetId();
304
305 // find the old one
306 int n = FindAccel(item->GetId());
307 if ( n == wxNOT_FOUND )
308 {
309 // no old, add new if any
310 if ( accel )
311 m_accels.Add(accel);
312 else
313 return; // skipping RebuildAccelTable() below
314 }
717a57c2 315 else
f6bcfd97
BP
316 {
317 // replace old with new or just remove the old one if no new
318 delete m_accels[n];
319 if ( accel )
320 m_accels[n] = accel;
321 else
b54e41c5 322 m_accels.RemoveAt(n);
f6bcfd97 323 }
717a57c2 324
f6bcfd97
BP
325 if ( IsAttached() )
326 {
4224f059 327 GetMenuBar()->RebuildAccelTable();
f6bcfd97 328 }
42e69d6b 329 }
f6bcfd97 330 //else: it is a separator, they can't have accels, nothing to do
717a57c2
VZ
331}
332
333#endif // wxUSE_ACCEL
334
335// append a new item or submenu to the menu
336bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos)
337{
338#if wxUSE_ACCEL
339 UpdateAccel(pItem);
d427503c 340#endif // wxUSE_ACCEL
42e69d6b 341
c626a8b7 342 UINT flags = 0;
2bda0e17 343
c2dcfdef
VZ
344 // if "Break" has just been called, insert a menu break before this item
345 // (and don't forget to reset the flag)
c626a8b7
VZ
346 if ( m_doBreak ) {
347 flags |= MF_MENUBREAK;
598ddd96 348 m_doBreak = false;
c626a8b7
VZ
349 }
350
351 if ( pItem->IsSeparator() ) {
352 flags |= MF_SEPARATOR;
353 }
2bda0e17 354
c2dcfdef
VZ
355 // id is the numeric id for normal menu items and HMENU for submenus as
356 // required by ::AppendMenu() API
c626a8b7 357 UINT id;
c2dcfdef
VZ
358 wxMenu *submenu = pItem->GetSubMenu();
359 if ( submenu != NULL ) {
717a57c2
VZ
360 wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") );
361
362 submenu->SetParent(this);
2bda0e17 363
c2dcfdef 364 id = (UINT)submenu->GetHMenu();
2bda0e17 365
c626a8b7
VZ
366 flags |= MF_POPUP;
367 }
368 else {
369 id = pItem->GetId();
370 }
2bda0e17 371
39d2f9a7
JS
372#ifdef __WXWINCE__
373 wxString strippedString;
374#endif
375
837e5743 376 LPCTSTR pData;
2bda0e17 377
47d67540 378#if wxUSE_OWNER_DRAWN
c626a8b7
VZ
379 if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
380 // item draws itself, pass pointer to it in data parameter
381 flags |= MF_OWNERDRAW;
837e5743 382 pData = (LPCTSTR)pItem;
c626a8b7
VZ
383 }
384 else
2bda0e17 385#endif
c626a8b7
VZ
386 {
387 // menu is just a normal string (passed in data parameter)
388 flags |= MF_STRING;
8fb3a512 389
39d2f9a7
JS
390#ifdef __WXWINCE__
391 strippedString = wxStripMenuCodes(pItem->GetText());
392 pData = (wxChar*)strippedString.c_str();
393#else
f5166ed4 394 pData = (wxChar*)pItem->GetText().c_str();
39d2f9a7 395#endif
c626a8b7 396 }
2bda0e17 397
717a57c2
VZ
398 BOOL ok;
399 if ( pos == (size_t)-1 )
400 {
401 ok = ::AppendMenu(GetHmenu(), flags, id, pData);
402 }
403 else
404 {
405 ok = ::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData);
406 }
407
408 if ( !ok )
c626a8b7 409 {
f6bcfd97 410 wxLogLastError(wxT("Insert or AppendMenu"));
717a57c2 411
598ddd96 412 return false;
c626a8b7 413 }
42e69d6b 414
0472ece7
VZ
415 // if we just appended the title, highlight it
416#ifdef __WIN32__
417 if ( (int)id == idMenuTitle )
418 {
419 // visually select the menu title
420 SetDefaultMenuItem(GetHmenu(), id);
421 }
42e69d6b
VZ
422#endif // __WIN32__
423
0472ece7 424 // if we're already attached to the menubar, we must update it
4224f059 425 if ( IsAttached() && GetMenuBar()->IsAttached() )
0472ece7 426 {
4224f059 427 GetMenuBar()->Refresh();
0472ece7
VZ
428 }
429
598ddd96 430 return true;
0472ece7
VZ
431}
432
433void wxMenu::EndRadioGroup()
434{
0472ece7
VZ
435 // we're not inside a radio group any longer
436 m_startRadioGroup = -1;
2bda0e17
KB
437}
438
9add9367 439wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
2bda0e17 440{
9add9367 441 wxCHECK_MSG( item, NULL, _T("NULL item in wxMenu::DoAppend") );
0472ece7 442
598ddd96 443 bool check = false;
be15b995 444
546bfbea 445 if ( item->GetKind() == wxITEM_RADIO )
0472ece7 446 {
be15b995
VZ
447 int count = GetMenuItemCount();
448
0472ece7
VZ
449 if ( m_startRadioGroup == -1 )
450 {
451 // start a new radio group
be15b995
VZ
452 m_startRadioGroup = count;
453
454 // for now it has just one element
455 item->SetAsRadioGroupStart();
456 item->SetRadioGroupEnd(m_startRadioGroup);
457
458 // ensure that we have a checked item in the radio group
598ddd96 459 check = true;
be15b995
VZ
460 }
461 else // extend the current radio group
462 {
463 // we need to update its end item
464 item->SetRadioGroupStart(m_startRadioGroup);
222ed1d6 465 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
be15b995
VZ
466
467 if ( node )
468 {
469 node->GetData()->SetRadioGroupEnd(count);
470 }
471 else
472 {
473 wxFAIL_MSG( _T("where is the radio group start item?") );
474 }
0472ece7
VZ
475 }
476 }
477 else // not a radio item
478 {
479 EndRadioGroup();
480 }
481
be15b995
VZ
482 if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) )
483 {
9add9367 484 return NULL;
be15b995
VZ
485 }
486
487 if ( check )
488 {
489 // check the item initially
598ddd96 490 item->Check(true);
be15b995
VZ
491 }
492
9add9367 493 return item;
2bda0e17
KB
494}
495
9add9367 496wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
2bda0e17 497{
9add9367
RD
498 if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos))
499 return item;
500 else
501 return NULL;
2bda0e17
KB
502}
503
717a57c2 504wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
2bda0e17 505{
717a57c2
VZ
506 // we need to find the items position in the child list
507 size_t pos;
222ed1d6 508 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
717a57c2 509 for ( pos = 0; node; pos++ )
c626a8b7 510 {
717a57c2 511 if ( node->GetData() == item )
c626a8b7 512 break;
717a57c2
VZ
513
514 node = node->GetNext();
c626a8b7
VZ
515 }
516
717a57c2
VZ
517 // DoRemove() (unlike Remove) can only be called for existing item!
518 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
c626a8b7 519
717a57c2
VZ
520#if wxUSE_ACCEL
521 // remove the corresponding accel from the accel table
522 int n = FindAccel(item->GetId());
523 if ( n != wxNOT_FOUND )
524 {
525 delete m_accels[n];
c626a8b7 526
b54e41c5 527 m_accels.RemoveAt(n);
c626a8b7 528 }
717a57c2
VZ
529 //else: this item doesn't have an accel, nothing to do
530#endif // wxUSE_ACCEL
531
532 // remove the item from the menu
533 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
534 {
f6bcfd97 535 wxLogLastError(wxT("RemoveMenu"));
c626a8b7
VZ
536 }
537
4224f059 538 if ( IsAttached() && GetMenuBar()->IsAttached() )
717a57c2
VZ
539 {
540 // otherwise, the chane won't be visible
4224f059 541 GetMenuBar()->Refresh();
717a57c2 542 }
2bda0e17 543
717a57c2
VZ
544 // and from internal data structures
545 return wxMenuBase::DoRemove(item);
546}
d427503c 547
42e69d6b
VZ
548// ---------------------------------------------------------------------------
549// accelerator helpers
550// ---------------------------------------------------------------------------
551
717a57c2
VZ
552#if wxUSE_ACCEL
553
42e69d6b
VZ
554// create the wxAcceleratorEntries for our accels and put them into provided
555// array - return the number of accels we have
556size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
557{
558 size_t count = GetAccelCount();
559 for ( size_t n = 0; n < count; n++ )
560 {
974e8d94 561 *accels++ = *m_accels[n];
42e69d6b
VZ
562 }
563
564 return count;
565}
566
d427503c
VZ
567#endif // wxUSE_ACCEL
568
c2dcfdef 569// ---------------------------------------------------------------------------
717a57c2 570// set wxMenu title
c2dcfdef
VZ
571// ---------------------------------------------------------------------------
572
2bda0e17
KB
573void wxMenu::SetTitle(const wxString& label)
574{
c626a8b7
VZ
575 bool hasNoTitle = m_title.IsEmpty();
576 m_title = label;
b8d3a4f1 577
c50f1fb9 578 HMENU hMenu = GetHmenu();
b8d3a4f1 579
c626a8b7 580 if ( hasNoTitle )
b8d3a4f1 581 {
c626a8b7
VZ
582 if ( !label.IsEmpty() )
583 {
717a57c2
VZ
584 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
585 (unsigned)idMenuTitle, m_title) ||
586 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
c626a8b7 587 {
f6bcfd97 588 wxLogLastError(wxT("InsertMenu"));
c626a8b7
VZ
589 }
590 }
b8d3a4f1
VZ
591 }
592 else
593 {
c626a8b7
VZ
594 if ( label.IsEmpty() )
595 {
596 // remove the title and the separator after it
597 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
598 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
599 {
f6bcfd97 600 wxLogLastError(wxT("RemoveMenu"));
c626a8b7
VZ
601 }
602 }
603 else
604 {
605 // modify the title
4676948b
JS
606#ifdef __WXWINCE__
607 MENUITEMINFO info;
608 wxZeroMemory(info);
609 info.cbSize = sizeof(info);
610 info.fMask = MIIM_TYPE;
611 info.fType = MFT_STRING;
612 info.cch = m_title.Length();
613 info.dwTypeData = (LPTSTR) m_title.c_str();
614 if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) )
615 {
616 wxLogLastError(wxT("SetMenuItemInfo"));
617 }
618#else
c626a8b7 619 if ( !ModifyMenu(hMenu, 0u,
717a57c2
VZ
620 MF_BYPOSITION | MF_STRING,
621 (unsigned)idMenuTitle, m_title) )
c626a8b7 622 {
f6bcfd97 623 wxLogLastError(wxT("ModifyMenu"));
c626a8b7 624 }
4676948b 625#endif
c626a8b7 626 }
b8d3a4f1 627 }
b8d3a4f1 628
42e69d6b 629#ifdef __WIN32__
c626a8b7
VZ
630 // put the title string in bold face
631 if ( !m_title.IsEmpty() )
a3f4e9e8 632 {
0472ece7 633 SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle);
a3f4e9e8 634 }
717a57c2 635#endif // Win32
2bda0e17
KB
636}
637
c2dcfdef
VZ
638// ---------------------------------------------------------------------------
639// event processing
640// ---------------------------------------------------------------------------
2bda0e17 641
debe6624 642bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id)
2bda0e17 643{
a3f4e9e8 644 // ignore commands from the menu title
a3f4e9e8
VZ
645 if ( id != (WXWORD)idMenuTitle )
646 {
36f1f456
VZ
647 // get the checked status of the command: notice that menuState is the
648 // old state of the menu, so the test for MF_CHECKED must be inversed
649 UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND);
650 SendEvent(id, !(menuState & MF_CHECKED));
a3f4e9e8
VZ
651 }
652
598ddd96 653 return true;
2bda0e17
KB
654}
655
c2dcfdef
VZ
656// ---------------------------------------------------------------------------
657// other
658// ---------------------------------------------------------------------------
2bda0e17 659
717a57c2
VZ
660wxWindow *wxMenu::GetWindow() const
661{
662 if ( m_invokingWindow != NULL )
663 return m_invokingWindow;
4224f059
DE
664 else if ( GetMenuBar() != NULL)
665 return GetMenuBar()->GetFrame();
717a57c2
VZ
666
667 return NULL;
c2dcfdef
VZ
668}
669
670// ---------------------------------------------------------------------------
2bda0e17 671// Menu Bar
c2dcfdef
VZ
672// ---------------------------------------------------------------------------
673
674void wxMenuBar::Init()
2bda0e17 675{
c626a8b7 676 m_eventHandler = this;
c626a8b7 677 m_hMenu = 0;
3d487566 678#if wxUSE_TOOLBAR && defined(__WXWINCE__)
39d2f9a7 679 m_toolBar = NULL;
a96b4743
JS
680#endif
681 // Not using a combined wxToolBar/wxMenuBar? then use
682 // a commandbar in WinCE .NET just to implement the
683 // menubar.
3d487566 684#if defined(WINCE_WITH_COMMANDBAR)
a96b4743 685 m_commandBar = NULL;
a9928e9d 686 m_adornmentsAdded = false;
39d2f9a7 687#endif
cba2db0c 688}
2bda0e17 689
c2dcfdef
VZ
690wxMenuBar::wxMenuBar()
691{
692 Init();
693}
694
cba2db0c
JS
695wxMenuBar::wxMenuBar( long WXUNUSED(style) )
696{
c2dcfdef 697 Init();
2bda0e17
KB
698}
699
c2dcfdef 700wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[])
2bda0e17 701{
c2dcfdef
VZ
702 Init();
703
a8cfd0cb 704 m_titles.Alloc(count);
c2dcfdef 705
a8cfd0cb
VZ
706 for ( int i = 0; i < count; i++ )
707 {
708 m_menus.Append(menus[i]);
709 m_titles.Add(titles[i]);
2bda0e17 710
a8cfd0cb
VZ
711 menus[i]->Attach(this);
712 }
2bda0e17
KB
713}
714
b8d3a4f1 715wxMenuBar::~wxMenuBar()
2bda0e17 716{
a96b4743 717 // In Windows CE (not .NET), the menubar is always associated
39d2f9a7 718 // with a toolbar, which destroys the menu implicitly.
3d487566 719#if defined(WINCE_WITHOUT_COMMANDBAR)
bf95a04f
JS
720 if (GetToolBar())
721 GetToolBar()->SetMenuBar(NULL);
722#else
7a0363dd
JS
723 // we should free Windows resources only if Windows doesn't do it for us
724 // which happens if we're attached to a frame
725 if (m_hMenu && !IsAttached())
726 {
3d487566 727#if defined(WINCE_WITH_COMMANDBAR)
a96b4743
JS
728 ::DestroyWindow((HWND) m_commandBar);
729 m_commandBar = (WXHWND) NULL;
730#else
7a0363dd 731 ::DestroyMenu((HMENU)m_hMenu);
598ddd96 732#endif
7a0363dd
JS
733 m_hMenu = (WXHMENU)NULL;
734 }
39d2f9a7 735#endif
c2dcfdef 736}
2bda0e17 737
c2dcfdef
VZ
738// ---------------------------------------------------------------------------
739// wxMenuBar helpers
740// ---------------------------------------------------------------------------
741
742void wxMenuBar::Refresh()
743{
065de612 744 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
c2dcfdef 745
3fd239fa 746#if defined(WINCE_WITHOUT_COMMANDBAR)
39d2f9a7
JS
747 if (GetToolBar())
748 {
749 CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0);
750 }
3fd239fa 751#elif defined(WINCE_WITH_COMMANDBAR)
a96b4743
JS
752 if (m_commandBar)
753 DrawMenuBar((HWND) m_commandBar);
39d2f9a7 754#else
1e6feb95 755 DrawMenuBar(GetHwndOf(GetFrame()));
39d2f9a7 756#endif
c2dcfdef
VZ
757}
758
759WXHMENU wxMenuBar::Create()
760{
beb471b5
JS
761 // Note: this totally doesn't work on Smartphone,
762 // since you have to use resources.
763 // We'll have to find another way to add a menu
764 // by changing/adding menu items to an existing menu.
3d487566 765#if defined(WINCE_WITHOUT_COMMANDBAR)
39d2f9a7
JS
766 if ( m_hMenu != 0 )
767 return m_hMenu;
768
769 if (!GetToolBar())
770 return 0;
771
772 HWND hCommandBar = (HWND) GetToolBar()->GetHWND();
773 HMENU hMenu = (HMENU)::SendMessage(hCommandBar, SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0);
774 if (hMenu)
775 {
598ddd96 776 TBBUTTON tbButton;
39d2f9a7
JS
777 memset(&tbButton, 0, sizeof(TBBUTTON));
778 tbButton.iBitmap = I_IMAGENONE;
779 tbButton.fsState = TBSTATE_ENABLED;
780 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
598ddd96 781
39d2f9a7
JS
782 size_t i;
783 for (i = 0; i < GetMenuCount(); i++)
784 {
785 HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu() ;
786 tbButton.dwData = (DWORD)hPopupMenu;
787 wxString label = wxStripMenuCodes(GetLabelTop(i));
788 tbButton.iString = (int) label.c_str();
beb471b5
JS
789
790 int position = i;
598ddd96 791
39d2f9a7 792 tbButton.idCommand = NewControlId();
beb471b5 793 if (!::SendMessage(hCommandBar, TB_INSERTBUTTON, position, (LPARAM)&tbButton))
39d2f9a7
JS
794 {
795 wxLogLastError(wxT("TB_INSERTBUTTON"));
796 }
797 }
798 }
799 m_hMenu = (WXHMENU) hMenu;
800 return m_hMenu;
801#else
3ca6a5f0 802 if ( m_hMenu != 0 )
717a57c2 803 return m_hMenu;
1cf27c63 804
c2dcfdef 805 m_hMenu = (WXHMENU)::CreateMenu();
2bda0e17 806
c2dcfdef 807 if ( !m_hMenu )
c626a8b7 808 {
f6bcfd97 809 wxLogLastError(wxT("CreateMenu"));
c626a8b7 810 }
c2dcfdef 811 else
c626a8b7 812 {
222ed1d6
MB
813 size_t count = GetMenuCount(), i;
814 wxMenuList::iterator it;
815 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
c2dcfdef
VZ
816 {
817 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
222ed1d6 818 (UINT)(*it)->GetHMenu(),
c2dcfdef
VZ
819 m_titles[i]) )
820 {
f6bcfd97 821 wxLogLastError(wxT("AppendMenu"));
c2dcfdef
VZ
822 }
823 }
c626a8b7 824 }
c626a8b7 825
c2dcfdef 826 return m_hMenu;
39d2f9a7 827#endif
2bda0e17
KB
828}
829
b2c5f143
DE
830int wxMenuBar::MSWPositionForWxMenu(wxMenu *menu, int wxpos)
831{
832 wxASSERT(menu);
833 wxASSERT(menu->GetHMenu());
834 wxASSERT(m_hMenu);
8cc4850c
JS
835
836#if defined(__WXWINCE__)
837 int totalMSWItems = GetMenuCount();
838#else
b2c5f143 839 int totalMSWItems = GetMenuItemCount((HMENU)m_hMenu);
8cc4850c
JS
840#endif
841
b2c5f143
DE
842 int i; // For old C++ compatibility
843 for(i=wxpos; i<totalMSWItems; i++)
844 {
845 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
846 return i;
847 }
848 for(i=0; i<wxpos; i++)
849 {
850 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
851 return i;
852 }
853 wxFAIL;
854 return -1;
855}
856
c2dcfdef 857// ---------------------------------------------------------------------------
3dfac970 858// wxMenuBar functions to work with the top level submenus
c2dcfdef
VZ
859// ---------------------------------------------------------------------------
860
3dfac970
VZ
861// NB: we don't support owner drawn top level items for now, if we do these
862// functions would have to be changed to use wxMenuItem as well
2bda0e17 863
a8cfd0cb 864void wxMenuBar::EnableTop(size_t pos, bool enable)
2bda0e17 865{
717a57c2 866 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
b2c5f143 867 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
717a57c2
VZ
868
869 int flag = enable ? MF_ENABLED : MF_GRAYED;
2bda0e17 870
b2c5f143 871 EnableMenuItem((HMENU)m_hMenu, MSWPositionForWxMenu(GetMenu(pos),pos), MF_BYPOSITION | flag);
adc6fb16
VZ
872
873 Refresh();
2bda0e17
KB
874}
875
a8cfd0cb 876void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
2bda0e17 877{
717a57c2
VZ
878 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
879
880 m_titles[pos] = label;
881
882 if ( !IsAttached() )
883 {
884 return;
885 }
886 //else: have to modify the existing menu
887
b2c5f143
DE
888 int mswpos = MSWPositionForWxMenu(GetMenu(pos),pos);
889
8cd85069 890 UINT id;
b2c5f143 891 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, mswpos, MF_BYPOSITION);
c2dcfdef 892 if ( flagsOld == 0xFFFFFFFF )
c626a8b7 893 {
223d09f6 894 wxLogLastError(wxT("GetMenuState"));
c2dcfdef
VZ
895
896 return;
897 }
898
899 if ( flagsOld & MF_POPUP )
900 {
901 // HIBYTE contains the number of items in the submenu in this case
ad9bb75f 902 flagsOld &= 0xff;
b2c5f143 903 id = (UINT)::GetSubMenu((HMENU)m_hMenu, mswpos);
c626a8b7
VZ
904 }
905 else
8cd85069
VZ
906 {
907 id = pos;
908 }
909
4676948b
JS
910#ifdef __WXWINCE__
911 MENUITEMINFO info;
912 wxZeroMemory(info);
913 info.cbSize = sizeof(info);
914 info.fMask = MIIM_TYPE;
915 info.fType = MFT_STRING;
916 info.cch = label.Length();
917 info.dwTypeData = (LPTSTR) label.c_str();
918 if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, & info) )
919 {
920 wxLogLastError(wxT("SetMenuItemInfo"));
921 }
598ddd96 922
4676948b 923#else
b2c5f143 924 if ( ::ModifyMenu(GetHmenu(), mswpos, MF_BYPOSITION | MF_STRING | flagsOld,
4676948b 925 id, label) == (int)0xFFFFFFFF )
c2dcfdef 926 {
f6bcfd97 927 wxLogLastError(wxT("ModifyMenu"));
c2dcfdef 928 }
4676948b 929#endif
717a57c2
VZ
930
931 Refresh();
2bda0e17
KB
932}
933
a8cfd0cb 934wxString wxMenuBar::GetLabelTop(size_t pos) const
2bda0e17 935{
717a57c2
VZ
936 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
937 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
8cd85069 938
4a1c207c 939 return wxMenuItem::GetLabelFromText(m_titles[pos]);
2bda0e17
KB
940}
941
ad9bb75f
VZ
942// ---------------------------------------------------------------------------
943// wxMenuBar construction
944// ---------------------------------------------------------------------------
945
a8cfd0cb 946wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 947{
ad9bb75f
VZ
948 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
949 if ( !menuOld )
f7f50f49
VZ
950 return NULL;
951
ad9bb75f 952 m_titles[pos] = title;
a8cfd0cb 953
ad9bb75f 954 if ( IsAttached() )
a8cfd0cb 955 {
b2c5f143
DE
956 int mswpos = MSWPositionForWxMenu(menuOld,pos);
957
ad9bb75f 958 // can't use ModifyMenu() because it deletes the submenu it replaces
b2c5f143 959 if ( !::RemoveMenu(GetHmenu(), (UINT)mswpos, MF_BYPOSITION) )
a8cfd0cb 960 {
f6bcfd97 961 wxLogLastError(wxT("RemoveMenu"));
a8cfd0cb 962 }
1cf27c63 963
b2c5f143 964 if ( !::InsertMenu(GetHmenu(), (UINT)mswpos,
ad9bb75f
VZ
965 MF_BYPOSITION | MF_POPUP | MF_STRING,
966 (UINT)GetHmenuOf(menu), title) )
967 {
f6bcfd97 968 wxLogLastError(wxT("InsertMenu"));
ad9bb75f
VZ
969 }
970
717a57c2
VZ
971#if wxUSE_ACCEL
972 if ( menuOld->HasAccels() || menu->HasAccels() )
973 {
974 // need to rebuild accell table
975 RebuildAccelTable();
976 }
977#endif // wxUSE_ACCEL
978
ad9bb75f 979 Refresh();
a8cfd0cb 980 }
ad9bb75f
VZ
981
982 return menuOld;
1cf27c63
UM
983}
984
a8cfd0cb 985bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 986{
b2c5f143
DE
987 // Find out which MSW item before which we'll be inserting before
988 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
3d27b574
DE
989 // If IsAttached() is false this won't be used anyway
990 int mswpos = (!IsAttached() || (pos == m_menus.GetCount()))
b2c5f143
DE
991 ? -1 // append the menu
992 : MSWPositionForWxMenu(GetMenu(pos),pos);
993
ad9bb75f 994 if ( !wxMenuBarBase::Insert(pos, menu, title) )
598ddd96 995 return false;
1cf27c63 996
ad9bb75f
VZ
997 m_titles.Insert(title, pos);
998
ad9bb75f
VZ
999 if ( IsAttached() )
1000 {
3d487566 1001#if defined(WINCE_WITHOUT_COMMANDAR)
39d2f9a7 1002 if (!GetToolBar())
598ddd96
WS
1003 return false;
1004 TBBUTTON tbButton;
39d2f9a7
JS
1005 memset(&tbButton, 0, sizeof(TBBUTTON));
1006 tbButton.iBitmap = I_IMAGENONE;
1007 tbButton.fsState = TBSTATE_ENABLED;
1008 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
598ddd96 1009
39d2f9a7
JS
1010 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1011 tbButton.dwData = (DWORD)hPopupMenu;
1012 wxString label = wxStripMenuCodes(title);
1013 tbButton.iString = (int) label.c_str();
598ddd96 1014
39d2f9a7
JS
1015 tbButton.idCommand = NewControlId();
1016 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1017 {
1018 wxLogLastError(wxT("TB_INSERTBUTTON"));
598ddd96 1019 return false;
39d2f9a7
JS
1020 }
1021#else
b2c5f143 1022 if ( !::InsertMenu(GetHmenu(), mswpos,
ad9bb75f
VZ
1023 MF_BYPOSITION | MF_POPUP | MF_STRING,
1024 (UINT)GetHmenuOf(menu), title) )
1025 {
f6bcfd97 1026 wxLogLastError(wxT("InsertMenu"));
ad9bb75f 1027 }
39d2f9a7 1028#endif
717a57c2
VZ
1029#if wxUSE_ACCEL
1030 if ( menu->HasAccels() )
1031 {
1032 // need to rebuild accell table
1033 RebuildAccelTable();
1034 }
1035#endif // wxUSE_ACCEL
1036
ad9bb75f 1037 Refresh();
a8cfd0cb 1038 }
ad9bb75f 1039
598ddd96 1040 return true;
1cf27c63
UM
1041}
1042
ad9bb75f 1043bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
2bda0e17 1044{
ad9bb75f 1045 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
598ddd96 1046 wxCHECK_MSG( submenu, false, wxT("can't append invalid menu to menubar") );
2bda0e17 1047
717a57c2 1048 if ( !wxMenuBarBase::Append(menu, title) )
598ddd96 1049 return false;
717a57c2 1050
717a57c2
VZ
1051 m_titles.Add(title);
1052
ad9bb75f
VZ
1053 if ( IsAttached() )
1054 {
3d487566 1055#if defined(WINCE_WITHOUT_COMMANDAR)
39d2f9a7 1056 if (!GetToolBar())
598ddd96
WS
1057 return false;
1058 TBBUTTON tbButton;
39d2f9a7
JS
1059 memset(&tbButton, 0, sizeof(TBBUTTON));
1060 tbButton.iBitmap = I_IMAGENONE;
1061 tbButton.fsState = TBSTATE_ENABLED;
1062 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
598ddd96 1063
39d2f9a7
JS
1064 size_t pos = GetMenuCount();
1065 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1066 tbButton.dwData = (DWORD)hPopupMenu;
1067 wxString label = wxStripMenuCodes(title);
1068 tbButton.iString = (int) label.c_str();
598ddd96 1069
39d2f9a7
JS
1070 tbButton.idCommand = NewControlId();
1071 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1072 {
1073 wxLogLastError(wxT("TB_INSERTBUTTON"));
598ddd96 1074 return false;
39d2f9a7
JS
1075 }
1076#else
ad9bb75f
VZ
1077 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
1078 (UINT)submenu, title) )
1079 {
1080 wxLogLastError(wxT("AppendMenu"));
1081 }
39d2f9a7 1082#endif
ad9bb75f 1083
717a57c2
VZ
1084#if wxUSE_ACCEL
1085 if ( menu->HasAccels() )
1086 {
39d2f9a7 1087 // need to rebuild accelerator table
717a57c2
VZ
1088 RebuildAccelTable();
1089 }
1090#endif // wxUSE_ACCEL
1091
ad9bb75f
VZ
1092 Refresh();
1093 }
2bda0e17 1094
598ddd96 1095 return true;
2bda0e17
KB
1096}
1097
a8cfd0cb 1098wxMenu *wxMenuBar::Remove(size_t pos)
2bda0e17 1099{
a8cfd0cb
VZ
1100 wxMenu *menu = wxMenuBarBase::Remove(pos);
1101 if ( !menu )
1102 return NULL;
c626a8b7 1103
ad9bb75f
VZ
1104 if ( IsAttached() )
1105 {
3d487566 1106#if defined(WINCE_WITHOUT_COMMANDAR)
39d2f9a7
JS
1107 if (GetToolBar())
1108 {
1109 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0))
1110 {
1111 wxLogLastError(wxT("TB_DELETEBUTTON"));
1112 }
1113 }
1114#else
b2c5f143 1115 if ( !::RemoveMenu(GetHmenu(), (UINT)MSWPositionForWxMenu(menu,pos), MF_BYPOSITION) )
ad9bb75f 1116 {
f6bcfd97 1117 wxLogLastError(wxT("RemoveMenu"));
ad9bb75f 1118 }
39d2f9a7 1119#endif
c4053ed3 1120
717a57c2
VZ
1121#if wxUSE_ACCEL
1122 if ( menu->HasAccels() )
1123 {
1124 // need to rebuild accell table
1125 RebuildAccelTable();
1126 }
1127#endif // wxUSE_ACCEL
1128
ad9bb75f
VZ
1129 Refresh();
1130 }
2bda0e17 1131
c4053ed3
RN
1132
1133 m_titles.RemoveAt(pos);
2bda0e17 1134
a8cfd0cb 1135 return menu;
2bda0e17
KB
1136}
1137
d427503c 1138#if wxUSE_ACCEL
717a57c2
VZ
1139
1140void wxMenuBar::RebuildAccelTable()
1141{
1142 // merge the accelerators of all menus into one accel table
42e69d6b 1143 size_t nAccelCount = 0;
a8cfd0cb 1144 size_t i, count = GetMenuCount();
222ed1d6
MB
1145 wxMenuList::iterator it;
1146 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
42e69d6b 1147 {
222ed1d6 1148 nAccelCount += (*it)->GetAccelCount();
42e69d6b
VZ
1149 }
1150
5df1250b 1151 if ( nAccelCount )
42e69d6b 1152 {
5df1250b 1153 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
42e69d6b 1154
5df1250b 1155 nAccelCount = 0;
222ed1d6 1156 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
5df1250b 1157 {
222ed1d6 1158 nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]);
5df1250b
VZ
1159 }
1160
1161 m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
42e69d6b 1162
5df1250b
VZ
1163 delete [] accelEntries;
1164 }
717a57c2
VZ
1165}
1166
1167#endif // wxUSE_ACCEL
1168
1169void wxMenuBar::Attach(wxFrame *frame)
1170{
1e6feb95 1171 wxMenuBarBase::Attach(frame);
717a57c2 1172
3fd239fa 1173#if defined(WINCE_WITH_COMMANDBAR)
a96b4743
JS
1174 if (!m_hMenu)
1175 this->Create();
a96b4743
JS
1176 if (!m_commandBar)
1177 m_commandBar = (WXHWND) CommandBar_Create(wxGetInstance(), (HWND) frame->GetHWND(), NewControlId());
1178 if (m_commandBar)
1179 {
1180 if (m_hMenu)
1181 {
1182 if (!CommandBar_InsertMenubarEx((HWND) m_commandBar, NULL, (LPTSTR) m_hMenu, 0))
1183 {
1184 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1185 }
1186 }
1187 }
1188#endif
a96b4743 1189
717a57c2
VZ
1190#if wxUSE_ACCEL
1191 RebuildAccelTable();
d427503c 1192#endif // wxUSE_ACCEL
42e69d6b
VZ
1193}
1194
3fd239fa 1195#if defined(WINCE_WITH_COMMANDBAR)
a9928e9d
JS
1196bool wxMenuBar::AddAdornments(long style)
1197{
1198 if (m_adornmentsAdded || !m_commandBar)
1199 return false;
1200
1201 if (style & wxCLOSE_BOX)
1202 {
1203 if (!CommandBar_AddAdornments((HWND) m_commandBar, 0, 0))
1204 wxLogLastError(wxT("CommandBar_AddAdornments"));
1205 else
1206 return true;
1207 }
1208 return false;
1209}
1210#endif
1211
1cf27c63
UM
1212void wxMenuBar::Detach()
1213{
1e6feb95 1214 wxMenuBarBase::Detach();
2bda0e17
KB
1215}
1216
1e6feb95 1217#endif // wxUSE_MENUS