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