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