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