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