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