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
- Added wxSYS_DCLICK_TIME system metric constant (Arne Steinarson)
- Added wxApp::Get/SetAppDisplayName() (Brian A. Vanderburg II)
- Added wxWindow::GetPopupMenuSelectionFromUser() (Arne Steinarson)
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.
\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}}

View File

@@ -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;

View File

@@ -256,6 +256,8 @@ enum
Menu_Popup_ToBeChecked,
Menu_Popup_Submenu,
Menu_PopupChoice,
Menu_Max
};
@@ -1005,6 +1007,28 @@ void MyFrame::ShowContextMenu(const wxPoint& pos)
{
wxMenu menu;
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"));
@@ -1018,7 +1042,7 @@ void MyFrame::ShowContextMenu(const wxPoint& pos)
menu.Check(Menu_Popup_ToBeChecked, true);
menu.Enable(Menu_Popup_ToBeGreyed, false);
PopupMenu(&menu, pos.x, pos.y);
PopupMenu(&menu, pos);
// test for destroying items in popup menus
#if 0 // doesn't work in wxGTK!
@@ -1027,6 +1051,7 @@ void MyFrame::ShowContextMenu(const wxPoint& pos)
PopupMenu( &menu, event.GetX(), event.GetY() );
#endif // 0
}
}
void MyFrame::OnTestNormal(wxCommandEvent& WXUNUSED(event))
{

View File

@@ -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__