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