]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/menu.cpp
don't use deprecated symbols
[wxWidgets.git] / src / msw / menu.cpp
... / ...
CommitLineData
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$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "menu.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#if wxUSE_MENUS
32
33#ifndef WX_PRECOMP
34 #include "wx/frame.h"
35 #include "wx/menu.h"
36 #include "wx/utils.h"
37 #include "wx/intl.h"
38 #include "wx/log.h"
39#endif
40
41#if wxUSE_OWNER_DRAWN
42 #include "wx/ownerdrw.h"
43#endif
44
45#include "wx/msw/private.h"
46
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
61// other standard headers
62#include <string.h>
63
64// ----------------------------------------------------------------------------
65// global variables
66// ----------------------------------------------------------------------------
67
68extern wxMenu *wxCurrentPopupMenu;
69
70// ----------------------------------------------------------------------------
71// constants
72// ----------------------------------------------------------------------------
73
74// the (popup) menu title has this special id
75static const int idMenuTitle = -2;
76
77// ----------------------------------------------------------------------------
78// private functions
79// ----------------------------------------------------------------------------
80
81// make the given menu item default
82static void SetDefaultMenuItem(HMENU hmenu, UINT id)
83{
84#ifndef __WXWINCE__
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 }
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;
108}
109#endif
110
111// ============================================================================
112// implementation
113// ============================================================================
114
115#include <wx/listimpl.cpp>
116
117WX_DEFINE_LIST( wxMenuInfoList ) ;
118
119#if wxUSE_EXTENDED_RTTI
120
121WX_DEFINE_FLAGS( wxMenuStyle )
122
123wxBEGIN_FLAGS( wxMenuStyle )
124 wxFLAGS_MEMBER(wxMENU_TEAROFF)
125wxEND_FLAGS( wxMenuStyle )
126
127IMPLEMENT_DYNAMIC_CLASS_XTI(wxMenu, wxEvtHandler,"wx/menu.h")
128
129wxCOLLECTION_TYPE_INFO( wxMenuItem * , wxMenuItemList ) ;
130
131template<> void wxCollectionToVariantArray( wxMenuItemList const &theList, wxxVariantArray &value)
132{
133 wxListCollectionToVariantArray<wxMenuItemList::compatibility_iterator>( theList , value ) ;
134}
135
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()
142
143wxBEGIN_HANDLERS_TABLE(wxMenu)
144wxEND_HANDLERS_TABLE()
145
146wxDIRECT_CONSTRUCTOR_2( wxMenu , wxString , Title , long , MenuStyle )
147
148WX_DEFINE_FLAGS( wxMenuBarStyle )
149
150wxBEGIN_FLAGS( wxMenuBarStyle )
151 wxFLAGS_MEMBER(wxMB_DOCKABLE)
152wxEND_FLAGS( wxMenuBarStyle )
153
154// the negative id would lead the window (its superclass !) to vetoe streaming out otherwise
155bool wxMenuBarStreamingCallback( const wxObject *WXUNUSED(object), wxWriter * , wxPersister * , wxxVariantArray & )
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
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()
168
169wxBEGIN_HANDLERS_TABLE(wxMenuInfo)
170wxEND_HANDLERS_TABLE()
171
172wxCONSTRUCTOR_2( wxMenuInfo , wxMenu* , Menu , wxString , Title )
173
174wxCOLLECTION_TYPE_INFO( wxMenuInfo * , wxMenuInfoList ) ;
175
176template<> void wxCollectionToVariantArray( wxMenuInfoList const &theList, wxxVariantArray &value)
177{
178 wxListCollectionToVariantArray<wxMenuInfoList::compatibility_iterator>( theList , value ) ;
179}
180
181wxBEGIN_PROPERTIES_TABLE(wxMenuBar)
182 wxPROPERTY_COLLECTION( MenuInfos , wxMenuInfoList , wxMenuInfo* , Append , GetMenuInfos , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
183wxEND_PROPERTIES_TABLE()
184
185wxBEGIN_HANDLERS_TABLE(wxMenuBar)
186wxEND_HANDLERS_TABLE()
187
188wxCONSTRUCTOR_DUMMY( wxMenuBar )
189
190#else
191IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
192IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxWindow)
193IMPLEMENT_DYNAMIC_CLASS(wxMenuInfo, wxObject)
194#endif
195
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}
208
209// ---------------------------------------------------------------------------
210// wxMenu construction, adding and removing menu items
211// ---------------------------------------------------------------------------
212
213// Construct a menu with optional title (then use append)
214void wxMenu::Init()
215{
216 m_doBreak = FALSE;
217 m_startRadioGroup = -1;
218
219 // create the menu
220 m_hMenu = (WXHMENU)CreatePopupMenu();
221 if ( !m_hMenu )
222 {
223 wxLogLastError(wxT("CreatePopupMenu"));
224 }
225
226 // if we have a title, insert it in the beginning of the menu
227 if ( !!m_title )
228 {
229 Append(idMenuTitle, m_title);
230 AppendSeparator();
231 }
232}
233
234// The wxWindow destructor will take care of deleting the submenus.
235wxMenu::~wxMenu()
236{
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() )
241 {
242 if ( !::DestroyMenu(GetHmenu()) )
243 {
244 wxLogLastError(wxT("DestroyMenu"));
245 }
246 }
247
248#if wxUSE_ACCEL
249 // delete accels
250 WX_CLEAR_ARRAY(m_accels);
251#endif // wxUSE_ACCEL
252}
253
254void wxMenu::Break()
255{
256 // this will take effect during the next call to Append()
257 m_doBreak = TRUE;
258}
259
260void wxMenu::Attach(wxMenuBarBase *menubar)
261{
262 wxMenuBase::Attach(menubar);
263
264 EndRadioGroup();
265}
266
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)
282{
283 if ( item->IsSubMenu() )
284 {
285 wxMenu *submenu = item->GetSubMenu();
286 wxMenuItemList::compatibility_iterator node = submenu->GetMenuItems().GetFirst();
287 while ( node )
288 {
289 UpdateAccel(node->GetData());
290
291 node = node->GetNext();
292 }
293 }
294 else if ( !item->IsSeparator() )
295 {
296 // find the (new) accel for this item
297 wxAcceleratorEntry *accel = wxGetAccelFromString(item->GetText());
298 if ( accel )
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 }
311 else
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
318 m_accels.RemoveAt(n);
319 }
320
321 if ( IsAttached() )
322 {
323 m_menuBar->RebuildAccelTable();
324 }
325 }
326 //else: it is a separator, they can't have accels, nothing to do
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);
336#endif // wxUSE_ACCEL
337
338 UINT flags = 0;
339
340 // if "Break" has just been called, insert a menu break before this item
341 // (and don't forget to reset the flag)
342 if ( m_doBreak ) {
343 flags |= MF_MENUBREAK;
344 m_doBreak = FALSE;
345 }
346
347 if ( pItem->IsSeparator() ) {
348 flags |= MF_SEPARATOR;
349 }
350
351 // id is the numeric id for normal menu items and HMENU for submenus as
352 // required by ::AppendMenu() API
353 UINT id;
354 wxMenu *submenu = pItem->GetSubMenu();
355 if ( submenu != NULL ) {
356 wxASSERT_MSG( submenu->GetHMenu(), wxT("invalid submenu") );
357
358 submenu->SetParent(this);
359
360 id = (UINT)submenu->GetHMenu();
361
362 flags |= MF_POPUP;
363 }
364 else {
365 id = pItem->GetId();
366 }
367
368#ifdef __WXWINCE__
369 wxString strippedString;
370#endif
371
372 LPCTSTR pData;
373
374#if wxUSE_OWNER_DRAWN
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;
378 pData = (LPCTSTR)pItem;
379 }
380 else
381#endif
382 {
383 // menu is just a normal string (passed in data parameter)
384 flags |= MF_STRING;
385
386#ifdef __WXWINCE__
387 strippedString = wxStripMenuCodes(pItem->GetText());
388 pData = (wxChar*)strippedString.c_str();
389#else
390 pData = (wxChar*)pItem->GetText().c_str();
391#endif
392 }
393
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 )
405 {
406 wxLogLastError(wxT("Insert or AppendMenu"));
407
408 return FALSE;
409 }
410
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 }
418#endif // __WIN32__
419
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{
431 // we're not inside a radio group any longer
432 m_startRadioGroup = -1;
433}
434
435bool wxMenu::DoAppend(wxMenuItem *item)
436{
437 wxCHECK_MSG( item, FALSE, _T("NULL item in wxMenu::DoAppend") );
438
439 bool check = FALSE;
440
441 if ( item->GetKind() == wxITEM_RADIO )
442 {
443 int count = GetMenuItemCount();
444
445 if ( m_startRadioGroup == -1 )
446 {
447 // start a new radio group
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);
461 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
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 }
471 }
472 }
473 else // not a radio item
474 {
475 EndRadioGroup();
476 }
477
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;
490}
491
492bool wxMenu::DoInsert(size_t pos, wxMenuItem *item)
493{
494 return wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos);
495}
496
497wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
498{
499 // we need to find the items position in the child list
500 size_t pos;
501 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
502 for ( pos = 0; node; pos++ )
503 {
504 if ( node->GetData() == item )
505 break;
506
507 node = node->GetNext();
508 }
509
510 // DoRemove() (unlike Remove) can only be called for existing item!
511 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
512
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];
519
520 m_accels.RemoveAt(n);
521 }
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 {
528 wxLogLastError(wxT("RemoveMenu"));
529 }
530
531 if ( IsAttached() && m_menuBar->IsAttached() )
532 {
533 // otherwise, the chane won't be visible
534 m_menuBar->Refresh();
535 }
536
537 // and from internal data structures
538 return wxMenuBase::DoRemove(item);
539}
540
541// ---------------------------------------------------------------------------
542// accelerator helpers
543// ---------------------------------------------------------------------------
544
545#if wxUSE_ACCEL
546
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 {
554 *accels++ = *m_accels[n];
555 }
556
557 return count;
558}
559
560#endif // wxUSE_ACCEL
561
562// ---------------------------------------------------------------------------
563// set wxMenu title
564// ---------------------------------------------------------------------------
565
566void wxMenu::SetTitle(const wxString& label)
567{
568 bool hasNoTitle = m_title.IsEmpty();
569 m_title = label;
570
571 HMENU hMenu = GetHmenu();
572
573 if ( hasNoTitle )
574 {
575 if ( !label.IsEmpty() )
576 {
577 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
578 (unsigned)idMenuTitle, m_title) ||
579 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
580 {
581 wxLogLastError(wxT("InsertMenu"));
582 }
583 }
584 }
585 else
586 {
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 {
593 wxLogLastError(wxT("RemoveMenu"));
594 }
595 }
596 else
597 {
598 // modify the title
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
612 if ( !ModifyMenu(hMenu, 0u,
613 MF_BYPOSITION | MF_STRING,
614 (unsigned)idMenuTitle, m_title) )
615 {
616 wxLogLastError(wxT("ModifyMenu"));
617 }
618#endif
619 }
620 }
621
622#ifdef __WIN32__
623 // put the title string in bold face
624 if ( !m_title.IsEmpty() )
625 {
626 SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle);
627 }
628#endif // Win32
629}
630
631// ---------------------------------------------------------------------------
632// event processing
633// ---------------------------------------------------------------------------
634
635bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id)
636{
637 // ignore commands from the menu title
638
639 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
640 if ( id != (WXWORD)idMenuTitle )
641 {
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
646 UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND) ;
647 SendEvent(id, menuState & MF_CHECKED);
648 }
649
650 return TRUE;
651}
652
653// ---------------------------------------------------------------------------
654// other
655// ---------------------------------------------------------------------------
656
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;
665}
666
667// ---------------------------------------------------------------------------
668// Menu Bar
669// ---------------------------------------------------------------------------
670
671void wxMenuBar::Init()
672{
673 m_eventHandler = this;
674 m_hMenu = 0;
675#ifdef __WXWINCE__
676 m_toolBar = NULL;
677#endif
678}
679
680wxMenuBar::wxMenuBar()
681{
682 Init();
683}
684
685wxMenuBar::wxMenuBar( long WXUNUSED(style) )
686{
687 Init();
688}
689
690wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[])
691{
692 Init();
693
694 m_titles.Alloc(count);
695
696 for ( int i = 0; i < count; i++ )
697 {
698 m_menus.Append(menus[i]);
699 m_titles.Add(titles[i]);
700
701 menus[i]->Attach(this);
702 }
703}
704
705wxMenuBar::~wxMenuBar()
706{
707 // In Windows CE, the menubar is always associated
708 // with a toolbar, which destroys the menu implicitly.
709#ifdef __WXWINCE__
710 if (GetToolBar())
711 GetToolBar()->SetMenuBar(NULL);
712#else
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 }
720#endif
721}
722
723// ---------------------------------------------------------------------------
724// wxMenuBar helpers
725// ---------------------------------------------------------------------------
726
727void wxMenuBar::Refresh()
728{
729 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
730
731#ifdef __WXWINCE__
732 if (GetToolBar())
733 {
734 CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0);
735 }
736#else
737 DrawMenuBar(GetHwndOf(GetFrame()));
738#endif
739}
740
741WXHMENU wxMenuBar::Create()
742{
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.
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();
771
772 int position = i;
773
774 tbButton.idCommand = NewControlId();
775 if (!::SendMessage(hCommandBar, TB_INSERTBUTTON, position, (LPARAM)&tbButton))
776 {
777 wxLogLastError(wxT("TB_INSERTBUTTON"));
778 }
779 }
780 }
781 m_hMenu = (WXHMENU) hMenu;
782 return m_hMenu;
783#else
784 if ( m_hMenu != 0 )
785 return m_hMenu;
786
787 m_hMenu = (WXHMENU)::CreateMenu();
788
789 if ( !m_hMenu )
790 {
791 wxLogLastError(wxT("CreateMenu"));
792 }
793 else
794 {
795 size_t count = GetMenuCount(), i;
796 wxMenuList::iterator it;
797 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
798 {
799 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
800 (UINT)(*it)->GetHMenu(),
801 m_titles[i]) )
802 {
803 wxLogLastError(wxT("AppendMenu"));
804 }
805 }
806 }
807
808 return m_hMenu;
809#endif
810}
811
812// ---------------------------------------------------------------------------
813// wxMenuBar functions to work with the top level submenus
814// ---------------------------------------------------------------------------
815
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
818
819void wxMenuBar::EnableTop(size_t pos, bool enable)
820{
821 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
822
823 int flag = enable ? MF_ENABLED : MF_GRAYED;
824
825 EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag);
826
827 Refresh();
828}
829
830void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
831{
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
842 UINT id;
843 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
844 if ( flagsOld == 0xFFFFFFFF )
845 {
846 wxLogLastError(wxT("GetMenuState"));
847
848 return;
849 }
850
851 if ( flagsOld & MF_POPUP )
852 {
853 // HIBYTE contains the number of items in the submenu in this case
854 flagsOld &= 0xff;
855 id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos);
856 }
857 else
858 {
859 id = pos;
860 }
861
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
876 if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld,
877 id, label) == (int)0xFFFFFFFF )
878 {
879 wxLogLastError(wxT("ModifyMenu"));
880 }
881#endif
882
883 Refresh();
884}
885
886wxString wxMenuBar::GetLabelTop(size_t pos) const
887{
888 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
889 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
890
891 return wxMenuItem::GetLabelFromText(m_titles[pos]);
892}
893
894// ---------------------------------------------------------------------------
895// wxMenuBar construction
896// ---------------------------------------------------------------------------
897
898wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
899{
900 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
901 if ( !menuOld )
902 return NULL;
903
904 m_titles[pos] = title;
905
906 if ( IsAttached() )
907 {
908 // can't use ModifyMenu() because it deletes the submenu it replaces
909 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
910 {
911 wxLogLastError(wxT("RemoveMenu"));
912 }
913
914 if ( !::InsertMenu(GetHmenu(), (UINT)pos,
915 MF_BYPOSITION | MF_POPUP | MF_STRING,
916 (UINT)GetHmenuOf(menu), title) )
917 {
918 wxLogLastError(wxT("InsertMenu"));
919 }
920
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
929 Refresh();
930 }
931
932 return menuOld;
933}
934
935bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
936{
937 if ( !wxMenuBarBase::Insert(pos, menu, title) )
938 return FALSE;
939
940 m_titles.Insert(title, pos);
941
942 if ( IsAttached() )
943 {
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
965 if ( !::InsertMenu(GetHmenu(), pos,
966 MF_BYPOSITION | MF_POPUP | MF_STRING,
967 (UINT)GetHmenuOf(menu), title) )
968 {
969 wxLogLastError(wxT("InsertMenu"));
970 }
971#endif
972#if wxUSE_ACCEL
973 if ( menu->HasAccels() )
974 {
975 // need to rebuild accell table
976 RebuildAccelTable();
977 }
978#endif // wxUSE_ACCEL
979
980 Refresh();
981 }
982
983 return TRUE;
984}
985
986bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
987{
988 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
989 wxCHECK_MSG( submenu, FALSE, wxT("can't append invalid menu to menubar") );
990
991 if ( !wxMenuBarBase::Append(menu, title) )
992 return FALSE;
993
994 m_titles.Add(title);
995
996 if ( IsAttached() )
997 {
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
1020 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
1021 (UINT)submenu, title) )
1022 {
1023 wxLogLastError(wxT("AppendMenu"));
1024 }
1025#endif
1026
1027#if wxUSE_ACCEL
1028 if ( menu->HasAccels() )
1029 {
1030 // need to rebuild accelerator table
1031 RebuildAccelTable();
1032 }
1033#endif // wxUSE_ACCEL
1034
1035 Refresh();
1036 }
1037
1038 return TRUE;
1039}
1040
1041wxMenu *wxMenuBar::Remove(size_t pos)
1042{
1043 wxMenu *menu = wxMenuBarBase::Remove(pos);
1044 if ( !menu )
1045 return NULL;
1046
1047 if ( IsAttached() )
1048 {
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
1058 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
1059 {
1060 wxLogLastError(wxT("RemoveMenu"));
1061 }
1062#endif
1063#if wxUSE_ACCEL
1064 if ( menu->HasAccels() )
1065 {
1066 // need to rebuild accell table
1067 RebuildAccelTable();
1068 }
1069#endif // wxUSE_ACCEL
1070
1071 Refresh();
1072 }
1073
1074 m_titles.RemoveAt(pos);
1075
1076 return menu;
1077}
1078
1079#if wxUSE_ACCEL
1080
1081void wxMenuBar::RebuildAccelTable()
1082{
1083 // merge the accelerators of all menus into one accel table
1084 size_t nAccelCount = 0;
1085 size_t i, count = GetMenuCount();
1086 wxMenuList::iterator it;
1087 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1088 {
1089 nAccelCount += (*it)->GetAccelCount();
1090 }
1091
1092 if ( nAccelCount )
1093 {
1094 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
1095
1096 nAccelCount = 0;
1097 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1098 {
1099 nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]);
1100 }
1101
1102 m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
1103
1104 delete [] accelEntries;
1105 }
1106}
1107
1108#endif // wxUSE_ACCEL
1109
1110void wxMenuBar::Attach(wxFrame *frame)
1111{
1112 wxMenuBarBase::Attach(frame);
1113
1114#if wxUSE_ACCEL
1115 RebuildAccelTable();
1116#endif // wxUSE_ACCEL
1117}
1118
1119void wxMenuBar::Detach()
1120{
1121 wxMenuBarBase::Detach();
1122}
1123
1124#endif // wxUSE_MENUS