]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicdgg.cpp
wxMGL revitalised with OpenWatcom.
[wxWidgets.git] / src / generic / choicdgg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
7b504551 2// Name: src/generic/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$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
d6c9c1b7
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
d6c9c1b7
VZ
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c801d85f
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
d427503c 24 #pragma hdrstop
c801d85f
KB
25#endif
26
1e6feb95
VZ
27#if wxUSE_CHOICEDLG
28
c801d85f 29#ifndef WX_PRECOMP
257bf510
VZ
30 #include <stdio.h>
31 #include "wx/utils.h"
32 #include "wx/dialog.h"
33 #include "wx/button.h"
34 #include "wx/listbox.h"
35 #include "wx/stattext.h"
36 #include "wx/intl.h"
92afa2b1 37 #include "wx/sizer.h"
2da2f941 38 #include "wx/arrstr.h"
dcf924a3
RR
39#endif
40
41#if wxUSE_STATLINE
c50f1fb9 42 #include "wx/statline.h"
c801d85f
KB
43#endif
44
45#include "wx/generic/choicdgg.h"
46
d6c9c1b7
VZ
47// ----------------------------------------------------------------------------
48// constants
49// ----------------------------------------------------------------------------
50
257bf510 51#define wxID_LISTBOX 3000
dcf924a3 52
4b088139
WS
53// ---------------------------------------------------------------------------
54// macros
55// ---------------------------------------------------------------------------
56
57/* Macro for avoiding #ifdefs when value have to be different depending on size of
9a357011 58 device we display on - take it from something like wxDesktopPolicy in the future
4b088139
WS
59 */
60
61#if defined(__SMARTPHONE__)
62 #define wxLARGESMALL(large,small) small
63#else
64 #define wxLARGESMALL(large,small) large
65#endif
66
d6c9c1b7
VZ
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
d6c9c1b7
VZ
132int wxGetSingleChoiceIndex( const wxString& message,
133 const wxString& caption,
134 int n, const wxString *choices,
135 wxWindow *parent,
136 int WXUNUSED(x), int WXUNUSED(y),
137 bool WXUNUSED(centre),
138 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 139{
d427503c 140 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
3ca6a5f0 141 int choice;
d427503c 142 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0 143 choice = dialog.GetSelection();
d427503c 144 else
3ca6a5f0
BP
145 choice = -1;
146
147 return choice;
c801d85f
KB
148}
149
2adaf596
VS
150int wxGetSingleChoiceIndex( const wxString& message,
151 const wxString& caption,
152 const wxArrayString& aChoices,
153 wxWindow *parent,
154 int x, int y,
155 bool centre,
156 int width, int height)
157{
158 wxString *choices;
159 int n = ConvertWXArrayToC(aChoices, &choices);
160 int res = wxGetSingleChoiceIndex(message, caption, n, choices, parent,
161 x, y, centre, width, height);
162 delete [] choices;
163
164 return res;
165}
166
d6c9c1b7
VZ
167void *wxGetSingleChoiceData( const wxString& message,
168 const wxString& caption,
169 int n, const wxString *choices,
170 void **client_data,
171 wxWindow *parent,
172 int WXUNUSED(x), int WXUNUSED(y),
173 bool WXUNUSED(centre),
174 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 175{
b41ec29a
VZ
176 wxSingleChoiceDialog dialog(parent, message, caption, n, choices,
177 (char **)client_data);
3ca6a5f0 178 void *data;
d427503c 179 if ( dialog.ShowModal() == wxID_OK )
3ca6a5f0 180 data = dialog.GetSelectionClientData();
d427503c 181 else
3ca6a5f0
BP
182 data = NULL;
183
184 return data;
c801d85f
KB
185}
186
b41ec29a
VZ
187void *wxGetSingleChoiceData( const wxString& message,
188 const wxString& caption,
189 const wxArrayString& aChoices,
190 void **client_data,
191 wxWindow *parent,
192 int x, int y,
193 bool centre,
194 int width, int height)
195{
196 wxString *choices;
197 int n = ConvertWXArrayToC(aChoices, &choices);
198 void *res = wxGetSingleChoiceData(message, caption, n, choices,
199 client_data, parent,
200 x, y, centre, width, height);
201 delete [] choices;
202
203 return res;
204}
205
d6c9c1b7
VZ
206size_t wxGetMultipleChoices(wxArrayInt& selections,
207 const wxString& message,
208 const wxString& caption,
209 int n, const wxString *choices,
210 wxWindow *parent,
211 int WXUNUSED(x), int WXUNUSED(y),
212 bool WXUNUSED(centre),
213 int WXUNUSED(width), int WXUNUSED(height))
214{
215 wxMultiChoiceDialog dialog(parent, message, caption, n, choices);
e93bfe3c 216
b2d739ba
VZ
217 // call this even if selections array is empty and this then (correctly)
218 // deselects the first item which is selected by default
219 dialog.SetSelections(selections);
e93bfe3c 220
d6c9c1b7
VZ
221 if ( dialog.ShowModal() == wxID_OK )
222 selections = dialog.GetSelections();
223 else
224 selections.Empty();
c801d85f 225
d6c9c1b7
VZ
226 return selections.GetCount();
227}
c801d85f 228
59bd9598 229size_t wxGetMultipleChoices(wxArrayInt& selections,
d6c9c1b7
VZ
230 const wxString& message,
231 const wxString& caption,
232 const wxArrayString& aChoices,
233 wxWindow *parent,
234 int x, int y,
235 bool centre,
236 int width, int height)
c801d85f 237{
d6c9c1b7
VZ
238 wxString *choices;
239 int n = ConvertWXArrayToC(aChoices, &choices);
240 size_t res = wxGetMultipleChoices(selections, message, caption,
241 n, choices, parent,
242 x, y, centre, width, height);
243 delete [] choices;
244
245 return res;
c801d85f 246}
c801d85f 247
d6c9c1b7
VZ
248// ----------------------------------------------------------------------------
249// wxAnyChoiceDialog
250// ----------------------------------------------------------------------------
251
252bool wxAnyChoiceDialog::Create(wxWindow *parent,
253 const wxString& message,
254 const wxString& caption,
255 int n, const wxString *choices,
ea660175 256 long styleDlg,
d6c9c1b7
VZ
257 const wxPoint& pos,
258 long styleLbox)
259{
aa66250b
JS
260#if defined(__SMARTPHONE__) || defined(__POCKETPC__)
261 styleDlg &= ~wxBORDER_MASK;
262 styleDlg &= ~wxRESIZE_BORDER;
263 styleDlg &= ~wxCAPTION;
264#endif
265
ca65c044
WS
266 if ( !wxDialog::Create(parent, wxID_ANY, caption, pos, wxDefaultSize, styleDlg) )
267 return false;
d6c9c1b7
VZ
268
269 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
270
64c794f6 271 // 1) text message
718970e5 272 topsizer->Add( CreateTextSizer( message ), 0, wxALL, wxLARGESMALL(10,0) );
64c794f6
WS
273
274 // 2) list box
275 m_listbox = new wxListBox( this, wxID_LISTBOX,
276 wxDefaultPosition, wxDefaultSize,
277 n, choices,
278 styleLbox );
279 if ( n > 0 )
280 m_listbox->SetSelection(0);
281
aa66250b 282 topsizer->Add( m_listbox, 1, wxEXPAND|wxLEFT|wxRIGHT, wxLARGESMALL(15,0) );
119727ad 283
9a357011 284 // smart phones does not support or do not waste space for wxButtons
119727ad 285#ifdef __SMARTPHONE__
64c794f6
WS
286
287 SetRightMenu(wxID_CANCEL, _("Cancel"));
288
289#else // __SMARTPHONE__/!__SMARTPHONE__
290
d6c9c1b7
VZ
291#if wxUSE_STATLINE
292 // 3) static line
ca65c044 293 topsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
d6c9c1b7
VZ
294#endif
295
296 // 4) buttons
acf2ac37 297 topsizer->Add( CreateButtonSizer( styleDlg & (wxOK|wxCANCEL) ), 0, wxEXPAND | wxALL, 10 );
d6c9c1b7 298
64c794f6
WS
299#endif // !__SMARTPHONE__
300
d6c9c1b7
VZ
301 SetSizer( topsizer );
302
aa66250b 303#if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
d6c9c1b7
VZ
304 topsizer->SetSizeHints( this );
305 topsizer->Fit( this );
306
8316ff5d
VS
307 if ( styleDlg & wxCENTRE )
308 Centre(wxBOTH);
aa66250b 309#endif
d6c9c1b7
VZ
310
311 m_listbox->SetFocus();
312
ca65c044 313 return true;
d6c9c1b7
VZ
314}
315
584ad2a3
MB
316bool wxAnyChoiceDialog::Create(wxWindow *parent,
317 const wxString& message,
318 const wxString& caption,
319 const wxArrayString& choices,
320 long styleDlg,
321 const wxPoint& pos,
322 long styleLbox)
323{
324 wxCArrayString chs(choices);
325 return Create(parent, message, caption, chs.GetCount(), chs.GetStrings(),
326 styleDlg, pos, styleLbox);
327}
328
d6c9c1b7 329// ----------------------------------------------------------------------------
c801d85f 330// wxSingleChoiceDialog
d6c9c1b7 331// ----------------------------------------------------------------------------
c801d85f 332
c801d85f 333BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
d427503c 334 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
7b504551 335#ifndef __SMARTPHONE__
d427503c 336 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
7b504551
WS
337#endif
338#ifdef __WXWINCE__
339 EVT_JOY_BUTTON_DOWN(wxSingleChoiceDialog::OnJoystickButtonDown)
340#endif
c801d85f
KB
341END_EVENT_TABLE()
342
d6c9c1b7 343IMPLEMENT_DYNAMIC_CLASS(wxSingleChoiceDialog, wxDialog)
257bf510
VZ
344
345wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
346 const wxString& message,
347 const wxString& caption,
c50f1fb9 348 int n,
257bf510
VZ
349 const wxString *choices,
350 char **clientData,
351 long style,
59bd9598 352 const wxPoint& WXUNUSED(pos))
c801d85f 353{
257bf510 354 Create(parent, message, caption, n, choices, clientData, style);
c801d85f
KB
355}
356
584ad2a3
MB
357wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
358 const wxString& message,
359 const wxString& caption,
360 const wxArrayString& choices,
361 char **clientData,
362 long style,
363 const wxPoint& WXUNUSED(pos))
364{
365 Create(parent, message, caption, choices, clientData, style);
366}
367
d6c9c1b7 368bool wxSingleChoiceDialog::Create( wxWindow *parent,
c50f1fb9 369 const wxString& message,
d6c9c1b7 370 const wxString& caption,
c50f1fb9 371 int n,
257bf510
VZ
372 const wxString *choices,
373 char **clientData,
374 long style,
d6c9c1b7 375 const wxPoint& pos )
c801d85f 376{
d6c9c1b7
VZ
377 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
378 n, choices,
379 style, pos) )
ca65c044 380 return false;
92afa2b1 381
d6c9c1b7 382 m_selection = n > 0 ? 0 : -1;
92afa2b1 383
92afa2b1 384 if (clientData)
d427503c 385 {
257bf510
VZ
386 for (int i = 0; i < n; i++)
387 m_listbox->SetClientData(i, clientData[i]);
d427503c 388 }
c801d85f 389
ca65c044 390 return true;
c801d85f
KB
391}
392
584ad2a3
MB
393bool wxSingleChoiceDialog::Create( wxWindow *parent,
394 const wxString& message,
395 const wxString& caption,
396 const wxArrayString& choices,
397 char **clientData,
398 long style,
399 const wxPoint& pos )
400{
401 wxCArrayString chs(choices);
402 return Create( parent, message, caption, chs.GetCount(), chs.GetStrings(),
403 clientData, style, pos );
404}
405
ef77f91e
JS
406// Set the selection
407void wxSingleChoiceDialog::SetSelection(int sel)
408{
257bf510 409 m_listbox->SetSelection(sel);
ef77f91e
JS
410 m_selection = sel;
411}
412
c801d85f
KB
413void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
414{
7b504551 415 DoChoice();
c801d85f
KB
416}
417
7b504551 418#ifndef __SMARTPHONE__
debe6624 419void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
7b504551
WS
420{
421 DoChoice();
422}
423#endif
424
425#ifdef __WXWINCE__
426void wxSingleChoiceDialog::OnJoystickButtonDown(wxJoystickEvent& WXUNUSED(event))
427{
428 DoChoice();
429}
430#endif
431
432void wxSingleChoiceDialog::DoChoice()
debe6624 433{
257bf510
VZ
434 m_selection = m_listbox->GetSelection();
435 m_stringSelection = m_listbox->GetStringSelection();
25f47127 436
eb553cb2 437 if ( m_listbox->HasClientUntypedData() )
59af5f19 438 SetClientData(m_listbox->GetClientData(m_selection));
d427503c
VZ
439
440 EndModal(wxID_OK);
debe6624
JS
441}
442
d6c9c1b7
VZ
443// ----------------------------------------------------------------------------
444// wxMultiChoiceDialog
445// ----------------------------------------------------------------------------
446
d6c9c1b7
VZ
447IMPLEMENT_DYNAMIC_CLASS(wxMultiChoiceDialog, wxDialog)
448
449bool wxMultiChoiceDialog::Create( wxWindow *parent,
450 const wxString& message,
451 const wxString& caption,
452 int n,
453 const wxString *choices,
454 long style,
455 const wxPoint& pos )
456{
457 if ( !wxAnyChoiceDialog::Create(parent, message, caption,
458 n, choices,
459 style, pos,
3d49ce44 460 wxLB_ALWAYS_SB | wxLB_EXTENDED) )
ca65c044 461 return false;
d6c9c1b7 462
ca65c044 463 return true;
d6c9c1b7
VZ
464}
465
584ad2a3
MB
466bool wxMultiChoiceDialog::Create( wxWindow *parent,
467 const wxString& message,
468 const wxString& caption,
469 const wxArrayString& choices,
470 long style,
471 const wxPoint& pos )
472{
473 wxCArrayString chs(choices);
474 return Create( parent, message, caption, chs.GetCount(),
475 chs.GetStrings(), style, pos );
476}
477
d6c9c1b7
VZ
478void wxMultiChoiceDialog::SetSelections(const wxArrayInt& selections)
479{
d0cc483d
VZ
480 // first clear all currently selected items
481 size_t n,
482 count = m_listbox->GetCount();
483 for ( n = 0; n < count; ++n )
484 {
485 m_listbox->Deselect(n);
486 }
487
488 // now select the ones which should be selected
489 count = selections.GetCount();
490 for ( n = 0; n < count; n++ )
d6c9c1b7
VZ
491 {
492 m_listbox->Select(selections[n]);
493 }
494}
495
3d49ce44 496bool wxMultiChoiceDialog::TransferDataFromWindow()
d6c9c1b7
VZ
497{
498 m_selections.Empty();
499 size_t count = m_listbox->GetCount();
500 for ( size_t n = 0; n < count; n++ )
501 {
502 if ( m_listbox->IsSelected(n) )
503 m_selections.Add(n);
504 }
505
ca65c044 506 return true;
d6c9c1b7 507}
1e6feb95
VZ
508
509#endif // wxUSE_CHOICEDLG