]> git.saurik.com Git - wxWidgets.git/blame - src/msw/menu.cpp
mention ShouldInheritColours() change
[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
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
c2dcfdef
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c626a8b7 21 #pragma implementation "menu.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
c626a8b7 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_MENUS
32
2bda0e17 33#ifndef WX_PRECOMP
c626a8b7
VZ
34 #include "wx/frame.h"
35 #include "wx/menu.h"
36 #include "wx/utils.h"
0c589ad0 37 #include "wx/intl.h"
717a57c2 38 #include "wx/log.h"
2bda0e17
KB
39#endif
40
47d67540 41#if wxUSE_OWNER_DRAWN
c626a8b7 42 #include "wx/ownerdrw.h"
2bda0e17
KB
43#endif
44
45#include "wx/msw/private.h"
2bda0e17 46
39d2f9a7
JS
47#ifdef __WXWINCE__
48#include <windows.h>
49#include <windowsx.h>
50#include <tchar.h>
51#include <ole2.h>
52#include <commctrl.h>
53#include <aygshell.h>
54
55#ifndef TBSTYLE_NO_DROPDOWN_ARROW
56#define TBSTYLE_NO_DROPDOWN_ARROW 0x0080
57#endif
58
59#endif
60
2bda0e17 61// other standard headers
2bda0e17
KB
62#include <string.h>
63
c626a8b7
VZ
64// ----------------------------------------------------------------------------
65// global variables
66// ----------------------------------------------------------------------------
67
68extern wxMenu *wxCurrentPopupMenu;
69
b8d3a4f1
VZ
70// ----------------------------------------------------------------------------
71// constants
72// ----------------------------------------------------------------------------
73
74// the (popup) menu title has this special id
75static const int idMenuTitle = -2;
76
77// ----------------------------------------------------------------------------
0472ece7 78// private functions
b8d3a4f1 79// ----------------------------------------------------------------------------
c626a8b7 80
0472ece7
VZ
81// make the given menu item default
82static void SetDefaultMenuItem(HMENU hmenu, UINT id)
83{
4676948b 84#ifndef __WXWINCE__
0472ece7
VZ
85 MENUITEMINFO mii;
86 wxZeroMemory(mii);
87 mii.cbSize = sizeof(MENUITEMINFO);
88 mii.fMask = MIIM_STATE;
89 mii.fState = MFS_DEFAULT;
90
91 if ( !::SetMenuItemInfo(hmenu, id, FALSE, &mii) )
92 {
93 wxLogLastError(wxT("SetMenuItemInfo"));
94 }
4676948b
JS
95#endif
96}
97
98#ifdef __WXWINCE__
99UINT GetMenuState(HMENU hMenu, UINT id, UINT flags)
100{
101 MENUITEMINFO info;
102 wxZeroMemory(info);
103 info.cbSize = sizeof(info);
104 info.fMask = MIIM_STATE;
105 if ( !GetMenuItemInfo(hMenu, id, flags & MF_BYCOMMAND ? FALSE : TRUE, & info) )
106 wxLogLastError(wxT("GetMenuItemInfo"));
107 return info.fState;
0472ece7 108}
4676948b 109#endif
2bda0e17
KB
110
111// ============================================================================
112// implementation
113// ============================================================================
114
e70b4f10
SC
115#include <wx/listimpl.cpp>
116
117WX_DEFINE_LIST( wxMenuInfoList ) ;
118
119#if wxUSE_EXTENDED_RTTI
120
121WX_DEFINE_FLAGS( wxMenuStyle )
122
3ff066a4
SC
123wxBEGIN_FLAGS( wxMenuStyle )
124 wxFLAGS_MEMBER(wxMENU_TEAROFF)
125wxEND_FLAGS( wxMenuStyle )
e70b4f10
SC
126
127IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu, wxEvtHandler,"wx/menu.h")
128
3ff066a4 129wxCOLLECTION_TYPE_INFO( wxMenuItem * , wxMenuItemList ) ;
e70b4f10
SC
130
131template<> void wxCollectionToVariantArray( wxMenuItemList const &theList, wxxVariantArray &value)
132{
133 wxListCollectionToVariantArray<wxMenuItemList::compatibility_iterator>( theList , value ) ;
134}
135
3ff066a4
SC
136wxBEGIN_PROPERTIES_TABLE(wxMenu)
137 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_MENU_SELECTED , wxCommandEvent)
138 wxPROPERTY( Title, wxString , SetTitle, GetTitle, wxString(), 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
139 wxREADONLY_PROPERTY_FLAGS( MenuStyle , wxMenuStyle , long , GetStyle , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
140 wxPROPERTY_COLLECTION( MenuItems , wxMenuItemList , wxMenuItem* , Append , GetMenuItems , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
141wxEND_PROPERTIES_TABLE()
e70b4f10 142
3ff066a4
SC
143wxBEGIN_HANDLERS_TABLE(wxMenu)
144wxEND_HANDLERS_TABLE()
e70b4f10 145
3ff066a4 146wxDIRECT_CONSTRUCTOR_2( wxMenu , wxString , Title , long , MenuStyle )
e70b4f10
SC
147
148WX_DEFINE_FLAGS( wxMenuBarStyle )
149
3ff066a4
SC
150wxBEGIN_FLAGS( wxMenuBarStyle )
151 wxFLAGS_MEMBER(wxMB_DOCKABLE)
152wxEND_FLAGS( wxMenuBarStyle )
e70b4f10
SC
153
154// the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
2341cf5f 155bool wxMenuBarStreamingCallback( const wxObject *WXUNUSED(object), wxWriter * , wxPersister * , wxxVariantArray & )
e70b4f10
SC
156{
157 return true ;
158}
159
160IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxMenuBar, wxWindow ,"wx/menu.h",wxMenuBarStreamingCallback)
161
162IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenuInfo, wxObject , "wx/menu.h" )
163
3ff066a4
SC
164wxBEGIN_PROPERTIES_TABLE(wxMenuInfo)
165 wxREADONLY_PROPERTY( Menu , wxMenu* , GetMenu , , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
166 wxREADONLY_PROPERTY( Title , wxString , GetTitle , wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
167wxEND_PROPERTIES_TABLE()
e70b4f10 168
3ff066a4
SC
169wxBEGIN_HANDLERS_TABLE(wxMenuInfo)
170wxEND_HANDLERS_TABLE()
e70b4f10 171
3ff066a4 172wxCONSTRUCTOR_2( wxMenuInfo , wxMenu* , Menu , wxString , Title )
e70b4f10 173
3ff066a4 174wxCOLLECTION_TYPE_INFO( wxMenuInfo * , wxMenuInfoList ) ;
e70b4f10
SC
175
176template<> void wxCollectionToVariantArray( wxMenuInfoList const &theList, wxxVariantArray &value)
177{
178 wxListCollectionToVariantArray<wxMenuInfoList::compatibility_iterator>( theList , value ) ;
179}
180
3ff066a4
SC
181wxBEGIN_PROPERTIES_TABLE(wxMenuBar)
182 wxPROPERTY_COLLECTION( MenuInfos , wxMenuInfoList , wxMenuInfo* , Append , GetMenuInfos , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
183wxEND_PROPERTIES_TABLE()
e70b4f10 184
3ff066a4
SC
185wxBEGIN_HANDLERS_TABLE(wxMenuBar)
186wxEND_HANDLERS_TABLE()
e70b4f10 187
3ff066a4 188wxCONSTRUCTOR_DUMMY( wxMenuBar )
e70b4f10
SC
189
190#else
0472ece7
VZ
191IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
192IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow)
e70b4f10
SC
193IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo, wxObject)
194#endif
0472ece7 195
e70b4f10
SC
196const wxMenuInfoList& wxMenuBar::GetMenuInfos() const
197{
198 wxMenuInfoList* list = const_cast< wxMenuInfoList* >( &m_menuInfos ) ;
199 WX_CLEAR_LIST( wxMenuInfoList , *list ) ;
200 for( size_t i = 0 ; i < GetMenuCount() ; ++i )
201 {
202 wxMenuInfo* info = new wxMenuInfo() ;
203 info->Create( const_cast<wxMenuBar*>(this)->GetMenu(i) , GetLabelTop(i) ) ;
204 list->Append( info ) ;
205 }
206 return m_menuInfos ;
207}
066f1b7a 208
c2dcfdef
VZ
209// ---------------------------------------------------------------------------
210// wxMenu construction, adding and removing menu items
211// ---------------------------------------------------------------------------
2bda0e17
KB
212
213// Construct a menu with optional title (then use append)
717a57c2 214void wxMenu::Init()
c626a8b7 215{
ad9bb75f 216 m_doBreak = FALSE;
0472ece7 217 m_startRadioGroup = -1;
c626a8b7 218
717a57c2
VZ
219 // create the menu
220 m_hMenu = (WXHMENU)CreatePopupMenu();
221 if ( !m_hMenu )
222 {
f6bcfd97 223 wxLogLastError(wxT("CreatePopupMenu"));
717a57c2
VZ
224 }
225
226 // if we have a title, insert it in the beginning of the menu
c626a8b7
VZ
227 if ( !!m_title )
228 {
ad9bb75f
VZ
229 Append(idMenuTitle, m_title);
230 AppendSeparator();
c626a8b7 231 }
2bda0e17
KB
232}
233
234// The wxWindow destructor will take care of deleting the submenus.
b8d3a4f1 235wxMenu::~wxMenu()
2bda0e17 236{
717a57c2
VZ
237 // we should free Windows resources only if Windows doesn't do it for us
238 // which happens if we're attached to a menubar or a submenu of another
239 // menu
240 if ( !IsAttached() && !GetParent() )
c2dcfdef 241 {
ad9bb75f
VZ
242 if ( !::DestroyMenu(GetHmenu()) )
243 {
f6bcfd97 244 wxLogLastError(wxT("DestroyMenu"));
ad9bb75f 245 }
c2dcfdef 246 }
c626a8b7 247
ad9bb75f
VZ
248#if wxUSE_ACCEL
249 // delete accels
250 WX_CLEAR_ARRAY(m_accels);
251#endif // wxUSE_ACCEL
2bda0e17
KB
252}
253
b8d3a4f1 254void wxMenu::Break()
2bda0e17 255{
717a57c2 256 // this will take effect during the next call to Append()
c2dcfdef 257 m_doBreak = TRUE;
2bda0e17
KB
258}
259
0472ece7
VZ
260void wxMenu::Attach(wxMenuBarBase *menubar)
261{
262 wxMenuBase::Attach(menubar);
263
264 EndRadioGroup();
265}
266
717a57c2
VZ
267#if wxUSE_ACCEL
268
269int wxMenu::FindAccel(int id) const
270{
271 size_t n, count = m_accels.GetCount();
272 for ( n = 0; n < count; n++ )
273 {
274 if ( m_accels[n]->m_command == id )
275 return n;
276 }
277
278 return wxNOT_FOUND;
279}
280
281void wxMenu::UpdateAccel(wxMenuItem *item)
2bda0e17 282{
f6bcfd97 283 if ( item->IsSubMenu() )
717a57c2 284 {
f6bcfd97 285 wxMenu *submenu = item->GetSubMenu();
222ed1d6 286 wxMenuItemList::compatibility_iterator node = submenu->GetMenuItems().GetFirst();
f6bcfd97
BP
287 while ( node )
288 {
289 UpdateAccel(node->GetData());
290
291 node = node->GetNext();
292 }
717a57c2 293 }
f6bcfd97 294 else if ( !item->IsSeparator() )
717a57c2 295 {
f6bcfd97
BP
296 // find the (new) accel for this item
297 wxAcceleratorEntry *accel = wxGetAccelFromString(item->GetText());
717a57c2 298 if ( accel )
f6bcfd97
BP
299 accel->m_command = item->GetId();
300
301 // find the old one
302 int n = FindAccel(item->GetId());
303 if ( n == wxNOT_FOUND )
304 {
305 // no old, add new if any
306 if ( accel )
307 m_accels.Add(accel);
308 else
309 return; // skipping RebuildAccelTable() below
310 }
717a57c2 311 else
f6bcfd97
BP
312 {
313 // replace old with new or just remove the old one if no new
314 delete m_accels[n];
315 if ( accel )
316 m_accels[n] = accel;
317 else
b54e41c5 318 m_accels.RemoveAt(n);
f6bcfd97 319 }
717a57c2 320
f6bcfd97
BP
321 if ( IsAttached() )
322 {
323 m_menuBar->RebuildAccelTable();
324 }
42e69d6b 325 }
f6bcfd97 326 //else: it is a separator, they can't have accels, nothing to do
717a57c2
VZ
327}
328
329#endif // wxUSE_ACCEL
330
331// append a new item or submenu to the menu
332bool wxMenu::DoInsertOrAppend(wxMenuItem *pItem, size_t pos)
333{
334#if wxUSE_ACCEL
335 UpdateAccel(pItem);
d427503c 336#endif // wxUSE_ACCEL
42e69d6b 337
c626a8b7 338 UINT flags = 0;
2bda0e17 339
c2dcfdef
VZ
340 // if "Break" has just been called, insert a menu break before this item
341 // (and don't forget to reset the flag)
c626a8b7
VZ
342 if ( m_doBreak ) {
343 flags |= MF_MENUBREAK;
344 m_doBreak = FALSE;
345 }
346
347 if ( pItem->IsSeparator() ) {
348 flags |= MF_SEPARATOR;
349 }
2bda0e17 350
c2dcfdef
VZ
351 // id is the numeric id for normal menu items and HMENU for submenus as
352 // required by ::AppendMenu() API
c626a8b7 353 UINT id;
c2dcfdef
VZ
354 wxMenu *submenu = pItem->GetSubMenu();
355 if ( submenu != NULL ) {
717a57c2
VZ
356 wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") );
357
358 submenu->SetParent(this);
2bda0e17 359
c2dcfdef 360 id = (UINT)submenu->GetHMenu();
2bda0e17 361
c626a8b7
VZ
362 flags |= MF_POPUP;
363 }
364 else {
365 id = pItem->GetId();
366 }
2bda0e17 367
39d2f9a7
JS
368#ifdef __WXWINCE__
369 wxString strippedString;
370#endif
371
837e5743 372 LPCTSTR pData;
2bda0e17 373
47d67540 374#if wxUSE_OWNER_DRAWN
c626a8b7
VZ
375 if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
376 // item draws itself, pass pointer to it in data parameter
377 flags |= MF_OWNERDRAW;
837e5743 378 pData = (LPCTSTR)pItem;
c626a8b7
VZ
379 }
380 else
2bda0e17 381#endif
c626a8b7
VZ
382 {
383 // menu is just a normal string (passed in data parameter)
384 flags |= MF_STRING;
8fb3a512 385
39d2f9a7
JS
386#ifdef __WXWINCE__
387 strippedString = wxStripMenuCodes(pItem->GetText());
388 pData = (wxChar*)strippedString.c_str();
389#else
f5166ed4 390 pData = (wxChar*)pItem->GetText().c_str();
39d2f9a7 391#endif
c626a8b7 392 }
2bda0e17 393
717a57c2
VZ
394 BOOL ok;
395 if ( pos == (size_t)-1 )
396 {
397 ok = ::AppendMenu(GetHmenu(), flags, id, pData);
398 }
399 else
400 {
401 ok = ::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData);
402 }
403
404 if ( !ok )
c626a8b7 405 {
f6bcfd97 406 wxLogLastError(wxT("Insert or AppendMenu"));
717a57c2
VZ
407
408 return FALSE;
c626a8b7 409 }
42e69d6b 410
0472ece7
VZ
411 // if we just appended the title, highlight it
412#ifdef __WIN32__
413 if ( (int)id == idMenuTitle )
414 {
415 // visually select the menu title
416 SetDefaultMenuItem(GetHmenu(), id);
417 }
42e69d6b
VZ
418#endif // __WIN32__
419
0472ece7
VZ
420 // if we're already attached to the menubar, we must update it
421 if ( IsAttached() && m_menuBar->IsAttached() )
422 {
423 m_menuBar->Refresh();
424 }
425
426 return TRUE;
427}
428
429void wxMenu::EndRadioGroup()
430{
0472ece7
VZ
431 // we're not inside a radio group any longer
432 m_startRadioGroup = -1;
2bda0e17
KB
433}
434
717a57c2 435bool wxMenu::DoAppend(wxMenuItem *item)
2bda0e17 436{
0472ece7
VZ
437 wxCHECK_MSG( item, FALSE, _T("NULL item in wxMenu::DoAppend") );
438
be15b995
VZ
439 bool check = FALSE;
440
546bfbea 441 if ( item->GetKind() == wxITEM_RADIO )
0472ece7 442 {
be15b995
VZ
443 int count = GetMenuItemCount();
444
0472ece7
VZ
445 if ( m_startRadioGroup == -1 )
446 {
447 // start a new radio group
be15b995
VZ
448 m_startRadioGroup = count;
449
450 // for now it has just one element
451 item->SetAsRadioGroupStart();
452 item->SetRadioGroupEnd(m_startRadioGroup);
453
454 // ensure that we have a checked item in the radio group
455 check = TRUE;
456 }
457 else // extend the current radio group
458 {
459 // we need to update its end item
460 item->SetRadioGroupStart(m_startRadioGroup);
222ed1d6 461 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
be15b995
VZ
462
463 if ( node )
464 {
465 node->GetData()->SetRadioGroupEnd(count);
466 }
467 else
468 {
469 wxFAIL_MSG( _T("where is the radio group start item?") );
470 }
0472ece7
VZ
471 }
472 }
473 else // not a radio item
474 {
475 EndRadioGroup();
476 }
477
be15b995
VZ
478 if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) )
479 {
480 return FALSE;
481 }
482
483 if ( check )
484 {
485 // check the item initially
486 item->Check(TRUE);
487 }
488
489 return TRUE;
2bda0e17
KB
490}
491
717a57c2 492bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
2bda0e17 493{
717a57c2 494 return wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos);
2bda0e17
KB
495}
496
717a57c2 497wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
2bda0e17 498{
717a57c2
VZ
499 // we need to find the items position in the child list
500 size_t pos;
222ed1d6 501 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
717a57c2 502 for ( pos = 0; node; pos++ )
c626a8b7 503 {
717a57c2 504 if ( node->GetData() == item )
c626a8b7 505 break;
717a57c2
VZ
506
507 node = node->GetNext();
c626a8b7
VZ
508 }
509
717a57c2
VZ
510 // DoRemove() (unlike Remove) can only be called for existing item!
511 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
c626a8b7 512
717a57c2
VZ
513#if wxUSE_ACCEL
514 // remove the corresponding accel from the accel table
515 int n = FindAccel(item->GetId());
516 if ( n != wxNOT_FOUND )
517 {
518 delete m_accels[n];
c626a8b7 519
b54e41c5 520 m_accels.RemoveAt(n);
c626a8b7 521 }
717a57c2
VZ
522 //else: this item doesn't have an accel, nothing to do
523#endif // wxUSE_ACCEL
524
525 // remove the item from the menu
526 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
527 {
f6bcfd97 528 wxLogLastError(wxT("RemoveMenu"));
c626a8b7
VZ
529 }
530
4bc1afd5 531 if ( IsAttached() && m_menuBar->IsAttached() )
717a57c2
VZ
532 {
533 // otherwise, the chane won't be visible
534 m_menuBar->Refresh();
535 }
2bda0e17 536
717a57c2
VZ
537 // and from internal data structures
538 return wxMenuBase::DoRemove(item);
539}
d427503c 540
42e69d6b
VZ
541// ---------------------------------------------------------------------------
542// accelerator helpers
543// ---------------------------------------------------------------------------
544
717a57c2
VZ
545#if wxUSE_ACCEL
546
42e69d6b
VZ
547// create the wxAcceleratorEntries for our accels and put them into provided
548// array - return the number of accels we have
549size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
550{
551 size_t count = GetAccelCount();
552 for ( size_t n = 0; n < count; n++ )
553 {
974e8d94 554 *accels++ = *m_accels[n];
42e69d6b
VZ
555 }
556
557 return count;
558}
559
d427503c
VZ
560#endif // wxUSE_ACCEL
561
c2dcfdef 562// ---------------------------------------------------------------------------
717a57c2 563// set wxMenu title
c2dcfdef
VZ
564// ---------------------------------------------------------------------------
565
2bda0e17
KB
566void wxMenu::SetTitle(const wxString& label)
567{
c626a8b7
VZ
568 bool hasNoTitle = m_title.IsEmpty();
569 m_title = label;
b8d3a4f1 570
c50f1fb9 571 HMENU hMenu = GetHmenu();
b8d3a4f1 572
c626a8b7 573 if ( hasNoTitle )
b8d3a4f1 574 {
c626a8b7
VZ
575 if ( !label.IsEmpty() )
576 {
717a57c2
VZ
577 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
578 (unsigned)idMenuTitle, m_title) ||
579 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
c626a8b7 580 {
f6bcfd97 581 wxLogLastError(wxT("InsertMenu"));
c626a8b7
VZ
582 }
583 }
b8d3a4f1
VZ
584 }
585 else
586 {
c626a8b7
VZ
587 if ( label.IsEmpty() )
588 {
589 // remove the title and the separator after it
590 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
591 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
592 {
f6bcfd97 593 wxLogLastError(wxT("RemoveMenu"));
c626a8b7
VZ
594 }
595 }
596 else
597 {
598 // modify the title
4676948b
JS
599#ifdef __WXWINCE__
600 MENUITEMINFO info;
601 wxZeroMemory(info);
602 info.cbSize = sizeof(info);
603 info.fMask = MIIM_TYPE;
604 info.fType = MFT_STRING;
605 info.cch = m_title.Length();
606 info.dwTypeData = (LPTSTR) m_title.c_str();
607 if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) )
608 {
609 wxLogLastError(wxT("SetMenuItemInfo"));
610 }
611#else
c626a8b7 612 if ( !ModifyMenu(hMenu, 0u,
717a57c2
VZ
613 MF_BYPOSITION | MF_STRING,
614 (unsigned)idMenuTitle, m_title) )
c626a8b7 615 {
f6bcfd97 616 wxLogLastError(wxT("ModifyMenu"));
c626a8b7 617 }
4676948b 618#endif
c626a8b7 619 }
b8d3a4f1 620 }
b8d3a4f1 621
42e69d6b 622#ifdef __WIN32__
c626a8b7
VZ
623 // put the title string in bold face
624 if ( !m_title.IsEmpty() )
a3f4e9e8 625 {
0472ece7 626 SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle);
a3f4e9e8 627 }
717a57c2 628#endif // Win32
2bda0e17
KB
629}
630
c2dcfdef
VZ
631// ---------------------------------------------------------------------------
632// event processing
633// ---------------------------------------------------------------------------
2bda0e17 634
debe6624 635bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id)
2bda0e17 636{
a3f4e9e8
VZ
637 // ignore commands from the menu title
638
639 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
640 if ( id != (WXWORD)idMenuTitle )
641 {
3ca6a5f0
BP
642 // VZ: previosuly, the command int was set to id too which was quite
643 // useless anyhow (as it could be retrieved using GetId()) and
644 // uncompatible with wxGTK, so now we use the command int instead
645 // to pass the checked status
4676948b
JS
646 UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND) ;
647 SendEvent(id, menuState & MF_CHECKED);
a3f4e9e8
VZ
648 }
649
650 return TRUE;
2bda0e17
KB
651}
652
c2dcfdef
VZ
653// ---------------------------------------------------------------------------
654// other
655// ---------------------------------------------------------------------------
2bda0e17 656
717a57c2
VZ
657wxWindow *wxMenu::GetWindow() const
658{
659 if ( m_invokingWindow != NULL )
660 return m_invokingWindow;
661 else if ( m_menuBar != NULL)
662 return m_menuBar->GetFrame();
663
664 return NULL;
c2dcfdef
VZ
665}
666
667// ---------------------------------------------------------------------------
2bda0e17 668// Menu Bar
c2dcfdef
VZ
669// ---------------------------------------------------------------------------
670
671void wxMenuBar::Init()
2bda0e17 672{
c626a8b7 673 m_eventHandler = this;
c626a8b7 674 m_hMenu = 0;
39d2f9a7
JS
675#ifdef __WXWINCE__
676 m_toolBar = NULL;
677#endif
cba2db0c 678}
2bda0e17 679
c2dcfdef
VZ
680wxMenuBar::wxMenuBar()
681{
682 Init();
683}
684
cba2db0c
JS
685wxMenuBar::wxMenuBar( long WXUNUSED(style) )
686{
c2dcfdef 687 Init();
2bda0e17
KB
688}
689
c2dcfdef 690wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[])
2bda0e17 691{
c2dcfdef
VZ
692 Init();
693
a8cfd0cb 694 m_titles.Alloc(count);
c2dcfdef 695
a8cfd0cb
VZ
696 for ( int i = 0; i < count; i++ )
697 {
698 m_menus.Append(menus[i]);
699 m_titles.Add(titles[i]);
2bda0e17 700
a8cfd0cb
VZ
701 menus[i]->Attach(this);
702 }
2bda0e17
KB
703}
704
b8d3a4f1 705wxMenuBar::~wxMenuBar()
2bda0e17 706{
39d2f9a7
JS
707 // In Windows CE, the menubar is always associated
708 // with a toolbar, which destroys the menu implicitly.
bf95a04f
JS
709#ifdef __WXWINCE__
710 if (GetToolBar())
711 GetToolBar()->SetMenuBar(NULL);
712#else
7a0363dd
JS
713 // we should free Windows resources only if Windows doesn't do it for us
714 // which happens if we're attached to a frame
715 if (m_hMenu && !IsAttached())
716 {
717 ::DestroyMenu((HMENU)m_hMenu);
718 m_hMenu = (WXHMENU)NULL;
719 }
39d2f9a7 720#endif
c2dcfdef 721}
2bda0e17 722
c2dcfdef
VZ
723// ---------------------------------------------------------------------------
724// wxMenuBar helpers
725// ---------------------------------------------------------------------------
726
727void wxMenuBar::Refresh()
728{
065de612 729 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
c2dcfdef 730
39d2f9a7
JS
731#ifdef __WXWINCE__
732 if (GetToolBar())
733 {
734 CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0);
735 }
736#else
1e6feb95 737 DrawMenuBar(GetHwndOf(GetFrame()));
39d2f9a7 738#endif
c2dcfdef
VZ
739}
740
741WXHMENU wxMenuBar::Create()
742{
beb471b5
JS
743 // Note: this totally doesn't work on Smartphone,
744 // since you have to use resources.
745 // We'll have to find another way to add a menu
746 // by changing/adding menu items to an existing menu.
39d2f9a7
JS
747#ifdef __WXWINCE__
748 if ( m_hMenu != 0 )
749 return m_hMenu;
750
751 if (!GetToolBar())
752 return 0;
753
754 HWND hCommandBar = (HWND) GetToolBar()->GetHWND();
755 HMENU hMenu = (HMENU)::SendMessage(hCommandBar, SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0);
756 if (hMenu)
757 {
758 TBBUTTON tbButton;
759 memset(&tbButton, 0, sizeof(TBBUTTON));
760 tbButton.iBitmap = I_IMAGENONE;
761 tbButton.fsState = TBSTATE_ENABLED;
762 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
763
764 size_t i;
765 for (i = 0; i < GetMenuCount(); i++)
766 {
767 HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu() ;
768 tbButton.dwData = (DWORD)hPopupMenu;
769 wxString label = wxStripMenuCodes(GetLabelTop(i));
770 tbButton.iString = (int) label.c_str();
beb471b5
JS
771
772 int position = i;
39d2f9a7
JS
773
774 tbButton.idCommand = NewControlId();
beb471b5 775 if (!::SendMessage(hCommandBar, TB_INSERTBUTTON, position, (LPARAM)&tbButton))
39d2f9a7
JS
776 {
777 wxLogLastError(wxT("TB_INSERTBUTTON"));
778 }
779 }
780 }
781 m_hMenu = (WXHMENU) hMenu;
782 return m_hMenu;
783#else
3ca6a5f0 784 if ( m_hMenu != 0 )
717a57c2 785 return m_hMenu;
1cf27c63 786
c2dcfdef 787 m_hMenu = (WXHMENU)::CreateMenu();
2bda0e17 788
c2dcfdef 789 if ( !m_hMenu )
c626a8b7 790 {
f6bcfd97 791 wxLogLastError(wxT("CreateMenu"));
c626a8b7 792 }
c2dcfdef 793 else
c626a8b7 794 {
222ed1d6
MB
795 size_t count = GetMenuCount(), i;
796 wxMenuList::iterator it;
797 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
c2dcfdef
VZ
798 {
799 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
222ed1d6 800 (UINT)(*it)->GetHMenu(),
c2dcfdef
VZ
801 m_titles[i]) )
802 {
f6bcfd97 803 wxLogLastError(wxT("AppendMenu"));
c2dcfdef
VZ
804 }
805 }
c626a8b7 806 }
c626a8b7 807
c2dcfdef 808 return m_hMenu;
39d2f9a7 809#endif
2bda0e17
KB
810}
811
c2dcfdef 812// ---------------------------------------------------------------------------
3dfac970 813// wxMenuBar functions to work with the top level submenus
c2dcfdef
VZ
814// ---------------------------------------------------------------------------
815
3dfac970
VZ
816// NB: we don't support owner drawn top level items for now, if we do these
817// functions would have to be changed to use wxMenuItem as well
2bda0e17 818
a8cfd0cb 819void wxMenuBar::EnableTop(size_t pos, bool enable)
2bda0e17 820{
717a57c2
VZ
821 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
822
823 int flag = enable ? MF_ENABLED : MF_GRAYED;
2bda0e17 824
c626a8b7 825 EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag);
adc6fb16
VZ
826
827 Refresh();
2bda0e17
KB
828}
829
a8cfd0cb 830void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
2bda0e17 831{
717a57c2
VZ
832 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
833
834 m_titles[pos] = label;
835
836 if ( !IsAttached() )
837 {
838 return;
839 }
840 //else: have to modify the existing menu
841
8cd85069 842 UINT id;
c2dcfdef
VZ
843 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
844 if ( flagsOld == 0xFFFFFFFF )
c626a8b7 845 {
223d09f6 846 wxLogLastError(wxT("GetMenuState"));
c2dcfdef
VZ
847
848 return;
849 }
850
851 if ( flagsOld & MF_POPUP )
852 {
853 // HIBYTE contains the number of items in the submenu in this case
ad9bb75f
VZ
854 flagsOld &= 0xff;
855 id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos);
c626a8b7
VZ
856 }
857 else
8cd85069
VZ
858 {
859 id = pos;
860 }
861
4676948b
JS
862#ifdef __WXWINCE__
863 MENUITEMINFO info;
864 wxZeroMemory(info);
865 info.cbSize = sizeof(info);
866 info.fMask = MIIM_TYPE;
867 info.fType = MFT_STRING;
868 info.cch = label.Length();
869 info.dwTypeData = (LPTSTR) label.c_str();
870 if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, & info) )
871 {
872 wxLogLastError(wxT("SetMenuItemInfo"));
873 }
874
875#else
c50f1fb9 876 if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld,
4676948b 877 id, label) == (int)0xFFFFFFFF )
c2dcfdef 878 {
f6bcfd97 879 wxLogLastError(wxT("ModifyMenu"));
c2dcfdef 880 }
4676948b 881#endif
717a57c2
VZ
882
883 Refresh();
2bda0e17
KB
884}
885
a8cfd0cb 886wxString wxMenuBar::GetLabelTop(size_t pos) const
2bda0e17 887{
717a57c2
VZ
888 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
889 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
8cd85069 890
4a1c207c 891 return wxMenuItem::GetLabelFromText(m_titles[pos]);
2bda0e17
KB
892}
893
ad9bb75f
VZ
894// ---------------------------------------------------------------------------
895// wxMenuBar construction
896// ---------------------------------------------------------------------------
897
a8cfd0cb 898wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 899{
ad9bb75f
VZ
900 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
901 if ( !menuOld )
f7f50f49
VZ
902 return NULL;
903
ad9bb75f 904 m_titles[pos] = title;
a8cfd0cb 905
ad9bb75f 906 if ( IsAttached() )
a8cfd0cb 907 {
ad9bb75f
VZ
908 // can't use ModifyMenu() because it deletes the submenu it replaces
909 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
a8cfd0cb 910 {
f6bcfd97 911 wxLogLastError(wxT("RemoveMenu"));
a8cfd0cb 912 }
1cf27c63 913
ad9bb75f
VZ
914 if ( !::InsertMenu(GetHmenu(), (UINT)pos,
915 MF_BYPOSITION | MF_POPUP | MF_STRING,
916 (UINT)GetHmenuOf(menu), title) )
917 {
f6bcfd97 918 wxLogLastError(wxT("InsertMenu"));
ad9bb75f
VZ
919 }
920
717a57c2
VZ
921#if wxUSE_ACCEL
922 if ( menuOld->HasAccels() || menu->HasAccels() )
923 {
924 // need to rebuild accell table
925 RebuildAccelTable();
926 }
927#endif // wxUSE_ACCEL
928
ad9bb75f 929 Refresh();
a8cfd0cb 930 }
ad9bb75f
VZ
931
932 return menuOld;
1cf27c63
UM
933}
934
a8cfd0cb 935bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1cf27c63 936{
ad9bb75f 937 if ( !wxMenuBarBase::Insert(pos, menu, title) )
a8cfd0cb 938 return FALSE;
1cf27c63 939
ad9bb75f
VZ
940 m_titles.Insert(title, pos);
941
ad9bb75f
VZ
942 if ( IsAttached() )
943 {
39d2f9a7
JS
944#ifdef __WXWINCE__
945 if (!GetToolBar())
946 return FALSE;
947 TBBUTTON tbButton;
948 memset(&tbButton, 0, sizeof(TBBUTTON));
949 tbButton.iBitmap = I_IMAGENONE;
950 tbButton.fsState = TBSTATE_ENABLED;
951 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
952
953 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
954 tbButton.dwData = (DWORD)hPopupMenu;
955 wxString label = wxStripMenuCodes(title);
956 tbButton.iString = (int) label.c_str();
957
958 tbButton.idCommand = NewControlId();
959 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
960 {
961 wxLogLastError(wxT("TB_INSERTBUTTON"));
962 return FALSE;
963 }
964#else
ad9bb75f
VZ
965 if ( !::InsertMenu(GetHmenu(), pos,
966 MF_BYPOSITION | MF_POPUP | MF_STRING,
967 (UINT)GetHmenuOf(menu), title) )
968 {
f6bcfd97 969 wxLogLastError(wxT("InsertMenu"));
ad9bb75f 970 }
39d2f9a7 971#endif
717a57c2
VZ
972#if wxUSE_ACCEL
973 if ( menu->HasAccels() )
974 {
975 // need to rebuild accell table
976 RebuildAccelTable();
977 }
978#endif // wxUSE_ACCEL
979
ad9bb75f 980 Refresh();
a8cfd0cb 981 }
ad9bb75f
VZ
982
983 return TRUE;
1cf27c63
UM
984}
985
ad9bb75f 986bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
2bda0e17 987{
ad9bb75f
VZ
988 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
989 wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") );
2bda0e17 990
717a57c2
VZ
991 if ( !wxMenuBarBase::Append(menu, title) )
992 return FALSE;
993
717a57c2
VZ
994 m_titles.Add(title);
995
ad9bb75f
VZ
996 if ( IsAttached() )
997 {
39d2f9a7
JS
998#ifdef __WXWINCE__
999 if (!GetToolBar())
1000 return FALSE;
1001 TBBUTTON tbButton;
1002 memset(&tbButton, 0, sizeof(TBBUTTON));
1003 tbButton.iBitmap = I_IMAGENONE;
1004 tbButton.fsState = TBSTATE_ENABLED;
1005 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
1006
1007 size_t pos = GetMenuCount();
1008 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1009 tbButton.dwData = (DWORD)hPopupMenu;
1010 wxString label = wxStripMenuCodes(title);
1011 tbButton.iString = (int) label.c_str();
1012
1013 tbButton.idCommand = NewControlId();
1014 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1015 {
1016 wxLogLastError(wxT("TB_INSERTBUTTON"));
1017 return FALSE;
1018 }
1019#else
ad9bb75f
VZ
1020 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
1021 (UINT)submenu, title) )
1022 {
1023 wxLogLastError(wxT("AppendMenu"));
1024 }
39d2f9a7 1025#endif
ad9bb75f 1026
717a57c2
VZ
1027#if wxUSE_ACCEL
1028 if ( menu->HasAccels() )
1029 {
39d2f9a7 1030 // need to rebuild accelerator table
717a57c2
VZ
1031 RebuildAccelTable();
1032 }
1033#endif // wxUSE_ACCEL
1034
ad9bb75f
VZ
1035 Refresh();
1036 }
2bda0e17 1037
a8cfd0cb 1038 return TRUE;
2bda0e17
KB
1039}
1040
a8cfd0cb 1041wxMenu *wxMenuBar::Remove(size_t pos)
2bda0e17 1042{
a8cfd0cb
VZ
1043 wxMenu *menu = wxMenuBarBase::Remove(pos);
1044 if ( !menu )
1045 return NULL;
c626a8b7 1046
ad9bb75f
VZ
1047 if ( IsAttached() )
1048 {
39d2f9a7
JS
1049#ifdef __WXWINCE__
1050 if (GetToolBar())
1051 {
1052 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0))
1053 {
1054 wxLogLastError(wxT("TB_DELETEBUTTON"));
1055 }
1056 }
1057#else
ad9bb75f
VZ
1058 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
1059 {
f6bcfd97 1060 wxLogLastError(wxT("RemoveMenu"));
ad9bb75f 1061 }
39d2f9a7 1062#endif
717a57c2
VZ
1063#if wxUSE_ACCEL
1064 if ( menu->HasAccels() )
1065 {
1066 // need to rebuild accell table
1067 RebuildAccelTable();
1068 }
1069#endif // wxUSE_ACCEL
1070
ad9bb75f
VZ
1071 Refresh();
1072 }
2bda0e17 1073
ba8c1601 1074 m_titles.RemoveAt(pos);
2bda0e17 1075
a8cfd0cb 1076 return menu;
2bda0e17
KB
1077}
1078
d427503c 1079#if wxUSE_ACCEL
717a57c2
VZ
1080
1081void wxMenuBar::RebuildAccelTable()
1082{
1083 // merge the accelerators of all menus into one accel table
42e69d6b 1084 size_t nAccelCount = 0;
a8cfd0cb 1085 size_t i, count = GetMenuCount();
222ed1d6
MB
1086 wxMenuList::iterator it;
1087 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
42e69d6b 1088 {
222ed1d6 1089 nAccelCount += (*it)->GetAccelCount();
42e69d6b
VZ
1090 }
1091
5df1250b 1092 if ( nAccelCount )
42e69d6b 1093 {
5df1250b 1094 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
42e69d6b 1095
5df1250b 1096 nAccelCount = 0;
222ed1d6 1097 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
5df1250b 1098 {
222ed1d6 1099 nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]);
5df1250b
VZ
1100 }
1101
1102 m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
42e69d6b 1103
5df1250b
VZ
1104 delete [] accelEntries;
1105 }
717a57c2
VZ
1106}
1107
1108#endif // wxUSE_ACCEL
1109
1110void wxMenuBar::Attach(wxFrame *frame)
1111{
1e6feb95 1112 wxMenuBarBase::Attach(frame);
717a57c2 1113
717a57c2
VZ
1114#if wxUSE_ACCEL
1115 RebuildAccelTable();
d427503c 1116#endif // wxUSE_ACCEL
42e69d6b
VZ
1117}
1118
1cf27c63
UM
1119void wxMenuBar::Detach()
1120{
1e6feb95 1121 wxMenuBarBase::Detach();
2bda0e17
KB
1122}
1123
1e6feb95 1124#endif // wxUSE_MENUS