]> git.saurik.com Git - wxWidgets.git/blame - src/msw/menu.cpp
Minor corrections to native renderer. I also corrected
[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);
72e7ec5b
JS
877
878 // hMenu may be zero on Windows Mobile 5. So add the menus anyway.
879 if (1) // (hMenu)
39d2f9a7 880 {
598ddd96 881 TBBUTTON tbButton;
39d2f9a7
JS
882 memset(&tbButton, 0, sizeof(TBBUTTON));
883 tbButton.iBitmap = I_IMAGENONE;
884 tbButton.fsState = TBSTATE_ENABLED;
885 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
598ddd96 886
39d2f9a7
JS
887 size_t i;
888 for (i = 0; i < GetMenuCount(); i++)
889 {
890 HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu() ;
891 tbButton.dwData = (DWORD)hPopupMenu;
892 wxString label = wxStripMenuCodes(GetLabelTop(i));
893 tbButton.iString = (int) label.c_str();
beb471b5
JS
894
895 int position = i;
598ddd96 896
39d2f9a7 897 tbButton.idCommand = NewControlId();
beb471b5 898 if (!::SendMessage(hCommandBar, TB_INSERTBUTTON, position, (LPARAM)&tbButton))
39d2f9a7
JS
899 {
900 wxLogLastError(wxT("TB_INSERTBUTTON"));
901 }
902 }
903 }
904 m_hMenu = (WXHMENU) hMenu;
905 return m_hMenu;
906#else
3ca6a5f0 907 if ( m_hMenu != 0 )
717a57c2 908 return m_hMenu;
1cf27c63 909
c2dcfdef 910 m_hMenu = (WXHMENU)::CreateMenu();
2bda0e17 911
c2dcfdef 912 if ( !m_hMenu )
c626a8b7 913 {
f6bcfd97 914 wxLogLastError(wxT("CreateMenu"));
c626a8b7 915 }
c2dcfdef 916 else
c626a8b7 917 {
222ed1d6
MB
918 size_t count = GetMenuCount(), i;
919 wxMenuList::iterator it;
920 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
c2dcfdef
VZ
921 {
922 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
222ed1d6 923 (UINT)(*it)->GetHMenu(),
c2dcfdef
VZ
924 m_titles[i]) )
925 {
f6bcfd97 926 wxLogLastError(wxT("AppendMenu"));
c2dcfdef
VZ
927 }
928 }
c626a8b7 929 }
c626a8b7 930
c2dcfdef 931 return m_hMenu;
39d2f9a7 932#endif
2bda0e17
KB
933}
934
b2c5f143
DE
935int wxMenuBar::MSWPositionForWxMenu(wxMenu *menu, int wxpos)
936{
937 wxASSERT(menu);
938 wxASSERT(menu->GetHMenu());
939 wxASSERT(m_hMenu);
8cc4850c
JS
940
941#if defined(__WXWINCE__)
942 int totalMSWItems = GetMenuCount();
943#else
b2c5f143 944 int totalMSWItems = GetMenuItemCount((HMENU)m_hMenu);
8cc4850c
JS
945#endif
946
b2c5f143
DE
947 int i; // For old C++ compatibility
948 for(i=wxpos; i<totalMSWItems; i++)
949 {
950 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
951 return i;
952 }
953 for(i=0; i<wxpos; i++)
954 {
955 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
956 return i;
957 }
958 wxFAIL;
959 return -1;
960}
961
c2dcfdef 962// ---------------------------------------------------------------------------
3dfac970 963// wxMenuBar functions to work with the top level submenus
c2dcfdef
VZ
964// ---------------------------------------------------------------------------
965
3dfac970
VZ
966// NB: we don't support owner drawn top level items for now, if we do these
967// functions would have to be changed to use wxMenuItem as well
2bda0e17 968
a8cfd0cb 969void wxMenuBar::EnableTop(size_t pos, bool enable)
2bda0e17 970{
717a57c2 971 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
b2c5f143 972 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
717a57c2
VZ
973
974 int flag = enable ? MF_ENABLED : MF_GRAYED;
2bda0e17 975
b2c5f143 976 EnableMenuItem((HMENU)m_hMenu, MSWPositionForWxMenu(GetMenu(pos),pos), MF_BYPOSITION | flag);
adc6fb16
VZ
977
978 Refresh();
2bda0e17
KB
979}
980
a8cfd0cb 981void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
2bda0e17 982{
717a57c2
VZ
983 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
984
985 m_titles[pos] = label;
986
987 if ( !IsAttached() )
988 {
989 return;
990 }
991 //else: have to modify the existing menu
992
b2c5f143
DE
993 int mswpos = MSWPositionForWxMenu(GetMenu(pos),pos);
994
8cd85069 995 UINT id;
b2c5f143 996 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, mswpos, MF_BYPOSITION);
c2dcfdef 997 if ( flagsOld == 0xFFFFFFFF )
c626a8b7 998 {
223d09f6 999 wxLogLastError(wxT("GetMenuState"));
c2dcfdef
VZ
1000
1001 return;
1002 }
1003
1004 if ( flagsOld & MF_POPUP )
1005 {
1006 // HIBYTE contains the number of items in the submenu in this case
ad9bb75f 1007 flagsOld &= 0xff;
b2c5f143 1008 id = (UINT)::GetSubMenu((HMENU)m_hMenu, mswpos);
c626a8b7
VZ
1009 }
1010 else
8cd85069
VZ
1011 {
1012 id = pos;
1013 }
1014
4676948b
JS
1015#ifdef __WXWINCE__
1016 MENUITEMINFO info;
1017 wxZeroMemory(info);
1018 info.cbSize = sizeof(info);
1019 info.fMask = MIIM_TYPE;
1020 info.fType = MFT_STRING;
7ec69821 1021 info.cch = label.length();
4676948b
JS
1022 info.dwTypeData = (LPTSTR) label.c_str();
1023 if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, & info) )
1024 {
1025 wxLogLastError(wxT("SetMenuItemInfo"));
1026 }
598ddd96 1027
4676948b 1028#else
b2c5f143 1029 if ( ::ModifyMenu(GetHmenu(), mswpos, MF_BYPOSITION | MF_STRING | flagsOld,
4676948b 1030 id, label) == (int)0xFFFFFFFF )
c2dcfdef 1031 {
f6bcfd97 1032 wxLogLastError(wxT("ModifyMenu"));
c2dcfdef 1033 }
4676948b 1034#endif
717a57c2
VZ
1035
1036 Refresh();
2bda0e17
KB
1037}
1038
a8cfd0cb 1039wxString wxMenuBar::GetLabelTop(size_t pos) const
2bda0e17 1040{
717a57c2
VZ
1041 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
1042 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
8cd85069 1043
4a1c207c 1044 return wxMenuItem::GetLabelFromText(m_titles[pos]);
2bda0e17
KB
1045}
1046
ad9bb75f
VZ
1047// ---------------------------------------------------------------------------
1048// wxMenuBar construction
1049// ---------------------------------------------------------------------------
1050
a8cfd0cb 1051wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 1052{
ad9bb75f
VZ
1053 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
1054 if ( !menuOld )
f7f50f49
VZ
1055 return NULL;
1056
ad9bb75f 1057 m_titles[pos] = title;
a8cfd0cb 1058
7e02be85
JS
1059#if defined(WINCE_WITHOUT_COMMANDBAR)
1060 if (IsAttached())
1061#else
1062 if (GetHmenu())
1063#endif
a8cfd0cb 1064 {
b2c5f143
DE
1065 int mswpos = MSWPositionForWxMenu(menuOld,pos);
1066
ad9bb75f 1067 // can't use ModifyMenu() because it deletes the submenu it replaces
b2c5f143 1068 if ( !::RemoveMenu(GetHmenu(), (UINT)mswpos, MF_BYPOSITION) )
a8cfd0cb 1069 {
f6bcfd97 1070 wxLogLastError(wxT("RemoveMenu"));
a8cfd0cb 1071 }
1cf27c63 1072
b2c5f143 1073 if ( !::InsertMenu(GetHmenu(), (UINT)mswpos,
ad9bb75f
VZ
1074 MF_BYPOSITION | MF_POPUP | MF_STRING,
1075 (UINT)GetHmenuOf(menu), title) )
1076 {
f6bcfd97 1077 wxLogLastError(wxT("InsertMenu"));
ad9bb75f
VZ
1078 }
1079
717a57c2
VZ
1080#if wxUSE_ACCEL
1081 if ( menuOld->HasAccels() || menu->HasAccels() )
1082 {
1083 // need to rebuild accell table
1084 RebuildAccelTable();
1085 }
1086#endif // wxUSE_ACCEL
1087
7e02be85
JS
1088 if (IsAttached())
1089 Refresh();
a8cfd0cb 1090 }
ad9bb75f
VZ
1091
1092 return menuOld;
1cf27c63
UM
1093}
1094
a8cfd0cb 1095bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 1096{
b2c5f143
DE
1097 // Find out which MSW item before which we'll be inserting before
1098 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
3d27b574 1099 // If IsAttached() is false this won't be used anyway
7e02be85
JS
1100 bool isAttached =
1101#if defined(WINCE_WITHOUT_COMMANDBAR)
1102 IsAttached();
1103#else
1104 (GetHmenu() != 0);
1105#endif
1106
1107 int mswpos = (!isAttached || (pos == m_menus.GetCount()))
b2c5f143
DE
1108 ? -1 // append the menu
1109 : MSWPositionForWxMenu(GetMenu(pos),pos);
1110
ad9bb75f 1111 if ( !wxMenuBarBase::Insert(pos, menu, title) )
598ddd96 1112 return false;
1cf27c63 1113
ad9bb75f
VZ
1114 m_titles.Insert(title, pos);
1115
7e02be85 1116 if ( isAttached )
ad9bb75f 1117 {
7e02be85 1118#if defined(WINCE_WITHOUT_COMMANDBAR)
39d2f9a7 1119 if (!GetToolBar())
598ddd96
WS
1120 return false;
1121 TBBUTTON tbButton;
39d2f9a7
JS
1122 memset(&tbButton, 0, sizeof(TBBUTTON));
1123 tbButton.iBitmap = I_IMAGENONE;
1124 tbButton.fsState = TBSTATE_ENABLED;
1125 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
598ddd96 1126
39d2f9a7
JS
1127 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1128 tbButton.dwData = (DWORD)hPopupMenu;
1129 wxString label = wxStripMenuCodes(title);
1130 tbButton.iString = (int) label.c_str();
598ddd96 1131
39d2f9a7
JS
1132 tbButton.idCommand = NewControlId();
1133 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1134 {
1135 wxLogLastError(wxT("TB_INSERTBUTTON"));
598ddd96 1136 return false;
39d2f9a7 1137 }
b9a958e6 1138 wxUnusedVar(mswpos);
39d2f9a7 1139#else
b2c5f143 1140 if ( !::InsertMenu(GetHmenu(), mswpos,
ad9bb75f
VZ
1141 MF_BYPOSITION | MF_POPUP | MF_STRING,
1142 (UINT)GetHmenuOf(menu), title) )
1143 {
f6bcfd97 1144 wxLogLastError(wxT("InsertMenu"));
ad9bb75f 1145 }
39d2f9a7 1146#endif
717a57c2
VZ
1147#if wxUSE_ACCEL
1148 if ( menu->HasAccels() )
1149 {
1150 // need to rebuild accell table
1151 RebuildAccelTable();
1152 }
1153#endif // wxUSE_ACCEL
1154
7e02be85
JS
1155 if (IsAttached())
1156 Refresh();
a8cfd0cb 1157 }
ad9bb75f 1158
598ddd96 1159 return true;
1cf27c63
UM
1160}
1161
ad9bb75f 1162bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
2bda0e17 1163{
ad9bb75f 1164 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
598ddd96 1165 wxCHECK_MSG( submenu, false, wxT("can't append invalid menu to menubar") );
2bda0e17 1166
717a57c2 1167 if ( !wxMenuBarBase::Append(menu, title) )
598ddd96 1168 return false;
717a57c2 1169
717a57c2
VZ
1170 m_titles.Add(title);
1171
7e02be85
JS
1172#if defined(WINCE_WITHOUT_COMMANDBAR)
1173 if (IsAttached())
1174#else
1175 if (GetHmenu())
1176#endif
ad9bb75f 1177 {
7e02be85 1178#if defined(WINCE_WITHOUT_COMMANDBAR)
39d2f9a7 1179 if (!GetToolBar())
598ddd96
WS
1180 return false;
1181 TBBUTTON tbButton;
39d2f9a7
JS
1182 memset(&tbButton, 0, sizeof(TBBUTTON));
1183 tbButton.iBitmap = I_IMAGENONE;
1184 tbButton.fsState = TBSTATE_ENABLED;
1185 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
598ddd96 1186
39d2f9a7
JS
1187 size_t pos = GetMenuCount();
1188 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1189 tbButton.dwData = (DWORD)hPopupMenu;
1190 wxString label = wxStripMenuCodes(title);
1191 tbButton.iString = (int) label.c_str();
598ddd96 1192
39d2f9a7
JS
1193 tbButton.idCommand = NewControlId();
1194 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1195 {
1196 wxLogLastError(wxT("TB_INSERTBUTTON"));
598ddd96 1197 return false;
39d2f9a7
JS
1198 }
1199#else
ad9bb75f
VZ
1200 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
1201 (UINT)submenu, title) )
1202 {
1203 wxLogLastError(wxT("AppendMenu"));
1204 }
39d2f9a7 1205#endif
ad9bb75f 1206
717a57c2
VZ
1207#if wxUSE_ACCEL
1208 if ( menu->HasAccels() )
1209 {
39d2f9a7 1210 // need to rebuild accelerator table
717a57c2
VZ
1211 RebuildAccelTable();
1212 }
1213#endif // wxUSE_ACCEL
1214
7e02be85
JS
1215 if (IsAttached())
1216 Refresh();
ad9bb75f 1217 }
2bda0e17 1218
598ddd96 1219 return true;
2bda0e17
KB
1220}
1221
a8cfd0cb 1222wxMenu *wxMenuBar::Remove(size_t pos)
2bda0e17 1223{
a8cfd0cb
VZ
1224 wxMenu *menu = wxMenuBarBase::Remove(pos);
1225 if ( !menu )
1226 return NULL;
c626a8b7 1227
7e02be85
JS
1228#if defined(WINCE_WITHOUT_COMMANDBAR)
1229 if (IsAttached())
1230#else
1231 if (GetHmenu())
1232#endif
ad9bb75f 1233 {
7e02be85 1234#if defined(WINCE_WITHOUT_COMMANDBAR)
39d2f9a7
JS
1235 if (GetToolBar())
1236 {
1237 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0))
1238 {
1239 wxLogLastError(wxT("TB_DELETEBUTTON"));
1240 }
1241 }
1242#else
b2c5f143 1243 if ( !::RemoveMenu(GetHmenu(), (UINT)MSWPositionForWxMenu(menu,pos), MF_BYPOSITION) )
ad9bb75f 1244 {
f6bcfd97 1245 wxLogLastError(wxT("RemoveMenu"));
ad9bb75f 1246 }
39d2f9a7 1247#endif
c4053ed3 1248
717a57c2
VZ
1249#if wxUSE_ACCEL
1250 if ( menu->HasAccels() )
1251 {
1252 // need to rebuild accell table
1253 RebuildAccelTable();
1254 }
1255#endif // wxUSE_ACCEL
1256
7e02be85
JS
1257 if (IsAttached())
1258 Refresh();
ad9bb75f 1259 }
2bda0e17 1260
c4053ed3 1261 m_titles.RemoveAt(pos);
2bda0e17 1262
a8cfd0cb 1263 return menu;
2bda0e17
KB
1264}
1265
d427503c 1266#if wxUSE_ACCEL
717a57c2
VZ
1267
1268void wxMenuBar::RebuildAccelTable()
1269{
1270 // merge the accelerators of all menus into one accel table
42e69d6b 1271 size_t nAccelCount = 0;
a8cfd0cb 1272 size_t i, count = GetMenuCount();
222ed1d6
MB
1273 wxMenuList::iterator it;
1274 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
42e69d6b 1275 {
222ed1d6 1276 nAccelCount += (*it)->GetAccelCount();
42e69d6b
VZ
1277 }
1278
5df1250b 1279 if ( nAccelCount )
42e69d6b 1280 {
5df1250b 1281 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
42e69d6b 1282
5df1250b 1283 nAccelCount = 0;
222ed1d6 1284 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
5df1250b 1285 {
222ed1d6 1286 nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]);
5df1250b
VZ
1287 }
1288
1289 m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
42e69d6b 1290
5df1250b
VZ
1291 delete [] accelEntries;
1292 }
717a57c2
VZ
1293}
1294
1295#endif // wxUSE_ACCEL
1296
1297void wxMenuBar::Attach(wxFrame *frame)
1298{
1e6feb95 1299 wxMenuBarBase::Attach(frame);
717a57c2 1300
3fd239fa 1301#if defined(WINCE_WITH_COMMANDBAR)
a96b4743
JS
1302 if (!m_hMenu)
1303 this->Create();
a96b4743
JS
1304 if (!m_commandBar)
1305 m_commandBar = (WXHWND) CommandBar_Create(wxGetInstance(), (HWND) frame->GetHWND(), NewControlId());
1306 if (m_commandBar)
1307 {
1308 if (m_hMenu)
1309 {
1310 if (!CommandBar_InsertMenubarEx((HWND) m_commandBar, NULL, (LPTSTR) m_hMenu, 0))
1311 {
1312 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1313 }
1314 }
1315 }
1316#endif
a96b4743 1317
717a57c2
VZ
1318#if wxUSE_ACCEL
1319 RebuildAccelTable();
d427503c 1320#endif // wxUSE_ACCEL
42e69d6b
VZ
1321}
1322
3fd239fa 1323#if defined(WINCE_WITH_COMMANDBAR)
a9928e9d
JS
1324bool wxMenuBar::AddAdornments(long style)
1325{
1326 if (m_adornmentsAdded || !m_commandBar)
1327 return false;
1328
1329 if (style & wxCLOSE_BOX)
1330 {
1331 if (!CommandBar_AddAdornments((HWND) m_commandBar, 0, 0))
1332 wxLogLastError(wxT("CommandBar_AddAdornments"));
1333 else
1334 return true;
1335 }
1336 return false;
1337}
1338#endif
1339
1cf27c63
UM
1340void wxMenuBar::Detach()
1341{
1e6feb95 1342 wxMenuBarBase::Detach();
2bda0e17
KB
1343}
1344
1e6feb95 1345#endif // wxUSE_MENUS