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