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