added missing function
[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 license
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 #ifndef WX_PRECOMP
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"
39 #include "wx/sizer.h"
40 #endif
41
42 #if wxUSE_STATLINE
43 #include "wx/statline.h"
44 #endif
45
46 #include "wx/generic/choicdgg.h"
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 #define wxID_LISTBOX 3000
53
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
70 static int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices);
71
72 // ============================================================================
73 // implementation
74 // ============================================================================
75
76 // ----------------------------------------------------------------------------
77 // helpers
78 // ----------------------------------------------------------------------------
79
80 int 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
96 wxString 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),
102 int WXUNUSED(width), int WXUNUSED(height) )
103 {
104 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
105 wxString choice;
106 if ( dialog.ShowModal() == wxID_OK )
107 choice = dialog.GetStringSelection();
108
109 return choice;
110 }
111
112 wxString 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
130 // Overloaded for backward compatibility
131 wxString wxGetSingleChoice( const wxString& message,
132 const wxString& caption,
133 int n, char *choices[],
134 wxWindow *parent,
135 int x, int y, bool centre,
136 int width, int height )
137 {
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;
148 }
149 #endif // WXWIN_COMPATIBILITY_2
150
151 int 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) )
158 {
159 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
160 int choice;
161 if ( dialog.ShowModal() == wxID_OK )
162 choice = dialog.GetSelection();
163 else
164 choice = -1;
165
166 return choice;
167 }
168
169 #ifdef WXWIN_COMPATIBILITY_2
170 // Overloaded for backward compatibility
171 int 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 )
177 {
178 wxString *strings = new wxString[n];
179 for ( int i = 0; i < n; i++)
180 strings[i] = choices[i];
181 int ans = wxGetSingleChoiceIndex(message, caption, n, (const wxString *)strings, parent,
182 x, y, centre, width, height);
183 delete[] strings;
184 return ans;
185 }
186 #endif // WXWIN_COMPATIBILITY_2
187
188 void *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) )
196 {
197 wxSingleChoiceDialog dialog(parent, message, caption, n, choices,
198 (char **)client_data);
199 void *data;
200 if ( dialog.ShowModal() == wxID_OK )
201 data = dialog.GetSelectionClientData();
202 else
203 data = NULL;
204
205 return data;
206 }
207
208 void *wxGetSingleChoiceData( const wxString& message,
209 const wxString& caption,
210 const wxArrayString& aChoices,
211 void **client_data,
212 wxWindow *parent,
213 int x, int y,
214 bool centre,
215 int width, int height)
216 {
217 wxString *choices;
218 int n = ConvertWXArrayToC(aChoices, &choices);
219 void *res = wxGetSingleChoiceData(message, caption, n, choices,
220 client_data, parent,
221 x, y, centre, width, height);
222 delete [] choices;
223
224 return res;
225 }
226
227 #ifdef WXWIN_COMPATIBILITY_2
228 // Overloaded for backward compatibility
229 void *wxGetSingleChoiceData( const wxString& message,
230 const wxString& caption,
231 int n, wxChar *choices[],
232 void **client_data,
233 wxWindow *parent,
234 int x, int y, bool centre, int width, int height )
235 {
236 wxString *strings = new wxString[n];
237 int i;
238 for ( i = 0; i < n; i++)
239 {
240 strings[i] = choices[i];
241 }
242 void *data = wxGetSingleChoiceData(message, caption,
243 n, (const wxString *)strings,
244 client_data, parent,
245 x, y, centre, width, height);
246 delete[] strings;
247 return data;
248 }
249 #endif // WXWIN_COMPATIBILITY_2
250
251 size_t wxGetMultipleChoices(wxArrayInt& selections,
252 const wxString& message,
253 const wxString& caption,
254 int n, const wxString *choices,
255 wxWindow *parent,
256 int WXUNUSED(x), int WXUNUSED(y),
257 bool WXUNUSED(centre),
258 int WXUNUSED(width), int WXUNUSED(height))
259 {
260 wxMultiChoiceDialog dialog(parent, message, caption, n, choices);
261 if ( dialog.ShowModal() == wxID_OK )
262 selections = dialog.GetSelections();
263 else
264 selections.Empty();
265
266 return selections.GetCount();
267 }
268
269 size_t wxGetMultipleChoices(wxArrayInt& selections,
270 const wxString& message,
271 const wxString& caption,
272 const wxArrayString& aChoices,
273 wxWindow *parent,
274 int x, int y,
275 bool centre,
276 int width, int height)
277 {
278 wxString *choices;
279 int n = ConvertWXArrayToC(aChoices, &choices);
280 size_t res = wxGetMultipleChoices(selections, message, caption,
281 n, choices, parent,
282 x, y, centre, width, height);
283 delete [] choices;
284
285 return res;
286 }
287
288 // ----------------------------------------------------------------------------
289 // wxAnyChoiceDialog
290 // ----------------------------------------------------------------------------
291
292 bool wxAnyChoiceDialog::Create(wxWindow *parent,
293 const wxString& message,
294 const wxString& caption,
295 int n, const wxString *choices,
296 long WXUNUSED(styleDlg), // FIXME: why unused?
297 const wxPoint& pos,
298 long styleLbox)
299 {
300 if ( !wxDialog::Create(parent, -1, caption, pos, wxDefaultSize,
301 wxCHOICEDLG_DIALOG_STYLE) )
302 return FALSE;
303
304 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
305
306 // 1) text message
307 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
308
309 // 2) list box
310 m_listbox = new wxListBox( this, wxID_LISTBOX,
311 wxDefaultPosition, wxDefaultSize,
312 n, choices,
313 styleLbox );
314 if ( n > 0 )
315 m_listbox->SetSelection(0);
316
317 topsizer->Add( m_listbox, 1, wxEXPAND | wxLEFT|wxRIGHT, 15 );
318
319 #if wxUSE_STATLINE
320 // 3) static line
321 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
322 #endif
323
324 // 4) buttons
325 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxCENTRE | wxALL, 10 );
326
327 SetAutoLayout( TRUE );
328 SetSizer( topsizer );
329
330 topsizer->SetSizeHints( this );
331 topsizer->Fit( this );
332
333 Centre( wxBOTH );
334
335 m_listbox->SetFocus();
336
337 return TRUE;
338 }
339
340 // ----------------------------------------------------------------------------
341 // wxSingleChoiceDialog
342 // ----------------------------------------------------------------------------
343
344 BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
345 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
346 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
347 END_EVENT_TABLE()
348
349 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
350
351 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
352 const wxString& message,
353 const wxString& caption,
354 int n,
355 const wxString *choices,
356 char **clientData,
357 long style,
358 const wxPoint& WXUNUSED(pos))
359 {
360 Create(parent, message, caption, n, choices, clientData, style);
361 }
362
363 #ifdef WXWIN_COMPATIBILITY_2
364
365 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
366 const wxString& message,
367 const wxString& caption,
368 const wxStringList& choices,
369 char **clientData,
370 long style,
371 const wxPoint& WXUNUSED(pos))
372 {
373 Create(parent, message, caption, choices, clientData, style);
374 }
375
376 bool wxSingleChoiceDialog::Create(wxWindow *parent,
377 const wxString& message,
378 const wxString& caption,
379 const wxStringList& choices,
380 char **clientData,
381 long style,
382 const wxPoint& pos)
383 {
384 wxString *strings = new wxString[choices.Number()];
385 int i;
386 for ( i = 0; i < choices.Number(); i++)
387 {
388 strings[i] = (char *)choices.Nth(i)->Data();
389 }
390 bool ans = Create(parent, message, caption, choices.Number(), strings, clientData, style, pos);
391 delete[] strings;
392 return ans;
393 }
394
395 #endif // WXWIN_COMPATIBILITY_2
396
397 bool wxSingleChoiceDialog::Create( wxWindow *parent,
398 const wxString& message,
399 const wxString& caption,
400 int n,
401 const wxString *choices,
402 char **clientData,
403 long style,
404 const wxPoint& pos )
405 {
406 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
407 n, choices,
408 style, pos) )
409 return FALSE;
410
411 m_selection = n > 0 ? 0 : -1;
412
413 if (clientData)
414 {
415 for (int i = 0; i < n; i++)
416 m_listbox->SetClientData(i, clientData[i]);
417 }
418
419 return TRUE;
420 }
421
422 // Set the selection
423 void wxSingleChoiceDialog::SetSelection(int sel)
424 {
425 m_listbox->SetSelection(sel);
426 m_selection = sel;
427 }
428
429 void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
430 {
431 m_selection = m_listbox->GetSelection();
432 m_stringSelection = m_listbox->GetStringSelection();
433 // TODO!
434 #ifndef __WXMOTIF__
435 if ( m_listbox->HasClientUntypedData() )
436 SetClientData(m_listbox->GetClientData(m_selection));
437 #endif
438 EndModal(wxID_OK);
439 }
440
441 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
442 {
443 m_selection = m_listbox->GetSelection();
444 m_stringSelection = m_listbox->GetStringSelection();
445
446 // TODO!
447 #ifndef __WXMOTIF__
448 if ( m_listbox->HasClientUntypedData() )
449 SetClientData(m_listbox->GetClientData(m_selection));
450 #endif
451
452 EndModal(wxID_OK);
453 }
454
455 // ----------------------------------------------------------------------------
456 // wxMultiChoiceDialog
457 // ----------------------------------------------------------------------------
458
459 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
460
461 bool wxMultiChoiceDialog::Create( wxWindow *parent,
462 const wxString& message,
463 const wxString& caption,
464 int n,
465 const wxString *choices,
466 long style,
467 const wxPoint& pos )
468 {
469 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
470 n, choices,
471 style, pos,
472 wxLB_ALWAYS_SB | wxLB_EXTENDED) )
473 return FALSE;
474
475 return TRUE;
476 }
477
478 void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
479 {
480 size_t count = selections.GetCount();
481 for ( size_t n = 0; n < count; n++ )
482 {
483 m_listbox->Select(selections[n]);
484 }
485 }
486
487 bool wxMultiChoiceDialog::TransferDataFromWindow()
488 {
489 // VZ: I hate to do it but I can't fix wxMotif right now (FIXME)
490 #ifdef __WXMOTIF__
491 #define IsSelected Selected
492 #endif
493
494 m_selections.Empty();
495 size_t count = m_listbox->GetCount();
496 for ( size_t n = 0; n < count; n++ )
497 {
498 if ( m_listbox->IsSelected(n) )
499 m_selections.Add(n);
500 }
501
502 return TRUE;
503 }