]> git.saurik.com Git - wxWidgets.git/blob - src/os2/menu.cpp
Fix some bugs
[wxWidgets.git] / src / os2 / menu.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: menu.cpp
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/10/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "menu.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/app.h"
21 #include "wx/frame.h"
22 #include "wx/menu.h"
23 #include "wx/utils.h"
24 #include "wx/intl.h"
25 #include "wx/log.h"
26 #endif
27
28 #if wxUSE_OWNER_DRAWN
29 #include "wx/ownerdrw.h"
30 #endif
31
32 #include "wx/os2/private.h"
33
34 // other standard headers
35 #include <string.h>
36
37 // ----------------------------------------------------------------------------
38 // global variables
39 // ----------------------------------------------------------------------------
40
41 extern wxMenu* wxCurrentPopupMenu;
42
43 // ----------------------------------------------------------------------------
44 // constants
45 // ----------------------------------------------------------------------------
46
47 //
48 // The (popup) menu title has this special id
49 //
50 static const int idMenuTitle = -2;
51
52 //
53 // The unique ID for Menus
54 //
55 USHORT wxMenu::m_nextMenuId = 0;
56
57 // ----------------------------------------------------------------------------
58 // macros
59 // ----------------------------------------------------------------------------
60
61 IMPLEMENT_DYNAMIC_CLASS(wxMenu, wxEvtHandler)
62 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar, wxEvtHandler)
63
64 // ============================================================================
65 // implementation
66 // ============================================================================
67
68 // ---------------------------------------------------------------------------
69 // wxMenu construction, adding and removing menu items
70 // ---------------------------------------------------------------------------
71
72 //
73 // Construct a menu with optional title (then use append)
74 //
75 void wxMenu::Init()
76 {
77 m_bDoBreak = FALSE;
78 m_nStartRadioGroup = -1;
79
80 //
81 // Create the menu (to be used as a submenu or a popup)
82 //
83 if ((m_hMenu = ::WinCreateWindow( HWND_DESKTOP
84 ,WC_MENU
85 ,"Menu"
86 ,0L
87 ,0L
88 ,0L
89 ,0L
90 ,0L
91 ,NULLHANDLE
92 ,HWND_TOP
93 ,0L
94 ,NULL
95 ,NULL
96 )) == 0)
97 {
98 wxLogLastError("WinLoadMenu");
99 }
100 m_vMenuData.iPosition = 0;
101 m_vMenuData.afStyle = MIS_SUBMENU | MIS_TEXT;
102 m_vMenuData.afAttribute = (USHORT)0;
103 m_vMenuData.id = m_nextMenuId++;
104 m_vMenuData.hwndSubMenu = m_hMenu;
105 m_vMenuData.hItem = NULLHANDLE;
106
107 //
108 // If we have a title, insert it in the beginning of the menu
109 //
110 if (!m_title.IsEmpty())
111 {
112 Append( idMenuTitle
113 ,m_title
114 ,wxEmptyString
115 ,wxITEM_NORMAL
116 );
117 AppendSeparator();
118 }
119 } // end of wxMenu::Init
120
121 //
122 // The wxWindow destructor will take care of deleting the submenus.
123 //
124 wxMenu::~wxMenu()
125 {
126 //
127 // We should free PM resources only if PM doesn't do it for us
128 // which happens if we're attached to a menubar or a submenu of another
129 // menu
130 if (!IsAttached() && !GetParent())
131 {
132 if (!::WinDestroyWindow((HWND)GetHmenu()) )
133 {
134 wxLogLastError("WinDestroyWindow");
135 }
136 }
137
138 #if wxUSE_ACCEL
139 //
140 // Delete accels
141 //
142 WX_CLEAR_ARRAY(m_vAccels);
143 #endif // wxUSE_ACCEL
144 } // end of wxMenu::~wxMenu
145
146 void wxMenu::Break()
147 {
148 // this will take effect during the next call to Append()
149 m_bDoBreak = TRUE;
150 } // end of wxMenu::Break
151
152 void wxMenu::Attach(
153 wxMenuBarBase* pMenubar
154 )
155 {
156 wxMenuBase::Attach(pMenubar);
157 EndRadioGroup();
158 } // end of wxMenu::Break;
159
160 #if wxUSE_ACCEL
161
162 int wxMenu::FindAccel(
163 int nId
164 ) const
165 {
166 size_t n;
167 size_t nCount = m_vAccels.GetCount();
168
169 for (n = 0; n < nCount; n++)
170 if (m_vAccels[n]->m_command == nId)
171 return n;
172 return wxNOT_FOUND;
173 } // end of wxMenu::FindAccel
174
175 void wxMenu::UpdateAccel(
176 wxMenuItem* pItem
177 )
178 {
179 if (pItem->IsSubMenu())
180 {
181 wxMenu* pSubmenu = pItem->GetSubMenu();
182 wxMenuItemList::Node* pNode = pSubmenu->GetMenuItems().GetFirst();
183
184 while (pNode)
185 {
186 UpdateAccel(pNode->GetData());
187 pNode = pNode->GetNext();
188 }
189 }
190 else if (!pItem->IsSeparator())
191 {
192 //
193 // Find the (new) accel for this item
194 //
195 wxAcceleratorEntry* pAccel = wxGetAccelFromString(pItem->GetText());
196
197 if (pAccel)
198 pAccel->m_command = pItem->GetId();
199
200 //
201 // Find the old one
202 //
203 size_t n = FindAccel(pItem->GetId());
204
205 if (n == (size_t)wxNOT_FOUND)
206 {
207 //
208 // No old, add new if any
209 //
210 if (pAccel)
211 m_vAccels.Add(pAccel);
212 else
213 return;
214 }
215 else
216 {
217 //
218 // Replace old with new or just remove the old one if no new
219 //
220 delete m_vAccels[n];
221 if (pAccel)
222 m_vAccels[n] = pAccel;
223 else
224 m_vAccels.RemoveAt(n);
225 }
226
227 if (IsAttached())
228 {
229 GetMenuBar()->RebuildAccelTable();
230 }
231 }
232 } // wxMenu::UpdateAccel
233
234 #endif // wxUSE_ACCEL
235
236 //
237 // Append a new item or submenu to the menu
238 //
239 bool wxMenu::DoInsertOrAppend(
240 wxMenuItem* pItem
241 , size_t nPos
242 )
243 {
244 wxMenu* pSubmenu = pItem->GetSubMenu();
245 MENUITEM& rItem = (pSubmenu != NULL)?pSubmenu->m_vMenuData:
246 pItem->m_vMenuData;
247
248 ERRORID vError;
249 wxString sError;
250
251 #if wxUSE_ACCEL
252 UpdateAccel(pItem);
253 #endif // wxUSE_ACCEL
254
255 //
256 // If "Break" has just been called, insert a menu break before this item
257 // (and don't forget to reset the flag)
258 //
259 if (m_bDoBreak)
260 {
261 rItem.afStyle |= MIS_BREAK;
262 m_bDoBreak = FALSE;
263 }
264
265 //
266 // Id is the numeric id for normal menu items and HMENU for submenus as
267 // required by ::MM_INSERTITEM message API
268 //
269 if (pSubmenu != NULL)
270 {
271 wxASSERT_MSG(pSubmenu->GetHMenu(), wxT("invalid submenu"));
272 pSubmenu->SetParent(this);
273
274 rItem.iPosition = 0; // submenus have a 0 position
275 rItem.id = (USHORT)pSubmenu->GetHMenu();
276 rItem.afStyle |= MIS_SUBMENU | MIS_TEXT;
277 }
278 else
279 {
280 rItem.id = pItem->GetId();
281 }
282
283 BYTE* pData;
284
285 #if wxUSE_OWNER_DRAWN
286 if (pItem->IsOwnerDrawn())
287 {
288 //
289 // Want to get {Measure|Draw}Item messages?
290 // item draws itself, passing pointer to data doesn't work in OS/2
291 // Will eventually need to set the image handle somewhere into vItem.hItem
292 //
293 rItem.afStyle |= MIS_OWNERDRAW;
294 pData = (BYTE*)NULL;
295 rItem.hItem = (HBITMAP)pItem->GetBitmap().GetHBITMAP();
296 pItem->m_vMenuData.afStyle = rItem.afStyle;
297 pItem->m_vMenuData.hItem = rItem.hItem;
298 }
299 else
300 #endif
301 if (pItem->IsSeparator())
302 {
303 rItem.afStyle = MIS_SEPARATOR;
304 }
305 else
306 {
307 //
308 // Menu is just a normal string (passed in data parameter)
309 //
310 rItem.afStyle |= MIS_TEXT;
311 pData = (char*)pItem->GetText().c_str();
312 }
313
314 if (nPos == (size_t)-1)
315 {
316 rItem.iPosition = MIT_END;
317 }
318 else
319 {
320 rItem.iPosition = nPos;
321 }
322
323 APIRET rc;
324
325 rc = (APIRET)::WinSendMsg( GetHmenu()
326 ,MM_INSERTITEM
327 ,(MPARAM)&rItem
328 ,(MPARAM)pData
329 );
330 #if wxUSE_OWNER_DRAWN
331 if (pItem->IsOwnerDrawn())
332 {
333 MENUITEM vMenuItem;
334
335 ::WinSendMsg( GetHmenu()
336 ,MM_QUERYITEM
337 ,MPFROM2SHORT( (USHORT)pItem->GetId()
338 ,(USHORT)(FALSE)
339 )
340 ,&vMenuItem
341 );
342 }
343 #endif
344 if (rc == (APIRET)MIT_MEMERROR || rc == (APIRET)MIT_ERROR)
345 {
346 vError = ::WinGetLastError(vHabmain);
347 sError = wxPMErrorToStr(vError);
348 wxLogError("Error inserting or appending a menuitem. Error: %s\n", sError.c_str());
349 wxLogLastError("Insert or AppendMenu");
350 return FALSE;
351 }
352 else
353 {
354 //
355 // If we're already attached to the menubar, we must update it
356 //
357 if (IsAttached() && GetMenuBar()->IsAttached())
358 {
359 GetMenuBar()->Refresh();
360 }
361 return TRUE;
362 }
363 return FALSE;
364 } // end of wxMenu::DoInsertOrAppend
365
366 void wxMenu::EndRadioGroup()
367 {
368 //
369 // We're not inside a radio group any longer
370 //
371 m_nStartRadioGroup = -1;
372 } // end of wxMenu::EndRadioGroup
373
374 wxMenuItem* wxMenu::DoAppend(
375 wxMenuItem* pItem
376 )
377 {
378 wxCHECK_MSG( pItem, NULL, _T("NULL item in wxMenu::DoAppend") );
379
380 bool bCheck = FALSE;
381
382 if (pItem->GetKind() == wxITEM_RADIO)
383 {
384 int nCount = GetMenuItemCount();
385
386 if (m_nStartRadioGroup == -1)
387 {
388 //
389 // Start a new radio group
390 //
391 m_nStartRadioGroup = nCount;
392
393 //
394 // For now it has just one element
395 //
396 pItem->SetAsRadioGroupStart();
397 pItem->SetRadioGroupEnd(m_nStartRadioGroup);
398
399 //
400 // Ensure that we have a checked item in the radio group
401 //
402 bCheck = TRUE;
403 }
404 else // extend the current radio group
405 {
406 //
407 // We need to update its end item
408 //
409 pItem->SetRadioGroupStart(m_nStartRadioGroup);
410
411 wxMenuItemList::Node* pNode = GetMenuItems().Item(m_nStartRadioGroup);
412
413 if (pNode)
414 {
415 pNode->GetData()->SetRadioGroupEnd(nCount);
416 }
417 else
418 {
419 wxFAIL_MSG( _T("where is the radio group start item?") );
420 }
421 }
422 }
423 else // not a radio item
424 {
425 EndRadioGroup();
426 }
427
428 if (!wxMenuBase::DoAppend(pItem) || !DoInsertOrAppend(pItem))
429 {
430 return NULL;
431 }
432 if (bCheck)
433 {
434 //
435 // Check the item initially
436 //
437 pItem->Check(TRUE);
438 }
439 return pItem;
440 } // end of wxMenu::DoAppend
441
442 wxMenuItem* wxMenu::DoInsert(
443 size_t nPos
444 , wxMenuItem* pItem
445 )
446 {
447 if ( wxMenuBase::DoInsert( nPos
448 ,pItem) &&
449 DoInsertOrAppend( pItem
450 ,nPos
451 ))
452 return pItem;
453 else
454 return NULL;
455 } // end of wxMenu::DoInsert
456
457 wxMenuItem* wxMenu::DoRemove(
458 wxMenuItem* pItem
459 )
460 {
461 //
462 // We need to find the items position in the child list
463 //
464 size_t nPos;
465 wxMenuItemList::Node* pNode = GetMenuItems().GetFirst();
466
467 for (nPos = 0; pNode; nPos++)
468 {
469 if (pNode->GetData() == pItem)
470 break;
471 pNode = pNode->GetNext();
472 }
473
474 //
475 // DoRemove() (unlike Remove) can only be called for existing item!
476 //
477 wxCHECK_MSG(pNode, NULL, wxT("bug in wxMenu::Remove logic"));
478
479 #if wxUSE_ACCEL
480 //
481 // Remove the corresponding accel from the accel table
482 //
483 int n = FindAccel(pItem->GetId());
484
485 if (n != wxNOT_FOUND)
486 {
487 delete m_vAccels[n];
488 m_vAccels.RemoveAt(n);
489 }
490
491 #endif // wxUSE_ACCEL
492 //
493 // Remove the item from the menu
494 //
495 ::WinSendMsg( GetHmenu()
496 ,MM_REMOVEITEM
497 ,MPFROM2SHORT(pItem->GetId(), TRUE)
498 ,(MPARAM)0
499 );
500 if (IsAttached() && GetMenuBar()->IsAttached())
501 {
502 //
503 // Otherwise, the chane won't be visible
504 //
505 GetMenuBar()->Refresh();
506 }
507
508 //
509 // And from internal data structures
510 //
511 return wxMenuBase::DoRemove(pItem);
512 } // end of wxMenu::DoRemove
513
514 // ---------------------------------------------------------------------------
515 // accelerator helpers
516 // ---------------------------------------------------------------------------
517
518 #if wxUSE_ACCEL
519
520 //
521 // Create the wxAcceleratorEntries for our accels and put them into provided
522 // array - return the number of accels we have
523 //
524 size_t wxMenu::CopyAccels(
525 wxAcceleratorEntry* pAccels
526 ) const
527 {
528 size_t nCount = GetAccelCount();
529
530 for (size_t n = 0; n < nCount; n++)
531 {
532 *pAccels++ = *m_vAccels[n];
533 }
534 return nCount;
535 } // end of wxMenu::CopyAccels
536
537 #endif // wxUSE_ACCEL
538
539 // ---------------------------------------------------------------------------
540 // set wxMenu title
541 // ---------------------------------------------------------------------------
542
543 void wxMenu::SetTitle(
544 const wxString& rLabel
545 )
546 {
547 bool bHasNoTitle = m_title.IsEmpty();
548 HWND hMenu = GetHmenu();
549
550 m_title = rLabel;
551 if (bHasNoTitle)
552 {
553 if (!rLabel.IsEmpty())
554 {
555 if (!::WinSetWindowText(hMenu, rLabel.c_str()))
556 {
557 wxLogLastError("SetMenuTitle");
558 }
559 }
560 }
561 else
562 {
563 if (rLabel.IsEmpty() )
564 {
565 ::WinSendMsg( GetHmenu()
566 ,MM_REMOVEITEM
567 ,MPFROM2SHORT(hMenu, TRUE)
568 ,(MPARAM)0
569 );
570 }
571 else
572 {
573 //
574 // Modify the title
575 //
576 if (!::WinSetWindowText(hMenu, rLabel.c_str()))
577 {
578 wxLogLastError("SetMenuTitle");
579 }
580 }
581 }
582 } // end of wxMenu::SetTitle
583
584 // ---------------------------------------------------------------------------
585 // event processing
586 // ---------------------------------------------------------------------------
587
588 bool wxMenu::OS2Command(
589 WXUINT WXUNUSED(uParam)
590 , WXWORD vId
591 )
592 {
593 //
594 // Ignore commands from the menu title
595 //
596
597 if (vId != (WXWORD)idMenuTitle)
598 {
599 SendEvent( vId
600 ,(int)::WinSendMsg( GetHmenu()
601 ,MM_QUERYITEMATTR
602 ,MPFROMSHORT(vId)
603 ,(MPARAM)MIA_CHECKED
604 )
605 );
606 }
607 return TRUE;
608 } // end of wxMenu::OS2Command
609
610 // ---------------------------------------------------------------------------
611 // other
612 // ---------------------------------------------------------------------------
613
614 wxWindow* wxMenu::GetWindow() const
615 {
616 if (m_invokingWindow != NULL)
617 return m_invokingWindow;
618 else if ( GetMenuBar() != NULL)
619 return GetMenuBar()->GetFrame();
620
621 return NULL;
622 } // end of wxMenu::GetWindow
623
624 // recursive search for item by id
625 wxMenuItem* wxMenu::FindItem(
626 int nItemId
627 , ULONG hItem
628 , wxMenu** ppItemMenu
629 ) const
630 {
631 if ( ppItemMenu )
632 *ppItemMenu = NULL;
633
634 wxMenuItem* pItem = NULL;
635
636 for ( wxMenuItemList::Node *node = m_items.GetFirst();
637 node && !pItem;
638 node = node->GetNext() )
639 {
640 pItem = node->GetData();
641
642 if ( pItem->GetId() == nItemId && pItem->m_vMenuData.hItem == hItem)
643 {
644 if ( ppItemMenu )
645 *ppItemMenu = (wxMenu *)this;
646 }
647 else if ( pItem->IsSubMenu() )
648 {
649 pItem = pItem->GetSubMenu()->FindItem( nItemId
650 ,hItem
651 ,ppItemMenu
652 );
653 if (pItem)
654 break;
655 }
656 else
657 {
658 // don't exit the loop
659 pItem = NULL;
660 }
661 }
662 return pItem;
663 } // end of wxMenu::FindItem
664
665 // ---------------------------------------------------------------------------
666 // Menu Bar
667 // ---------------------------------------------------------------------------
668
669 void wxMenuBar::Init()
670 {
671 m_eventHandler = this;
672 m_menuBarFrame = NULL;
673 m_hMenu = 0;
674 } // end of wxMenuBar::Init
675
676 wxMenuBar::wxMenuBar()
677 {
678 Init();
679 } // end of wxMenuBar::wxMenuBar
680
681 wxMenuBar::wxMenuBar(
682 long WXUNUSED(lStyle)
683 )
684 {
685 Init();
686 } // end of wxMenuBar::wxMenuBar
687
688 wxMenuBar::wxMenuBar(
689 int nCount
690 , wxMenu* vMenus[]
691 , const wxString sTitles[]
692 )
693 {
694 Init();
695
696 m_titles.Alloc(nCount);
697 for ( int i = 0; i < nCount; i++ )
698 {
699 m_menus.Append(vMenus[i]);
700 m_titles.Add(sTitles[i]);
701 vMenus[i]->Attach(this);
702 }
703 } // end of wxMenuBar::wxMenuBar
704
705 wxMenuBar::~wxMenuBar()
706 {
707 //
708 // We should free PM's resources only if PM doesn't do it for us
709 // which happens if we're attached to a frame
710 //
711 if (m_hMenu && !IsAttached())
712 {
713 ::WinDestroyWindow((HMENU)m_hMenu);
714 m_hMenu = (WXHMENU)NULL;
715 }
716 } // end of wxMenuBar::~wxMenuBar
717
718 // ---------------------------------------------------------------------------
719 // wxMenuBar helpers
720 // ---------------------------------------------------------------------------
721
722 void wxMenuBar::Refresh()
723 {
724 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
725
726 WinSendMsg(GetWinHwnd(m_menuBarFrame), WM_UPDATEFRAME, (MPARAM)FCF_MENU, (MPARAM)0);
727 } // end of wxMenuBar::Refresh
728
729 WXHMENU wxMenuBar::Create()
730 {
731 HWND hFrame;
732
733 if (m_hMenu != 0 )
734 return m_hMenu;
735
736 wxCHECK_MSG(!m_hMenu, TRUE, wxT("menubar already created"));
737
738 //
739 // Menubars should be associated with a frame otherwise they are popups
740 //
741 if (m_menuBarFrame != NULL)
742 hFrame = GetWinHwnd(m_menuBarFrame);
743 else
744 hFrame = HWND_DESKTOP;
745 //
746 // Create an empty menu and then fill it with insertions
747 //
748 if ((m_hMenu = ::WinCreateWindow( hFrame
749 ,WC_MENU
750 ,(PSZ)NULL
751 ,MS_ACTIONBAR | WS_SYNCPAINT | WS_VISIBLE
752 ,0L
753 ,0L
754 ,0L
755 ,0L
756 ,hFrame
757 ,HWND_TOP
758 ,FID_MENU
759 ,NULL
760 ,NULL
761 )) == 0)
762 {
763 wxLogLastError("WinLoadMenu");
764 }
765 else
766 {
767 size_t nCount = GetMenuCount();
768
769 for (size_t i = 0; i < nCount; i++)
770 {
771 APIRET rc;
772 ERRORID vError;
773 wxString sError;
774 HWND hSubMenu;
775
776 //
777 // Set the parent and owner of the submenues to be the menubar, not the desktop
778 //
779 hSubMenu = m_menus[i]->m_vMenuData.hwndSubMenu;
780 if (!::WinSetParent(m_menus[i]->m_vMenuData.hwndSubMenu, m_hMenu, FALSE))
781 {
782 vError = ::WinGetLastError(vHabmain);
783 sError = wxPMErrorToStr(vError);
784 wxLogError("Error setting parent for submenu. Error: %s\n", sError.c_str());
785 return NULLHANDLE;
786 }
787
788 if (!::WinSetOwner(m_menus[i]->m_vMenuData.hwndSubMenu, m_hMenu))
789 {
790 vError = ::WinGetLastError(vHabmain);
791 sError = wxPMErrorToStr(vError);
792 wxLogError("Error setting parent for submenu. Error: %s\n", sError.c_str());
793 return NULLHANDLE;
794 }
795
796 m_menus[i]->m_vMenuData.iPosition = i;
797
798 rc = (APIRET)::WinSendMsg(m_hMenu, MM_INSERTITEM, (MPARAM)&m_menus[i]->m_vMenuData, (MPARAM)m_titles[i].c_str());
799 if (rc == (APIRET)MIT_MEMERROR || rc == (APIRET)MIT_ERROR)
800 {
801 vError = ::WinGetLastError(vHabmain);
802 sError = wxPMErrorToStr(vError);
803 wxLogError("Error inserting or appending a menuitem. Error: %s\n", sError.c_str());
804 return NULLHANDLE;
805 }
806 }
807 }
808 return m_hMenu;
809 } // end of wxMenuBar::Create
810
811 // ---------------------------------------------------------------------------
812 // wxMenuBar functions to work with the top level submenus
813 // ---------------------------------------------------------------------------
814
815 //
816 // NB: we don't support owner drawn top level items for now, if we do these
817 // functions would have to be changed to use wxMenuItem as well
818 //
819 void wxMenuBar::EnableTop(
820 size_t nPos
821 , bool bEnable
822 )
823 {
824 wxCHECK_RET(IsAttached(), wxT("doesn't work with unattached menubars"));
825 USHORT uFlag = 0;
826 SHORT nId;
827
828 if(!bEnable)
829 uFlag = MIA_DISABLED;
830
831 nId = SHORT1FROMMR(::WinSendMsg((HWND)m_hMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT(nPos), (MPARAM)0));
832 if (nId == MIT_ERROR)
833 {
834 wxLogLastError("LogLastError");
835 return;
836 }
837 ::WinSendMsg((HWND)m_hMenu, MM_SETITEMATTR, MPFROM2SHORT(nId, TRUE), MPFROM2SHORT(MIA_DISABLED, uFlag));
838 Refresh();
839 } // end of wxMenuBar::EnableTop
840
841 void wxMenuBar::SetLabelTop(
842 size_t nPos
843 , const wxString& rLabel
844 )
845 {
846 SHORT nId;
847 MENUITEM vItem;
848
849 wxCHECK_RET(nPos < GetMenuCount(), wxT("invalid menu index"));
850 m_titles[nPos] = rLabel;
851
852 if (!IsAttached())
853 {
854 return;
855 }
856
857 nId = SHORT1FROMMR(::WinSendMsg((HWND)m_hMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT(nPos), (MPARAM)0));
858 if (nId == MIT_ERROR)
859 {
860 wxLogLastError("LogLastError");
861 return;
862 }
863 if(!::WinSendMsg( (HWND)m_hMenu
864 ,MM_QUERYITEM
865 ,MPFROM2SHORT(nId, TRUE)
866 ,MPARAM(&vItem)
867 ))
868 {
869 wxLogLastError("QueryItem");
870 }
871 nId = vItem.id;
872
873 if (::WinSendMsg(GetHmenu(), MM_SETITEMTEXT, MPFROMSHORT(nId), (MPARAM)rLabel.c_str()));
874 {
875 wxLogLastError("ModifyMenu");
876 }
877 Refresh();
878 } // end of wxMenuBar::SetLabelTop
879
880 wxString wxMenuBar::GetLabelTop(
881 size_t nPos
882 ) const
883 {
884 wxCHECK_MSG( nPos < GetMenuCount(), wxEmptyString,
885 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
886 return m_titles[nPos];
887 } // end of wxMenuBar::GetLabelTop
888
889 // ---------------------------------------------------------------------------
890 // wxMenuBar construction
891 // ---------------------------------------------------------------------------
892
893 wxMenu* wxMenuBar::Replace(
894 size_t nPos
895 , wxMenu* pMenu
896 , const wxString& rTitle
897 )
898 {
899 SHORT nId;
900 wxString sTitle = wxPMTextToLabel(rTitle);
901 wxMenu* pMenuOld = wxMenuBarBase::Replace( nPos
902 ,pMenu
903 ,sTitle
904 );
905
906
907 nId = SHORT1FROMMR(::WinSendMsg((HWND)m_hMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT(nPos), (MPARAM)0));
908 if (nId == MIT_ERROR)
909 {
910 wxLogLastError("LogLastError");
911 return NULL;
912 }
913 if (!pMenuOld)
914 return NULL;
915 m_titles[nPos] = sTitle;
916 if (IsAttached())
917 {
918 ::WinSendMsg((HWND)m_hMenu, MM_REMOVEITEM, MPFROM2SHORT(nId, TRUE), (MPARAM)0);
919 ::WinSendMsg((HWND)m_hMenu, MM_INSERTITEM, (MPARAM)&pMenu->m_vMenuData, (MPARAM)sTitle.c_str());
920
921 #if wxUSE_ACCEL
922 if (pMenuOld->HasAccels() || pMenu->HasAccels())
923 {
924 //
925 // Need to rebuild accell table
926 //
927 RebuildAccelTable();
928 }
929 #endif // wxUSE_ACCEL
930 Refresh();
931 }
932 return pMenuOld;
933 } // end of wxMenuBar::Replace
934
935 bool wxMenuBar::Insert(
936 size_t nPos
937 , wxMenu* pMenu
938 , const wxString& rTitle
939 )
940 {
941 wxString sTitle = wxPMTextToLabel(rTitle);
942
943 if (!wxMenuBarBase::Insert( nPos
944 ,pMenu
945 ,sTitle
946 ))
947 return FALSE;
948
949 m_titles.Insert( sTitle
950 ,nPos
951 );
952
953 if (IsAttached())
954 {
955 pMenu->m_vMenuData.iPosition = nPos;
956 ::WinSendMsg( (HWND)m_hMenu
957 ,MM_INSERTITEM
958 ,(MPARAM)&pMenu->m_vMenuData
959 ,(MPARAM)sTitle.c_str()
960 );
961 #if wxUSE_ACCEL
962 if (pMenu->HasAccels())
963 {
964 // need to rebuild accell table
965 RebuildAccelTable();
966 }
967 #endif // wxUSE_ACCEL
968 Refresh();
969 }
970 return TRUE;
971 } // end of wxMenuBar::Insert
972
973 bool wxMenuBar::Append(
974 wxMenu* pMenu
975 , const wxString& rsTitle
976 )
977 {
978 WXHMENU hSubmenu = pMenu ? pMenu->GetHMenu() : 0;
979
980 wxCHECK_MSG(hSubmenu, FALSE, wxT("can't append invalid menu to menubar"));
981
982 wxString sTitle = wxPMTextToLabel(rsTitle);
983
984 if (!wxMenuBarBase::Append(pMenu, sTitle))
985 return FALSE;
986
987 m_titles.Add(sTitle);
988
989 if ( IsAttached() )
990 {
991 pMenu->m_vMenuData.iPosition = MIT_END;
992 ::WinSendMsg((HWND)m_hMenu, MM_INSERTITEM, (MPARAM)&pMenu->m_vMenuData, (MPARAM)sTitle.c_str());
993 #if wxUSE_ACCEL
994 if (pMenu->HasAccels())
995 {
996 //
997 // Need to rebuild accell table
998 //
999 RebuildAccelTable();
1000 }
1001 #endif // wxUSE_ACCEL
1002 Refresh();
1003 }
1004 return TRUE;
1005 } // end of wxMenuBar::Append
1006
1007 wxMenu* wxMenuBar::Remove(
1008 size_t nPos
1009 )
1010 {
1011 wxMenu* pMenu = wxMenuBarBase::Remove(nPos);
1012 SHORT nId;
1013
1014 if (!pMenu)
1015 return NULL;
1016
1017 nId = SHORT1FROMMR(::WinSendMsg( (HWND)GetHmenu()
1018 ,MM_ITEMIDFROMPOSITION
1019 ,MPFROMSHORT(nPos)
1020 ,(MPARAM)0)
1021 );
1022 if (nId == MIT_ERROR)
1023 {
1024 wxLogLastError("LogLastError");
1025 return NULL;
1026 }
1027 if (IsAttached())
1028 {
1029 ::WinSendMsg( (HWND)GetHmenu()
1030 ,MM_REMOVEITEM
1031 ,MPFROM2SHORT(nId, TRUE)
1032 ,(MPARAM)0
1033 );
1034
1035 #if wxUSE_ACCEL
1036 if (pMenu->HasAccels())
1037 {
1038 //
1039 // Need to rebuild accell table
1040 //
1041 RebuildAccelTable();
1042 }
1043 #endif // wxUSE_ACCEL
1044 Refresh();
1045 }
1046 m_titles.Remove(nPos);
1047 return pMenu;
1048 } // end of wxMenuBar::Remove
1049
1050 #if wxUSE_ACCEL
1051
1052 void wxMenuBar::RebuildAccelTable()
1053 {
1054 //
1055 // Merge the accelerators of all menus into one accel table
1056 //
1057 size_t nAccelCount = 0;
1058 size_t i;
1059 size_t nCount = GetMenuCount();
1060
1061 for (i = 0; i < nCount; i++)
1062 {
1063 nAccelCount += m_menus[i]->GetAccelCount();
1064 }
1065
1066 if (nAccelCount)
1067 {
1068 wxAcceleratorEntry* pAccelEntries = new wxAcceleratorEntry[nAccelCount];
1069
1070 nAccelCount = 0;
1071 for (i = 0; i < nCount; i++)
1072 {
1073 nAccelCount += m_menus[i]->CopyAccels(&pAccelEntries[nAccelCount]);
1074 }
1075 m_vAccelTable = wxAcceleratorTable( nAccelCount
1076 ,pAccelEntries
1077 );
1078 delete [] pAccelEntries;
1079 }
1080 } // end of wxMenuBar::RebuildAccelTable
1081
1082 #endif // wxUSE_ACCEL
1083
1084 void wxMenuBar::Attach(
1085 wxFrame* pFrame
1086 )
1087 {
1088 wxMenuBarBase::Attach(pFrame);
1089
1090 #if wxUSE_ACCEL
1091 RebuildAccelTable();
1092 //
1093 // Ensure the accelerator table is set to the frame (not the client!)
1094 //
1095 if (!::WinSetAccelTable( vHabmain
1096 ,m_vAccelTable.GetHACCEL()
1097 ,(HWND)pFrame->GetFrame()
1098 ))
1099 wxLogLastError("WinSetAccelTable");
1100 #endif // wxUSE_ACCEL
1101 } // end of wxMenuBar::Attach
1102
1103 void wxMenuBar::Detach()
1104 {
1105 ::WinDestroyWindow((HWND)m_hMenu);
1106 m_hMenu = (WXHMENU)NULL;
1107 m_menuBarFrame = NULL;
1108 } // end of wxMenuBar::Detach
1109
1110 // ---------------------------------------------------------------------------
1111 // wxMenuBar searching for menu items
1112 // ---------------------------------------------------------------------------
1113
1114 //
1115 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
1116 //
1117 int wxMenuBar::FindMenuItem(
1118 const wxString& rMenuString
1119 , const wxString& rItemString
1120 ) const
1121 {
1122 wxString sMenuLabel = wxStripMenuCodes(rMenuString);
1123 size_t nCount = GetMenuCount();
1124
1125 for (size_t i = 0; i < nCount; i++)
1126 {
1127 wxString sTitle = wxStripMenuCodes(m_titles[i]);
1128
1129 if (rMenuString == sTitle)
1130 return m_menus[i]->FindItem(rItemString);
1131 }
1132 return wxNOT_FOUND;
1133 } // end of wxMenuBar::FindMenuItem
1134
1135 wxMenuItem* wxMenuBar::FindItem(
1136 int nId
1137 , wxMenu** ppItemMenu
1138 ) const
1139 {
1140 if (ppItemMenu)
1141 *ppItemMenu = NULL;
1142
1143 wxMenuItem* pItem = NULL;
1144 size_t nCount = GetMenuCount();
1145
1146 for (size_t i = 0; !pItem && (i < nCount); i++)
1147 {
1148 pItem = m_menus[i]->FindItem( nId
1149 ,ppItemMenu
1150 );
1151 }
1152 return pItem;
1153 } // end of wxMenuBar::FindItem
1154
1155 wxMenuItem* wxMenuBar::FindItem(
1156 int nId
1157 , ULONG hItem
1158 , wxMenu** ppItemMenu
1159 ) const
1160 {
1161 if (ppItemMenu)
1162 *ppItemMenu = NULL;
1163
1164 wxMenuItem* pItem = NULL;
1165 size_t nCount = GetMenuCount();
1166
1167 for (size_t i = 0; !pItem && (i < nCount); i++)
1168 {
1169 pItem = m_menus[i]->FindItem( nId
1170 ,hItem
1171 ,ppItemMenu
1172 );
1173 }
1174 return pItem;
1175 } // end of wxMenuBar::FindItem
1176