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