]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicdgg.cpp
A little clarification
[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$
d6c9c1b7 8// Copyright: (c) wxWindows team
6aa89a22 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
d6c9c1b7
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
14f355c2 16#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
d427503c 17 #pragma implementation "choicdgg.h"
c801d85f
KB
18#endif
19
d6c9c1b7
VZ
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
c801d85f
KB
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
d427503c 28 #pragma hdrstop
c801d85f
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_CHOICEDLG
32
c801d85f 33#ifndef WX_PRECOMP
257bf510
VZ
34 #include <stdio.h>
35 #include "wx/utils.h"
36 #include "wx/dialog.h"
37 #include "wx/button.h"
38 #include "wx/listbox.h"
39 #include "wx/stattext.h"
40 #include "wx/intl.h"
92afa2b1 41 #include "wx/sizer.h"
2da2f941 42 #include "wx/arrstr.h"
dcf924a3
RR
43#endif
44
45#if wxUSE_STATLINE
c50f1fb9 46 #include "wx/statline.h"
c801d85f
KB
47#endif
48
49#include "wx/generic/choicdgg.h"
50
d6c9c1b7
VZ
51// ----------------------------------------------------------------------------
52// constants
53// ----------------------------------------------------------------------------
54
257bf510 55#define wxID_LISTBOX 3000
dcf924a3 56
d6c9c1b7
VZ
57// ----------------------------------------------------------------------------
58// private functions
59// ----------------------------------------------------------------------------
60
61// convert wxArrayString into a wxString[] which must be delete[]d by caller
62static int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices);
63
64// ============================================================================
65// implementation
66// ============================================================================
67
68// ----------------------------------------------------------------------------
69// helpers
70// ----------------------------------------------------------------------------
71
72int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices)
73{
74 int n = aChoices.GetCount();
75 *choices = new wxString[n];
54b84891 76
d6c9c1b7
VZ
77 for ( int i = 0; i < n; i++ )
78 {
ea660175 79 (*choices)[i] = aChoices[i];
d6c9c1b7
VZ
80 }
81
82 return n;
83}
84
85// ----------------------------------------------------------------------------
86// wrapper functions
87// ----------------------------------------------------------------------------
88
89wxString wxGetSingleChoice( const wxString& message,
90 const wxString& caption,
91 int n, const wxString *choices,
92 wxWindow *parent,
93 int WXUNUSED(x), int WXUNUSED(y),
94 bool WXUNUSED(centre),
257bf510 95 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 96{
d427503c 97 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
3ca6a5f0 98 wxString choice;
d427503c 99 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0
BP
100 choice = dialog.GetStringSelection();
101
102 return choice;
c801d85f
KB
103}
104
d6c9c1b7
VZ
105wxString wxGetSingleChoice( const wxString& message,
106 const wxString& caption,
107 const wxArrayString& aChoices,
108 wxWindow *parent,
109 int x, int y,
110 bool centre,
111 int width, int height)
112{
113 wxString *choices;
114 int n = ConvertWXArrayToC(aChoices, &choices);
115 wxString res = wxGetSingleChoice(message, caption, n, choices, parent,
116 x, y, centre, width, height);
117 delete [] choices;
118
119 return res;
120}
121
d6c9c1b7
VZ
122int wxGetSingleChoiceIndex( const wxString& message,
123 const wxString& caption,
124 int n, const wxString *choices,
125 wxWindow *parent,
126 int WXUNUSED(x), int WXUNUSED(y),
127 bool WXUNUSED(centre),
128 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 129{
d427503c 130 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
3ca6a5f0 131 int choice;
d427503c 132 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0 133 choice = dialog.GetSelection();
d427503c 134 else
3ca6a5f0
BP
135 choice = -1;
136
137 return choice;
c801d85f
KB
138}
139
2adaf596
VS
140int wxGetSingleChoiceIndex( const wxString& message,
141 const wxString& caption,
142 const wxArrayString& aChoices,
143 wxWindow *parent,
144 int x, int y,
145 bool centre,
146 int width, int height)
147{
148 wxString *choices;
149 int n = ConvertWXArrayToC(aChoices, &choices);
150 int res = wxGetSingleChoiceIndex(message, caption, n, choices, parent,
151 x, y, centre, width, height);
152 delete [] choices;
153
154 return res;
155}
156
d6c9c1b7
VZ
157void *wxGetSingleChoiceData( const wxString& message,
158 const wxString& caption,
159 int n, const wxString *choices,
160 void **client_data,
161 wxWindow *parent,
162 int WXUNUSED(x), int WXUNUSED(y),
163 bool WXUNUSED(centre),
164 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 165{
b41ec29a
VZ
166 wxSingleChoiceDialog dialog(parent, message, caption, n, choices,
167 (char **)client_data);
3ca6a5f0 168 void *data;
d427503c 169 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0 170 data = dialog.GetSelectionClientData();
d427503c 171 else
3ca6a5f0
BP
172 data = NULL;
173
174 return data;
c801d85f
KB
175}
176
b41ec29a
VZ
177void *wxGetSingleChoiceData( const wxString& message,
178 const wxString& caption,
179 const wxArrayString& aChoices,
180 void **client_data,
181 wxWindow *parent,
182 int x, int y,
183 bool centre,
184 int width, int height)
185{
186 wxString *choices;
187 int n = ConvertWXArrayToC(aChoices, &choices);
188 void *res = wxGetSingleChoiceData(message, caption, n, choices,
189 client_data, parent,
190 x, y, centre, width, height);
191 delete [] choices;
192
193 return res;
194}
195
d6c9c1b7
VZ
196size_t wxGetMultipleChoices(wxArrayInt& selections,
197 const wxString& message,
198 const wxString& caption,
199 int n, const wxString *choices,
200 wxWindow *parent,
201 int WXUNUSED(x), int WXUNUSED(y),
202 bool WXUNUSED(centre),
203 int WXUNUSED(width), int WXUNUSED(height))
204{
205 wxMultiChoiceDialog dialog(parent, message, caption, n, choices);
e93bfe3c
VZ
206
207 if ( !selections.IsEmpty() )
208 dialog.SetSelections(selections);
209
d6c9c1b7
VZ
210 if ( dialog.ShowModal() == wxID_OK )
211 selections = dialog.GetSelections();
212 else
213 selections.Empty();
c801d85f 214
d6c9c1b7
VZ
215 return selections.GetCount();
216}
c801d85f 217
59bd9598 218size_t wxGetMultipleChoices(wxArrayInt& selections,
d6c9c1b7
VZ
219 const wxString& message,
220 const wxString& caption,
221 const wxArrayString& aChoices,
222 wxWindow *parent,
223 int x, int y,
224 bool centre,
225 int width, int height)
c801d85f 226{
d6c9c1b7
VZ
227 wxString *choices;
228 int n = ConvertWXArrayToC(aChoices, &choices);
229 size_t res = wxGetMultipleChoices(selections, message, caption,
230 n, choices, parent,
231 x, y, centre, width, height);
232 delete [] choices;
233
234 return res;
c801d85f 235}
c801d85f 236
d6c9c1b7
VZ
237// ----------------------------------------------------------------------------
238// wxAnyChoiceDialog
239// ----------------------------------------------------------------------------
240
1169a919
JS
241wxAnyChoiceDialog::wxAnyChoiceDialog()
242{
243}
244
245wxAnyChoiceDialog::wxAnyChoiceDialog(wxWindow *parent,
246 const wxString& message,
247 const wxString& caption,
248 int n,
249 const wxString *choices,
250 long styleDlg,
251 const wxPoint& pos,
252 long styleLbox)
253{
254 (void)Create(parent, message, caption, n, choices, styleDlg, pos, styleLbox);
255}
256
d6c9c1b7
VZ
257bool wxAnyChoiceDialog::Create(wxWindow *parent,
258 const wxString& message,
259 const wxString& caption,
260 int n, const wxString *choices,
ea660175 261 long styleDlg,
d6c9c1b7
VZ
262 const wxPoint& pos,
263 long styleLbox)
264{
13a6fb3a 265 if ( !wxDialog::Create(parent, -1, caption, pos, wxDefaultSize, styleDlg) )
d6c9c1b7
VZ
266 return FALSE;
267
268 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
269
270 // 1) text message
271 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
272
273 // 2) list box
274 m_listbox = new wxListBox( this, wxID_LISTBOX,
275 wxDefaultPosition, wxDefaultSize,
276 n, choices,
277 styleLbox );
278 if ( n > 0 )
279 m_listbox->SetSelection(0);
280
281 topsizer->Add( m_listbox, 1, wxEXPAND | wxLEFT|wxRIGHT, 15 );
282
283#if wxUSE_STATLINE
284 // 3) static line
285 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
286#endif
287
288 // 4) buttons
ea660175 289 topsizer->Add( CreateButtonSizer( styleDlg & (wxOK|wxCANCEL) ), 0, wxCENTRE | wxALL, 10 );
d6c9c1b7
VZ
290
291 SetAutoLayout( TRUE );
292 SetSizer( topsizer );
293
294 topsizer->SetSizeHints( this );
295 topsizer->Fit( this );
296
297 Centre( wxBOTH );
298
299 m_listbox->SetFocus();
300
301 return TRUE;
302}
303
304// ----------------------------------------------------------------------------
c801d85f 305// wxSingleChoiceDialog
d6c9c1b7 306// ----------------------------------------------------------------------------
c801d85f 307
c801d85f 308BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
d427503c
VZ
309 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
310 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
c801d85f
KB
311END_EVENT_TABLE()
312
d6c9c1b7 313IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
257bf510 314
1169a919
JS
315wxSingleChoiceDialog::wxSingleChoiceDialog()
316{
317 m_selection = -1;
318}
319
257bf510
VZ
320wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
321 const wxString& message,
322 const wxString& caption,
c50f1fb9 323 int n,
257bf510
VZ
324 const wxString *choices,
325 char **clientData,
326 long style,
59bd9598 327 const wxPoint& WXUNUSED(pos))
c801d85f 328{
257bf510 329 Create(parent, message, caption, n, choices, clientData, style);
c801d85f
KB
330}
331
d6c9c1b7 332bool wxSingleChoiceDialog::Create( wxWindow *parent,
c50f1fb9 333 const wxString& message,
d6c9c1b7 334 const wxString& caption,
c50f1fb9 335 int n,
257bf510
VZ
336 const wxString *choices,
337 char **clientData,
338 long style,
d6c9c1b7 339 const wxPoint& pos )
c801d85f 340{
d6c9c1b7
VZ
341 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
342 n, choices,
343 style, pos) )
344 return FALSE;
92afa2b1 345
d6c9c1b7 346 m_selection = n > 0 ? 0 : -1;
92afa2b1 347
92afa2b1 348 if (clientData)
d427503c 349 {
257bf510
VZ
350 for (int i = 0; i < n; i++)
351 m_listbox->SetClientData(i, clientData[i]);
d427503c 352 }
c801d85f 353
d427503c 354 return TRUE;
c801d85f
KB
355}
356
ef77f91e
JS
357// Set the selection
358void wxSingleChoiceDialog::SetSelection(int sel)
359{
257bf510 360 m_listbox->SetSelection(sel);
ef77f91e
JS
361 m_selection = sel;
362}
363
c801d85f
KB
364void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
365{
257bf510
VZ
366 m_selection = m_listbox->GetSelection();
367 m_stringSelection = m_listbox->GetStringSelection();
eb553cb2 368 if ( m_listbox->HasClientUntypedData() )
59af5f19 369 SetClientData(m_listbox->GetClientData(m_selection));
d427503c 370 EndModal(wxID_OK);
c801d85f
KB
371}
372
debe6624
JS
373void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
374{
257bf510
VZ
375 m_selection = m_listbox->GetSelection();
376 m_stringSelection = m_listbox->GetStringSelection();
25f47127 377
eb553cb2 378 if ( m_listbox->HasClientUntypedData() )
59af5f19 379 SetClientData(m_listbox->GetClientData(m_selection));
d427503c
VZ
380
381 EndModal(wxID_OK);
debe6624
JS
382}
383
d6c9c1b7
VZ
384// ----------------------------------------------------------------------------
385// wxMultiChoiceDialog
386// ----------------------------------------------------------------------------
387
d6c9c1b7
VZ
388IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
389
1169a919
JS
390wxMultiChoiceDialog::wxMultiChoiceDialog()
391{
392}
393
394wxMultiChoiceDialog::wxMultiChoiceDialog(wxWindow *parent,
395 const wxString& message,
396 const wxString& caption,
397 int n,
398 const wxString *choices,
399 long style,
400 const wxPoint& pos)
401{
402 (void)Create(parent, message, caption, n, choices, style, pos);
403}
404
d6c9c1b7
VZ
405bool wxMultiChoiceDialog::Create( wxWindow *parent,
406 const wxString& message,
407 const wxString& caption,
408 int n,
409 const wxString *choices,
410 long style,
411 const wxPoint& pos )
412{
413 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
414 n, choices,
415 style, pos,
3d49ce44 416 wxLB_ALWAYS_SB | wxLB_EXTENDED) )
d6c9c1b7
VZ
417 return FALSE;
418
419 return TRUE;
420}
421
422void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
423{
424 size_t count = selections.GetCount();
425 for ( size_t n = 0; n < count; n++ )
426 {
427 m_listbox->Select(selections[n]);
428 }
429}
430
3d49ce44 431bool wxMultiChoiceDialog::TransferDataFromWindow()
d6c9c1b7
VZ
432{
433 m_selections.Empty();
434 size_t count = m_listbox->GetCount();
435 for ( size_t n = 0; n < count; n++ )
436 {
437 if ( m_listbox->IsSelected(n) )
438 m_selections.Add(n);
439 }
440
3d49ce44 441 return TRUE;
d6c9c1b7 442}
1e6feb95
VZ
443
444#endif // wxUSE_CHOICEDLG