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