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