]> git.saurik.com Git - wxWidgets.git/blame - src/msw/menu.cpp
enable wxFontMapper in wxDFB port
[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
57bd4c60 32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
c626a8b7 33 #include "wx/frame.h"
c626a8b7 34 #include "wx/utils.h"
0c589ad0 35 #include "wx/intl.h"
717a57c2 36 #include "wx/log.h"
2bda0e17
KB
37#endif
38
47d67540 39#if wxUSE_OWNER_DRAWN
c626a8b7 40 #include "wx/ownerdrw.h"
2bda0e17
KB
41#endif
42
43#include "wx/msw/private.h"
2bda0e17 44
39d2f9a7
JS
45#ifdef __WXWINCE__
46#include <windows.h>
47#include <windowsx.h>
48#include <tchar.h>
49#include <ole2.h>
eae4425d 50#include <shellapi.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 315 // find the (new) accel for this item
90527a50 316 wxAcceleratorEntry *accel = wxAcceleratorEntry::Create(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;
3633deed 403
99a7bebb
JS
404#if wxUSE_OWNER_DRAWN
405 // Currently, mixing owner-drawn and non-owner-drawn items results in
406 // inconsistent margins, so we force this to be owner-drawn if any other
407 // items already are. Later we might want to use a boolean in the wxMenu
408 // to avoid search. Also we might make this fix unnecessary by getting the correct
409 // margin using NONCLIENTMETRICS.
3633deed 410 if ( !pItem->IsOwnerDrawn() && !pItem->IsSeparator() )
99a7bebb
JS
411 {
412 // Check if any other items are ownerdrawn, and make ownerdrawn if so
413 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
414 while (node)
415 {
416 if (node->GetData()->IsOwnerDrawn())
417 {
418 pItem->SetOwnerDrawn(true);
419 break;
420 }
421 node = node->GetNext();
422 }
423 }
424#endif
2bda0e17 425
cb9eed05 426 // check if we have something more than a simple text item
47d67540 427#if wxUSE_OWNER_DRAWN
cb9eed05
VZ
428 if ( pItem->IsOwnerDrawn() )
429 {
9c80e160
VZ
430 // is the item owner-drawn just because of the [checked] bitmap?
431 if ( (pItem->GetBitmap(false).Ok() || pItem->GetBitmap(true).Ok()) &&
cb9eed05
VZ
432 !pItem->GetTextColour().Ok() &&
433 !pItem->GetBackgroundColour().Ok() &&
9c80e160 434 !pItem->GetFont().Ok() )
cb9eed05 435 {
6a17b868
SN
436 // try to use InsertMenuItem() as it's guaranteed to look correct
437 // while our owner-drawn code is not
cb9eed05
VZ
438
439 // first compile-time check
be326aad 440#if defined(MIIM_BITMAP) && (_WIN32_WINNT >= 0x0500)
cb9eed05
VZ
441 WinStruct<MENUITEMINFO> mii;
442
443 // now run-time one: MIIM_BITMAP only works under WinME/2000+
783e2cc6 444 if ( wxGetWinVersion() >= wxWinVersion_98 )
cb9eed05 445 {
2919a8b5 446 mii.fMask = MIIM_STRING | MIIM_DATA | MIIM_BITMAP;
9c80e160
VZ
447 if ( pItem->IsCheckable() )
448 {
449 // need to set checked/unchecked bitmaps as otherwise our
450 // MSWOnDrawItem() item is not called
451 mii.fMask |= MIIM_CHECKMARKS;
452 }
453
cb9eed05
VZ
454 mii.cch = itemText.length();
455 mii.dwTypeData = wx_const_cast(wxChar *, itemText.c_str());
456
2919a8b5
JG
457 if (flags & MF_POPUP)
458 {
459 mii.fMask |= MIIM_SUBMENU;
460 mii.hSubMenu = (HMENU)pItem->GetSubMenu()->GetHMenu();
461 }
462 else
463 {
464 mii.fMask |= MIIM_ID;
465 mii.wID = id;
466 }
467
cb9eed05
VZ
468 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
469 // 1. we can't draw it with transparency then (this is not
470 // very important now but would be with themed menu bg)
6a17b868 471 // 2. worse, Windows inverts the bitmap for the selected
cb9eed05
VZ
472 // item and this looks downright ugly
473 //
474 // so instead draw it ourselves in MSWOnDrawItem()
475 mii.dwItemData = wx_reinterpret_cast(ULONG_PTR, pItem);
9c80e160
VZ
476 if ( pItem->IsCheckable() )
477 {
478 mii.hbmpChecked =
479 mii.hbmpUnchecked = HBMMENU_CALLBACK;
480 }
cb9eed05
VZ
481 mii.hbmpItem = HBMMENU_CALLBACK;
482
483 ok = ::InsertMenuItem(GetHmenu(), pos, TRUE /* by pos */, &mii);
484 if ( !ok )
485 {
486 wxLogLastError(wxT("InsertMenuItem()"));
487 }
488 else // InsertMenuItem() ok
489 {
490 // we need to remove the extra indent which is reserved for
491 // the checkboxes by default as it looks ugly unless check
492 // boxes are used together with bitmaps and this is not the
493 // case in wx API
494 WinStruct<MENUINFO> mi;
495
9f7e1cff
VZ
496 // don't call SetMenuInfo() directly, this would prevent
497 // the app from starting up under Windows 95/NT 4
498 typedef BOOL (WINAPI *SetMenuInfo_t)(HMENU, MENUINFO *);
499
500 wxDynamicLibrary dllUser(_T("user32"));
501 wxDYNLIB_FUNCTION(SetMenuInfo_t, SetMenuInfo, dllUser);
502 if ( pfnSetMenuInfo )
503 {
504 mi.fMask = MIM_STYLE;
505 mi.dwStyle = MNS_CHECKORBMP;
506 if ( !(*pfnSetMenuInfo)(GetHmenu(), &mi) )
507 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
508 }
cb9eed05
VZ
509
510 // tell the item that it's not really owner-drawn but only
511 // needs to draw its bitmap, the rest is done by Windows
512 pItem->ResetOwnerDrawn();
513 }
514 }
515#endif // MIIM_BITMAP
516 }
517
518 if ( !ok )
519 {
520 // item draws itself, pass pointer to it in data parameter
521 flags |= MF_OWNERDRAW;
522 pData = (LPCTSTR)pItem;
523 }
c626a8b7
VZ
524 }
525 else
cb9eed05 526#endif // wxUSE_OWNER_DRAWN
c626a8b7 527 {
6a17b868 528 // item is just a normal string (passed in data parameter)
c626a8b7 529 flags |= MF_STRING;
8fb3a512 530
39d2f9a7 531#ifdef __WXWINCE__
cb9eed05 532 itemText = wxMenuItem::GetLabelFromText(itemText);
39d2f9a7 533#endif
2bda0e17 534
cb9eed05 535 pData = (wxChar*)itemText.c_str();
717a57c2
VZ
536 }
537
6a17b868 538 // item might have already been inserted by InsertMenuItem() above
717a57c2 539 if ( !ok )
c626a8b7 540 {
cb9eed05
VZ
541 if ( !::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData) )
542 {
543 wxLogLastError(wxT("InsertMenu[Item]()"));
717a57c2 544
cb9eed05
VZ
545 return false;
546 }
c626a8b7 547 }
42e69d6b 548
cb9eed05 549
0472ece7 550 // if we just appended the title, highlight it
0472ece7
VZ
551 if ( (int)id == idMenuTitle )
552 {
553 // visually select the menu title
554 SetDefaultMenuItem(GetHmenu(), id);
555 }
42e69d6b 556
0472ece7 557 // if we're already attached to the menubar, we must update it
4224f059 558 if ( IsAttached() && GetMenuBar()->IsAttached() )
0472ece7 559 {
4224f059 560 GetMenuBar()->Refresh();
0472ece7
VZ
561 }
562
598ddd96 563 return true;
0472ece7
VZ
564}
565
566void wxMenu::EndRadioGroup()
567{
0472ece7
VZ
568 // we're not inside a radio group any longer
569 m_startRadioGroup = -1;
2bda0e17
KB
570}
571
9add9367 572wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
2bda0e17 573{
9add9367 574 wxCHECK_MSG( item, NULL, _T("NULL item in wxMenu::DoAppend") );
0472ece7 575
598ddd96 576 bool check = false;
be15b995 577
546bfbea 578 if ( item->GetKind() == wxITEM_RADIO )
0472ece7 579 {
be15b995
VZ
580 int count = GetMenuItemCount();
581
0472ece7
VZ
582 if ( m_startRadioGroup == -1 )
583 {
584 // start a new radio group
be15b995
VZ
585 m_startRadioGroup = count;
586
587 // for now it has just one element
588 item->SetAsRadioGroupStart();
589 item->SetRadioGroupEnd(m_startRadioGroup);
590
591 // ensure that we have a checked item in the radio group
598ddd96 592 check = true;
be15b995
VZ
593 }
594 else // extend the current radio group
595 {
596 // we need to update its end item
597 item->SetRadioGroupStart(m_startRadioGroup);
222ed1d6 598 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
be15b995
VZ
599
600 if ( node )
601 {
602 node->GetData()->SetRadioGroupEnd(count);
603 }
604 else
605 {
606 wxFAIL_MSG( _T("where is the radio group start item?") );
607 }
0472ece7
VZ
608 }
609 }
610 else // not a radio item
611 {
612 EndRadioGroup();
613 }
614
be15b995
VZ
615 if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) )
616 {
9add9367 617 return NULL;
be15b995
VZ
618 }
619
620 if ( check )
621 {
622 // check the item initially
598ddd96 623 item->Check(true);
be15b995
VZ
624 }
625
9add9367 626 return item;
2bda0e17
KB
627}
628
9add9367 629wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
2bda0e17 630{
9add9367
RD
631 if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos))
632 return item;
633 else
634 return NULL;
2bda0e17
KB
635}
636
717a57c2 637wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
2bda0e17 638{
6a17b868 639 // we need to find the item's position in the child list
717a57c2 640 size_t pos;
222ed1d6 641 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
717a57c2 642 for ( pos = 0; node; pos++ )
c626a8b7 643 {
717a57c2 644 if ( node->GetData() == item )
c626a8b7 645 break;
717a57c2
VZ
646
647 node = node->GetNext();
c626a8b7
VZ
648 }
649
6a17b868 650 // DoRemove() (unlike Remove) can only be called for an existing item!
717a57c2 651 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
c626a8b7 652
717a57c2
VZ
653#if wxUSE_ACCEL
654 // remove the corresponding accel from the accel table
655 int n = FindAccel(item->GetId());
656 if ( n != wxNOT_FOUND )
657 {
658 delete m_accels[n];
c626a8b7 659
b54e41c5 660 m_accels.RemoveAt(n);
c626a8b7 661 }
717a57c2
VZ
662 //else: this item doesn't have an accel, nothing to do
663#endif // wxUSE_ACCEL
664
665 // remove the item from the menu
666 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
667 {
f6bcfd97 668 wxLogLastError(wxT("RemoveMenu"));
c626a8b7
VZ
669 }
670
4224f059 671 if ( IsAttached() && GetMenuBar()->IsAttached() )
717a57c2 672 {
6a17b868 673 // otherwise, the change won't be visible
4224f059 674 GetMenuBar()->Refresh();
717a57c2 675 }
2bda0e17 676
717a57c2
VZ
677 // and from internal data structures
678 return wxMenuBase::DoRemove(item);
679}
d427503c 680
42e69d6b
VZ
681// ---------------------------------------------------------------------------
682// accelerator helpers
683// ---------------------------------------------------------------------------
684
717a57c2
VZ
685#if wxUSE_ACCEL
686
6a17b868 687// create the wxAcceleratorEntries for our accels and put them into the provided
42e69d6b
VZ
688// array - return the number of accels we have
689size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
690{
691 size_t count = GetAccelCount();
692 for ( size_t n = 0; n < count; n++ )
693 {
974e8d94 694 *accels++ = *m_accels[n];
42e69d6b
VZ
695 }
696
697 return count;
698}
699
d427503c
VZ
700#endif // wxUSE_ACCEL
701
c2dcfdef 702// ---------------------------------------------------------------------------
717a57c2 703// set wxMenu title
c2dcfdef
VZ
704// ---------------------------------------------------------------------------
705
2bda0e17
KB
706void wxMenu::SetTitle(const wxString& label)
707{
ed7fdb86 708 bool hasNoTitle = m_title.empty();
c626a8b7 709 m_title = label;
b8d3a4f1 710
c50f1fb9 711 HMENU hMenu = GetHmenu();
b8d3a4f1 712
c626a8b7 713 if ( hasNoTitle )
b8d3a4f1 714 {
ed7fdb86 715 if ( !label.empty() )
c626a8b7 716 {
717a57c2
VZ
717 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
718 (unsigned)idMenuTitle, m_title) ||
719 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
c626a8b7 720 {
f6bcfd97 721 wxLogLastError(wxT("InsertMenu"));
c626a8b7
VZ
722 }
723 }
b8d3a4f1
VZ
724 }
725 else
726 {
ed7fdb86 727 if ( label.empty() )
c626a8b7
VZ
728 {
729 // remove the title and the separator after it
730 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
731 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
732 {
f6bcfd97 733 wxLogLastError(wxT("RemoveMenu"));
c626a8b7
VZ
734 }
735 }
736 else
737 {
738 // modify the title
4676948b
JS
739#ifdef __WXWINCE__
740 MENUITEMINFO info;
741 wxZeroMemory(info);
742 info.cbSize = sizeof(info);
743 info.fMask = MIIM_TYPE;
744 info.fType = MFT_STRING;
7ec69821 745 info.cch = m_title.length();
4676948b
JS
746 info.dwTypeData = (LPTSTR) m_title.c_str();
747 if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) )
748 {
749 wxLogLastError(wxT("SetMenuItemInfo"));
750 }
751#else
c626a8b7 752 if ( !ModifyMenu(hMenu, 0u,
717a57c2
VZ
753 MF_BYPOSITION | MF_STRING,
754 (unsigned)idMenuTitle, m_title) )
c626a8b7 755 {
f6bcfd97 756 wxLogLastError(wxT("ModifyMenu"));
c626a8b7 757 }
4676948b 758#endif
c626a8b7 759 }
b8d3a4f1 760 }
b8d3a4f1 761
42e69d6b 762#ifdef __WIN32__
c626a8b7 763 // put the title string in bold face
ed7fdb86 764 if ( !m_title.empty() )
a3f4e9e8 765 {
0472ece7 766 SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle);
a3f4e9e8 767 }
717a57c2 768#endif // Win32
2bda0e17
KB
769}
770
c2dcfdef
VZ
771// ---------------------------------------------------------------------------
772// event processing
773// ---------------------------------------------------------------------------
2bda0e17 774
debe6624 775bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id)
2bda0e17 776{
a3f4e9e8 777 // ignore commands from the menu title
a3f4e9e8
VZ
778 if ( id != (WXWORD)idMenuTitle )
779 {
36f1f456 780 // get the checked status of the command: notice that menuState is the
3103e8a9 781 // old state of the menu, so the test for MF_CHECKED must be inverted
36f1f456
VZ
782 UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND);
783 SendEvent(id, !(menuState & MF_CHECKED));
a3f4e9e8
VZ
784 }
785
598ddd96 786 return true;
2bda0e17
KB
787}
788
c2dcfdef
VZ
789// ---------------------------------------------------------------------------
790// other
791// ---------------------------------------------------------------------------
2bda0e17 792
717a57c2
VZ
793wxWindow *wxMenu::GetWindow() const
794{
795 if ( m_invokingWindow != NULL )
796 return m_invokingWindow;
4224f059
DE
797 else if ( GetMenuBar() != NULL)
798 return GetMenuBar()->GetFrame();
717a57c2
VZ
799
800 return NULL;
c2dcfdef
VZ
801}
802
803// ---------------------------------------------------------------------------
2bda0e17 804// Menu Bar
c2dcfdef
VZ
805// ---------------------------------------------------------------------------
806
807void wxMenuBar::Init()
2bda0e17 808{
c626a8b7 809 m_eventHandler = this;
c626a8b7 810 m_hMenu = 0;
3d487566 811#if wxUSE_TOOLBAR && defined(__WXWINCE__)
39d2f9a7 812 m_toolBar = NULL;
a96b4743
JS
813#endif
814 // Not using a combined wxToolBar/wxMenuBar? then use
815 // a commandbar in WinCE .NET just to implement the
816 // menubar.
3d487566 817#if defined(WINCE_WITH_COMMANDBAR)
a96b4743 818 m_commandBar = NULL;
a9928e9d 819 m_adornmentsAdded = false;
39d2f9a7 820#endif
cba2db0c 821}
2bda0e17 822
c2dcfdef
VZ
823wxMenuBar::wxMenuBar()
824{
825 Init();
826}
827
cba2db0c
JS
828wxMenuBar::wxMenuBar( long WXUNUSED(style) )
829{
c2dcfdef 830 Init();
2bda0e17
KB
831}
832
294ea16d 833wxMenuBar::wxMenuBar(size_t count, wxMenu *menus[], const wxString titles[], long WXUNUSED(style))
2bda0e17 834{
c2dcfdef
VZ
835 Init();
836
a8cfd0cb 837 m_titles.Alloc(count);
c2dcfdef 838
d2103c8c 839 for ( size_t i = 0; i < count; i++ )
a8cfd0cb
VZ
840 {
841 m_menus.Append(menus[i]);
842 m_titles.Add(titles[i]);
2bda0e17 843
a8cfd0cb
VZ
844 menus[i]->Attach(this);
845 }
2bda0e17
KB
846}
847
b8d3a4f1 848wxMenuBar::~wxMenuBar()
2bda0e17 849{
a96b4743 850 // In Windows CE (not .NET), the menubar is always associated
39d2f9a7 851 // with a toolbar, which destroys the menu implicitly.
a9102b36 852#if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
bf95a04f 853 if (GetToolBar())
a9102b36
JS
854 {
855 wxToolMenuBar* toolMenuBar = wxDynamicCast(GetToolBar(), wxToolMenuBar);
856 if (toolMenuBar)
857 toolMenuBar->SetMenuBar(NULL);
858 }
bf95a04f 859#else
7a0363dd
JS
860 // we should free Windows resources only if Windows doesn't do it for us
861 // which happens if we're attached to a frame
862 if (m_hMenu && !IsAttached())
863 {
3d487566 864#if defined(WINCE_WITH_COMMANDBAR)
a96b4743
JS
865 ::DestroyWindow((HWND) m_commandBar);
866 m_commandBar = (WXHWND) NULL;
867#else
7a0363dd 868 ::DestroyMenu((HMENU)m_hMenu);
598ddd96 869#endif
7a0363dd
JS
870 m_hMenu = (WXHMENU)NULL;
871 }
39d2f9a7 872#endif
c2dcfdef 873}
2bda0e17 874
c2dcfdef
VZ
875// ---------------------------------------------------------------------------
876// wxMenuBar helpers
877// ---------------------------------------------------------------------------
878
879void wxMenuBar::Refresh()
880{
c849ecef
VZ
881 if ( IsFrozen() )
882 return;
883
065de612 884 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
c2dcfdef 885
3fd239fa 886#if defined(WINCE_WITHOUT_COMMANDBAR)
39d2f9a7
JS
887 if (GetToolBar())
888 {
889 CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0);
890 }
3fd239fa 891#elif defined(WINCE_WITH_COMMANDBAR)
a96b4743
JS
892 if (m_commandBar)
893 DrawMenuBar((HWND) m_commandBar);
39d2f9a7 894#else
1e6feb95 895 DrawMenuBar(GetHwndOf(GetFrame()));
39d2f9a7 896#endif
c2dcfdef
VZ
897}
898
899WXHMENU wxMenuBar::Create()
900{
6a17b868 901 // Note: this doesn't work at all on Smartphone,
beb471b5
JS
902 // since you have to use resources.
903 // We'll have to find another way to add a menu
904 // by changing/adding menu items to an existing menu.
3d487566 905#if defined(WINCE_WITHOUT_COMMANDBAR)
39d2f9a7
JS
906 if ( m_hMenu != 0 )
907 return m_hMenu;
908
909 if (!GetToolBar())
910 return 0;
911
912 HWND hCommandBar = (HWND) GetToolBar()->GetHWND();
913 HMENU hMenu = (HMENU)::SendMessage(hCommandBar, SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0);
72e7ec5b 914
3b3dc801 915 // hMenu may be zero on Windows Mobile 5. So add the menus anyway.
72e7ec5b 916 if (1) // (hMenu)
39d2f9a7 917 {
598ddd96 918 TBBUTTON tbButton;
39d2f9a7
JS
919 memset(&tbButton, 0, sizeof(TBBUTTON));
920 tbButton.iBitmap = I_IMAGENONE;
921 tbButton.fsState = TBSTATE_ENABLED;
922 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
598ddd96 923
39d2f9a7
JS
924 size_t i;
925 for (i = 0; i < GetMenuCount(); i++)
926 {
927 HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu() ;
928 tbButton.dwData = (DWORD)hPopupMenu;
929 wxString label = wxStripMenuCodes(GetLabelTop(i));
930 tbButton.iString = (int) label.c_str();
beb471b5
JS
931
932 int position = i;
598ddd96 933
39d2f9a7 934 tbButton.idCommand = NewControlId();
beb471b5 935 if (!::SendMessage(hCommandBar, TB_INSERTBUTTON, position, (LPARAM)&tbButton))
39d2f9a7
JS
936 {
937 wxLogLastError(wxT("TB_INSERTBUTTON"));
938 }
939 }
940 }
941 m_hMenu = (WXHMENU) hMenu;
942 return m_hMenu;
943#else
3ca6a5f0 944 if ( m_hMenu != 0 )
717a57c2 945 return m_hMenu;
1cf27c63 946
c2dcfdef 947 m_hMenu = (WXHMENU)::CreateMenu();
2bda0e17 948
c2dcfdef 949 if ( !m_hMenu )
c626a8b7 950 {
f6bcfd97 951 wxLogLastError(wxT("CreateMenu"));
c626a8b7 952 }
c2dcfdef 953 else
c626a8b7 954 {
222ed1d6
MB
955 size_t count = GetMenuCount(), i;
956 wxMenuList::iterator it;
957 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
c2dcfdef
VZ
958 {
959 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
222ed1d6 960 (UINT)(*it)->GetHMenu(),
c2dcfdef
VZ
961 m_titles[i]) )
962 {
f6bcfd97 963 wxLogLastError(wxT("AppendMenu"));
c2dcfdef
VZ
964 }
965 }
c626a8b7 966 }
c626a8b7 967
c2dcfdef 968 return m_hMenu;
39d2f9a7 969#endif
2bda0e17
KB
970}
971
b2c5f143
DE
972int wxMenuBar::MSWPositionForWxMenu(wxMenu *menu, int wxpos)
973{
974 wxASSERT(menu);
975 wxASSERT(menu->GetHMenu());
976 wxASSERT(m_hMenu);
8cc4850c
JS
977
978#if defined(__WXWINCE__)
979 int totalMSWItems = GetMenuCount();
980#else
b2c5f143 981 int totalMSWItems = GetMenuItemCount((HMENU)m_hMenu);
8cc4850c
JS
982#endif
983
b2c5f143
DE
984 int i; // For old C++ compatibility
985 for(i=wxpos; i<totalMSWItems; i++)
986 {
987 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
988 return i;
989 }
990 for(i=0; i<wxpos; i++)
991 {
992 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
993 return i;
994 }
995 wxFAIL;
996 return -1;
997}
998
c2dcfdef 999// ---------------------------------------------------------------------------
3dfac970 1000// wxMenuBar functions to work with the top level submenus
c2dcfdef
VZ
1001// ---------------------------------------------------------------------------
1002
3dfac970
VZ
1003// NB: we don't support owner drawn top level items for now, if we do these
1004// functions would have to be changed to use wxMenuItem as well
2bda0e17 1005
a8cfd0cb 1006void wxMenuBar::EnableTop(size_t pos, bool enable)
2bda0e17 1007{
717a57c2 1008 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
b2c5f143 1009 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
717a57c2
VZ
1010
1011 int flag = enable ? MF_ENABLED : MF_GRAYED;
2bda0e17 1012
b2c5f143 1013 EnableMenuItem((HMENU)m_hMenu, MSWPositionForWxMenu(GetMenu(pos),pos), MF_BYPOSITION | flag);
adc6fb16
VZ
1014
1015 Refresh();
2bda0e17
KB
1016}
1017
a8cfd0cb 1018void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
2bda0e17 1019{
717a57c2
VZ
1020 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
1021
1022 m_titles[pos] = label;
1023
1024 if ( !IsAttached() )
1025 {
1026 return;
1027 }
1028 //else: have to modify the existing menu
1029
b2c5f143
DE
1030 int mswpos = MSWPositionForWxMenu(GetMenu(pos),pos);
1031
8cd85069 1032 UINT id;
b2c5f143 1033 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, mswpos, MF_BYPOSITION);
c2dcfdef 1034 if ( flagsOld == 0xFFFFFFFF )
c626a8b7 1035 {
223d09f6 1036 wxLogLastError(wxT("GetMenuState"));
c2dcfdef
VZ
1037
1038 return;
1039 }
1040
1041 if ( flagsOld & MF_POPUP )
1042 {
1043 // HIBYTE contains the number of items in the submenu in this case
ad9bb75f 1044 flagsOld &= 0xff;
b2c5f143 1045 id = (UINT)::GetSubMenu((HMENU)m_hMenu, mswpos);
c626a8b7
VZ
1046 }
1047 else
8cd85069
VZ
1048 {
1049 id = pos;
1050 }
1051
4676948b
JS
1052#ifdef __WXWINCE__
1053 MENUITEMINFO info;
1054 wxZeroMemory(info);
1055 info.cbSize = sizeof(info);
1056 info.fMask = MIIM_TYPE;
1057 info.fType = MFT_STRING;
7ec69821 1058 info.cch = label.length();
4676948b
JS
1059 info.dwTypeData = (LPTSTR) label.c_str();
1060 if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, & info) )
1061 {
1062 wxLogLastError(wxT("SetMenuItemInfo"));
1063 }
598ddd96 1064
4676948b 1065#else
b2c5f143 1066 if ( ::ModifyMenu(GetHmenu(), mswpos, MF_BYPOSITION | MF_STRING | flagsOld,
4676948b 1067 id, label) == (int)0xFFFFFFFF )
c2dcfdef 1068 {
f6bcfd97 1069 wxLogLastError(wxT("ModifyMenu"));
c2dcfdef 1070 }
4676948b 1071#endif
717a57c2
VZ
1072
1073 Refresh();
2bda0e17
KB
1074}
1075
a8cfd0cb 1076wxString wxMenuBar::GetLabelTop(size_t pos) const
2bda0e17 1077{
717a57c2
VZ
1078 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
1079 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
8cd85069 1080
4a1c207c 1081 return wxMenuItem::GetLabelFromText(m_titles[pos]);
2bda0e17
KB
1082}
1083
ad9bb75f
VZ
1084// ---------------------------------------------------------------------------
1085// wxMenuBar construction
1086// ---------------------------------------------------------------------------
1087
a8cfd0cb 1088wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 1089{
ad9bb75f
VZ
1090 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
1091 if ( !menuOld )
f7f50f49
VZ
1092 return NULL;
1093
ad9bb75f 1094 m_titles[pos] = title;
a8cfd0cb 1095
7e02be85
JS
1096#if defined(WINCE_WITHOUT_COMMANDBAR)
1097 if (IsAttached())
1098#else
1099 if (GetHmenu())
1100#endif
a8cfd0cb 1101 {
b2c5f143
DE
1102 int mswpos = MSWPositionForWxMenu(menuOld,pos);
1103
ad9bb75f 1104 // can't use ModifyMenu() because it deletes the submenu it replaces
b2c5f143 1105 if ( !::RemoveMenu(GetHmenu(), (UINT)mswpos, MF_BYPOSITION) )
a8cfd0cb 1106 {
f6bcfd97 1107 wxLogLastError(wxT("RemoveMenu"));
a8cfd0cb 1108 }
1cf27c63 1109
b2c5f143 1110 if ( !::InsertMenu(GetHmenu(), (UINT)mswpos,
ad9bb75f
VZ
1111 MF_BYPOSITION | MF_POPUP | MF_STRING,
1112 (UINT)GetHmenuOf(menu), title) )
1113 {
f6bcfd97 1114 wxLogLastError(wxT("InsertMenu"));
ad9bb75f
VZ
1115 }
1116
717a57c2
VZ
1117#if wxUSE_ACCEL
1118 if ( menuOld->HasAccels() || menu->HasAccels() )
1119 {
1120 // need to rebuild accell table
1121 RebuildAccelTable();
1122 }
1123#endif // wxUSE_ACCEL
1124
7e02be85
JS
1125 if (IsAttached())
1126 Refresh();
a8cfd0cb 1127 }
ad9bb75f
VZ
1128
1129 return menuOld;
1cf27c63
UM
1130}
1131
a8cfd0cb 1132bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 1133{
b2c5f143
DE
1134 // Find out which MSW item before which we'll be inserting before
1135 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
3d27b574 1136 // If IsAttached() is false this won't be used anyway
7e02be85
JS
1137 bool isAttached =
1138#if defined(WINCE_WITHOUT_COMMANDBAR)
1139 IsAttached();
1140#else
1141 (GetHmenu() != 0);
1142#endif
1143
1144 int mswpos = (!isAttached || (pos == m_menus.GetCount()))
b2c5f143
DE
1145 ? -1 // append the menu
1146 : MSWPositionForWxMenu(GetMenu(pos),pos);
1147
ad9bb75f 1148 if ( !wxMenuBarBase::Insert(pos, menu, title) )
598ddd96 1149 return false;
1cf27c63 1150
ad9bb75f
VZ
1151 m_titles.Insert(title, pos);
1152
7e02be85 1153 if ( isAttached )
ad9bb75f 1154 {
7e02be85 1155#if defined(WINCE_WITHOUT_COMMANDBAR)
39d2f9a7 1156 if (!GetToolBar())
598ddd96
WS
1157 return false;
1158 TBBUTTON tbButton;
39d2f9a7
JS
1159 memset(&tbButton, 0, sizeof(TBBUTTON));
1160 tbButton.iBitmap = I_IMAGENONE;
1161 tbButton.fsState = TBSTATE_ENABLED;
1162 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
598ddd96 1163
39d2f9a7
JS
1164 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1165 tbButton.dwData = (DWORD)hPopupMenu;
1166 wxString label = wxStripMenuCodes(title);
1167 tbButton.iString = (int) label.c_str();
598ddd96 1168
39d2f9a7
JS
1169 tbButton.idCommand = NewControlId();
1170 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1171 {
1172 wxLogLastError(wxT("TB_INSERTBUTTON"));
598ddd96 1173 return false;
39d2f9a7 1174 }
b9a958e6 1175 wxUnusedVar(mswpos);
39d2f9a7 1176#else
b2c5f143 1177 if ( !::InsertMenu(GetHmenu(), mswpos,
ad9bb75f
VZ
1178 MF_BYPOSITION | MF_POPUP | MF_STRING,
1179 (UINT)GetHmenuOf(menu), title) )
1180 {
f6bcfd97 1181 wxLogLastError(wxT("InsertMenu"));
ad9bb75f 1182 }
39d2f9a7 1183#endif
717a57c2
VZ
1184#if wxUSE_ACCEL
1185 if ( menu->HasAccels() )
1186 {
1187 // need to rebuild accell table
1188 RebuildAccelTable();
1189 }
1190#endif // wxUSE_ACCEL
1191
7e02be85
JS
1192 if (IsAttached())
1193 Refresh();
a8cfd0cb 1194 }
ad9bb75f 1195
598ddd96 1196 return true;
1cf27c63
UM
1197}
1198
ad9bb75f 1199bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
2bda0e17 1200{
ad9bb75f 1201 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
598ddd96 1202 wxCHECK_MSG( submenu, false, wxT("can't append invalid menu to menubar") );
2bda0e17 1203
717a57c2 1204 if ( !wxMenuBarBase::Append(menu, title) )
598ddd96 1205 return false;
717a57c2 1206
717a57c2
VZ
1207 m_titles.Add(title);
1208
7e02be85
JS
1209#if defined(WINCE_WITHOUT_COMMANDBAR)
1210 if (IsAttached())
1211#else
1212 if (GetHmenu())
1213#endif
ad9bb75f 1214 {
7e02be85 1215#if defined(WINCE_WITHOUT_COMMANDBAR)
39d2f9a7 1216 if (!GetToolBar())
598ddd96
WS
1217 return false;
1218 TBBUTTON tbButton;
39d2f9a7
JS
1219 memset(&tbButton, 0, sizeof(TBBUTTON));
1220 tbButton.iBitmap = I_IMAGENONE;
1221 tbButton.fsState = TBSTATE_ENABLED;
1222 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
598ddd96 1223
39d2f9a7
JS
1224 size_t pos = GetMenuCount();
1225 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1226 tbButton.dwData = (DWORD)hPopupMenu;
1227 wxString label = wxStripMenuCodes(title);
1228 tbButton.iString = (int) label.c_str();
598ddd96 1229
39d2f9a7
JS
1230 tbButton.idCommand = NewControlId();
1231 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1232 {
1233 wxLogLastError(wxT("TB_INSERTBUTTON"));
598ddd96 1234 return false;
39d2f9a7
JS
1235 }
1236#else
ad9bb75f
VZ
1237 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
1238 (UINT)submenu, title) )
1239 {
1240 wxLogLastError(wxT("AppendMenu"));
1241 }
39d2f9a7 1242#endif
ad9bb75f 1243
717a57c2
VZ
1244#if wxUSE_ACCEL
1245 if ( menu->HasAccels() )
1246 {
39d2f9a7 1247 // need to rebuild accelerator table
717a57c2
VZ
1248 RebuildAccelTable();
1249 }
1250#endif // wxUSE_ACCEL
1251
7e02be85
JS
1252 if (IsAttached())
1253 Refresh();
ad9bb75f 1254 }
2bda0e17 1255
598ddd96 1256 return true;
2bda0e17
KB
1257}
1258
a8cfd0cb 1259wxMenu *wxMenuBar::Remove(size_t pos)
2bda0e17 1260{
a8cfd0cb
VZ
1261 wxMenu *menu = wxMenuBarBase::Remove(pos);
1262 if ( !menu )
1263 return NULL;
c626a8b7 1264
7e02be85
JS
1265#if defined(WINCE_WITHOUT_COMMANDBAR)
1266 if (IsAttached())
1267#else
1268 if (GetHmenu())
1269#endif
ad9bb75f 1270 {
7e02be85 1271#if defined(WINCE_WITHOUT_COMMANDBAR)
39d2f9a7
JS
1272 if (GetToolBar())
1273 {
1274 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0))
1275 {
1276 wxLogLastError(wxT("TB_DELETEBUTTON"));
1277 }
1278 }
1279#else
b2c5f143 1280 if ( !::RemoveMenu(GetHmenu(), (UINT)MSWPositionForWxMenu(menu,pos), MF_BYPOSITION) )
ad9bb75f 1281 {
f6bcfd97 1282 wxLogLastError(wxT("RemoveMenu"));
ad9bb75f 1283 }
39d2f9a7 1284#endif
c4053ed3 1285
717a57c2
VZ
1286#if wxUSE_ACCEL
1287 if ( menu->HasAccels() )
1288 {
1289 // need to rebuild accell table
1290 RebuildAccelTable();
1291 }
1292#endif // wxUSE_ACCEL
1293
7e02be85
JS
1294 if (IsAttached())
1295 Refresh();
ad9bb75f 1296 }
2bda0e17 1297
c4053ed3 1298 m_titles.RemoveAt(pos);
2bda0e17 1299
a8cfd0cb 1300 return menu;
2bda0e17
KB
1301}
1302
d427503c 1303#if wxUSE_ACCEL
717a57c2
VZ
1304
1305void wxMenuBar::RebuildAccelTable()
1306{
1307 // merge the accelerators of all menus into one accel table
42e69d6b 1308 size_t nAccelCount = 0;
a8cfd0cb 1309 size_t i, count = GetMenuCount();
222ed1d6
MB
1310 wxMenuList::iterator it;
1311 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
42e69d6b 1312 {
222ed1d6 1313 nAccelCount += (*it)->GetAccelCount();
42e69d6b
VZ
1314 }
1315
5df1250b 1316 if ( nAccelCount )
42e69d6b 1317 {
5df1250b 1318 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
42e69d6b 1319
5df1250b 1320 nAccelCount = 0;
222ed1d6 1321 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
5df1250b 1322 {
222ed1d6 1323 nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]);
5df1250b
VZ
1324 }
1325
1326 m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
42e69d6b 1327
5df1250b
VZ
1328 delete [] accelEntries;
1329 }
717a57c2
VZ
1330}
1331
1332#endif // wxUSE_ACCEL
1333
1334void wxMenuBar::Attach(wxFrame *frame)
1335{
1e6feb95 1336 wxMenuBarBase::Attach(frame);
717a57c2 1337
3fd239fa 1338#if defined(WINCE_WITH_COMMANDBAR)
a96b4743
JS
1339 if (!m_hMenu)
1340 this->Create();
a96b4743
JS
1341 if (!m_commandBar)
1342 m_commandBar = (WXHWND) CommandBar_Create(wxGetInstance(), (HWND) frame->GetHWND(), NewControlId());
1343 if (m_commandBar)
1344 {
1345 if (m_hMenu)
1346 {
1347 if (!CommandBar_InsertMenubarEx((HWND) m_commandBar, NULL, (LPTSTR) m_hMenu, 0))
1348 {
1349 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1350 }
1351 }
1352 }
1353#endif
a96b4743 1354
717a57c2
VZ
1355#if wxUSE_ACCEL
1356 RebuildAccelTable();
d427503c 1357#endif // wxUSE_ACCEL
42e69d6b
VZ
1358}
1359
3fd239fa 1360#if defined(WINCE_WITH_COMMANDBAR)
a9928e9d
JS
1361bool wxMenuBar::AddAdornments(long style)
1362{
1363 if (m_adornmentsAdded || !m_commandBar)
1364 return false;
1365
1366 if (style & wxCLOSE_BOX)
1367 {
1368 if (!CommandBar_AddAdornments((HWND) m_commandBar, 0, 0))
1369 wxLogLastError(wxT("CommandBar_AddAdornments"));
1370 else
1371 return true;
1372 }
1373 return false;
1374}
1375#endif
1376
1cf27c63
UM
1377void wxMenuBar::Detach()
1378{
1e6feb95 1379 wxMenuBarBase::Detach();
2bda0e17
KB
1380}
1381
1e6feb95 1382#endif // wxUSE_MENUS