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