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