]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicdgg.cpp
Could of Unicode fixes.
[wxWidgets.git] / src / generic / choicdgg.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
dfad0599 2// Name: choicdgg.cpp
c801d85f
KB
3// Purpose: Choice dialogs
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
d427503c 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
d427503c 13 #pragma implementation "choicdgg.h"
c801d85f
KB
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
d427503c 20 #pragma hdrstop
c801d85f
KB
21#endif
22
d427503c
VZ
23#if wxUSE_CONSTRAINTS
24
c801d85f 25#ifndef WX_PRECOMP
d427503c
VZ
26 #include <stdio.h>
27 #include "wx/utils.h"
28 #include "wx/dialog.h"
29 #include "wx/listbox.h"
30 #include "wx/button.h"
31 #include "wx/stattext.h"
32 #include "wx/layout.h"
33 #include "wx/intl.h"
c801d85f
KB
34#endif
35
36#include "wx/generic/choicdgg.h"
37
dfad0599 38// Split message, using constraints to position controls
87138c52 39static void wxSplitMessage2(const wxChar *message, wxList *messageList, wxWindow *parent, wxRowColSizer *sizer)
dfad0599 40{
87138c52 41 wxChar *copyMessage = copystring(message);
dfad0599 42 size_t i = 0;
87138c52
OK
43 size_t len = wxStrlen(copyMessage);
44 wxChar *currentMessage = copyMessage;
dfad0599
JS
45
46// wxWindow *lastWindow = parent;
47
48 while (i < len) {
87138c52 49 while ((i < len) && (copyMessage[i] != _T('\n'))) i++;
dfad0599
JS
50 if (i < len) copyMessage[i] = 0;
51 wxStaticText *mess = new wxStaticText(parent, -1, currentMessage);
52
53/*
54 wxLayoutConstraints *c = new wxLayoutConstraints;
d427503c
VZ
55 c->left.SameAs (parent, wxLeft, 10);
56 c->top.SameAs (lastWindow, wxBottom, 5);
57 c->right.AsIs ();
58 c->height.AsIs ();
dfad0599
JS
59
60 mess->SetConstraints(c);
61*/
62 sizer->AddSizerChild(mess);
63
64 messageList->Append(mess);
65
66 currentMessage = copyMessage + i + 1;
67 }
68 delete[] copyMessage;
69}
c801d85f 70
debe6624
JS
71wxString wxGetSingleChoice( const wxString& message, const wxString& caption, int n,
72 const wxString *choices, wxWindow *parent,
d427503c
VZ
73 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
74 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 75{
d427503c
VZ
76 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
77 if ( dialog.ShowModal() == wxID_OK )
78 {
79 return dialog.GetStringSelection();
80 }
81 else
82 return "";
c801d85f
KB
83}
84
85// Overloaded for backward compatibility
debe6624
JS
86wxString wxGetSingleChoice( const wxString& message, const wxString& caption, int n,
87 char *choices[], wxWindow *parent,
d427503c
VZ
88 int x, int y, bool centre,
89 int width, int height )
c801d85f 90{
d427503c
VZ
91 wxString *strings = new wxString[n];
92 int i;
93 for ( i = 0; i < n; i++)
94 {
95 strings[i] = choices[i];
96 }
97 wxString ans(wxGetSingleChoice(message, caption, n, (const wxString *)strings, parent,
98 x, y, centre, width, height));
99 delete[] strings;
100 return ans;
c801d85f
KB
101}
102
debe6624
JS
103int wxGetSingleChoiceIndex( const wxString& message, const wxString& caption, int n,
104 const wxString *choices, wxWindow *parent,
d427503c
VZ
105 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
106 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 107{
d427503c
VZ
108 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
109 if ( dialog.ShowModal() == wxID_OK )
110 {
111 return dialog.GetSelection();
112 }
113 else
114 return -1;
c801d85f
KB
115}
116
117// Overloaded for backward compatibility
debe6624 118int wxGetSingleChoiceIndex( const wxString& message, const wxString& caption, int n,
87138c52 119 wxChar *choices[], wxWindow *parent,
d427503c
VZ
120 int x, int y, bool centre,
121 int width, int height )
c801d85f 122{
d427503c
VZ
123 wxString *strings = new wxString[n];
124 int i;
125 for ( i = 0; i < n; i++)
126 {
127 strings[i] = choices[i];
128 }
129 int ans = wxGetSingleChoiceIndex(message, caption, n, (const wxString *)strings, parent,
130 x, y, centre, width, height);
131 delete[] strings;
132 return ans;
c801d85f
KB
133}
134
87138c52 135wxChar *wxGetSingleChoiceData( const wxString& message, const wxString& caption, int n,
d427503c
VZ
136 const wxString *choices, wxChar **client_data, wxWindow *parent,
137 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
138 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 139{
d427503c
VZ
140 wxSingleChoiceDialog dialog(parent, message, caption, n, choices, client_data);
141 if ( dialog.ShowModal() == wxID_OK )
142 {
143 return dialog.GetSelectionClientData();
144 }
145 else
146 return NULL;
c801d85f
KB
147}
148
149// Overloaded for backward compatibility
87138c52 150wxChar *wxGetSingleChoiceData( const wxString& message, const wxString& caption, int n,
d427503c
VZ
151 wxChar *choices[], wxChar **client_data, wxWindow *parent,
152 int x, int y, bool centre,
153 int width, int height )
c801d85f 154{
d427503c
VZ
155 wxString *strings = new wxString[n];
156 int i;
157 for ( i = 0; i < n; i++)
158 {
159 strings[i] = choices[i];
160 }
161 wxChar *data = wxGetSingleChoiceData(message, caption, n, (const wxString *)strings, client_data, parent,
162 x, y, centre, width, height);
163 delete[] strings;
164 return data;
c801d85f
KB
165}
166
167
168/* Multiple choice dialog contributed by Robert Cowell
169 *
170
171The new data passed are in the "int nsel" and "int * selection"
172
173The idea is to make a multiple selection from list of strings.
174The returned value is the total number selected. initialily there
175are nsel selected, with indices stored in
176selection[0],...,selection[nsel-1] which appear highlighted to
177begin with. On exit with value i
178selection[0..i-1] contains the indices of the selected items.
179(Some prior selectecions might be deselected.)
180Thus selection must be as big as choices, in case all items are
181selected.
182
183*/
184/*
185int wxGetMultipleChoice(const wxString& message, const wxString& caption,
d427503c
VZ
186 int n, const wxString *choices,
187 int nsel, int * selection,
188 wxWindow *parent , int x , int y, bool centre,
189 int width, int height)
c801d85f 190{
d427503c 191 return -1;
c801d85f
KB
192}
193*/
194
195// wxSingleChoiceDialog
196
197#if !USE_SHARED_LIBRARY
198BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
d427503c
VZ
199 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
200 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
c801d85f
KB
201END_EVENT_TABLE()
202
203IMPLEMENT_CLASS(wxSingleChoiceDialog, wxDialog)
204#endif
205
206wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent, const wxString& message, const wxString& caption,
87138c52 207 int n, const wxString *choices, wxChar **clientData, long style, const wxPoint& pos):
d427503c 208 wxDialog(parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL|wxTAB_TRAVERSAL)
c801d85f
KB
209{
210 Create(parent, message, caption, n, choices, clientData, style);
211}
212
213wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent, const wxString& message, const wxString& caption,
87138c52 214 const wxStringList& choices, wxChar **clientData, long style, const wxPoint& pos):
d427503c 215 wxDialog(parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL)
c801d85f
KB
216{
217 Create(parent, message, caption, choices, clientData, style);
218}
219
220bool wxSingleChoiceDialog::Create(wxWindow *parent, const wxString& message, const wxString& caption,
87138c52 221 const wxStringList& choices, wxChar **clientData, long style, const wxPoint& pos)
c801d85f 222{
d427503c
VZ
223 wxString *strings = new wxString[choices.Number()];
224 int i;
225 for ( i = 0; i < choices.Number(); i++)
226 {
227 strings[i] = (char *)choices.Nth(i)->Data();
228 }
229 bool ans = Create(parent, message, caption, choices.Number(), strings, clientData, style, pos);
230 delete[] strings;
231 return ans;
c801d85f
KB
232}
233
234bool wxSingleChoiceDialog::Create( wxWindow *WXUNUSED(parent), const wxString& message,
debe6624 235 const wxString& WXUNUSED(caption), int n,
d427503c
VZ
236 const wxString *choices, wxChar **clientData, long style,
237 const wxPoint& WXUNUSED(pos) )
c801d85f 238{
d427503c
VZ
239 m_dialogStyle = style;
240 m_selection = 0;
241 m_stringSelection = _T("");
242 m_clientData = NULL;
c801d85f 243
d427503c 244 wxBeginBusyCursor();
c801d85f 245
d427503c
VZ
246 wxSizer *topSizer = new wxSizer(this, wxSizerShrink);
247 topSizer->SetBorder(10, 10);
c801d85f 248
d427503c
VZ
249 wxRowColSizer *messageSizer = new wxRowColSizer(topSizer, wxSIZER_COLS, 100);
250 messageSizer->SetName(_T("messageSizer"));
c801d85f 251
d427503c 252 // bool centre = ((style & wxCENTRE) == wxCENTRE);
c801d85f 253
d427503c
VZ
254 wxList messageList;
255 wxSplitMessage2(message, &messageList, this, messageSizer);
c801d85f 256
d427503c
VZ
257 // Insert a spacer
258 wxSpacingSizer *spacingSizer = new wxSpacingSizer(topSizer, wxBelow, messageSizer, 10);
c801d85f 259
d427503c
VZ
260 wxListBox *listBox = new wxListBox(this, wxID_LISTBOX, wxPoint(-1, -1), wxSize(240, 160),
261 n, choices);
ef77f91e 262 listBox->SetSelection(m_selection);
d427503c
VZ
263 if ( clientData )
264 {
265 int i;
266 for ( i = 0; i < n; i++)
267 {
268 listBox->SetClientData(i, clientData[i]);
269 }
270 }
271
272 wxRowColSizer *listBoxSizer = new wxRowColSizer(topSizer, wxSIZER_ROWS);
273 listBoxSizer->AddSizerChild(listBox);
274 listBoxSizer->SetName(_T("listBoxSizer"));
275
276 // Create constraints for the text sizer
277 wxLayoutConstraints *textC = new wxLayoutConstraints;
278 textC->left.SameAs (messageSizer, wxLeft);
279 textC->top.Below (spacingSizer);
280 listBoxSizer->SetConstraints(textC);
281
282 // Insert another spacer
283 wxSpacingSizer *spacingSizer2 = new wxSpacingSizer(topSizer, wxBelow, listBoxSizer, 10);
284 spacingSizer->SetName(_T("spacingSizer2"));
285
286 // Insert a sizer for the buttons
287 wxRowColSizer *buttonSizer = new wxRowColSizer(topSizer, wxSIZER_ROWS);
288 buttonSizer->SetName(_T("buttonSizer"));
289 buttonSizer->SetSpacing(12,0);
290
291 // Specify constraints for the button sizer
292 wxLayoutConstraints *c = new wxLayoutConstraints;
293 c->width.AsIs ();
294 c->height.AsIs ();
295 c->top.Below (spacingSizer2);
296 c->centreX.SameAs (listBoxSizer, wxCentreX);
297 buttonSizer->SetConstraints(c);
c801d85f
KB
298
299 wxButton *ok = NULL;
d427503c 300 wxButton *cancel = NULL;
c801d85f 301
d427503c
VZ
302 if (style & wxOK) {
303 ok = new wxButton(this, wxID_OK, _("OK"), wxDefaultPosition, wxSize(75,-1) );
304 buttonSizer->AddSizerChild(ok);
305 }
c801d85f 306
d427503c
VZ
307 if (style & wxCANCEL) {
308 cancel = new wxButton(this, wxID_CANCEL, _("Cancel"), wxDefaultPosition, wxSize(75,-1));
309 buttonSizer->AddSizerChild(cancel);
310 }
c801d85f 311
d427503c
VZ
312 if (ok)
313 {
314 ok->SetDefault();
315 ok->SetFocus();
316 }
c801d85f 317
d427503c
VZ
318 Layout();
319 Centre(wxBOTH);
c801d85f 320
d427503c 321 wxEndBusyCursor();
c801d85f 322
d427503c 323 return TRUE;
c801d85f
KB
324}
325
ef77f91e
JS
326// Set the selection
327void wxSingleChoiceDialog::SetSelection(int sel)
328{
d427503c 329 wxListBox *listBox = (wxListBox *)FindWindow(wxID_LISTBOX);
ef77f91e
JS
330 if (listBox)
331 {
332 listBox->SetSelection(sel);
333 }
334 m_selection = sel;
335}
336
c801d85f
KB
337void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
338{
d427503c
VZ
339 wxListBox *listBox = (wxListBox *)FindWindow(wxID_LISTBOX);
340 if ( listBox )
341 {
342 m_selection = listBox->GetSelection();
343 m_stringSelection = listBox->GetStringSelection();
344 m_clientData = (wxChar*)listBox->GetClientData(m_selection);
345 }
346
347 EndModal(wxID_OK);
c801d85f
KB
348}
349
debe6624
JS
350void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
351{
d427503c
VZ
352 wxListBox *listBox = (wxListBox *)FindWindow(wxID_LISTBOX);
353 if ( listBox )
354 {
355 m_selection = listBox->GetSelection();
356 m_stringSelection = listBox->GetStringSelection();
357 m_clientData = (wxChar*)listBox->GetClientData(m_selection);
358 }
359
360 EndModal(wxID_OK);
debe6624
JS
361}
362
d427503c 363#endif // wxUSE_CONSTRAINTS
c801d85f 364