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