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