]> git.saurik.com Git - wxWidgets.git/commitdiff
added wxWindow::GetPopupMenuSelectionFromUser() (modified patch 1793823)
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 23 Sep 2007 23:42:31 +0000 (23:42 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 23 Sep 2007 23:42:31 +0000 (23:42 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@48913 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

docs/changes.txt
docs/latex/wx/window.tex
include/wx/window.h
samples/menu/menu.cpp
src/common/wincmn.cpp

index 6ae475f79fbe5a6b2fa2b159cfdbf4b499c18f20..70f5e72c74f8b46140375264876b1b5c5c9169f1 100644 (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:
 
index 81cc3b622bfeb6ffc957d67929b507c92ee920e0..d19f545197cbd40dd1ac9aef24a0131fcdf7cd2f 100644 (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}}
index 717aabd93e54c580d05d987264b02e4599c4f59f..62396ddcac870e20a19b7c17475147a22b288668 100644 (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;
index b3d2bf27be703ea2973180681ce17ee6740875e3..10e6268e48448239437f5f9cf990ce75efeac758 100644 (file)
@@ -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))
index 43d9b39a76e51b3da04bae960b8e318a27c3ca9e..200adf03afec71175f4394db66c00d22f70a0901 100644 (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__