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