From a29e7318e652d65c32fca91656291ea47b1474e0 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 15 Jul 2014 13:27:01 +0000 Subject: [PATCH] Remove unnecessary ConvertWXArrayToC() helper choice dialog code. Just use the appropriate wxSingleChoiceDialog ctor directly. Also some renamings/reformattings, but no other real changes. Closes #16383. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76930 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/generic/choicdgg.cpp | 152 ++++++++++++++------------------------- 1 file changed, 54 insertions(+), 98 deletions(-) diff --git a/src/generic/choicdgg.cpp b/src/generic/choicdgg.cpp index 6acb6833f9..9b9cb923e0 100644 --- a/src/generic/choicdgg.cpp +++ b/src/generic/choicdgg.cpp @@ -52,30 +52,10 @@ // private functions // ---------------------------------------------------------------------------- -// convert wxArrayString into a wxString[] which must be delete[]d by caller -static int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices); - // ============================================================================ // implementation // ============================================================================ -// ---------------------------------------------------------------------------- -// helpers -// ---------------------------------------------------------------------------- - -int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices) -{ - int n = aChoices.GetCount(); - *choices = new wxString[n]; - - for ( int i = 0; i < n; i++ ) - { - (*choices)[i] = aChoices[i]; - } - - return n; -} - // ---------------------------------------------------------------------------- // wrapper functions // ---------------------------------------------------------------------------- @@ -92,31 +72,22 @@ wxString wxGetSingleChoice( const wxString& message, wxSingleChoiceDialog dialog(parent, message, caption, n, choices); dialog.SetSelection(initialSelection); - - wxString choice; - if ( dialog.ShowModal() == wxID_OK ) - choice = dialog.GetStringSelection(); - - return choice; + return dialog.ShowModal() == wxID_OK ? dialog.GetStringSelection() : wxString(); } wxString wxGetSingleChoice( const wxString& message, const wxString& caption, - const wxArrayString& aChoices, + const wxArrayString& choices, wxWindow *parent, - int x, int y, - bool centre, - int width, int height, + int WXUNUSED(x), int WXUNUSED(y), + bool WXUNUSED(centre), + int WXUNUSED(width), int WXUNUSED(height), int initialSelection) { - wxString *choices; - int n = ConvertWXArrayToC(aChoices, &choices); - wxString res = wxGetSingleChoice(message, caption, n, choices, parent, - x, y, centre, width, height, - initialSelection); - delete [] choices; + wxSingleChoiceDialog dialog(parent, message, caption, choices); - return res; + dialog.SetSelection(initialSelection); + return dialog.ShowModal() == wxID_OK ? dialog.GetStringSelection() : wxString(); } wxString wxGetSingleChoice( const wxString& message, @@ -155,33 +126,22 @@ int wxGetSingleChoiceIndex( const wxString& message, wxSingleChoiceDialog dialog(parent, message, caption, n, choices); dialog.SetSelection(initialSelection); - - int choice; - if ( dialog.ShowModal() == wxID_OK ) - choice = dialog.GetSelection(); - else - choice = -1; - - return choice; + return dialog.ShowModal() == wxID_OK ? dialog.GetSelection() : -1; } int wxGetSingleChoiceIndex( const wxString& message, const wxString& caption, - const wxArrayString& aChoices, + const wxArrayString& choices, wxWindow *parent, - int x, int y, - bool centre, - int width, int height, + int WXUNUSED(x), int WXUNUSED(y), + bool WXUNUSED(centre), + int WXUNUSED(width), int WXUNUSED(height), int initialSelection) { - wxString *choices; - int n = ConvertWXArrayToC(aChoices, &choices); - int res = wxGetSingleChoiceIndex(message, caption, n, choices, parent, - x, y, centre, width, height, - initialSelection); - delete [] choices; + wxSingleChoiceDialog dialog(parent, message, caption, choices); - return res; + dialog.SetSelection(initialSelection); + return dialog.ShowModal() == wxID_OK ? dialog.GetSelection() : -1; } int wxGetSingleChoiceIndex( const wxString& message, @@ -224,35 +184,23 @@ void *wxGetSingleChoiceData( const wxString& message, client_data); dialog.SetSelection(initialSelection); - - void *data; - if ( dialog.ShowModal() == wxID_OK ) - data = dialog.GetSelectionData(); - else - data = NULL; - - return data; + return dialog.ShowModal() == wxID_OK ? dialog.GetSelectionData() : NULL; } void *wxGetSingleChoiceData( const wxString& message, const wxString& caption, - const wxArrayString& aChoices, + const wxArrayString& choices, void **client_data, wxWindow *parent, - int x, int y, - bool centre, - int width, int height, + int WXUNUSED(x), int WXUNUSED(y), + bool WXUNUSED(centre), + int WXUNUSED(width), int WXUNUSED(height), int initialSelection) { - wxString *choices; - int n = ConvertWXArrayToC(aChoices, &choices); - void *res = wxGetSingleChoiceData(message, caption, n, choices, - client_data, parent, - x, y, centre, width, height, - initialSelection); - delete [] choices; + wxSingleChoiceDialog dialog(parent, message, caption, choices, client_data); - return res; + dialog.SetSelection(initialSelection); + return dialog.ShowModal() == wxID_OK ? dialog.GetSelectionData() : NULL; } void* wxGetSingleChoiceData( const wxString& message, @@ -285,13 +233,13 @@ void* wxGetSingleChoiceData( const wxString& message, int wxGetSelectedChoices(wxArrayInt& selections, - const wxString& message, - const wxString& caption, - int n, const wxString *choices, - wxWindow *parent, - int WXUNUSED(x), int WXUNUSED(y), - bool WXUNUSED(centre), - int WXUNUSED(width), int WXUNUSED(height)) + const wxString& message, + const wxString& caption, + int n, const wxString *choices, + wxWindow *parent, + int WXUNUSED(x), int WXUNUSED(y), + bool WXUNUSED(centre), + int WXUNUSED(width), int WXUNUSED(height)) { wxMultiChoiceDialog dialog(parent, message, caption, n, choices); @@ -308,26 +256,34 @@ int wxGetSelectedChoices(wxArrayInt& selections, } selections = dialog.GetSelections(); - return selections.GetCount(); + return static_cast(selections.GetCount()); } int wxGetSelectedChoices(wxArrayInt& selections, - const wxString& message, - const wxString& caption, - const wxArrayString& aChoices, - wxWindow *parent, - int x, int y, - bool centre, - int width, int height) + const wxString& message, + const wxString& caption, + const wxArrayString& choices, + wxWindow *parent, + int WXUNUSED(x), int WXUNUSED(y), + bool WXUNUSED(centre), + int WXUNUSED(width), int WXUNUSED(height)) { - wxString *choices; - int n = ConvertWXArrayToC(aChoices, &choices); - int res = wxGetSelectedChoices(selections, message, caption, - n, choices, parent, - x, y, centre, width, height); - delete [] choices; + wxMultiChoiceDialog dialog(parent, message, caption, choices); - return res; + // call this even if selections array is empty and this then (correctly) + // deselects the first item which is selected by default + dialog.SetSelections(selections); + + 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 static_cast(selections.GetCount()); } #if WXWIN_COMPATIBILITY_2_8