]>
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 | |
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 | #ifdef __SMARTPHONE__ | |
282 | ||
283 | SetRightMenu(wxID_CANCEL, _("Cancel")); | |
284 | ||
285 | #else // __SMARTPHONE__/!__SMARTPHONE__ | |
286 | ||
287 | #if wxUSE_STATLINE | |
288 | // 3) static line | |
289 | topsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 ); | |
290 | #endif | |
291 | ||
292 | // 4) buttons | |
293 | topsizer->Add( CreateButtonSizer( styleDlg & (wxOK|wxCANCEL) ), 0, wxCENTRE | wxALL, 10 ); | |
294 | ||
295 | #endif // !__SMARTPHONE__ | |
296 | ||
297 | SetAutoLayout( true ); | |
298 | SetSizer( topsizer ); | |
299 | ||
300 | topsizer->SetSizeHints( this ); | |
301 | topsizer->Fit( this ); | |
302 | ||
303 | if ( styleDlg & wxCENTRE ) | |
304 | Centre(wxBOTH); | |
305 | ||
306 | m_listbox->SetFocus(); | |
307 | ||
308 | return true; | |
309 | } | |
310 | ||
311 | bool wxAnyChoiceDialog::Create(wxWindow *parent, | |
312 | const wxString& message, | |
313 | const wxString& caption, | |
314 | const wxArrayString& choices, | |
315 | long styleDlg, | |
316 | const wxPoint& pos, | |
317 | long styleLbox) | |
318 | { | |
319 | wxCArrayString chs(choices); | |
320 | return Create(parent, message, caption, chs.GetCount(), chs.GetStrings(), | |
321 | styleDlg, pos, styleLbox); | |
322 | } | |
323 | ||
324 | // ---------------------------------------------------------------------------- | |
325 | // wxSingleChoiceDialog | |
326 | // ---------------------------------------------------------------------------- | |
327 | ||
328 | BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog) | |
329 | EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK) | |
330 | EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick) | |
331 | END_EVENT_TABLE() | |
332 | ||
333 | IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog) | |
334 | ||
335 | wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent, | |
336 | const wxString& message, | |
337 | const wxString& caption, | |
338 | int n, | |
339 | const wxString *choices, | |
340 | char **clientData, | |
341 | long style, | |
342 | const wxPoint& WXUNUSED(pos)) | |
343 | { | |
344 | Create(parent, message, caption, n, choices, clientData, style); | |
345 | } | |
346 | ||
347 | wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent, | |
348 | const wxString& message, | |
349 | const wxString& caption, | |
350 | const wxArrayString& choices, | |
351 | char **clientData, | |
352 | long style, | |
353 | const wxPoint& WXUNUSED(pos)) | |
354 | { | |
355 | Create(parent, message, caption, choices, clientData, style); | |
356 | } | |
357 | ||
358 | bool wxSingleChoiceDialog::Create( wxWindow *parent, | |
359 | const wxString& message, | |
360 | const wxString& caption, | |
361 | int n, | |
362 | const wxString *choices, | |
363 | char **clientData, | |
364 | long style, | |
365 | const wxPoint& pos ) | |
366 | { | |
367 | if ( !wxAnyChoiceDialog::Create(parent, message, caption, | |
368 | n, choices, | |
369 | style, pos) ) | |
370 | return false; | |
371 | ||
372 | m_selection = n > 0 ? 0 : -1; | |
373 | ||
374 | if (clientData) | |
375 | { | |
376 | for (int i = 0; i < n; i++) | |
377 | m_listbox->SetClientData(i, clientData[i]); | |
378 | } | |
379 | ||
380 | return true; | |
381 | } | |
382 | ||
383 | bool wxSingleChoiceDialog::Create( wxWindow *parent, | |
384 | const wxString& message, | |
385 | const wxString& caption, | |
386 | const wxArrayString& choices, | |
387 | char **clientData, | |
388 | long style, | |
389 | const wxPoint& pos ) | |
390 | { | |
391 | wxCArrayString chs(choices); | |
392 | return Create( parent, message, caption, chs.GetCount(), chs.GetStrings(), | |
393 | clientData, style, pos ); | |
394 | } | |
395 | ||
396 | // Set the selection | |
397 | void wxSingleChoiceDialog::SetSelection(int sel) | |
398 | { | |
399 | m_listbox->SetSelection(sel); | |
400 | m_selection = sel; | |
401 | } | |
402 | ||
403 | void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event)) | |
404 | { | |
405 | m_selection = m_listbox->GetSelection(); | |
406 | m_stringSelection = m_listbox->GetStringSelection(); | |
407 | if ( m_listbox->HasClientUntypedData() ) | |
408 | SetClientData(m_listbox->GetClientData(m_selection)); | |
409 | EndModal(wxID_OK); | |
410 | } | |
411 | ||
412 | void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event)) | |
413 | { | |
414 | m_selection = m_listbox->GetSelection(); | |
415 | m_stringSelection = m_listbox->GetStringSelection(); | |
416 | ||
417 | if ( m_listbox->HasClientUntypedData() ) | |
418 | SetClientData(m_listbox->GetClientData(m_selection)); | |
419 | ||
420 | EndModal(wxID_OK); | |
421 | } | |
422 | ||
423 | // ---------------------------------------------------------------------------- | |
424 | // wxMultiChoiceDialog | |
425 | // ---------------------------------------------------------------------------- | |
426 | ||
427 | IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog) | |
428 | ||
429 | bool wxMultiChoiceDialog::Create( wxWindow *parent, | |
430 | const wxString& message, | |
431 | const wxString& caption, | |
432 | int n, | |
433 | const wxString *choices, | |
434 | long style, | |
435 | const wxPoint& pos ) | |
436 | { | |
437 | if ( !wxAnyChoiceDialog::Create(parent, message, caption, | |
438 | n, choices, | |
439 | style, pos, | |
440 | wxLB_ALWAYS_SB | wxLB_EXTENDED) ) | |
441 | return false; | |
442 | ||
443 | return true; | |
444 | } | |
445 | ||
446 | bool wxMultiChoiceDialog::Create( wxWindow *parent, | |
447 | const wxString& message, | |
448 | const wxString& caption, | |
449 | const wxArrayString& choices, | |
450 | long style, | |
451 | const wxPoint& pos ) | |
452 | { | |
453 | wxCArrayString chs(choices); | |
454 | return Create( parent, message, caption, chs.GetCount(), | |
455 | chs.GetStrings(), style, pos ); | |
456 | } | |
457 | ||
458 | void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections) | |
459 | { | |
460 | // first clear all currently selected items | |
461 | size_t n, | |
462 | count = m_listbox->GetCount(); | |
463 | for ( n = 0; n < count; ++n ) | |
464 | { | |
465 | m_listbox->Deselect(n); | |
466 | } | |
467 | ||
468 | // now select the ones which should be selected | |
469 | count = selections.GetCount(); | |
470 | for ( n = 0; n < count; n++ ) | |
471 | { | |
472 | m_listbox->Select(selections[n]); | |
473 | } | |
474 | } | |
475 | ||
476 | bool wxMultiChoiceDialog::TransferDataFromWindow() | |
477 | { | |
478 | m_selections.Empty(); | |
479 | size_t count = m_listbox->GetCount(); | |
480 | for ( size_t n = 0; n < count; n++ ) | |
481 | { | |
482 | if ( m_listbox->IsSelected(n) ) | |
483 | m_selections.Add(n); | |
484 | } | |
485 | ||
486 | return true; | |
487 | } | |
488 | ||
489 | #endif // wxUSE_CHOICEDLG |