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