]> git.saurik.com Git - wxWidgets.git/blob - src/msw/menu.cpp
wxBook additions; added a couple of pixels in menu drawing; taskbar
[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 and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
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 #ifndef WX_PRECOMP
32 #include "wx/frame.h"
33 #include "wx/menu.h"
34 #include "wx/utils.h"
35 #include "wx/intl.h"
36 #endif
37
38 #if wxUSE_OWNER_DRAWN
39 #include "wx/ownerdrw.h"
40 #endif
41
42 #include "wx/msw/private.h"
43 #include "wx/msw/menu.h"
44 #include "wx/menuitem.h"
45 #include "wx/log.h"
46
47 // other standard headers
48 #include <string.h>
49
50 // ----------------------------------------------------------------------------
51 // global variables
52 // ----------------------------------------------------------------------------
53
54 extern wxMenu *wxCurrentPopupMenu;
55
56 // ----------------------------------------------------------------------------
57 // constants
58 // ----------------------------------------------------------------------------
59
60 // the (popup) menu title has this special id
61 static const int idMenuTitle = -2;
62
63 // ----------------------------------------------------------------------------
64 // macros
65 // ----------------------------------------------------------------------------
66
67 #if !USE_SHARED_LIBRARY
68 IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
69 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler)
70 #endif
71
72 // ============================================================================
73 // implementation
74 // ============================================================================
75
76 // ---------------------------------------------------------------------------
77 // wxMenu construction, adding and removing menu items
78 // ---------------------------------------------------------------------------
79
80 // Construct a menu with optional title (then use append)
81 void wxMenu::Init(const wxString& title, const wxFunction func )
82 {
83 m_title = title;
84 m_parent = NULL;
85 m_eventHandler = this;
86 m_pInvokingWindow = NULL;
87 m_doBreak = FALSE ;
88 m_noItems = 0;
89 m_menuBar = NULL;
90 m_hMenu = (WXHMENU) CreatePopupMenu();
91 m_savehMenu = 0 ;
92 m_topLevelMenu = this;
93 m_clientData = (void*) NULL;
94
95 if ( !!m_title )
96 {
97 Append(idMenuTitle, m_title) ;
98 AppendSeparator() ;
99 }
100
101 Callback(func);
102 }
103
104 // The wxWindow destructor will take care of deleting the submenus.
105 wxMenu::~wxMenu()
106 {
107 // free Windows resources
108 if ( m_hMenu )
109 {
110 ::DestroyMenu((HMENU)m_hMenu);
111 m_hMenu = 0;
112 }
113
114 // delete submenus
115 wxNode *node = m_menuItems.First();
116 while ( node )
117 {
118 wxMenuItem *item = (wxMenuItem *)node->Data();
119
120 // Delete child menus.
121 // Beware: they must not be appended to children list!!!
122 // (because order of delete is significant)
123 if ( item->IsSubMenu() )
124 item->DeleteSubMenu();
125
126 wxNode *next = node->Next();
127 delete item;
128 delete node;
129 node = next;
130 }
131 }
132
133 void wxMenu::Break()
134 {
135 m_doBreak = TRUE;
136 }
137
138 // function appends a new item or submenu to the menu
139 void wxMenu::Append(wxMenuItem *pItem)
140 {
141 wxCHECK_RET( pItem != NULL, _T("can't append NULL item to the menu") );
142
143 #if wxUSE_ACCEL
144 // check for accelerators: they are given after '\t'
145 wxString label = pItem->GetName();
146 int posTab = label.Find(_T('\t'));
147 if ( posTab != wxNOT_FOUND ) {
148 // parse the accelerator string
149 int keyCode = 0;
150 int accelFlags = wxACCEL_NORMAL;
151 wxString current;
152 for ( size_t n = (size_t)posTab + 1; n < label.Len(); n++ ) {
153 if ( (label[n] == '+') || (label[n] == '-') ) {
154 if ( current == _("ctrl") )
155 accelFlags |= wxACCEL_CTRL;
156 else if ( current == _("alt") )
157 accelFlags |= wxACCEL_ALT;
158 else if ( current == _("shift") )
159 accelFlags |= wxACCEL_SHIFT;
160 else {
161 wxLogDebug(_T("Unknown accel modifier: '%s'"),
162 current.c_str());
163 }
164
165 current.Empty();
166 }
167 else {
168 current += wxTolower(label[n]);
169 }
170 }
171
172 if ( current.IsEmpty() ) {
173 wxLogDebug(_T("No accel key found, accel string ignored."));
174 }
175 else {
176 if ( current.Len() == 1 ) {
177 // it's a letter
178 keyCode = wxToupper(current[0U]);
179 }
180 else {
181 // it should be a function key
182 if ( current[0U] == 'f' && isdigit(current[1U]) &&
183 (current.Len() == 2 ||
184 (current.Len() == 3 && isdigit(current[2U]))) ) {
185 int n;
186 wxSscanf(current.c_str() + 1, _T("%d"), &n);
187
188 keyCode = VK_F1 + n - 1;
189 }
190 else {
191 wxLogDebug(_T("Unrecognized accel key '%s', accel "
192 "string ignored."), current.c_str());
193 }
194 }
195 }
196
197 if ( keyCode ) {
198 // do add an entry
199 m_accelKeyCodes.Add(keyCode);
200 m_accelFlags.Add(accelFlags);
201 m_accelIds.Add(pItem->GetId());
202 }
203 }
204 #endif // wxUSE_ACCEL
205
206 UINT flags = 0;
207
208 // if "Break" has just been called, insert a menu break before this item
209 // (and don't forget to reset the flag)
210 if ( m_doBreak ) {
211 flags |= MF_MENUBREAK;
212 m_doBreak = FALSE;
213 }
214
215 if ( pItem->IsSeparator() ) {
216 flags |= MF_SEPARATOR;
217 }
218
219 // id is the numeric id for normal menu items and HMENU for submenus as
220 // required by ::AppendMenu() API
221 UINT id;
222 wxMenu *submenu = pItem->GetSubMenu();
223 if ( submenu != NULL ) {
224 wxASSERT( submenu->GetHMenu() != (WXHMENU) NULL );
225
226 id = (UINT)submenu->GetHMenu();
227 submenu->m_topLevelMenu = m_topLevelMenu;
228 submenu->m_parent = this;
229 submenu->m_savehMenu = (WXHMENU)id;
230 submenu->m_hMenu = 0;
231
232 flags |= MF_POPUP;
233 }
234 else {
235 id = pItem->GetId();
236 }
237
238 LPCTSTR pData;
239
240 #if wxUSE_OWNER_DRAWN
241 if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
242 // item draws itself, pass pointer to it in data parameter
243 flags |= MF_OWNERDRAW;
244 pData = (LPCTSTR)pItem;
245 }
246 else
247 #endif
248 {
249 // menu is just a normal string (passed in data parameter)
250 flags |= MF_STRING;
251 pData = label;
252 }
253
254 if ( !::AppendMenu(GetHmenu(), flags, id, pData) )
255 {
256 wxLogLastError("AppendMenu");
257 }
258 else
259 {
260 #ifdef __WIN32__
261 if ( id == idMenuTitle )
262 {
263 // visually select the menu title
264 MENUITEMINFO mii;
265 mii.cbSize = sizeof(mii);
266 mii.fMask = MIIM_STATE;
267 mii.fState = MFS_DEFAULT;
268
269 if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id, FALSE, &mii) )
270 {
271 wxLogLastError(_T("SetMenuItemInfo"));
272 }
273 }
274 #endif // __WIN32__
275
276 m_menuItems.Append(pItem);
277 m_noItems++;
278 }
279 }
280
281 void wxMenu::AppendSeparator()
282 {
283 Append(new wxMenuItem(this, ID_SEPARATOR));
284 }
285
286 // Pullright item
287 void wxMenu::Append(int id,
288 const wxString& label,
289 wxMenu *SubMenu,
290 const wxString& helpString)
291 {
292 Append(new wxMenuItem(this, id, label, helpString, FALSE, SubMenu));
293 }
294
295 // Ordinary menu item
296 void wxMenu::Append(int id,
297 const wxString& label,
298 const wxString& helpString,
299 bool checkable)
300 {
301 // 'checkable' parameter is useless for Windows.
302 Append(new wxMenuItem(this, id, label, helpString, checkable));
303 }
304
305 // delete item by id
306 void wxMenu::Delete(int id)
307 {
308 wxMenuItem *item = NULL;
309 int pos;
310 wxNode *node;
311 for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++)
312 {
313 item = (wxMenuItem *)node->Data();
314 if ( item->GetId() == id )
315 break;
316 }
317
318 wxCHECK_RET( node, _T("wxMenu::Delete(): item doesn't exist") );
319
320 HMENU menu = GetHmenu();
321
322 wxMenu *pSubMenu = item->GetSubMenu();
323 if ( pSubMenu != NULL ) {
324 RemoveMenu(menu, (UINT)pos, MF_BYPOSITION);
325 pSubMenu->m_hMenu = pSubMenu->m_savehMenu;
326 pSubMenu->m_savehMenu = 0;
327 pSubMenu->m_parent = NULL;
328 // RemoveChild(item->subMenu);
329 pSubMenu->m_topLevelMenu = NULL;
330 // TODO: Why isn't subMenu deleted here???
331 // Will put this in for now. Assuming this is supposed
332 // to delete the menu, not just remove it.
333 item->DeleteSubMenu();
334 }
335 else {
336 DeleteMenu(menu, (UINT)pos, MF_BYPOSITION);
337 }
338
339 m_menuItems.DeleteNode(node);
340 delete item;
341 }
342
343 #if wxUSE_ACCEL
344
345 // ---------------------------------------------------------------------------
346 // accelerator helpers
347 // ---------------------------------------------------------------------------
348
349 // create the wxAcceleratorEntries for our accels and put them into provided
350 // array - return the number of accels we have
351 size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
352 {
353 size_t count = GetAccelCount();
354 for ( size_t n = 0; n < count; n++ )
355 {
356 (*accels++).Set(m_accelFlags[n], m_accelKeyCodes[n], m_accelIds[n]);
357 }
358
359 return count;
360 }
361
362 #endif // wxUSE_ACCEL
363
364 // ---------------------------------------------------------------------------
365 // wxMenu functions implemented in wxMenuItem
366 // ---------------------------------------------------------------------------
367
368 void wxMenu::Enable(int id, bool Flag)
369 {
370 wxMenuItem *item = FindItemForId(id);
371 wxCHECK_RET( item != NULL, _T("can't enable non-existing menu item") );
372
373 item->Enable(Flag);
374 }
375
376 bool wxMenu::IsEnabled(int id) const
377 {
378 wxMenuItem *item = FindItemForId(id);
379 wxCHECK_MSG( item != NULL, FALSE, _T("invalid item id") );
380
381 return item->IsEnabled();
382 }
383
384 void wxMenu::Check(int id, bool Flag)
385 {
386 wxMenuItem *item = FindItemForId(id);
387 wxCHECK_RET( item != NULL, _T("can't get status of non-existing menu item") );
388
389 item->Check(Flag);
390 }
391
392 bool wxMenu::IsChecked(int id) const
393 {
394 wxMenuItem *item = FindItemForId(id);
395 wxCHECK_MSG( item != NULL, FALSE, _T("invalid item id") );
396
397 return item->IsChecked();
398 }
399
400 void wxMenu::SetLabel(int id, const wxString& label)
401 {
402 wxMenuItem *item = FindItemForId(id) ;
403 wxCHECK_RET( item, _T("wxMenu::SetLabel: no such item") );
404
405 item->SetName(label);
406 }
407
408 wxString wxMenu::GetLabel(int id) const
409 {
410 wxString label;
411 wxMenuItem *pItem = FindItemForId(id) ;
412 if (pItem)
413 label = pItem->GetName() ;
414 else
415 wxFAIL_MSG(_T("wxMenu::GetLabel: item doesn't exist"));
416
417 return label;
418 }
419
420 void wxMenu::SetHelpString(int itemId, const wxString& helpString)
421 {
422 wxMenuItem *item = FindItemForId (itemId);
423 if (item)
424 item->SetHelp(helpString);
425 else
426 wxFAIL_MSG(_T("wxMenu::SetHelpString: item doesn't exist"));
427 }
428
429 wxString wxMenu::GetHelpString (int itemId) const
430 {
431 wxString help;
432 wxMenuItem *item = FindItemForId (itemId);
433 if (item)
434 help = item->GetHelp();
435 else
436 wxFAIL_MSG(_T("wxMenu::GetHelpString: item doesn't exist"));
437
438 return help;
439 }
440
441 // ---------------------------------------------------------------------------
442 // wxMenu title
443 // ---------------------------------------------------------------------------
444
445 void wxMenu::SetTitle(const wxString& label)
446 {
447 bool hasNoTitle = m_title.IsEmpty();
448 m_title = label;
449
450 HMENU hMenu = GetHmenu();
451
452 if ( hasNoTitle )
453 {
454 if ( !label.IsEmpty() )
455 {
456 if ( !InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
457 (unsigned)idMenuTitle, m_title) ||
458 !InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
459 {
460 wxLogLastError(_T("InsertMenu"));
461 }
462 }
463 }
464 else
465 {
466 if ( label.IsEmpty() )
467 {
468 // remove the title and the separator after it
469 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
470 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
471 {
472 wxLogLastError("RemoveMenu");
473 }
474 }
475 else
476 {
477 // modify the title
478 if ( !ModifyMenu(hMenu, 0u,
479 MF_BYPOSITION | MF_STRING,
480 (unsigned)idMenuTitle, m_title) )
481 {
482 wxLogLastError("ModifyMenu");
483 }
484 }
485 }
486
487 #ifdef __WIN32__
488 // put the title string in bold face
489 if ( !m_title.IsEmpty() )
490 {
491 MENUITEMINFO mii;
492 mii.cbSize = sizeof(mii);
493 mii.fMask = MIIM_STATE;
494 mii.fState = MFS_DEFAULT;
495
496 if ( !SetMenuItemInfo(hMenu, (unsigned)idMenuTitle, FALSE, &mii) )
497 {
498 wxLogLastError("SetMenuItemInfo");
499 }
500 }
501 #endif
502 }
503
504 const wxString wxMenu::GetTitle() const
505 {
506 return m_title;
507 }
508
509 // ---------------------------------------------------------------------------
510 // event processing
511 // ---------------------------------------------------------------------------
512
513 bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id)
514 {
515 // ignore commands from the menu title
516
517 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
518 if ( id != (WXWORD)idMenuTitle )
519 {
520 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED);
521 event.SetEventObject( this );
522 event.SetId( id );
523 event.SetInt( id );
524 ProcessCommand(event);
525 }
526
527 return TRUE;
528 }
529
530 bool wxMenu::ProcessCommand(wxCommandEvent & event)
531 {
532 bool processed = FALSE;
533
534 // Try a callback
535 if (m_callback)
536 {
537 (void)(*(m_callback))(*this, event);
538 processed = TRUE;
539 }
540
541 // Try the menu's event handler
542 if ( !processed && GetEventHandler())
543 {
544 processed = GetEventHandler()->ProcessEvent(event);
545 }
546
547 // Try the window the menu was popped up from (and up through the
548 // hierarchy)
549 wxWindow *win = GetInvokingWindow();
550 if ( !processed && win )
551 processed = win->GetEventHandler()->ProcessEvent(event);
552
553 return processed;
554 }
555
556 // ---------------------------------------------------------------------------
557 // Item search
558 // ---------------------------------------------------------------------------
559
560 // Finds the item id matching the given string, -1 if not found.
561 int wxMenu::FindItem (const wxString& itemString) const
562 {
563 wxString itemLabel = wxStripMenuCodes(itemString);
564 for ( wxNode *node = m_menuItems.First(); node; node = node->Next() )
565 {
566 wxMenuItem *item = (wxMenuItem *)node->Data();
567 if ( item->IsSubMenu() )
568 {
569 int ans = item->GetSubMenu()->FindItem(itemString);
570 if ( ans != wxNOT_FOUND )
571 return ans;
572 }
573 else if ( !item->IsSeparator() )
574 {
575 wxString label = wxStripMenuCodes(item->GetName());
576 if ( itemLabel == label )
577 return item->GetId();
578 }
579 }
580
581 return wxNOT_FOUND;
582 }
583
584 wxMenuItem *wxMenu::FindItemForId(int itemId, wxMenu ** itemMenu) const
585 {
586 if ( itemMenu )
587 *itemMenu = NULL;
588
589 wxMenuItem *item = NULL;
590 for ( wxNode *node = m_menuItems.First(); node && !item; node = node->Next() )
591 {
592 item = (wxMenuItem *)node->Data();
593
594 if ( item->GetId() == itemId )
595 {
596 if (itemMenu)
597 *itemMenu = (wxMenu *)this;
598 }
599 else if ( item->IsSubMenu() )
600 {
601 item = item->GetSubMenu()->FindItemForId(itemId, itemMenu);
602 }
603 else
604 {
605 // don't exit the loop
606 item = NULL;
607 }
608 }
609
610 return item;
611 }
612
613 // ---------------------------------------------------------------------------
614 // other
615 // ---------------------------------------------------------------------------
616
617 void wxMenu::Attach(wxMenuBar *menubar)
618 {
619 // menu can be in at most one menubar because otherwise they would both
620 // delete the menu pointer
621 wxASSERT_MSG( !m_menuBar, _T("menu belongs to 2 menubars, expect a crash") );
622
623 m_menuBar = menubar;
624 m_savehMenu = m_hMenu;
625 m_hMenu = 0;
626 }
627
628 void wxMenu::Detach()
629 {
630 wxASSERT_MSG( m_menuBar, _T("can't detach menu if it's not attached") );
631
632 m_hMenu = m_savehMenu;
633 m_savehMenu = 0;
634 }
635
636 // ---------------------------------------------------------------------------
637 // Menu Bar
638 // ---------------------------------------------------------------------------
639
640 void wxMenuBar::Init()
641 {
642 m_eventHandler = this;
643 m_menuCount = 0;
644 m_menus = NULL;
645 m_titles = NULL;
646 m_menuBarFrame = NULL;
647 m_hMenu = 0;
648 }
649
650 wxMenuBar::wxMenuBar()
651 {
652 Init();
653 }
654
655 wxMenuBar::wxMenuBar( long WXUNUSED(style) )
656 {
657 Init();
658 }
659
660 wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[])
661 {
662 Init();
663
664 m_menuCount = count;
665 m_menus = menus;
666 m_titles = new wxString[count];
667
668 int i;
669 for ( i = 0; i < count; i++ )
670 m_titles[i] = titles[i];
671
672 for ( i = 0; i < count; i++ )
673 m_menus[i]->Attach(this);
674 }
675
676 wxMenuBar::~wxMenuBar()
677 {
678 for ( int i = 0; i < m_menuCount; i++ )
679 {
680 delete m_menus[i];
681 }
682
683 delete[] m_menus;
684 delete[] m_titles;
685 }
686
687 // ---------------------------------------------------------------------------
688 // wxMenuBar helpers
689 // ---------------------------------------------------------------------------
690
691 void wxMenuBar::Refresh()
692 {
693 wxCHECK_RET( m_menuBarFrame, _T("can't refresh a menubar withotu a frame") );
694
695 DrawMenuBar((HWND)m_menuBarFrame->GetHWND()) ;
696 }
697
698 WXHMENU wxMenuBar::Create()
699 {
700 wxCHECK_MSG( !m_hMenu, TRUE, _T("menubar already created") );
701
702 m_hMenu = (WXHMENU)::CreateMenu();
703
704 if ( !m_hMenu )
705 {
706 wxLogLastError("CreateMenu");
707 }
708 else
709 {
710 for ( int i = 0; i < m_menuCount; i++ )
711 {
712 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
713 (UINT)m_menus[i]->GetHMenu(),
714 m_titles[i]) )
715 {
716 wxLogLastError("AppendMenu");
717 }
718 }
719 }
720
721 return m_hMenu;
722 }
723
724 // ---------------------------------------------------------------------------
725 // wxMenuBar functions forwarded to wxMenuItem
726 // ---------------------------------------------------------------------------
727
728 // Must only be used AFTER menu has been attached to frame,
729 // otherwise use individual menus to enable/disable items
730 void wxMenuBar::Enable(int id, bool enable)
731 {
732 wxMenu *itemMenu = NULL;
733 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
734
735 wxCHECK_RET( item, _T("attempt to enable an item which doesn't exist") );
736
737 item->Enable(enable);
738 }
739
740 void wxMenuBar::EnableTop(int pos, bool enable)
741 {
742 int flag = enable ? MF_ENABLED : MF_GRAYED;;
743
744 EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag);
745 }
746
747 // Must only be used AFTER menu has been attached to frame,
748 // otherwise use individual menus
749 void wxMenuBar::Check(int id, bool check)
750 {
751 wxMenu *itemMenu = NULL;
752 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
753
754 wxCHECK_RET( item, _T("attempt to check an item which doesn't exist") );
755 wxCHECK_RET( item->IsCheckable(), _T("attempt to check an uncheckable item") );
756
757 item->Check(check);
758 }
759
760 bool wxMenuBar::IsChecked(int id) const
761 {
762 wxMenu *itemMenu = NULL;
763 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
764
765 wxCHECK_MSG( item, FALSE, _T("wxMenuBar::IsChecked(): no such item") );
766
767 int flag = ::GetMenuState(GetHmenuOf(itemMenu), id, MF_BYCOMMAND);
768
769 return (flag & MF_CHECKED) != 0;
770 }
771
772 bool wxMenuBar::IsEnabled(int id) const
773 {
774 wxMenu *itemMenu = NULL;
775 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
776
777 wxCHECK_MSG( item, FALSE, _T("wxMenuBar::IsEnabled(): no such item") );
778
779 int flag = ::GetMenuState(GetHmenuOf(itemMenu), id, MF_BYCOMMAND) ;
780
781 // don't "and" with MF_ENABLED because its value is 0
782 return (flag & MF_DISABLED) == 0;
783 }
784
785 void wxMenuBar::SetLabel(int id, const wxString& label)
786 {
787 wxMenu *itemMenu = NULL;
788 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
789
790 wxCHECK_RET( item, _T("wxMenuBar::SetLabel(): no such item") );
791
792 item->SetName(label);
793 }
794
795 wxString wxMenuBar::GetLabel(int id) const
796 {
797 wxMenu *itemMenu = NULL;
798 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
799
800 wxCHECK_MSG( item, _T(""), _T("wxMenuBar::GetLabel(): no such item") );
801
802 return item->GetName();
803 }
804
805 void wxMenuBar::SetHelpString (int id, const wxString& helpString)
806 {
807 wxMenu *itemMenu = NULL;
808 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
809
810 wxCHECK_RET( item, _T("wxMenuBar::SetHelpString(): no such item") );
811
812 item->SetHelp(helpString);
813 }
814
815 wxString wxMenuBar::GetHelpString (int id) const
816 {
817 wxMenu *itemMenu = NULL;
818 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
819
820 wxCHECK_MSG( item, _T(""), _T("wxMenuBar::GetHelpString(): no such item") );
821
822 return item->GetHelp();
823 }
824
825 // ---------------------------------------------------------------------------
826 // wxMenuBar functions to work with the top level submenus
827 // ---------------------------------------------------------------------------
828
829 // NB: we don't support owner drawn top level items for now, if we do these
830 // functions would have to be changed to use wxMenuItem as well
831
832 void wxMenuBar::SetLabelTop(int pos, const wxString& label)
833 {
834 UINT id;
835 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
836 if ( flagsOld == 0xFFFFFFFF )
837 {
838 wxLogLastError(_T("GetMenuState"));
839
840 return;
841 }
842
843 if ( flagsOld & MF_POPUP )
844 {
845 // HIBYTE contains the number of items in the submenu in this case
846 flagsOld &= 0xff ;
847 id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos) ;
848 }
849 else
850 {
851 id = pos;
852 }
853
854 if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld,
855 id, label) == 0xFFFFFFFF )
856 {
857 wxLogLastError("ModifyMenu");
858 }
859 }
860
861 wxString wxMenuBar::GetLabelTop(int pos) const
862 {
863 int len = ::GetMenuString((HMENU)m_hMenu, pos, NULL, 0, MF_BYCOMMAND);
864
865 len++; // for the NUL character
866 wxString label;
867 ::GetMenuString(GetHmenu(), pos, label.GetWriteBuf(len), len, MF_BYCOMMAND);
868 label.UngetWriteBuf();
869
870 return label;
871 }
872
873 // ---------------------------------------------------------------------------
874 // wxMenuBar notifications
875 // ---------------------------------------------------------------------------
876
877 bool wxMenuBar::OnDelete(wxMenu *a_menu, int pos)
878 {
879 if ( !m_menuBarFrame )
880 return TRUE;
881
882 if ( ::RemoveMenu((HMENU)m_hMenu, (UINT)pos, MF_BYPOSITION) )
883 {
884 // VZ: I'm not sure about what's going on here, so I leave an assert
885 wxASSERT_MSG( m_menus[pos] == a_menu, _T("what is this parameter for??") );
886
887 a_menu->Detach();
888
889 if ( m_menuBarFrame )
890 Refresh();
891
892 return TRUE;
893 }
894 else
895 {
896 wxLogLastError("RemoveMenu");
897 }
898
899 return FALSE;
900 }
901
902 bool wxMenuBar::OnAppend(wxMenu *a_menu, const wxChar *title)
903 {
904 WXHMENU submenu = a_menu->GetHMenu();
905 if ( !submenu )
906 return FALSE;
907
908 if ( !m_menuBarFrame )
909 return TRUE;
910
911 a_menu->Attach(this);
912
913 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
914 (UINT)submenu, title) )
915 {
916 wxLogLastError(_T("AppendMenu"));
917 }
918
919 Refresh();
920
921 return TRUE;
922 }
923
924 // ---------------------------------------------------------------------------
925 // wxMenuBar construction
926 // ---------------------------------------------------------------------------
927
928 void wxMenuBar::Append (wxMenu * menu, const wxString& title)
929 {
930 if (!OnAppend(menu, title))
931 return;
932
933 m_menuCount ++;
934 wxMenu **new_menus = new wxMenu *[m_menuCount];
935 wxString *new_titles = new wxString[m_menuCount];
936 int i;
937
938 for (i = 0; i < m_menuCount - 1; i++)
939 {
940 new_menus[i] = m_menus[i];
941 m_menus[i] = NULL;
942 new_titles[i] = m_titles[i];
943 m_titles[i] = _T("");
944 }
945 if (m_menus)
946 {
947 delete[]m_menus;
948 delete[]m_titles;
949 }
950 m_menus = new_menus;
951 m_titles = new_titles;
952
953 m_menus[m_menuCount - 1] = (wxMenu *)menu;
954 m_titles[m_menuCount - 1] = title;
955
956 menu->SetParent(this);
957 }
958
959 void wxMenuBar::Delete(wxMenu * menu, int i)
960 {
961 int j;
962 int ii = (int) i;
963
964 if (menu != 0) {
965 for (ii = 0; ii < m_menuCount; ii++) {
966 if (m_menus[ii] == menu)
967 break;
968 }
969 if (ii >= m_menuCount)
970 return;
971 } else {
972 if (ii < 0 || ii >= m_menuCount)
973 return;
974 menu = m_menus[ii];
975 }
976
977 if (!OnDelete(menu, ii))
978 return;
979
980 menu->SetParent(NULL);
981
982 -- m_menuCount;
983 for (j = ii; j < m_menuCount; j++) {
984 m_menus[j] = m_menus[j + 1];
985 m_titles[j] = m_titles[j + 1];
986 }
987 }
988
989 void wxMenuBar::Attach(wxFrame *frame)
990 {
991 wxASSERT_MSG( !m_menuBarFrame, _T("menubar already attached!") );
992
993 m_menuBarFrame = frame;
994
995 #if wxUSE_ACCEL
996 // create the accel table - we consider that the menubar construction is
997 // finished
998 size_t nAccelCount = 0;
999 int i;
1000 for ( i = 0; i < m_menuCount; i++ )
1001 {
1002 nAccelCount += m_menus[i]->GetAccelCount();
1003 }
1004
1005 if ( nAccelCount )
1006 {
1007 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
1008
1009 nAccelCount = 0;
1010 for ( i = 0; i < m_menuCount; i++ )
1011 {
1012 nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]);
1013 }
1014
1015 m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
1016
1017 delete [] accelEntries;
1018 }
1019 #endif // wxUSE_ACCEL
1020 }
1021
1022 // ---------------------------------------------------------------------------
1023 // wxMenuBar searching for menu items
1024 // ---------------------------------------------------------------------------
1025
1026 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
1027 int wxMenuBar::FindMenuItem(const wxString& menuString,
1028 const wxString& itemString) const
1029 {
1030 wxString menuLabel = wxStripMenuCodes(menuString);
1031 for ( int i = 0; i < m_menuCount; i++ )
1032 {
1033 wxString title = wxStripMenuCodes(m_titles[i]);
1034 if ( menuString == title )
1035 return m_menus[i]->FindItem(itemString);
1036 }
1037
1038 return wxNOT_FOUND;
1039 }
1040
1041 wxMenuItem *wxMenuBar::FindItemForId (int id, wxMenu **itemMenu) const
1042 {
1043 if ( itemMenu )
1044 *itemMenu = NULL;
1045
1046 wxMenuItem *item = NULL;
1047 for ( int i = 0; !item && (i < m_menuCount); i++ )
1048 {
1049 item = m_menus[i]->FindItemForId(id, itemMenu);
1050 }
1051
1052 return item;
1053 }
1054
1055
1056 // ----------------------------------------------------------------------------
1057 // helper functions
1058 // ----------------------------------------------------------------------------
1059
1060 wxWindow *wxMenu::GetWindow() const
1061 {
1062 if ( m_pInvokingWindow != NULL )
1063 return m_pInvokingWindow;
1064 else if ( m_menuBar != NULL)
1065 return m_menuBar->GetFrame();
1066
1067 return NULL;
1068 }
1069
1070 WXHMENU wxMenu::GetHMenu() const
1071 {
1072 if ( m_hMenu != 0 )
1073 return m_hMenu;
1074 else if ( m_savehMenu != 0 )
1075 return m_savehMenu;
1076
1077 wxFAIL_MSG(_T("wxMenu without HMENU"));
1078
1079 return 0;
1080 }
1081
1082 // Update a menu and all submenus recursively. source is the object that has
1083 // the update event handlers defined for it. If NULL, the menu or associated
1084 // window will be used.
1085 void wxMenu::UpdateUI(wxEvtHandler* source)
1086 {
1087 if (!source && GetInvokingWindow())
1088 source = GetInvokingWindow()->GetEventHandler();
1089 if (!source)
1090 source = GetEventHandler();
1091 if (!source)
1092 source = this;
1093
1094 wxNode* node = GetItems().First();
1095 while (node)
1096 {
1097 wxMenuItem* item = (wxMenuItem*) node->Data();
1098 if ( !item->IsSeparator() )
1099 {
1100 wxWindowID id = item->GetId();
1101 wxUpdateUIEvent event(id);
1102 event.SetEventObject( source );
1103
1104 if (source->ProcessEvent(event))
1105 {
1106 if (event.GetSetText())
1107 SetLabel(id, event.GetText());
1108 if (event.GetSetChecked())
1109 Check(id, event.GetChecked());
1110 if (event.GetSetEnabled())
1111 Enable(id, event.GetEnabled());
1112 }
1113
1114 if (item->GetSubMenu())
1115 item->GetSubMenu()->UpdateUI(source);
1116 }
1117 node = node->Next();
1118 }
1119 }