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