]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicdgg.cpp
Removed duplicate WM line from motif/frame.cpp
[wxWidgets.git] / src / generic / choicdgg.cpp
CommitLineData
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
1e6feb95
VZ
31#if wxUSE_CHOICEDLG
32
c801d85f 33#ifndef WX_PRECOMP
257bf510
VZ
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"
92afa2b1 41 #include "wx/sizer.h"
dcf924a3
RR
42#endif
43
44#if wxUSE_STATLINE
c50f1fb9 45 #include "wx/statline.h"
c801d85f
KB
46#endif
47
48#include "wx/generic/choicdgg.h"
49
d6c9c1b7
VZ
50// ----------------------------------------------------------------------------
51// constants
52// ----------------------------------------------------------------------------
53
257bf510 54#define wxID_LISTBOX 3000
dcf924a3 55
d6c9c1b7
VZ
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
72static int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices);
73
74// ============================================================================
75// implementation
76// ============================================================================
77
78// ----------------------------------------------------------------------------
79// helpers
80// ----------------------------------------------------------------------------
81
82int ConvertWXArrayToC(const wxArrayString& aChoices, wxString **choices)
83{
84 int n = aChoices.GetCount();
85 *choices = new wxString[n];
54b84891 86
d6c9c1b7
VZ
87 for ( int i = 0; i < n; i++ )
88 {
ea660175 89 (*choices)[i] = aChoices[i];
d6c9c1b7
VZ
90 }
91
92 return n;
93}
94
95// ----------------------------------------------------------------------------
96// wrapper functions
97// ----------------------------------------------------------------------------
98
99wxString 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),
257bf510 105 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 106{
d427503c 107 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
3ca6a5f0 108 wxString choice;
d427503c 109 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0
BP
110 choice = dialog.GetStringSelection();
111
112 return choice;
c801d85f
KB
113}
114
d6c9c1b7
VZ
115wxString 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
1d79bd3e 132#if WXWIN_COMPATIBILITY_2
c801d85f 133// Overloaded for backward compatibility
d6c9c1b7
VZ
134wxString wxGetSingleChoice( const wxString& message,
135 const wxString& caption,
136 int n, char *choices[],
137 wxWindow *parent,
c50f1fb9 138 int x, int y, bool centre,
257bf510 139 int width, int height )
c801d85f 140{
d427503c
VZ
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;
c801d85f 151}
d6c9c1b7
VZ
152#endif // WXWIN_COMPATIBILITY_2
153
154int 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) )
c801d85f 161{
d427503c 162 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
3ca6a5f0 163 int choice;
d427503c 164 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0 165 choice = dialog.GetSelection();
d427503c 166 else
3ca6a5f0
BP
167 choice = -1;
168
169 return choice;
c801d85f
KB
170}
171
2adaf596
VS
172int 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
1d79bd3e 189#if WXWIN_COMPATIBILITY_2
c801d85f 190// Overloaded for backward compatibility
d6c9c1b7
VZ
191int 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 )
c801d85f 197{
d427503c 198 wxString *strings = new wxString[n];
dcf924a3 199 for ( int i = 0; i < n; i++)
d427503c 200 strings[i] = choices[i];
d427503c
VZ
201 int ans = wxGetSingleChoiceIndex(message, caption, n, (const wxString *)strings, parent,
202 x, y, centre, width, height);
203 delete[] strings;
204 return ans;
c801d85f 205}
d6c9c1b7
VZ
206#endif // WXWIN_COMPATIBILITY_2
207
208void *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) )
c801d85f 216{
b41ec29a
VZ
217 wxSingleChoiceDialog dialog(parent, message, caption, n, choices,
218 (char **)client_data);
3ca6a5f0 219 void *data;
d427503c 220 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0 221 data = dialog.GetSelectionClientData();
d427503c 222 else
3ca6a5f0
BP
223 data = NULL;
224
225 return data;
c801d85f
KB
226}
227
b41ec29a
VZ
228void *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
1d79bd3e 247#if WXWIN_COMPATIBILITY_2
c801d85f 248// Overloaded for backward compatibility
d6c9c1b7
VZ
249void *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 )
c801d85f 255{
d427503c
VZ
256 wxString *strings = new wxString[n];
257 int i;
258 for ( i = 0; i < n; i++)
259 {
260 strings[i] = choices[i];
261 }
d6c9c1b7
VZ
262 void *data = wxGetSingleChoiceData(message, caption,
263 n, (const wxString *)strings,
264 client_data, parent,
265 x, y, centre, width, height);
d427503c
VZ
266 delete[] strings;
267 return data;
c801d85f 268}
d6c9c1b7
VZ
269#endif // WXWIN_COMPATIBILITY_2
270
271size_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);
e93bfe3c
VZ
281
282 if ( !selections.IsEmpty() )
283 dialog.SetSelections(selections);
284
d6c9c1b7
VZ
285 if ( dialog.ShowModal() == wxID_OK )
286 selections = dialog.GetSelections();
287 else
288 selections.Empty();
c801d85f 289
d6c9c1b7
VZ
290 return selections.GetCount();
291}
c801d85f 292
59bd9598 293size_t wxGetMultipleChoices(wxArrayInt& selections,
d6c9c1b7
VZ
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)
c801d85f 301{
d6c9c1b7
VZ
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;
c801d85f 310}
c801d85f 311
d6c9c1b7
VZ
312// ----------------------------------------------------------------------------
313// wxAnyChoiceDialog
314// ----------------------------------------------------------------------------
315
316bool wxAnyChoiceDialog::Create(wxWindow *parent,
317 const wxString& message,
318 const wxString& caption,
319 int n, const wxString *choices,
ea660175 320 long styleDlg,
d6c9c1b7
VZ
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
ea660175 349 topsizer->Add( CreateButtonSizer( styleDlg & (wxOK|wxCANCEL) ), 0, wxCENTRE | wxALL, 10 );
d6c9c1b7
VZ
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// ----------------------------------------------------------------------------
c801d85f 365// wxSingleChoiceDialog
d6c9c1b7 366// ----------------------------------------------------------------------------
c801d85f 367
c801d85f 368BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
d427503c
VZ
369 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
370 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
c801d85f
KB
371END_EVENT_TABLE()
372
d6c9c1b7 373IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
257bf510
VZ
374
375wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
376 const wxString& message,
377 const wxString& caption,
c50f1fb9 378 int n,
257bf510
VZ
379 const wxString *choices,
380 char **clientData,
381 long style,
59bd9598 382 const wxPoint& WXUNUSED(pos))
c801d85f 383{
257bf510 384 Create(parent, message, caption, n, choices, clientData, style);
c801d85f
KB
385}
386
1d79bd3e 387#if WXWIN_COMPATIBILITY_2
d6c9c1b7 388
257bf510
VZ
389wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
390 const wxString& message,
391 const wxString& caption,
c50f1fb9 392 const wxStringList& choices,
b91b2200 393 char **clientData,
c50f1fb9 394 long style,
59bd9598 395 const wxPoint& WXUNUSED(pos))
c801d85f 396{
257bf510 397 Create(parent, message, caption, choices, clientData, style);
c801d85f
KB
398}
399
257bf510
VZ
400bool 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)
c801d85f 407{
d427503c
VZ
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;
c801d85f
KB
417}
418
d6c9c1b7
VZ
419#endif // WXWIN_COMPATIBILITY_2
420
421bool wxSingleChoiceDialog::Create( wxWindow *parent,
c50f1fb9 422 const wxString& message,
d6c9c1b7 423 const wxString& caption,
c50f1fb9 424 int n,
257bf510
VZ
425 const wxString *choices,
426 char **clientData,
427 long style,
d6c9c1b7 428 const wxPoint& pos )
c801d85f 429{
d6c9c1b7
VZ
430 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
431 n, choices,
432 style, pos) )
433 return FALSE;
92afa2b1 434
d6c9c1b7 435 m_selection = n > 0 ? 0 : -1;
92afa2b1 436
92afa2b1 437 if (clientData)
d427503c 438 {
257bf510
VZ
439 for (int i = 0; i < n; i++)
440 m_listbox->SetClientData(i, clientData[i]);
d427503c 441 }
c801d85f 442
d427503c 443 return TRUE;
c801d85f
KB
444}
445
ef77f91e
JS
446// Set the selection
447void wxSingleChoiceDialog::SetSelection(int sel)
448{
257bf510 449 m_listbox->SetSelection(sel);
ef77f91e
JS
450 m_selection = sel;
451}
452
c801d85f
KB
453void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
454{
257bf510
VZ
455 m_selection = m_listbox->GetSelection();
456 m_stringSelection = m_listbox->GetStringSelection();
25f47127
JS
457 // TODO!
458#ifndef __WXMOTIF__
eb553cb2
VZ
459 if ( m_listbox->HasClientUntypedData() )
460 SetClientData(m_listbox->GetClientData(m_selection));
25f47127 461#endif
d427503c 462 EndModal(wxID_OK);
c801d85f
KB
463}
464
debe6624
JS
465void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
466{
257bf510
VZ
467 m_selection = m_listbox->GetSelection();
468 m_stringSelection = m_listbox->GetStringSelection();
25f47127
JS
469
470 // TODO!
471#ifndef __WXMOTIF__
eb553cb2
VZ
472 if ( m_listbox->HasClientUntypedData() )
473 SetClientData(m_listbox->GetClientData(m_selection));
25f47127 474#endif
d427503c
VZ
475
476 EndModal(wxID_OK);
debe6624
JS
477}
478
d6c9c1b7
VZ
479// ----------------------------------------------------------------------------
480// wxMultiChoiceDialog
481// ----------------------------------------------------------------------------
482
d6c9c1b7
VZ
483IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
484
485bool 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,
3d49ce44 496 wxLB_ALWAYS_SB | wxLB_EXTENDED) )
d6c9c1b7
VZ
497 return FALSE;
498
499 return TRUE;
500}
501
502void 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
3d49ce44 511bool wxMultiChoiceDialog::TransferDataFromWindow()
d6c9c1b7 512{
51a9ecbc
VZ
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
d6c9c1b7
VZ
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
3d49ce44 526 return TRUE;
d6c9c1b7 527}
1e6feb95
VZ
528
529#endif // wxUSE_CHOICEDLG