added wxGetMultiChoice() (which refuses to work for some reason - will fix
[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, (char **)client_data);
198 void *data;
199 if ( dialog.ShowModal() == wxID_OK )
200 data = dialog.GetSelectionClientData();
201 else
202 data = NULL;
203
204 return data;
205 }
206
207 #ifdef WXWIN_COMPATIBILITY_2
208 // Overloaded for backward compatibility
209 void *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 )
215 {
216 wxString *strings = new wxString[n];
217 int i;
218 for ( i = 0; i < n; i++)
219 {
220 strings[i] = choices[i];
221 }
222 void *data = wxGetSingleChoiceData(message, caption,
223 n, (const wxString *)strings,
224 client_data, parent,
225 x, y, centre, width, height);
226 delete[] strings;
227 return data;
228 }
229 #endif // WXWIN_COMPATIBILITY_2
230
231 size_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();
245
246 return selections.GetCount();
247 }
248
249 size_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)
257 {
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;
266 }
267
268 // ----------------------------------------------------------------------------
269 // wxAnyChoiceDialog
270 // ----------------------------------------------------------------------------
271
272 bool 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 // ----------------------------------------------------------------------------
321 // wxSingleChoiceDialog
322 // ----------------------------------------------------------------------------
323
324 BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
325 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
326 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
327 END_EVENT_TABLE()
328
329 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
330
331 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
332 const wxString& message,
333 const wxString& caption,
334 int n,
335 const wxString *choices,
336 char **clientData,
337 long style,
338 const wxPoint& pos)
339 {
340 Create(parent, message, caption, n, choices, clientData, style);
341 }
342
343 #ifdef WXWIN_COMPATIBILITY_2
344
345 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
346 const wxString& message,
347 const wxString& caption,
348 const wxStringList& choices,
349 char **clientData,
350 long style,
351 const wxPoint& pos)
352 {
353 Create(parent, message, caption, choices, clientData, style);
354 }
355
356 bool 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)
363 {
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;
373 }
374
375 #endif // WXWIN_COMPATIBILITY_2
376
377 bool wxSingleChoiceDialog::Create( wxWindow *parent,
378 const wxString& message,
379 const wxString& caption,
380 int n,
381 const wxString *choices,
382 char **clientData,
383 long style,
384 const wxPoint& pos )
385 {
386 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
387 n, choices,
388 style, pos) )
389 return FALSE;
390
391 m_selection = n > 0 ? 0 : -1;
392
393 if (clientData)
394 {
395 for (int i = 0; i < n; i++)
396 m_listbox->SetClientData(i, clientData[i]);
397 }
398
399 return TRUE;
400 }
401
402 // Set the selection
403 void wxSingleChoiceDialog::SetSelection(int sel)
404 {
405 m_listbox->SetSelection(sel);
406 m_selection = sel;
407 }
408
409 void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
410 {
411 m_selection = m_listbox->GetSelection();
412 m_stringSelection = m_listbox->GetStringSelection();
413 // TODO!
414 #ifndef __WXMOTIF__
415 if ( m_listbox->HasClientUntypedData() )
416 SetClientData(m_listbox->GetClientData(m_selection));
417 #endif
418 EndModal(wxID_OK);
419 }
420
421 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
422 {
423 m_selection = m_listbox->GetSelection();
424 m_stringSelection = m_listbox->GetStringSelection();
425
426 // TODO!
427 #ifndef __WXMOTIF__
428 if ( m_listbox->HasClientUntypedData() )
429 SetClientData(m_listbox->GetClientData(m_selection));
430 #endif
431
432 EndModal(wxID_OK);
433 }
434
435 // ----------------------------------------------------------------------------
436 // wxMultiChoiceDialog
437 // ----------------------------------------------------------------------------
438
439 BEGIN_EVENT_TABLE(wxMultiChoiceDialog, wxDialog)
440 EVT_BUTTON(wxID_OK, wxMultiChoiceDialog::OnOK)
441 END_EVENT_TABLE()
442
443 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
444
445 bool wxMultiChoiceDialog::Create( wxWindow *parent,
446 const wxString& message,
447 const wxString& caption,
448 int n,
449 const wxString *choices,
450 long style,
451 const wxPoint& pos )
452 {
453 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
454 n, choices,
455 style, pos,
456 wxLB_ALWAYS_SB | wxLB_MULTIPLE) )
457 return FALSE;
458
459 return TRUE;
460 }
461
462 void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
463 {
464 size_t count = selections.GetCount();
465 for ( size_t n = 0; n < count; n++ )
466 {
467 m_listbox->Select(selections[n]);
468 }
469 }
470
471 void wxMultiChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
472 {
473 m_selections.Empty();
474 size_t count = m_listbox->GetCount();
475 for ( size_t n = 0; n < count; n++ )
476 {
477 if ( m_listbox->IsSelected(n) )
478 m_selections.Add(n);
479 }
480
481 EndModal(wxID_OK);
482 }