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