]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicdgg.cpp
Don't crash in wxGridCellAutoWrapStringRenderer when the column is hidden.
[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 6// Created: 04/01/98
77ffb593 7// Copyright: (c) wxWidgets team
65571936 8// Licence: wxWindows licence
c801d85f
KB
9/////////////////////////////////////////////////////////////////////////////
10
d6c9c1b7
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
d6c9c1b7
VZ
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
c801d85f
KB
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
d427503c 23 #pragma hdrstop
c801d85f
KB
24#endif
25
1e6feb95
VZ
26#if wxUSE_CHOICEDLG
27
c801d85f 28#ifndef WX_PRECOMP
257bf510
VZ
29 #include <stdio.h>
30 #include "wx/utils.h"
31 #include "wx/dialog.h"
32 #include "wx/button.h"
33 #include "wx/listbox.h"
63c02113 34 #include "wx/checklst.h"
257bf510
VZ
35 #include "wx/stattext.h"
36 #include "wx/intl.h"
92afa2b1 37 #include "wx/sizer.h"
2da2f941 38 #include "wx/arrstr.h"
dcf924a3
RR
39#endif
40
897b24cf 41#include "wx/statline.h"
6fe7685d 42#include "wx/settings.h"
c801d85f
KB
43#include "wx/generic/choicdgg.h"
44
d6c9c1b7
VZ
45// ----------------------------------------------------------------------------
46// constants
47// ----------------------------------------------------------------------------
48
257bf510 49#define wxID_LISTBOX 3000
dcf924a3 50
d6c9c1b7
VZ
51// ----------------------------------------------------------------------------
52// private functions
53// ----------------------------------------------------------------------------
54
55// convert wxArrayString into a wxString[] which must be delete[]d by caller
56static int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices);
57
58// ============================================================================
59// implementation
60// ============================================================================
61
62// ----------------------------------------------------------------------------
63// helpers
64// ----------------------------------------------------------------------------
65
66int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices)
67{
68 int n = aChoices.GetCount();
69 *choices = new wxString[n];
54b84891 70
d6c9c1b7
VZ
71 for ( int i = 0; i < n; i++ )
72 {
ea660175 73 (*choices)[i] = aChoices[i];
d6c9c1b7
VZ
74 }
75
76 return n;
77}
78
79// ----------------------------------------------------------------------------
80// wrapper functions
81// ----------------------------------------------------------------------------
82
83wxString wxGetSingleChoice( const wxString& message,
84 const wxString& caption,
85 int n, const wxString *choices,
86 wxWindow *parent,
87 int WXUNUSED(x), int WXUNUSED(y),
88 bool WXUNUSED(centre),
697f4a96
VZ
89 int WXUNUSED(width), int WXUNUSED(height),
90 int initialSelection)
c801d85f 91{
d427503c 92 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
697f4a96
VZ
93
94 dialog.SetSelection(initialSelection);
95
3ca6a5f0 96 wxString choice;
d427503c 97 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0
BP
98 choice = dialog.GetStringSelection();
99
100 return choice;
c801d85f
KB
101}
102
d6c9c1b7
VZ
103wxString wxGetSingleChoice( const wxString& message,
104 const wxString& caption,
105 const wxArrayString& aChoices,
106 wxWindow *parent,
107 int x, int y,
108 bool centre,
697f4a96
VZ
109 int width, int height,
110 int initialSelection)
d6c9c1b7
VZ
111{
112 wxString *choices;
113 int n = ConvertWXArrayToC(aChoices, &choices);
114 wxString res = wxGetSingleChoice(message, caption, n, choices, parent,
697f4a96
VZ
115 x, y, centre, width, height,
116 initialSelection);
d6c9c1b7
VZ
117 delete [] choices;
118
119 return res;
120}
121
697f4a96
VZ
122wxString wxGetSingleChoice( const wxString& message,
123 const wxString& caption,
124 const wxArrayString& choices,
125 int initialSelection,
126 wxWindow *parent)
127{
128 return wxGetSingleChoice(message, caption, choices, parent,
129 wxDefaultCoord, wxDefaultCoord,
130 true, wxCHOICE_WIDTH, wxCHOICE_HEIGHT,
131 initialSelection);
132}
133
134wxString wxGetSingleChoice( const wxString& message,
135 const wxString& caption,
136 int n, const wxString *choices,
137 int initialSelection,
138 wxWindow *parent)
139{
140 return wxGetSingleChoice(message, caption, n, choices, parent,
141 wxDefaultCoord, wxDefaultCoord,
142 true, wxCHOICE_WIDTH, wxCHOICE_HEIGHT,
143 initialSelection);
144}
145
d6c9c1b7
VZ
146int wxGetSingleChoiceIndex( const wxString& message,
147 const wxString& caption,
148 int n, const wxString *choices,
149 wxWindow *parent,
150 int WXUNUSED(x), int WXUNUSED(y),
151 bool WXUNUSED(centre),
697f4a96
VZ
152 int WXUNUSED(width), int WXUNUSED(height),
153 int initialSelection)
c801d85f 154{
d427503c 155 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
697f4a96
VZ
156
157 dialog.SetSelection(initialSelection);
158
3ca6a5f0 159 int choice;
d427503c 160 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0 161 choice = dialog.GetSelection();
d427503c 162 else
3ca6a5f0
BP
163 choice = -1;
164
165 return choice;
c801d85f
KB
166}
167
2adaf596
VS
168int wxGetSingleChoiceIndex( const wxString& message,
169 const wxString& caption,
170 const wxArrayString& aChoices,
171 wxWindow *parent,
172 int x, int y,
173 bool centre,
697f4a96
VZ
174 int width, int height,
175 int initialSelection)
2adaf596
VS
176{
177 wxString *choices;
178 int n = ConvertWXArrayToC(aChoices, &choices);
179 int res = wxGetSingleChoiceIndex(message, caption, n, choices, parent,
697f4a96
VZ
180 x, y, centre, width, height,
181 initialSelection);
2adaf596
VS
182 delete [] choices;
183
184 return res;
185}
186
697f4a96
VZ
187int wxGetSingleChoiceIndex( const wxString& message,
188 const wxString& caption,
189 const wxArrayString& choices,
190 int initialSelection,
191 wxWindow *parent)
192{
193 return wxGetSingleChoiceIndex(message, caption, choices, parent,
194 wxDefaultCoord, wxDefaultCoord,
195 true, wxCHOICE_WIDTH, wxCHOICE_HEIGHT,
196 initialSelection);
197}
198
199
200int wxGetSingleChoiceIndex( const wxString& message,
201 const wxString& caption,
202 int n, const wxString *choices,
203 int initialSelection,
204 wxWindow *parent)
205{
206 return wxGetSingleChoiceIndex(message, caption, n, choices, parent,
207 wxDefaultCoord, wxDefaultCoord,
208 true, wxCHOICE_WIDTH, wxCHOICE_HEIGHT,
209 initialSelection);
210}
211
212
d6c9c1b7
VZ
213void *wxGetSingleChoiceData( const wxString& message,
214 const wxString& caption,
215 int n, const wxString *choices,
216 void **client_data,
217 wxWindow *parent,
218 int WXUNUSED(x), int WXUNUSED(y),
219 bool WXUNUSED(centre),
697f4a96
VZ
220 int WXUNUSED(width), int WXUNUSED(height),
221 int initialSelection)
c801d85f 222{
b41ec29a 223 wxSingleChoiceDialog dialog(parent, message, caption, n, choices,
fc12b1f1 224 client_data);
697f4a96
VZ
225
226 dialog.SetSelection(initialSelection);
227
3ca6a5f0 228 void *data;
d427503c 229 if ( dialog.ShowModal() == wxID_OK )
fc12b1f1 230 data = dialog.GetSelectionData();
d427503c 231 else
3ca6a5f0
BP
232 data = NULL;
233
234 return data;
c801d85f
KB
235}
236
b41ec29a
VZ
237void *wxGetSingleChoiceData( const wxString& message,
238 const wxString& caption,
239 const wxArrayString& aChoices,
240 void **client_data,
241 wxWindow *parent,
242 int x, int y,
243 bool centre,
697f4a96
VZ
244 int width, int height,
245 int initialSelection)
b41ec29a
VZ
246{
247 wxString *choices;
248 int n = ConvertWXArrayToC(aChoices, &choices);
249 void *res = wxGetSingleChoiceData(message, caption, n, choices,
250 client_data, parent,
697f4a96
VZ
251 x, y, centre, width, height,
252 initialSelection);
b41ec29a
VZ
253 delete [] choices;
254
255 return res;
256}
257
697f4a96
VZ
258void* wxGetSingleChoiceData( const wxString& message,
259 const wxString& caption,
260 const wxArrayString& choices,
261 void **client_data,
262 int initialSelection,
263 wxWindow *parent)
264{
265 return wxGetSingleChoiceData(message, caption, choices,
266 client_data, parent,
267 wxDefaultCoord, wxDefaultCoord,
268 true, wxCHOICE_WIDTH, wxCHOICE_HEIGHT,
269 initialSelection);
270}
271
272void* wxGetSingleChoiceData( const wxString& message,
273 const wxString& caption,
274 int n, const wxString *choices,
275 void **client_data,
276 int initialSelection,
277 wxWindow *parent)
278{
279 return wxGetSingleChoiceData(message, caption, n, choices,
280 client_data, parent,
281 wxDefaultCoord, wxDefaultCoord,
282 true, wxCHOICE_WIDTH, wxCHOICE_HEIGHT,
283 initialSelection);
284}
285
286
e5cfb314 287int wxGetSelectedChoices(wxArrayInt& selections,
d6c9c1b7
VZ
288 const wxString& message,
289 const wxString& caption,
290 int n, const wxString *choices,
291 wxWindow *parent,
292 int WXUNUSED(x), int WXUNUSED(y),
293 bool WXUNUSED(centre),
294 int WXUNUSED(width), int WXUNUSED(height))
295{
296 wxMultiChoiceDialog dialog(parent, message, caption, n, choices);
e93bfe3c 297
b2d739ba
VZ
298 // call this even if selections array is empty and this then (correctly)
299 // deselects the first item which is selected by default
300 dialog.SetSelections(selections);
e93bfe3c 301
e5cfb314
VZ
302 if ( dialog.ShowModal() != wxID_OK )
303 {
304 // NB: intentionally do not clear the selections array here, the caller
305 // might want to preserve its original contents if the dialog was
306 // cancelled
307 return -1;
308 }
c801d85f 309
e5cfb314 310 selections = dialog.GetSelections();
d6c9c1b7
VZ
311 return selections.GetCount();
312}
c801d85f 313
e5cfb314 314int wxGetSelectedChoices(wxArrayInt& selections,
d6c9c1b7
VZ
315 const wxString& message,
316 const wxString& caption,
317 const wxArrayString& aChoices,
318 wxWindow *parent,
319 int x, int y,
320 bool centre,
321 int width, int height)
c801d85f 322{
d6c9c1b7
VZ
323 wxString *choices;
324 int n = ConvertWXArrayToC(aChoices, &choices);
e5cfb314 325 int res = wxGetSelectedChoices(selections, message, caption,
d6c9c1b7
VZ
326 n, choices, parent,
327 x, y, centre, width, height);
328 delete [] choices;
329
330 return res;
c801d85f 331}
c801d85f 332
e5cfb314
VZ
333#if WXWIN_COMPATIBILITY_2_8
334size_t wxGetMultipleChoices(wxArrayInt& selections,
335 const wxString& message,
336 const wxString& caption,
337 int n, const wxString *choices,
338 wxWindow *parent,
339 int x, int y,
340 bool centre,
341 int width, int height)
342{
343 int rc = wxGetSelectedChoices(selections, message, caption,
344 n, choices,
345 parent, x, y, centre, width, height);
346 if ( rc == -1 )
347 {
348 selections.clear();
349 return 0;
350 }
351
352 return rc;
353}
354
355size_t wxGetMultipleChoices(wxArrayInt& selections,
356 const wxString& message,
357 const wxString& caption,
358 const wxArrayString& aChoices,
359 wxWindow *parent,
360 int x, int y,
361 bool centre,
362 int width, int height)
363{
364 int rc = wxGetSelectedChoices(selections, message, caption,
365 aChoices,
366 parent, x, y, centre, width, height);
367 if ( rc == -1 )
368 {
369 selections.clear();
370 return 0;
371 }
372
373 return rc;
374}
375#endif // WXWIN_COMPATIBILITY_2_8
376
d6c9c1b7
VZ
377// ----------------------------------------------------------------------------
378// wxAnyChoiceDialog
379// ----------------------------------------------------------------------------
380
381bool wxAnyChoiceDialog::Create(wxWindow *parent,
382 const wxString& message,
383 const wxString& caption,
384 int n, const wxString *choices,
ea660175 385 long styleDlg,
d6c9c1b7
VZ
386 const wxPoint& pos,
387 long styleLbox)
388{
12a124dd
VZ
389 // extract the buttons styles from the dialog one and remove them from it
390 const long styleBtns = styleDlg & (wxOK | wxCANCEL);
391 styleDlg &= ~styleBtns;
392
ca65c044
WS
393 if ( !wxDialog::Create(parent, wxID_ANY, caption, pos, wxDefaultSize, styleDlg) )
394 return false;
d6c9c1b7
VZ
395
396 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
397
64c794f6 398 // 1) text message
bd9f3519
VZ
399 topsizer->
400 Add(CreateTextSizer(message), wxSizerFlags().Expand().TripleBorder());
401
64c794f6 402 // 2) list box
bd9f3519 403 m_listbox = CreateList(n, choices, styleLbox);
60104cba 404
64c794f6
WS
405 if ( n > 0 )
406 m_listbox->SetSelection(0);
407
bd9f3519 408 topsizer->
6fe7685d 409 Add(m_listbox, wxSizerFlags().Expand().Proportion(1).TripleBorder(wxLEFT | wxRIGHT));
119727ad 410
897b24cf 411 // 3) buttons if any
bd9f3519 412 wxSizer *
12a124dd 413 buttonSizer = CreateSeparatedButtonSizer(styleBtns);
bd9f3519 414 if ( buttonSizer )
897b24cf 415 {
bd9f3519 416 topsizer->Add(buttonSizer, wxSizerFlags().Expand().DoubleBorder());
897b24cf 417 }
64c794f6 418
d6c9c1b7
VZ
419 SetSizer( topsizer );
420
421 topsizer->SetSizeHints( this );
422 topsizer->Fit( this );
423
8316ff5d
VS
424 if ( styleDlg & wxCENTRE )
425 Centre(wxBOTH);
d6c9c1b7
VZ
426
427 m_listbox->SetFocus();
428
ca65c044 429 return true;
d6c9c1b7
VZ
430}
431
584ad2a3
MB
432bool wxAnyChoiceDialog::Create(wxWindow *parent,
433 const wxString& message,
434 const wxString& caption,
435 const wxArrayString& choices,
436 long styleDlg,
437 const wxPoint& pos,
438 long styleLbox)
439{
440 wxCArrayString chs(choices);
441 return Create(parent, message, caption, chs.GetCount(), chs.GetStrings(),
442 styleDlg, pos, styleLbox);
443}
444
60104cba
WS
445wxListBoxBase *wxAnyChoiceDialog::CreateList(int n, const wxString *choices, long styleLbox)
446{
447 return new wxListBox( this, wxID_LISTBOX,
61a11cd6 448 wxDefaultPosition, wxDefaultSize,
60104cba
WS
449 n, choices,
450 styleLbox );
451}
452
d6c9c1b7 453// ----------------------------------------------------------------------------
c801d85f 454// wxSingleChoiceDialog
d6c9c1b7 455// ----------------------------------------------------------------------------
c801d85f 456
c801d85f 457BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
d427503c 458 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
7b504551 459#ifndef __SMARTPHONE__
d427503c 460 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
7b504551
WS
461#endif
462#ifdef __WXWINCE__
463 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown)
464#endif
c801d85f
KB
465END_EVENT_TABLE()
466
d6c9c1b7 467IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
257bf510 468
d6c9c1b7 469bool wxSingleChoiceDialog::Create( wxWindow *parent,
c50f1fb9 470 const wxString& message,
d6c9c1b7 471 const wxString& caption,
c50f1fb9 472 int n,
257bf510 473 const wxString *choices,
fc12b1f1 474 void **clientData,
257bf510 475 long style,
d6c9c1b7 476 const wxPoint& pos )
c801d85f 477{
d6c9c1b7
VZ
478 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
479 n, choices,
480 style, pos) )
ca65c044 481 return false;
92afa2b1 482
d6c9c1b7 483 m_selection = n > 0 ? 0 : -1;
92afa2b1 484
92afa2b1 485 if (clientData)
d427503c 486 {
257bf510
VZ
487 for (int i = 0; i < n; i++)
488 m_listbox->SetClientData(i, clientData[i]);
d427503c 489 }
c801d85f 490
ca65c044 491 return true;
c801d85f
KB
492}
493
584ad2a3
MB
494bool wxSingleChoiceDialog::Create( wxWindow *parent,
495 const wxString& message,
496 const wxString& caption,
497 const wxArrayString& choices,
fc12b1f1 498 void **clientData,
584ad2a3
MB
499 long style,
500 const wxPoint& pos )
501{
502 wxCArrayString chs(choices);
503 return Create( parent, message, caption, chs.GetCount(), chs.GetStrings(),
504 clientData, style, pos );
505}
506
ef77f91e
JS
507// Set the selection
508void wxSingleChoiceDialog::SetSelection(int sel)
509{
e0856740 510 wxCHECK_RET( sel >= 0 && (unsigned)sel < m_listbox->GetCount(),
697f4a96
VZ
511 "Invalid initial selection" );
512
257bf510 513 m_listbox->SetSelection(sel);
ef77f91e
JS
514 m_selection = sel;
515}
516
c801d85f
KB
517void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
518{
7b504551 519 DoChoice();
c801d85f
KB
520}
521
7b504551 522#ifndef __SMARTPHONE__
debe6624 523void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
7b504551
WS
524{
525 DoChoice();
526}
527#endif
528
529#ifdef __WXWINCE__
530void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent& WXUNUSED(event))
531{
532 DoChoice();
533}
534#endif
535
536void wxSingleChoiceDialog::DoChoice()
debe6624 537{
257bf510
VZ
538 m_selection = m_listbox->GetSelection();
539 m_stringSelection = m_listbox->GetStringSelection();
25f47127 540
eb553cb2 541 if ( m_listbox->HasClientUntypedData() )
59af5f19 542 SetClientData(m_listbox->GetClientData(m_selection));
d427503c
VZ
543
544 EndModal(wxID_OK);
debe6624
JS
545}
546
d6c9c1b7
VZ
547// ----------------------------------------------------------------------------
548// wxMultiChoiceDialog
549// ----------------------------------------------------------------------------
550
d6c9c1b7
VZ
551IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
552
553bool wxMultiChoiceDialog::Create( wxWindow *parent,
554 const wxString& message,
555 const wxString& caption,
556 int n,
557 const wxString *choices,
558 long style,
559 const wxPoint& pos )
560{
cb5d54ff
RD
561 long styleLbox;
562#if wxUSE_CHECKLISTBOX
563 styleLbox = wxLB_ALWAYS_SB;
564#else
565 styleLbox = wxLB_ALWAYS_SB | wxLB_EXTENDED;
566#endif
6fe7685d 567
d6c9c1b7
VZ
568 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
569 n, choices,
570 style, pos,
cb5d54ff 571 styleLbox) )
ca65c044 572 return false;
d6c9c1b7 573
ca65c044 574 return true;
d6c9c1b7
VZ
575}
576
584ad2a3
MB
577bool wxMultiChoiceDialog::Create( wxWindow *parent,
578 const wxString& message,
579 const wxString& caption,
580 const wxArrayString& choices,
581 long style,
582 const wxPoint& pos )
583{
584 wxCArrayString chs(choices);
585 return Create( parent, message, caption, chs.GetCount(),
586 chs.GetStrings(), style, pos );
587}
588
d6c9c1b7
VZ
589void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
590{
abc2857f
JS
591#if wxUSE_CHECKLISTBOX
592 wxCheckListBox* checkListBox = wxDynamicCast(m_listbox, wxCheckListBox);
593 if (checkListBox)
594 {
595 // first clear all currently selected items
596 size_t n,
597 count = checkListBox->GetCount();
598 for ( n = 0; n < count; ++n )
599 {
600 if (checkListBox->IsChecked(n))
601 checkListBox->Check(n, false);
602 }
603
604 // now select the ones which should be selected
605 count = selections.GetCount();
606 for ( n = 0; n < count; n++ )
607 {
608 checkListBox->Check(selections[n]);
609 }
6fe7685d 610
abc2857f
JS
611 return;
612 }
613#endif
6fe7685d 614
d0cc483d
VZ
615 // first clear all currently selected items
616 size_t n,
617 count = m_listbox->GetCount();
618 for ( n = 0; n < count; ++n )
619 {
620 m_listbox->Deselect(n);
621 }
622
623 // now select the ones which should be selected
624 count = selections.GetCount();
625 for ( n = 0; n < count; n++ )
d6c9c1b7
VZ
626 {
627 m_listbox->Select(selections[n]);
628 }
629}
630
3d49ce44 631bool wxMultiChoiceDialog::TransferDataFromWindow()
d6c9c1b7
VZ
632{
633 m_selections.Empty();
abc2857f
JS
634
635#if wxUSE_CHECKLISTBOX
636 wxCheckListBox* checkListBox = wxDynamicCast(m_listbox, wxCheckListBox);
637 if (checkListBox)
638 {
639 size_t count = checkListBox->GetCount();
640 for ( size_t n = 0; n < count; n++ )
641 {
642 if ( checkListBox->IsChecked(n) )
643 m_selections.Add(n);
644 }
645 return true;
646 }
647#endif
648
d6c9c1b7
VZ
649 size_t count = m_listbox->GetCount();
650 for ( size_t n = 0; n < count; n++ )
651 {
652 if ( m_listbox->IsSelected(n) )
653 m_selections.Add(n);
654 }
655
ca65c044 656 return true;
d6c9c1b7 657}
1e6feb95 658
60104cba
WS
659#if wxUSE_CHECKLISTBOX
660
661wxListBoxBase *wxMultiChoiceDialog::CreateList(int n, const wxString *choices, long styleLbox)
662{
663 return new wxCheckListBox( this, wxID_LISTBOX,
61a11cd6 664 wxDefaultPosition, wxDefaultSize,
60104cba
WS
665 n, choices,
666 styleLbox );
667}
668
669#endif // wxUSE_CHECKLISTBOX
670
1e6feb95 671#endif // wxUSE_CHOICEDLG