1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMenu, wxMenuBar, wxMenuItem
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "menu.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/ownerdrw.h"
32 #include "wx/os2/private.h"
34 // other standard headers
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 extern wxMenu
* wxCurrentPopupMenu
;
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
48 // The (popup) menu title has this special id
50 static const int idMenuTitle
= -2;
53 // The unique ID for Menus
56 USHORT
wxMenu::m_nextMenuId
= 0;
58 static USHORT
wxMenu::m_nextMenuId
= 0;
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 IMPLEMENT_DYNAMIC_CLASS(wxMenu
, wxEvtHandler
)
66 IMPLEMENT_DYNAMIC_CLASS(wxMenuBar
, wxEvtHandler
)
68 // ----------------------------------------------------------------------------
69 // static function for translating menu labels
70 // ----------------------------------------------------------------------------
72 static wxString
TextToLabel(const wxString
& rTitle
)
76 for (pc
= rTitle
; *pc
!= wxT('\0'); pc
++ )
80 if (*(pc
+1) == wxT('&'))
88 // else if (*pc == wxT('/'))
90 // Title << wxT('\\');
94 if ( *pc
== wxT('~') )
96 // tildes must be doubled to prevent them from being
97 // interpreted as accelerator character prefix by PM ???
106 // ============================================================================
108 // ============================================================================
110 // ---------------------------------------------------------------------------
111 // wxMenu construction, adding and removing menu items
112 // ---------------------------------------------------------------------------
115 // Construct a menu with optional title (then use append)
122 // Create the menu (to be used as a submenu or a popup)
124 if ((m_hMenu
= ::WinCreateWindow( HWND_DESKTOP
139 wxLogLastError("WinLoadMenu");
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
;
149 // If we have a title, insert it in the beginning of the menu
151 if (!m_title
.IsEmpty())
158 } // end of wxMenu::Init
161 // The wxWindow destructor will take care of deleting the submenus.
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
169 if (!IsAttached() && !GetParent())
171 if (!::WinDestroyWindow((HWND
)GetHmenu()) )
173 wxLogLastError("WinDestroyWindow");
181 #if (!(defined(__VISAGECPP__) && (__IBMCPP__ < 400 || __IBMC__ < 400 )))
182 WX_CLEAR_ARRAY(m_vAccels
);
184 #endif // wxUSE_ACCEL
185 } // end of wxMenu::~wxMenu
189 // this will take effect during the next call to Append()
191 } // end of wxMenu::Break
195 int wxMenu::FindAccel(
200 size_t nCount
= m_vAccels
.GetCount();
202 for (n
= 0; n
< nCount
; n
++)
204 if (m_vAccels
[n
]->m_command
== nId
)
208 } // end of wxMenu::FindAccel
210 void wxMenu::UpdateAccel(
215 // Find the (new) accel for this item
217 wxAcceleratorEntry
* pAccel
= wxGetAccelFromString(pItem
->GetText());
220 pAccel
->m_command
= pItem
->GetId();
225 int n
= FindAccel(pItem
->GetId());
227 if (n
== wxNOT_FOUND
)
230 // No old, add new if any
233 m_vAccels
.Add(pAccel
);
235 return; // skipping RebuildAccelTable() below
240 // Replace old with new or just remove the old one if no new
245 m_vAccels
[n
] = pAccel
;
252 m_menuBar
->RebuildAccelTable();
254 } // wxMenu::UpdateAccel
256 #endif // wxUSE_ACCEL
259 // Append a new item or submenu to the menu
261 bool wxMenu::DoInsertOrAppend(
270 #endif // wxUSE_ACCEL
273 // rItem is the member MENUITEM for the menu items and the submenu's
274 // MENUITEM for submenus as required by ::MM_INSERTITEM message API
277 wxMenu
* pSubmenu
= pItem
->GetSubMenu();
278 MENUITEM
& rItem
= (pSubmenu
!= NULL
)?pSubmenu
->m_vMenuData
:
282 wxASSERT_MSG(pSubmenu
->GetHMenu(), wxT("invalid submenu"));
283 pSubmenu
->SetParent(this);
284 rItem
.afStyle
|= MIS_SUBMENU
| MIS_TEXT
;
288 // If "Break" has just been called, insert a menu break before this item
289 // (and don't forget to reset the flag)
293 rItem
.afStyle
|= MIS_BREAK
;
297 if (pItem
->IsSeparator())
299 rItem
.afStyle
|= MIS_SEPARATOR
;
303 // Id is the numeric id for normal menu items and HMENU for submenus as
304 // required by ::MM_INSERTITEM message API
307 if (pSubmenu
!= NULL
)
309 wxASSERT_MSG(pSubmenu
->GetHMenu(), wxT("invalid submenu"));
310 pSubmenu
->SetParent(this);
312 rItem
.iPosition
= 0; // submenus have a 0 position
313 rItem
.id
= (USHORT
)pSubmenu
->GetHMenu();
314 rItem
.afStyle
|= MIS_SUBMENU
| MIS_TEXT
;
318 rItem
.id
= pItem
->GetId();
323 #if wxUSE_OWNER_DRAWN
324 if (pItem
->IsOwnerDrawn())
327 // Want to get {Measure|Draw}Item messages?
328 // item draws itself, pass pointer to it in data parameter
329 // Will eventually need to set the image handle somewhere into vItem.hItem
331 rItem
.afStyle
|= MIS_OWNERDRAW
;
332 pData
= (BYTE
*)pItem
;
333 // vItem.hItem = ????
339 // Menu is just a normal string (passed in data parameter)
341 rItem
.afStyle
|= MIS_TEXT
;
342 pData
= (char*)pItem
->GetText().c_str();
345 if (nPos
== (size_t)-1)
347 rItem
.iPosition
= MIT_END
;
351 rItem
.iPosition
= nPos
;
356 rc
= (APIRET
)::WinSendMsg( GetHmenu()
361 if (rc
== MIT_MEMERROR
|| rc
== MIT_ERROR
)
363 vError
= ::WinGetLastError(vHabmain
);
364 sError
= wxPMErrorToStr(vError
);
365 wxLogError("Error inserting or appending a menuitem. Error: %s\n", sError
);
366 wxLogLastError("Insert or AppendMenu");
372 // If we're already attached to the menubar, we must update it
376 m_menuBar
->Refresh();
381 } // end of wxMenu::DoInsertOrAppend
383 bool wxMenu::DoAppend(
387 return wxMenuBase::DoAppend(pItem
) && DoInsertOrAppend(pItem
);
390 bool wxMenu::DoInsert(
395 return ( wxMenuBase::DoInsert( nPos
397 DoInsertOrAppend( pItem
400 } // end of wxMenu::DoInsert
402 wxMenuItem
* wxMenu::DoRemove(
407 // We need to find the items position in the child list
410 wxMenuItemList::Node
* pNode
= GetMenuItems().GetFirst();
412 for (nPos
= 0; pNode
; nPos
++)
414 if (pNode
->GetData() == pItem
)
416 pNode
= pNode
->GetNext();
420 // DoRemove() (unlike Remove) can only be called for existing item!
422 wxCHECK_MSG(pNode
, NULL
, wxT("bug in wxMenu::Remove logic"));
426 // Remove the corresponding accel from the accel table
428 int n
= FindAccel(pItem
->GetId());
430 if (n
!= wxNOT_FOUND
)
436 #endif // wxUSE_ACCEL
438 // Remove the item from the menu
440 ::WinSendMsg( GetHmenu()
442 ,MPFROM2SHORT(pItem
->GetId(), TRUE
)
448 // Otherwise, the chane won't be visible
450 m_menuBar
->Refresh();
454 // And from internal data structures
456 return wxMenuBase::DoRemove(pItem
);
457 } // end of wxMenu::DoRemove
459 // ---------------------------------------------------------------------------
460 // accelerator helpers
461 // ---------------------------------------------------------------------------
466 // Create the wxAcceleratorEntries for our accels and put them into provided
467 // array - return the number of accels we have
469 size_t wxMenu::CopyAccels(
470 wxAcceleratorEntry
* pAccels
473 size_t nCount
= GetAccelCount();
475 for (size_t n
= 0; n
< nCount
; n
++)
477 *pAccels
++ = *m_vAccels
[n
];
480 } // end of wxMenu::CopyAccels
482 #endif // wxUSE_ACCEL
484 // ---------------------------------------------------------------------------
486 // ---------------------------------------------------------------------------
488 void wxMenu::SetTitle(
489 const wxString
& rLabel
492 bool bHasNoTitle
= m_title
.IsEmpty();
493 HWND hMenu
= GetHmenu();
498 if (!rLabel
.IsEmpty())
500 if (!::WinSetWindowText(hMenu
, rLabel
.c_str()))
502 wxLogLastError("SetMenuTitle");
508 if (rLabel
.IsEmpty() )
510 ::WinSendMsg( GetHmenu()
512 ,MPFROM2SHORT(hMenu
, TRUE
)
521 if (!::WinSetWindowText(hMenu
, rLabel
.c_str()))
523 wxLogLastError("SetMenuTitle");
527 } // end of wxMenu::SetTitle
529 // ---------------------------------------------------------------------------
531 // ---------------------------------------------------------------------------
533 bool wxMenu::OS2Command(
534 WXUINT
WXUNUSED(uParam
)
539 // Ignore commands from the menu title
542 if (vId
!= (WXWORD
)idMenuTitle
)
544 wxCommandEvent
vEvent(wxEVT_COMMAND_MENU_SELECTED
);
546 vEvent
.SetEventObject(this);
549 ProcessCommand(vEvent
);
552 } // end of wxMenu::OS2Command
554 bool wxMenu::ProcessCommand(
555 wxCommandEvent
& rEvent
558 bool bProcessed
= FALSE
;
560 #if wxUSE_MENU_CALLBACK
566 (void)(*(m_callback
))(*this, rEvent
);
569 #endif // wxUSE_MENU_CALLBACK
572 // Try the menu's event handler
574 if (!bProcessed
&& GetEventHandler())
576 bProcessed
= GetEventHandler()->ProcessEvent(rEvent
);
580 // Try the window the menu was popped up from (and up through the
582 wxWindow
* pWin
= GetInvokingWindow();
584 if (!bProcessed
&& pWin
)
585 bProcessed
= pWin
->GetEventHandler()->ProcessEvent(rEvent
);
587 } // end of wxMenu::ProcessCommand
589 // ---------------------------------------------------------------------------
591 // ---------------------------------------------------------------------------
598 // Menu can be in at most one menubar because otherwise they would both
599 // delete the menu pointer
601 wxASSERT_MSG(!m_menuBar
, wxT("menu belongs to 2 menubars, expect a crash"));
602 m_menuBar
= pMenubar
;
605 void wxMenu::Detach()
607 wxASSERT_MSG( m_menuBar
, wxT("can't detach menu if it's not attached") );
609 } // end of wxMenu::Detach
611 wxWindow
* wxMenu::GetWindow() const
613 if (m_invokingWindow
!= NULL
)
614 return m_invokingWindow
;
615 else if ( m_menuBar
!= NULL
)
616 return m_menuBar
->GetFrame();
619 } // end of wxMenu::GetWindow
621 // ---------------------------------------------------------------------------
623 // ---------------------------------------------------------------------------
625 void wxMenuBar::Init()
627 m_eventHandler
= this;
628 m_pMenuBarFrame
= NULL
;
630 } // end of wxMenuBar::Init
632 wxMenuBar::wxMenuBar()
635 } // end of wxMenuBar::wxMenuBar
637 wxMenuBar::wxMenuBar(
638 long WXUNUSED(lStyle
)
642 } // end of wxMenuBar::wxMenuBar
644 wxMenuBar::wxMenuBar(
647 , const wxString sTitles
[]
652 m_titles
.Alloc(nCount
);
653 for ( int i
= 0; i
< nCount
; i
++ )
655 m_menus
.Append(vMenus
[i
]);
656 m_titles
.Add(sTitles
[i
]);
657 vMenus
[i
]->Attach(this);
659 } // end of wxMenuBar::wxMenuBar
661 wxMenuBar::~wxMenuBar()
663 } // end of wxMenuBar::~wxMenuBar
665 // ---------------------------------------------------------------------------
667 // ---------------------------------------------------------------------------
669 void wxMenuBar::Refresh()
671 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
673 WinSendMsg(GetWinHwnd(m_pMenuBarFrame
), WM_UPDATEFRAME
, (MPARAM
)FCF_MENU
, (MPARAM
)0);
674 } // end of wxMenuBar::Refresh
676 WXHMENU
wxMenuBar::Create()
684 wxCHECK_MSG(!m_hMenu
, TRUE
, wxT("menubar already created"));
687 // Menubars should be associated with a frame otherwise they are popups
689 if (m_pMenuBarFrame
!= NULL
)
690 hFrame
= GetWinHwnd(m_pMenuBarFrame
);
692 hFrame
= HWND_DESKTOP
;
694 // Create an empty menu and then fill it with insertions
696 if ((m_hMenu
= ::WinCreateWindow( hFrame
699 ,MS_ACTIONBAR
| WS_SYNCPAINT
| WS_VISIBLE
711 wxLogLastError("WinLoadMenu");
715 size_t nCount
= GetMenuCount();
717 for (size_t i
= 0; i
< nCount
; i
++)
725 // Set the parent and owner of the submenues to be the menubar, not the desktop
727 hSubMenu
= m_menus
[i
]->m_vMenuData
.hwndSubMenu
;
728 if (!::WinSetParent(m_menus
[i
]->m_vMenuData
.hwndSubMenu
, m_hMenu
, FALSE
))
730 vError
= ::WinGetLastError(vHabmain
);
731 sError
= wxPMErrorToStr(vError
);
732 wxLogError("Error setting parent for submenu. Error: %s\n", sError
);
736 if (!::WinSetOwner(m_menus
[i
]->m_vMenuData
.hwndSubMenu
, m_hMenu
))
738 vError
= ::WinGetLastError(vHabmain
);
739 sError
= wxPMErrorToStr(vError
);
740 wxLogError("Error setting parent for submenu. Error: %s\n", sError
);
744 m_menus
[i
]->m_vMenuData
.iPosition
= i
;
746 rc
= (APIRET
)::WinSendMsg(m_hMenu
, MM_INSERTITEM
, (MPARAM
)&m_menus
[i
]->m_vMenuData
, (MPARAM
)m_titles
[i
].c_str());
747 if (rc
== MIT_MEMERROR
|| rc
== MIT_ERROR
)
749 vError
= ::WinGetLastError(vHabmain
);
750 sError
= wxPMErrorToStr(vError
);
751 wxLogError("Error inserting or appending a menuitem. Error: %s\n", sError
);
757 } // end of wxMenuBar::Create
759 // ---------------------------------------------------------------------------
760 // wxMenuBar functions to work with the top level submenus
761 // ---------------------------------------------------------------------------
764 // NB: we don't support owner drawn top level items for now, if we do these
765 // functions would have to be changed to use wxMenuItem as well
767 void wxMenuBar::EnableTop(
772 wxCHECK_RET(IsAttached(), wxT("doesn't work with unattached menubars"));
777 uFlag
= MIA_DISABLED
;
779 nId
= SHORT1FROMMR(::WinSendMsg((HWND
)m_hMenu
, MM_ITEMIDFROMPOSITION
, MPFROMSHORT(nPos
), (MPARAM
)0));
780 if (nId
== MIT_ERROR
)
782 wxLogLastError("LogLastError");
785 ::WinSendMsg((HWND
)m_hMenu
, MM_SETITEMATTR
, MPFROM2SHORT(nId
, TRUE
), MPFROM2SHORT(MIA_DISABLED
, uFlag
));
787 } // end of wxMenuBar::EnableTop
789 void wxMenuBar::SetLabelTop(
791 , const wxString
& rLabel
797 wxCHECK_RET(nPos
< GetMenuCount(), wxT("invalid menu index"));
798 m_titles
[nPos
] = rLabel
;
805 nId
= SHORT1FROMMR(::WinSendMsg((HWND
)m_hMenu
, MM_ITEMIDFROMPOSITION
, MPFROMSHORT(nPos
), (MPARAM
)0));
806 if (nId
== MIT_ERROR
)
808 wxLogLastError("LogLastError");
811 if(!::WinSendMsg( (HWND
)m_hMenu
813 ,MPFROM2SHORT(nId
, TRUE
)
817 wxLogLastError("QueryItem");
821 if (::WinSendMsg(GetHmenu(), MM_SETITEMTEXT
, MPFROMSHORT(nId
), (MPARAM
)rLabel
.c_str()));
823 wxLogLastError("ModifyMenu");
826 } // end of wxMenuBar::SetLabelTop
828 wxString
wxMenuBar::GetLabelTop(
832 wxCHECK_MSG( nPos
< GetMenuCount(), wxEmptyString
,
833 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
834 return m_titles
[nPos
];
835 } // end of wxMenuBar::GetLabelTop
837 // ---------------------------------------------------------------------------
838 // wxMenuBar construction
839 // ---------------------------------------------------------------------------
841 wxMenu
* wxMenuBar::Replace(
844 , const wxString
& rTitle
848 wxString Title
= TextToLabel(rTitle
);
849 wxMenu
* pMenuOld
= wxMenuBarBase::Replace( nPos
855 nId
= SHORT1FROMMR(::WinSendMsg((HWND
)m_hMenu
, MM_ITEMIDFROMPOSITION
, MPFROMSHORT(nPos
), (MPARAM
)0));
856 if (nId
== MIT_ERROR
)
858 wxLogLastError("LogLastError");
863 m_titles
[nPos
] = Title
;
866 ::WinSendMsg((HWND
)m_hMenu
, MM_REMOVEITEM
, MPFROM2SHORT(nId
, TRUE
), (MPARAM
)0);
867 ::WinSendMsg((HWND
)m_hMenu
, MM_INSERTITEM
, (MPARAM
)&pMenu
->m_vMenuData
, (MPARAM
)Title
.c_str());
870 if (pMenuOld
->HasAccels() || pMenu
->HasAccels())
873 // Need to rebuild accell table
877 #endif // wxUSE_ACCEL
881 } // end of wxMenuBar::Replace
883 bool wxMenuBar::Insert(
886 , const wxString
& rTitle
889 wxString Title
= TextToLabel(rTitle
);
890 if (!wxMenuBarBase::Insert( nPos
896 m_titles
.Insert( Title
904 ::WinSendMsg((HWND
)m_hMenu
, MM_INSERTITEM
, (MPARAM
)&pMenu
->m_vMenuData
, (MPARAM
)Title
.c_str());
906 if (pMenu
->HasAccels())
908 // need to rebuild accell table
911 #endif // wxUSE_ACCEL
915 } // end of wxMenuBar::Insert
917 bool wxMenuBar::Append(
919 , const wxString
& rTitle
922 WXHMENU hSubmenu
= pMenu
? pMenu
->GetHMenu() : 0;
924 wxCHECK_MSG(hSubmenu
, FALSE
, wxT("can't append invalid menu to menubar"));
926 wxString Title
= TextToLabel(rTitle
);
927 if (!wxMenuBarBase::Append(pMenu
, Title
))
935 pMenu
->m_vMenuData
.iPosition
= MIT_END
;
936 ::WinSendMsg((HWND
)m_hMenu
, MM_INSERTITEM
, (MPARAM
)&pMenu
->m_vMenuData
, (MPARAM
)Title
.c_str());
938 if (pMenu
->HasAccels())
941 // Need to rebuild accell table
945 #endif // wxUSE_ACCEL
949 } // end of wxMenuBar::Append
951 wxMenu
* wxMenuBar::Remove(
955 wxMenu
* pMenu
= wxMenuBarBase::Remove(nPos
);
961 nId
= SHORT1FROMMR(::WinSendMsg((HWND
)GetHmenu(), MM_ITEMIDFROMPOSITION
, MPFROMSHORT(nPos
), (MPARAM
)0));
962 if (nId
== MIT_ERROR
)
964 wxLogLastError("LogLastError");
969 ::WinSendMsg((HWND
)GetHmenu(), MM_REMOVEITEM
, MPFROM2SHORT(nId
, TRUE
), (MPARAM
)0);
973 if (pMenu
->HasAccels())
976 // Need to rebuild accell table
980 #endif // wxUSE_ACCEL
983 m_titles
.Remove(nPos
);
985 } // end of wxMenuBar::Remove
989 void wxMenuBar::RebuildAccelTable()
992 // Merge the accelerators of all menus into one accel table
994 size_t nAccelCount
= 0;
996 size_t nCount
= GetMenuCount();
998 for (i
= 0; i
< nCount
; i
++)
1000 nAccelCount
+= m_menus
[i
]->GetAccelCount();
1005 wxAcceleratorEntry
* pAccelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1008 for (i
= 0; i
< nCount
; i
++)
1010 nAccelCount
+= m_menus
[i
]->CopyAccels(&pAccelEntries
[nAccelCount
]);
1012 m_vAccelTable
= wxAcceleratorTable( nAccelCount
1015 delete [] pAccelEntries
;
1017 } // end of wxMenuBar::RebuildAccelTable
1019 #endif // wxUSE_ACCEL
1021 void wxMenuBar::Attach(
1025 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
1026 m_pMenuBarFrame
= pFrame
;
1029 RebuildAccelTable();
1031 // Ensure the accelerator table is set to the frame (not the client!)
1033 if (!::WinSetAccelTable( vHabmain
1034 ,(HWND
)pFrame
->GetHWND()
1035 ,m_vAccelTable
.GetHACCEL()
1037 wxLogLastError("WinSetAccelTable");
1038 #endif // wxUSE_ACCEL
1039 } // end of wxMenuBar::Attach
1041 void wxMenuBar::Detach()
1043 ::WinDestroyWindow((HWND
)m_hMenu
);
1044 m_hMenu
= (WXHMENU
)NULL
;
1045 m_pMenuBarFrame
= NULL
;
1046 } // end of wxMenuBar::Detach
1048 // ---------------------------------------------------------------------------
1049 // wxMenuBar searching for menu items
1050 // ---------------------------------------------------------------------------
1053 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
1055 int wxMenuBar::FindMenuItem(
1056 const wxString
& rMenuString
1057 , const wxString
& rItemString
1060 wxString sMenuLabel
= wxStripMenuCodes(rMenuString
);
1061 size_t nCount
= GetMenuCount();
1063 for (size_t i
= 0; i
< nCount
; i
++)
1065 wxString sTitle
= wxStripMenuCodes(m_titles
[i
]);
1067 if (rMenuString
== sTitle
)
1068 return m_menus
[i
]->FindItem(rItemString
);
1071 } // end of wxMenuBar::FindMenuItem
1073 wxMenuItem
* wxMenuBar::FindItem(
1075 , wxMenu
** ppItemMenu
1081 wxMenuItem
* pItem
= NULL
;
1082 size_t nCount
= GetMenuCount();
1084 for (size_t i
= 0; !pItem
&& (i
< nCount
); i
++)
1086 pItem
= m_menus
[i
]->FindItem( nId
1091 } // end of wxMenuBar::FindItem