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