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