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