From: Vadim Zeitlin Date: Sun, 23 Sep 2007 23:42:31 +0000 (+0000) Subject: added wxWindow::GetPopupMenuSelectionFromUser() (modified patch 1793823) X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/00a77b7c5d9e1714198cd5d7e3a905bca2df2534?ds=inline added wxWindow::GetPopupMenuSelectionFromUser() (modified patch 1793823) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@48913 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index 6ae475f79f..70f5e72c74 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -192,6 +192,7 @@ All (GUI): - Add support for reading alpha data from TIFF images - Added wxSYS_DCLICK_TIME system metric constant (Arne Steinarson) - Added wxApp::Get/SetAppDisplayName() (Brian A. Vanderburg II) +- Added wxWindow::GetPopupMenuSelectionFromUser() (Arne Steinarson) wxGTK: diff --git a/docs/latex/wx/window.tex b/docs/latex/wx/window.tex index 81cc3b622b..d19f545197 100644 --- a/docs/latex/wx/window.tex +++ b/docs/latex/wx/window.tex @@ -1161,6 +1161,33 @@ name in the window constructor or via \helpref{wxWindow::SetName}{wxwindowsetnam Returns the parent of the window, or NULL if there is no parent. +\membersection{wxWindow::GetPopupMenuSelectionFromUser}\label{wxwindowgetpopupmenuselectionfromuser} + +\func{int}{GetPopupMenuSelectionFromUser}{\param{wxMenu\&}{ menu}, \param{const wxPoint\&}{ pos}} + +\func{int}{GetPopupMenuSelectionFromUser}{\param{wxMenu\&}{ menu}, \param{int}{ x}, \param{int}{ y}} + +This function shows a popup menu at the given position in this window and +returns the selected id. It can be more convenient than the general purpose +\helpref{PopupMenu}{wxwindowpopupmenu} function for simple menus proposing a +choice in a list of strings to the user. + +\wxheading{Parameters} + +\docparam{menu}{The menu to show} + +\docparam{pos}{The position at which to show the menu in client coordinates} + +\docparam{x}{The horizontal position of the menu} + +\docparam{y}{The vertical position of the menu} + +\wxheading{Return value} + +The selected menu item id or \texttt{wxID\_NONE} if none selected or an error +occurred. + + \membersection{wxWindow::GetPosition}\label{wxwindowgetposition} \constfunc{virtual void}{GetPosition}{\param{int* }{x}, \param{int* }{y}} diff --git a/include/wx/window.h b/include/wx/window.h index 717aabd93e..62396ddcac 100644 --- a/include/wx/window.h +++ b/include/wx/window.h @@ -988,10 +988,19 @@ public: virtual void DoUpdateWindowUI(wxUpdateUIEvent& event) ; #if wxUSE_MENUS + // show popup menu at the given position, generate events for the items + // selected in it bool PopupMenu(wxMenu *menu, const wxPoint& pos = wxDefaultPosition) { return DoPopupMenu(menu, pos.x, pos.y); } bool PopupMenu(wxMenu *menu, int x, int y) { return DoPopupMenu(menu, x, y); } + + // simply return the id of the selected item or wxID_NONE without + // generating any events + int GetPopupMenuSelectionFromUser(wxMenu& menu, const wxPoint& pos) + { return DoGetPopupMenuSelectionFromUser(menu, pos.x, pos.y); } + int GetPopupMenuSelectionFromUser(wxMenu& menu, int x, int y) + { return DoGetPopupMenuSelectionFromUser(menu, x, y); } #endif // wxUSE_MENUS // override this method to return true for controls having multiple pages @@ -1497,6 +1506,13 @@ private: // enabled/disabled void NotifyWindowOnEnableChange(bool enabled); +#if wxUSE_MENUS + // temporary event handler used by GetPopupMenuSelectionFromUser() + void InternalOnPopupMenu(wxCommandEvent& event); + + // implementation of the public GetPopupMenuSelectionFromUser() method + int DoGetPopupMenuSelectionFromUser(wxMenu& menu, int x, int y); +#endif // wxUSE_MENUS // contains the last id generated by NewControlId static int ms_lastControlId; diff --git a/samples/menu/menu.cpp b/samples/menu/menu.cpp index b3d2bf27be..10e6268e48 100644 --- a/samples/menu/menu.cpp +++ b/samples/menu/menu.cpp @@ -256,6 +256,8 @@ enum Menu_Popup_ToBeChecked, Menu_Popup_Submenu, + Menu_PopupChoice, + Menu_Max }; @@ -1005,27 +1007,50 @@ void MyFrame::ShowContextMenu(const wxPoint& pos) { wxMenu menu; - menu.Append(Menu_Help_About, _T("&About")); - menu.Append(Menu_Popup_Submenu, _T("&Submenu"), CreateDummyMenu(NULL)); - menu.Append(Menu_Popup_ToBeDeleted, _T("To be &deleted")); - menu.AppendCheckItem(Menu_Popup_ToBeChecked, _T("To be &checked")); - menu.Append(Menu_Popup_ToBeGreyed, _T("To be &greyed"), - _T("This menu item should be initially greyed out")); - menu.AppendSeparator(); - menu.Append(Menu_File_Quit, _T("E&xit")); - - menu.Delete(Menu_Popup_ToBeDeleted); - menu.Check(Menu_Popup_ToBeChecked, true); - menu.Enable(Menu_Popup_ToBeGreyed, false); - - PopupMenu(&menu, pos.x, pos.y); - - // test for destroying items in popup menus + if ( wxGetKeyState(WXK_SHIFT) ) + { + // when Shift is pressed, demonstrate the use of a simple function + // returning the id of the item selected in the popup menu + menu.SetTitle("Choose one of:"); + static const char *choices[] = { "Apple", "Banana", "Cherry" }; + for ( size_t n = 0; n < WXSIZEOF(choices); n++ ) + menu.Append(Menu_PopupChoice + n, choices[n]); + + const int rc = GetPopupMenuSelectionFromUser(menu, pos); + if ( rc == wxID_NONE ) + { + wxLogMessage("No selection"); + } + else + { + wxLogMessage("You have selected \"%s\"", + choices[rc - Menu_PopupChoice]); + } + } + else // normal case, shift not pressed + { + menu.Append(Menu_Help_About, _T("&About")); + menu.Append(Menu_Popup_Submenu, _T("&Submenu"), CreateDummyMenu(NULL)); + menu.Append(Menu_Popup_ToBeDeleted, _T("To be &deleted")); + menu.AppendCheckItem(Menu_Popup_ToBeChecked, _T("To be &checked")); + menu.Append(Menu_Popup_ToBeGreyed, _T("To be &greyed"), + _T("This menu item should be initially greyed out")); + menu.AppendSeparator(); + menu.Append(Menu_File_Quit, _T("E&xit")); + + menu.Delete(Menu_Popup_ToBeDeleted); + menu.Check(Menu_Popup_ToBeChecked, true); + menu.Enable(Menu_Popup_ToBeGreyed, false); + + PopupMenu(&menu, pos); + + // test for destroying items in popup menus #if 0 // doesn't work in wxGTK! - menu.Destroy(Menu_Popup_Submenu); + menu.Destroy(Menu_Popup_Submenu); - PopupMenu( &menu, event.GetX(), event.GetY() ); + PopupMenu( &menu, event.GetX(), event.GetY() ); #endif // 0 + } } void MyFrame::OnTestNormal(wxCommandEvent& WXUNUSED(event)) diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index 43d9b39a76..200adf03af 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -2233,6 +2233,47 @@ void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) ) UpdateWindowUI(wxUPDATE_UI_RECURSE); } +// ---------------------------------------------------------------------------- +// menu-related functions +// ---------------------------------------------------------------------------- + +#if wxUSE_MENUS + +// this is used to pass the id of the selected item from the menu event handler +// to the main function itself +// +// it's ok to use a global here as there can be at most one popup menu shown at +// any time +static int gs_popupMenuSelection = wxID_NONE; + +void wxWindowBase::InternalOnPopupMenu(wxCommandEvent& event) +{ + // store the id in a global variable where we'll retrieve it from later + gs_popupMenuSelection = event.GetId(); +} + +int +wxWindowBase::DoGetPopupMenuSelectionFromUser(wxMenu& menu, int x, int y) +{ + gs_popupMenuSelection = wxID_NONE; + + Connect(wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu), + NULL, + this); + + PopupMenu(&menu, x, y); + + Disconnect(wxEVT_COMMAND_MENU_SELECTED, + wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu), + NULL, + this); + + return gs_popupMenuSelection; +} + +#endif // wxUSE_MENUS + // methods for drawing the sizers in a visible way #ifdef __WXDEBUG__