]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicdgg.cpp
Fixed description with number of ways to use timer class, updated note about main...
[wxWidgets.git] / src / generic / choicdgg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
dfad0599 2// Name: 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"
35 #include "wx/stattext.h"
36 #include "wx/intl.h"
92afa2b1 37 #include "wx/sizer.h"
2da2f941 38 #include "wx/arrstr.h"
dcf924a3
RR
39#endif
40
41#if wxUSE_STATLINE
c50f1fb9 42 #include "wx/statline.h"
c801d85f
KB
43#endif
44
45#include "wx/generic/choicdgg.h"
46
d6c9c1b7
VZ
47// ----------------------------------------------------------------------------
48// constants
49// ----------------------------------------------------------------------------
50
257bf510 51#define wxID_LISTBOX 3000
dcf924a3 52
4b088139
WS
53// ---------------------------------------------------------------------------
54// macros
55// ---------------------------------------------------------------------------
56
57/* Macro for avoiding #ifdefs when value have to be different depending on size of
9a357011 58 device we display on - take it from something like wxDesktopPolicy in the future
4b088139
WS
59 */
60
61#if defined(__SMARTPHONE__)
62 #define wxLARGESMALL(large,small) small
63#else
64 #define wxLARGESMALL(large,small) large
65#endif
66
d6c9c1b7
VZ
67// ----------------------------------------------------------------------------
68// private functions
69// ----------------------------------------------------------------------------
70
71// convert wxArrayString into a wxString[] which must be delete[]d by caller
72static int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices);
73
74// ============================================================================
75// implementation
76// ============================================================================
77
78// ----------------------------------------------------------------------------
79// helpers
80// ----------------------------------------------------------------------------
81
82int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices)
83{
84 int n = aChoices.GetCount();
85 *choices = new wxString[n];
54b84891 86
d6c9c1b7
VZ
87 for ( int i = 0; i < n; i++ )
88 {
ea660175 89 (*choices)[i] = aChoices[i];
d6c9c1b7
VZ
90 }
91
92 return n;
93}
94
95// ----------------------------------------------------------------------------
96// wrapper functions
97// ----------------------------------------------------------------------------
98
99wxString 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),
257bf510 105 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 106{
d427503c 107 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
3ca6a5f0 108 wxString choice;
d427503c 109 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0
BP
110 choice = dialog.GetStringSelection();
111
112 return choice;
c801d85f
KB
113}
114
d6c9c1b7
VZ
115wxString 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
d6c9c1b7
VZ
132int 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) )
c801d85f 139{
d427503c 140 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
3ca6a5f0 141 int choice;
d427503c 142 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0 143 choice = dialog.GetSelection();
d427503c 144 else
3ca6a5f0
BP
145 choice = -1;
146
147 return choice;
c801d85f
KB
148}
149
2adaf596
VS
150int 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
d6c9c1b7
VZ
167void *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) )
c801d85f 175{
b41ec29a
VZ
176 wxSingleChoiceDialog dialog(parent, message, caption, n, choices,
177 (char **)client_data);
3ca6a5f0 178 void *data;
d427503c 179 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0 180 data = dialog.GetSelectionClientData();
d427503c 181 else
3ca6a5f0
BP
182 data = NULL;
183
184 return data;
c801d85f
KB
185}
186
b41ec29a
VZ
187void *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
d6c9c1b7
VZ
206size_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);
e93bfe3c 216
b2d739ba
VZ
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);
e93bfe3c 220
d6c9c1b7
VZ
221 if ( dialog.ShowModal() == wxID_OK )
222 selections = dialog.GetSelections();
223 else
224 selections.Empty();
c801d85f 225
d6c9c1b7
VZ
226 return selections.GetCount();
227}
c801d85f 228
59bd9598 229size_t wxGetMultipleChoices(wxArrayInt& selections,
d6c9c1b7
VZ
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)
c801d85f 237{
d6c9c1b7
VZ
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;
c801d85f 246}
c801d85f 247
d6c9c1b7
VZ
248// ----------------------------------------------------------------------------
249// wxAnyChoiceDialog
250// ----------------------------------------------------------------------------
251
252bool wxAnyChoiceDialog::Create(wxWindow *parent,
253 const wxString& message,
254 const wxString& caption,
255 int n, const wxString *choices,
ea660175 256 long styleDlg,
d6c9c1b7
VZ
257 const wxPoint& pos,
258 long styleLbox)
259{
aa66250b
JS
260#if defined(__SMARTPHONE__) || defined(__POCKETPC__)
261 styleDlg &= ~wxBORDER_MASK;
262 styleDlg &= ~wxRESIZE_BORDER;
263 styleDlg &= ~wxCAPTION;
264#endif
265
ca65c044
WS
266 if ( !wxDialog::Create(parent, wxID_ANY, caption, pos, wxDefaultSize, styleDlg) )
267 return false;
d6c9c1b7
VZ
268
269 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
270
64c794f6 271 // 1) text message
718970e5 272 topsizer->Add( CreateTextSizer( message ), 0, wxALL, wxLARGESMALL(10,0) );
64c794f6
WS
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
aa66250b 282 topsizer->Add( m_listbox, 1, wxEXPAND|wxLEFT|wxRIGHT, wxLARGESMALL(15,0) );
119727ad 283
9a357011 284 // smart phones does not support or do not waste space for wxButtons
119727ad 285#ifdef __SMARTPHONE__
64c794f6
WS
286
287 SetRightMenu(wxID_CANCEL, _("Cancel"));
288
289#else // __SMARTPHONE__/!__SMARTPHONE__
290
d6c9c1b7
VZ
291#if wxUSE_STATLINE
292 // 3) static line
ca65c044 293 topsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
d6c9c1b7
VZ
294#endif
295
296 // 4) buttons
acf2ac37 297 topsizer->Add( CreateButtonSizer( styleDlg & (wxOK|wxCANCEL) ), 0, wxEXPAND | wxALL, 10 );
d6c9c1b7 298
64c794f6
WS
299#endif // !__SMARTPHONE__
300
d6c9c1b7
VZ
301 SetSizer( topsizer );
302
aa66250b 303#if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
d6c9c1b7
VZ
304 topsizer->SetSizeHints( this );
305 topsizer->Fit( this );
306
8316ff5d
VS
307 if ( styleDlg & wxCENTRE )
308 Centre(wxBOTH);
aa66250b 309#endif
d6c9c1b7
VZ
310
311 m_listbox->SetFocus();
312
ca65c044 313 return true;
d6c9c1b7
VZ
314}
315
584ad2a3
MB
316bool 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
d6c9c1b7 329// ----------------------------------------------------------------------------
c801d85f 330// wxSingleChoiceDialog
d6c9c1b7 331// ----------------------------------------------------------------------------
c801d85f 332
c801d85f 333BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
d427503c
VZ
334 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
335 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
c801d85f
KB
336END_EVENT_TABLE()
337
d6c9c1b7 338IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
257bf510
VZ
339
340wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
341 const wxString& message,
342 const wxString& caption,
c50f1fb9 343 int n,
257bf510
VZ
344 const wxString *choices,
345 char **clientData,
346 long style,
59bd9598 347 const wxPoint& WXUNUSED(pos))
c801d85f 348{
257bf510 349 Create(parent, message, caption, n, choices, clientData, style);
c801d85f
KB
350}
351
584ad2a3
MB
352wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
353 const wxString& message,
354 const wxString& caption,
355 const wxArrayString& choices,
356 char **clientData,
357 long style,
358 const wxPoint& WXUNUSED(pos))
359{
360 Create(parent, message, caption, choices, clientData, style);
361}
362
d6c9c1b7 363bool wxSingleChoiceDialog::Create( wxWindow *parent,
c50f1fb9 364 const wxString& message,
d6c9c1b7 365 const wxString& caption,
c50f1fb9 366 int n,
257bf510
VZ
367 const wxString *choices,
368 char **clientData,
369 long style,
d6c9c1b7 370 const wxPoint& pos )
c801d85f 371{
d6c9c1b7
VZ
372 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
373 n, choices,
374 style, pos) )
ca65c044 375 return false;
92afa2b1 376
d6c9c1b7 377 m_selection = n > 0 ? 0 : -1;
92afa2b1 378
92afa2b1 379 if (clientData)
d427503c 380 {
257bf510
VZ
381 for (int i = 0; i < n; i++)
382 m_listbox->SetClientData(i, clientData[i]);
d427503c 383 }
c801d85f 384
ca65c044 385 return true;
c801d85f
KB
386}
387
584ad2a3
MB
388bool wxSingleChoiceDialog::Create( wxWindow *parent,
389 const wxString& message,
390 const wxString& caption,
391 const wxArrayString& choices,
392 char **clientData,
393 long style,
394 const wxPoint& pos )
395{
396 wxCArrayString chs(choices);
397 return Create( parent, message, caption, chs.GetCount(), chs.GetStrings(),
398 clientData, style, pos );
399}
400
ef77f91e
JS
401// Set the selection
402void wxSingleChoiceDialog::SetSelection(int sel)
403{
257bf510 404 m_listbox->SetSelection(sel);
ef77f91e
JS
405 m_selection = sel;
406}
407
c801d85f
KB
408void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
409{
257bf510
VZ
410 m_selection = m_listbox->GetSelection();
411 m_stringSelection = m_listbox->GetStringSelection();
eb553cb2 412 if ( m_listbox->HasClientUntypedData() )
59af5f19 413 SetClientData(m_listbox->GetClientData(m_selection));
d427503c 414 EndModal(wxID_OK);
c801d85f
KB
415}
416
debe6624
JS
417void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
418{
257bf510
VZ
419 m_selection = m_listbox->GetSelection();
420 m_stringSelection = m_listbox->GetStringSelection();
25f47127 421
eb553cb2 422 if ( m_listbox->HasClientUntypedData() )
59af5f19 423 SetClientData(m_listbox->GetClientData(m_selection));
d427503c
VZ
424
425 EndModal(wxID_OK);
debe6624
JS
426}
427
d6c9c1b7
VZ
428// ----------------------------------------------------------------------------
429// wxMultiChoiceDialog
430// ----------------------------------------------------------------------------
431
d6c9c1b7
VZ
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 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
443 n, choices,
444 style, pos,
3d49ce44 445 wxLB_ALWAYS_SB | wxLB_EXTENDED) )
ca65c044 446 return false;
d6c9c1b7 447
ca65c044 448 return true;
d6c9c1b7
VZ
449}
450
584ad2a3
MB
451bool wxMultiChoiceDialog::Create( wxWindow *parent,
452 const wxString& message,
453 const wxString& caption,
454 const wxArrayString& choices,
455 long style,
456 const wxPoint& pos )
457{
458 wxCArrayString chs(choices);
459 return Create( parent, message, caption, chs.GetCount(),
460 chs.GetStrings(), style, pos );
461}
462
d6c9c1b7
VZ
463void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
464{
d0cc483d
VZ
465 // first clear all currently selected items
466 size_t n,
467 count = m_listbox->GetCount();
468 for ( n = 0; n < count; ++n )
469 {
470 m_listbox->Deselect(n);
471 }
472
473 // now select the ones which should be selected
474 count = selections.GetCount();
475 for ( n = 0; n < count; n++ )
d6c9c1b7
VZ
476 {
477 m_listbox->Select(selections[n]);
478 }
479}
480
3d49ce44 481bool wxMultiChoiceDialog::TransferDataFromWindow()
d6c9c1b7
VZ
482{
483 m_selections.Empty();
484 size_t count = m_listbox->GetCount();
485 for ( size_t n = 0; n < count; n++ )
486 {
487 if ( m_listbox->IsSelected(n) )
488 m_selections.Add(n);
489 }
490
ca65c044 491 return true;
d6c9c1b7 492}
1e6feb95
VZ
493
494#endif // wxUSE_CHOICEDLG