]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicdgg.cpp
remove unneeded wxCHECK_MSG: GetNextMessage() can get the msg even if the event loop...
[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{
a319ce7d 293#ifdef __WXMAC__
bd9f3519 294 // FIXME: why??
a319ce7d
SC
295 if ( !wxDialog::Create(parent, wxID_ANY, caption, pos, wxDefaultSize, styleDlg & (~wxCANCEL) ) )
296 return false;
297#else
ca65c044
WS
298 if ( !wxDialog::Create(parent, wxID_ANY, caption, pos, wxDefaultSize, styleDlg) )
299 return false;
a319ce7d 300#endif
d6c9c1b7
VZ
301
302 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
303
64c794f6 304 // 1) text message
bd9f3519
VZ
305 topsizer->
306 Add(CreateTextSizer(message), wxSizerFlags().Expand().TripleBorder());
307
64c794f6 308 // 2) list box
bd9f3519 309 m_listbox = CreateList(n, choices, styleLbox);
60104cba 310
64c794f6
WS
311 if ( n > 0 )
312 m_listbox->SetSelection(0);
313
bd9f3519 314 topsizer->
6fe7685d 315 Add(m_listbox, wxSizerFlags().Expand().Proportion(1).TripleBorder(wxLEFT | wxRIGHT));
119727ad 316
897b24cf 317 // 3) buttons if any
bd9f3519
VZ
318 wxSizer *
319 buttonSizer = CreateSeparatedButtonSizer(styleDlg & ButtonSizerFlags);
320 if ( buttonSizer )
897b24cf 321 {
bd9f3519 322 topsizer->Add(buttonSizer, wxSizerFlags().Expand().DoubleBorder());
897b24cf 323 }
64c794f6 324
d6c9c1b7
VZ
325 SetSizer( topsizer );
326
327 topsizer->SetSizeHints( this );
328 topsizer->Fit( this );
329
8316ff5d
VS
330 if ( styleDlg & wxCENTRE )
331 Centre(wxBOTH);
d6c9c1b7
VZ
332
333 m_listbox->SetFocus();
334
ca65c044 335 return true;
d6c9c1b7
VZ
336}
337
584ad2a3
MB
338bool wxAnyChoiceDialog::Create(wxWindow *parent,
339 const wxString& message,
340 const wxString& caption,
341 const wxArrayString& choices,
342 long styleDlg,
343 const wxPoint& pos,
344 long styleLbox)
345{
346 wxCArrayString chs(choices);
347 return Create(parent, message, caption, chs.GetCount(), chs.GetStrings(),
348 styleDlg, pos, styleLbox);
349}
350
60104cba
WS
351wxListBoxBase *wxAnyChoiceDialog::CreateList(int n, const wxString *choices, long styleLbox)
352{
353 return new wxListBox( this, wxID_LISTBOX,
61a11cd6 354 wxDefaultPosition, wxDefaultSize,
60104cba
WS
355 n, choices,
356 styleLbox );
357}
358
d6c9c1b7 359// ----------------------------------------------------------------------------
c801d85f 360// wxSingleChoiceDialog
d6c9c1b7 361// ----------------------------------------------------------------------------
c801d85f 362
c801d85f 363BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
d427503c 364 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
7b504551 365#ifndef __SMARTPHONE__
d427503c 366 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
7b504551
WS
367#endif
368#ifdef __WXWINCE__
369 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown)
370#endif
c801d85f
KB
371END_EVENT_TABLE()
372
d6c9c1b7 373IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
257bf510
VZ
374
375wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
376 const wxString& message,
377 const wxString& caption,
c50f1fb9 378 int n,
257bf510
VZ
379 const wxString *choices,
380 char **clientData,
381 long style,
59bd9598 382 const wxPoint& WXUNUSED(pos))
c801d85f 383{
257bf510 384 Create(parent, message, caption, n, choices, clientData, style);
c801d85f
KB
385}
386
584ad2a3
MB
387wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
388 const wxString& message,
389 const wxString& caption,
390 const wxArrayString& choices,
391 char **clientData,
392 long style,
393 const wxPoint& WXUNUSED(pos))
394{
395 Create(parent, message, caption, choices, clientData, style);
396}
397
d6c9c1b7 398bool wxSingleChoiceDialog::Create( wxWindow *parent,
c50f1fb9 399 const wxString& message,
d6c9c1b7 400 const wxString& caption,
c50f1fb9 401 int n,
257bf510
VZ
402 const wxString *choices,
403 char **clientData,
404 long style,
d6c9c1b7 405 const wxPoint& pos )
c801d85f 406{
d6c9c1b7
VZ
407 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
408 n, choices,
409 style, pos) )
ca65c044 410 return false;
92afa2b1 411
d6c9c1b7 412 m_selection = n > 0 ? 0 : -1;
92afa2b1 413
92afa2b1 414 if (clientData)
d427503c 415 {
257bf510
VZ
416 for (int i = 0; i < n; i++)
417 m_listbox->SetClientData(i, clientData[i]);
d427503c 418 }
c801d85f 419
ca65c044 420 return true;
c801d85f
KB
421}
422
584ad2a3
MB
423bool wxSingleChoiceDialog::Create( wxWindow *parent,
424 const wxString& message,
425 const wxString& caption,
426 const wxArrayString& choices,
427 char **clientData,
428 long style,
429 const wxPoint& pos )
430{
431 wxCArrayString chs(choices);
432 return Create( parent, message, caption, chs.GetCount(), chs.GetStrings(),
433 clientData, style, pos );
434}
435
ef77f91e
JS
436// Set the selection
437void wxSingleChoiceDialog::SetSelection(int sel)
438{
257bf510 439 m_listbox->SetSelection(sel);
ef77f91e
JS
440 m_selection = sel;
441}
442
c801d85f
KB
443void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
444{
7b504551 445 DoChoice();
c801d85f
KB
446}
447
7b504551 448#ifndef __SMARTPHONE__
debe6624 449void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
7b504551
WS
450{
451 DoChoice();
452}
453#endif
454
455#ifdef __WXWINCE__
456void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent& WXUNUSED(event))
457{
458 DoChoice();
459}
460#endif
461
462void wxSingleChoiceDialog::DoChoice()
debe6624 463{
257bf510
VZ
464 m_selection = m_listbox->GetSelection();
465 m_stringSelection = m_listbox->GetStringSelection();
25f47127 466
eb553cb2 467 if ( m_listbox->HasClientUntypedData() )
59af5f19 468 SetClientData(m_listbox->GetClientData(m_selection));
d427503c
VZ
469
470 EndModal(wxID_OK);
debe6624
JS
471}
472
d6c9c1b7
VZ
473// ----------------------------------------------------------------------------
474// wxMultiChoiceDialog
475// ----------------------------------------------------------------------------
476
d6c9c1b7
VZ
477IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
478
479bool wxMultiChoiceDialog::Create( wxWindow *parent,
480 const wxString& message,
481 const wxString& caption,
482 int n,
483 const wxString *choices,
484 long style,
485 const wxPoint& pos )
486{
cb5d54ff
RD
487 long styleLbox;
488#if wxUSE_CHECKLISTBOX
489 styleLbox = wxLB_ALWAYS_SB;
490#else
491 styleLbox = wxLB_ALWAYS_SB | wxLB_EXTENDED;
492#endif
6fe7685d 493
d6c9c1b7
VZ
494 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
495 n, choices,
496 style, pos,
cb5d54ff 497 styleLbox) )
ca65c044 498 return false;
d6c9c1b7 499
ca65c044 500 return true;
d6c9c1b7
VZ
501}
502
584ad2a3
MB
503bool wxMultiChoiceDialog::Create( wxWindow *parent,
504 const wxString& message,
505 const wxString& caption,
506 const wxArrayString& choices,
507 long style,
508 const wxPoint& pos )
509{
510 wxCArrayString chs(choices);
511 return Create( parent, message, caption, chs.GetCount(),
512 chs.GetStrings(), style, pos );
513}
514
d6c9c1b7
VZ
515void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
516{
abc2857f
JS
517#if wxUSE_CHECKLISTBOX
518 wxCheckListBox* checkListBox = wxDynamicCast(m_listbox, wxCheckListBox);
519 if (checkListBox)
520 {
521 // first clear all currently selected items
522 size_t n,
523 count = checkListBox->GetCount();
524 for ( n = 0; n < count; ++n )
525 {
526 if (checkListBox->IsChecked(n))
527 checkListBox->Check(n, false);
528 }
529
530 // now select the ones which should be selected
531 count = selections.GetCount();
532 for ( n = 0; n < count; n++ )
533 {
534 checkListBox->Check(selections[n]);
535 }
6fe7685d 536
abc2857f
JS
537 return;
538 }
539#endif
6fe7685d 540
d0cc483d
VZ
541 // first clear all currently selected items
542 size_t n,
543 count = m_listbox->GetCount();
544 for ( n = 0; n < count; ++n )
545 {
546 m_listbox->Deselect(n);
547 }
548
549 // now select the ones which should be selected
550 count = selections.GetCount();
551 for ( n = 0; n < count; n++ )
d6c9c1b7
VZ
552 {
553 m_listbox->Select(selections[n]);
554 }
555}
556
3d49ce44 557bool wxMultiChoiceDialog::TransferDataFromWindow()
d6c9c1b7
VZ
558{
559 m_selections.Empty();
abc2857f
JS
560
561#if wxUSE_CHECKLISTBOX
562 wxCheckListBox* checkListBox = wxDynamicCast(m_listbox, wxCheckListBox);
563 if (checkListBox)
564 {
565 size_t count = checkListBox->GetCount();
566 for ( size_t n = 0; n < count; n++ )
567 {
568 if ( checkListBox->IsChecked(n) )
569 m_selections.Add(n);
570 }
571 return true;
572 }
573#endif
574
d6c9c1b7
VZ
575 size_t count = m_listbox->GetCount();
576 for ( size_t n = 0; n < count; n++ )
577 {
578 if ( m_listbox->IsSelected(n) )
579 m_selections.Add(n);
580 }
581
ca65c044 582 return true;
d6c9c1b7 583}
1e6feb95 584
60104cba
WS
585#if wxUSE_CHECKLISTBOX
586
587wxListBoxBase *wxMultiChoiceDialog::CreateList(int n, const wxString *choices, long styleLbox)
588{
589 return new wxCheckListBox( this, wxID_LISTBOX,
61a11cd6 590 wxDefaultPosition, wxDefaultSize,
60104cba
WS
591 n, choices,
592 styleLbox );
593}
594
595#endif // wxUSE_CHECKLISTBOX
596
1e6feb95 597#endif // wxUSE_CHOICEDLG