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