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