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