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