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