]> git.saurik.com Git - wxWidgets.git/blob - src/generic/choicdgg.cpp
Don't leak m_protocol in wxURL.
[wxWidgets.git] / src / generic / choicdgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/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) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_CHOICEDLG
28
29 #ifndef WX_PRECOMP
30 #include <stdio.h>
31 #include "wx/utils.h"
32 #include "wx/dialog.h"
33 #include "wx/button.h"
34 #include "wx/listbox.h"
35 #include "wx/checklst.h"
36 #include "wx/stattext.h"
37 #include "wx/intl.h"
38 #include "wx/sizer.h"
39 #include "wx/arrstr.h"
40 #endif
41
42 #include "wx/statline.h"
43 #include "wx/settings.h"
44 #include "wx/generic/choicdgg.h"
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 #define wxID_LISTBOX 3000
51
52 // ----------------------------------------------------------------------------
53 // private functions
54 // ----------------------------------------------------------------------------
55
56 // convert wxArrayString into a wxString[] which must be delete[]d by caller
57 static int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices);
58
59 // ============================================================================
60 // implementation
61 // ============================================================================
62
63 // ----------------------------------------------------------------------------
64 // helpers
65 // ----------------------------------------------------------------------------
66
67 int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices)
68 {
69 int n = aChoices.GetCount();
70 *choices = new wxString[n];
71
72 for ( int i = 0; i < n; i++ )
73 {
74 (*choices)[i] = aChoices[i];
75 }
76
77 return n;
78 }
79
80 // ----------------------------------------------------------------------------
81 // wrapper functions
82 // ----------------------------------------------------------------------------
83
84 wxString 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),
90 int WXUNUSED(width), int WXUNUSED(height) )
91 {
92 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
93 wxString choice;
94 if ( dialog.ShowModal() == wxID_OK )
95 choice = dialog.GetStringSelection();
96
97 return choice;
98 }
99
100 wxString 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
117 int 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) )
124 {
125 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
126 int choice;
127 if ( dialog.ShowModal() == wxID_OK )
128 choice = dialog.GetSelection();
129 else
130 choice = -1;
131
132 return choice;
133 }
134
135 int 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
152 void *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) )
160 {
161 wxSingleChoiceDialog dialog(parent, message, caption, n, choices,
162 (char **)client_data);
163 void *data;
164 if ( dialog.ShowModal() == wxID_OK )
165 data = dialog.GetSelectionClientData();
166 else
167 data = NULL;
168
169 return data;
170 }
171
172 void *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
191 int wxGetSelectedChoices(wxArrayInt& selections,
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);
201
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);
205
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 }
213
214 selections = dialog.GetSelections();
215 return selections.GetCount();
216 }
217
218 int wxGetSelectedChoices(wxArrayInt& selections,
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)
226 {
227 wxString *choices;
228 int n = ConvertWXArrayToC(aChoices, &choices);
229 int res = wxGetSelectedChoices(selections, message, caption,
230 n, choices, parent,
231 x, y, centre, width, height);
232 delete [] choices;
233
234 return res;
235 }
236
237 #if WXWIN_COMPATIBILITY_2_8
238 size_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
259 size_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
281 // ----------------------------------------------------------------------------
282 // wxAnyChoiceDialog
283 // ----------------------------------------------------------------------------
284
285 bool wxAnyChoiceDialog::Create(wxWindow *parent,
286 const wxString& message,
287 const wxString& caption,
288 int n, const wxString *choices,
289 long styleDlg,
290 const wxPoint& pos,
291 long styleLbox)
292 {
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
297 if ( !wxDialog::Create(parent, wxID_ANY, caption, pos, wxDefaultSize, styleDlg) )
298 return false;
299
300 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
301
302 // 1) text message
303 topsizer->
304 Add(CreateTextSizer(message), wxSizerFlags().Expand().TripleBorder());
305
306 // 2) list box
307 m_listbox = CreateList(n, choices, styleLbox);
308
309 if ( n > 0 )
310 m_listbox->SetSelection(0);
311
312 topsizer->
313 Add(m_listbox, wxSizerFlags().Expand().Proportion(1).TripleBorder(wxLEFT | wxRIGHT));
314
315 // 3) buttons if any
316 wxSizer *
317 buttonSizer = CreateSeparatedButtonSizer(styleBtns);
318 if ( buttonSizer )
319 {
320 topsizer->Add(buttonSizer, wxSizerFlags().Expand().DoubleBorder());
321 }
322
323 SetSizer( topsizer );
324
325 topsizer->SetSizeHints( this );
326 topsizer->Fit( this );
327
328 if ( styleDlg & wxCENTRE )
329 Centre(wxBOTH);
330
331 m_listbox->SetFocus();
332
333 return true;
334 }
335
336 bool 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
349 wxListBoxBase *wxAnyChoiceDialog::CreateList(int n, const wxString *choices, long styleLbox)
350 {
351 return new wxListBox( this, wxID_LISTBOX,
352 wxDefaultPosition, wxDefaultSize,
353 n, choices,
354 styleLbox );
355 }
356
357 // ----------------------------------------------------------------------------
358 // wxSingleChoiceDialog
359 // ----------------------------------------------------------------------------
360
361 BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
362 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
363 #ifndef __SMARTPHONE__
364 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
365 #endif
366 #ifdef __WXWINCE__
367 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown)
368 #endif
369 END_EVENT_TABLE()
370
371 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
372
373 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
374 const wxString& message,
375 const wxString& caption,
376 int n,
377 const wxString *choices,
378 char **clientData,
379 long style,
380 const wxPoint& WXUNUSED(pos))
381 {
382 Create(parent, message, caption, n, choices, clientData, style);
383 }
384
385 wxSingleChoiceDialog::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
396 bool wxSingleChoiceDialog::Create( wxWindow *parent,
397 const wxString& message,
398 const wxString& caption,
399 int n,
400 const wxString *choices,
401 char **clientData,
402 long style,
403 const wxPoint& pos )
404 {
405 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
406 n, choices,
407 style, pos) )
408 return false;
409
410 m_selection = n > 0 ? 0 : -1;
411
412 if (clientData)
413 {
414 for (int i = 0; i < n; i++)
415 m_listbox->SetClientData(i, clientData[i]);
416 }
417
418 return true;
419 }
420
421 bool 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
434 // Set the selection
435 void wxSingleChoiceDialog::SetSelection(int sel)
436 {
437 m_listbox->SetSelection(sel);
438 m_selection = sel;
439 }
440
441 void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
442 {
443 DoChoice();
444 }
445
446 #ifndef __SMARTPHONE__
447 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
448 {
449 DoChoice();
450 }
451 #endif
452
453 #ifdef __WXWINCE__
454 void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent& WXUNUSED(event))
455 {
456 DoChoice();
457 }
458 #endif
459
460 void wxSingleChoiceDialog::DoChoice()
461 {
462 m_selection = m_listbox->GetSelection();
463 m_stringSelection = m_listbox->GetStringSelection();
464
465 if ( m_listbox->HasClientUntypedData() )
466 SetClientData(m_listbox->GetClientData(m_selection));
467
468 EndModal(wxID_OK);
469 }
470
471 // ----------------------------------------------------------------------------
472 // wxMultiChoiceDialog
473 // ----------------------------------------------------------------------------
474
475 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
476
477 bool 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 {
485 long styleLbox;
486 #if wxUSE_CHECKLISTBOX
487 styleLbox = wxLB_ALWAYS_SB;
488 #else
489 styleLbox = wxLB_ALWAYS_SB | wxLB_EXTENDED;
490 #endif
491
492 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
493 n, choices,
494 style, pos,
495 styleLbox) )
496 return false;
497
498 return true;
499 }
500
501 bool 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
513 void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
514 {
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 }
534
535 return;
536 }
537 #endif
538
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++ )
550 {
551 m_listbox->Select(selections[n]);
552 }
553 }
554
555 bool wxMultiChoiceDialog::TransferDataFromWindow()
556 {
557 m_selections.Empty();
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
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
580 return true;
581 }
582
583 #if wxUSE_CHECKLISTBOX
584
585 wxListBoxBase *wxMultiChoiceDialog::CreateList(int n, const wxString *choices, long styleLbox)
586 {
587 return new wxCheckListBox( this, wxID_LISTBOX,
588 wxDefaultPosition, wxDefaultSize,
589 n, choices,
590 styleLbox );
591 }
592
593 #endif // wxUSE_CHECKLISTBOX
594
595 #endif // wxUSE_CHOICEDLG