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