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