From e5cfb314ae1cadac46cc50d37d9f6d15d8260a29 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 7 Mar 2009 15:39:09 +0000 Subject: [PATCH] replace wxGetMultipleChoices() with wxGetSelectedChoices() which allows to distinguish between cancelling the dialog and not selecting any items in it (closes #10057) git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59417 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 4 ++- include/wx/generic/choicdgg.h | 33 +++++++++++++++++-- interface/wx/choicdlg.h | 7 ++-- samples/dialogs/dialogs.cpp | 23 ++++++++----- src/generic/choicdgg.cpp | 62 +++++++++++++++++++++++++++++++---- 5 files changed, 107 insertions(+), 22 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 41c2e66288..d0ef4c1da9 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -274,7 +274,8 @@ Deprecated methods and their replacements were deprecated in favour of wxFileName methods. See docs for more info. - wxEvtHandler::TryValidator/Parent() are deprecated, override the new and documented TryBefore/After() methods if you used to override these ones. - +- wxGetMultipleChoices() is deprecated, use wxGetSelectedChoices() which has + the same signature but returns -1 and not 0 if the dialog was cancelled. Major new features in this release ---------------------------------- @@ -498,6 +499,7 @@ All (GUI): - Render element contents in bold in wxHTML. - Added wxGrid::{Set,Get}{Row,Col}Sizes() methods (Andrey Putrin). - Add support for wxSP_WRAP in the generic version of wxSpinCtrlDouble. +- Added wxGetSelectedChoices() replacing wxGetMultipleChoices() (Kolya Kosenko). wxGTK: diff --git a/include/wx/generic/choicdgg.h b/include/wx/generic/choicdgg.h index 97b9a8949b..505a63b3a5 100644 --- a/include/wx/generic/choicdgg.h +++ b/include/wx/generic/choicdgg.h @@ -294,8 +294,8 @@ WXDLLIMPEXP_CORE void* wxGetSingleChoiceData(const wxString& message, // fill the array with the indices of the chosen items, it will be empty // if no items were selected or Cancel was pressed - return the number of -// selections -WXDLLIMPEXP_CORE size_t wxGetMultipleChoices(wxArrayInt& selections, +// selections or -1 if cancelled +WXDLLIMPEXP_CORE int wxGetSelectedChoices(wxArrayInt& selections, const wxString& message, const wxString& caption, int n, const wxString *choices, @@ -306,7 +306,7 @@ WXDLLIMPEXP_CORE size_t wxGetMultipleChoices(wxArrayInt& selections, int width = wxCHOICE_WIDTH, int height = wxCHOICE_HEIGHT); -WXDLLIMPEXP_CORE size_t wxGetMultipleChoices(wxArrayInt& selections, +WXDLLIMPEXP_CORE int wxGetSelectedChoices(wxArrayInt& selections, const wxString& message, const wxString& caption, const wxArrayString& choices, @@ -317,4 +317,31 @@ WXDLLIMPEXP_CORE size_t wxGetMultipleChoices(wxArrayInt& selections, int width = wxCHOICE_WIDTH, int height = wxCHOICE_HEIGHT); +#if WXWIN_COMPATIBILITY_2_8 +// fill the array with the indices of the chosen items, it will be empty +// if no items were selected or Cancel was pressed - return the number of +// selections +wxDEPRECATED( WXDLLIMPEXP_CORE size_t wxGetMultipleChoices(wxArrayInt& selections, + const wxString& message, + const wxString& caption, + int n, const wxString *choices, + wxWindow *parent = NULL, + int x = wxDefaultCoord, + int y = wxDefaultCoord, + bool centre = true, + int width = wxCHOICE_WIDTH, + int height = wxCHOICE_HEIGHT) ); + +wxDEPRECATED( WXDLLIMPEXP_CORE size_t wxGetMultipleChoices(wxArrayInt& selections, + const wxString& message, + const wxString& caption, + const wxArrayString& choices, + wxWindow *parent = NULL, + int x = wxDefaultCoord, + int y = wxDefaultCoord, + bool centre = true, + int width = wxCHOICE_WIDTH, + int height = wxCHOICE_HEIGHT)); +#endif // WXWIN_COMPATIBILITY_2_8 + #endif // _WX_GENERIC_CHOICDGG_H_ diff --git a/interface/wx/choicdlg.h b/interface/wx/choicdlg.h index bd2238a33b..118f09048f 100644 --- a/interface/wx/choicdlg.h +++ b/interface/wx/choicdlg.h @@ -394,7 +394,8 @@ wxString wxGetSingleChoiceData(const wxString& message, multiple-selection listbox. The user may choose an arbitrary (including 0) number of items in the listbox whose indices will be returned in @c selections array. The initial contents of this array will be used to - select the items when the dialog is shown. + select the items when the dialog is shown. If the user cancels the dialog, + the function returns -1 and @c selections array is left unchanged. You may pass the list of strings to choose from either using @c choices which is an array of @a n strings for the listbox or by using a single @@ -405,7 +406,7 @@ wxString wxGetSingleChoiceData(const wxString& message, @header{wx/choicdlg.h} */ -size_t wxGetMultipleChoices(wxArrayInt& selections, +int wxGetSelectedChoices(wxArrayInt& selections, const wxString& message, const wxString& caption, const wxArrayString& aChoices, @@ -415,7 +416,7 @@ size_t wxGetMultipleChoices(wxArrayInt& selections, bool centre = true, int width = 150, int height = 200); -size_t wxGetMultipleChoices(wxArrayInt& selections, +int wxGetSelectedChoices(wxArrayInt& selections, const wxString& message, const wxString& caption, int n, diff --git a/samples/dialogs/dialogs.cpp b/samples/dialogs/dialogs.cpp index 59197b7642..ceed79323d 100644 --- a/samples/dialogs/dialogs.cpp +++ b/samples/dialogs/dialogs.cpp @@ -809,25 +809,32 @@ void MyFrame::MultiChoice(wxCommandEvent& WXUNUSED(event) ) }; wxArrayInt selections; - size_t count = wxGetMultipleChoices(selections, + const int count = wxGetSelectedChoices(selections, _T("This is a small sample\n") _T("A multi-choice convenience dialog"), _T("Please select a value"), WXSIZEOF(choices), choices, this); - if ( count ) + if ( count >= 0 ) { wxString msg; - msg.Printf(wxT("You selected %u items:\n"), (unsigned)count); - for ( size_t n = 0; n < count; n++ ) + if ( count == 0 ) + { + msg = wxT("You did not select any items"); + } + else { - msg += wxString::Format(wxT("\t%u: %u (%s)\n"), - (unsigned)n, (unsigned)selections[n], - choices[selections[n]].c_str()); + msg.Printf(wxT("You selected %u items:\n"), (unsigned)count); + for ( int n = 0; n < count; n++ ) + { + msg += wxString::Format(wxT("\t%u: %u (%s)\n"), + (unsigned)n, (unsigned)selections[n], + choices[selections[n]].c_str()); + } } wxLogMessage(msg); } - //else: cancelled or nothing selected + //else: cancelled } #endif // wxUSE_CHOICEDLG diff --git a/src/generic/choicdgg.cpp b/src/generic/choicdgg.cpp index fa3c19415b..fb4eb2677d 100644 --- a/src/generic/choicdgg.cpp +++ b/src/generic/choicdgg.cpp @@ -188,7 +188,7 @@ void *wxGetSingleChoiceData( const wxString& message, return res; } -size_t wxGetMultipleChoices(wxArrayInt& selections, +int wxGetSelectedChoices(wxArrayInt& selections, const wxString& message, const wxString& caption, int n, const wxString *choices, @@ -203,15 +203,19 @@ size_t wxGetMultipleChoices(wxArrayInt& selections, // deselects the first item which is selected by default dialog.SetSelections(selections); - if ( dialog.ShowModal() == wxID_OK ) - selections = dialog.GetSelections(); - else - selections.Empty(); + if ( dialog.ShowModal() != wxID_OK ) + { + // NB: intentionally do not clear the selections array here, the caller + // might want to preserve its original contents if the dialog was + // cancelled + return -1; + } + selections = dialog.GetSelections(); return selections.GetCount(); } -size_t wxGetMultipleChoices(wxArrayInt& selections, +int wxGetSelectedChoices(wxArrayInt& selections, const wxString& message, const wxString& caption, const wxArrayString& aChoices, @@ -222,7 +226,7 @@ size_t wxGetMultipleChoices(wxArrayInt& selections, { wxString *choices; int n = ConvertWXArrayToC(aChoices, &choices); - size_t res = wxGetMultipleChoices(selections, message, caption, + int res = wxGetSelectedChoices(selections, message, caption, n, choices, parent, x, y, centre, width, height); delete [] choices; @@ -230,6 +234,50 @@ size_t wxGetMultipleChoices(wxArrayInt& selections, return res; } +#if WXWIN_COMPATIBILITY_2_8 +size_t wxGetMultipleChoices(wxArrayInt& selections, + const wxString& message, + const wxString& caption, + int n, const wxString *choices, + wxWindow *parent, + int x, int y, + bool centre, + int width, int height) +{ + int rc = wxGetSelectedChoices(selections, message, caption, + n, choices, + parent, x, y, centre, width, height); + if ( rc == -1 ) + { + selections.clear(); + return 0; + } + + return rc; +} + +size_t wxGetMultipleChoices(wxArrayInt& selections, + const wxString& message, + const wxString& caption, + const wxArrayString& aChoices, + wxWindow *parent, + int x, int y, + bool centre, + int width, int height) +{ + int rc = wxGetSelectedChoices(selections, message, caption, + aChoices, + parent, x, y, centre, width, height); + if ( rc == -1 ) + { + selections.clear(); + return 0; + } + + return rc; +} +#endif // WXWIN_COMPATIBILITY_2_8 + // ---------------------------------------------------------------------------- // wxAnyChoiceDialog // ---------------------------------------------------------------------------- -- 2.45.2