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