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