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