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 vItem
.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 wxMenu
* pSubmenu
= pItem
->GetSubMenu();
309 if (pSubmenu
!= NULL
)
311 wxASSERT_MSG(pSubmenu
->GetHMenu(), wxT("invalid submenu"));
312 pSubmenu
->SetParent(this);
314 vItem
.iPosition
= 0; // submenus have a 0 position
315 vItem
.id
= (USHORT
)pSubmenu
->GetHMenu();
316 vItem
.afStyle
|= MIS_SUBMENU
| MIS_TEXT
;
320 vItem
.id
= pItem
->GetId();
325 #if wxUSE_OWNER_DRAWN
326 if (pItem
->IsOwnerDrawn())
329 // Want to get {Measure|Draw}Item messages?
330 // item draws itself, pass pointer to it in data parameter
331 // Will eventually need to set the image handle somewhere into vItem.hItem
333 rItem
.afStyle
|= MIS_OWNERDRAW
;
334 pData
= (BYTE
*)pItem
;
335 // vItem.hItem = ????
341 // Menu is just a normal string (passed in data parameter)
343 vItem
.afStyle
|= MIS_TEXT
;
344 pData
= (char*)pItem
->GetText().c_str();
347 if (nPos
== (size_t)-1)
349 rItem
.iPosition
= MIT_END
;
353 rItem
.iPosition
= nPos
;
358 rc
= (APIRET
)::WinSendMsg( GetHmenu()
363 if (rc
== MIT_MEMERROR
|| rc
== MIT_ERROR
)
365 vError
= ::WinGetLastError(vHabmain
);
366 sError
= wxPMErrorToStr(vError
);
367 wxLogError("Error inserting or appending a menuitem. Error: %s\n", sError
);
368 wxLogLastError("Insert or AppendMenu");
374 // If we're already attached to the menubar, we must update it
378 m_menuBar
->Refresh();
383 } // end of wxMenu::DoInsertOrAppend
385 bool wxMenu::DoAppend(
389 return wxMenuBase::DoAppend(pItem
) && DoInsertOrAppend(pItem
);
392 bool wxMenu::DoInsert(
397 return ( wxMenuBase::DoInsert( nPos
399 DoInsertOrAppend( pItem
402 } // end of wxMenu::DoInsert
404 wxMenuItem
* wxMenu::DoRemove(
409 // We need to find the items position in the child list
412 wxMenuItemList::Node
* pNode
= GetMenuItems().GetFirst();
414 for (nPos
= 0; pNode
; nPos
++)
416 if (pNode
->GetData() == pItem
)
418 pNode
= pNode
->GetNext();
422 // DoRemove() (unlike Remove) can only be called for existing item!
424 wxCHECK_MSG(pNode
, NULL
, wxT("bug in wxMenu::Remove logic"));
428 // Remove the corresponding accel from the accel table
430 int n
= FindAccel(pItem
->GetId());
432 if (n
!= wxNOT_FOUND
)
438 #endif // wxUSE_ACCEL
440 // Remove the item from the menu
442 ::WinSendMsg( GetHmenu()
444 ,MPFROM2SHORT(pItem
->GetId(), TRUE
)
450 // Otherwise, the chane won't be visible
452 m_menuBar
->Refresh();
456 // And from internal data structures
458 return wxMenuBase::DoRemove(pItem
);
459 } // end of wxMenu::DoRemove
461 // ---------------------------------------------------------------------------
462 // accelerator helpers
463 // ---------------------------------------------------------------------------
468 // Create the wxAcceleratorEntries for our accels and put them into provided
469 // array - return the number of accels we have
471 size_t wxMenu::CopyAccels(
472 wxAcceleratorEntry
* pAccels
475 size_t nCount
= GetAccelCount();
477 for (size_t n
= 0; n
< nCount
; n
++)
479 *pAccels
++ = *m_vAccels
[n
];
482 } // end of wxMenu::CopyAccels
484 #endif // wxUSE_ACCEL
486 // ---------------------------------------------------------------------------
488 // ---------------------------------------------------------------------------
490 void wxMenu::SetTitle(
491 const wxString
& rLabel
494 bool bHasNoTitle
= m_title
.IsEmpty();
495 HWND hMenu
= GetHmenu();
500 if (!rLabel
.IsEmpty())
502 if (!::WinSetWindowText(hMenu
, rLabel
.c_str()))
504 wxLogLastError("SetMenuTitle");
510 if (rLabel
.IsEmpty() )
512 ::WinSendMsg( GetHmenu()
514 ,MPFROM2SHORT(hMenu
, TRUE
)
523 if (!::WinSetWindowText(hMenu
, rLabel
.c_str()))
525 wxLogLastError("SetMenuTitle");
529 } // end of wxMenu::SetTitle
531 // ---------------------------------------------------------------------------
533 // ---------------------------------------------------------------------------
535 bool wxMenu::OS2Command(
536 WXUINT
WXUNUSED(uParam
)
541 // Ignore commands from the menu title
544 if (vId
!= (WXWORD
)idMenuTitle
)
546 wxCommandEvent
vEvent(wxEVT_COMMAND_MENU_SELECTED
);
548 vEvent
.SetEventObject(this);
551 ProcessCommand(vEvent
);
554 } // end of wxMenu::OS2Command
556 bool wxMenu::ProcessCommand(
557 wxCommandEvent
& rEvent
560 bool bProcessed
= FALSE
;
562 #if wxUSE_MENU_CALLBACK
568 (void)(*(m_callback
))(*this, rEvent
);
571 #endif // wxUSE_MENU_CALLBACK
574 // Try the menu's event handler
576 if (!bProcessed
&& GetEventHandler())
578 bProcessed
= GetEventHandler()->ProcessEvent(rEvent
);
582 // Try the window the menu was popped up from (and up through the
584 wxWindow
* pWin
= GetInvokingWindow();
586 if (!bProcessed
&& pWin
)
587 bProcessed
= pWin
->GetEventHandler()->ProcessEvent(rEvent
);
589 } // end of wxMenu::ProcessCommand
591 // ---------------------------------------------------------------------------
593 // ---------------------------------------------------------------------------
600 // Menu can be in at most one menubar because otherwise they would both
601 // delete the menu pointer
603 wxASSERT_MSG(!m_menuBar
, wxT("menu belongs to 2 menubars, expect a crash"));
604 m_menuBar
= pMenubar
;
607 void wxMenu::Detach()
609 wxASSERT_MSG( m_menuBar
, wxT("can't detach menu if it's not attached") );
611 } // end of wxMenu::Detach
613 wxWindow
* wxMenu::GetWindow() const
615 if (m_invokingWindow
!= NULL
)
616 return m_invokingWindow
;
617 else if ( m_menuBar
!= NULL
)
618 return m_menuBar
->GetFrame();
621 } // end of wxMenu::GetWindow
623 // ---------------------------------------------------------------------------
625 // ---------------------------------------------------------------------------
627 void wxMenuBar::Init()
629 m_eventHandler
= this;
630 m_pMenuBarFrame
= NULL
;
632 } // end of wxMenuBar::Init
634 wxMenuBar::wxMenuBar()
637 } // end of wxMenuBar::wxMenuBar
639 wxMenuBar::wxMenuBar(
640 long WXUNUSED(lStyle
)
644 } // end of wxMenuBar::wxMenuBar
646 wxMenuBar::wxMenuBar(
649 , const wxString sTitles
[]
654 m_titles
.Alloc(nCount
);
655 for ( int i
= 0; i
< nCount
; i
++ )
657 m_menus
.Append(vMenus
[i
]);
658 m_titles
.Add(sTitles
[i
]);
659 vMenus
[i
]->Attach(this);
661 } // end of wxMenuBar::wxMenuBar
663 wxMenuBar::~wxMenuBar()
665 } // end of wxMenuBar::~wxMenuBar
667 // ---------------------------------------------------------------------------
669 // ---------------------------------------------------------------------------
671 void wxMenuBar::Refresh()
673 wxCHECK_RET( IsAttached(), wxT("can't refresh unatteched menubar") );
675 WinSendMsg(GetWinHwnd(m_pMenuBarFrame
), WM_UPDATEFRAME
, (MPARAM
)FCF_MENU
, (MPARAM
)0);
676 } // end of wxMenuBar::Refresh
678 WXHMENU
wxMenuBar::Create()
686 wxCHECK_MSG(!m_hMenu
, TRUE
, wxT("menubar already created"));
689 // Menubars should be associated with a frame otherwise they are popups
691 if (m_pMenuBarFrame
!= NULL
)
692 hFrame
= GetWinHwnd(m_pMenuBarFrame
);
694 hFrame
= HWND_DESKTOP
;
696 // Create an empty menu and then fill it with insertions
698 if ((m_hMenu
= ::WinCreateWindow( hFrame
701 ,MS_ACTIONBAR
| WS_SYNCPAINT
| WS_VISIBLE
713 wxLogLastError("WinLoadMenu");
717 size_t nCount
= GetMenuCount();
719 for (size_t i
= 0; i
< nCount
; i
++)
727 // Set the parent and owner of the submenues to be the menubar, not the desktop
729 hSubMenu
= m_menus
[i
]->m_vMenuData
.hwndSubMenu
;
730 if (!::WinSetParent(m_menus
[i
]->m_vMenuData
.hwndSubMenu
, m_hMenu
, FALSE
))
732 vError
= ::WinGetLastError(vHabmain
);
733 sError
= wxPMErrorToStr(vError
);
734 wxLogError("Error setting parent for submenu. Error: %s\n", sError
);
738 if (!::WinSetOwner(m_menus
[i
]->m_vMenuData
.hwndSubMenu
, m_hMenu
))
740 vError
= ::WinGetLastError(vHabmain
);
741 sError
= wxPMErrorToStr(vError
);
742 wxLogError("Error setting parent for submenu. Error: %s\n", sError
);
746 m_menus
[i
]->m_vMenuData
.iPosition
= i
;
748 rc
= (APIRET
)::WinSendMsg(m_hMenu
, MM_INSERTITEM
, (MPARAM
)&m_menus
[i
]->m_vMenuData
, (MPARAM
)m_titles
[i
].c_str());
749 if (rc
== MIT_MEMERROR
|| rc
== MIT_ERROR
)
751 vError
= ::WinGetLastError(vHabmain
);
752 sError
= wxPMErrorToStr(vError
);
753 wxLogError("Error inserting or appending a menuitem. Error: %s\n", sError
);
759 } // end of wxMenuBar::Create
761 // ---------------------------------------------------------------------------
762 // wxMenuBar functions to work with the top level submenus
763 // ---------------------------------------------------------------------------
766 // NB: we don't support owner drawn top level items for now, if we do these
767 // functions would have to be changed to use wxMenuItem as well
769 void wxMenuBar::EnableTop(
774 wxCHECK_RET(IsAttached(), wxT("doesn't work with unattached menubars"));
779 uFlag
= MIA_DISABLED
;
781 nId
= SHORT1FROMMR(::WinSendMsg((HWND
)m_hMenu
, MM_ITEMIDFROMPOSITION
, MPFROMSHORT(nPos
), (MPARAM
)0));
782 if (nId
== MIT_ERROR
)
784 wxLogLastError("LogLastError");
787 ::WinSendMsg((HWND
)m_hMenu
, MM_SETITEMATTR
, MPFROM2SHORT(nId
, TRUE
), MPFROM2SHORT(MIA_DISABLED
, uFlag
));
789 } // end of wxMenuBar::EnableTop
791 void wxMenuBar::SetLabelTop(
793 , const wxString
& rLabel
799 wxCHECK_RET(nPos
< GetMenuCount(), wxT("invalid menu index"));
800 m_titles
[nPos
] = rLabel
;
807 nId
= SHORT1FROMMR(::WinSendMsg((HWND
)m_hMenu
, MM_ITEMIDFROMPOSITION
, MPFROMSHORT(nPos
), (MPARAM
)0));
808 if (nId
== MIT_ERROR
)
810 wxLogLastError("LogLastError");
813 if(!::WinSendMsg( (HWND
)m_hMenu
815 ,MPFROM2SHORT(nId
, TRUE
)
819 wxLogLastError("QueryItem");
823 if (::WinSendMsg(GetHmenu(), MM_SETITEMTEXT
, MPFROMSHORT(nId
), (MPARAM
)rLabel
.c_str()));
825 wxLogLastError("ModifyMenu");
828 } // end of wxMenuBar::SetLabelTop
830 wxString
wxMenuBar::GetLabelTop(
834 wxCHECK_MSG( nPos
< GetMenuCount(), wxEmptyString
,
835 wxT("invalid menu index in wxMenuBar::GetLabelTop") );
836 return m_titles
[nPos
];
837 } // end of wxMenuBar::GetLabelTop
839 // ---------------------------------------------------------------------------
840 // wxMenuBar construction
841 // ---------------------------------------------------------------------------
843 wxMenu
* wxMenuBar::Replace(
846 , const wxString
& rTitle
850 wxString Title
= TextToLabel(rTitle
);
851 wxMenu
* pMenuOld
= wxMenuBarBase::Replace( nPos
857 nId
= SHORT1FROMMR(::WinSendMsg((HWND
)m_hMenu
, MM_ITEMIDFROMPOSITION
, MPFROMSHORT(nPos
), (MPARAM
)0));
858 if (nId
== MIT_ERROR
)
860 wxLogLastError("LogLastError");
865 m_titles
[nPos
] = Title
;
868 ::WinSendMsg((HWND
)m_hMenu
, MM_REMOVEITEM
, MPFROM2SHORT(nId
, TRUE
), (MPARAM
)0);
869 ::WinSendMsg((HWND
)m_hMenu
, MM_INSERTITEM
, (MPARAM
)&pMenu
->m_vMenuData
, (MPARAM
)Title
.c_str());
872 if (pMenuOld
->HasAccels() || pMenu
->HasAccels())
875 // Need to rebuild accell table
879 #endif // wxUSE_ACCEL
883 } // end of wxMenuBar::Replace
885 bool wxMenuBar::Insert(
888 , const wxString
& rTitle
891 wxString Title
= TextToLabel(rTitle
);
892 if (!wxMenuBarBase::Insert( nPos
898 m_titles
.Insert( Title
906 ::WinSendMsg((HWND
)m_hMenu
, MM_INSERTITEM
, (MPARAM
)&pMenu
->m_vMenuData
, (MPARAM
)Title
.c_str());
908 if (pMenu
->HasAccels())
910 // need to rebuild accell table
913 #endif // wxUSE_ACCEL
917 } // end of wxMenuBar::Insert
919 bool wxMenuBar::Append(
921 , const wxString
& rTitle
924 WXHMENU hSubmenu
= pMenu
? pMenu
->GetHMenu() : 0;
926 wxCHECK_MSG(hSubmenu
, FALSE
, wxT("can't append invalid menu to menubar"));
928 wxString Title
= TextToLabel(rTitle
);
929 if (!wxMenuBarBase::Append(pMenu
, Title
))
937 pMenu
->m_vMenuData
.iPosition
= MIT_END
;
938 ::WinSendMsg((HWND
)m_hMenu
, MM_INSERTITEM
, (MPARAM
)&pMenu
->m_vMenuData
, (MPARAM
)Title
.c_str());
940 if (pMenu
->HasAccels())
943 // Need to rebuild accell table
947 #endif // wxUSE_ACCEL
951 } // end of wxMenuBar::Append
953 wxMenu
* wxMenuBar::Remove(
957 wxMenu
* pMenu
= wxMenuBarBase::Remove(nPos
);
963 nId
= SHORT1FROMMR(::WinSendMsg((HWND
)GetHmenu(), MM_ITEMIDFROMPOSITION
, MPFROMSHORT(nPos
), (MPARAM
)0));
964 if (nId
== MIT_ERROR
)
966 wxLogLastError("LogLastError");
971 ::WinSendMsg((HWND
)GetHmenu(), MM_REMOVEITEM
, MPFROM2SHORT(nId
, TRUE
), (MPARAM
)0);
975 if (pMenu
->HasAccels())
978 // Need to rebuild accell table
982 #endif // wxUSE_ACCEL
985 m_titles
.Remove(nPos
);
987 } // end of wxMenuBar::Remove
991 void wxMenuBar::RebuildAccelTable()
994 // Merge the accelerators of all menus into one accel table
996 size_t nAccelCount
= 0;
998 size_t nCount
= GetMenuCount();
1000 for (i
= 0; i
< nCount
; i
++)
1002 nAccelCount
+= m_menus
[i
]->GetAccelCount();
1007 wxAcceleratorEntry
* pAccelEntries
= new wxAcceleratorEntry
[nAccelCount
];
1010 for (i
= 0; i
< nCount
; i
++)
1012 nAccelCount
+= m_menus
[i
]->CopyAccels(&pAccelEntries
[nAccelCount
]);
1014 m_vAccelTable
= wxAcceleratorTable( nAccelCount
1017 delete [] pAccelEntries
;
1019 } // end of wxMenuBar::RebuildAccelTable
1021 #endif // wxUSE_ACCEL
1023 void wxMenuBar::Attach(
1027 wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") );
1028 m_pMenuBarFrame
= pFrame
;
1031 RebuildAccelTable();
1033 // Ensure the accelerator table is set to the frame (not the client!)
1035 if (!::WinSetAccelTable( vHabmain
1036 ,(HWND
)pFrame
->GetFrame()
1037 ,m_vAccelTable
.GetHACCEL()
1039 wxLogLastError("WinSetAccelTable");
1040 #endif // wxUSE_ACCEL
1041 } // end of wxMenuBar::Attach
1043 void wxMenuBar::Detach()
1045 ::WinDestroyWindow((HWND
)m_hMenu
);
1046 m_hMenu
= (WXHMENU
)NULL
;
1047 m_pMenuBarFrame
= NULL
;
1048 } // end of wxMenuBar::Detach
1050 // ---------------------------------------------------------------------------
1051 // wxMenuBar searching for menu items
1052 // ---------------------------------------------------------------------------
1055 // Find the itemString in menuString, and return the item id or wxNOT_FOUND
1057 int wxMenuBar::FindMenuItem(
1058 const wxString
& rMenuString
1059 , const wxString
& rItemString
1062 wxString sMenuLabel
= wxStripMenuCodes(rMenuString
);
1063 size_t nCount
= GetMenuCount();
1065 for (size_t i
= 0; i
< nCount
; i
++)
1067 wxString sTitle
= wxStripMenuCodes(m_titles
[i
]);
1069 if (rMenuString
== sTitle
)
1070 return m_menus
[i
]->FindItem(rItemString
);
1073 } // end of wxMenuBar::FindMenuItem
1075 wxMenuItem
* wxMenuBar::FindItem(
1077 , wxMenu
** ppItemMenu
1083 wxMenuItem
* pItem
= NULL
;
1084 size_t nCount
= GetMenuCount();
1086 for (size_t i
= 0; !pItem
&& (i
< nCount
); i
++)
1088 pItem
= m_menus
[i
]->FindItem( nId
1093 } // end of wxMenuBar::FindItem