]> git.saurik.com Git - wxWidgets.git/blob - src/msw/menu.cpp
wxControl and wxChoice derive from the base classes under wxGTK too now
[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, wxT("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(wxT('\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(wxT("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(wxT("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 // is it 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, wxT("%d"), &n);
187
188 keyCode = VK_F1 + n - 1;
189 }
190 else {
191 // several special cases
192 current.MakeUpper();
193 if ( current == wxT("DEL") ) {
194 keyCode = VK_DELETE;
195 }
196 else if ( current == wxT("PGUP") ) {
197 keyCode = VK_PRIOR;
198 }
199 else if ( current == wxT("PGDN") ) {
200 keyCode = VK_NEXT;
201 }
202 else {
203 wxLogDebug(wxT("Unrecognized accel key '%s', accel "
204 "string ignored."), current.c_str());
205 }
206 }
207 }
208 }
209
210 if ( keyCode ) {
211 // do add an entry
212 m_accelKeyCodes.Add(keyCode);
213 m_accelFlags.Add(accelFlags);
214 m_accelIds.Add(pItem->GetId());
215 }
216 }
217 #endif // wxUSE_ACCEL
218
219 UINT flags = 0;
220
221 // if "Break" has just been called, insert a menu break before this item
222 // (and don't forget to reset the flag)
223 if ( m_doBreak ) {
224 flags |= MF_MENUBREAK;
225 m_doBreak = FALSE;
226 }
227
228 if ( pItem->IsSeparator() ) {
229 flags |= MF_SEPARATOR;
230 }
231
232 // id is the numeric id for normal menu items and HMENU for submenus as
233 // required by ::AppendMenu() API
234 UINT id;
235 wxMenu *submenu = pItem->GetSubMenu();
236 if ( submenu != NULL ) {
237 wxASSERT( submenu->GetHMenu() != (WXHMENU) NULL );
238
239 id = (UINT)submenu->GetHMenu();
240 submenu->m_topLevelMenu = m_topLevelMenu;
241 submenu->m_parent = this;
242 submenu->m_savehMenu = (WXHMENU)id;
243 submenu->m_hMenu = 0;
244
245 flags |= MF_POPUP;
246 }
247 else {
248 id = pItem->GetId();
249 }
250
251 LPCTSTR pData;
252
253 #if wxUSE_OWNER_DRAWN
254 if ( pItem->IsOwnerDrawn() ) { // want to get {Measure|Draw}Item messages?
255 // item draws itself, pass pointer to it in data parameter
256 flags |= MF_OWNERDRAW;
257 pData = (LPCTSTR)pItem;
258 }
259 else
260 #endif
261 {
262 // menu is just a normal string (passed in data parameter)
263 flags |= MF_STRING;
264 pData = label;
265 }
266
267 if ( !::AppendMenu(GetHmenu(), flags, id, pData) )
268 {
269 wxLogLastError("AppendMenu");
270 }
271 else
272 {
273 #ifdef __WIN32__
274 if ( (int)id == idMenuTitle )
275 {
276 // visually select the menu title
277 MENUITEMINFO mii;
278 mii.cbSize = sizeof(mii);
279 mii.fMask = MIIM_STATE;
280 mii.fState = MFS_DEFAULT;
281
282 if ( !SetMenuItemInfo(GetHmenu(), (unsigned)id, FALSE, &mii) )
283 {
284 wxLogLastError(wxT("SetMenuItemInfo"));
285 }
286 }
287 #endif // __WIN32__
288
289 m_menuItems.Append(pItem);
290 m_noItems++;
291 }
292 }
293
294 void wxMenu::AppendSeparator()
295 {
296 Append(new wxMenuItem(this, ID_SEPARATOR));
297 }
298
299 // Pullright item
300 void wxMenu::Append(int id,
301 const wxString& label,
302 wxMenu *SubMenu,
303 const wxString& helpString)
304 {
305 Append(new wxMenuItem(this, id, label, helpString, FALSE, SubMenu));
306 }
307
308 // Ordinary menu item
309 void wxMenu::Append(int id,
310 const wxString& label,
311 const wxString& helpString,
312 bool checkable)
313 {
314 // 'checkable' parameter is useless for Windows.
315 Append(new wxMenuItem(this, id, label, helpString, checkable));
316 }
317
318 // delete item by id
319 void wxMenu::Delete(int id)
320 {
321 wxMenuItem *item = NULL;
322 int pos;
323 wxNode *node;
324 for (pos = 0, node = m_menuItems.First(); node; node = node->Next(), pos++)
325 {
326 item = (wxMenuItem *)node->Data();
327 if ( item->GetId() == id )
328 break;
329 }
330
331 wxCHECK_RET( node, wxT("wxMenu::Delete(): item doesn't exist") );
332
333 HMENU menu = GetHmenu();
334
335 wxMenu *pSubMenu = item->GetSubMenu();
336 if ( pSubMenu != NULL ) {
337 RemoveMenu(menu, (UINT)pos, MF_BYPOSITION);
338 pSubMenu->m_hMenu = pSubMenu->m_savehMenu;
339 pSubMenu->m_savehMenu = 0;
340 pSubMenu->m_parent = NULL;
341 // RemoveChild(item->subMenu);
342 pSubMenu->m_topLevelMenu = NULL;
343 // TODO: Why isn't subMenu deleted here???
344 // Will put this in for now. Assuming this is supposed
345 // to delete the menu, not just remove it.
346 item->DeleteSubMenu();
347 }
348 else {
349 DeleteMenu(menu, (UINT)pos, MF_BYPOSITION);
350 }
351
352 m_menuItems.DeleteNode(node);
353 delete item;
354 }
355
356 #if wxUSE_ACCEL
357
358 // ---------------------------------------------------------------------------
359 // accelerator helpers
360 // ---------------------------------------------------------------------------
361
362 // create the wxAcceleratorEntries for our accels and put them into provided
363 // array - return the number of accels we have
364 size_t wxMenu::CopyAccels(wxAcceleratorEntry *accels) const
365 {
366 size_t count = GetAccelCount();
367 for ( size_t n = 0; n < count; n++ )
368 {
369 (*accels++).Set(m_accelFlags[n], m_accelKeyCodes[n], m_accelIds[n]);
370 }
371
372 return count;
373 }
374
375 #endif // wxUSE_ACCEL
376
377 // ---------------------------------------------------------------------------
378 // wxMenu functions implemented in wxMenuItem
379 // ---------------------------------------------------------------------------
380
381 void wxMenu::Enable(int id, bool Flag)
382 {
383 wxMenuItem *item = FindItemForId(id);
384 wxCHECK_RET( item != NULL, wxT("can't enable non-existing menu item") );
385
386 item->Enable(Flag);
387 }
388
389 bool wxMenu::IsEnabled(int id) const
390 {
391 wxMenuItem *item = FindItemForId(id);
392 wxCHECK_MSG( item != NULL, FALSE, wxT("invalid item id") );
393
394 return item->IsEnabled();
395 }
396
397 void wxMenu::Check(int id, bool Flag)
398 {
399 wxMenuItem *item = FindItemForId(id);
400 wxCHECK_RET( item != NULL, wxT("can't get status of non-existing menu item") );
401
402 item->Check(Flag);
403 }
404
405 bool wxMenu::IsChecked(int id) const
406 {
407 wxMenuItem *item = FindItemForId(id);
408 wxCHECK_MSG( item != NULL, FALSE, wxT("invalid item id") );
409
410 return item->IsChecked();
411 }
412
413 void wxMenu::SetLabel(int id, const wxString& label)
414 {
415 wxMenuItem *item = FindItemForId(id) ;
416 wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") );
417
418 item->SetName(label);
419 }
420
421 wxString wxMenu::GetLabel(int id) const
422 {
423 wxString label;
424 wxMenuItem *pItem = FindItemForId(id) ;
425 if (pItem)
426 label = pItem->GetName() ;
427 else
428 wxFAIL_MSG(wxT("wxMenu::GetLabel: item doesn't exist"));
429
430 return label;
431 }
432
433 void wxMenu::SetHelpString(int itemId, const wxString& helpString)
434 {
435 wxMenuItem *item = FindItemForId (itemId);
436 if (item)
437 item->SetHelp(helpString);
438 else
439 wxFAIL_MSG(wxT("wxMenu::SetHelpString: item doesn't exist"));
440 }
441
442 wxString wxMenu::GetHelpString (int itemId) const
443 {
444 wxString help;
445 wxMenuItem *item = FindItemForId (itemId);
446 if (item)
447 help = item->GetHelp();
448 else
449 wxFAIL_MSG(wxT("wxMenu::GetHelpString: item doesn't exist"));
450
451 return help;
452 }
453
454 // ---------------------------------------------------------------------------
455 // wxMenu title
456 // ---------------------------------------------------------------------------
457
458 void wxMenu::SetTitle(const wxString& label)
459 {
460 bool hasNoTitle = m_title.IsEmpty();
461 m_title = label;
462
463 HMENU hMenu = GetHmenu();
464
465 if ( hasNoTitle )
466 {
467 if ( !label.IsEmpty() )
468 {
469 if ( !InsertMenu(hMenu, 0u, MF_BYPOSITION | MF_STRING,
470 (unsigned)idMenuTitle, m_title) ||
471 !InsertMenu(hMenu, 1u, MF_BYPOSITION, (unsigned)-1, NULL) )
472 {
473 wxLogLastError(wxT("InsertMenu"));
474 }
475 }
476 }
477 else
478 {
479 if ( label.IsEmpty() )
480 {
481 // remove the title and the separator after it
482 if ( !RemoveMenu(hMenu, 0, MF_BYPOSITION) ||
483 !RemoveMenu(hMenu, 0, MF_BYPOSITION) )
484 {
485 wxLogLastError("RemoveMenu");
486 }
487 }
488 else
489 {
490 // modify the title
491 if ( !ModifyMenu(hMenu, 0u,
492 MF_BYPOSITION | MF_STRING,
493 (unsigned)idMenuTitle, m_title) )
494 {
495 wxLogLastError("ModifyMenu");
496 }
497 }
498 }
499
500 #ifdef __WIN32__
501 // put the title string in bold face
502 if ( !m_title.IsEmpty() )
503 {
504 MENUITEMINFO mii;
505 mii.cbSize = sizeof(mii);
506 mii.fMask = MIIM_STATE;
507 mii.fState = MFS_DEFAULT;
508
509 if ( !SetMenuItemInfo(hMenu, (unsigned)idMenuTitle, FALSE, &mii) )
510 {
511 wxLogLastError("SetMenuItemInfo");
512 }
513 }
514 #endif
515 }
516
517 const wxString wxMenu::GetTitle() const
518 {
519 return m_title;
520 }
521
522 // ---------------------------------------------------------------------------
523 // event processing
524 // ---------------------------------------------------------------------------
525
526 bool wxMenu::MSWCommand(WXUINT WXUNUSED(param), WXWORD id)
527 {
528 // ignore commands from the menu title
529
530 // NB: VC++ generates wrong assembler for `if ( id != idMenuTitle )'!!
531 if ( id != (WXWORD)idMenuTitle )
532 {
533 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED);
534 event.SetEventObject( this );
535 event.SetId( id );
536 event.SetInt( id );
537 ProcessCommand(event);
538 }
539
540 return TRUE;
541 }
542
543 bool wxMenu::ProcessCommand(wxCommandEvent & event)
544 {
545 bool processed = FALSE;
546
547 // Try a callback
548 if (m_callback)
549 {
550 (void)(*(m_callback))(*this, event);
551 processed = TRUE;
552 }
553
554 // Try the menu's event handler
555 if ( !processed && GetEventHandler())
556 {
557 processed = GetEventHandler()->ProcessEvent(event);
558 }
559
560 // Try the window the menu was popped up from (and up through the
561 // hierarchy)
562 wxWindow *win = GetInvokingWindow();
563 if ( !processed && win )
564 processed = win->GetEventHandler()->ProcessEvent(event);
565
566 return processed;
567 }
568
569 // ---------------------------------------------------------------------------
570 // Item search
571 // ---------------------------------------------------------------------------
572
573 // Finds the item id matching the given string, -1 if not found.
574 int wxMenu::FindItem (const wxString& itemString) const
575 {
576 wxString itemLabel = wxStripMenuCodes(itemString);
577 for ( wxNode *node = m_menuItems.First(); node; node = node->Next() )
578 {
579 wxMenuItem *item = (wxMenuItem *)node->Data();
580 if ( item->IsSubMenu() )
581 {
582 int ans = item->GetSubMenu()->FindItem(itemString);
583 if ( ans != wxNOT_FOUND )
584 return ans;
585 }
586 else if ( !item->IsSeparator() )
587 {
588 wxString label = wxStripMenuCodes(item->GetName());
589 if ( itemLabel == label )
590 return item->GetId();
591 }
592 }
593
594 return wxNOT_FOUND;
595 }
596
597 wxMenuItem *wxMenu::FindItemForId(int itemId, wxMenu ** itemMenu) const
598 {
599 if ( itemMenu )
600 *itemMenu = NULL;
601
602 wxMenuItem *item = NULL;
603 for ( wxNode *node = m_menuItems.First(); node && !item; node = node->Next() )
604 {
605 item = (wxMenuItem *)node->Data();
606
607 if ( item->GetId() == itemId )
608 {
609 if (itemMenu)
610 *itemMenu = (wxMenu *)this;
611 }
612 else if ( item->IsSubMenu() )
613 {
614 item = item->GetSubMenu()->FindItemForId(itemId, itemMenu);
615 }
616 else
617 {
618 // don't exit the loop
619 item = NULL;
620 }
621 }
622
623 return item;
624 }
625
626 // ---------------------------------------------------------------------------
627 // other
628 // ---------------------------------------------------------------------------
629
630 void wxMenu::Attach(wxMenuBar *menubar)
631 {
632 // menu can be in at most one menubar because otherwise they would both
633 // delete the menu pointer
634 wxASSERT_MSG( !m_menuBar, wxT("menu belongs to 2 menubars, expect a crash") );
635
636 m_menuBar = menubar;
637 m_savehMenu = m_hMenu;
638 m_hMenu = 0;
639 }
640
641 void wxMenu::Detach()
642 {
643 wxASSERT_MSG( m_menuBar, wxT("can't detach menu if it's not attached") );
644
645 m_hMenu = m_savehMenu;
646 m_savehMenu = 0;
647 }
648
649 // ---------------------------------------------------------------------------
650 // Menu Bar
651 // ---------------------------------------------------------------------------
652
653 void wxMenuBar::Init()
654 {
655 m_eventHandler = this;
656 m_menuCount = 0;
657 m_menus = NULL;
658 m_titles = NULL;
659 m_menuBarFrame = NULL;
660 m_hMenu = 0;
661 }
662
663 wxMenuBar::wxMenuBar()
664 {
665 Init();
666 }
667
668 wxMenuBar::wxMenuBar( long WXUNUSED(style) )
669 {
670 Init();
671 }
672
673 wxMenuBar::wxMenuBar(int count, wxMenu *menus[], const wxString titles[])
674 {
675 Init();
676
677 m_menuCount = count;
678 m_menus = menus;
679 m_titles = new wxString[count];
680
681 int i;
682 for ( i = 0; i < count; i++ )
683 m_titles[i] = titles[i];
684
685 for ( i = 0; i < count; i++ )
686 m_menus[i]->Attach(this);
687 }
688
689 wxMenuBar::~wxMenuBar()
690 {
691 for ( int i = 0; i < m_menuCount; i++ )
692 {
693 delete m_menus[i];
694 }
695
696 delete[] m_menus;
697 delete[] m_titles;
698 }
699
700 // ---------------------------------------------------------------------------
701 // wxMenuBar helpers
702 // ---------------------------------------------------------------------------
703
704 void wxMenuBar::Refresh()
705 {
706 wxCHECK_RET( m_menuBarFrame, wxT("can't refresh a menubar withotu a frame") );
707
708 DrawMenuBar((HWND)m_menuBarFrame->GetHWND()) ;
709 }
710
711 WXHMENU wxMenuBar::Create()
712 {
713 if (m_hMenu != 0 )
714 return m_hMenu;
715
716 wxCHECK_MSG( !m_hMenu, TRUE, wxT("menubar already created") );
717
718 m_hMenu = (WXHMENU)::CreateMenu();
719
720 if ( !m_hMenu )
721 {
722 wxLogLastError("CreateMenu");
723 }
724 else
725 {
726 for ( int i = 0; i < m_menuCount; i++ )
727 {
728 if ( !::AppendMenu((HMENU)m_hMenu, MF_POPUP | MF_STRING,
729 (UINT)m_menus[i]->GetHMenu(),
730 m_titles[i]) )
731 {
732 wxLogLastError("AppendMenu");
733 }
734 }
735 }
736
737 return m_hMenu;
738 }
739
740 // ---------------------------------------------------------------------------
741 // wxMenuBar functions forwarded to wxMenuItem
742 // ---------------------------------------------------------------------------
743
744 // Must only be used AFTER menu has been attached to frame,
745 // otherwise use individual menus to enable/disable items
746 void wxMenuBar::Enable(int id, bool enable)
747 {
748 wxMenu *itemMenu = NULL;
749 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
750
751 wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") );
752
753 item->Enable(enable);
754 }
755
756 void wxMenuBar::EnableTop(int pos, bool enable)
757 {
758 int flag = enable ? MF_ENABLED : MF_GRAYED;;
759
760 EnableMenuItem((HMENU)m_hMenu, pos, MF_BYPOSITION | flag);
761 }
762
763 // Must only be used AFTER menu has been attached to frame,
764 // otherwise use individual menus
765 void wxMenuBar::Check(int id, bool check)
766 {
767 wxMenu *itemMenu = NULL;
768 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
769
770 wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") );
771 wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") );
772
773 item->Check(check);
774 }
775
776 bool wxMenuBar::IsChecked(int id) const
777 {
778 wxMenu *itemMenu = NULL;
779 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
780
781 wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsChecked(): no such item") );
782
783 int flag = ::GetMenuState(GetHmenuOf(itemMenu), id, MF_BYCOMMAND);
784
785 return (flag & MF_CHECKED) != 0;
786 }
787
788 bool wxMenuBar::IsEnabled(int id) const
789 {
790 wxMenu *itemMenu = NULL;
791 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
792
793 wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsEnabled(): no such item") );
794
795 int flag = ::GetMenuState(GetHmenuOf(itemMenu), id, MF_BYCOMMAND) ;
796
797 // don't "and" with MF_ENABLED because its value is 0
798 return (flag & MF_DISABLED) == 0;
799 }
800
801 void wxMenuBar::SetLabel(int id, const wxString& label)
802 {
803 wxMenu *itemMenu = NULL;
804 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
805
806 wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") );
807
808 item->SetName(label);
809 }
810
811 wxString wxMenuBar::GetLabel(int id) const
812 {
813 wxMenu *itemMenu = NULL;
814 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
815
816 wxCHECK_MSG( item, wxT(""), wxT("wxMenuBar::GetLabel(): no such item") );
817
818 return item->GetName();
819 }
820
821 void wxMenuBar::SetHelpString (int id, const wxString& helpString)
822 {
823 wxMenu *itemMenu = NULL;
824 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
825
826 wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") );
827
828 item->SetHelp(helpString);
829 }
830
831 wxString wxMenuBar::GetHelpString (int id) const
832 {
833 wxMenu *itemMenu = NULL;
834 wxMenuItem *item = FindItemForId(id, &itemMenu) ;
835
836 wxCHECK_MSG( item, wxT(""), wxT("wxMenuBar::GetHelpString(): no such item") );
837
838 return item->GetHelp();
839 }
840
841 // ---------------------------------------------------------------------------
842 // wxMenuBar functions to work with the top level submenus
843 // ---------------------------------------------------------------------------
844
845 // NB: we don't support owner drawn top level items for now, if we do these
846 // functions would have to be changed to use wxMenuItem as well
847
848 void wxMenuBar::SetLabelTop(int pos, const wxString& label)
849 {
850 UINT id;
851 UINT flagsOld = ::GetMenuState((HMENU)m_hMenu, pos, MF_BYPOSITION);
852 if ( flagsOld == 0xFFFFFFFF )
853 {
854 wxLogLastError(wxT("GetMenuState"));
855
856 return;
857 }
858
859 if ( flagsOld & MF_POPUP )
860 {
861 // HIBYTE contains the number of items in the submenu in this case
862 flagsOld &= 0xff ;
863 id = (UINT)::GetSubMenu((HMENU)m_hMenu, pos) ;
864 }
865 else
866 {
867 id = pos;
868 }
869
870 if ( ::ModifyMenu(GetHmenu(), pos, MF_BYPOSITION | MF_STRING | flagsOld,
871 id, label) == (int)0xFFFFFFFF )
872 {
873 wxLogLastError("ModifyMenu");
874 }
875 }
876
877 wxString wxMenuBar::GetLabelTop(int pos) const
878 {
879 int len = ::GetMenuString((HMENU)m_hMenu, pos, NULL, 0, MF_BYCOMMAND);
880
881 len++; // for the NUL character
882 wxString label;
883 ::GetMenuString(GetHmenu(), pos, label.GetWriteBuf(len), len, MF_BYCOMMAND);
884 label.UngetWriteBuf();
885
886 return label;
887 }
888
889 // ---------------------------------------------------------------------------
890 // wxMenuBar notifications
891 // ---------------------------------------------------------------------------
892
893 bool wxMenuBar::OnDelete(wxMenu *a_menu, int pos)
894 {
895 if ( !m_menuBarFrame )
896 return TRUE;
897
898 if ( ::RemoveMenu((HMENU)m_hMenu, (UINT)pos, MF_BYPOSITION) )
899 {
900 // VZ: I'm not sure about what's going on here, so I leave an assert
901 wxASSERT_MSG( m_menus[pos] == a_menu, wxT("what is this parameter for??") );
902
903 a_menu->Detach();
904
905 if ( m_menuBarFrame )
906 Refresh();
907
908 return TRUE;
909 }
910 else
911 {
912 wxLogLastError("RemoveMenu");
913 }
914
915 return FALSE;
916 }
917
918 bool wxMenuBar::OnAppend(wxMenu *a_menu, const wxChar *title)
919 {
920 WXHMENU submenu = a_menu->GetHMenu();
921 if ( !submenu )
922 return FALSE;
923
924 if ( !m_menuBarFrame )
925 return TRUE;
926
927 a_menu->Attach(this);
928
929 if ( !::AppendMenu(GetHmenu(), MF_POPUP | MF_STRING,
930 (UINT)submenu, title) )
931 {
932 wxLogLastError(wxT("AppendMenu"));
933 }
934
935 Refresh();
936
937 return TRUE;
938 }
939
940 // ---------------------------------------------------------------------------
941 // wxMenuBar construction
942 // ---------------------------------------------------------------------------
943 int wxMenuBar::FindMenu(const wxString& title)
944 {
945 wxString menuTitle = wxStripMenuCodes(title);
946 for ( int i = 0; i < m_menuCount; i++ )
947 {
948 wxString title = wxStripMenuCodes(m_titles[i]);
949 if ( menuTitle == title )
950 return i;
951 }
952
953 return wxNOT_FOUND;
954
955 }
956
957
958 void wxMenuBar::ReplaceMenu(int pos, wxMenu * new_menu, const wxString& title)
959 {
960 if (m_menuBarFrame) return;
961
962 if ( pos >= 0 && pos < m_menuCount )
963 {
964 wxMenu *old_menu = m_menus[pos];
965 m_menus[pos] = new_menu;
966 delete old_menu;
967 }
968
969 }
970
971
972 void wxMenuBar::Insert(int pos, wxMenu * menu, const wxString& title)
973 {
974 if (m_menuBarFrame) return;
975 if ( pos < 0 && pos >= m_menuCount ) return;
976
977 m_menuCount ++;
978 wxMenu **new_menus = new wxMenu *[m_menuCount];
979 wxString *new_titles = new wxString[m_menuCount];
980 int i;
981
982 for (i = 0; i < pos; i++)
983 {
984 new_menus[i] = m_menus[i];
985 m_menus[i] = NULL;
986 new_titles[i] = m_titles[i];
987 m_titles[i] = wxT("");
988 }
989
990 new_menus[pos] = (wxMenu *)menu;
991 new_titles[i] = title;
992
993 for (i = pos+1; i < m_menuCount; i++)
994 {
995 new_menus[i] = m_menus[i-1];
996 m_menus[i-1] = NULL;
997 new_titles[i] = m_titles[i-1];
998 m_titles[i-1] = wxT("");
999 }
1000 if (m_menus)
1001 {
1002 delete[]m_menus;
1003 delete[]m_titles;
1004 }
1005 m_menus = new_menus;
1006 m_titles = new_titles;
1007
1008 menu->SetParent(this);
1009
1010 }
1011
1012
1013 void wxMenuBar::Append (wxMenu * menu, const wxString& title)
1014 {
1015 if (!OnAppend(menu, title))
1016 return;
1017
1018 m_menuCount ++;
1019 wxMenu **new_menus = new wxMenu *[m_menuCount];
1020 wxString *new_titles = new wxString[m_menuCount];
1021 int i;
1022
1023 for (i = 0; i < m_menuCount - 1; i++)
1024 {
1025 new_menus[i] = m_menus[i];
1026 m_menus[i] = NULL;
1027 new_titles[i] = m_titles[i];
1028 m_titles[i] = wxT("");
1029 }
1030 if (m_menus)
1031 {
1032 delete[]m_menus;
1033 delete[]m_titles;
1034 }
1035 m_menus = new_menus;
1036 m_titles = new_titles;
1037
1038 m_menus[m_menuCount - 1] = (wxMenu *)menu;
1039 m_titles[m_menuCount - 1] = title;
1040
1041 menu->SetParent(this);
1042 }
1043
1044 void wxMenuBar::Delete(wxMenu * menu, int i)
1045 {
1046 int j;
1047 int ii = (int) i;
1048
1049 if (menu != 0) {
1050 for (ii = 0; ii < m_menuCount; ii++) {
1051 if (m_menus[ii] == menu)
1052 break;
1053 }
1054 if (ii >= m_menuCount)
1055 return;
1056 } else {
1057 if (ii < 0 || ii >= m_menuCount)
1058 return;
1059 menu = m_menus[ii];
1060 }
1061
1062 if (!OnDelete(menu, ii))
1063 return;
1064
1065 menu->SetParent(NULL);
1066
1067 -- m_menuCount;
1068 for (j = ii; j < m_menuCount; j++) {
1069 m_menus[j] = m_menus[j + 1];
1070 m_titles[j] = m_titles[j + 1];
1071 }
1072 }
1073
1074 void wxMenuBar::Attach(wxFrame *frame)
1075 {
1076 wxASSERT_MSG( !m_menuBarFrame, wxT("menubar already attached!") );
1077
1078 m_menuBarFrame = frame;
1079
1080 #if wxUSE_ACCEL
1081 // create the accel table - we consider that the menubar construction is
1082 // finished
1083 size_t nAccelCount = 0;
1084 int i;
1085 for ( i = 0; i < m_menuCount; i++ )
1086 {
1087 nAccelCount += m_menus[i]->GetAccelCount();
1088 }
1089
1090 if ( nAccelCount )
1091 {
1092 wxAcceleratorEntry *accelEntries = new wxAcceleratorEntry[nAccelCount];
1093
1094 nAccelCount = 0;
1095 for ( i = 0; i < m_menuCount; i++ )
1096 {
1097 nAccelCount += m_menus[i]->CopyAccels(&accelEntries[nAccelCount]);
1098 }
1099
1100 m_accelTable = wxAcceleratorTable(nAccelCount, accelEntries);
1101
1102 delete [] accelEntries;
1103 }
1104 #endif // wxUSE_ACCEL
1105 }
1106
1107 void wxMenuBar::Detach()
1108 {
1109 // ::DestroyMenu((HMENU)m_hMenu);
1110 m_hMenu = (WXHMENU)NULL;
1111 m_menuBarFrame = NULL;
1112 }
1113
1114
1115 // ---------------------------------------------------------------------------
1116 // wxMenuBar searching for menu items
1117 // ---------------------------------------------------------------------------
1118
1119 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
1120 int wxMenuBar::FindMenuItem(const wxString& menuString,
1121 const wxString& itemString) const
1122 {
1123 wxString menuLabel = wxStripMenuCodes(menuString);
1124 for ( int i = 0; i < m_menuCount; i++ )
1125 {
1126 wxString title = wxStripMenuCodes(m_titles[i]);
1127 if ( menuString == title )
1128 return m_menus[i]->FindItem(itemString);
1129 }
1130
1131 return wxNOT_FOUND;
1132 }
1133
1134 wxMenuItem *wxMenuBar::FindItemForId (int id, wxMenu **itemMenu) const
1135 {
1136 if ( itemMenu )
1137 *itemMenu = NULL;
1138
1139 wxMenuItem *item = NULL;
1140 for ( int i = 0; !item && (i < m_menuCount); i++ )
1141 {
1142 item = m_menus[i]->FindItemForId(id, itemMenu);
1143 }
1144
1145 return item;
1146 }
1147
1148
1149 // ----------------------------------------------------------------------------
1150 // helper functions
1151 // ----------------------------------------------------------------------------
1152
1153 wxWindow *wxMenu::GetWindow() const
1154 {
1155 if ( m_pInvokingWindow != NULL )
1156 return m_pInvokingWindow;
1157 else if ( m_menuBar != NULL)
1158 return m_menuBar->GetFrame();
1159
1160 return NULL;
1161 }
1162
1163 WXHMENU wxMenu::GetHMenu() const
1164 {
1165 if ( m_hMenu != 0 )
1166 return m_hMenu;
1167 else if ( m_savehMenu != 0 )
1168 return m_savehMenu;
1169
1170 wxFAIL_MSG(wxT("wxMenu without HMENU"));
1171
1172 return 0;
1173 }
1174
1175 // Update a menu and all submenus recursively. source is the object that has
1176 // the update event handlers defined for it. If NULL, the menu or associated
1177 // window will be used.
1178 void wxMenu::UpdateUI(wxEvtHandler* source)
1179 {
1180 if (!source && GetInvokingWindow())
1181 source = GetInvokingWindow()->GetEventHandler();
1182 if (!source)
1183 source = GetEventHandler();
1184 if (!source)
1185 source = this;
1186
1187 wxNode* node = GetItems().First();
1188 while (node)
1189 {
1190 wxMenuItem* item = (wxMenuItem*) node->Data();
1191 if ( !item->IsSeparator() )
1192 {
1193 wxWindowID id = item->GetId();
1194 wxUpdateUIEvent event(id);
1195 event.SetEventObject( source );
1196
1197 if (source->ProcessEvent(event))
1198 {
1199 if (event.GetSetText())
1200 SetLabel(id, event.GetText());
1201 if (event.GetSetChecked())
1202 Check(id, event.GetChecked());
1203 if (event.GetSetEnabled())
1204 Enable(id, event.GetEnabled());
1205 }
1206
1207 if (item->GetSubMenu())
1208 item->GetSubMenu()->UpdateUI(source);
1209 }
1210 node = node->Next();
1211 }
1212 }