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