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