]> git.saurik.com Git - wxWidgets.git/blame - src/msw/menu.cpp
Fix harmless g++ warning about operator precedence.
[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__
be85a191 193 WinStruct<MENUITEMINFO> mii;
0472ece7
VZ
194 mii.fMask = MIIM_STATE;
195 mii.fState = MFS_DEFAULT;
196
197 if ( !::SetMenuItemInfo(hmenu, id, FALSE, &mii) )
198 {
199 wxLogLastError(wxT("SetMenuItemInfo"));
200 }
d08504df
VZ
201#endif // !__WXWINCE__
202}
203
204// make the given menu item owner-drawn
205void SetOwnerDrawnMenuItem(HMENU WXUNUSED_IN_WINCE(hmenu),
206 UINT WXUNUSED_IN_WINCE(id),
aa4919ed
VZ
207 ULONG_PTR WXUNUSED_IN_WINCE(data),
208 BOOL WXUNUSED_IN_WINCE(byPositon = FALSE))
d08504df
VZ
209{
210#ifndef __WXWINCE__
be85a191 211 WinStruct<MENUITEMINFO> mii;
d08504df
VZ
212 mii.fMask = MIIM_FTYPE | MIIM_DATA;
213 mii.fType = MFT_OWNERDRAW;
214 mii.dwItemData = data;
215
aa4919ed
VZ
216 if ( reinterpret_cast<wxMenuItem*>(data)->IsSeparator() )
217 mii.fType |= MFT_SEPARATOR;
218
219 if ( !::SetMenuItemInfo(hmenu, id, byPositon, &mii) )
d08504df
VZ
220 {
221 wxLogLastError(wxT("SetMenuItemInfo"));
222 }
223#endif // !__WXWINCE__
4676948b
JS
224}
225
226#ifdef __WXWINCE__
227UINT GetMenuState(HMENU hMenu, UINT id, UINT flags)
228{
be85a191 229 WinStruct<MENUITEMINFO> info;
4676948b 230 info.fMask = MIIM_STATE;
3519d946
JS
231 // MF_BYCOMMAND is zero so test MF_BYPOSITION
232 if ( !::GetMenuItemInfo(hMenu, id, flags & MF_BYPOSITION ? TRUE : FALSE , & info) )
43b2d5e7 233 {
4676948b 234 wxLogLastError(wxT("GetMenuItemInfo"));
43b2d5e7 235 }
4676948b 236 return info.fState;
0472ece7 237}
37ddd6ea
VZ
238#endif // __WXWINCE__
239
c7123802 240inline bool IsGreaterThanStdSize(const wxBitmap& bmp)
37ddd6ea 241{
c7123802
VZ
242 return bmp.GetWidth() > ::GetSystemMetrics(SM_CXMENUCHECK) ||
243 bmp.GetHeight() > ::GetSystemMetrics(SM_CYMENUCHECK);
37ddd6ea
VZ
244}
245
246} // anonymous namespace
2bda0e17
KB
247
248// ============================================================================
249// implementation
250// ============================================================================
251
c2dcfdef
VZ
252// ---------------------------------------------------------------------------
253// wxMenu construction, adding and removing menu items
254// ---------------------------------------------------------------------------
2bda0e17
KB
255
256// Construct a menu with optional title (then use append)
96049305 257void wxMenu::InitNoCreate()
c626a8b7 258{
89511b42 259 m_radioData = NULL;
598ddd96 260 m_doBreak = false;
c626a8b7 261
d08504df
VZ
262#if wxUSE_OWNER_DRAWN
263 m_ownerDrawn = false;
264 m_maxBitmapWidth = 0;
9c32ed26 265 m_maxAccelWidth = -1;
d08504df 266#endif // wxUSE_OWNER_DRAWN
96049305
VZ
267}
268
269void wxMenu::Init()
270{
271 InitNoCreate();
d08504df 272
717a57c2
VZ
273 // create the menu
274 m_hMenu = (WXHMENU)CreatePopupMenu();
275 if ( !m_hMenu )
276 {
f6bcfd97 277 wxLogLastError(wxT("CreatePopupMenu"));
717a57c2
VZ
278 }
279
280 // if we have a title, insert it in the beginning of the menu
ed7fdb86 281 if ( !m_title.empty() )
c626a8b7 282 {
163e127d
VS
283 const wxString title = m_title;
284 m_title.clear(); // so that SetTitle() knows there was no title before
285 SetTitle(title);
c626a8b7 286 }
2bda0e17
KB
287}
288
96049305
VZ
289wxMenu::wxMenu(WXHMENU hMenu)
290{
291 InitNoCreate();
292
293 m_hMenu = hMenu;
294
295 // Ensure that our internal idea of how many items we have corresponds to
296 // the real number of items in the menu.
297 //
298 // We could also retrieve the real labels of the items here but it doesn't
299 // seem to be worth the trouble.
300 const int numExistingItems = ::GetMenuItemCount(m_hMenu);
301 for ( int n = 0; n < numExistingItems; n++ )
302 {
303 wxMenuBase::DoAppend(wxMenuItem::New(this, wxID_SEPARATOR));
304 }
305}
306
2bda0e17 307// The wxWindow destructor will take care of deleting the submenus.
b8d3a4f1 308wxMenu::~wxMenu()
2bda0e17 309{
deac32b0
VZ
310 // we should free Windows resources only if Windows doesn't do it for us
311 // which happens if we're attached to a menubar or a submenu of another
312 // menu
313 if ( !IsAttached() && !GetParent() )
c2dcfdef 314 {
deac32b0
VZ
315 if ( !::DestroyMenu(GetHmenu()) )
316 {
317 wxLogLastError(wxT("DestroyMenu"));
318 }
c2dcfdef 319 }
c626a8b7 320
ad9bb75f
VZ
321#if wxUSE_ACCEL
322 // delete accels
323 WX_CLEAR_ARRAY(m_accels);
324#endif // wxUSE_ACCEL
89511b42
VZ
325
326 delete m_radioData;
2bda0e17
KB
327}
328
b8d3a4f1 329void wxMenu::Break()
2bda0e17 330{
717a57c2 331 // this will take effect during the next call to Append()
598ddd96 332 m_doBreak = true;
2bda0e17
KB
333}
334
717a57c2
VZ
335#if wxUSE_ACCEL
336
337int wxMenu::FindAccel(int id) const
338{
339 size_t n, count = m_accels.GetCount();
340 for ( n = 0; n < count; n++ )
341 {
342 if ( m_accels[n]->m_command == id )
343 return n;
344 }
345
346 return wxNOT_FOUND;
347}
348
349void wxMenu::UpdateAccel(wxMenuItem *item)
2bda0e17 350{
f6bcfd97 351 if ( item->IsSubMenu() )
717a57c2 352 {
f6bcfd97 353 wxMenu *submenu = item->GetSubMenu();
222ed1d6 354 wxMenuItemList::compatibility_iterator node = submenu->GetMenuItems().GetFirst();
f6bcfd97
BP
355 while ( node )
356 {
357 UpdateAccel(node->GetData());
358
359 node = node->GetNext();
360 }
717a57c2 361 }
f6bcfd97 362 else if ( !item->IsSeparator() )
717a57c2 363 {
99f0dc68
VZ
364 // recurse upwards: we should only modify m_accels of the top level
365 // menus, not of the submenus as wxMenuBar doesn't look at them
366 // (alternative and arguable cleaner solution would be to recurse
367 // downwards in GetAccelCount() and CopyAccels())
368 if ( GetParent() )
369 {
370 GetParent()->UpdateAccel(item);
371 return;
372 }
373
f6bcfd97 374 // find the (new) accel for this item
52af3158 375 wxAcceleratorEntry *accel = wxAcceleratorEntry::Create(item->GetItemLabel());
717a57c2 376 if ( accel )
f6bcfd97
BP
377 accel->m_command = item->GetId();
378
379 // find the old one
380 int n = FindAccel(item->GetId());
381 if ( n == wxNOT_FOUND )
382 {
383 // no old, add new if any
384 if ( accel )
385 m_accels.Add(accel);
386 else
387 return; // skipping RebuildAccelTable() below
388 }
717a57c2 389 else
f6bcfd97
BP
390 {
391 // replace old with new or just remove the old one if no new
392 delete m_accels[n];
393 if ( accel )
394 m_accels[n] = accel;
395 else
b54e41c5 396 m_accels.RemoveAt(n);
f6bcfd97 397 }
717a57c2 398
f6bcfd97
BP
399 if ( IsAttached() )
400 {
4224f059 401 GetMenuBar()->RebuildAccelTable();
f6bcfd97 402 }
9c32ed26 403
af36ec0a 404#if wxUSE_OWNER_DRAWN
9c32ed26 405 ResetMaxAccelWidth();
51c2f7ea 406#endif
42e69d6b 407 }
f6bcfd97 408 //else: it is a separator, they can't have accels, nothing to do
717a57c2
VZ
409}
410
411#endif // wxUSE_ACCEL
412
a5d85ef0
VZ
413namespace
414{
415
416// helper of DoInsertOrAppend(): returns the HBITMAP to use in MENUITEMINFO
417HBITMAP GetHBitmapForMenu(wxMenuItem *pItem, bool checked = true)
418{
419 // Under versions of Windows older than Vista we can't pass HBITMAP
420 // directly as hbmpItem for 2 reasons:
421 // 1. We can't draw it with transparency then (this is not
422 // very important now but would be with themed menu bg)
423 // 2. Worse, Windows inverts the bitmap for the selected
424 // item and this looks downright ugly
425 //
426 // So we prefer to instead draw it ourselves in MSWOnDrawItem().by using
427 // HBMMENU_CALLBACK when inserting it
428 //
429 // However under Vista using HBMMENU_CALLBACK causes the entire menu to be
430 // drawn using the classic theme instead of the current one and it does
431 // handle transparency just fine so do use the real bitmap there
432#if wxUSE_IMAGE
433 if ( wxGetWinVersion() >= wxWinVersion_Vista )
434 {
af36ec0a 435#if wxUSE_OWNER_DRAWN
a5d85ef0
VZ
436 wxBitmap bmp = pItem->GetBitmap(checked);
437 if ( bmp.IsOk() )
438 {
439 // we must use PARGB DIB for the menu bitmaps so ensure that we do
440 wxImage img(bmp.ConvertToImage());
441 if ( !img.HasAlpha() )
442 {
443 img.InitAlpha();
444 pItem->SetBitmap(img, checked);
445 }
446
bbb8a1aa 447 return GetHbitmapOf(pItem->GetBitmap(checked));
a5d85ef0 448 }
af36ec0a 449#endif // wxUSE_OWNER_DRAWN
4e7c767f
VZ
450 //else: bitmap is not set
451
452 return NULL;
a5d85ef0
VZ
453 }
454#endif // wxUSE_IMAGE
455
456 return HBMMENU_CALLBACK;
457}
458
459} // anonymous namespace
460
89511b42
VZ
461bool wxMenu::MSWGetRadioGroupRange(int pos, int *start, int *end) const
462{
463 return m_radioData && m_radioData->GetGroupRange(pos, start, end);
464}
465
717a57c2
VZ
466// append a new item or submenu to the menu
467bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos)
468{
469#if wxUSE_ACCEL
470 UpdateAccel(pItem);
d427503c 471#endif // wxUSE_ACCEL
42e69d6b 472
67cb1dfa
VZ
473 // we should support disabling the item even prior to adding it to the menu
474 UINT flags = pItem->IsEnabled() ? MF_ENABLED : MF_GRAYED;
2bda0e17 475
c2dcfdef
VZ
476 // if "Break" has just been called, insert a menu break before this item
477 // (and don't forget to reset the flag)
c626a8b7
VZ
478 if ( m_doBreak ) {
479 flags |= MF_MENUBREAK;
598ddd96 480 m_doBreak = false;
c626a8b7
VZ
481 }
482
483 if ( pItem->IsSeparator() ) {
484 flags |= MF_SEPARATOR;
485 }
2bda0e17 486
c2dcfdef
VZ
487 // id is the numeric id for normal menu items and HMENU for submenus as
488 // required by ::AppendMenu() API
dca0f651 489 UINT_PTR id;
c2dcfdef
VZ
490 wxMenu *submenu = pItem->GetSubMenu();
491 if ( submenu != NULL ) {
717a57c2
VZ
492 wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") );
493
494 submenu->SetParent(this);
2bda0e17 495
dca0f651 496 id = (UINT_PTR)submenu->GetHMenu();
2bda0e17 497
c626a8b7
VZ
498 flags |= MF_POPUP;
499 }
500 else {
660e7fda 501 id = pItem->GetMSWId();
c626a8b7 502 }
2bda0e17 503
39d2f9a7 504
cb9eed05 505 // prepare to insert the item in the menu
52af3158 506 wxString itemText = pItem->GetItemLabel();
cb9eed05
VZ
507 LPCTSTR pData = NULL;
508 if ( pos == (size_t)-1 )
509 {
e7084852
VZ
510 // append at the end (note that the item is already appended to
511 // internal data structures)
512 pos = GetMenuItemCount() - 1;
cb9eed05
VZ
513 }
514
89511b42
VZ
515 // Update radio groups data if we're inserting a new radio item.
516 //
517 // NB: If we supported inserting non-radio items in the middle of existing
518 // radio groups to break them into two subgroups, we'd need to update
519 // m_radioData in this case too but currently this is not supported.
520 bool checkInitially = false;
521 if ( pItem->GetKind() == wxITEM_RADIO )
522 {
523 if ( !m_radioData )
524 m_radioData = new wxMenuRadioItemsData;
525
526 if ( m_radioData->UpdateOnInsert(pos) )
527 checkInitially = true;
528 }
529
41628a43
VZ
530 // adjust position to account for the title of a popup menu, if any
531 if ( !GetMenuBar() && !m_title.empty() )
5c5871a4
VZ
532 pos += 2; // for the title itself and its separator
533
cb9eed05 534 BOOL ok = false;
3633deed 535
99a7bebb 536#if wxUSE_OWNER_DRAWN
a5d85ef0
VZ
537 // Under older systems mixing owner-drawn and non-owner-drawn items results
538 // in inconsistent margins, so we force this one to be owner-drawn if any
d08504df 539 // other items already are.
aa4919ed 540 if ( m_ownerDrawn )
d08504df 541 pItem->SetOwnerDrawn(true);
a5d85ef0 542#endif // wxUSE_OWNER_DRAWN
2bda0e17 543
cb9eed05 544 // check if we have something more than a simple text item
47d67540 545#if wxUSE_OWNER_DRAWN
cb9eed05
VZ
546 if ( pItem->IsOwnerDrawn() )
547 {
f4322df6 548#ifndef __DMC__
37ddd6ea 549
aa4919ed 550 if ( !m_ownerDrawn && !pItem->IsSeparator() )
37ddd6ea 551 {
d08504df
VZ
552 // MIIM_BITMAP only works under WinME/2000+ so we always use owner
553 // drawn item under the previous versions and we also have to use
554 // them in any case if the item has custom colours or font
555 static const wxWinVersion winver = wxGetWinVersion();
556 bool mustUseOwnerDrawn = winver < wxWinVersion_98 ||
a1b806b9
DS
557 pItem->GetTextColour().IsOk() ||
558 pItem->GetBackgroundColour().IsOk() ||
559 pItem->GetFont().IsOk();
d08504df
VZ
560
561 if ( !mustUseOwnerDrawn )
f4322df6 562 {
d08504df
VZ
563 const wxBitmap& bmpUnchecked = pItem->GetBitmap(false),
564 bmpChecked = pItem->GetBitmap(true);
565
a1b806b9
DS
566 if ( (bmpUnchecked.IsOk() && IsGreaterThanStdSize(bmpUnchecked)) ||
567 (bmpChecked.IsOk() && IsGreaterThanStdSize(bmpChecked)) )
d08504df
VZ
568 {
569 mustUseOwnerDrawn = true;
570 }
37ddd6ea 571 }
d08504df
VZ
572
573 // use InsertMenuItem() if possible as it's guaranteed to look
574 // correct while our owner-drawn code is not
575 if ( !mustUseOwnerDrawn )
4e7c767f 576 {
d08504df
VZ
577 WinStruct<MENUITEMINFO> mii;
578 mii.fMask = MIIM_STRING | MIIM_DATA;
579
580 // don't set hbmpItem for the checkable items as it would
581 // be used for both checked and unchecked state
582 if ( pItem->IsCheckable() )
583 {
584 mii.fMask |= MIIM_CHECKMARKS;
585 mii.hbmpChecked = GetHBitmapForMenu(pItem, true);
586 mii.hbmpUnchecked = GetHBitmapForMenu(pItem, false);
587 }
588 else if ( pItem->GetBitmap().IsOk() )
589 {
590 mii.fMask |= MIIM_BITMAP;
591 mii.hbmpItem = GetHBitmapForMenu(pItem);
592 }
9c80e160 593
d08504df 594 mii.cch = itemText.length();
017dc06b 595 mii.dwTypeData = wxMSW_CONV_LPTSTR(itemText);
cb9eed05 596
d08504df
VZ
597 if ( flags & MF_POPUP )
598 {
599 mii.fMask |= MIIM_SUBMENU;
600 mii.hSubMenu = GetHmenuOf(pItem->GetSubMenu());
601 }
602 else
603 {
604 mii.fMask |= MIIM_ID;
605 mii.wID = id;
606 }
2919a8b5 607
d08504df 608 mii.dwItemData = reinterpret_cast<ULONG_PTR>(pItem);
cb9eed05 609
d08504df
VZ
610 ok = ::InsertMenuItem(GetHmenu(), pos, TRUE /* by pos */, &mii);
611 if ( !ok )
612 {
613 wxLogLastError(wxT("InsertMenuItem()"));
614 }
615 else // InsertMenuItem() ok
cb9eed05 616 {
d08504df
VZ
617 // we need to remove the extra indent which is reserved for
618 // the checkboxes by default as it looks ugly unless check
619 // boxes are used together with bitmaps and this is not the
620 // case in wx API
621 WinStruct<MENUINFO> mi;
622
623 // don't call SetMenuInfo() directly, this would prevent
624 // the app from starting up under Windows 95/NT 4
625 typedef BOOL (WINAPI *SetMenuInfo_t)(HMENU, MENUINFO *);
626
627 wxDynamicLibrary dllUser(wxT("user32"));
628 wxDYNLIB_FUNCTION(SetMenuInfo_t, SetMenuInfo, dllUser);
629 if ( pfnSetMenuInfo )
43b2d5e7 630 {
d08504df
VZ
631 mi.fMask = MIM_STYLE;
632 mi.dwStyle = MNS_CHECKORBMP;
633 if ( !(*pfnSetMenuInfo)(GetHmenu(), &mi) )
634 {
635 wxLogLastError(wxT("SetMenuInfo(MNS_NOCHECK)"));
636 }
43b2d5e7 637 }
37ddd6ea 638
d08504df
VZ
639 // tell the item that it's not really owner-drawn but only
640 // needs to draw its bitmap, the rest is done by Windows
641 pItem->SetOwnerDrawn(false);
642 }
cb9eed05 643 }
cb9eed05 644 }
37ddd6ea 645#endif // __DMC__
cb9eed05
VZ
646
647 if ( !ok )
648 {
649 // item draws itself, pass pointer to it in data parameter
650 flags |= MF_OWNERDRAW;
651 pData = (LPCTSTR)pItem;
d08504df
VZ
652
653 bool updateAllMargins = false;
654
655 // get size of bitmap always return valid value (0 for invalid bitmap),
656 // so we don't needed check if bitmap is valid ;)
657 int uncheckedW = pItem->GetBitmap(false).GetWidth();
658 int checkedW = pItem->GetBitmap(true).GetWidth();
659
660 if ( m_maxBitmapWidth < uncheckedW )
661 {
662 m_maxBitmapWidth = uncheckedW;
663 updateAllMargins = true;
664 }
665
666 if ( m_maxBitmapWidth < checkedW )
667 {
668 m_maxBitmapWidth = checkedW;
669 updateAllMargins = true;
670 }
671
672 // make other item ownerdrawn and update margin width for equals alignment
673 if ( !m_ownerDrawn || updateAllMargins )
674 {
aa4919ed
VZ
675 // we must use position in SetOwnerDrawnMenuItem because
676 // all separators have the same id
677 int pos = 0;
d08504df
VZ
678 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
679 while (node)
680 {
681 wxMenuItem* item = node->GetData();
682
aa4919ed 683 if ( !item->IsOwnerDrawn())
d08504df 684 {
aa4919ed
VZ
685 item->SetOwnerDrawn(true);
686 SetOwnerDrawnMenuItem(GetHmenu(), pos,
687 reinterpret_cast<ULONG_PTR>(item), TRUE);
d08504df
VZ
688 }
689
aa4919ed
VZ
690 item->SetMarginWidth(m_maxBitmapWidth);
691
d08504df 692 node = node->GetNext();
aa4919ed 693 pos++;
d08504df
VZ
694 }
695
696 // set menu as ownerdrawn
697 m_ownerDrawn = true;
9c32ed26
VZ
698
699 ResetMaxAccelWidth();
d08504df
VZ
700 }
701 // only update our margin for equals alignment to other item
702 else if ( !updateAllMargins )
703 {
704 pItem->SetMarginWidth(m_maxBitmapWidth);
705 }
cb9eed05 706 }
c626a8b7
VZ
707 }
708 else
cb9eed05 709#endif // wxUSE_OWNER_DRAWN
c626a8b7 710 {
6a17b868 711 // item is just a normal string (passed in data parameter)
c626a8b7 712 flags |= MF_STRING;
8fb3a512 713
39d2f9a7 714#ifdef __WXWINCE__
52af3158 715 itemText = wxMenuItem::GetLabelText(itemText);
39d2f9a7 716#endif
2bda0e17 717
017dc06b 718 pData = itemText.t_str();
717a57c2
VZ
719 }
720
6a17b868 721 // item might have already been inserted by InsertMenuItem() above
717a57c2 722 if ( !ok )
c626a8b7 723 {
cb9eed05
VZ
724 if ( !::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData) )
725 {
726 wxLogLastError(wxT("InsertMenu[Item]()"));
717a57c2 727
cb9eed05
VZ
728 return false;
729 }
c626a8b7 730 }
42e69d6b 731
cb9eed05 732
89511b42
VZ
733 // Check the item if it should be initially checked.
734 if ( checkInitially )
735 pItem->Check(true);
736
0472ece7 737 // if we just appended the title, highlight it
008809c7 738 if ( id == (UINT_PTR)idMenuTitle )
0472ece7
VZ
739 {
740 // visually select the menu title
741 SetDefaultMenuItem(GetHmenu(), id);
742 }
42e69d6b 743
0472ece7 744 // if we're already attached to the menubar, we must update it
4224f059 745 if ( IsAttached() && GetMenuBar()->IsAttached() )
0472ece7 746 {
4224f059 747 GetMenuBar()->Refresh();
0472ece7
VZ
748 }
749
598ddd96 750 return true;
0472ece7
VZ
751}
752
9add9367 753wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
2bda0e17 754{
89511b42 755 return wxMenuBase::DoAppend(item) && DoInsertOrAppend(item) ? item : NULL;
2bda0e17
KB
756}
757
9add9367 758wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
2bda0e17 759{
9add9367
RD
760 if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos))
761 return item;
762 else
763 return NULL;
2bda0e17
KB
764}
765
717a57c2 766wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
2bda0e17 767{
6a17b868 768 // we need to find the item's position in the child list
717a57c2 769 size_t pos;
222ed1d6 770 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
717a57c2 771 for ( pos = 0; node; pos++ )
c626a8b7 772 {
717a57c2 773 if ( node->GetData() == item )
c626a8b7 774 break;
717a57c2
VZ
775
776 node = node->GetNext();
c626a8b7
VZ
777 }
778
6a17b868 779 // DoRemove() (unlike Remove) can only be called for an existing item!
717a57c2 780 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
c626a8b7 781
717a57c2
VZ
782#if wxUSE_ACCEL
783 // remove the corresponding accel from the accel table
784 int n = FindAccel(item->GetId());
785 if ( n != wxNOT_FOUND )
786 {
787 delete m_accels[n];
c626a8b7 788
b54e41c5 789 m_accels.RemoveAt(n);
9c32ed26 790
af36ec0a 791#if wxUSE_OWNER_DRAWN
9c32ed26 792 ResetMaxAccelWidth();
51c2f7ea 793#endif
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,
017dc06b 888 (UINT_PTR)idMenuTitle, m_title.t_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 909#ifdef __WXWINCE__
be85a191 910 WinStruct<MENUITEMINFO> info;
4676948b
JS
911 info.fMask = MIIM_TYPE;
912 info.fType = MFT_STRING;
7ec69821 913 info.cch = m_title.length();
017dc06b 914 info.dwTypeData = wxMSW_CONV_LPTSTR(m_title);
4676948b
JS
915 if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) )
916 {
917 wxLogLastError(wxT("SetMenuItemInfo"));
918 }
919#else
c626a8b7 920 if ( !ModifyMenu(hMenu, 0u,
717a57c2 921 MF_BYPOSITION | MF_STRING,
017dc06b 922 (UINT_PTR)idMenuTitle, m_title.t_str()) )
c626a8b7 923 {
f6bcfd97 924 wxLogLastError(wxT("ModifyMenu"));
c626a8b7 925 }
4676948b 926#endif
c626a8b7 927 }
b8d3a4f1 928 }
b8d3a4f1 929
42e69d6b 930#ifdef __WIN32__
c626a8b7 931 // put the title string in bold face
ed7fdb86 932 if ( !m_title.empty() )
a3f4e9e8 933 {
d34791c2 934 SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle);
a3f4e9e8 935 }
717a57c2 936#endif // Win32
2bda0e17
KB
937}
938
c2dcfdef
VZ
939// ---------------------------------------------------------------------------
940// event processing
941// ---------------------------------------------------------------------------
2bda0e17 942
0edeeb6d 943bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id_)
2bda0e17 944{
0edeeb6d
VZ
945 const int id = (signed short)id_;
946
a3f4e9e8 947 // ignore commands from the menu title
008809c7 948 if ( id != idMenuTitle )
a3f4e9e8 949 {
e23e368b
VZ
950 // Default value for uncheckable items.
951 int checked = -1;
952
18138662
VZ
953 // update the check item when it's clicked
954 wxMenuItem * const item = FindItem(id);
955 if ( item && item->IsCheckable() )
e23e368b 956 {
18138662
VZ
957 item->Toggle();
958
e23e368b
VZ
959 // Get the status of the menu item: note that it has been just changed
960 // by Toggle() above so here we already get the new state of the item.
961 //
962 // Also notice that we must pass unsigned id_ and not sign-extended id
963 // to ::GetMenuState() as this is what it expects.
964 UINT menuState = ::GetMenuState(GetHmenu(), id_, MF_BYCOMMAND);
965 checked = (menuState & MF_CHECKED) != 0;
966 }
967
968 SendEvent(id, checked);
a3f4e9e8
VZ
969 }
970
598ddd96 971 return true;
2bda0e17
KB
972}
973
a99a3029 974// get the menu with given handle (recursively)
af36ec0a 975#if wxUSE_OWNER_DRAWN
a99a3029
VZ
976wxMenu* wxMenu::MSWGetMenu(WXHMENU hMenu)
977{
978 // check self
979 if ( GetHMenu() == hMenu )
980 return this;
981
982 // recursively query submenus
983 for ( size_t n = 0 ; n < GetMenuItemCount(); ++n )
984 {
985 wxMenuItem* item = FindItemByPosition(n);
986 wxMenu* submenu = item->GetSubMenu();
987 if ( submenu )
988 {
989 submenu = submenu->MSWGetMenu(hMenu);
990 if (submenu)
991 return submenu;
992 }
993 }
994
995 // unknown hMenu
996 return NULL;
997}
af36ec0a 998#endif // wxUSE_OWNER_DRAWN
a99a3029 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));
017dc06b 1130 tbButton.iString = (int) wxMSW_CONV_LPCTSTR(label);
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(),
017dc06b 1159 (*it)->GetTitle().t_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
e4a23857
VZ
1216bool wxMenuBar::IsEnabledTop(size_t pos) const
1217{
1218 wxCHECK_MSG( pos < GetMenuCount(), false, wxS("invalid menu index") );
1219 WinStruct<MENUITEMINFO> mii;
1220 mii.fMask = MIIM_STATE;
1221 if ( !::GetMenuItemInfo(GetHmenu(), pos, TRUE, &mii) )
1222 {
1223 wxLogLastError(wxS("GetMenuItemInfo(menubar)"));
1224 }
1225
1226 return !(mii.fState & MFS_GRAYED);
1227}
1228
52af3158 1229void wxMenuBar::SetMenuLabel(size_t pos, const wxString& label)
2bda0e17 1230{
717a57c2
VZ
1231 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
1232
7ee9a64b 1233 m_menus[pos]->wxMenuBase::SetTitle(label);
717a57c2
VZ
1234
1235 if ( !IsAttached() )
1236 {
1237 return;
1238 }
1239 //else: have to modify the existing menu
1240
b2c5f143
DE
1241 int mswpos = MSWPositionForWxMenu(GetMenu(pos),pos);
1242
dca0f651 1243 UINT_PTR id;
b2c5f143 1244 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, mswpos, MF_BYPOSITION);
c2dcfdef 1245 if ( flagsOld == 0xFFFFFFFF )
c626a8b7 1246 {
223d09f6 1247 wxLogLastError(wxT("GetMenuState"));
c2dcfdef
VZ
1248
1249 return;
1250 }
1251
1252 if ( flagsOld & MF_POPUP )
1253 {
1254 // HIBYTE contains the number of items in the submenu in this case
ad9bb75f 1255 flagsOld &= 0xff;
dca0f651 1256 id = (UINT_PTR)::GetSubMenu((HMENU)m_hMenu, mswpos);
c626a8b7
VZ
1257 }
1258 else
8cd85069
VZ
1259 {
1260 id = pos;
1261 }
1262
4676948b 1263#ifdef __WXWINCE__
be85a191 1264 WinStruct<MENUITEMINFO> info;
4676948b
JS
1265 info.fMask = MIIM_TYPE;
1266 info.fType = MFT_STRING;
7ec69821 1267 info.cch = label.length();
017dc06b 1268 info.dwTypeData = wxMSW_CONV_LPTSTR(label);
dca0f651 1269 if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, &info) )
4676948b
JS
1270 {
1271 wxLogLastError(wxT("SetMenuItemInfo"));
1272 }
598ddd96 1273
4676948b 1274#else
b2c5f143 1275 if ( ::ModifyMenu(GetHmenu(), mswpos, MF_BYPOSITION | MF_STRING | flagsOld,
017dc06b 1276 id, label.t_str()) == (int)0xFFFFFFFF )
c2dcfdef 1277 {
f6bcfd97 1278 wxLogLastError(wxT("ModifyMenu"));
c2dcfdef 1279 }
4676948b 1280#endif
717a57c2
VZ
1281
1282 Refresh();
2bda0e17
KB
1283}
1284
52af3158 1285wxString wxMenuBar::GetMenuLabel(size_t pos) const
2bda0e17 1286{
717a57c2 1287 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
52af3158 1288 wxT("invalid menu index in wxMenuBar::GetMenuLabel") );
8cd85069 1289
7ee9a64b 1290 return m_menus[pos]->GetTitle();
2bda0e17
KB
1291}
1292
ad9bb75f
VZ
1293// ---------------------------------------------------------------------------
1294// wxMenuBar construction
1295// ---------------------------------------------------------------------------
1296
a8cfd0cb 1297wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 1298{
ad9bb75f
VZ
1299 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
1300 if ( !menuOld )
f7f50f49
VZ
1301 return NULL;
1302
7ee9a64b 1303 menu->wxMenuBase::SetTitle(title);
a8cfd0cb 1304
7e02be85
JS
1305#if defined(WINCE_WITHOUT_COMMANDBAR)
1306 if (IsAttached())
1307#else
1308 if (GetHmenu())
1309#endif
a8cfd0cb 1310 {
b2c5f143
DE
1311 int mswpos = MSWPositionForWxMenu(menuOld,pos);
1312
ad9bb75f 1313 // can't use ModifyMenu() because it deletes the submenu it replaces
b2c5f143 1314 if ( !::RemoveMenu(GetHmenu(), (UINT)mswpos, MF_BYPOSITION) )
a8cfd0cb 1315 {
f6bcfd97 1316 wxLogLastError(wxT("RemoveMenu"));
a8cfd0cb 1317 }
1cf27c63 1318
b2c5f143 1319 if ( !::InsertMenu(GetHmenu(), (UINT)mswpos,
ad9bb75f 1320 MF_BYPOSITION | MF_POPUP | MF_STRING,
017dc06b 1321 (UINT_PTR)GetHmenuOf(menu), title.t_str()) )
ad9bb75f 1322 {
f6bcfd97 1323 wxLogLastError(wxT("InsertMenu"));
ad9bb75f
VZ
1324 }
1325
717a57c2
VZ
1326#if wxUSE_ACCEL
1327 if ( menuOld->HasAccels() || menu->HasAccels() )
1328 {
1329 // need to rebuild accell table
1330 RebuildAccelTable();
1331 }
1332#endif // wxUSE_ACCEL
1333
7e02be85
JS
1334 if (IsAttached())
1335 Refresh();
a8cfd0cb 1336 }
ad9bb75f
VZ
1337
1338 return menuOld;
1cf27c63
UM
1339}
1340
a8cfd0cb 1341bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 1342{
b2c5f143
DE
1343 // Find out which MSW item before which we'll be inserting before
1344 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
3d27b574 1345 // If IsAttached() is false this won't be used anyway
7e02be85
JS
1346 bool isAttached =
1347#if defined(WINCE_WITHOUT_COMMANDBAR)
1348 IsAttached();
1349#else
1350 (GetHmenu() != 0);
1351#endif
1352
1353 int mswpos = (!isAttached || (pos == m_menus.GetCount()))
b2c5f143
DE
1354 ? -1 // append the menu
1355 : MSWPositionForWxMenu(GetMenu(pos),pos);
1356
ad9bb75f 1357 if ( !wxMenuBarBase::Insert(pos, menu, title) )
598ddd96 1358 return false;
1cf27c63 1359
7ee9a64b 1360 menu->wxMenuBase::SetTitle(title);
ad9bb75f 1361
7e02be85 1362 if ( isAttached )
ad9bb75f 1363 {
7e02be85 1364#if defined(WINCE_WITHOUT_COMMANDBAR)
39d2f9a7 1365 if (!GetToolBar())
598ddd96
WS
1366 return false;
1367 TBBUTTON tbButton;
39d2f9a7
JS
1368 memset(&tbButton, 0, sizeof(TBBUTTON));
1369 tbButton.iBitmap = I_IMAGENONE;
1370 tbButton.fsState = TBSTATE_ENABLED;
1371 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
598ddd96 1372
39d2f9a7
JS
1373 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1374 tbButton.dwData = (DWORD)hPopupMenu;
1375 wxString label = wxStripMenuCodes(title);
017dc06b 1376 tbButton.iString = (int) wxMSW_CONV_LPCTSTR(label);
598ddd96 1377
39d2f9a7
JS
1378 tbButton.idCommand = NewControlId();
1379 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1380 {
1381 wxLogLastError(wxT("TB_INSERTBUTTON"));
598ddd96 1382 return false;
39d2f9a7 1383 }
b9a958e6 1384 wxUnusedVar(mswpos);
39d2f9a7 1385#else
b2c5f143 1386 if ( !::InsertMenu(GetHmenu(), mswpos,
ad9bb75f 1387 MF_BYPOSITION | MF_POPUP | MF_STRING,
017dc06b 1388 (UINT_PTR)GetHmenuOf(menu), title.t_str()) )
ad9bb75f 1389 {
f6bcfd97 1390 wxLogLastError(wxT("InsertMenu"));
ad9bb75f 1391 }
39d2f9a7 1392#endif
717a57c2
VZ
1393#if wxUSE_ACCEL
1394 if ( menu->HasAccels() )
1395 {
1396 // need to rebuild accell table
1397 RebuildAccelTable();
1398 }
1399#endif // wxUSE_ACCEL
1400
7e02be85
JS
1401 if (IsAttached())
1402 Refresh();
a8cfd0cb 1403 }
ad9bb75f 1404
598ddd96 1405 return true;
1cf27c63
UM
1406}
1407
ad9bb75f 1408bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
2bda0e17 1409{
ad9bb75f 1410 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
598ddd96 1411 wxCHECK_MSG( submenu, false, wxT("can't append invalid menu to menubar") );
2bda0e17 1412
717a57c2 1413 if ( !wxMenuBarBase::Append(menu, title) )
598ddd96 1414 return false;
717a57c2 1415
7ee9a64b 1416 menu->wxMenuBase::SetTitle(title);
717a57c2 1417
7e02be85
JS
1418#if defined(WINCE_WITHOUT_COMMANDBAR)
1419 if (IsAttached())
1420#else
1421 if (GetHmenu())
1422#endif
ad9bb75f 1423 {
7e02be85 1424#if defined(WINCE_WITHOUT_COMMANDBAR)
39d2f9a7 1425 if (!GetToolBar())
598ddd96
WS
1426 return false;
1427 TBBUTTON tbButton;
39d2f9a7
JS
1428 memset(&tbButton, 0, sizeof(TBBUTTON));
1429 tbButton.iBitmap = I_IMAGENONE;
1430 tbButton.fsState = TBSTATE_ENABLED;
1431 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
598ddd96 1432
39d2f9a7
JS
1433 size_t pos = GetMenuCount();
1434 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1435 tbButton.dwData = (DWORD)hPopupMenu;
1436 wxString label = wxStripMenuCodes(title);
017dc06b 1437 tbButton.iString = (int) wxMSW_CONV_LPCTSTR(label);
598ddd96 1438
39d2f9a7
JS
1439 tbButton.idCommand = NewControlId();
1440 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1441 {
1442 wxLogLastError(wxT("TB_INSERTBUTTON"));
598ddd96 1443 return false;
39d2f9a7
JS
1444 }
1445#else
ad9bb75f 1446 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
017dc06b 1447 (UINT_PTR)submenu, title.t_str()) )
ad9bb75f
VZ
1448 {
1449 wxLogLastError(wxT("AppendMenu"));
1450 }
39d2f9a7 1451#endif
ad9bb75f 1452
717a57c2
VZ
1453#if wxUSE_ACCEL
1454 if ( menu->HasAccels() )
1455 {
39d2f9a7 1456 // need to rebuild accelerator table
717a57c2
VZ
1457 RebuildAccelTable();
1458 }
1459#endif // wxUSE_ACCEL
1460
7e02be85
JS
1461 if (IsAttached())
1462 Refresh();
ad9bb75f 1463 }
2bda0e17 1464
598ddd96 1465 return true;
2bda0e17
KB
1466}
1467
a8cfd0cb 1468wxMenu *wxMenuBar::Remove(size_t pos)
2bda0e17 1469{
a8cfd0cb
VZ
1470 wxMenu *menu = wxMenuBarBase::Remove(pos);
1471 if ( !menu )
1472 return NULL;
c626a8b7 1473
7e02be85
JS
1474#if defined(WINCE_WITHOUT_COMMANDBAR)
1475 if (IsAttached())
1476#else
1477 if (GetHmenu())
1478#endif
ad9bb75f 1479 {
7e02be85 1480#if defined(WINCE_WITHOUT_COMMANDBAR)
39d2f9a7
JS
1481 if (GetToolBar())
1482 {
1483 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0))
1484 {
1485 wxLogLastError(wxT("TB_DELETEBUTTON"));
1486 }
1487 }
1488#else
b2c5f143 1489 if ( !::RemoveMenu(GetHmenu(), (UINT)MSWPositionForWxMenu(menu,pos), MF_BYPOSITION) )
ad9bb75f 1490 {
f6bcfd97 1491 wxLogLastError(wxT("RemoveMenu"));
ad9bb75f 1492 }
39d2f9a7 1493#endif
c4053ed3 1494
717a57c2
VZ
1495#if wxUSE_ACCEL
1496 if ( menu->HasAccels() )
1497 {
1498 // need to rebuild accell table
1499 RebuildAccelTable();
1500 }
1501#endif // wxUSE_ACCEL
1502
7e02be85
JS
1503 if (IsAttached())
1504 Refresh();
ad9bb75f 1505 }
2bda0e17 1506
a8cfd0cb 1507 return menu;
2bda0e17
KB
1508}
1509
d427503c 1510#if wxUSE_ACCEL
717a57c2
VZ
1511
1512void wxMenuBar::RebuildAccelTable()
1513{
1514 // merge the accelerators of all menus into one accel table
42e69d6b 1515 size_t nAccelCount = 0;
a8cfd0cb 1516 size_t i, count = GetMenuCount();
222ed1d6
MB
1517 wxMenuList::iterator it;
1518 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
42e69d6b 1519 {
222ed1d6 1520 nAccelCount += (*it)->GetAccelCount();
42e69d6b
VZ
1521 }
1522
5df1250b 1523 if ( nAccelCount )
42e69d6b 1524 {
5df1250b 1525 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
42e69d6b 1526
5df1250b 1527 nAccelCount = 0;
222ed1d6 1528 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
5df1250b 1529 {
222ed1d6 1530 nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]);
5df1250b
VZ
1531 }
1532
7802da36 1533 SetAcceleratorTable(wxAcceleratorTable(nAccelCount, accelEntries));
42e69d6b 1534
5df1250b
VZ
1535 delete [] accelEntries;
1536 }
717a57c2
VZ
1537}
1538
1539#endif // wxUSE_ACCEL
1540
1541void wxMenuBar::Attach(wxFrame *frame)
1542{
1e6feb95 1543 wxMenuBarBase::Attach(frame);
717a57c2 1544
3fd239fa 1545#if defined(WINCE_WITH_COMMANDBAR)
a96b4743
JS
1546 if (!m_hMenu)
1547 this->Create();
a96b4743
JS
1548 if (!m_commandBar)
1549 m_commandBar = (WXHWND) CommandBar_Create(wxGetInstance(), (HWND) frame->GetHWND(), NewControlId());
1550 if (m_commandBar)
1551 {
1552 if (m_hMenu)
1553 {
1554 if (!CommandBar_InsertMenubarEx((HWND) m_commandBar, NULL, (LPTSTR) m_hMenu, 0))
1555 {
1556 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1557 }
1558 }
1559 }
1560#endif
a96b4743 1561
717a57c2
VZ
1562#if wxUSE_ACCEL
1563 RebuildAccelTable();
d427503c 1564#endif // wxUSE_ACCEL
42e69d6b
VZ
1565}
1566
3fd239fa 1567#if defined(WINCE_WITH_COMMANDBAR)
a9928e9d
JS
1568bool wxMenuBar::AddAdornments(long style)
1569{
1570 if (m_adornmentsAdded || !m_commandBar)
1571 return false;
1572
1573 if (style & wxCLOSE_BOX)
1574 {
1575 if (!CommandBar_AddAdornments((HWND) m_commandBar, 0, 0))
43b2d5e7 1576 {
a9928e9d 1577 wxLogLastError(wxT("CommandBar_AddAdornments"));
43b2d5e7 1578 }
a9928e9d 1579 else
43b2d5e7 1580 {
a9928e9d 1581 return true;
43b2d5e7 1582 }
a9928e9d
JS
1583 }
1584 return false;
1585}
1586#endif
1587
1cf27c63
UM
1588void wxMenuBar::Detach()
1589{
1e6feb95 1590 wxMenuBarBase::Detach();
2bda0e17
KB
1591}
1592
a99a3029
VZ
1593// get the menu with given handle (recursively)
1594wxMenu* wxMenuBar::MSWGetMenu(WXHMENU hMenu)
1595{
1596 wxCHECK_MSG( GetHMenu() != hMenu, NULL,
1597 wxT("wxMenuBar::MSWGetMenu(): menu handle is wxMenuBar, not wxMenu") );
1598
af36ec0a 1599#if wxUSE_OWNER_DRAWN
a99a3029
VZ
1600 // query all menus
1601 for ( size_t n = 0 ; n < GetMenuCount(); ++n )
1602 {
1603 wxMenu* menu = GetMenu(n)->MSWGetMenu(hMenu);
1604 if ( menu )
1605 return menu;
1606 }
51c2f7ea 1607#endif
a99a3029
VZ
1608
1609 // unknown hMenu
1610 return NULL;
1611}
1612
1e6feb95 1613#endif // wxUSE_MENUS