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