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