added wxWindow::GetPopupMenuSelectionFromUser() (modified patch 1793823)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@48913 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2007-09-23 23:42:31 +00:00
parent ab171e9502
commit 00a77b7c5d
5 changed files with 125 additions and 15 deletions

View File

@@ -192,6 +192,7 @@ All (GUI):
- Add support for reading alpha data from TIFF images - Add support for reading alpha data from TIFF images
- Added wxSYS_DCLICK_TIME system metric constant (Arne Steinarson) - Added wxSYS_DCLICK_TIME system metric constant (Arne Steinarson)
- Added wxApp::Get/SetAppDisplayName() (Brian A. Vanderburg II) - Added wxApp::Get/SetAppDisplayName() (Brian A. Vanderburg II)
- Added wxWindow::GetPopupMenuSelectionFromUser() (Arne Steinarson)
wxGTK: wxGTK:

View File

@@ -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. 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} \membersection{wxWindow::GetPosition}\label{wxwindowgetposition}
\constfunc{virtual void}{GetPosition}{\param{int* }{x}, \param{int* }{y}} \constfunc{virtual void}{GetPosition}{\param{int* }{x}, \param{int* }{y}}

View File

@@ -988,10 +988,19 @@ public:
virtual void DoUpdateWindowUI(wxUpdateUIEvent& event) ; virtual void DoUpdateWindowUI(wxUpdateUIEvent& event) ;
#if wxUSE_MENUS #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) bool PopupMenu(wxMenu *menu, const wxPoint& pos = wxDefaultPosition)
{ return DoPopupMenu(menu, pos.x, pos.y); } { return DoPopupMenu(menu, pos.x, pos.y); }
bool PopupMenu(wxMenu *menu, int x, int y) bool PopupMenu(wxMenu *menu, int x, int y)
{ return DoPopupMenu(menu, x, 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 #endif // wxUSE_MENUS
// override this method to return true for controls having multiple pages // override this method to return true for controls having multiple pages
@@ -1497,6 +1506,13 @@ private:
// enabled/disabled // enabled/disabled
void NotifyWindowOnEnableChange(bool enabled); 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 // contains the last id generated by NewControlId
static int ms_lastControlId; static int ms_lastControlId;

View File

@@ -256,6 +256,8 @@ enum
Menu_Popup_ToBeChecked, Menu_Popup_ToBeChecked,
Menu_Popup_Submenu, Menu_Popup_Submenu,
Menu_PopupChoice,
Menu_Max Menu_Max
}; };
@@ -1005,27 +1007,50 @@ void MyFrame::ShowContextMenu(const wxPoint& pos)
{ {
wxMenu menu; wxMenu menu;
menu.Append(Menu_Help_About, _T("&About")); if ( wxGetKeyState(WXK_SHIFT) )
menu.Append(Menu_Popup_Submenu, _T("&Submenu"), CreateDummyMenu(NULL)); {
menu.Append(Menu_Popup_ToBeDeleted, _T("To be &deleted")); // when Shift is pressed, demonstrate the use of a simple function
menu.AppendCheckItem(Menu_Popup_ToBeChecked, _T("To be &checked")); // returning the id of the item selected in the popup menu
menu.Append(Menu_Popup_ToBeGreyed, _T("To be &greyed"), menu.SetTitle("Choose one of:");
_T("This menu item should be initially greyed out")); static const char *choices[] = { "Apple", "Banana", "Cherry" };
menu.AppendSeparator(); for ( size_t n = 0; n < WXSIZEOF(choices); n++ )
menu.Append(Menu_File_Quit, _T("E&xit")); menu.Append(Menu_PopupChoice + n, choices[n]);
menu.Delete(Menu_Popup_ToBeDeleted); const int rc = GetPopupMenuSelectionFromUser(menu, pos);
menu.Check(Menu_Popup_ToBeChecked, true); if ( rc == wxID_NONE )
menu.Enable(Menu_Popup_ToBeGreyed, false); {
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"));
PopupMenu(&menu, pos.x, pos.y); menu.Delete(Menu_Popup_ToBeDeleted);
menu.Check(Menu_Popup_ToBeChecked, true);
menu.Enable(Menu_Popup_ToBeGreyed, false);
// test for destroying items in popup menus PopupMenu(&menu, pos);
// test for destroying items in popup menus
#if 0 // doesn't work in wxGTK! #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 #endif // 0
}
} }
void MyFrame::OnTestNormal(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnTestNormal(wxCommandEvent& WXUNUSED(event))

View File

@@ -2233,6 +2233,47 @@ void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) )
UpdateWindowUI(wxUPDATE_UI_RECURSE); 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 // methods for drawing the sizers in a visible way
#ifdef __WXDEBUG__ #ifdef __WXDEBUG__