]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicdgg.cpp
fixed problems with sometimes processing the events twice introduced in rev 1.170...
[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
266
ca65c044
WS
267 if ( !wxDialog::Create(parent, wxID_ANY, caption, pos, wxDefaultSize, styleDlg) )
268 return false;
d6c9c1b7
VZ
269
270 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
271
64c794f6 272 // 1) text message
718970e5 273 topsizer->Add( CreateTextSizer( message ), 0, wxALL, wxLARGESMALL(10,0) );
64c794f6
WS
274
275 // 2) list box
60104cba
WS
276 m_listbox = CreateList(n,choices,styleLbox);
277
64c794f6
WS
278 if ( n > 0 )
279 m_listbox->SetSelection(0);
280
aa66250b 281 topsizer->Add( m_listbox, 1, wxEXPAND|wxLEFT|wxRIGHT, wxLARGESMALL(15,0) );
119727ad 282
9a357011 283 // smart phones does not support or do not waste space for wxButtons
119727ad 284#ifdef __SMARTPHONE__
64c794f6
WS
285
286 SetRightMenu(wxID_CANCEL, _("Cancel"));
287
288#else // __SMARTPHONE__/!__SMARTPHONE__
289
d6c9c1b7
VZ
290#if wxUSE_STATLINE
291 // 3) static line
ca65c044 292 topsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
d6c9c1b7
VZ
293#endif
294
295 // 4) buttons
acf2ac37 296 topsizer->Add( CreateButtonSizer( styleDlg & (wxOK|wxCANCEL) ), 0, wxEXPAND | wxALL, 10 );
d6c9c1b7 297
64c794f6
WS
298#endif // !__SMARTPHONE__
299
d6c9c1b7
VZ
300 SetSizer( topsizer );
301
aa66250b 302#if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
d6c9c1b7
VZ
303 topsizer->SetSizeHints( this );
304 topsizer->Fit( this );
305
8316ff5d
VS
306 if ( styleDlg & wxCENTRE )
307 Centre(wxBOTH);
aa66250b 308#endif
d6c9c1b7
VZ
309
310 m_listbox->SetFocus();
311
ca65c044 312 return true;
d6c9c1b7
VZ
313}
314
584ad2a3
MB
315bool wxAnyChoiceDialog::Create(wxWindow *parent,
316 const wxString& message,
317 const wxString& caption,
318 const wxArrayString& choices,
319 long styleDlg,
320 const wxPoint& pos,
321 long styleLbox)
322{
323 wxCArrayString chs(choices);
324 return Create(parent, message, caption, chs.GetCount(), chs.GetStrings(),
325 styleDlg, pos, styleLbox);
326}
327
60104cba
WS
328wxListBoxBase *wxAnyChoiceDialog::CreateList(int n, const wxString *choices, long styleLbox)
329{
330 return new wxListBox( this, wxID_LISTBOX,
331 wxDefaultPosition, wxDefaultSize,
332 n, choices,
333 styleLbox );
334}
335
d6c9c1b7 336// ----------------------------------------------------------------------------
c801d85f 337// wxSingleChoiceDialog
d6c9c1b7 338// ----------------------------------------------------------------------------
c801d85f 339
c801d85f 340BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
d427503c 341 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
7b504551 342#ifndef __SMARTPHONE__
d427503c 343 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
7b504551
WS
344#endif
345#ifdef __WXWINCE__
346 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown)
347#endif
c801d85f
KB
348END_EVENT_TABLE()
349
d6c9c1b7 350IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
257bf510
VZ
351
352wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
353 const wxString& message,
354 const wxString& caption,
c50f1fb9 355 int n,
257bf510
VZ
356 const wxString *choices,
357 char **clientData,
358 long style,
59bd9598 359 const wxPoint& WXUNUSED(pos))
c801d85f 360{
257bf510 361 Create(parent, message, caption, n, choices, clientData, style);
c801d85f
KB
362}
363
584ad2a3
MB
364wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
365 const wxString& message,
366 const wxString& caption,
367 const wxArrayString& choices,
368 char **clientData,
369 long style,
370 const wxPoint& WXUNUSED(pos))
371{
372 Create(parent, message, caption, choices, clientData, style);
373}
374
d6c9c1b7 375bool wxSingleChoiceDialog::Create( wxWindow *parent,
c50f1fb9 376 const wxString& message,
d6c9c1b7 377 const wxString& caption,
c50f1fb9 378 int n,
257bf510
VZ
379 const wxString *choices,
380 char **clientData,
381 long style,
d6c9c1b7 382 const wxPoint& pos )
c801d85f 383{
d6c9c1b7
VZ
384 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
385 n, choices,
386 style, pos) )
ca65c044 387 return false;
92afa2b1 388
d6c9c1b7 389 m_selection = n > 0 ? 0 : -1;
92afa2b1 390
92afa2b1 391 if (clientData)
d427503c 392 {
257bf510
VZ
393 for (int i = 0; i < n; i++)
394 m_listbox->SetClientData(i, clientData[i]);
d427503c 395 }
c801d85f 396
ca65c044 397 return true;
c801d85f
KB
398}
399
584ad2a3
MB
400bool wxSingleChoiceDialog::Create( wxWindow *parent,
401 const wxString& message,
402 const wxString& caption,
403 const wxArrayString& choices,
404 char **clientData,
405 long style,
406 const wxPoint& pos )
407{
408 wxCArrayString chs(choices);
409 return Create( parent, message, caption, chs.GetCount(), chs.GetStrings(),
410 clientData, style, pos );
411}
412
ef77f91e
JS
413// Set the selection
414void wxSingleChoiceDialog::SetSelection(int sel)
415{
257bf510 416 m_listbox->SetSelection(sel);
ef77f91e
JS
417 m_selection = sel;
418}
419
c801d85f
KB
420void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
421{
7b504551 422 DoChoice();
c801d85f
KB
423}
424
7b504551 425#ifndef __SMARTPHONE__
debe6624 426void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
7b504551
WS
427{
428 DoChoice();
429}
430#endif
431
432#ifdef __WXWINCE__
433void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent& WXUNUSED(event))
434{
435 DoChoice();
436}
437#endif
438
439void wxSingleChoiceDialog::DoChoice()
debe6624 440{
257bf510
VZ
441 m_selection = m_listbox->GetSelection();
442 m_stringSelection = m_listbox->GetStringSelection();
25f47127 443
eb553cb2 444 if ( m_listbox->HasClientUntypedData() )
59af5f19 445 SetClientData(m_listbox->GetClientData(m_selection));
d427503c
VZ
446
447 EndModal(wxID_OK);
debe6624
JS
448}
449
d6c9c1b7
VZ
450// ----------------------------------------------------------------------------
451// wxMultiChoiceDialog
452// ----------------------------------------------------------------------------
453
d6c9c1b7
VZ
454IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
455
456bool wxMultiChoiceDialog::Create( wxWindow *parent,
457 const wxString& message,
458 const wxString& caption,
459 int n,
460 const wxString *choices,
461 long style,
462 const wxPoint& pos )
463{
464 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
465 n, choices,
466 style, pos,
3d49ce44 467 wxLB_ALWAYS_SB | wxLB_EXTENDED) )
ca65c044 468 return false;
d6c9c1b7 469
ca65c044 470 return true;
d6c9c1b7
VZ
471}
472
584ad2a3
MB
473bool wxMultiChoiceDialog::Create( wxWindow *parent,
474 const wxString& message,
475 const wxString& caption,
476 const wxArrayString& choices,
477 long style,
478 const wxPoint& pos )
479{
480 wxCArrayString chs(choices);
481 return Create( parent, message, caption, chs.GetCount(),
482 chs.GetStrings(), style, pos );
483}
484
d6c9c1b7
VZ
485void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
486{
d0cc483d
VZ
487 // first clear all currently selected items
488 size_t n,
489 count = m_listbox->GetCount();
490 for ( n = 0; n < count; ++n )
491 {
492 m_listbox->Deselect(n);
493 }
494
495 // now select the ones which should be selected
496 count = selections.GetCount();
497 for ( n = 0; n < count; n++ )
d6c9c1b7
VZ
498 {
499 m_listbox->Select(selections[n]);
500 }
501}
502
3d49ce44 503bool wxMultiChoiceDialog::TransferDataFromWindow()
d6c9c1b7
VZ
504{
505 m_selections.Empty();
506 size_t count = m_listbox->GetCount();
507 for ( size_t n = 0; n < count; n++ )
508 {
509 if ( m_listbox->IsSelected(n) )
510 m_selections.Add(n);
511 }
512
ca65c044 513 return true;
d6c9c1b7 514}
1e6feb95 515
60104cba
WS
516#if wxUSE_CHECKLISTBOX
517
518wxListBoxBase *wxMultiChoiceDialog::CreateList(int n, const wxString *choices, long styleLbox)
519{
520 return new wxCheckListBox( this, wxID_LISTBOX,
521 wxDefaultPosition, wxDefaultSize,
522 n, choices,
523 styleLbox );
524}
525
526#endif // wxUSE_CHECKLISTBOX
527
1e6feb95 528#endif // wxUSE_CHOICEDLG