]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicdgg.cpp
minor cleanup
[wxWidgets.git] / src / generic / choicdgg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
7b504551 2// Name: src/generic/choicdgg.cpp
c801d85f
KB
3// Purpose: Choice dialogs
4// Author: Julian Smart
d6c9c1b7 5// Modified by: 03.11.00: VZ to add wxArrayString and multiple sel functions
c801d85f
KB
6// Created: 04/01/98
7// RCS-ID: $Id$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
d6c9c1b7
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
d6c9c1b7
VZ
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c801d85f
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
d427503c 24 #pragma hdrstop
c801d85f
KB
25#endif
26
1e6feb95
VZ
27#if wxUSE_CHOICEDLG
28
c801d85f 29#ifndef WX_PRECOMP
257bf510
VZ
30 #include <stdio.h>
31 #include "wx/utils.h"
32 #include "wx/dialog.h"
33 #include "wx/button.h"
34 #include "wx/listbox.h"
63c02113 35 #include "wx/checklst.h"
257bf510
VZ
36 #include "wx/stattext.h"
37 #include "wx/intl.h"
92afa2b1 38 #include "wx/sizer.h"
2da2f941 39 #include "wx/arrstr.h"
dcf924a3
RR
40#endif
41
42#if wxUSE_STATLINE
c50f1fb9 43 #include "wx/statline.h"
c801d85f
KB
44#endif
45
46#include "wx/generic/choicdgg.h"
47
d6c9c1b7
VZ
48// ----------------------------------------------------------------------------
49// constants
50// ----------------------------------------------------------------------------
51
257bf510 52#define wxID_LISTBOX 3000
dcf924a3 53
4b088139
WS
54// ---------------------------------------------------------------------------
55// macros
56// ---------------------------------------------------------------------------
57
58/* Macro for avoiding #ifdefs when value have to be different depending on size of
9a357011 59 device we display on - take it from something like wxDesktopPolicy in the future
4b088139
WS
60 */
61
62#if defined(__SMARTPHONE__)
63 #define wxLARGESMALL(large,small) small
64#else
65 #define wxLARGESMALL(large,small) large
66#endif
67
d6c9c1b7
VZ
68// ----------------------------------------------------------------------------
69// private functions
70// ----------------------------------------------------------------------------
71
72// convert wxArrayString into a wxString[] which must be delete[]d by caller
73static int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices);
74
75// ============================================================================
76// implementation
77// ============================================================================
78
79// ----------------------------------------------------------------------------
80// helpers
81// ----------------------------------------------------------------------------
82
83int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices)
84{
85 int n = aChoices.GetCount();
86 *choices = new wxString[n];
54b84891 87
d6c9c1b7
VZ
88 for ( int i = 0; i < n; i++ )
89 {
ea660175 90 (*choices)[i] = aChoices[i];
d6c9c1b7
VZ
91 }
92
93 return n;
94}
95
96// ----------------------------------------------------------------------------
97// wrapper functions
98// ----------------------------------------------------------------------------
99
100wxString 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),
257bf510 106 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 107{
d427503c 108 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
3ca6a5f0 109 wxString choice;
d427503c 110 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0
BP
111 choice = dialog.GetStringSelection();
112
113 return choice;
c801d85f
KB
114}
115
d6c9c1b7
VZ
116wxString 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
d6c9c1b7
VZ
133int 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) )
c801d85f 140{
d427503c 141 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
3ca6a5f0 142 int choice;
d427503c 143 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0 144 choice = dialog.GetSelection();
d427503c 145 else
3ca6a5f0
BP
146 choice = -1;
147
148 return choice;
c801d85f
KB
149}
150
2adaf596
VS
151int 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
d6c9c1b7
VZ
168void *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) )
c801d85f 176{
b41ec29a
VZ
177 wxSingleChoiceDialog dialog(parent, message, caption, n, choices,
178 (char **)client_data);
3ca6a5f0 179 void *data;
d427503c 180 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0 181 data = dialog.GetSelectionClientData();
d427503c 182 else
3ca6a5f0
BP
183 data = NULL;
184
185 return data;
c801d85f
KB
186}
187
b41ec29a
VZ
188void *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
d6c9c1b7
VZ
207size_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);
e93bfe3c 217
b2d739ba
VZ
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);
e93bfe3c 221
d6c9c1b7
VZ
222 if ( dialog.ShowModal() == wxID_OK )
223 selections = dialog.GetSelections();
224 else
225 selections.Empty();
c801d85f 226
d6c9c1b7
VZ
227 return selections.GetCount();
228}
c801d85f 229
59bd9598 230size_t wxGetMultipleChoices(wxArrayInt& selections,
d6c9c1b7
VZ
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)
c801d85f 238{
d6c9c1b7
VZ
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;
c801d85f 247}
c801d85f 248
d6c9c1b7
VZ
249// ----------------------------------------------------------------------------
250// wxAnyChoiceDialog
251// ----------------------------------------------------------------------------
252
253bool wxAnyChoiceDialog::Create(wxWindow *parent,
254 const wxString& message,
255 const wxString& caption,
256 int n, const wxString *choices,
ea660175 257 long styleDlg,
d6c9c1b7
VZ
258 const wxPoint& pos,
259 long styleLbox)
260{
aa66250b
JS
261#if defined(__SMARTPHONE__) || defined(__POCKETPC__)
262 styleDlg &= ~wxBORDER_MASK;
263 styleDlg &= ~wxRESIZE_BORDER;
264 styleDlg &= ~wxCAPTION;
265#endif
a319ce7d
SC
266#ifdef __WXMAC__
267 if ( !wxDialog::Create(parent, wxID_ANY, caption, pos, wxDefaultSize, styleDlg & (~wxCANCEL) ) )
268 return false;
269#else
ca65c044
WS
270 if ( !wxDialog::Create(parent, wxID_ANY, caption, pos, wxDefaultSize, styleDlg) )
271 return false;
a319ce7d 272#endif
d6c9c1b7
VZ
273
274 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
275
64c794f6 276 // 1) text message
a319ce7d
SC
277#ifdef __WXMAC__
278 // align text and list at least on mac
279 topsizer->Add( CreateTextSizer( message ), 0, wxALL, wxLARGESMALL(15,0) );
280#else
718970e5 281 topsizer->Add( CreateTextSizer( message ), 0, wxALL, wxLARGESMALL(10,0) );
a319ce7d 282#endif
64c794f6 283 // 2) list box
60104cba
WS
284 m_listbox = CreateList(n,choices,styleLbox);
285
64c794f6
WS
286 if ( n > 0 )
287 m_listbox->SetSelection(0);
288
aa66250b 289 topsizer->Add( m_listbox, 1, wxEXPAND|wxLEFT|wxRIGHT, wxLARGESMALL(15,0) );
119727ad 290
9a357011 291 // smart phones does not support or do not waste space for wxButtons
119727ad 292#ifdef __SMARTPHONE__
64c794f6
WS
293
294 SetRightMenu(wxID_CANCEL, _("Cancel"));
295
296#else // __SMARTPHONE__/!__SMARTPHONE__
297
a319ce7d
SC
298 // Mac Human Interface Guidelines recommend not to use static lines as grouping elements
299#ifndef __WXMAC__
d6c9c1b7
VZ
300#if wxUSE_STATLINE
301 // 3) static line
ca65c044 302 topsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
d6c9c1b7 303#endif
a319ce7d
SC
304#endif
305
d6c9c1b7 306 // 4) buttons
acf2ac37 307 topsizer->Add( CreateButtonSizer( styleDlg & (wxOK|wxCANCEL) ), 0, wxEXPAND | wxALL, 10 );
d6c9c1b7 308
64c794f6
WS
309#endif // !__SMARTPHONE__
310
d6c9c1b7
VZ
311 SetSizer( topsizer );
312
aa66250b 313#if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
d6c9c1b7
VZ
314 topsizer->SetSizeHints( this );
315 topsizer->Fit( this );
316
8316ff5d
VS
317 if ( styleDlg & wxCENTRE )
318 Centre(wxBOTH);
aa66250b 319#endif
d6c9c1b7
VZ
320
321 m_listbox->SetFocus();
322
ca65c044 323 return true;
d6c9c1b7
VZ
324}
325
584ad2a3
MB
326bool 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
60104cba
WS
339wxListBoxBase *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
d6c9c1b7 347// ----------------------------------------------------------------------------
c801d85f 348// wxSingleChoiceDialog
d6c9c1b7 349// ----------------------------------------------------------------------------
c801d85f 350
c801d85f 351BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
d427503c 352 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
7b504551 353#ifndef __SMARTPHONE__
d427503c 354 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
7b504551
WS
355#endif
356#ifdef __WXWINCE__
357 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown)
358#endif
c801d85f
KB
359END_EVENT_TABLE()
360
d6c9c1b7 361IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
257bf510
VZ
362
363wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
364 const wxString& message,
365 const wxString& caption,
c50f1fb9 366 int n,
257bf510
VZ
367 const wxString *choices,
368 char **clientData,
369 long style,
59bd9598 370 const wxPoint& WXUNUSED(pos))
c801d85f 371{
257bf510 372 Create(parent, message, caption, n, choices, clientData, style);
c801d85f
KB
373}
374
584ad2a3
MB
375wxSingleChoiceDialog::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
d6c9c1b7 386bool wxSingleChoiceDialog::Create( wxWindow *parent,
c50f1fb9 387 const wxString& message,
d6c9c1b7 388 const wxString& caption,
c50f1fb9 389 int n,
257bf510
VZ
390 const wxString *choices,
391 char **clientData,
392 long style,
d6c9c1b7 393 const wxPoint& pos )
c801d85f 394{
d6c9c1b7
VZ
395 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
396 n, choices,
397 style, pos) )
ca65c044 398 return false;
92afa2b1 399
d6c9c1b7 400 m_selection = n > 0 ? 0 : -1;
92afa2b1 401
92afa2b1 402 if (clientData)
d427503c 403 {
257bf510
VZ
404 for (int i = 0; i < n; i++)
405 m_listbox->SetClientData(i, clientData[i]);
d427503c 406 }
c801d85f 407
ca65c044 408 return true;
c801d85f
KB
409}
410
584ad2a3
MB
411bool 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
ef77f91e
JS
424// Set the selection
425void wxSingleChoiceDialog::SetSelection(int sel)
426{
257bf510 427 m_listbox->SetSelection(sel);
ef77f91e
JS
428 m_selection = sel;
429}
430
c801d85f
KB
431void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
432{
7b504551 433 DoChoice();
c801d85f
KB
434}
435
7b504551 436#ifndef __SMARTPHONE__
debe6624 437void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
7b504551
WS
438{
439 DoChoice();
440}
441#endif
442
443#ifdef __WXWINCE__
444void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent& WXUNUSED(event))
445{
446 DoChoice();
447}
448#endif
449
450void wxSingleChoiceDialog::DoChoice()
debe6624 451{
257bf510
VZ
452 m_selection = m_listbox->GetSelection();
453 m_stringSelection = m_listbox->GetStringSelection();
25f47127 454
eb553cb2 455 if ( m_listbox->HasClientUntypedData() )
59af5f19 456 SetClientData(m_listbox->GetClientData(m_selection));
d427503c
VZ
457
458 EndModal(wxID_OK);
debe6624
JS
459}
460
d6c9c1b7
VZ
461// ----------------------------------------------------------------------------
462// wxMultiChoiceDialog
463// ----------------------------------------------------------------------------
464
d6c9c1b7
VZ
465IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
466
467bool 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,
3d49ce44 478 wxLB_ALWAYS_SB | wxLB_EXTENDED) )
ca65c044 479 return false;
d6c9c1b7 480
ca65c044 481 return true;
d6c9c1b7
VZ
482}
483
584ad2a3
MB
484bool 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
d6c9c1b7
VZ
496void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
497{
d0cc483d
VZ
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++ )
d6c9c1b7
VZ
509 {
510 m_listbox->Select(selections[n]);
511 }
512}
513
3d49ce44 514bool wxMultiChoiceDialog::TransferDataFromWindow()
d6c9c1b7
VZ
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
ca65c044 524 return true;
d6c9c1b7 525}
1e6feb95 526
60104cba
WS
527#if wxUSE_CHECKLISTBOX
528
529wxListBoxBase *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
1e6feb95 539#endif // wxUSE_CHOICEDLG