]> git.saurik.com Git - wxWidgets.git/blob - src/generic/choicdgg.cpp
fix compilation both for Robert's cross-compiler and for all normal compilers by...
[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 // macros
53 // ---------------------------------------------------------------------------
54
55 /* Macro for avoiding #ifdefs when value have to be different depending on size of
56 device we display on - take it from something like wxDesktopPolicy in the future
57 */
58
59 #if defined(__SMARTPHONE__)
60 #define wxLARGESMALL(large,small) small
61 #else
62 #define wxLARGESMALL(large,small) large
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
85 for ( int i = 0; i < n; i++ )
86 {
87 (*choices)[i] = aChoices[i];
88 }
89
90 return n;
91 }
92
93 // ----------------------------------------------------------------------------
94 // wrapper functions
95 // ----------------------------------------------------------------------------
96
97 wxString wxGetSingleChoice( const wxString& message,
98 const wxString& caption,
99 int n, const wxString *choices,
100 wxWindow *parent,
101 int WXUNUSED(x), int WXUNUSED(y),
102 bool WXUNUSED(centre),
103 int WXUNUSED(width), int WXUNUSED(height) )
104 {
105 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
106 wxString choice;
107 if ( dialog.ShowModal() == wxID_OK )
108 choice = dialog.GetStringSelection();
109
110 return choice;
111 }
112
113 wxString wxGetSingleChoice( const wxString& message,
114 const wxString& caption,
115 const wxArrayString& aChoices,
116 wxWindow *parent,
117 int x, int y,
118 bool centre,
119 int width, int height)
120 {
121 wxString *choices;
122 int n = ConvertWXArrayToC(aChoices, &choices);
123 wxString res = wxGetSingleChoice(message, caption, n, choices, parent,
124 x, y, centre, width, height);
125 delete [] choices;
126
127 return res;
128 }
129
130 int wxGetSingleChoiceIndex( const wxString& message,
131 const wxString& caption,
132 int n, const wxString *choices,
133 wxWindow *parent,
134 int WXUNUSED(x), int WXUNUSED(y),
135 bool WXUNUSED(centre),
136 int WXUNUSED(width), int WXUNUSED(height) )
137 {
138 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
139 int choice;
140 if ( dialog.ShowModal() == wxID_OK )
141 choice = dialog.GetSelection();
142 else
143 choice = -1;
144
145 return choice;
146 }
147
148 int wxGetSingleChoiceIndex( const wxString& message,
149 const wxString& caption,
150 const wxArrayString& aChoices,
151 wxWindow *parent,
152 int x, int y,
153 bool centre,
154 int width, int height)
155 {
156 wxString *choices;
157 int n = ConvertWXArrayToC(aChoices, &choices);
158 int res = wxGetSingleChoiceIndex(message, caption, n, choices, parent,
159 x, y, centre, width, height);
160 delete [] choices;
161
162 return res;
163 }
164
165 void *wxGetSingleChoiceData( const wxString& message,
166 const wxString& caption,
167 int n, const wxString *choices,
168 void **client_data,
169 wxWindow *parent,
170 int WXUNUSED(x), int WXUNUSED(y),
171 bool WXUNUSED(centre),
172 int WXUNUSED(width), int WXUNUSED(height) )
173 {
174 wxSingleChoiceDialog dialog(parent, message, caption, n, choices,
175 (char **)client_data);
176 void *data;
177 if ( dialog.ShowModal() == wxID_OK )
178 data = dialog.GetSelectionClientData();
179 else
180 data = NULL;
181
182 return data;
183 }
184
185 void *wxGetSingleChoiceData( const wxString& message,
186 const wxString& caption,
187 const wxArrayString& aChoices,
188 void **client_data,
189 wxWindow *parent,
190 int x, int y,
191 bool centre,
192 int width, int height)
193 {
194 wxString *choices;
195 int n = ConvertWXArrayToC(aChoices, &choices);
196 void *res = wxGetSingleChoiceData(message, caption, n, choices,
197 client_data, parent,
198 x, y, centre, width, height);
199 delete [] choices;
200
201 return res;
202 }
203
204 size_t wxGetMultipleChoices(wxArrayInt& selections,
205 const wxString& message,
206 const wxString& caption,
207 int n, const wxString *choices,
208 wxWindow *parent,
209 int WXUNUSED(x), int WXUNUSED(y),
210 bool WXUNUSED(centre),
211 int WXUNUSED(width), int WXUNUSED(height))
212 {
213 wxMultiChoiceDialog dialog(parent, message, caption, n, choices);
214
215 // call this even if selections array is empty and this then (correctly)
216 // deselects the first item which is selected by default
217 dialog.SetSelections(selections);
218
219 if ( dialog.ShowModal() == wxID_OK )
220 selections = dialog.GetSelections();
221 else
222 selections.Empty();
223
224 return selections.GetCount();
225 }
226
227 size_t wxGetMultipleChoices(wxArrayInt& selections,
228 const wxString& message,
229 const wxString& caption,
230 const wxArrayString& aChoices,
231 wxWindow *parent,
232 int x, int y,
233 bool centre,
234 int width, int height)
235 {
236 wxString *choices;
237 int n = ConvertWXArrayToC(aChoices, &choices);
238 size_t res = wxGetMultipleChoices(selections, message, caption,
239 n, choices, parent,
240 x, y, centre, width, height);
241 delete [] choices;
242
243 return res;
244 }
245
246 // ----------------------------------------------------------------------------
247 // wxAnyChoiceDialog
248 // ----------------------------------------------------------------------------
249
250 bool wxAnyChoiceDialog::Create(wxWindow *parent,
251 const wxString& message,
252 const wxString& caption,
253 int n, const wxString *choices,
254 long styleDlg,
255 const wxPoint& pos,
256 long styleLbox)
257 {
258 #ifdef __WXMAC__
259 if ( !wxDialog::Create(parent, wxID_ANY, caption, pos, wxDefaultSize, styleDlg & (~wxCANCEL) ) )
260 return false;
261 #else
262 if ( !wxDialog::Create(parent, wxID_ANY, caption, pos, wxDefaultSize, styleDlg) )
263 return false;
264 #endif
265
266 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
267
268 // 1) text message
269 #ifdef __WXMAC__
270 // align text and list at least on mac
271 topsizer->Add( CreateTextSizer( message ), 0, wxALL, wxLARGESMALL(15,0) );
272 #else
273 topsizer->Add( CreateTextSizer( message ), 0, wxALL, wxLARGESMALL(10,0) );
274 #endif
275 // 2) list box
276 m_listbox = CreateList(n,choices,styleLbox);
277
278 if ( n > 0 )
279 m_listbox->SetSelection(0);
280
281 topsizer->Add( m_listbox, 1, wxEXPAND|wxLEFT|wxRIGHT, wxLARGESMALL(15,0) );
282
283 // 3) buttons if any
284 wxSizer *buttonSizer = CreateButtonSizer( styleDlg & ButtonSizerFlags , true, wxLARGESMALL(10,0) );
285 if(buttonSizer->GetChildren().GetCount() > 0 )
286 {
287 topsizer->Add( buttonSizer, 0, wxEXPAND | wxALL, wxLARGESMALL(10,0) );
288 }
289 else
290 {
291 topsizer->AddSpacer( wxLARGESMALL(15,0) );
292 delete buttonSizer;
293 }
294
295 SetSizer( topsizer );
296
297 topsizer->SetSizeHints( this );
298 topsizer->Fit( this );
299
300 if ( styleDlg & wxCENTRE )
301 Centre(wxBOTH);
302
303 m_listbox->SetFocus();
304
305 return true;
306 }
307
308 bool wxAnyChoiceDialog::Create(wxWindow *parent,
309 const wxString& message,
310 const wxString& caption,
311 const wxArrayString& choices,
312 long styleDlg,
313 const wxPoint& pos,
314 long styleLbox)
315 {
316 wxCArrayString chs(choices);
317 return Create(parent, message, caption, chs.GetCount(), chs.GetStrings(),
318 styleDlg, pos, styleLbox);
319 }
320
321 wxListBoxBase *wxAnyChoiceDialog::CreateList(int n, const wxString *choices, long styleLbox)
322 {
323 return new wxListBox( this, wxID_LISTBOX,
324 wxDefaultPosition, wxDefaultSize,
325 n, choices,
326 styleLbox );
327 }
328
329 // ----------------------------------------------------------------------------
330 // wxSingleChoiceDialog
331 // ----------------------------------------------------------------------------
332
333 BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
334 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
335 #ifndef __SMARTPHONE__
336 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
337 #endif
338 #ifdef __WXWINCE__
339 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown)
340 #endif
341 END_EVENT_TABLE()
342
343 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
344
345 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
346 const wxString& message,
347 const wxString& caption,
348 int n,
349 const wxString *choices,
350 char **clientData,
351 long style,
352 const wxPoint& WXUNUSED(pos))
353 {
354 Create(parent, message, caption, n, choices, clientData, style);
355 }
356
357 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
358 const wxString& message,
359 const wxString& caption,
360 const wxArrayString& choices,
361 char **clientData,
362 long style,
363 const wxPoint& WXUNUSED(pos))
364 {
365 Create(parent, message, caption, choices, clientData, style);
366 }
367
368 bool wxSingleChoiceDialog::Create( wxWindow *parent,
369 const wxString& message,
370 const wxString& caption,
371 int n,
372 const wxString *choices,
373 char **clientData,
374 long style,
375 const wxPoint& pos )
376 {
377 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
378 n, choices,
379 style, pos) )
380 return false;
381
382 m_selection = n > 0 ? 0 : -1;
383
384 if (clientData)
385 {
386 for (int i = 0; i < n; i++)
387 m_listbox->SetClientData(i, clientData[i]);
388 }
389
390 return true;
391 }
392
393 bool wxSingleChoiceDialog::Create( wxWindow *parent,
394 const wxString& message,
395 const wxString& caption,
396 const wxArrayString& choices,
397 char **clientData,
398 long style,
399 const wxPoint& pos )
400 {
401 wxCArrayString chs(choices);
402 return Create( parent, message, caption, chs.GetCount(), chs.GetStrings(),
403 clientData, style, pos );
404 }
405
406 // Set the selection
407 void wxSingleChoiceDialog::SetSelection(int sel)
408 {
409 m_listbox->SetSelection(sel);
410 m_selection = sel;
411 }
412
413 void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
414 {
415 DoChoice();
416 }
417
418 #ifndef __SMARTPHONE__
419 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
420 {
421 DoChoice();
422 }
423 #endif
424
425 #ifdef __WXWINCE__
426 void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent& WXUNUSED(event))
427 {
428 DoChoice();
429 }
430 #endif
431
432 void wxSingleChoiceDialog::DoChoice()
433 {
434 m_selection = m_listbox->GetSelection();
435 m_stringSelection = m_listbox->GetStringSelection();
436
437 if ( m_listbox->HasClientUntypedData() )
438 SetClientData(m_listbox->GetClientData(m_selection));
439
440 EndModal(wxID_OK);
441 }
442
443 // ----------------------------------------------------------------------------
444 // wxMultiChoiceDialog
445 // ----------------------------------------------------------------------------
446
447 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
448
449 bool wxMultiChoiceDialog::Create( wxWindow *parent,
450 const wxString& message,
451 const wxString& caption,
452 int n,
453 const wxString *choices,
454 long style,
455 const wxPoint& pos )
456 {
457 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
458 n, choices,
459 style, pos,
460 wxLB_ALWAYS_SB | wxLB_EXTENDED) )
461 return false;
462
463 return true;
464 }
465
466 bool wxMultiChoiceDialog::Create( wxWindow *parent,
467 const wxString& message,
468 const wxString& caption,
469 const wxArrayString& choices,
470 long style,
471 const wxPoint& pos )
472 {
473 wxCArrayString chs(choices);
474 return Create( parent, message, caption, chs.GetCount(),
475 chs.GetStrings(), style, pos );
476 }
477
478 void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
479 {
480 // first clear all currently selected items
481 size_t n,
482 count = m_listbox->GetCount();
483 for ( n = 0; n < count; ++n )
484 {
485 m_listbox->Deselect(n);
486 }
487
488 // now select the ones which should be selected
489 count = selections.GetCount();
490 for ( n = 0; n < count; n++ )
491 {
492 m_listbox->Select(selections[n]);
493 }
494 }
495
496 bool wxMultiChoiceDialog::TransferDataFromWindow()
497 {
498 m_selections.Empty();
499 size_t count = m_listbox->GetCount();
500 for ( size_t n = 0; n < count; n++ )
501 {
502 if ( m_listbox->IsSelected(n) )
503 m_selections.Add(n);
504 }
505
506 return true;
507 }
508
509 #if wxUSE_CHECKLISTBOX
510
511 wxListBoxBase *wxMultiChoiceDialog::CreateList(int n, const wxString *choices, long styleLbox)
512 {
513 return new wxCheckListBox( this, wxID_LISTBOX,
514 wxDefaultPosition, wxDefaultSize,
515 n, choices,
516 styleLbox );
517 }
518
519 #endif // wxUSE_CHECKLISTBOX
520
521 #endif // wxUSE_CHOICEDLG