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