]> git.saurik.com Git - wxWidgets.git/blob - src/generic/choicdgg.cpp
another fix for !USE_PCH: added missing wx/imaglist.h include
[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) 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/stattext.h"
36 #include "wx/intl.h"
37 #include "wx/sizer.h"
38 #include "wx/arrstr.h"
39 #endif
40
41 #if wxUSE_STATLINE
42 #include "wx/statline.h"
43 #endif
44
45 #include "wx/generic/choicdgg.h"
46
47 // ----------------------------------------------------------------------------
48 // constants
49 // ----------------------------------------------------------------------------
50
51 #define wxID_LISTBOX 3000
52
53 // ---------------------------------------------------------------------------
54 // macros
55 // ---------------------------------------------------------------------------
56
57 /* Macro for avoiding #ifdefs when value have to be different depending on size of
58 device we display on - take it from something like wxDesktopPolicy in the future
59 */
60
61 #if defined(__SMARTPHONE__)
62 #define wxLARGESMALL(large,small) small
63 #else
64 #define wxLARGESMALL(large,small) large
65 #endif
66
67 // ----------------------------------------------------------------------------
68 // private functions
69 // ----------------------------------------------------------------------------
70
71 // convert wxArrayString into a wxString[] which must be delete[]d by caller
72 static int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices);
73
74 // ============================================================================
75 // implementation
76 // ============================================================================
77
78 // ----------------------------------------------------------------------------
79 // helpers
80 // ----------------------------------------------------------------------------
81
82 int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices)
83 {
84 int n = aChoices.GetCount();
85 *choices = new wxString[n];
86
87 for ( int i = 0; i < n; i++ )
88 {
89 (*choices)[i] = aChoices[i];
90 }
91
92 return n;
93 }
94
95 // ----------------------------------------------------------------------------
96 // wrapper functions
97 // ----------------------------------------------------------------------------
98
99 wxString wxGetSingleChoice( const wxString& message,
100 const wxString& caption,
101 int n, const wxString *choices,
102 wxWindow *parent,
103 int WXUNUSED(x), int WXUNUSED(y),
104 bool WXUNUSED(centre),
105 int WXUNUSED(width), int WXUNUSED(height) )
106 {
107 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
108 wxString choice;
109 if ( dialog.ShowModal() == wxID_OK )
110 choice = dialog.GetStringSelection();
111
112 return choice;
113 }
114
115 wxString wxGetSingleChoice( const wxString& message,
116 const wxString& caption,
117 const wxArrayString& aChoices,
118 wxWindow *parent,
119 int x, int y,
120 bool centre,
121 int width, int height)
122 {
123 wxString *choices;
124 int n = ConvertWXArrayToC(aChoices, &choices);
125 wxString res = wxGetSingleChoice(message, caption, n, choices, parent,
126 x, y, centre, width, height);
127 delete [] choices;
128
129 return res;
130 }
131
132 int wxGetSingleChoiceIndex( const wxString& message,
133 const wxString& caption,
134 int n, const wxString *choices,
135 wxWindow *parent,
136 int WXUNUSED(x), int WXUNUSED(y),
137 bool WXUNUSED(centre),
138 int WXUNUSED(width), int WXUNUSED(height) )
139 {
140 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
141 int choice;
142 if ( dialog.ShowModal() == wxID_OK )
143 choice = dialog.GetSelection();
144 else
145 choice = -1;
146
147 return choice;
148 }
149
150 int wxGetSingleChoiceIndex( const wxString& message,
151 const wxString& caption,
152 const wxArrayString& aChoices,
153 wxWindow *parent,
154 int x, int y,
155 bool centre,
156 int width, int height)
157 {
158 wxString *choices;
159 int n = ConvertWXArrayToC(aChoices, &choices);
160 int res = wxGetSingleChoiceIndex(message, caption, n, choices, parent,
161 x, y, centre, width, height);
162 delete [] choices;
163
164 return res;
165 }
166
167 void *wxGetSingleChoiceData( const wxString& message,
168 const wxString& caption,
169 int n, const wxString *choices,
170 void **client_data,
171 wxWindow *parent,
172 int WXUNUSED(x), int WXUNUSED(y),
173 bool WXUNUSED(centre),
174 int WXUNUSED(width), int WXUNUSED(height) )
175 {
176 wxSingleChoiceDialog dialog(parent, message, caption, n, choices,
177 (char **)client_data);
178 void *data;
179 if ( dialog.ShowModal() == wxID_OK )
180 data = dialog.GetSelectionClientData();
181 else
182 data = NULL;
183
184 return data;
185 }
186
187 void *wxGetSingleChoiceData( const wxString& message,
188 const wxString& caption,
189 const wxArrayString& aChoices,
190 void **client_data,
191 wxWindow *parent,
192 int x, int y,
193 bool centre,
194 int width, int height)
195 {
196 wxString *choices;
197 int n = ConvertWXArrayToC(aChoices, &choices);
198 void *res = wxGetSingleChoiceData(message, caption, n, choices,
199 client_data, parent,
200 x, y, centre, width, height);
201 delete [] choices;
202
203 return res;
204 }
205
206 size_t wxGetMultipleChoices(wxArrayInt& selections,
207 const wxString& message,
208 const wxString& caption,
209 int n, const wxString *choices,
210 wxWindow *parent,
211 int WXUNUSED(x), int WXUNUSED(y),
212 bool WXUNUSED(centre),
213 int WXUNUSED(width), int WXUNUSED(height))
214 {
215 wxMultiChoiceDialog dialog(parent, message, caption, n, choices);
216
217 // call this even if selections array is empty and this then (correctly)
218 // deselects the first item which is selected by default
219 dialog.SetSelections(selections);
220
221 if ( dialog.ShowModal() == wxID_OK )
222 selections = dialog.GetSelections();
223 else
224 selections.Empty();
225
226 return selections.GetCount();
227 }
228
229 size_t wxGetMultipleChoices(wxArrayInt& selections,
230 const wxString& message,
231 const wxString& caption,
232 const wxArrayString& aChoices,
233 wxWindow *parent,
234 int x, int y,
235 bool centre,
236 int width, int height)
237 {
238 wxString *choices;
239 int n = ConvertWXArrayToC(aChoices, &choices);
240 size_t res = wxGetMultipleChoices(selections, message, caption,
241 n, choices, parent,
242 x, y, centre, width, height);
243 delete [] choices;
244
245 return res;
246 }
247
248 // ----------------------------------------------------------------------------
249 // wxAnyChoiceDialog
250 // ----------------------------------------------------------------------------
251
252 bool wxAnyChoiceDialog::Create(wxWindow *parent,
253 const wxString& message,
254 const wxString& caption,
255 int n, const wxString *choices,
256 long styleDlg,
257 const wxPoint& pos,
258 long styleLbox)
259 {
260 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
261 styleDlg &= ~wxBORDER_MASK;
262 styleDlg &= ~wxRESIZE_BORDER;
263 styleDlg &= ~wxCAPTION;
264 #endif
265
266 if ( !wxDialog::Create(parent, wxID_ANY, caption, pos, wxDefaultSize, styleDlg) )
267 return false;
268
269 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
270
271 // 1) text message
272 topsizer->Add( CreateTextSizer( message ), 0, wxALL, wxLARGESMALL(10,0) );
273
274 // 2) list box
275 m_listbox = new wxListBox( this, wxID_LISTBOX,
276 wxDefaultPosition, wxDefaultSize,
277 n, choices,
278 styleLbox );
279 if ( n > 0 )
280 m_listbox->SetSelection(0);
281
282 topsizer->Add( m_listbox, 1, wxEXPAND|wxLEFT|wxRIGHT, wxLARGESMALL(15,0) );
283
284 // smart phones does not support or do not waste space for wxButtons
285 #ifdef __SMARTPHONE__
286
287 SetRightMenu(wxID_CANCEL, _("Cancel"));
288
289 #else // __SMARTPHONE__/!__SMARTPHONE__
290
291 #if wxUSE_STATLINE
292 // 3) static line
293 topsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
294 #endif
295
296 // 4) buttons
297 topsizer->Add( CreateButtonSizer( styleDlg & (wxOK|wxCANCEL) ), 0, wxEXPAND | wxALL, 10 );
298
299 #endif // !__SMARTPHONE__
300
301 SetSizer( topsizer );
302
303 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
304 topsizer->SetSizeHints( this );
305 topsizer->Fit( this );
306
307 if ( styleDlg & wxCENTRE )
308 Centre(wxBOTH);
309 #endif
310
311 m_listbox->SetFocus();
312
313 return true;
314 }
315
316 bool wxAnyChoiceDialog::Create(wxWindow *parent,
317 const wxString& message,
318 const wxString& caption,
319 const wxArrayString& choices,
320 long styleDlg,
321 const wxPoint& pos,
322 long styleLbox)
323 {
324 wxCArrayString chs(choices);
325 return Create(parent, message, caption, chs.GetCount(), chs.GetStrings(),
326 styleDlg, pos, styleLbox);
327 }
328
329 // ----------------------------------------------------------------------------
330 // wxSingleChoiceDialog
331 // ----------------------------------------------------------------------------
332
333 BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
334 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
335 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
336 END_EVENT_TABLE()
337
338 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
339
340 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
341 const wxString& message,
342 const wxString& caption,
343 int n,
344 const wxString *choices,
345 char **clientData,
346 long style,
347 const wxPoint& WXUNUSED(pos))
348 {
349 Create(parent, message, caption, n, choices, clientData, style);
350 }
351
352 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
353 const wxString& message,
354 const wxString& caption,
355 const wxArrayString& choices,
356 char **clientData,
357 long style,
358 const wxPoint& WXUNUSED(pos))
359 {
360 Create(parent, message, caption, choices, clientData, style);
361 }
362
363 bool wxSingleChoiceDialog::Create( wxWindow *parent,
364 const wxString& message,
365 const wxString& caption,
366 int n,
367 const wxString *choices,
368 char **clientData,
369 long style,
370 const wxPoint& pos )
371 {
372 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
373 n, choices,
374 style, pos) )
375 return false;
376
377 m_selection = n > 0 ? 0 : -1;
378
379 if (clientData)
380 {
381 for (int i = 0; i < n; i++)
382 m_listbox->SetClientData(i, clientData[i]);
383 }
384
385 return true;
386 }
387
388 bool wxSingleChoiceDialog::Create( wxWindow *parent,
389 const wxString& message,
390 const wxString& caption,
391 const wxArrayString& choices,
392 char **clientData,
393 long style,
394 const wxPoint& pos )
395 {
396 wxCArrayString chs(choices);
397 return Create( parent, message, caption, chs.GetCount(), chs.GetStrings(),
398 clientData, style, pos );
399 }
400
401 // Set the selection
402 void wxSingleChoiceDialog::SetSelection(int sel)
403 {
404 m_listbox->SetSelection(sel);
405 m_selection = sel;
406 }
407
408 void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
409 {
410 m_selection = m_listbox->GetSelection();
411 m_stringSelection = m_listbox->GetStringSelection();
412 if ( m_listbox->HasClientUntypedData() )
413 SetClientData(m_listbox->GetClientData(m_selection));
414 EndModal(wxID_OK);
415 }
416
417 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
418 {
419 m_selection = m_listbox->GetSelection();
420 m_stringSelection = m_listbox->GetStringSelection();
421
422 if ( m_listbox->HasClientUntypedData() )
423 SetClientData(m_listbox->GetClientData(m_selection));
424
425 EndModal(wxID_OK);
426 }
427
428 // ----------------------------------------------------------------------------
429 // wxMultiChoiceDialog
430 // ----------------------------------------------------------------------------
431
432 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
433
434 bool wxMultiChoiceDialog::Create( wxWindow *parent,
435 const wxString& message,
436 const wxString& caption,
437 int n,
438 const wxString *choices,
439 long style,
440 const wxPoint& pos )
441 {
442 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
443 n, choices,
444 style, pos,
445 wxLB_ALWAYS_SB | wxLB_EXTENDED) )
446 return false;
447
448 return true;
449 }
450
451 bool wxMultiChoiceDialog::Create( wxWindow *parent,
452 const wxString& message,
453 const wxString& caption,
454 const wxArrayString& choices,
455 long style,
456 const wxPoint& pos )
457 {
458 wxCArrayString chs(choices);
459 return Create( parent, message, caption, chs.GetCount(),
460 chs.GetStrings(), style, pos );
461 }
462
463 void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
464 {
465 // first clear all currently selected items
466 size_t n,
467 count = m_listbox->GetCount();
468 for ( n = 0; n < count; ++n )
469 {
470 m_listbox->Deselect(n);
471 }
472
473 // now select the ones which should be selected
474 count = selections.GetCount();
475 for ( n = 0; n < count; n++ )
476 {
477 m_listbox->Select(selections[n]);
478 }
479 }
480
481 bool wxMultiChoiceDialog::TransferDataFromWindow()
482 {
483 m_selections.Empty();
484 size_t count = m_listbox->GetCount();
485 for ( size_t n = 0; n < count; n++ )
486 {
487 if ( m_listbox->IsSelected(n) )
488 m_selections.Add(n);
489 }
490
491 return true;
492 }
493
494 #endif // wxUSE_CHOICEDLG