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