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