Pressing build-in joystick on WinCE phones fires wxEVT_JOY_BUTTON_DOWN event.
[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/stattext.h"
36 #include "wx/intl.h"
37 #include "wx/sizer.h"
38 #include "wx/arrstr.h"
39 #endif
40
41 #if wxUSE_STATLINE
42 #include "wx/statline.h"
43 #endif
44
45 #include "wx/generic/choicdgg.h"
46
47 // ----------------------------------------------------------------------------
48 // constants
49 // ----------------------------------------------------------------------------
50
51 #define wxID_LISTBOX 3000
52
53 // ---------------------------------------------------------------------------
54 // macros
55 // ---------------------------------------------------------------------------
56
57 /* Macro for avoiding #ifdefs when value have to be different depending on size of
58 device we display on - take it from something like wxDesktopPolicy in the future
59 */
60
61 #if defined(__SMARTPHONE__)
62 #define wxLARGESMALL(large,small) small
63 #else
64 #define wxLARGESMALL(large,small) large
65 #endif
66
67 // ----------------------------------------------------------------------------
68 // private functions
69 // ----------------------------------------------------------------------------
70
71 // convert wxArrayString into a wxString[] which must be delete[]d by caller
72 static int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices);
73
74 // ============================================================================
75 // implementation
76 // ============================================================================
77
78 // ----------------------------------------------------------------------------
79 // helpers
80 // ----------------------------------------------------------------------------
81
82 int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices)
83 {
84 int n = aChoices.GetCount();
85 *choices = new wxString[n];
86
87 for ( int i = 0; i < n; i++ )
88 {
89 (*choices)[i] = aChoices[i];
90 }
91
92 return n;
93 }
94
95 // ----------------------------------------------------------------------------
96 // wrapper functions
97 // ----------------------------------------------------------------------------
98
99 wxString wxGetSingleChoice( const wxString& message,
100 const wxString& caption,
101 int n, const wxString *choices,
102 wxWindow *parent,
103 int WXUNUSED(x), int WXUNUSED(y),
104 bool WXUNUSED(centre),
105 int WXUNUSED(width), int WXUNUSED(height) )
106 {
107 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
108 wxString choice;
109 if ( dialog.ShowModal() == wxID_OK )
110 choice = dialog.GetStringSelection();
111
112 return choice;
113 }
114
115 wxString wxGetSingleChoice( const wxString& message,
116 const wxString& caption,
117 const wxArrayString& aChoices,
118 wxWindow *parent,
119 int x, int y,
120 bool centre,
121 int width, int height)
122 {
123 wxString *choices;
124 int n = ConvertWXArrayToC(aChoices, &choices);
125 wxString res = wxGetSingleChoice(message, caption, n, choices, parent,
126 x, y, centre, width, height);
127 delete [] choices;
128
129 return res;
130 }
131
132 int wxGetSingleChoiceIndex( const wxString& message,
133 const wxString& caption,
134 int n, const wxString *choices,
135 wxWindow *parent,
136 int WXUNUSED(x), int WXUNUSED(y),
137 bool WXUNUSED(centre),
138 int WXUNUSED(width), int WXUNUSED(height) )
139 {
140 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
141 int choice;
142 if ( dialog.ShowModal() == wxID_OK )
143 choice = dialog.GetSelection();
144 else
145 choice = -1;
146
147 return choice;
148 }
149
150 int wxGetSingleChoiceIndex( const wxString& message,
151 const wxString& caption,
152 const wxArrayString& aChoices,
153 wxWindow *parent,
154 int x, int y,
155 bool centre,
156 int width, int height)
157 {
158 wxString *choices;
159 int n = ConvertWXArrayToC(aChoices, &choices);
160 int res = wxGetSingleChoiceIndex(message, caption, n, choices, parent,
161 x, y, centre, width, height);
162 delete [] choices;
163
164 return res;
165 }
166
167 void *wxGetSingleChoiceData( const wxString& message,
168 const wxString& caption,
169 int n, const wxString *choices,
170 void **client_data,
171 wxWindow *parent,
172 int WXUNUSED(x), int WXUNUSED(y),
173 bool WXUNUSED(centre),
174 int WXUNUSED(width), int WXUNUSED(height) )
175 {
176 wxSingleChoiceDialog dialog(parent, message, caption, n, choices,
177 (char **)client_data);
178 void *data;
179 if ( dialog.ShowModal() == wxID_OK )
180 data = dialog.GetSelectionClientData();
181 else
182 data = NULL;
183
184 return data;
185 }
186
187 void *wxGetSingleChoiceData( const wxString& message,
188 const wxString& caption,
189 const wxArrayString& aChoices,
190 void **client_data,
191 wxWindow *parent,
192 int x, int y,
193 bool centre,
194 int width, int height)
195 {
196 wxString *choices;
197 int n = ConvertWXArrayToC(aChoices, &choices);
198 void *res = wxGetSingleChoiceData(message, caption, n, choices,
199 client_data, parent,
200 x, y, centre, width, height);
201 delete [] choices;
202
203 return res;
204 }
205
206 size_t wxGetMultipleChoices(wxArrayInt& selections,
207 const wxString& message,
208 const wxString& caption,
209 int n, const wxString *choices,
210 wxWindow *parent,
211 int WXUNUSED(x), int WXUNUSED(y),
212 bool WXUNUSED(centre),
213 int WXUNUSED(width), int WXUNUSED(height))
214 {
215 wxMultiChoiceDialog dialog(parent, message, caption, n, choices);
216
217 // call this even if selections array is empty and this then (correctly)
218 // deselects the first item which is selected by default
219 dialog.SetSelections(selections);
220
221 if ( dialog.ShowModal() == wxID_OK )
222 selections = dialog.GetSelections();
223 else
224 selections.Empty();
225
226 return selections.GetCount();
227 }
228
229 size_t wxGetMultipleChoices(wxArrayInt& selections,
230 const wxString& message,
231 const wxString& caption,
232 const wxArrayString& aChoices,
233 wxWindow *parent,
234 int x, int y,
235 bool centre,
236 int width, int height)
237 {
238 wxString *choices;
239 int n = ConvertWXArrayToC(aChoices, &choices);
240 size_t res = wxGetMultipleChoices(selections, message, caption,
241 n, choices, parent,
242 x, y, centre, width, height);
243 delete [] choices;
244
245 return res;
246 }
247
248 // ----------------------------------------------------------------------------
249 // wxAnyChoiceDialog
250 // ----------------------------------------------------------------------------
251
252 bool wxAnyChoiceDialog::Create(wxWindow *parent,
253 const wxString& message,
254 const wxString& caption,
255 int n, const wxString *choices,
256 long styleDlg,
257 const wxPoint& pos,
258 long styleLbox)
259 {
260 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
261 styleDlg &= ~wxBORDER_MASK;
262 styleDlg &= ~wxRESIZE_BORDER;
263 styleDlg &= ~wxCAPTION;
264 #endif
265
266 if ( !wxDialog::Create(parent, wxID_ANY, caption, pos, wxDefaultSize, styleDlg) )
267 return false;
268
269 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
270
271 // 1) text message
272 topsizer->Add( CreateTextSizer( message ), 0, wxALL, wxLARGESMALL(10,0) );
273
274 // 2) list box
275 m_listbox = new wxListBox( this, wxID_LISTBOX,
276 wxDefaultPosition, wxDefaultSize,
277 n, choices,
278 styleLbox );
279 if ( n > 0 )
280 m_listbox->SetSelection(0);
281
282 topsizer->Add( m_listbox, 1, wxEXPAND|wxLEFT|wxRIGHT, wxLARGESMALL(15,0) );
283
284 // smart phones does not support or do not waste space for wxButtons
285 #ifdef __SMARTPHONE__
286
287 SetRightMenu(wxID_CANCEL, _("Cancel"));
288
289 #else // __SMARTPHONE__/!__SMARTPHONE__
290
291 #if wxUSE_STATLINE
292 // 3) static line
293 topsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
294 #endif
295
296 // 4) buttons
297 topsizer->Add( CreateButtonSizer( styleDlg & (wxOK|wxCANCEL) ), 0, wxEXPAND | wxALL, 10 );
298
299 #endif // !__SMARTPHONE__
300
301 SetSizer( topsizer );
302
303 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
304 topsizer->SetSizeHints( this );
305 topsizer->Fit( this );
306
307 if ( styleDlg & wxCENTRE )
308 Centre(wxBOTH);
309 #endif
310
311 m_listbox->SetFocus();
312
313 return true;
314 }
315
316 bool wxAnyChoiceDialog::Create(wxWindow *parent,
317 const wxString& message,
318 const wxString& caption,
319 const wxArrayString& choices,
320 long styleDlg,
321 const wxPoint& pos,
322 long styleLbox)
323 {
324 wxCArrayString chs(choices);
325 return Create(parent, message, caption, chs.GetCount(), chs.GetStrings(),
326 styleDlg, pos, 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 #endif // wxUSE_CHOICEDLG