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