]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicdgg.cpp
Added wxInfoBar::AddButton().
[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
e5cfb314 191int wxGetSelectedChoices(wxArrayInt& selections,
d6c9c1b7
VZ
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
e5cfb314
VZ
206 if ( dialog.ShowModal() != wxID_OK )
207 {
208 // NB: intentionally do not clear the selections array here, the caller
209 // might want to preserve its original contents if the dialog was
210 // cancelled
211 return -1;
212 }
c801d85f 213
e5cfb314 214 selections = dialog.GetSelections();
d6c9c1b7
VZ
215 return selections.GetCount();
216}
c801d85f 217
e5cfb314 218int wxGetSelectedChoices(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);
e5cfb314 229 int res = wxGetSelectedChoices(selections, message, caption,
d6c9c1b7
VZ
230 n, choices, parent,
231 x, y, centre, width, height);
232 delete [] choices;
233
234 return res;
c801d85f 235}
c801d85f 236
e5cfb314
VZ
237#if WXWIN_COMPATIBILITY_2_8
238size_t wxGetMultipleChoices(wxArrayInt& selections,
239 const wxString& message,
240 const wxString& caption,
241 int n, const wxString *choices,
242 wxWindow *parent,
243 int x, int y,
244 bool centre,
245 int width, int height)
246{
247 int rc = wxGetSelectedChoices(selections, message, caption,
248 n, choices,
249 parent, x, y, centre, width, height);
250 if ( rc == -1 )
251 {
252 selections.clear();
253 return 0;
254 }
255
256 return rc;
257}
258
259size_t wxGetMultipleChoices(wxArrayInt& selections,
260 const wxString& message,
261 const wxString& caption,
262 const wxArrayString& aChoices,
263 wxWindow *parent,
264 int x, int y,
265 bool centre,
266 int width, int height)
267{
268 int rc = wxGetSelectedChoices(selections, message, caption,
269 aChoices,
270 parent, x, y, centre, width, height);
271 if ( rc == -1 )
272 {
273 selections.clear();
274 return 0;
275 }
276
277 return rc;
278}
279#endif // WXWIN_COMPATIBILITY_2_8
280
d6c9c1b7
VZ
281// ----------------------------------------------------------------------------
282// wxAnyChoiceDialog
283// ----------------------------------------------------------------------------
284
285bool wxAnyChoiceDialog::Create(wxWindow *parent,
286 const wxString& message,
287 const wxString& caption,
288 int n, const wxString *choices,
ea660175 289 long styleDlg,
d6c9c1b7
VZ
290 const wxPoint& pos,
291 long styleLbox)
292{
12a124dd
VZ
293 // extract the buttons styles from the dialog one and remove them from it
294 const long styleBtns = styleDlg & (wxOK | wxCANCEL);
295 styleDlg &= ~styleBtns;
296
ca65c044
WS
297 if ( !wxDialog::Create(parent, wxID_ANY, caption, pos, wxDefaultSize, styleDlg) )
298 return false;
d6c9c1b7
VZ
299
300 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
301
64c794f6 302 // 1) text message
bd9f3519
VZ
303 topsizer->
304 Add(CreateTextSizer(message), wxSizerFlags().Expand().TripleBorder());
305
64c794f6 306 // 2) list box
bd9f3519 307 m_listbox = CreateList(n, choices, styleLbox);
60104cba 308
64c794f6
WS
309 if ( n > 0 )
310 m_listbox->SetSelection(0);
311
bd9f3519 312 topsizer->
6fe7685d 313 Add(m_listbox, wxSizerFlags().Expand().Proportion(1).TripleBorder(wxLEFT | wxRIGHT));
119727ad 314
897b24cf 315 // 3) buttons if any
bd9f3519 316 wxSizer *
12a124dd 317 buttonSizer = CreateSeparatedButtonSizer(styleBtns);
bd9f3519 318 if ( buttonSizer )
897b24cf 319 {
bd9f3519 320 topsizer->Add(buttonSizer, wxSizerFlags().Expand().DoubleBorder());
897b24cf 321 }
64c794f6 322
d6c9c1b7
VZ
323 SetSizer( topsizer );
324
325 topsizer->SetSizeHints( this );
326 topsizer->Fit( this );
327
8316ff5d
VS
328 if ( styleDlg & wxCENTRE )
329 Centre(wxBOTH);
d6c9c1b7
VZ
330
331 m_listbox->SetFocus();
332
ca65c044 333 return true;
d6c9c1b7
VZ
334}
335
584ad2a3
MB
336bool wxAnyChoiceDialog::Create(wxWindow *parent,
337 const wxString& message,
338 const wxString& caption,
339 const wxArrayString& choices,
340 long styleDlg,
341 const wxPoint& pos,
342 long styleLbox)
343{
344 wxCArrayString chs(choices);
345 return Create(parent, message, caption, chs.GetCount(), chs.GetStrings(),
346 styleDlg, pos, styleLbox);
347}
348
60104cba
WS
349wxListBoxBase *wxAnyChoiceDialog::CreateList(int n, const wxString *choices, long styleLbox)
350{
351 return new wxListBox( this, wxID_LISTBOX,
61a11cd6 352 wxDefaultPosition, wxDefaultSize,
60104cba
WS
353 n, choices,
354 styleLbox );
355}
356
d6c9c1b7 357// ----------------------------------------------------------------------------
c801d85f 358// wxSingleChoiceDialog
d6c9c1b7 359// ----------------------------------------------------------------------------
c801d85f 360
c801d85f 361BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
d427503c 362 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
7b504551 363#ifndef __SMARTPHONE__
d427503c 364 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
7b504551
WS
365#endif
366#ifdef __WXWINCE__
367 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown)
368#endif
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
584ad2a3
MB
385wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
386 const wxString& message,
387 const wxString& caption,
388 const wxArrayString& choices,
389 char **clientData,
390 long style,
391 const wxPoint& WXUNUSED(pos))
392{
393 Create(parent, message, caption, choices, clientData, style);
394}
395
d6c9c1b7 396bool wxSingleChoiceDialog::Create( wxWindow *parent,
c50f1fb9 397 const wxString& message,
d6c9c1b7 398 const wxString& caption,
c50f1fb9 399 int n,
257bf510
VZ
400 const wxString *choices,
401 char **clientData,
402 long style,
d6c9c1b7 403 const wxPoint& pos )
c801d85f 404{
d6c9c1b7
VZ
405 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
406 n, choices,
407 style, pos) )
ca65c044 408 return false;
92afa2b1 409
d6c9c1b7 410 m_selection = n > 0 ? 0 : -1;
92afa2b1 411
92afa2b1 412 if (clientData)
d427503c 413 {
257bf510
VZ
414 for (int i = 0; i < n; i++)
415 m_listbox->SetClientData(i, clientData[i]);
d427503c 416 }
c801d85f 417
ca65c044 418 return true;
c801d85f
KB
419}
420
584ad2a3
MB
421bool wxSingleChoiceDialog::Create( wxWindow *parent,
422 const wxString& message,
423 const wxString& caption,
424 const wxArrayString& choices,
425 char **clientData,
426 long style,
427 const wxPoint& pos )
428{
429 wxCArrayString chs(choices);
430 return Create( parent, message, caption, chs.GetCount(), chs.GetStrings(),
431 clientData, style, pos );
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{
7b504551 443 DoChoice();
c801d85f
KB
444}
445
7b504551 446#ifndef __SMARTPHONE__
debe6624 447void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
7b504551
WS
448{
449 DoChoice();
450}
451#endif
452
453#ifdef __WXWINCE__
454void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent& WXUNUSED(event))
455{
456 DoChoice();
457}
458#endif
459
460void wxSingleChoiceDialog::DoChoice()
debe6624 461{
257bf510
VZ
462 m_selection = m_listbox->GetSelection();
463 m_stringSelection = m_listbox->GetStringSelection();
25f47127 464
eb553cb2 465 if ( m_listbox->HasClientUntypedData() )
59af5f19 466 SetClientData(m_listbox->GetClientData(m_selection));
d427503c
VZ
467
468 EndModal(wxID_OK);
debe6624
JS
469}
470
d6c9c1b7
VZ
471// ----------------------------------------------------------------------------
472// wxMultiChoiceDialog
473// ----------------------------------------------------------------------------
474
d6c9c1b7
VZ
475IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
476
477bool wxMultiChoiceDialog::Create( wxWindow *parent,
478 const wxString& message,
479 const wxString& caption,
480 int n,
481 const wxString *choices,
482 long style,
483 const wxPoint& pos )
484{
cb5d54ff
RD
485 long styleLbox;
486#if wxUSE_CHECKLISTBOX
487 styleLbox = wxLB_ALWAYS_SB;
488#else
489 styleLbox = wxLB_ALWAYS_SB | wxLB_EXTENDED;
490#endif
6fe7685d 491
d6c9c1b7
VZ
492 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
493 n, choices,
494 style, pos,
cb5d54ff 495 styleLbox) )
ca65c044 496 return false;
d6c9c1b7 497
ca65c044 498 return true;
d6c9c1b7
VZ
499}
500
584ad2a3
MB
501bool wxMultiChoiceDialog::Create( wxWindow *parent,
502 const wxString& message,
503 const wxString& caption,
504 const wxArrayString& choices,
505 long style,
506 const wxPoint& pos )
507{
508 wxCArrayString chs(choices);
509 return Create( parent, message, caption, chs.GetCount(),
510 chs.GetStrings(), style, pos );
511}
512
d6c9c1b7
VZ
513void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
514{
abc2857f
JS
515#if wxUSE_CHECKLISTBOX
516 wxCheckListBox* checkListBox = wxDynamicCast(m_listbox, wxCheckListBox);
517 if (checkListBox)
518 {
519 // first clear all currently selected items
520 size_t n,
521 count = checkListBox->GetCount();
522 for ( n = 0; n < count; ++n )
523 {
524 if (checkListBox->IsChecked(n))
525 checkListBox->Check(n, false);
526 }
527
528 // now select the ones which should be selected
529 count = selections.GetCount();
530 for ( n = 0; n < count; n++ )
531 {
532 checkListBox->Check(selections[n]);
533 }
6fe7685d 534
abc2857f
JS
535 return;
536 }
537#endif
6fe7685d 538
d0cc483d
VZ
539 // first clear all currently selected items
540 size_t n,
541 count = m_listbox->GetCount();
542 for ( n = 0; n < count; ++n )
543 {
544 m_listbox->Deselect(n);
545 }
546
547 // now select the ones which should be selected
548 count = selections.GetCount();
549 for ( n = 0; n < count; n++ )
d6c9c1b7
VZ
550 {
551 m_listbox->Select(selections[n]);
552 }
553}
554
3d49ce44 555bool wxMultiChoiceDialog::TransferDataFromWindow()
d6c9c1b7
VZ
556{
557 m_selections.Empty();
abc2857f
JS
558
559#if wxUSE_CHECKLISTBOX
560 wxCheckListBox* checkListBox = wxDynamicCast(m_listbox, wxCheckListBox);
561 if (checkListBox)
562 {
563 size_t count = checkListBox->GetCount();
564 for ( size_t n = 0; n < count; n++ )
565 {
566 if ( checkListBox->IsChecked(n) )
567 m_selections.Add(n);
568 }
569 return true;
570 }
571#endif
572
d6c9c1b7
VZ
573 size_t count = m_listbox->GetCount();
574 for ( size_t n = 0; n < count; n++ )
575 {
576 if ( m_listbox->IsSelected(n) )
577 m_selections.Add(n);
578 }
579
ca65c044 580 return true;
d6c9c1b7 581}
1e6feb95 582
60104cba
WS
583#if wxUSE_CHECKLISTBOX
584
585wxListBoxBase *wxMultiChoiceDialog::CreateList(int n, const wxString *choices, long styleLbox)
586{
587 return new wxCheckListBox( this, wxID_LISTBOX,
61a11cd6 588 wxDefaultPosition, wxDefaultSize,
60104cba
WS
589 n, choices,
590 styleLbox );
591}
592
593#endif // wxUSE_CHECKLISTBOX
594
1e6feb95 595#endif // wxUSE_CHOICEDLG