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