]> git.saurik.com Git - wxWidgets.git/blame - src/msw/menu.cpp
Added missing include
[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() &&
411 !pItem->GetFont().Ok() )
412 {
413 // try to use InsertMenuItem() as it's guaranteed to look correctly
414 // while our owner-drawning code is not
415
416 // first compile-time check
417#ifdef MIIM_BITMAP
418 WinStruct<MENUITEMINFO> mii;
419
420 // now run-time one: MIIM_BITMAP only works under WinME/2000+
783e2cc6 421 if ( wxGetWinVersion() >= wxWinVersion_98 )
cb9eed05 422 {
2919a8b5 423 mii.fMask = MIIM_STRING | MIIM_DATA | MIIM_BITMAP;
cb9eed05
VZ
424 mii.cch = itemText.length();
425 mii.dwTypeData = wx_const_cast(wxChar *, itemText.c_str());
426
2919a8b5
JG
427 if (flags & MF_POPUP)
428 {
429 mii.fMask |= MIIM_SUBMENU;
430 mii.hSubMenu = (HMENU)pItem->GetSubMenu()->GetHMenu();
431 }
432 else
433 {
434 mii.fMask |= MIIM_ID;
435 mii.wID = id;
436 }
437
cb9eed05
VZ
438 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
439 // 1. we can't draw it with transparency then (this is not
440 // very important now but would be with themed menu bg)
441 // 2. worse, Windows inverses the bitmap for the selected
442 // item and this looks downright ugly
443 //
444 // so instead draw it ourselves in MSWOnDrawItem()
445 mii.dwItemData = wx_reinterpret_cast(ULONG_PTR, pItem);
446 mii.hbmpItem = HBMMENU_CALLBACK;
447
448 ok = ::InsertMenuItem(GetHmenu(), pos, TRUE /* by pos */, &mii);
449 if ( !ok )
450 {
451 wxLogLastError(wxT("InsertMenuItem()"));
452 }
453 else // InsertMenuItem() ok
454 {
455 // we need to remove the extra indent which is reserved for
456 // the checkboxes by default as it looks ugly unless check
457 // boxes are used together with bitmaps and this is not the
458 // case in wx API
459 WinStruct<MENUINFO> mi;
460
9f7e1cff
VZ
461 // don't call SetMenuInfo() directly, this would prevent
462 // the app from starting up under Windows 95/NT 4
463 typedef BOOL (WINAPI *SetMenuInfo_t)(HMENU, MENUINFO *);
464
465 wxDynamicLibrary dllUser(_T("user32"));
466 wxDYNLIB_FUNCTION(SetMenuInfo_t, SetMenuInfo, dllUser);
467 if ( pfnSetMenuInfo )
468 {
469 mi.fMask = MIM_STYLE;
470 mi.dwStyle = MNS_CHECKORBMP;
471 if ( !(*pfnSetMenuInfo)(GetHmenu(), &mi) )
472 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
473 }
cb9eed05
VZ
474
475 // tell the item that it's not really owner-drawn but only
476 // needs to draw its bitmap, the rest is done by Windows
477 pItem->ResetOwnerDrawn();
478 }
479 }
480#endif // MIIM_BITMAP
481 }
482
483 if ( !ok )
484 {
485 // item draws itself, pass pointer to it in data parameter
486 flags |= MF_OWNERDRAW;
487 pData = (LPCTSTR)pItem;
488 }
c626a8b7
VZ
489 }
490 else
cb9eed05 491#endif // wxUSE_OWNER_DRAWN
c626a8b7
VZ
492 {
493 // menu is just a normal string (passed in data parameter)
494 flags |= MF_STRING;
8fb3a512 495
39d2f9a7 496#ifdef __WXWINCE__
cb9eed05 497 itemText = wxMenuItem::GetLabelFromText(itemText);
39d2f9a7 498#endif
2bda0e17 499
cb9eed05 500 pData = (wxChar*)itemText.c_str();
717a57c2
VZ
501 }
502
cb9eed05 503 // item might have been already inserted by InsertMenuItem() above
717a57c2 504 if ( !ok )
c626a8b7 505 {
cb9eed05
VZ
506 if ( !::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData) )
507 {
508 wxLogLastError(wxT("InsertMenu[Item]()"));
717a57c2 509
cb9eed05
VZ
510 return false;
511 }
c626a8b7 512 }
42e69d6b 513
cb9eed05 514
0472ece7 515 // if we just appended the title, highlight it
0472ece7
VZ
516 if ( (int)id == idMenuTitle )
517 {
518 // visually select the menu title
519 SetDefaultMenuItem(GetHmenu(), id);
520 }
42e69d6b 521
0472ece7 522 // if we're already attached to the menubar, we must update it
4224f059 523 if ( IsAttached() && GetMenuBar()->IsAttached() )
0472ece7 524 {
4224f059 525 GetMenuBar()->Refresh();
0472ece7
VZ
526 }
527
598ddd96 528 return true;
0472ece7
VZ
529}
530
531void wxMenu::EndRadioGroup()
532{
0472ece7
VZ
533 // we're not inside a radio group any longer
534 m_startRadioGroup = -1;
2bda0e17
KB
535}
536
9add9367 537wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
2bda0e17 538{
9add9367 539 wxCHECK_MSG( item, NULL, _T("NULL item in wxMenu::DoAppend") );
0472ece7 540
598ddd96 541 bool check = false;
be15b995 542
546bfbea 543 if ( item->GetKind() == wxITEM_RADIO )
0472ece7 544 {
be15b995
VZ
545 int count = GetMenuItemCount();
546
0472ece7
VZ
547 if ( m_startRadioGroup == -1 )
548 {
549 // start a new radio group
be15b995
VZ
550 m_startRadioGroup = count;
551
552 // for now it has just one element
553 item->SetAsRadioGroupStart();
554 item->SetRadioGroupEnd(m_startRadioGroup);
555
556 // ensure that we have a checked item in the radio group
598ddd96 557 check = true;
be15b995
VZ
558 }
559 else // extend the current radio group
560 {
561 // we need to update its end item
562 item->SetRadioGroupStart(m_startRadioGroup);
222ed1d6 563 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
be15b995
VZ
564
565 if ( node )
566 {
567 node->GetData()->SetRadioGroupEnd(count);
568 }
569 else
570 {
571 wxFAIL_MSG( _T("where is the radio group start item?") );
572 }
0472ece7
VZ
573 }
574 }
575 else // not a radio item
576 {
577 EndRadioGroup();
578 }
579
be15b995
VZ
580 if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) )
581 {
9add9367 582 return NULL;
be15b995
VZ
583 }
584
585 if ( check )
586 {
587 // check the item initially
598ddd96 588 item->Check(true);
be15b995
VZ
589 }
590
9add9367 591 return item;
2bda0e17
KB
592}
593
9add9367 594wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
2bda0e17 595{
9add9367
RD
596 if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos))
597 return item;
598 else
599 return NULL;
2bda0e17
KB
600}
601
717a57c2 602wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
2bda0e17 603{
717a57c2
VZ
604 // we need to find the items position in the child list
605 size_t pos;
222ed1d6 606 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
717a57c2 607 for ( pos = 0; node; pos++ )
c626a8b7 608 {
717a57c2 609 if ( node->GetData() == item )
c626a8b7 610 break;
717a57c2
VZ
611
612 node = node->GetNext();
c626a8b7
VZ
613 }
614
717a57c2
VZ
615 // DoRemove() (unlike Remove) can only be called for existing item!
616 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
c626a8b7 617
717a57c2
VZ
618#if wxUSE_ACCEL
619 // remove the corresponding accel from the accel table
620 int n = FindAccel(item->GetId());
621 if ( n != wxNOT_FOUND )
622 {
623 delete m_accels[n];
c626a8b7 624
b54e41c5 625 m_accels.RemoveAt(n);
c626a8b7 626 }
717a57c2
VZ
627 //else: this item doesn't have an accel, nothing to do
628#endif // wxUSE_ACCEL
629
630 // remove the item from the menu
631 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
632 {
f6bcfd97 633 wxLogLastError(wxT("RemoveMenu"));
c626a8b7
VZ
634 }
635
4224f059 636 if ( IsAttached() && GetMenuBar()->IsAttached() )
717a57c2
VZ
637 {
638 // otherwise, the chane won't be visible
4224f059 639 GetMenuBar()->Refresh();
717a57c2 640 }
2bda0e17 641
717a57c2
VZ
642 // and from internal data structures
643 return wxMenuBase::DoRemove(item);
644}
d427503c 645
42e69d6b
VZ
646// ---------------------------------------------------------------------------
647// accelerator helpers
648// ---------------------------------------------------------------------------
649
717a57c2
VZ
650#if wxUSE_ACCEL
651
42e69d6b
VZ
652// create the wxAcceleratorEntries for our accels and put them into provided
653// array - return the number of accels we have
654size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
655{
656 size_t count = GetAccelCount();
657 for ( size_t n = 0; n < count; n++ )
658 {
974e8d94 659 *accels++ = *m_accels[n];
42e69d6b
VZ
660 }
661
662 return count;
663}
664
d427503c
VZ
665#endif // wxUSE_ACCEL
666
c2dcfdef 667// ---------------------------------------------------------------------------
717a57c2 668// set wxMenu title
c2dcfdef
VZ
669// ---------------------------------------------------------------------------
670
2bda0e17
KB
671void wxMenu::SetTitle(const wxString& label)
672{
ed7fdb86 673 bool hasNoTitle = m_title.empty();
c626a8b7 674 m_title = label;
b8d3a4f1 675
c50f1fb9 676 HMENU hMenu = GetHmenu();
b8d3a4f1 677
c626a8b7 678 if ( hasNoTitle )
b8d3a4f1 679 {
ed7fdb86 680 if ( !label.empty() )
c626a8b7 681 {
717a57c2
VZ
682 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
683 (unsigned)idMenuTitle, m_title) ||
684 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
c626a8b7 685 {
f6bcfd97 686 wxLogLastError(wxT("InsertMenu"));
c626a8b7
VZ
687 }
688 }
b8d3a4f1
VZ
689 }
690 else
691 {
ed7fdb86 692 if ( label.empty() )
c626a8b7
VZ
693 {
694 // remove the title and the separator after it
695 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
696 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
697 {
f6bcfd97 698 wxLogLastError(wxT("RemoveMenu"));
c626a8b7
VZ
699 }
700 }
701 else
702 {
703 // modify the title
4676948b
JS
704#ifdef __WXWINCE__
705 MENUITEMINFO info;
706 wxZeroMemory(info);
707 info.cbSize = sizeof(info);
708 info.fMask = MIIM_TYPE;
709 info.fType = MFT_STRING;
7ec69821 710 info.cch = m_title.length();
4676948b
JS
711 info.dwTypeData = (LPTSTR) m_title.c_str();
712 if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) )
713 {
714 wxLogLastError(wxT("SetMenuItemInfo"));
715 }
716#else
c626a8b7 717 if ( !ModifyMenu(hMenu, 0u,
717a57c2
VZ
718 MF_BYPOSITION | MF_STRING,
719 (unsigned)idMenuTitle, m_title) )
c626a8b7 720 {
f6bcfd97 721 wxLogLastError(wxT("ModifyMenu"));
c626a8b7 722 }
4676948b 723#endif
c626a8b7 724 }
b8d3a4f1 725 }
b8d3a4f1 726
42e69d6b 727#ifdef __WIN32__
c626a8b7 728 // put the title string in bold face
ed7fdb86 729 if ( !m_title.empty() )
a3f4e9e8 730 {
0472ece7 731 SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle);
a3f4e9e8 732 }
717a57c2 733#endif // Win32
2bda0e17
KB
734}
735
c2dcfdef
VZ
736// ---------------------------------------------------------------------------
737// event processing
738// ---------------------------------------------------------------------------
2bda0e17 739
debe6624 740bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id)
2bda0e17 741{
a3f4e9e8 742 // ignore commands from the menu title
a3f4e9e8
VZ
743 if ( id != (WXWORD)idMenuTitle )
744 {
36f1f456 745 // get the checked status of the command: notice that menuState is the
3103e8a9 746 // old state of the menu, so the test for MF_CHECKED must be inverted
36f1f456
VZ
747 UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND);
748 SendEvent(id, !(menuState & MF_CHECKED));
a3f4e9e8
VZ
749 }
750
598ddd96 751 return true;
2bda0e17
KB
752}
753
c2dcfdef
VZ
754// ---------------------------------------------------------------------------
755// other
756// ---------------------------------------------------------------------------
2bda0e17 757
717a57c2
VZ
758wxWindow *wxMenu::GetWindow() const
759{
760 if ( m_invokingWindow != NULL )
761 return m_invokingWindow;
4224f059
DE
762 else if ( GetMenuBar() != NULL)
763 return GetMenuBar()->GetFrame();
717a57c2
VZ
764
765 return NULL;
c2dcfdef
VZ
766}
767
768// ---------------------------------------------------------------------------
2bda0e17 769// Menu Bar
c2dcfdef
VZ
770// ---------------------------------------------------------------------------
771
772void wxMenuBar::Init()
2bda0e17 773{
c626a8b7 774 m_eventHandler = this;
c626a8b7 775 m_hMenu = 0;
3d487566 776#if wxUSE_TOOLBAR && defined(__WXWINCE__)
39d2f9a7 777 m_toolBar = NULL;
a96b4743
JS
778#endif
779 // Not using a combined wxToolBar/wxMenuBar? then use
780 // a commandbar in WinCE .NET just to implement the
781 // menubar.
3d487566 782#if defined(WINCE_WITH_COMMANDBAR)
a96b4743 783 m_commandBar = NULL;
a9928e9d 784 m_adornmentsAdded = false;
39d2f9a7 785#endif
cba2db0c 786}
2bda0e17 787
c2dcfdef
VZ
788wxMenuBar::wxMenuBar()
789{
790 Init();
791}
792
cba2db0c
JS
793wxMenuBar::wxMenuBar( long WXUNUSED(style) )
794{
c2dcfdef 795 Init();
2bda0e17
KB
796}
797
294ea16d 798wxMenuBar::wxMenuBar(size_t count, wxMenu *menus[], const wxString titles[], long WXUNUSED(style))
2bda0e17 799{
c2dcfdef
VZ
800 Init();
801
a8cfd0cb 802 m_titles.Alloc(count);
c2dcfdef 803
d2103c8c 804 for ( size_t i = 0; i < count; i++ )
a8cfd0cb
VZ
805 {
806 m_menus.Append(menus[i]);
807 m_titles.Add(titles[i]);
2bda0e17 808
a8cfd0cb
VZ
809 menus[i]->Attach(this);
810 }
2bda0e17
KB
811}
812
b8d3a4f1 813wxMenuBar::~wxMenuBar()
2bda0e17 814{
a96b4743 815 // In Windows CE (not .NET), the menubar is always associated
39d2f9a7 816 // with a toolbar, which destroys the menu implicitly.
a9102b36 817#if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
bf95a04f 818 if (GetToolBar())
a9102b36
JS
819 {
820 wxToolMenuBar* toolMenuBar = wxDynamicCast(GetToolBar(), wxToolMenuBar);
821 if (toolMenuBar)
822 toolMenuBar->SetMenuBar(NULL);
823 }
bf95a04f 824#else
7a0363dd
JS
825 // we should free Windows resources only if Windows doesn't do it for us
826 // which happens if we're attached to a frame
827 if (m_hMenu && !IsAttached())
828 {
3d487566 829#if defined(WINCE_WITH_COMMANDBAR)
a96b4743
JS
830 ::DestroyWindow((HWND) m_commandBar);
831 m_commandBar = (WXHWND) NULL;
832#else
7a0363dd 833 ::DestroyMenu((HMENU)m_hMenu);
598ddd96 834#endif
7a0363dd
JS
835 m_hMenu = (WXHMENU)NULL;
836 }
39d2f9a7 837#endif
c2dcfdef 838}
2bda0e17 839
c2dcfdef
VZ
840// ---------------------------------------------------------------------------
841// wxMenuBar helpers
842// ---------------------------------------------------------------------------
843
844void wxMenuBar::Refresh()
845{
065de612 846 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
c2dcfdef 847
3fd239fa 848#if defined(WINCE_WITHOUT_COMMANDBAR)
39d2f9a7
JS
849 if (GetToolBar())
850 {
851 CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0);
852 }
3fd239fa 853#elif defined(WINCE_WITH_COMMANDBAR)
a96b4743
JS
854 if (m_commandBar)
855 DrawMenuBar((HWND) m_commandBar);
39d2f9a7 856#else
1e6feb95 857 DrawMenuBar(GetHwndOf(GetFrame()));
39d2f9a7 858#endif
c2dcfdef
VZ
859}
860
861WXHMENU wxMenuBar::Create()
862{
beb471b5
JS
863 // Note: this totally doesn't work on Smartphone,
864 // since you have to use resources.
865 // We'll have to find another way to add a menu
866 // by changing/adding menu items to an existing menu.
3d487566 867#if defined(WINCE_WITHOUT_COMMANDBAR)
39d2f9a7
JS
868 if ( m_hMenu != 0 )
869 return m_hMenu;
870
871 if (!GetToolBar())
872 return 0;
873
874 HWND hCommandBar = (HWND) GetToolBar()->GetHWND();
875 HMENU hMenu = (HMENU)::SendMessage(hCommandBar, SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0);
876 if (hMenu)
877 {
598ddd96 878 TBBUTTON tbButton;
39d2f9a7
JS
879 memset(&tbButton, 0, sizeof(TBBUTTON));
880 tbButton.iBitmap = I_IMAGENONE;
881 tbButton.fsState = TBSTATE_ENABLED;
882 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
598ddd96 883
39d2f9a7
JS
884 size_t i;
885 for (i = 0; i < GetMenuCount(); i++)
886 {
887 HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu() ;
888 tbButton.dwData = (DWORD)hPopupMenu;
889 wxString label = wxStripMenuCodes(GetLabelTop(i));
890 tbButton.iString = (int) label.c_str();
beb471b5
JS
891
892 int position = i;
598ddd96 893
39d2f9a7 894 tbButton.idCommand = NewControlId();
beb471b5 895 if (!::SendMessage(hCommandBar, TB_INSERTBUTTON, position, (LPARAM)&tbButton))
39d2f9a7
JS
896 {
897 wxLogLastError(wxT("TB_INSERTBUTTON"));
898 }
899 }
900 }
901 m_hMenu = (WXHMENU) hMenu;
902 return m_hMenu;
903#else
3ca6a5f0 904 if ( m_hMenu != 0 )
717a57c2 905 return m_hMenu;
1cf27c63 906
c2dcfdef 907 m_hMenu = (WXHMENU)::CreateMenu();
2bda0e17 908
c2dcfdef 909 if ( !m_hMenu )
c626a8b7 910 {
f6bcfd97 911 wxLogLastError(wxT("CreateMenu"));
c626a8b7 912 }
c2dcfdef 913 else
c626a8b7 914 {
222ed1d6
MB
915 size_t count = GetMenuCount(), i;
916 wxMenuList::iterator it;
917 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
c2dcfdef
VZ
918 {
919 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
222ed1d6 920 (UINT)(*it)->GetHMenu(),
c2dcfdef
VZ
921 m_titles[i]) )
922 {
f6bcfd97 923 wxLogLastError(wxT("AppendMenu"));
c2dcfdef
VZ
924 }
925 }
c626a8b7 926 }
c626a8b7 927
c2dcfdef 928 return m_hMenu;
39d2f9a7 929#endif
2bda0e17
KB
930}
931
b2c5f143
DE
932int wxMenuBar::MSWPositionForWxMenu(wxMenu *menu, int wxpos)
933{
934 wxASSERT(menu);
935 wxASSERT(menu->GetHMenu());
936 wxASSERT(m_hMenu);
8cc4850c
JS
937
938#if defined(__WXWINCE__)
939 int totalMSWItems = GetMenuCount();
940#else
b2c5f143 941 int totalMSWItems = GetMenuItemCount((HMENU)m_hMenu);
8cc4850c
JS
942#endif
943
b2c5f143
DE
944 int i; // For old C++ compatibility
945 for(i=wxpos; i<totalMSWItems; i++)
946 {
947 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
948 return i;
949 }
950 for(i=0; i<wxpos; i++)
951 {
952 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
953 return i;
954 }
955 wxFAIL;
956 return -1;
957}
958
c2dcfdef 959// ---------------------------------------------------------------------------
3dfac970 960// wxMenuBar functions to work with the top level submenus
c2dcfdef
VZ
961// ---------------------------------------------------------------------------
962
3dfac970
VZ
963// NB: we don't support owner drawn top level items for now, if we do these
964// functions would have to be changed to use wxMenuItem as well
2bda0e17 965
a8cfd0cb 966void wxMenuBar::EnableTop(size_t pos, bool enable)
2bda0e17 967{
717a57c2 968 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
b2c5f143 969 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
717a57c2
VZ
970
971 int flag = enable ? MF_ENABLED : MF_GRAYED;
2bda0e17 972
b2c5f143 973 EnableMenuItem((HMENU)m_hMenu, MSWPositionForWxMenu(GetMenu(pos),pos), MF_BYPOSITION | flag);
adc6fb16
VZ
974
975 Refresh();
2bda0e17
KB
976}
977
a8cfd0cb 978void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
2bda0e17 979{
717a57c2
VZ
980 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
981
982 m_titles[pos] = label;
983
984 if ( !IsAttached() )
985 {
986 return;
987 }
988 //else: have to modify the existing menu
989
b2c5f143
DE
990 int mswpos = MSWPositionForWxMenu(GetMenu(pos),pos);
991
8cd85069 992 UINT id;
b2c5f143 993 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, mswpos, MF_BYPOSITION);
c2dcfdef 994 if ( flagsOld == 0xFFFFFFFF )
c626a8b7 995 {
223d09f6 996 wxLogLastError(wxT("GetMenuState"));
c2dcfdef
VZ
997
998 return;
999 }
1000
1001 if ( flagsOld & MF_POPUP )
1002 {
1003 // HIBYTE contains the number of items in the submenu in this case
ad9bb75f 1004 flagsOld &= 0xff;
b2c5f143 1005 id = (UINT)::GetSubMenu((HMENU)m_hMenu, mswpos);
c626a8b7
VZ
1006 }
1007 else
8cd85069
VZ
1008 {
1009 id = pos;
1010 }
1011
4676948b
JS
1012#ifdef __WXWINCE__
1013 MENUITEMINFO info;
1014 wxZeroMemory(info);
1015 info.cbSize = sizeof(info);
1016 info.fMask = MIIM_TYPE;
1017 info.fType = MFT_STRING;
7ec69821 1018 info.cch = label.length();
4676948b
JS
1019 info.dwTypeData = (LPTSTR) label.c_str();
1020 if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, & info) )
1021 {
1022 wxLogLastError(wxT("SetMenuItemInfo"));
1023 }
598ddd96 1024
4676948b 1025#else
b2c5f143 1026 if ( ::ModifyMenu(GetHmenu(), mswpos, MF_BYPOSITION | MF_STRING | flagsOld,
4676948b 1027 id, label) == (int)0xFFFFFFFF )
c2dcfdef 1028 {
f6bcfd97 1029 wxLogLastError(wxT("ModifyMenu"));
c2dcfdef 1030 }
4676948b 1031#endif
717a57c2
VZ
1032
1033 Refresh();
2bda0e17
KB
1034}
1035
a8cfd0cb 1036wxString wxMenuBar::GetLabelTop(size_t pos) const
2bda0e17 1037{
717a57c2
VZ
1038 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
1039 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
8cd85069 1040
4a1c207c 1041 return wxMenuItem::GetLabelFromText(m_titles[pos]);
2bda0e17
KB
1042}
1043
ad9bb75f
VZ
1044// ---------------------------------------------------------------------------
1045// wxMenuBar construction
1046// ---------------------------------------------------------------------------
1047
a8cfd0cb 1048wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 1049{
ad9bb75f
VZ
1050 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
1051 if ( !menuOld )
f7f50f49
VZ
1052 return NULL;
1053
ad9bb75f 1054 m_titles[pos] = title;
a8cfd0cb 1055
ad9bb75f 1056 if ( IsAttached() )
a8cfd0cb 1057 {
b2c5f143
DE
1058 int mswpos = MSWPositionForWxMenu(menuOld,pos);
1059
ad9bb75f 1060 // can't use ModifyMenu() because it deletes the submenu it replaces
b2c5f143 1061 if ( !::RemoveMenu(GetHmenu(), (UINT)mswpos, MF_BYPOSITION) )
a8cfd0cb 1062 {
f6bcfd97 1063 wxLogLastError(wxT("RemoveMenu"));
a8cfd0cb 1064 }
1cf27c63 1065
b2c5f143 1066 if ( !::InsertMenu(GetHmenu(), (UINT)mswpos,
ad9bb75f
VZ
1067 MF_BYPOSITION | MF_POPUP | MF_STRING,
1068 (UINT)GetHmenuOf(menu), title) )
1069 {
f6bcfd97 1070 wxLogLastError(wxT("InsertMenu"));
ad9bb75f
VZ
1071 }
1072
717a57c2
VZ
1073#if wxUSE_ACCEL
1074 if ( menuOld->HasAccels() || menu->HasAccels() )
1075 {
1076 // need to rebuild accell table
1077 RebuildAccelTable();
1078 }
1079#endif // wxUSE_ACCEL
1080
ad9bb75f 1081 Refresh();
a8cfd0cb 1082 }
ad9bb75f
VZ
1083
1084 return menuOld;
1cf27c63
UM
1085}
1086
a8cfd0cb 1087bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 1088{
b2c5f143
DE
1089 // Find out which MSW item before which we'll be inserting before
1090 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
3d27b574
DE
1091 // If IsAttached() is false this won't be used anyway
1092 int mswpos = (!IsAttached() || (pos == m_menus.GetCount()))
b2c5f143
DE
1093 ? -1 // append the menu
1094 : MSWPositionForWxMenu(GetMenu(pos),pos);
1095
ad9bb75f 1096 if ( !wxMenuBarBase::Insert(pos, menu, title) )
598ddd96 1097 return false;
1cf27c63 1098
ad9bb75f
VZ
1099 m_titles.Insert(title, pos);
1100
ad9bb75f
VZ
1101 if ( IsAttached() )
1102 {
3d487566 1103#if defined(WINCE_WITHOUT_COMMANDAR)
39d2f9a7 1104 if (!GetToolBar())
598ddd96
WS
1105 return false;
1106 TBBUTTON tbButton;
39d2f9a7
JS
1107 memset(&tbButton, 0, sizeof(TBBUTTON));
1108 tbButton.iBitmap = I_IMAGENONE;
1109 tbButton.fsState = TBSTATE_ENABLED;
1110 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
598ddd96 1111
39d2f9a7
JS
1112 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1113 tbButton.dwData = (DWORD)hPopupMenu;
1114 wxString label = wxStripMenuCodes(title);
1115 tbButton.iString = (int) label.c_str();
598ddd96 1116
39d2f9a7
JS
1117 tbButton.idCommand = NewControlId();
1118 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1119 {
1120 wxLogLastError(wxT("TB_INSERTBUTTON"));
598ddd96 1121 return false;
39d2f9a7
JS
1122 }
1123#else
b2c5f143 1124 if ( !::InsertMenu(GetHmenu(), mswpos,
ad9bb75f
VZ
1125 MF_BYPOSITION | MF_POPUP | MF_STRING,
1126 (UINT)GetHmenuOf(menu), title) )
1127 {
f6bcfd97 1128 wxLogLastError(wxT("InsertMenu"));
ad9bb75f 1129 }
39d2f9a7 1130#endif
717a57c2
VZ
1131#if wxUSE_ACCEL
1132 if ( menu->HasAccels() )
1133 {
1134 // need to rebuild accell table
1135 RebuildAccelTable();
1136 }
1137#endif // wxUSE_ACCEL
1138
ad9bb75f 1139 Refresh();
a8cfd0cb 1140 }
ad9bb75f 1141
598ddd96 1142 return true;
1cf27c63
UM
1143}
1144
ad9bb75f 1145bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
2bda0e17 1146{
ad9bb75f 1147 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
598ddd96 1148 wxCHECK_MSG( submenu, false, wxT("can't append invalid menu to menubar") );
2bda0e17 1149
717a57c2 1150 if ( !wxMenuBarBase::Append(menu, title) )
598ddd96 1151 return false;
717a57c2 1152
717a57c2
VZ
1153 m_titles.Add(title);
1154
ad9bb75f
VZ
1155 if ( IsAttached() )
1156 {
3d487566 1157#if defined(WINCE_WITHOUT_COMMANDAR)
39d2f9a7 1158 if (!GetToolBar())
598ddd96
WS
1159 return false;
1160 TBBUTTON tbButton;
39d2f9a7
JS
1161 memset(&tbButton, 0, sizeof(TBBUTTON));
1162 tbButton.iBitmap = I_IMAGENONE;
1163 tbButton.fsState = TBSTATE_ENABLED;
1164 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
598ddd96 1165
39d2f9a7
JS
1166 size_t pos = GetMenuCount();
1167 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1168 tbButton.dwData = (DWORD)hPopupMenu;
1169 wxString label = wxStripMenuCodes(title);
1170 tbButton.iString = (int) label.c_str();
598ddd96 1171
39d2f9a7
JS
1172 tbButton.idCommand = NewControlId();
1173 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1174 {
1175 wxLogLastError(wxT("TB_INSERTBUTTON"));
598ddd96 1176 return false;
39d2f9a7
JS
1177 }
1178#else
ad9bb75f
VZ
1179 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
1180 (UINT)submenu, title) )
1181 {
1182 wxLogLastError(wxT("AppendMenu"));
1183 }
39d2f9a7 1184#endif
ad9bb75f 1185
717a57c2
VZ
1186#if wxUSE_ACCEL
1187 if ( menu->HasAccels() )
1188 {
39d2f9a7 1189 // need to rebuild accelerator table
717a57c2
VZ
1190 RebuildAccelTable();
1191 }
1192#endif // wxUSE_ACCEL
1193
ad9bb75f
VZ
1194 Refresh();
1195 }
2bda0e17 1196
598ddd96 1197 return true;
2bda0e17
KB
1198}
1199
a8cfd0cb 1200wxMenu *wxMenuBar::Remove(size_t pos)
2bda0e17 1201{
a8cfd0cb
VZ
1202 wxMenu *menu = wxMenuBarBase::Remove(pos);
1203 if ( !menu )
1204 return NULL;
c626a8b7 1205
ad9bb75f
VZ
1206 if ( IsAttached() )
1207 {
3d487566 1208#if defined(WINCE_WITHOUT_COMMANDAR)
39d2f9a7
JS
1209 if (GetToolBar())
1210 {
1211 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0))
1212 {
1213 wxLogLastError(wxT("TB_DELETEBUTTON"));
1214 }
1215 }
1216#else
b2c5f143 1217 if ( !::RemoveMenu(GetHmenu(), (UINT)MSWPositionForWxMenu(menu,pos), MF_BYPOSITION) )
ad9bb75f 1218 {
f6bcfd97 1219 wxLogLastError(wxT("RemoveMenu"));
ad9bb75f 1220 }
39d2f9a7 1221#endif
c4053ed3 1222
717a57c2
VZ
1223#if wxUSE_ACCEL
1224 if ( menu->HasAccels() )
1225 {
1226 // need to rebuild accell table
1227 RebuildAccelTable();
1228 }
1229#endif // wxUSE_ACCEL
1230
ad9bb75f
VZ
1231 Refresh();
1232 }
2bda0e17 1233
c4053ed3
RN
1234
1235 m_titles.RemoveAt(pos);
2bda0e17 1236
a8cfd0cb 1237 return menu;
2bda0e17
KB
1238}
1239
d427503c 1240#if wxUSE_ACCEL
717a57c2
VZ
1241
1242void wxMenuBar::RebuildAccelTable()
1243{
1244 // merge the accelerators of all menus into one accel table
42e69d6b 1245 size_t nAccelCount = 0;
a8cfd0cb 1246 size_t i, count = GetMenuCount();
222ed1d6
MB
1247 wxMenuList::iterator it;
1248 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
42e69d6b 1249 {
222ed1d6 1250 nAccelCount += (*it)->GetAccelCount();
42e69d6b
VZ
1251 }
1252
5df1250b 1253 if ( nAccelCount )
42e69d6b 1254 {
5df1250b 1255 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
42e69d6b 1256
5df1250b 1257 nAccelCount = 0;
222ed1d6 1258 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
5df1250b 1259 {
222ed1d6 1260 nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]);
5df1250b
VZ
1261 }
1262
1263 m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
42e69d6b 1264
5df1250b
VZ
1265 delete [] accelEntries;
1266 }
717a57c2
VZ
1267}
1268
1269#endif // wxUSE_ACCEL
1270
1271void wxMenuBar::Attach(wxFrame *frame)
1272{
1e6feb95 1273 wxMenuBarBase::Attach(frame);
717a57c2 1274
3fd239fa 1275#if defined(WINCE_WITH_COMMANDBAR)
a96b4743
JS
1276 if (!m_hMenu)
1277 this->Create();
a96b4743
JS
1278 if (!m_commandBar)
1279 m_commandBar = (WXHWND) CommandBar_Create(wxGetInstance(), (HWND) frame->GetHWND(), NewControlId());
1280 if (m_commandBar)
1281 {
1282 if (m_hMenu)
1283 {
1284 if (!CommandBar_InsertMenubarEx((HWND) m_commandBar, NULL, (LPTSTR) m_hMenu, 0))
1285 {
1286 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1287 }
1288 }
1289 }
1290#endif
a96b4743 1291
717a57c2
VZ
1292#if wxUSE_ACCEL
1293 RebuildAccelTable();
d427503c 1294#endif // wxUSE_ACCEL
42e69d6b
VZ
1295}
1296
3fd239fa 1297#if defined(WINCE_WITH_COMMANDBAR)
a9928e9d
JS
1298bool wxMenuBar::AddAdornments(long style)
1299{
1300 if (m_adornmentsAdded || !m_commandBar)
1301 return false;
1302
1303 if (style & wxCLOSE_BOX)
1304 {
1305 if (!CommandBar_AddAdornments((HWND) m_commandBar, 0, 0))
1306 wxLogLastError(wxT("CommandBar_AddAdornments"));
1307 else
1308 return true;
1309 }
1310 return false;
1311}
1312#endif
1313
1cf27c63
UM
1314void wxMenuBar::Detach()
1315{
1e6feb95 1316 wxMenuBarBase::Detach();
2bda0e17
KB
1317}
1318
1e6feb95 1319#endif // wxUSE_MENUS