]> git.saurik.com Git - wxWidgets.git/blob - src/os2/menu.cpp
various cleanups
[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 = -3;
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=NULL;
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 if (pItem->GetId() == idMenuTitle)
308 {
309 // Item is an unselectable title to be passed via pData
310 rItem.afStyle = MIS_STATIC;
311 }
312 else
313 {
314 //
315 // Menu is just a normal string (passed in data parameter)
316 //
317 rItem.afStyle |= MIS_TEXT;
318 }
319 pData = (char*)pItem->GetText().c_str();
320 }
321
322 if (nPos == (size_t)-1)
323 {
324 rItem.iPosition = MIT_END;
325 }
326 else
327 {
328 rItem.iPosition = nPos;
329 }
330
331 APIRET rc;
332
333 rc = (APIRET)::WinSendMsg( GetHmenu()
334 ,MM_INSERTITEM
335 ,(MPARAM)&rItem
336 ,(MPARAM)pData
337 );
338 #if wxUSE_OWNER_DRAWN
339 if (pItem->IsOwnerDrawn())
340 {
341 MENUITEM vMenuItem;
342
343 ::WinSendMsg( GetHmenu()
344 ,MM_QUERYITEM
345 ,MPFROM2SHORT( (USHORT)pItem->GetId()
346 ,(USHORT)(FALSE)
347 )
348 ,&vMenuItem
349 );
350 }
351 #endif
352 if (rc == (APIRET)MIT_MEMERROR || rc == (APIRET)MIT_ERROR)
353 {
354 vError = ::WinGetLastError(vHabmain);
355 sError = wxPMErrorToStr(vError);
356 wxLogError("Error inserting or appending a menuitem. Error: %s\n", sError.c_str());
357 wxLogLastError("Insert or AppendMenu");
358 return FALSE;
359 }
360 else
361 {
362 //
363 // If we're already attached to the menubar, we must update it
364 //
365 if (IsAttached() && GetMenuBar()->IsAttached())
366 {
367 GetMenuBar()->Refresh();
368 }
369 return TRUE;
370 }
371 return FALSE;
372 } // end of wxMenu::DoInsertOrAppend
373
374 void wxMenu::EndRadioGroup()
375 {
376 //
377 // We're not inside a radio group any longer
378 //
379 m_nStartRadioGroup = -1;
380 } // end of wxMenu::EndRadioGroup
381
382 wxMenuItem* wxMenu::DoAppend(
383 wxMenuItem* pItem
384 )
385 {
386 wxCHECK_MSG( pItem, NULL, _T("NULL item in wxMenu::DoAppend") );
387
388 bool bCheck = FALSE;
389
390 if (pItem->GetKind() == wxITEM_RADIO)
391 {
392 int nCount = GetMenuItemCount();
393
394 if (m_nStartRadioGroup == -1)
395 {
396 //
397 // Start a new radio group
398 //
399 m_nStartRadioGroup = nCount;
400
401 //
402 // For now it has just one element
403 //
404 pItem->SetAsRadioGroupStart();
405 pItem->SetRadioGroupEnd(m_nStartRadioGroup);
406
407 //
408 // Ensure that we have a checked item in the radio group
409 //
410 bCheck = TRUE;
411 }
412 else // extend the current radio group
413 {
414 //
415 // We need to update its end item
416 //
417 pItem->SetRadioGroupStart(m_nStartRadioGroup);
418
419 wxMenuItemList::Node* pNode = GetMenuItems().Item(m_nStartRadioGroup);
420
421 if (pNode)
422 {
423 pNode->GetData()->SetRadioGroupEnd(nCount);
424 }
425 else
426 {
427 wxFAIL_MSG( _T("where is the radio group start item?") );
428 }
429 }
430 }
431 else // not a radio item
432 {
433 EndRadioGroup();
434 }
435
436 if (!wxMenuBase::DoAppend(pItem) || !DoInsertOrAppend(pItem))
437 {
438 return NULL;
439 }
440 if (bCheck)
441 {
442 //
443 // Check the item initially
444 //
445 pItem->Check(TRUE);
446 }
447 return pItem;
448 } // end of wxMenu::DoAppend
449
450 wxMenuItem* wxMenu::DoInsert(
451 size_t nPos
452 , wxMenuItem* pItem
453 )
454 {
455 if ( wxMenuBase::DoInsert( nPos
456 ,pItem) &&
457 DoInsertOrAppend( pItem
458 ,nPos
459 ))
460 return pItem;
461 else
462 return NULL;
463 } // end of wxMenu::DoInsert
464
465 wxMenuItem* wxMenu::DoRemove(
466 wxMenuItem* pItem
467 )
468 {
469 //
470 // We need to find the items position in the child list
471 //
472 size_t nPos;
473 wxMenuItemList::Node* pNode = GetMenuItems().GetFirst();
474
475 for (nPos = 0; pNode; nPos++)
476 {
477 if (pNode->GetData() == pItem)
478 break;
479 pNode = pNode->GetNext();
480 }
481
482 //
483 // DoRemove() (unlike Remove) can only be called for existing item!
484 //
485 wxCHECK_MSG(pNode, NULL, wxT("bug in wxMenu::Remove logic"));
486
487 #if wxUSE_ACCEL
488 //
489 // Remove the corresponding accel from the accel table
490 //
491 int n = FindAccel(pItem->GetId());
492
493 if (n != wxNOT_FOUND)
494 {
495 delete m_vAccels[n];
496 m_vAccels.RemoveAt(n);
497 }
498
499 #endif // wxUSE_ACCEL
500 //
501 // Remove the item from the menu
502 //
503 ::WinSendMsg( GetHmenu()
504 ,MM_REMOVEITEM
505 ,MPFROM2SHORT(pItem->GetId(), TRUE)
506 ,(MPARAM)0
507 );
508 if (IsAttached() && GetMenuBar()->IsAttached())
509 {
510 //
511 // Otherwise, the chane won't be visible
512 //
513 GetMenuBar()->Refresh();
514 }
515
516 //
517 // And from internal data structures
518 //
519 return wxMenuBase::DoRemove(pItem);
520 } // end of wxMenu::DoRemove
521
522 // ---------------------------------------------------------------------------
523 // accelerator helpers
524 // ---------------------------------------------------------------------------
525
526 #if wxUSE_ACCEL
527
528 //
529 // Create the wxAcceleratorEntries for our accels and put them into provided
530 // array - return the number of accels we have
531 //
532 size_t wxMenu::CopyAccels(
533 wxAcceleratorEntry* pAccels
534 ) const
535 {
536 size_t nCount = GetAccelCount();
537
538 for (size_t n = 0; n < nCount; n++)
539 {
540 *pAccels++ = *m_vAccels[n];
541 }
542 return nCount;
543 } // end of wxMenu::CopyAccels
544
545 #endif // wxUSE_ACCEL
546
547 // ---------------------------------------------------------------------------
548 // set wxMenu title
549 // ---------------------------------------------------------------------------
550
551 void wxMenu::SetTitle(
552 const wxString& rLabel
553 )
554 {
555 bool bHasNoTitle = m_title.IsEmpty();
556 HWND hMenu = GetHmenu();
557
558 m_title = rLabel;
559 if (bHasNoTitle)
560 {
561 if (!rLabel.IsEmpty())
562 {
563 if (!::WinSetWindowText(hMenu, rLabel.c_str()))
564 {
565 wxLogLastError("SetMenuTitle");
566 }
567 }
568 }
569 else
570 {
571 if (rLabel.IsEmpty() )
572 {
573 ::WinSendMsg( GetHmenu()
574 ,MM_REMOVEITEM
575 ,MPFROM2SHORT(hMenu, TRUE)
576 ,(MPARAM)0
577 );
578 }
579 else
580 {
581 //
582 // Modify the title
583 //
584 if (!::WinSetWindowText(hMenu, rLabel.c_str()))
585 {
586 wxLogLastError("SetMenuTitle");
587 }
588 }
589 }
590 } // end of wxMenu::SetTitle
591
592 // ---------------------------------------------------------------------------
593 // event processing
594 // ---------------------------------------------------------------------------
595
596 bool wxMenu::OS2Command(
597 WXUINT WXUNUSED(uParam)
598 , WXWORD vId
599 )
600 {
601 //
602 // Ignore commands from the menu title
603 //
604
605 if (vId != (WXWORD)idMenuTitle)
606 {
607 SendEvent( vId
608 ,(int)::WinSendMsg( GetHmenu()
609 ,MM_QUERYITEMATTR
610 ,MPFROMSHORT(vId)
611 ,(MPARAM)MIA_CHECKED
612 )
613 );
614 }
615 return TRUE;
616 } // end of wxMenu::OS2Command
617
618 // ---------------------------------------------------------------------------
619 // other
620 // ---------------------------------------------------------------------------
621
622 wxWindow* wxMenu::GetWindow() const
623 {
624 if (m_invokingWindow != NULL)
625 return m_invokingWindow;
626 else if ( GetMenuBar() != NULL)
627 return GetMenuBar()->GetFrame();
628
629 return NULL;
630 } // end of wxMenu::GetWindow
631
632 // recursive search for item by id
633 wxMenuItem* wxMenu::FindItem(
634 int nItemId
635 , ULONG hItem
636 , wxMenu** ppItemMenu
637 ) const
638 {
639 if ( ppItemMenu )
640 *ppItemMenu = NULL;
641
642 wxMenuItem* pItem = NULL;
643
644 for ( wxMenuItemList::Node *node = m_items.GetFirst();
645 node && !pItem;
646 node = node->GetNext() )
647 {
648 pItem = node->GetData();
649
650 if ( pItem->GetId() == nItemId && pItem->m_vMenuData.hItem == hItem)
651 {
652 if ( ppItemMenu )
653 *ppItemMenu = (wxMenu *)this;
654 }
655 else if ( pItem->IsSubMenu() )
656 {
657 pItem = pItem->GetSubMenu()->FindItem( nItemId
658 ,hItem
659 ,ppItemMenu
660 );
661 if (pItem)
662 break;
663 }
664 else
665 {
666 // don't exit the loop
667 pItem = NULL;
668 }
669 }
670 return pItem;
671 } // end of wxMenu::FindItem
672
673 // ---------------------------------------------------------------------------
674 // Menu Bar
675 // ---------------------------------------------------------------------------
676
677 void wxMenuBar::Init()
678 {
679 m_eventHandler = this;
680 m_menuBarFrame = NULL;
681 m_hMenu = 0;
682 } // end of wxMenuBar::Init
683
684 wxMenuBar::wxMenuBar()
685 {
686 Init();
687 } // end of wxMenuBar::wxMenuBar
688
689 wxMenuBar::wxMenuBar(
690 long WXUNUSED(lStyle)
691 )
692 {
693 Init();
694 } // end of wxMenuBar::wxMenuBar
695
696 wxMenuBar::wxMenuBar(
697 int nCount
698 , wxMenu* vMenus[]
699 , const wxString sTitles[]
700 )
701 {
702 Init();
703
704 m_titles.Alloc(nCount);
705 for ( int i = 0; i < nCount; i++ )
706 {
707 m_menus.Append(vMenus[i]);
708 m_titles.Add(sTitles[i]);
709 vMenus[i]->Attach(this);
710 }
711 } // end of wxMenuBar::wxMenuBar
712
713 wxMenuBar::~wxMenuBar()
714 {
715 //
716 // We should free PM's resources only if PM doesn't do it for us
717 // which happens if we're attached to a frame
718 //
719 if (m_hMenu && !IsAttached())
720 {
721 ::WinDestroyWindow((HMENU)m_hMenu);
722 m_hMenu = (WXHMENU)NULL;
723 }
724 } // end of wxMenuBar::~wxMenuBar
725
726 // ---------------------------------------------------------------------------
727 // wxMenuBar helpers
728 // ---------------------------------------------------------------------------
729
730 void wxMenuBar::Refresh()
731 {
732 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
733
734 WinSendMsg(GetWinHwnd(m_menuBarFrame), WM_UPDATEFRAME, (MPARAM)FCF_MENU, (MPARAM)0);
735 } // end of wxMenuBar::Refresh
736
737 WXHMENU wxMenuBar::Create()
738 {
739 HWND hFrame;
740
741 if (m_hMenu != 0 )
742 return m_hMenu;
743
744 wxCHECK_MSG(!m_hMenu, TRUE, wxT("menubar already created"));
745
746 //
747 // Menubars should be associated with a frame otherwise they are popups
748 //
749 if (m_menuBarFrame != NULL)
750 hFrame = GetWinHwnd(m_menuBarFrame);
751 else
752 hFrame = HWND_DESKTOP;
753 //
754 // Create an empty menu and then fill it with insertions
755 //
756 if ((m_hMenu = ::WinCreateWindow( hFrame
757 ,WC_MENU
758 ,(PSZ)NULL
759 ,MS_ACTIONBAR | WS_SYNCPAINT | WS_VISIBLE
760 ,0L
761 ,0L
762 ,0L
763 ,0L
764 ,hFrame
765 ,HWND_TOP
766 ,FID_MENU
767 ,NULL
768 ,NULL
769 )) == 0)
770 {
771 wxLogLastError("WinLoadMenu");
772 }
773 else
774 {
775 size_t nCount = GetMenuCount();
776
777 for (size_t i = 0; i < nCount; i++)
778 {
779 APIRET rc;
780 ERRORID vError;
781 wxString sError;
782 HWND hSubMenu;
783
784 //
785 // Set the parent and owner of the submenues to be the menubar, not the desktop
786 //
787 hSubMenu = m_menus[i]->m_vMenuData.hwndSubMenu;
788 if (!::WinSetParent(m_menus[i]->m_vMenuData.hwndSubMenu, m_hMenu, FALSE))
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 if (!::WinSetOwner(m_menus[i]->m_vMenuData.hwndSubMenu, m_hMenu))
797 {
798 vError = ::WinGetLastError(vHabmain);
799 sError = wxPMErrorToStr(vError);
800 wxLogError("Error setting parent for submenu. Error: %s\n", sError.c_str());
801 return NULLHANDLE;
802 }
803
804 m_menus[i]->m_vMenuData.iPosition = i;
805
806 rc = (APIRET)::WinSendMsg(m_hMenu, MM_INSERTITEM, (MPARAM)&m_menus[i]->m_vMenuData, (MPARAM)m_titles[i].c_str());
807 if (rc == (APIRET)MIT_MEMERROR || rc == (APIRET)MIT_ERROR)
808 {
809 vError = ::WinGetLastError(vHabmain);
810 sError = wxPMErrorToStr(vError);
811 wxLogError("Error inserting or appending a menuitem. Error: %s\n", sError.c_str());
812 return NULLHANDLE;
813 }
814 }
815 }
816 return m_hMenu;
817 } // end of wxMenuBar::Create
818
819 // ---------------------------------------------------------------------------
820 // wxMenuBar functions to work with the top level submenus
821 // ---------------------------------------------------------------------------
822
823 //
824 // NB: we don't support owner drawn top level items for now, if we do these
825 // functions would have to be changed to use wxMenuItem as well
826 //
827 void wxMenuBar::EnableTop(
828 size_t nPos
829 , bool bEnable
830 )
831 {
832 wxCHECK_RET(IsAttached(), wxT("doesn't work with unattached menubars"));
833 USHORT uFlag = 0;
834 SHORT nId;
835
836 if(!bEnable)
837 uFlag = MIA_DISABLED;
838
839 nId = SHORT1FROMMR(::WinSendMsg((HWND)m_hMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT(nPos), (MPARAM)0));
840 if (nId == MIT_ERROR)
841 {
842 wxLogLastError("LogLastError");
843 return;
844 }
845 ::WinSendMsg((HWND)m_hMenu, MM_SETITEMATTR, MPFROM2SHORT(nId, TRUE), MPFROM2SHORT(MIA_DISABLED, uFlag));
846 Refresh();
847 } // end of wxMenuBar::EnableTop
848
849 void wxMenuBar::SetLabelTop(
850 size_t nPos
851 , const wxString& rLabel
852 )
853 {
854 SHORT nId;
855 MENUITEM vItem;
856
857 wxCHECK_RET(nPos < GetMenuCount(), wxT("invalid menu index"));
858 m_titles[nPos] = rLabel;
859
860 if (!IsAttached())
861 {
862 return;
863 }
864
865 nId = SHORT1FROMMR(::WinSendMsg((HWND)m_hMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT(nPos), (MPARAM)0));
866 if (nId == MIT_ERROR)
867 {
868 wxLogLastError("LogLastError");
869 return;
870 }
871 if(!::WinSendMsg( (HWND)m_hMenu
872 ,MM_QUERYITEM
873 ,MPFROM2SHORT(nId, TRUE)
874 ,MPARAM(&vItem)
875 ))
876 {
877 wxLogLastError("QueryItem");
878 }
879 nId = vItem.id;
880
881 if (::WinSendMsg(GetHmenu(), MM_SETITEMTEXT, MPFROMSHORT(nId), (MPARAM)rLabel.c_str()));
882 {
883 wxLogLastError("ModifyMenu");
884 }
885 Refresh();
886 } // end of wxMenuBar::SetLabelTop
887
888 wxString wxMenuBar::GetLabelTop(
889 size_t nPos
890 ) const
891 {
892 wxCHECK_MSG( nPos < GetMenuCount(), wxEmptyString,
893 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
894 return m_titles[nPos];
895 } // end of wxMenuBar::GetLabelTop
896
897 // ---------------------------------------------------------------------------
898 // wxMenuBar construction
899 // ---------------------------------------------------------------------------
900
901 wxMenu* wxMenuBar::Replace(
902 size_t nPos
903 , wxMenu* pMenu
904 , const wxString& rTitle
905 )
906 {
907 SHORT nId;
908 wxString sTitle = wxPMTextToLabel(rTitle);
909 wxMenu* pMenuOld = wxMenuBarBase::Replace( nPos
910 ,pMenu
911 ,sTitle
912 );
913
914
915 nId = SHORT1FROMMR(::WinSendMsg((HWND)m_hMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT(nPos), (MPARAM)0));
916 if (nId == MIT_ERROR)
917 {
918 wxLogLastError("LogLastError");
919 return NULL;
920 }
921 if (!pMenuOld)
922 return NULL;
923 m_titles[nPos] = sTitle;
924 if (IsAttached())
925 {
926 ::WinSendMsg((HWND)m_hMenu, MM_REMOVEITEM, MPFROM2SHORT(nId, TRUE), (MPARAM)0);
927 ::WinSendMsg((HWND)m_hMenu, MM_INSERTITEM, (MPARAM)&pMenu->m_vMenuData, (MPARAM)sTitle.c_str());
928
929 #if wxUSE_ACCEL
930 if (pMenuOld->HasAccels() || pMenu->HasAccels())
931 {
932 //
933 // Need to rebuild accell table
934 //
935 RebuildAccelTable();
936 }
937 #endif // wxUSE_ACCEL
938 Refresh();
939 }
940 return pMenuOld;
941 } // end of wxMenuBar::Replace
942
943 bool wxMenuBar::Insert(
944 size_t nPos
945 , wxMenu* pMenu
946 , const wxString& rTitle
947 )
948 {
949 wxString sTitle = wxPMTextToLabel(rTitle);
950
951 if (!wxMenuBarBase::Insert( nPos
952 ,pMenu
953 ,sTitle
954 ))
955 return FALSE;
956
957 m_titles.Insert( sTitle
958 ,nPos
959 );
960
961 if (IsAttached())
962 {
963 pMenu->m_vMenuData.iPosition = nPos;
964 ::WinSendMsg( (HWND)m_hMenu
965 ,MM_INSERTITEM
966 ,(MPARAM)&pMenu->m_vMenuData
967 ,(MPARAM)sTitle.c_str()
968 );
969 #if wxUSE_ACCEL
970 if (pMenu->HasAccels())
971 {
972 // need to rebuild accell table
973 RebuildAccelTable();
974 }
975 #endif // wxUSE_ACCEL
976 Refresh();
977 }
978 return TRUE;
979 } // end of wxMenuBar::Insert
980
981 bool wxMenuBar::Append(
982 wxMenu* pMenu
983 , const wxString& rsTitle
984 )
985 {
986 WXHMENU hSubmenu = pMenu ? pMenu->GetHMenu() : 0;
987
988 wxCHECK_MSG(hSubmenu, FALSE, wxT("can't append invalid menu to menubar"));
989
990 wxString sTitle = wxPMTextToLabel(rsTitle);
991
992 if (!wxMenuBarBase::Append(pMenu, sTitle))
993 return FALSE;
994
995 m_titles.Add(sTitle);
996
997 if ( IsAttached() )
998 {
999 pMenu->m_vMenuData.iPosition = MIT_END;
1000 ::WinSendMsg((HWND)m_hMenu, MM_INSERTITEM, (MPARAM)&pMenu->m_vMenuData, (MPARAM)sTitle.c_str());
1001 #if wxUSE_ACCEL
1002 if (pMenu->HasAccels())
1003 {
1004 //
1005 // Need to rebuild accell table
1006 //
1007 RebuildAccelTable();
1008 }
1009 #endif // wxUSE_ACCEL
1010 Refresh();
1011 }
1012 return TRUE;
1013 } // end of wxMenuBar::Append
1014
1015 wxMenu* wxMenuBar::Remove(
1016 size_t nPos
1017 )
1018 {
1019 wxMenu* pMenu = wxMenuBarBase::Remove(nPos);
1020 SHORT nId;
1021
1022 if (!pMenu)
1023 return NULL;
1024
1025 nId = SHORT1FROMMR(::WinSendMsg( (HWND)GetHmenu()
1026 ,MM_ITEMIDFROMPOSITION
1027 ,MPFROMSHORT(nPos)
1028 ,(MPARAM)0)
1029 );
1030 if (nId == MIT_ERROR)
1031 {
1032 wxLogLastError("LogLastError");
1033 return NULL;
1034 }
1035 if (IsAttached())
1036 {
1037 ::WinSendMsg( (HWND)GetHmenu()
1038 ,MM_REMOVEITEM
1039 ,MPFROM2SHORT(nId, TRUE)
1040 ,(MPARAM)0
1041 );
1042
1043 #if wxUSE_ACCEL
1044 if (pMenu->HasAccels())
1045 {
1046 //
1047 // Need to rebuild accell table
1048 //
1049 RebuildAccelTable();
1050 }
1051 #endif // wxUSE_ACCEL
1052 Refresh();
1053 }
1054 m_titles.Remove(nPos);
1055 return pMenu;
1056 } // end of wxMenuBar::Remove
1057
1058 #if wxUSE_ACCEL
1059
1060 void wxMenuBar::RebuildAccelTable()
1061 {
1062 //
1063 // Merge the accelerators of all menus into one accel table
1064 //
1065 size_t nAccelCount = 0;
1066 size_t i;
1067 size_t nCount = GetMenuCount();
1068
1069 for (i = 0; i < nCount; i++)
1070 {
1071 nAccelCount += m_menus[i]->GetAccelCount();
1072 }
1073
1074 if (nAccelCount)
1075 {
1076 wxAcceleratorEntry* pAccelEntries = new wxAcceleratorEntry[nAccelCount];
1077
1078 nAccelCount = 0;
1079 for (i = 0; i < nCount; i++)
1080 {
1081 nAccelCount += m_menus[i]->CopyAccels(&pAccelEntries[nAccelCount]);
1082 }
1083 m_vAccelTable = wxAcceleratorTable( nAccelCount
1084 ,pAccelEntries
1085 );
1086 delete [] pAccelEntries;
1087 }
1088 } // end of wxMenuBar::RebuildAccelTable
1089
1090 #endif // wxUSE_ACCEL
1091
1092 void wxMenuBar::Attach(
1093 wxFrame* pFrame
1094 )
1095 {
1096 wxMenuBarBase::Attach(pFrame);
1097
1098 #if wxUSE_ACCEL
1099 RebuildAccelTable();
1100 //
1101 // Ensure the accelerator table is set to the frame (not the client!)
1102 //
1103 if (!::WinSetAccelTable( vHabmain
1104 ,m_vAccelTable.GetHACCEL()
1105 ,(HWND)pFrame->GetFrame()
1106 ))
1107 wxLogLastError("WinSetAccelTable");
1108 #endif // wxUSE_ACCEL
1109 } // end of wxMenuBar::Attach
1110
1111 void wxMenuBar::Detach()
1112 {
1113 ::WinDestroyWindow((HWND)m_hMenu);
1114 m_hMenu = (WXHMENU)NULL;
1115 m_menuBarFrame = NULL;
1116 } // end of wxMenuBar::Detach
1117
1118 // ---------------------------------------------------------------------------
1119 // wxMenuBar searching for menu items
1120 // ---------------------------------------------------------------------------
1121
1122 //
1123 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
1124 //
1125 int wxMenuBar::FindMenuItem(
1126 const wxString& rMenuString
1127 , const wxString& rItemString
1128 ) const
1129 {
1130 wxString sMenuLabel = wxStripMenuCodes(rMenuString);
1131 size_t nCount = GetMenuCount();
1132
1133 for (size_t i = 0; i < nCount; i++)
1134 {
1135 wxString sTitle = wxStripMenuCodes(m_titles[i]);
1136
1137 if (rMenuString == sTitle)
1138 return m_menus[i]->FindItem(rItemString);
1139 }
1140 return wxNOT_FOUND;
1141 } // end of wxMenuBar::FindMenuItem
1142
1143 wxMenuItem* wxMenuBar::FindItem(
1144 int nId
1145 , wxMenu** ppItemMenu
1146 ) const
1147 {
1148 if (ppItemMenu)
1149 *ppItemMenu = NULL;
1150
1151 wxMenuItem* pItem = NULL;
1152 size_t nCount = GetMenuCount();
1153
1154 for (size_t i = 0; !pItem && (i < nCount); i++)
1155 {
1156 pItem = m_menus[i]->FindItem( nId
1157 ,ppItemMenu
1158 );
1159 }
1160 return pItem;
1161 } // end of wxMenuBar::FindItem
1162
1163 wxMenuItem* wxMenuBar::FindItem(
1164 int nId
1165 , ULONG hItem
1166 , wxMenu** ppItemMenu
1167 ) const
1168 {
1169 if (ppItemMenu)
1170 *ppItemMenu = NULL;
1171
1172 wxMenuItem* pItem = NULL;
1173 size_t nCount = GetMenuCount();
1174
1175 for (size_t i = 0; !pItem && (i < nCount); i++)
1176 {
1177 pItem = m_menus[i]->FindItem( nId
1178 ,hItem
1179 ,ppItemMenu
1180 );
1181 }
1182 return pItem;
1183 } // end of wxMenuBar::FindItem
1184