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