]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicdgg.cpp
oops, typo leading to compilation problem fixed
[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
d427503c 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
d6c9c1b7
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
c801d85f 16#ifdef __GNUG__
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
31#ifndef WX_PRECOMP
257bf510
VZ
32 #include <stdio.h>
33 #include "wx/utils.h"
34 #include "wx/dialog.h"
35 #include "wx/button.h"
36 #include "wx/listbox.h"
37 #include "wx/stattext.h"
38 #include "wx/intl.h"
92afa2b1 39 #include "wx/sizer.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
d6c9c1b7
VZ
54#if defined(__WXMSW__) || defined(__WXMAC__)
55#define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
56 wxDIALOG_MODAL | \
57 wxTAB_TRAVERSAL)
58#else
59#define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
60 wxDIALOG_MODAL | \
61 wxRESIZE_BORDER | \
62 wxTAB_TRAVERSAL)
63#endif
64
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 {
54b84891 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
130#ifdef WXWIN_COMPATIBILITY_2
c801d85f 131// Overloaded for backward compatibility
d6c9c1b7
VZ
132wxString wxGetSingleChoice( const wxString& message,
133 const wxString& caption,
134 int n, char *choices[],
135 wxWindow *parent,
c50f1fb9 136 int x, int y, bool centre,
257bf510 137 int width, int height )
c801d85f 138{
d427503c
VZ
139 wxString *strings = new wxString[n];
140 int i;
141 for ( i = 0; i < n; i++)
142 {
143 strings[i] = choices[i];
144 }
145 wxString ans(wxGetSingleChoice(message, caption, n, (const wxString *)strings, parent,
146 x, y, centre, width, height));
147 delete[] strings;
148 return ans;
c801d85f 149}
d6c9c1b7
VZ
150#endif // WXWIN_COMPATIBILITY_2
151
152int wxGetSingleChoiceIndex( const wxString& message,
153 const wxString& caption,
154 int n, const wxString *choices,
155 wxWindow *parent,
156 int WXUNUSED(x), int WXUNUSED(y),
157 bool WXUNUSED(centre),
158 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 159{
d427503c 160 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
3ca6a5f0 161 int choice;
d427503c 162 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0 163 choice = dialog.GetSelection();
d427503c 164 else
3ca6a5f0
BP
165 choice = -1;
166
167 return choice;
c801d85f
KB
168}
169
2adaf596
VS
170int wxGetSingleChoiceIndex( const wxString& message,
171 const wxString& caption,
172 const wxArrayString& aChoices,
173 wxWindow *parent,
174 int x, int y,
175 bool centre,
176 int width, int height)
177{
178 wxString *choices;
179 int n = ConvertWXArrayToC(aChoices, &choices);
180 int res = wxGetSingleChoiceIndex(message, caption, n, choices, parent,
181 x, y, centre, width, height);
182 delete [] choices;
183
184 return res;
185}
186
d6c9c1b7 187#ifdef WXWIN_COMPATIBILITY_2
c801d85f 188// Overloaded for backward compatibility
d6c9c1b7
VZ
189int wxGetSingleChoiceIndex( const wxString& message,
190 const wxString& caption,
191 int n, wxChar *choices[],
192 wxWindow *parent,
193 int x, int y, bool centre,
194 int width, int height )
c801d85f 195{
d427503c 196 wxString *strings = new wxString[n];
dcf924a3 197 for ( int i = 0; i < n; i++)
d427503c 198 strings[i] = choices[i];
d427503c
VZ
199 int ans = wxGetSingleChoiceIndex(message, caption, n, (const wxString *)strings, parent,
200 x, y, centre, width, height);
201 delete[] strings;
202 return ans;
c801d85f 203}
d6c9c1b7
VZ
204#endif // WXWIN_COMPATIBILITY_2
205
206void *wxGetSingleChoiceData( const wxString& message,
207 const wxString& caption,
208 int n, const wxString *choices,
209 void **client_data,
210 wxWindow *parent,
211 int WXUNUSED(x), int WXUNUSED(y),
212 bool WXUNUSED(centre),
213 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 214{
b41ec29a
VZ
215 wxSingleChoiceDialog dialog(parent, message, caption, n, choices,
216 (char **)client_data);
3ca6a5f0 217 void *data;
d427503c 218 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0 219 data = dialog.GetSelectionClientData();
d427503c 220 else
3ca6a5f0
BP
221 data = NULL;
222
223 return data;
c801d85f
KB
224}
225
b41ec29a
VZ
226void *wxGetSingleChoiceData( const wxString& message,
227 const wxString& caption,
228 const wxArrayString& aChoices,
229 void **client_data,
230 wxWindow *parent,
231 int x, int y,
232 bool centre,
233 int width, int height)
234{
235 wxString *choices;
236 int n = ConvertWXArrayToC(aChoices, &choices);
237 void *res = wxGetSingleChoiceData(message, caption, n, choices,
238 client_data, parent,
239 x, y, centre, width, height);
240 delete [] choices;
241
242 return res;
243}
244
d6c9c1b7 245#ifdef WXWIN_COMPATIBILITY_2
c801d85f 246// Overloaded for backward compatibility
d6c9c1b7
VZ
247void *wxGetSingleChoiceData( const wxString& message,
248 const wxString& caption,
249 int n, wxChar *choices[],
250 void **client_data,
251 wxWindow *parent,
252 int x, int y, bool centre, int width, int height )
c801d85f 253{
d427503c
VZ
254 wxString *strings = new wxString[n];
255 int i;
256 for ( i = 0; i < n; i++)
257 {
258 strings[i] = choices[i];
259 }
d6c9c1b7
VZ
260 void *data = wxGetSingleChoiceData(message, caption,
261 n, (const wxString *)strings,
262 client_data, parent,
263 x, y, centre, width, height);
d427503c
VZ
264 delete[] strings;
265 return data;
c801d85f 266}
d6c9c1b7
VZ
267#endif // WXWIN_COMPATIBILITY_2
268
269size_t wxGetMultipleChoices(wxArrayInt& selections,
270 const wxString& message,
271 const wxString& caption,
272 int n, const wxString *choices,
273 wxWindow *parent,
274 int WXUNUSED(x), int WXUNUSED(y),
275 bool WXUNUSED(centre),
276 int WXUNUSED(width), int WXUNUSED(height))
277{
278 wxMultiChoiceDialog dialog(parent, message, caption, n, choices);
e93bfe3c
VZ
279
280 if ( !selections.IsEmpty() )
281 dialog.SetSelections(selections);
282
d6c9c1b7
VZ
283 if ( dialog.ShowModal() == wxID_OK )
284 selections = dialog.GetSelections();
285 else
286 selections.Empty();
c801d85f 287
d6c9c1b7
VZ
288 return selections.GetCount();
289}
c801d85f 290
59bd9598 291size_t wxGetMultipleChoices(wxArrayInt& selections,
d6c9c1b7
VZ
292 const wxString& message,
293 const wxString& caption,
294 const wxArrayString& aChoices,
295 wxWindow *parent,
296 int x, int y,
297 bool centre,
298 int width, int height)
c801d85f 299{
d6c9c1b7
VZ
300 wxString *choices;
301 int n = ConvertWXArrayToC(aChoices, &choices);
302 size_t res = wxGetMultipleChoices(selections, message, caption,
303 n, choices, parent,
304 x, y, centre, width, height);
305 delete [] choices;
306
307 return res;
c801d85f 308}
c801d85f 309
d6c9c1b7
VZ
310// ----------------------------------------------------------------------------
311// wxAnyChoiceDialog
312// ----------------------------------------------------------------------------
313
314bool wxAnyChoiceDialog::Create(wxWindow *parent,
315 const wxString& message,
316 const wxString& caption,
317 int n, const wxString *choices,
59bd9598 318 long WXUNUSED(styleDlg), // FIXME: why unused?
d6c9c1b7
VZ
319 const wxPoint& pos,
320 long styleLbox)
321{
322 if ( !wxDialog::Create(parent, -1, caption, pos, wxDefaultSize,
323 wxCHOICEDLG_DIALOG_STYLE) )
324 return FALSE;
325
326 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
327
328 // 1) text message
329 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
330
331 // 2) list box
332 m_listbox = new wxListBox( this, wxID_LISTBOX,
333 wxDefaultPosition, wxDefaultSize,
334 n, choices,
335 styleLbox );
336 if ( n > 0 )
337 m_listbox->SetSelection(0);
338
339 topsizer->Add( m_listbox, 1, wxEXPAND | wxLEFT|wxRIGHT, 15 );
340
341#if wxUSE_STATLINE
342 // 3) static line
343 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
344#endif
345
346 // 4) buttons
347 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxCENTRE | wxALL, 10 );
348
349 SetAutoLayout( TRUE );
350 SetSizer( topsizer );
351
352 topsizer->SetSizeHints( this );
353 topsizer->Fit( this );
354
355 Centre( wxBOTH );
356
357 m_listbox->SetFocus();
358
359 return TRUE;
360}
361
362// ----------------------------------------------------------------------------
c801d85f 363// wxSingleChoiceDialog
d6c9c1b7 364// ----------------------------------------------------------------------------
c801d85f 365
c801d85f 366BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
d427503c
VZ
367 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
368 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
c801d85f
KB
369END_EVENT_TABLE()
370
d6c9c1b7 371IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
257bf510
VZ
372
373wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
374 const wxString& message,
375 const wxString& caption,
c50f1fb9 376 int n,
257bf510
VZ
377 const wxString *choices,
378 char **clientData,
379 long style,
59bd9598 380 const wxPoint& WXUNUSED(pos))
c801d85f 381{
257bf510 382 Create(parent, message, caption, n, choices, clientData, style);
c801d85f
KB
383}
384
d6c9c1b7
VZ
385#ifdef WXWIN_COMPATIBILITY_2
386
257bf510
VZ
387wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
388 const wxString& message,
389 const wxString& caption,
c50f1fb9 390 const wxStringList& choices,
b91b2200 391 char **clientData,
c50f1fb9 392 long style,
59bd9598 393 const wxPoint& WXUNUSED(pos))
c801d85f 394{
257bf510 395 Create(parent, message, caption, choices, clientData, style);
c801d85f
KB
396}
397
257bf510
VZ
398bool wxSingleChoiceDialog::Create(wxWindow *parent,
399 const wxString& message,
400 const wxString& caption,
401 const wxStringList& choices,
402 char **clientData,
403 long style,
404 const wxPoint& pos)
c801d85f 405{
d427503c
VZ
406 wxString *strings = new wxString[choices.Number()];
407 int i;
408 for ( i = 0; i < choices.Number(); i++)
409 {
410 strings[i] = (char *)choices.Nth(i)->Data();
411 }
412 bool ans = Create(parent, message, caption, choices.Number(), strings, clientData, style, pos);
413 delete[] strings;
414 return ans;
c801d85f
KB
415}
416
d6c9c1b7
VZ
417#endif // WXWIN_COMPATIBILITY_2
418
419bool wxSingleChoiceDialog::Create( wxWindow *parent,
c50f1fb9 420 const wxString& message,
d6c9c1b7 421 const wxString& caption,
c50f1fb9 422 int n,
257bf510
VZ
423 const wxString *choices,
424 char **clientData,
425 long style,
d6c9c1b7 426 const wxPoint& pos )
c801d85f 427{
d6c9c1b7
VZ
428 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
429 n, choices,
430 style, pos) )
431 return FALSE;
92afa2b1 432
d6c9c1b7 433 m_selection = n > 0 ? 0 : -1;
92afa2b1 434
92afa2b1 435 if (clientData)
d427503c 436 {
257bf510
VZ
437 for (int i = 0; i < n; i++)
438 m_listbox->SetClientData(i, clientData[i]);
d427503c 439 }
c801d85f 440
d427503c 441 return TRUE;
c801d85f
KB
442}
443
ef77f91e
JS
444// Set the selection
445void wxSingleChoiceDialog::SetSelection(int sel)
446{
257bf510 447 m_listbox->SetSelection(sel);
ef77f91e
JS
448 m_selection = sel;
449}
450
c801d85f
KB
451void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
452{
257bf510
VZ
453 m_selection = m_listbox->GetSelection();
454 m_stringSelection = m_listbox->GetStringSelection();
25f47127
JS
455 // TODO!
456#ifndef __WXMOTIF__
eb553cb2
VZ
457 if ( m_listbox->HasClientUntypedData() )
458 SetClientData(m_listbox->GetClientData(m_selection));
25f47127 459#endif
d427503c 460 EndModal(wxID_OK);
c801d85f
KB
461}
462
debe6624
JS
463void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
464{
257bf510
VZ
465 m_selection = m_listbox->GetSelection();
466 m_stringSelection = m_listbox->GetStringSelection();
25f47127
JS
467
468 // TODO!
469#ifndef __WXMOTIF__
eb553cb2
VZ
470 if ( m_listbox->HasClientUntypedData() )
471 SetClientData(m_listbox->GetClientData(m_selection));
25f47127 472#endif
d427503c
VZ
473
474 EndModal(wxID_OK);
debe6624
JS
475}
476
d6c9c1b7
VZ
477// ----------------------------------------------------------------------------
478// wxMultiChoiceDialog
479// ----------------------------------------------------------------------------
480
d6c9c1b7
VZ
481IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
482
483bool wxMultiChoiceDialog::Create( wxWindow *parent,
484 const wxString& message,
485 const wxString& caption,
486 int n,
487 const wxString *choices,
488 long style,
489 const wxPoint& pos )
490{
491 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
492 n, choices,
493 style, pos,
3d49ce44 494 wxLB_ALWAYS_SB | wxLB_EXTENDED) )
d6c9c1b7
VZ
495 return FALSE;
496
497 return TRUE;
498}
499
500void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
501{
502 size_t count = selections.GetCount();
503 for ( size_t n = 0; n < count; n++ )
504 {
505 m_listbox->Select(selections[n]);
506 }
507}
508
3d49ce44 509bool wxMultiChoiceDialog::TransferDataFromWindow()
d6c9c1b7 510{
51a9ecbc
VZ
511 // VZ: I hate to do it but I can't fix wxMotif right now (FIXME)
512#ifdef __WXMOTIF__
513 #define IsSelected Selected
514#endif
515
d6c9c1b7
VZ
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
3d49ce44 524 return TRUE;
d6c9c1b7 525}