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