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