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