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