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