]> git.saurik.com Git - wxWidgets.git/blob - src/generic/choicdgg.cpp
added missing -univ package control files.
[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) wxWindows team
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 #ifdef __GNUG__
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 #endif
43
44 #if wxUSE_STATLINE
45 #include "wx/statline.h"
46 #endif
47
48 #include "wx/generic/choicdgg.h"
49
50 // ----------------------------------------------------------------------------
51 // constants
52 // ----------------------------------------------------------------------------
53
54 #define wxID_LISTBOX 3000
55
56 #if defined(__WXMSW__) || defined(__WXMAC__)
57 #define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
58 wxDIALOG_MODAL | \
59 wxTAB_TRAVERSAL)
60 #else
61 #define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
62 wxDIALOG_MODAL | \
63 wxRESIZE_BORDER | \
64 wxTAB_TRAVERSAL)
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 #if WXWIN_COMPATIBILITY_2
133 // Overloaded for backward compatibility
134 wxString wxGetSingleChoice( const wxString& message,
135 const wxString& caption,
136 int n, char *choices[],
137 wxWindow *parent,
138 int x, int y, bool centre,
139 int width, int height )
140 {
141 wxString *strings = new wxString[n];
142 int i;
143 for ( i = 0; i < n; i++)
144 {
145 strings[i] = choices[i];
146 }
147 wxString ans(wxGetSingleChoice(message, caption, n, (const wxString *)strings, parent,
148 x, y, centre, width, height));
149 delete[] strings;
150 return ans;
151 }
152 #endif // WXWIN_COMPATIBILITY_2
153
154 int wxGetSingleChoiceIndex( const wxString& message,
155 const wxString& caption,
156 int n, const wxString *choices,
157 wxWindow *parent,
158 int WXUNUSED(x), int WXUNUSED(y),
159 bool WXUNUSED(centre),
160 int WXUNUSED(width), int WXUNUSED(height) )
161 {
162 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
163 int choice;
164 if ( dialog.ShowModal() == wxID_OK )
165 choice = dialog.GetSelection();
166 else
167 choice = -1;
168
169 return choice;
170 }
171
172 int wxGetSingleChoiceIndex( const wxString& message,
173 const wxString& caption,
174 const wxArrayString& aChoices,
175 wxWindow *parent,
176 int x, int y,
177 bool centre,
178 int width, int height)
179 {
180 wxString *choices;
181 int n = ConvertWXArrayToC(aChoices, &choices);
182 int res = wxGetSingleChoiceIndex(message, caption, n, choices, parent,
183 x, y, centre, width, height);
184 delete [] choices;
185
186 return res;
187 }
188
189 #if WXWIN_COMPATIBILITY_2
190 // Overloaded for backward compatibility
191 int wxGetSingleChoiceIndex( const wxString& message,
192 const wxString& caption,
193 int n, wxChar *choices[],
194 wxWindow *parent,
195 int x, int y, bool centre,
196 int width, int height )
197 {
198 wxString *strings = new wxString[n];
199 for ( int i = 0; i < n; i++)
200 strings[i] = choices[i];
201 int ans = wxGetSingleChoiceIndex(message, caption, n, (const wxString *)strings, parent,
202 x, y, centre, width, height);
203 delete[] strings;
204 return ans;
205 }
206 #endif // WXWIN_COMPATIBILITY_2
207
208 void *wxGetSingleChoiceData( const wxString& message,
209 const wxString& caption,
210 int n, const wxString *choices,
211 void **client_data,
212 wxWindow *parent,
213 int WXUNUSED(x), int WXUNUSED(y),
214 bool WXUNUSED(centre),
215 int WXUNUSED(width), int WXUNUSED(height) )
216 {
217 wxSingleChoiceDialog dialog(parent, message, caption, n, choices,
218 (char **)client_data);
219 void *data;
220 if ( dialog.ShowModal() == wxID_OK )
221 data = dialog.GetSelectionClientData();
222 else
223 data = NULL;
224
225 return data;
226 }
227
228 void *wxGetSingleChoiceData( const wxString& message,
229 const wxString& caption,
230 const wxArrayString& aChoices,
231 void **client_data,
232 wxWindow *parent,
233 int x, int y,
234 bool centre,
235 int width, int height)
236 {
237 wxString *choices;
238 int n = ConvertWXArrayToC(aChoices, &choices);
239 void *res = wxGetSingleChoiceData(message, caption, n, choices,
240 client_data, parent,
241 x, y, centre, width, height);
242 delete [] choices;
243
244 return res;
245 }
246
247 #if WXWIN_COMPATIBILITY_2
248 // Overloaded for backward compatibility
249 void *wxGetSingleChoiceData( const wxString& message,
250 const wxString& caption,
251 int n, wxChar *choices[],
252 void **client_data,
253 wxWindow *parent,
254 int x, int y, bool centre, int width, int height )
255 {
256 wxString *strings = new wxString[n];
257 int i;
258 for ( i = 0; i < n; i++)
259 {
260 strings[i] = choices[i];
261 }
262 void *data = wxGetSingleChoiceData(message, caption,
263 n, (const wxString *)strings,
264 client_data, parent,
265 x, y, centre, width, height);
266 delete[] strings;
267 return data;
268 }
269 #endif // WXWIN_COMPATIBILITY_2
270
271 size_t wxGetMultipleChoices(wxArrayInt& selections,
272 const wxString& message,
273 const wxString& caption,
274 int n, const wxString *choices,
275 wxWindow *parent,
276 int WXUNUSED(x), int WXUNUSED(y),
277 bool WXUNUSED(centre),
278 int WXUNUSED(width), int WXUNUSED(height))
279 {
280 wxMultiChoiceDialog dialog(parent, message, caption, n, choices);
281
282 if ( !selections.IsEmpty() )
283 dialog.SetSelections(selections);
284
285 if ( dialog.ShowModal() == wxID_OK )
286 selections = dialog.GetSelections();
287 else
288 selections.Empty();
289
290 return selections.GetCount();
291 }
292
293 size_t wxGetMultipleChoices(wxArrayInt& selections,
294 const wxString& message,
295 const wxString& caption,
296 const wxArrayString& aChoices,
297 wxWindow *parent,
298 int x, int y,
299 bool centre,
300 int width, int height)
301 {
302 wxString *choices;
303 int n = ConvertWXArrayToC(aChoices, &choices);
304 size_t res = wxGetMultipleChoices(selections, message, caption,
305 n, choices, parent,
306 x, y, centre, width, height);
307 delete [] choices;
308
309 return res;
310 }
311
312 // ----------------------------------------------------------------------------
313 // wxAnyChoiceDialog
314 // ----------------------------------------------------------------------------
315
316 bool wxAnyChoiceDialog::Create(wxWindow *parent,
317 const wxString& message,
318 const wxString& caption,
319 int n, const wxString *choices,
320 long WXUNUSED(styleDlg), // FIXME: why unused?
321 const wxPoint& pos,
322 long styleLbox)
323 {
324 if ( !wxDialog::Create(parent, -1, caption, pos, wxDefaultSize,
325 wxCHOICEDLG_DIALOG_STYLE) )
326 return FALSE;
327
328 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
329
330 // 1) text message
331 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
332
333 // 2) list box
334 m_listbox = new wxListBox( this, wxID_LISTBOX,
335 wxDefaultPosition, wxDefaultSize,
336 n, choices,
337 styleLbox );
338 if ( n > 0 )
339 m_listbox->SetSelection(0);
340
341 topsizer->Add( m_listbox, 1, wxEXPAND | wxLEFT|wxRIGHT, 15 );
342
343 #if wxUSE_STATLINE
344 // 3) static line
345 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
346 #endif
347
348 // 4) buttons
349 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxCENTRE | wxALL, 10 );
350
351 SetAutoLayout( TRUE );
352 SetSizer( topsizer );
353
354 topsizer->SetSizeHints( this );
355 topsizer->Fit( this );
356
357 Centre( wxBOTH );
358
359 m_listbox->SetFocus();
360
361 return TRUE;
362 }
363
364 // ----------------------------------------------------------------------------
365 // wxSingleChoiceDialog
366 // ----------------------------------------------------------------------------
367
368 BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
369 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
370 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
371 END_EVENT_TABLE()
372
373 IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
374
375 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
376 const wxString& message,
377 const wxString& caption,
378 int n,
379 const wxString *choices,
380 char **clientData,
381 long style,
382 const wxPoint& WXUNUSED(pos))
383 {
384 Create(parent, message, caption, n, choices, clientData, style);
385 }
386
387 #if WXWIN_COMPATIBILITY_2
388
389 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
390 const wxString& message,
391 const wxString& caption,
392 const wxStringList& choices,
393 char **clientData,
394 long style,
395 const wxPoint& WXUNUSED(pos))
396 {
397 Create(parent, message, caption, choices, clientData, style);
398 }
399
400 bool wxSingleChoiceDialog::Create(wxWindow *parent,
401 const wxString& message,
402 const wxString& caption,
403 const wxStringList& choices,
404 char **clientData,
405 long style,
406 const wxPoint& pos)
407 {
408 wxString *strings = new wxString[choices.Number()];
409 int i;
410 for ( i = 0; i < choices.Number(); i++)
411 {
412 strings[i] = (char *)choices.Nth(i)->Data();
413 }
414 bool ans = Create(parent, message, caption, choices.Number(), strings, clientData, style, pos);
415 delete[] strings;
416 return ans;
417 }
418
419 #endif // WXWIN_COMPATIBILITY_2
420
421 bool wxSingleChoiceDialog::Create( wxWindow *parent,
422 const wxString& message,
423 const wxString& caption,
424 int n,
425 const wxString *choices,
426 char **clientData,
427 long style,
428 const wxPoint& pos )
429 {
430 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
431 n, choices,
432 style, pos) )
433 return FALSE;
434
435 m_selection = n > 0 ? 0 : -1;
436
437 if (clientData)
438 {
439 for (int i = 0; i < n; i++)
440 m_listbox->SetClientData(i, clientData[i]);
441 }
442
443 return TRUE;
444 }
445
446 // Set the selection
447 void wxSingleChoiceDialog::SetSelection(int sel)
448 {
449 m_listbox->SetSelection(sel);
450 m_selection = sel;
451 }
452
453 void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
454 {
455 m_selection = m_listbox->GetSelection();
456 m_stringSelection = m_listbox->GetStringSelection();
457 // TODO!
458 #ifndef __WXMOTIF__
459 if ( m_listbox->HasClientUntypedData() )
460 SetClientData(m_listbox->GetClientData(m_selection));
461 #endif
462 EndModal(wxID_OK);
463 }
464
465 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
466 {
467 m_selection = m_listbox->GetSelection();
468 m_stringSelection = m_listbox->GetStringSelection();
469
470 // TODO!
471 #ifndef __WXMOTIF__
472 if ( m_listbox->HasClientUntypedData() )
473 SetClientData(m_listbox->GetClientData(m_selection));
474 #endif
475
476 EndModal(wxID_OK);
477 }
478
479 // ----------------------------------------------------------------------------
480 // wxMultiChoiceDialog
481 // ----------------------------------------------------------------------------
482
483 IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
484
485 bool wxMultiChoiceDialog::Create( wxWindow *parent,
486 const wxString& message,
487 const wxString& caption,
488 int n,
489 const wxString *choices,
490 long style,
491 const wxPoint& pos )
492 {
493 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
494 n, choices,
495 style, pos,
496 wxLB_ALWAYS_SB | wxLB_EXTENDED) )
497 return FALSE;
498
499 return TRUE;
500 }
501
502 void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
503 {
504 size_t count = selections.GetCount();
505 for ( size_t n = 0; n < count; n++ )
506 {
507 m_listbox->Select(selections[n]);
508 }
509 }
510
511 bool wxMultiChoiceDialog::TransferDataFromWindow()
512 {
513 // VZ: I hate to do it but I can't fix wxMotif right now (FIXME)
514 #ifdef __WXMOTIF__
515 #define IsSelected Selected
516 #endif
517
518 m_selections.Empty();
519 size_t count = m_listbox->GetCount();
520 for ( size_t n = 0; n < count; n++ )
521 {
522 if ( m_listbox->IsSelected(n) )
523 m_selections.Add(n);
524 }
525
526 return TRUE;
527 }
528
529 #endif // wxUSE_CHOICEDLG