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