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