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