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