]> git.saurik.com Git - wxWidgets.git/blob - src/msw/menu.cpp
remove GetImageList(int)
[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 // 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_ID | MIIM_STRING | MIIM_DATA | MIIM_BITMAP;
424 mii.wID = id;
425 mii.cch = itemText.length();
426 mii.dwTypeData = wx_const_cast(wxChar *, itemText.c_str());
427
428 // we can't pass HBITMAP directly as hbmpItem for 2 reasons:
429 // 1. we can't draw it with transparency then (this is not
430 // very important now but would be with themed menu bg)
431 // 2. worse, Windows inverses the bitmap for the selected
432 // item and this looks downright ugly
433 //
434 // so instead draw it ourselves in MSWOnDrawItem()
435 mii.dwItemData = wx_reinterpret_cast(ULONG_PTR, pItem);
436 mii.hbmpItem = HBMMENU_CALLBACK;
437
438 ok = ::InsertMenuItem(GetHmenu(), pos, TRUE /* by pos */, &mii);
439 if ( !ok )
440 {
441 wxLogLastError(wxT("InsertMenuItem()"));
442 }
443 else // InsertMenuItem() ok
444 {
445 // we need to remove the extra indent which is reserved for
446 // the checkboxes by default as it looks ugly unless check
447 // boxes are used together with bitmaps and this is not the
448 // case in wx API
449 WinStruct<MENUINFO> mi;
450
451 // don't call SetMenuInfo() directly, this would prevent
452 // the app from starting up under Windows 95/NT 4
453 typedef BOOL (WINAPI *SetMenuInfo_t)(HMENU, MENUINFO *);
454
455 wxDynamicLibrary dllUser(_T("user32"));
456 wxDYNLIB_FUNCTION(SetMenuInfo_t, SetMenuInfo, dllUser);
457 if ( pfnSetMenuInfo )
458 {
459 mi.fMask = MIM_STYLE;
460 mi.dwStyle = MNS_CHECKORBMP;
461 if ( !(*pfnSetMenuInfo)(GetHmenu(), &mi) )
462 wxLogLastError(_T("SetMenuInfo(MNS_NOCHECK)"));
463 }
464
465 // tell the item that it's not really owner-drawn but only
466 // needs to draw its bitmap, the rest is done by Windows
467 pItem->ResetOwnerDrawn();
468 }
469 }
470 #endif // MIIM_BITMAP
471 }
472
473 if ( !ok )
474 {
475 // item draws itself, pass pointer to it in data parameter
476 flags |= MF_OWNERDRAW;
477 pData = (LPCTSTR)pItem;
478 }
479 }
480 else
481 #endif // wxUSE_OWNER_DRAWN
482 {
483 // menu is just a normal string (passed in data parameter)
484 flags |= MF_STRING;
485
486 #ifdef __WXWINCE__
487 itemText = wxMenuItem::GetLabelFromText(itemText);
488 #endif
489
490 pData = (wxChar*)itemText.c_str();
491 }
492
493 // item might have been already inserted by InsertMenuItem() above
494 if ( !ok )
495 {
496 if ( !::InsertMenu(GetHmenu(), pos, flags | MF_BYPOSITION, id, pData) )
497 {
498 wxLogLastError(wxT("InsertMenu[Item]()"));
499
500 return false;
501 }
502 }
503
504
505 // if we just appended the title, highlight it
506 if ( (int)id == idMenuTitle )
507 {
508 // visually select the menu title
509 SetDefaultMenuItem(GetHmenu(), id);
510 }
511
512 // if we're already attached to the menubar, we must update it
513 if ( IsAttached() && GetMenuBar()->IsAttached() )
514 {
515 GetMenuBar()->Refresh();
516 }
517
518 return true;
519 }
520
521 void wxMenu::EndRadioGroup()
522 {
523 // we're not inside a radio group any longer
524 m_startRadioGroup = -1;
525 }
526
527 wxMenuItem* wxMenu::DoAppend(wxMenuItem *item)
528 {
529 wxCHECK_MSG( item, NULL, _T("NULL item in wxMenu::DoAppend") );
530
531 bool check = false;
532
533 if ( item->GetKind() == wxITEM_RADIO )
534 {
535 int count = GetMenuItemCount();
536
537 if ( m_startRadioGroup == -1 )
538 {
539 // start a new radio group
540 m_startRadioGroup = count;
541
542 // for now it has just one element
543 item->SetAsRadioGroupStart();
544 item->SetRadioGroupEnd(m_startRadioGroup);
545
546 // ensure that we have a checked item in the radio group
547 check = true;
548 }
549 else // extend the current radio group
550 {
551 // we need to update its end item
552 item->SetRadioGroupStart(m_startRadioGroup);
553 wxMenuItemList::compatibility_iterator node = GetMenuItems().Item(m_startRadioGroup);
554
555 if ( node )
556 {
557 node->GetData()->SetRadioGroupEnd(count);
558 }
559 else
560 {
561 wxFAIL_MSG( _T("where is the radio group start item?") );
562 }
563 }
564 }
565 else // not a radio item
566 {
567 EndRadioGroup();
568 }
569
570 if ( !wxMenuBase::DoAppend(item) || !DoInsertOrAppend(item) )
571 {
572 return NULL;
573 }
574
575 if ( check )
576 {
577 // check the item initially
578 item->Check(true);
579 }
580
581 return item;
582 }
583
584 wxMenuItem* wxMenu::DoInsert(size_t pos, wxMenuItem *item)
585 {
586 if (wxMenuBase::DoInsert(pos, item) && DoInsertOrAppend(item, pos))
587 return item;
588 else
589 return NULL;
590 }
591
592 wxMenuItem *wxMenu::DoRemove(wxMenuItem *item)
593 {
594 // we need to find the items position in the child list
595 size_t pos;
596 wxMenuItemList::compatibility_iterator node = GetMenuItems().GetFirst();
597 for ( pos = 0; node; pos++ )
598 {
599 if ( node->GetData() == item )
600 break;
601
602 node = node->GetNext();
603 }
604
605 // DoRemove() (unlike Remove) can only be called for existing item!
606 wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") );
607
608 #if wxUSE_ACCEL
609 // remove the corresponding accel from the accel table
610 int n = FindAccel(item->GetId());
611 if ( n != wxNOT_FOUND )
612 {
613 delete m_accels[n];
614
615 m_accels.RemoveAt(n);
616 }
617 //else: this item doesn't have an accel, nothing to do
618 #endif // wxUSE_ACCEL
619
620 // remove the item from the menu
621 if ( !::RemoveMenu(GetHmenu(), (UINT)pos, MF_BYPOSITION) )
622 {
623 wxLogLastError(wxT("RemoveMenu"));
624 }
625
626 if ( IsAttached() && GetMenuBar()->IsAttached() )
627 {
628 // otherwise, the chane won't be visible
629 GetMenuBar()->Refresh();
630 }
631
632 // and from internal data structures
633 return wxMenuBase::DoRemove(item);
634 }
635
636 // ---------------------------------------------------------------------------
637 // accelerator helpers
638 // ---------------------------------------------------------------------------
639
640 #if wxUSE_ACCEL
641
642 // create the wxAcceleratorEntries for our accels and put them into provided
643 // array - return the number of accels we have
644 size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
645 {
646 size_t count = GetAccelCount();
647 for ( size_t n = 0; n < count; n++ )
648 {
649 *accels++ = *m_accels[n];
650 }
651
652 return count;
653 }
654
655 #endif // wxUSE_ACCEL
656
657 // ---------------------------------------------------------------------------
658 // set wxMenu title
659 // ---------------------------------------------------------------------------
660
661 void wxMenu::SetTitle(const wxString& label)
662 {
663 bool hasNoTitle = m_title.empty();
664 m_title = label;
665
666 HMENU hMenu = GetHmenu();
667
668 if ( hasNoTitle )
669 {
670 if ( !label.empty() )
671 {
672 if ( !::InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
673 (unsigned)idMenuTitle, m_title) ||
674 !::InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
675 {
676 wxLogLastError(wxT("InsertMenu"));
677 }
678 }
679 }
680 else
681 {
682 if ( label.empty() )
683 {
684 // remove the title and the separator after it
685 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
686 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
687 {
688 wxLogLastError(wxT("RemoveMenu"));
689 }
690 }
691 else
692 {
693 // modify the title
694 #ifdef __WXWINCE__
695 MENUITEMINFO info;
696 wxZeroMemory(info);
697 info.cbSize = sizeof(info);
698 info.fMask = MIIM_TYPE;
699 info.fType = MFT_STRING;
700 info.cch = m_title.Length();
701 info.dwTypeData = (LPTSTR) m_title.c_str();
702 if ( !SetMenuItemInfo(hMenu, 0, TRUE, & info) )
703 {
704 wxLogLastError(wxT("SetMenuItemInfo"));
705 }
706 #else
707 if ( !ModifyMenu(hMenu, 0u,
708 MF_BYPOSITION | MF_STRING,
709 (unsigned)idMenuTitle, m_title) )
710 {
711 wxLogLastError(wxT("ModifyMenu"));
712 }
713 #endif
714 }
715 }
716
717 #ifdef __WIN32__
718 // put the title string in bold face
719 if ( !m_title.empty() )
720 {
721 SetDefaultMenuItem(GetHmenu(), (UINT)idMenuTitle);
722 }
723 #endif // Win32
724 }
725
726 // ---------------------------------------------------------------------------
727 // event processing
728 // ---------------------------------------------------------------------------
729
730 bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id)
731 {
732 // ignore commands from the menu title
733 if ( id != (WXWORD)idMenuTitle )
734 {
735 // get the checked status of the command: notice that menuState is the
736 // old state of the menu, so the test for MF_CHECKED must be inverted
737 UINT menuState = ::GetMenuState(GetHmenu(), id, MF_BYCOMMAND);
738 SendEvent(id, !(menuState & MF_CHECKED));
739 }
740
741 return true;
742 }
743
744 // ---------------------------------------------------------------------------
745 // other
746 // ---------------------------------------------------------------------------
747
748 wxWindow *wxMenu::GetWindow() const
749 {
750 if ( m_invokingWindow != NULL )
751 return m_invokingWindow;
752 else if ( GetMenuBar() != NULL)
753 return GetMenuBar()->GetFrame();
754
755 return NULL;
756 }
757
758 // ---------------------------------------------------------------------------
759 // Menu Bar
760 // ---------------------------------------------------------------------------
761
762 void wxMenuBar::Init()
763 {
764 m_eventHandler = this;
765 m_hMenu = 0;
766 #if wxUSE_TOOLBAR && defined(__WXWINCE__)
767 m_toolBar = NULL;
768 #endif
769 // Not using a combined wxToolBar/wxMenuBar? then use
770 // a commandbar in WinCE .NET just to implement the
771 // menubar.
772 #if defined(WINCE_WITH_COMMANDBAR)
773 m_commandBar = NULL;
774 m_adornmentsAdded = false;
775 #endif
776 }
777
778 wxMenuBar::wxMenuBar()
779 {
780 Init();
781 }
782
783 wxMenuBar::wxMenuBar( long WXUNUSED(style) )
784 {
785 Init();
786 }
787
788 wxMenuBar::wxMenuBar(size_t count, wxMenu *menus[], const wxString titles[], long WXUNUSED(style))
789 {
790 Init();
791
792 m_titles.Alloc(count);
793
794 for ( size_t i = 0; i < count; i++ )
795 {
796 m_menus.Append(menus[i]);
797 m_titles.Add(titles[i]);
798
799 menus[i]->Attach(this);
800 }
801 }
802
803 wxMenuBar::~wxMenuBar()
804 {
805 // In Windows CE (not .NET), the menubar is always associated
806 // with a toolbar, which destroys the menu implicitly.
807 #if defined(WINCE_WITHOUT_COMMANDBAR) && defined(__POCKETPC__)
808 if (GetToolBar())
809 {
810 wxToolMenuBar* toolMenuBar = wxDynamicCast(GetToolBar(), wxToolMenuBar);
811 if (toolMenuBar)
812 toolMenuBar->SetMenuBar(NULL);
813 }
814 #else
815 // we should free Windows resources only if Windows doesn't do it for us
816 // which happens if we're attached to a frame
817 if (m_hMenu && !IsAttached())
818 {
819 #if defined(WINCE_WITH_COMMANDBAR)
820 ::DestroyWindow((HWND) m_commandBar);
821 m_commandBar = (WXHWND) NULL;
822 #else
823 ::DestroyMenu((HMENU)m_hMenu);
824 #endif
825 m_hMenu = (WXHMENU)NULL;
826 }
827 #endif
828 }
829
830 // ---------------------------------------------------------------------------
831 // wxMenuBar helpers
832 // ---------------------------------------------------------------------------
833
834 void wxMenuBar::Refresh()
835 {
836 wxCHECK_RET( IsAttached(), wxT("can't refresh unattached menubar") );
837
838 #if defined(WINCE_WITHOUT_COMMANDBAR)
839 if (GetToolBar())
840 {
841 CommandBar_DrawMenuBar((HWND) GetToolBar()->GetHWND(), 0);
842 }
843 #elif defined(WINCE_WITH_COMMANDBAR)
844 if (m_commandBar)
845 DrawMenuBar((HWND) m_commandBar);
846 #else
847 DrawMenuBar(GetHwndOf(GetFrame()));
848 #endif
849 }
850
851 WXHMENU wxMenuBar::Create()
852 {
853 // Note: this totally doesn't work on Smartphone,
854 // since you have to use resources.
855 // We'll have to find another way to add a menu
856 // by changing/adding menu items to an existing menu.
857 #if defined(WINCE_WITHOUT_COMMANDBAR)
858 if ( m_hMenu != 0 )
859 return m_hMenu;
860
861 if (!GetToolBar())
862 return 0;
863
864 HWND hCommandBar = (HWND) GetToolBar()->GetHWND();
865 HMENU hMenu = (HMENU)::SendMessage(hCommandBar, SHCMBM_GETMENU, (WPARAM)0, (LPARAM)0);
866 if (hMenu)
867 {
868 TBBUTTON tbButton;
869 memset(&tbButton, 0, sizeof(TBBUTTON));
870 tbButton.iBitmap = I_IMAGENONE;
871 tbButton.fsState = TBSTATE_ENABLED;
872 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
873
874 size_t i;
875 for (i = 0; i < GetMenuCount(); i++)
876 {
877 HMENU hPopupMenu = (HMENU) GetMenu(i)->GetHMenu() ;
878 tbButton.dwData = (DWORD)hPopupMenu;
879 wxString label = wxStripMenuCodes(GetLabelTop(i));
880 tbButton.iString = (int) label.c_str();
881
882 int position = i;
883
884 tbButton.idCommand = NewControlId();
885 if (!::SendMessage(hCommandBar, TB_INSERTBUTTON, position, (LPARAM)&tbButton))
886 {
887 wxLogLastError(wxT("TB_INSERTBUTTON"));
888 }
889 }
890 }
891 m_hMenu = (WXHMENU) hMenu;
892 return m_hMenu;
893 #else
894 if ( m_hMenu != 0 )
895 return m_hMenu;
896
897 m_hMenu = (WXHMENU)::CreateMenu();
898
899 if ( !m_hMenu )
900 {
901 wxLogLastError(wxT("CreateMenu"));
902 }
903 else
904 {
905 size_t count = GetMenuCount(), i;
906 wxMenuList::iterator it;
907 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
908 {
909 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
910 (UINT)(*it)->GetHMenu(),
911 m_titles[i]) )
912 {
913 wxLogLastError(wxT("AppendMenu"));
914 }
915 }
916 }
917
918 return m_hMenu;
919 #endif
920 }
921
922 int wxMenuBar::MSWPositionForWxMenu(wxMenu *menu, int wxpos)
923 {
924 wxASSERT(menu);
925 wxASSERT(menu->GetHMenu());
926 wxASSERT(m_hMenu);
927
928 #if defined(__WXWINCE__)
929 int totalMSWItems = GetMenuCount();
930 #else
931 int totalMSWItems = GetMenuItemCount((HMENU)m_hMenu);
932 #endif
933
934 int i; // For old C++ compatibility
935 for(i=wxpos; i<totalMSWItems; i++)
936 {
937 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
938 return i;
939 }
940 for(i=0; i<wxpos; i++)
941 {
942 if(GetSubMenu((HMENU)m_hMenu,i)==(HMENU)menu->GetHMenu())
943 return i;
944 }
945 wxFAIL;
946 return -1;
947 }
948
949 // ---------------------------------------------------------------------------
950 // wxMenuBar functions to work with the top level submenus
951 // ---------------------------------------------------------------------------
952
953 // NB: we don't support owner drawn top level items for now, if we do these
954 // functions would have to be changed to use wxMenuItem as well
955
956 void wxMenuBar::EnableTop(size_t pos, bool enable)
957 {
958 wxCHECK_RET( IsAttached(), wxT("doesn't work with unattached menubars") );
959 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
960
961 int flag = enable ? MF_ENABLED : MF_GRAYED;
962
963 EnableMenuItem((HMENU)m_hMenu, MSWPositionForWxMenu(GetMenu(pos),pos), MF_BYPOSITION | flag);
964
965 Refresh();
966 }
967
968 void wxMenuBar::SetLabelTop(size_t pos, const wxString& label)
969 {
970 wxCHECK_RET( pos < GetMenuCount(), wxT("invalid menu index") );
971
972 m_titles[pos] = label;
973
974 if ( !IsAttached() )
975 {
976 return;
977 }
978 //else: have to modify the existing menu
979
980 int mswpos = MSWPositionForWxMenu(GetMenu(pos),pos);
981
982 UINT id;
983 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, mswpos, MF_BYPOSITION);
984 if ( flagsOld == 0xFFFFFFFF )
985 {
986 wxLogLastError(wxT("GetMenuState"));
987
988 return;
989 }
990
991 if ( flagsOld & MF_POPUP )
992 {
993 // HIBYTE contains the number of items in the submenu in this case
994 flagsOld &= 0xff;
995 id = (UINT)::GetSubMenu((HMENU)m_hMenu, mswpos);
996 }
997 else
998 {
999 id = pos;
1000 }
1001
1002 #ifdef __WXWINCE__
1003 MENUITEMINFO info;
1004 wxZeroMemory(info);
1005 info.cbSize = sizeof(info);
1006 info.fMask = MIIM_TYPE;
1007 info.fType = MFT_STRING;
1008 info.cch = label.Length();
1009 info.dwTypeData = (LPTSTR) label.c_str();
1010 if ( !SetMenuItemInfo(GetHmenu(), id, TRUE, & info) )
1011 {
1012 wxLogLastError(wxT("SetMenuItemInfo"));
1013 }
1014
1015 #else
1016 if ( ::ModifyMenu(GetHmenu(), mswpos, MF_BYPOSITION | MF_STRING | flagsOld,
1017 id, label) == (int)0xFFFFFFFF )
1018 {
1019 wxLogLastError(wxT("ModifyMenu"));
1020 }
1021 #endif
1022
1023 Refresh();
1024 }
1025
1026 wxString wxMenuBar::GetLabelTop(size_t pos) const
1027 {
1028 wxCHECK_MSG( pos < GetMenuCount(), wxEmptyString,
1029 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
1030
1031 return wxMenuItem::GetLabelFromText(m_titles[pos]);
1032 }
1033
1034 // ---------------------------------------------------------------------------
1035 // wxMenuBar construction
1036 // ---------------------------------------------------------------------------
1037
1038 wxMenu *wxMenuBar::Replace(size_t pos, wxMenu *menu, const wxString& title)
1039 {
1040 wxMenu *menuOld = wxMenuBarBase::Replace(pos, menu, title);
1041 if ( !menuOld )
1042 return NULL;
1043
1044 m_titles[pos] = title;
1045
1046 if ( IsAttached() )
1047 {
1048 int mswpos = MSWPositionForWxMenu(menuOld,pos);
1049
1050 // can't use ModifyMenu() because it deletes the submenu it replaces
1051 if ( !::RemoveMenu(GetHmenu(), (UINT)mswpos, MF_BYPOSITION) )
1052 {
1053 wxLogLastError(wxT("RemoveMenu"));
1054 }
1055
1056 if ( !::InsertMenu(GetHmenu(), (UINT)mswpos,
1057 MF_BYPOSITION | MF_POPUP | MF_STRING,
1058 (UINT)GetHmenuOf(menu), title) )
1059 {
1060 wxLogLastError(wxT("InsertMenu"));
1061 }
1062
1063 #if wxUSE_ACCEL
1064 if ( menuOld->HasAccels() || menu->HasAccels() )
1065 {
1066 // need to rebuild accell table
1067 RebuildAccelTable();
1068 }
1069 #endif // wxUSE_ACCEL
1070
1071 Refresh();
1072 }
1073
1074 return menuOld;
1075 }
1076
1077 bool wxMenuBar::Insert(size_t pos, wxMenu *menu, const wxString& title)
1078 {
1079 // Find out which MSW item before which we'll be inserting before
1080 // wxMenuBarBase::Insert is called and GetMenu(pos) is the new menu.
1081 // If IsAttached() is false this won't be used anyway
1082 int mswpos = (!IsAttached() || (pos == m_menus.GetCount()))
1083 ? -1 // append the menu
1084 : MSWPositionForWxMenu(GetMenu(pos),pos);
1085
1086 if ( !wxMenuBarBase::Insert(pos, menu, title) )
1087 return false;
1088
1089 m_titles.Insert(title, pos);
1090
1091 if ( IsAttached() )
1092 {
1093 #if defined(WINCE_WITHOUT_COMMANDAR)
1094 if (!GetToolBar())
1095 return false;
1096 TBBUTTON tbButton;
1097 memset(&tbButton, 0, sizeof(TBBUTTON));
1098 tbButton.iBitmap = I_IMAGENONE;
1099 tbButton.fsState = TBSTATE_ENABLED;
1100 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
1101
1102 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1103 tbButton.dwData = (DWORD)hPopupMenu;
1104 wxString label = wxStripMenuCodes(title);
1105 tbButton.iString = (int) label.c_str();
1106
1107 tbButton.idCommand = NewControlId();
1108 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1109 {
1110 wxLogLastError(wxT("TB_INSERTBUTTON"));
1111 return false;
1112 }
1113 #else
1114 if ( !::InsertMenu(GetHmenu(), mswpos,
1115 MF_BYPOSITION | MF_POPUP | MF_STRING,
1116 (UINT)GetHmenuOf(menu), title) )
1117 {
1118 wxLogLastError(wxT("InsertMenu"));
1119 }
1120 #endif
1121 #if wxUSE_ACCEL
1122 if ( menu->HasAccels() )
1123 {
1124 // need to rebuild accell table
1125 RebuildAccelTable();
1126 }
1127 #endif // wxUSE_ACCEL
1128
1129 Refresh();
1130 }
1131
1132 return true;
1133 }
1134
1135 bool wxMenuBar::Append(wxMenu *menu, const wxString& title)
1136 {
1137 WXHMENU submenu = menu ? menu->GetHMenu() : 0;
1138 wxCHECK_MSG( submenu, false, wxT("can't append invalid menu to menubar") );
1139
1140 if ( !wxMenuBarBase::Append(menu, title) )
1141 return false;
1142
1143 m_titles.Add(title);
1144
1145 if ( IsAttached() )
1146 {
1147 #if defined(WINCE_WITHOUT_COMMANDAR)
1148 if (!GetToolBar())
1149 return false;
1150 TBBUTTON tbButton;
1151 memset(&tbButton, 0, sizeof(TBBUTTON));
1152 tbButton.iBitmap = I_IMAGENONE;
1153 tbButton.fsState = TBSTATE_ENABLED;
1154 tbButton.fsStyle = TBSTYLE_DROPDOWN | TBSTYLE_NO_DROPDOWN_ARROW | TBSTYLE_AUTOSIZE;
1155
1156 size_t pos = GetMenuCount();
1157 HMENU hPopupMenu = (HMENU) menu->GetHMenu() ;
1158 tbButton.dwData = (DWORD)hPopupMenu;
1159 wxString label = wxStripMenuCodes(title);
1160 tbButton.iString = (int) label.c_str();
1161
1162 tbButton.idCommand = NewControlId();
1163 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_INSERTBUTTON, pos, (LPARAM)&tbButton))
1164 {
1165 wxLogLastError(wxT("TB_INSERTBUTTON"));
1166 return false;
1167 }
1168 #else
1169 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
1170 (UINT)submenu, title) )
1171 {
1172 wxLogLastError(wxT("AppendMenu"));
1173 }
1174 #endif
1175
1176 #if wxUSE_ACCEL
1177 if ( menu->HasAccels() )
1178 {
1179 // need to rebuild accelerator table
1180 RebuildAccelTable();
1181 }
1182 #endif // wxUSE_ACCEL
1183
1184 Refresh();
1185 }
1186
1187 return true;
1188 }
1189
1190 wxMenu *wxMenuBar::Remove(size_t pos)
1191 {
1192 wxMenu *menu = wxMenuBarBase::Remove(pos);
1193 if ( !menu )
1194 return NULL;
1195
1196 if ( IsAttached() )
1197 {
1198 #if defined(WINCE_WITHOUT_COMMANDAR)
1199 if (GetToolBar())
1200 {
1201 if (!::SendMessage((HWND) GetToolBar()->GetHWND(), TB_DELETEBUTTON, (UINT) pos, (LPARAM) 0))
1202 {
1203 wxLogLastError(wxT("TB_DELETEBUTTON"));
1204 }
1205 }
1206 #else
1207 if ( !::RemoveMenu(GetHmenu(), (UINT)MSWPositionForWxMenu(menu,pos), MF_BYPOSITION) )
1208 {
1209 wxLogLastError(wxT("RemoveMenu"));
1210 }
1211 #endif
1212
1213 #if wxUSE_ACCEL
1214 if ( menu->HasAccels() )
1215 {
1216 // need to rebuild accell table
1217 RebuildAccelTable();
1218 }
1219 #endif // wxUSE_ACCEL
1220
1221 Refresh();
1222 }
1223
1224
1225 m_titles.RemoveAt(pos);
1226
1227 return menu;
1228 }
1229
1230 #if wxUSE_ACCEL
1231
1232 void wxMenuBar::RebuildAccelTable()
1233 {
1234 // merge the accelerators of all menus into one accel table
1235 size_t nAccelCount = 0;
1236 size_t i, count = GetMenuCount();
1237 wxMenuList::iterator it;
1238 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1239 {
1240 nAccelCount += (*it)->GetAccelCount();
1241 }
1242
1243 if ( nAccelCount )
1244 {
1245 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
1246
1247 nAccelCount = 0;
1248 for ( i = 0, it = m_menus.begin(); i < count; i++, it++ )
1249 {
1250 nAccelCount += (*it)->CopyAccels(&accelEntries[nAccelCount]);
1251 }
1252
1253 m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
1254
1255 delete [] accelEntries;
1256 }
1257 }
1258
1259 #endif // wxUSE_ACCEL
1260
1261 void wxMenuBar::Attach(wxFrame *frame)
1262 {
1263 wxMenuBarBase::Attach(frame);
1264
1265 #if defined(WINCE_WITH_COMMANDBAR)
1266 if (!m_hMenu)
1267 this->Create();
1268 if (!m_commandBar)
1269 m_commandBar = (WXHWND) CommandBar_Create(wxGetInstance(), (HWND) frame->GetHWND(), NewControlId());
1270 if (m_commandBar)
1271 {
1272 if (m_hMenu)
1273 {
1274 if (!CommandBar_InsertMenubarEx((HWND) m_commandBar, NULL, (LPTSTR) m_hMenu, 0))
1275 {
1276 wxLogLastError(wxT("CommandBar_InsertMenubarEx"));
1277 }
1278 }
1279 }
1280 #endif
1281
1282 #if wxUSE_ACCEL
1283 RebuildAccelTable();
1284 #endif // wxUSE_ACCEL
1285 }
1286
1287 #if defined(WINCE_WITH_COMMANDBAR)
1288 bool wxMenuBar::AddAdornments(long style)
1289 {
1290 if (m_adornmentsAdded || !m_commandBar)
1291 return false;
1292
1293 if (style & wxCLOSE_BOX)
1294 {
1295 if (!CommandBar_AddAdornments((HWND) m_commandBar, 0, 0))
1296 wxLogLastError(wxT("CommandBar_AddAdornments"));
1297 else
1298 return true;
1299 }
1300 return false;
1301 }
1302 #endif
1303
1304 void wxMenuBar::Detach()
1305 {
1306 wxMenuBarBase::Detach();
1307 }
1308
1309 #endif // wxUSE_MENUS