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