]> git.saurik.com Git - wxWidgets.git/blob - src/generic/choicdgg.cpp
Added wxPaintDCEx class, to handle the case where an HDC
[wxWidgets.git] / src / generic / choicdgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: choicdgg.cpp
3 // Purpose: Choice dialogs
4 // Author: Julian Smart
5 // Modified by: 03.11.00: VZ to add wxArrayString and multiple sel functions
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 #ifdef __GNUG__
17 #pragma implementation "choicdgg.h"
18 #endif
19
20 // ----------------------------------------------------------------------------
21 // headers
22 // ----------------------------------------------------------------------------
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_CHOICEDLG
32
33 #ifndef WX_PRECOMP
34 #include <stdio.h>
35 #include "wx/utils.h"
36 #include "wx/dialog.h"
37 #include "wx/button.h"
38 #include "wx/listbox.h"
39 #include "wx/stattext.h"
40 #include "wx/intl.h"
41 #include "wx/sizer.h"
42 #include "wx/arrstr.h"
43 #endif
44
45 #if wxUSE_STATLINE
46 #include "wx/statline.h"
47 #endif
48
49 #include "wx/generic/choicdgg.h"
50
51 // ----------------------------------------------------------------------------
52 // constants
53 // ----------------------------------------------------------------------------
54
55 #define wxID_LISTBOX 3000
56
57 // ----------------------------------------------------------------------------
58 // private functions
59 // ----------------------------------------------------------------------------
60
61 // convert wxArrayString into a wxString[] which must be delete[]d by caller
62 static int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices);
63
64 // ============================================================================
65 // implementation
66 // ============================================================================
67
68 // ----------------------------------------------------------------------------
69 // helpers
70 // ----------------------------------------------------------------------------
71
72 int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices)
73 {
74 int n = aChoices.GetCount();
75 *choices = new wxString[n];
76
77 for ( int i = 0; i < n; i++ )
78 {
79 (*choices)[i] = aChoices[i];
80 }
81
82 return n;
83 }
84
85 // ----------------------------------------------------------------------------
86 // wrapper functions
87 // ----------------------------------------------------------------------------
88
89 wxString wxGetSingleChoice( const wxString& message,
90 const wxString& caption,
91 int n, const wxString *choices,
92 wxWindow *parent,
93 int WXUNUSED(x), int WXUNUSED(y),
94 bool WXUNUSED(centre),
95 int WXUNUSED(width), int WXUNUSED(height) )
96 {
97 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
98 wxString choice;
99 if ( dialog.ShowModal() == wxID_OK )
100 choice = dialog.GetStringSelection();
101
102 return choice;
103 }
104
105 wxString wxGetSingleChoice( const wxString& message,
106 const wxString& caption,
107 const wxArrayString& aChoices,
108 wxWindow *parent,
109 int x, int y,
110 bool centre,
111 int width, int height)
112 {
113 wxString *choices;
114 int n = ConvertWXArrayToC(aChoices, &choices);
115 wxString res = wxGetSingleChoice(message, caption, n, choices, parent,
116 x, y, centre, width, height);
117 delete [] choices;
118
119 return res;
120 }
121
122 #if WXWIN_COMPATIBILITY_2
123 // Overloaded for backward compatibility
124 wxString wxGetSingleChoice( const wxString& message,
125 const wxString& caption,
126 int n, wxChar *choices[],
127 wxWindow *parent,
128 int x, int y, bool centre,
129 int width, int height )
130 {
131 wxString *strings = new wxString[n];
132 int i;
133 for ( i = 0; i < n; i++)
134 {
135 strings[i] = choices[i];
136 }
137 wxString ans(wxGetSingleChoice(message, caption, n, (const wxString *)strings, parent,
138 x, y, centre, width, height));
139 delete[] strings;
140 return ans;
141 }
142 #endif // WXWIN_COMPATIBILITY_2
143
144 int wxGetSingleChoiceIndex( const wxString& message,
145 const wxString& caption,
146 int n, const wxString *choices,
147 wxWindow *parent,
148 int WXUNUSED(x), int WXUNUSED(y),
149 bool WXUNUSED(centre),
150 int WXUNUSED(width), int WXUNUSED(height) )
151 {
152 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
153 int choice;
154 if ( dialog.ShowModal() == wxID_OK )
155 choice = dialog.GetSelection();
156 else
157 choice = -1;
158
159 return choice;
160 }
161
162 int wxGetSingleChoiceIndex( const wxString& message,
163 const wxString& caption,
164 const wxArrayString& aChoices,
165 wxWindow *parent,
166 int x, int y,
167 bool centre,
168 int width, int height)
169 {
170 wxString *choices;
171 int n = ConvertWXArrayToC(aChoices, &choices);
172 int res = wxGetSingleChoiceIndex(message, caption, n, choices, parent,
173 x, y, centre, width, height);
174 delete [] choices;
175
176 return res;
177 }
178
179 #if WXWIN_COMPATIBILITY_2
180 // Overloaded for backward compatibility
181 int wxGetSingleChoiceIndex( const wxString& message,
182 const wxString& caption,
183 int n, wxChar *choices[],
184 wxWindow *parent,
185 int x, int y, bool centre,
186 int width, int height )
187 {
188 wxString *strings = new wxString[n];
189 for ( int i = 0; i < n; i++)
190 strings[i] = choices[i];
191 int ans = wxGetSingleChoiceIndex(message, caption, n, (const wxString *)strings, parent,
192 x, y, centre, width, height);
193 delete[] strings;
194 return ans;
195 }
196 #endif // WXWIN_COMPATIBILITY_2
197
198 void *wxGetSingleChoiceData( const wxString& message,
199 const wxString& caption,
200 int n, const wxString *choices,
201 void **client_data,
202 wxWindow *parent,
203 int WXUNUSED(x), int WXUNUSED(y),
204 bool WXUNUSED(centre),
205 int WXUNUSED(width), int WXUNUSED(height) )
206 {
207 wxSingleChoiceDialog dialog(parent, message, caption, n, choices,
208 (char **)client_data);
209 void *data;
210 if ( dialog.ShowModal() == wxID_OK )
211 data = dialog.GetSelectionClientData();
212 else
213 data = NULL;
214
215 return data;
216 }
217
218 void *wxGetSingleChoiceData( const wxString& message,
219 const wxString& caption,
220 const wxArrayString& aChoices,
221 void **client_data,
222 wxWindow *parent,
223 int x, int y,
224 bool centre,
225 int width, int height)
226 {
227 wxString *choices;
228 int n = ConvertWXArrayToC(aChoices, &choices);
229 void *res = wxGetSingleChoiceData(message, caption, n, choices,
230 client_data, parent,
231 x, y, centre, width, height);
232 delete [] choices;
233
234 return res;
235 }
236
237 #if WXWIN_COMPATIBILITY_2
238 // Overloaded for backward compatibility
239 void *wxGetSingleChoiceData( const wxString& message,
240 const wxString& caption,
241 int n, wxChar *choices[],
242 void **client_data,
243 wxWindow *parent,
244 int x, int y, bool centre, int width, int height )
245 {
246 wxString *strings = new wxString[n];
247 int i;
248 for ( i = 0; i < n; i++)
249 {
250 strings[i] = choices[i];
251 }
252 void *data = wxGetSingleChoiceData(message, caption,
253 n, (const wxString *)strings,
254 client_data, parent,
255 x, y, centre, width, height);
256 delete[] strings;
257 return data;
258 }
259 #endif // WXWIN_COMPATIBILITY_2
260
261 size_t wxGetMultipleChoices(wxArrayInt& selections,
262 const wxString& message,
263 const wxString& caption,
264 int n, const wxString *choices,
265 wxWindow *parent,
266 int WXUNUSED(x), int WXUNUSED(y),
267 bool WXUNUSED(centre),
268 int WXUNUSED(width), int WXUNUSED(height))
269 {
270 wxMultiChoiceDialog dialog(parent, message, caption, n, choices);
271
272 if ( !selections.IsEmpty() )
273 dialog.SetSelections(selections);
274
275 if ( dialog.ShowModal() == wxID_OK )
276 selections = dialog.GetSelections();
277 else
278 selections.Empty();
279
280 return selections.GetCount();
281 }
282
283 size_t wxGetMultipleChoices(wxArrayInt& selections,
284 const wxString& message,
285 const wxString& caption,
286 const wxArrayString& aChoices,
287 wxWindow *parent,
288 int x, int y,
289 bool centre,
290 int width, int height)
291 {
292 wxString *choices;
293 int n = ConvertWXArrayToC(aChoices, &choices);
294 size_t res = wxGetMultipleChoices(selections, message, caption,
295 n, choices, parent,
296 x, y, centre, width, height);
297 delete [] choices;
298
299 return res;
300 }
301
302 // ----------------------------------------------------------------------------
303 // wxAnyChoiceDialog
304 // ----------------------------------------------------------------------------
305
306 bool wxAnyChoiceDialog::Create(wxWindow *parent,
307 const wxString& message,
308 const wxString& caption,
309 int n, const wxString *choices,
310 long styleDlg,
311 const wxPoint& pos,
312 long styleLbox)
313 {
314 if ( !wxDialog::Create(parent, -1, caption, pos, wxDefaultSize, styleDlg) )
315 return FALSE;
316
317 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
318
319 // 1) text message
320 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
321
322 // 2) list box
323 m_listbox = new wxListBox( this, wxID_LISTBOX,
324 wxDefaultPosition, wxDefaultSize,
325 n, choices,
326 styleLbox );
327 if ( n > 0 )
328 m_listbox->SetSelection(0);
329
330 topsizer->Add( m_listbox, 1, wxEXPAND | wxLEFT|wxRIGHT, 15 );
331
332 #if wxUSE_STATLINE
333 // 3) static line
334 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
335 #endif
336
337 // 4) buttons
338 topsizer->Add( CreateButtonSizer( styleDlg & (wxOK|wxCANCEL) ), 0, wxCENTRE | wxALL, 10 );
339
340 SetAutoLayout( TRUE );
341 SetSizer( topsizer );
342
343 topsizer->SetSizeHints( this );
344 topsizer->Fit( this );
345
346 Centre( wxBOTH );
347
348 m_listbox->SetFocus();
349
350 return TRUE;
351 }
352
353 // ----------------------------------------------------------------------------
354 // wxSingleChoiceDialog
355 // ----------------------------------------------------------------------------
356
357 BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
358 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
359 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
360 END_EVENT_TABLE()
361
362 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
363
364 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
365 const wxString& message,
366 const wxString& caption,
367 int n,
368 const wxString *choices,
369 char **clientData,
370 long style,
371 const wxPoint& WXUNUSED(pos))
372 {
373 Create(parent, message, caption, n, choices, clientData, style);
374 }
375
376 #if WXWIN_COMPATIBILITY_2
377
378 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
379 const wxString& message,
380 const wxString& caption,
381 const wxStringList& choices,
382 char **clientData,
383 long style,
384 const wxPoint& WXUNUSED(pos))
385 {
386 Create(parent, message, caption, choices, clientData, style);
387 }
388
389 bool wxSingleChoiceDialog::Create(wxWindow *parent,
390 const wxString& message,
391 const wxString& caption,
392 const wxStringList& choices,
393 char **clientData,
394 long style,
395 const wxPoint& pos)
396 {
397 wxString *strings = new wxString[choices.Number()];
398 int i;
399 for ( i = 0; i < choices.Number(); i++)
400 {
401 strings[i] = (char *)choices.Nth(i)->Data();
402 }
403 bool ans = Create(parent, message, caption, choices.Number(), strings, clientData, style, pos);
404 delete[] strings;
405 return ans;
406 }
407
408 #endif // WXWIN_COMPATIBILITY_2
409
410 bool wxSingleChoiceDialog::Create( wxWindow *parent,
411 const wxString& message,
412 const wxString& caption,
413 int n,
414 const wxString *choices,
415 char **clientData,
416 long style,
417 const wxPoint& pos )
418 {
419 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
420 n, choices,
421 style, pos) )
422 return FALSE;
423
424 m_selection = n > 0 ? 0 : -1;
425
426 if (clientData)
427 {
428 for (int i = 0; i < n; i++)
429 m_listbox->SetClientData(i, clientData[i]);
430 }
431
432 return TRUE;
433 }
434
435 // Set the selection
436 void wxSingleChoiceDialog::SetSelection(int sel)
437 {
438 m_listbox->SetSelection(sel);
439 m_selection = sel;
440 }
441
442 void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
443 {
444 m_selection = m_listbox->GetSelection();
445 m_stringSelection = m_listbox->GetStringSelection();
446 if ( m_listbox->HasClientUntypedData() )
447 SetClientData(m_listbox->GetClientData(m_selection));
448 EndModal(wxID_OK);
449 }
450
451 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
452 {
453 m_selection = m_listbox->GetSelection();
454 m_stringSelection = m_listbox->GetStringSelection();
455
456 if ( m_listbox->HasClientUntypedData() )
457 SetClientData(m_listbox->GetClientData(m_selection));
458
459 EndModal(wxID_OK);
460 }
461
462 // ----------------------------------------------------------------------------
463 // wxMultiChoiceDialog
464 // ----------------------------------------------------------------------------
465
466 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
467
468 bool wxMultiChoiceDialog::Create( wxWindow *parent,
469 const wxString& message,
470 const wxString& caption,
471 int n,
472 const wxString *choices,
473 long style,
474 const wxPoint& pos )
475 {
476 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
477 n, choices,
478 style, pos,
479 wxLB_ALWAYS_SB | wxLB_EXTENDED) )
480 return FALSE;
481
482 return TRUE;
483 }
484
485 void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
486 {
487 size_t count = selections.GetCount();
488 for ( size_t n = 0; n < count; n++ )
489 {
490 m_listbox->Select(selections[n]);
491 }
492 }
493
494 bool wxMultiChoiceDialog::TransferDataFromWindow()
495 {
496 m_selections.Empty();
497 size_t count = m_listbox->GetCount();
498 for ( size_t n = 0; n < count; n++ )
499 {
500 if ( m_listbox->IsSelected(n) )
501 m_selections.Add(n);
502 }
503
504 return TRUE;
505 }
506
507 #endif // wxUSE_CHOICEDLG