]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicdgg.cpp
and another note about wxStringList vs wxArrayString
[wxWidgets.git] / src / generic / choicdgg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
dfad0599 2// Name: choicdgg.cpp
c801d85f
KB
3// Purpose: Choice dialogs
4// Author: Julian Smart
d6c9c1b7 5// Modified by: 03.11.00: VZ to add wxArrayString and multiple sel functions
c801d85f
KB
6// Created: 04/01/98
7// RCS-ID: $Id$
d6c9c1b7 8// Copyright: (c) wxWindows team
d427503c 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
d6c9c1b7
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
c801d85f 16#ifdef __GNUG__
d427503c 17 #pragma implementation "choicdgg.h"
c801d85f
KB
18#endif
19
d6c9c1b7
VZ
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
c801d85f
KB
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
d427503c 28 #pragma hdrstop
c801d85f
KB
29#endif
30
31#ifndef WX_PRECOMP
257bf510
VZ
32 #include <stdio.h>
33 #include "wx/utils.h"
34 #include "wx/dialog.h"
35 #include "wx/button.h"
36 #include "wx/listbox.h"
37 #include "wx/stattext.h"
38 #include "wx/intl.h"
92afa2b1 39 #include "wx/sizer.h"
dcf924a3
RR
40#endif
41
42#if wxUSE_STATLINE
c50f1fb9 43 #include "wx/statline.h"
c801d85f
KB
44#endif
45
46#include "wx/generic/choicdgg.h"
47
d6c9c1b7
VZ
48// ----------------------------------------------------------------------------
49// constants
50// ----------------------------------------------------------------------------
51
257bf510 52#define wxID_LISTBOX 3000
dcf924a3 53
d6c9c1b7
VZ
54#if defined(__WXMSW__) || defined(__WXMAC__)
55#define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
56 wxDIALOG_MODAL | \
57 wxTAB_TRAVERSAL)
58#else
59#define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
60 wxDIALOG_MODAL | \
61 wxRESIZE_BORDER | \
62 wxTAB_TRAVERSAL)
63#endif
64
65// ----------------------------------------------------------------------------
66// private functions
67// ----------------------------------------------------------------------------
68
69// convert wxArrayString into a wxString[] which must be delete[]d by caller
70static int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices);
71
72// ============================================================================
73// implementation
74// ============================================================================
75
76// ----------------------------------------------------------------------------
77// helpers
78// ----------------------------------------------------------------------------
79
80int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices)
81{
82 int n = aChoices.GetCount();
83 *choices = new wxString[n];
84 for ( int i = 0; i < n; i++ )
85 {
86 *choices[i] = aChoices[i];
87 }
88
89 return n;
90}
91
92// ----------------------------------------------------------------------------
93// wrapper functions
94// ----------------------------------------------------------------------------
95
96wxString wxGetSingleChoice( const wxString& message,
97 const wxString& caption,
98 int n, const wxString *choices,
99 wxWindow *parent,
100 int WXUNUSED(x), int WXUNUSED(y),
101 bool WXUNUSED(centre),
257bf510 102 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 103{
d427503c 104 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
3ca6a5f0 105 wxString choice;
d427503c 106 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0
BP
107 choice = dialog.GetStringSelection();
108
109 return choice;
c801d85f
KB
110}
111
d6c9c1b7
VZ
112wxString wxGetSingleChoice( const wxString& message,
113 const wxString& caption,
114 const wxArrayString& aChoices,
115 wxWindow *parent,
116 int x, int y,
117 bool centre,
118 int width, int height)
119{
120 wxString *choices;
121 int n = ConvertWXArrayToC(aChoices, &choices);
122 wxString res = wxGetSingleChoice(message, caption, n, choices, parent,
123 x, y, centre, width, height);
124 delete [] choices;
125
126 return res;
127}
128
129#ifdef WXWIN_COMPATIBILITY_2
c801d85f 130// Overloaded for backward compatibility
d6c9c1b7
VZ
131wxString wxGetSingleChoice( const wxString& message,
132 const wxString& caption,
133 int n, char *choices[],
134 wxWindow *parent,
c50f1fb9 135 int x, int y, bool centre,
257bf510 136 int width, int height )
c801d85f 137{
d427503c
VZ
138 wxString *strings = new wxString[n];
139 int i;
140 for ( i = 0; i < n; i++)
141 {
142 strings[i] = choices[i];
143 }
144 wxString ans(wxGetSingleChoice(message, caption, n, (const wxString *)strings, parent,
145 x, y, centre, width, height));
146 delete[] strings;
147 return ans;
c801d85f 148}
d6c9c1b7
VZ
149#endif // WXWIN_COMPATIBILITY_2
150
151int wxGetSingleChoiceIndex( const wxString& message,
152 const wxString& caption,
153 int n, const wxString *choices,
154 wxWindow *parent,
155 int WXUNUSED(x), int WXUNUSED(y),
156 bool WXUNUSED(centre),
157 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 158{
d427503c 159 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
3ca6a5f0 160 int choice;
d427503c 161 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0 162 choice = dialog.GetSelection();
d427503c 163 else
3ca6a5f0
BP
164 choice = -1;
165
166 return choice;
c801d85f
KB
167}
168
d6c9c1b7 169#ifdef WXWIN_COMPATIBILITY_2
c801d85f 170// Overloaded for backward compatibility
d6c9c1b7
VZ
171int wxGetSingleChoiceIndex( const wxString& message,
172 const wxString& caption,
173 int n, wxChar *choices[],
174 wxWindow *parent,
175 int x, int y, bool centre,
176 int width, int height )
c801d85f 177{
d427503c 178 wxString *strings = new wxString[n];
dcf924a3 179 for ( int i = 0; i < n; i++)
d427503c 180 strings[i] = choices[i];
d427503c
VZ
181 int ans = wxGetSingleChoiceIndex(message, caption, n, (const wxString *)strings, parent,
182 x, y, centre, width, height);
183 delete[] strings;
184 return ans;
c801d85f 185}
d6c9c1b7
VZ
186#endif // WXWIN_COMPATIBILITY_2
187
188void *wxGetSingleChoiceData( const wxString& message,
189 const wxString& caption,
190 int n, const wxString *choices,
191 void **client_data,
192 wxWindow *parent,
193 int WXUNUSED(x), int WXUNUSED(y),
194 bool WXUNUSED(centre),
195 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 196{
b91b2200 197 wxSingleChoiceDialog dialog(parent, message, caption, n, choices, (char **)client_data);
3ca6a5f0 198 void *data;
d427503c 199 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0 200 data = dialog.GetSelectionClientData();
d427503c 201 else
3ca6a5f0
BP
202 data = NULL;
203
204 return data;
c801d85f
KB
205}
206
d6c9c1b7 207#ifdef WXWIN_COMPATIBILITY_2
c801d85f 208// Overloaded for backward compatibility
d6c9c1b7
VZ
209void *wxGetSingleChoiceData( const wxString& message,
210 const wxString& caption,
211 int n, wxChar *choices[],
212 void **client_data,
213 wxWindow *parent,
214 int x, int y, bool centre, int width, int height )
c801d85f 215{
d427503c
VZ
216 wxString *strings = new wxString[n];
217 int i;
218 for ( i = 0; i < n; i++)
219 {
220 strings[i] = choices[i];
221 }
d6c9c1b7
VZ
222 void *data = wxGetSingleChoiceData(message, caption,
223 n, (const wxString *)strings,
224 client_data, parent,
225 x, y, centre, width, height);
d427503c
VZ
226 delete[] strings;
227 return data;
c801d85f 228}
d6c9c1b7
VZ
229#endif // WXWIN_COMPATIBILITY_2
230
231size_t wxGetMultipleChoices(wxArrayInt& selections,
232 const wxString& message,
233 const wxString& caption,
234 int n, const wxString *choices,
235 wxWindow *parent,
236 int WXUNUSED(x), int WXUNUSED(y),
237 bool WXUNUSED(centre),
238 int WXUNUSED(width), int WXUNUSED(height))
239{
240 wxMultiChoiceDialog dialog(parent, message, caption, n, choices);
241 if ( dialog.ShowModal() == wxID_OK )
242 selections = dialog.GetSelections();
243 else
244 selections.Empty();
c801d85f 245
d6c9c1b7
VZ
246 return selections.GetCount();
247}
c801d85f 248
d6c9c1b7
VZ
249size_t wxGetMultipleChoices(wxArrayInt selections,
250 const wxString& message,
251 const wxString& caption,
252 const wxArrayString& aChoices,
253 wxWindow *parent,
254 int x, int y,
255 bool centre,
256 int width, int height)
c801d85f 257{
d6c9c1b7
VZ
258 wxString *choices;
259 int n = ConvertWXArrayToC(aChoices, &choices);
260 size_t res = wxGetMultipleChoices(selections, message, caption,
261 n, choices, parent,
262 x, y, centre, width, height);
263 delete [] choices;
264
265 return res;
c801d85f 266}
c801d85f 267
d6c9c1b7
VZ
268// ----------------------------------------------------------------------------
269// wxAnyChoiceDialog
270// ----------------------------------------------------------------------------
271
272bool wxAnyChoiceDialog::Create(wxWindow *parent,
273 const wxString& message,
274 const wxString& caption,
275 int n, const wxString *choices,
276 long styleDlg,
277 const wxPoint& pos,
278 long styleLbox)
279{
280 if ( !wxDialog::Create(parent, -1, caption, pos, wxDefaultSize,
281 wxCHOICEDLG_DIALOG_STYLE) )
282 return FALSE;
283
284 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
285
286 // 1) text message
287 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
288
289 // 2) list box
290 m_listbox = new wxListBox( this, wxID_LISTBOX,
291 wxDefaultPosition, wxDefaultSize,
292 n, choices,
293 styleLbox );
294 if ( n > 0 )
295 m_listbox->SetSelection(0);
296
297 topsizer->Add( m_listbox, 1, wxEXPAND | wxLEFT|wxRIGHT, 15 );
298
299#if wxUSE_STATLINE
300 // 3) static line
301 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
302#endif
303
304 // 4) buttons
305 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxCENTRE | wxALL, 10 );
306
307 SetAutoLayout( TRUE );
308 SetSizer( topsizer );
309
310 topsizer->SetSizeHints( this );
311 topsizer->Fit( this );
312
313 Centre( wxBOTH );
314
315 m_listbox->SetFocus();
316
317 return TRUE;
318}
319
320// ----------------------------------------------------------------------------
c801d85f 321// wxSingleChoiceDialog
d6c9c1b7 322// ----------------------------------------------------------------------------
c801d85f 323
c801d85f 324BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
d427503c
VZ
325 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
326 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
c801d85f
KB
327END_EVENT_TABLE()
328
d6c9c1b7 329IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
257bf510
VZ
330
331wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
332 const wxString& message,
333 const wxString& caption,
c50f1fb9 334 int n,
257bf510
VZ
335 const wxString *choices,
336 char **clientData,
337 long style,
338 const wxPoint& pos)
c801d85f 339{
257bf510 340 Create(parent, message, caption, n, choices, clientData, style);
c801d85f
KB
341}
342
d6c9c1b7
VZ
343#ifdef WXWIN_COMPATIBILITY_2
344
257bf510
VZ
345wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
346 const wxString& message,
347 const wxString& caption,
c50f1fb9 348 const wxStringList& choices,
b91b2200 349 char **clientData,
c50f1fb9 350 long style,
257bf510 351 const wxPoint& pos)
c801d85f 352{
257bf510 353 Create(parent, message, caption, choices, clientData, style);
c801d85f
KB
354}
355
257bf510
VZ
356bool wxSingleChoiceDialog::Create(wxWindow *parent,
357 const wxString& message,
358 const wxString& caption,
359 const wxStringList& choices,
360 char **clientData,
361 long style,
362 const wxPoint& pos)
c801d85f 363{
d427503c
VZ
364 wxString *strings = new wxString[choices.Number()];
365 int i;
366 for ( i = 0; i < choices.Number(); i++)
367 {
368 strings[i] = (char *)choices.Nth(i)->Data();
369 }
370 bool ans = Create(parent, message, caption, choices.Number(), strings, clientData, style, pos);
371 delete[] strings;
372 return ans;
c801d85f
KB
373}
374
d6c9c1b7
VZ
375#endif // WXWIN_COMPATIBILITY_2
376
377bool wxSingleChoiceDialog::Create( wxWindow *parent,
c50f1fb9 378 const wxString& message,
d6c9c1b7 379 const wxString& caption,
c50f1fb9 380 int n,
257bf510
VZ
381 const wxString *choices,
382 char **clientData,
383 long style,
d6c9c1b7 384 const wxPoint& pos )
c801d85f 385{
d6c9c1b7
VZ
386 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
387 n, choices,
388 style, pos) )
389 return FALSE;
92afa2b1 390
d6c9c1b7 391 m_selection = n > 0 ? 0 : -1;
92afa2b1 392
92afa2b1 393 if (clientData)
d427503c 394 {
257bf510
VZ
395 for (int i = 0; i < n; i++)
396 m_listbox->SetClientData(i, clientData[i]);
d427503c 397 }
c801d85f 398
d427503c 399 return TRUE;
c801d85f
KB
400}
401
ef77f91e
JS
402// Set the selection
403void wxSingleChoiceDialog::SetSelection(int sel)
404{
257bf510 405 m_listbox->SetSelection(sel);
ef77f91e
JS
406 m_selection = sel;
407}
408
c801d85f
KB
409void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
410{
257bf510
VZ
411 m_selection = m_listbox->GetSelection();
412 m_stringSelection = m_listbox->GetStringSelection();
25f47127
JS
413 // TODO!
414#ifndef __WXMOTIF__
eb553cb2
VZ
415 if ( m_listbox->HasClientUntypedData() )
416 SetClientData(m_listbox->GetClientData(m_selection));
25f47127 417#endif
d427503c 418 EndModal(wxID_OK);
c801d85f
KB
419}
420
debe6624
JS
421void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
422{
257bf510
VZ
423 m_selection = m_listbox->GetSelection();
424 m_stringSelection = m_listbox->GetStringSelection();
25f47127
JS
425
426 // TODO!
427#ifndef __WXMOTIF__
eb553cb2
VZ
428 if ( m_listbox->HasClientUntypedData() )
429 SetClientData(m_listbox->GetClientData(m_selection));
25f47127 430#endif
d427503c
VZ
431
432 EndModal(wxID_OK);
debe6624
JS
433}
434
d6c9c1b7
VZ
435// ----------------------------------------------------------------------------
436// wxMultiChoiceDialog
437// ----------------------------------------------------------------------------
438
d6c9c1b7
VZ
439IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
440
441bool wxMultiChoiceDialog::Create( wxWindow *parent,
442 const wxString& message,
443 const wxString& caption,
444 int n,
445 const wxString *choices,
446 long style,
447 const wxPoint& pos )
448{
449 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
450 n, choices,
451 style, pos,
3d49ce44 452 wxLB_ALWAYS_SB | wxLB_EXTENDED) )
d6c9c1b7
VZ
453 return FALSE;
454
455 return TRUE;
456}
457
458void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
459{
460 size_t count = selections.GetCount();
461 for ( size_t n = 0; n < count; n++ )
462 {
463 m_listbox->Select(selections[n]);
464 }
465}
466
3d49ce44 467bool wxMultiChoiceDialog::TransferDataFromWindow()
d6c9c1b7
VZ
468{
469 m_selections.Empty();
470 size_t count = m_listbox->GetCount();
471 for ( size_t n = 0; n < count; n++ )
472 {
473 if ( m_listbox->IsSelected(n) )
474 m_selections.Add(n);
475 }
476
3d49ce44 477 return TRUE;
d6c9c1b7 478}