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