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