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