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