1. some more of "#if wxUSE_XXX" here and there
[wxWidgets.git] / src / generic / choicdgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: choicdgg.cpp
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
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "choicdgg.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_CONSTRAINTS
24
25 #ifndef WX_PRECOMP
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"
34 #endif
35
36 #include "wx/generic/choicdgg.h"
37
38 // Split message, using constraints to position controls
39 static void wxSplitMessage2(const wxChar *message, wxList *messageList, wxWindow *parent, wxRowColSizer *sizer)
40 {
41 wxChar *copyMessage = copystring(message);
42 size_t i = 0;
43 size_t len = wxStrlen(copyMessage);
44 wxChar *currentMessage = copyMessage;
45
46 // wxWindow *lastWindow = parent;
47
48 while (i < len) {
49 while ((i < len) && (copyMessage[i] != _T('\n'))) i++;
50 if (i < len) copyMessage[i] = 0;
51 wxStaticText *mess = new wxStaticText(parent, -1, currentMessage);
52
53 /*
54 wxLayoutConstraints *c = new wxLayoutConstraints;
55 c->left.SameAs (parent, wxLeft, 10);
56 c->top.SameAs (lastWindow, wxBottom, 5);
57 c->right.AsIs ();
58 c->height.AsIs ();
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 }
70
71 wxString wxGetSingleChoice( const wxString& message, const wxString& caption, int n,
72 const wxString *choices, wxWindow *parent,
73 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
74 int WXUNUSED(width), int WXUNUSED(height) )
75 {
76 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
77 if ( dialog.ShowModal() == wxID_OK )
78 {
79 return dialog.GetStringSelection();
80 }
81 else
82 return "";
83 }
84
85 // Overloaded for backward compatibility
86 wxString wxGetSingleChoice( const wxString& message, const wxString& caption, int n,
87 char *choices[], wxWindow *parent,
88 int x, int y, bool centre,
89 int width, int height )
90 {
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;
101 }
102
103 int wxGetSingleChoiceIndex( const wxString& message, const wxString& caption, int n,
104 const wxString *choices, wxWindow *parent,
105 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
106 int WXUNUSED(width), int WXUNUSED(height) )
107 {
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;
115 }
116
117 // Overloaded for backward compatibility
118 int wxGetSingleChoiceIndex( const wxString& message, const wxString& caption, int n,
119 wxChar *choices[], wxWindow *parent,
120 int x, int y, bool centre,
121 int width, int height )
122 {
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;
133 }
134
135 wxChar *wxGetSingleChoiceData( const wxString& message, const wxString& caption, int n,
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) )
139 {
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;
147 }
148
149 // Overloaded for backward compatibility
150 wxChar *wxGetSingleChoiceData( const wxString& message, const wxString& caption, int n,
151 wxChar *choices[], wxChar **client_data, wxWindow *parent,
152 int x, int y, bool centre,
153 int width, int height )
154 {
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;
165 }
166
167
168 /* Multiple choice dialog contributed by Robert Cowell
169 *
170
171 The new data passed are in the "int nsel" and "int * selection"
172
173 The idea is to make a multiple selection from list of strings.
174 The returned value is the total number selected. initialily there
175 are nsel selected, with indices stored in
176 selection[0],...,selection[nsel-1] which appear highlighted to
177 begin with. On exit with value i
178 selection[0..i-1] contains the indices of the selected items.
179 (Some prior selectecions might be deselected.)
180 Thus selection must be as big as choices, in case all items are
181 selected.
182
183 */
184 /*
185 int wxGetMultipleChoice(const wxString& message, const wxString& caption,
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)
190 {
191 return -1;
192 }
193 */
194
195 // wxSingleChoiceDialog
196
197 #if !USE_SHARED_LIBRARY
198 BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
199 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
200 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
201 END_EVENT_TABLE()
202
203 IMPLEMENT_CLASS(wxSingleChoiceDialog, wxDialog)
204 #endif
205
206 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent, const wxString& message, const wxString& caption,
207 int n, const wxString *choices, wxChar **clientData, long style, const wxPoint& pos):
208 wxDialog(parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL|wxTAB_TRAVERSAL)
209 {
210 Create(parent, message, caption, n, choices, clientData, style);
211 }
212
213 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent, const wxString& message, const wxString& caption,
214 const wxStringList& choices, wxChar **clientData, long style, const wxPoint& pos):
215 wxDialog(parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL)
216 {
217 Create(parent, message, caption, choices, clientData, style);
218 }
219
220 bool wxSingleChoiceDialog::Create(wxWindow *parent, const wxString& message, const wxString& caption,
221 const wxStringList& choices, wxChar **clientData, long style, const wxPoint& pos)
222 {
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;
232 }
233
234 bool wxSingleChoiceDialog::Create( wxWindow *WXUNUSED(parent), const wxString& message,
235 const wxString& WXUNUSED(caption), int n,
236 const wxString *choices, wxChar **clientData, long style,
237 const wxPoint& WXUNUSED(pos) )
238 {
239 m_dialogStyle = style;
240 m_selection = 0;
241 m_stringSelection = _T("");
242 m_clientData = NULL;
243
244 wxBeginBusyCursor();
245
246 wxSizer *topSizer = new wxSizer(this, wxSizerShrink);
247 topSizer->SetBorder(10, 10);
248
249 wxRowColSizer *messageSizer = new wxRowColSizer(topSizer, wxSIZER_COLS, 100);
250 messageSizer->SetName(_T("messageSizer"));
251
252 // bool centre = ((style & wxCENTRE) == wxCENTRE);
253
254 wxList messageList;
255 wxSplitMessage2(message, &messageList, this, messageSizer);
256
257 // Insert a spacer
258 wxSpacingSizer *spacingSizer = new wxSpacingSizer(topSizer, wxBelow, messageSizer, 10);
259
260 wxListBox *listBox = new wxListBox(this, wxID_LISTBOX, wxPoint(-1, -1), wxSize(240, 160),
261 n, choices);
262 listBox->SetSelection(m_selection);
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);
298
299 wxButton *ok = NULL;
300 wxButton *cancel = NULL;
301
302 if (style & wxOK) {
303 ok = new wxButton(this, wxID_OK, _("OK"), wxDefaultPosition, wxSize(75,-1) );
304 buttonSizer->AddSizerChild(ok);
305 }
306
307 if (style & wxCANCEL) {
308 cancel = new wxButton(this, wxID_CANCEL, _("Cancel"), wxDefaultPosition, wxSize(75,-1));
309 buttonSizer->AddSizerChild(cancel);
310 }
311
312 if (ok)
313 {
314 ok->SetDefault();
315 ok->SetFocus();
316 }
317
318 Layout();
319 Centre(wxBOTH);
320
321 wxEndBusyCursor();
322
323 return TRUE;
324 }
325
326 // Set the selection
327 void wxSingleChoiceDialog::SetSelection(int sel)
328 {
329 wxListBox *listBox = (wxListBox *)FindWindow(wxID_LISTBOX);
330 if (listBox)
331 {
332 listBox->SetSelection(sel);
333 }
334 m_selection = sel;
335 }
336
337 void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
338 {
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);
348 }
349
350 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
351 {
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);
361 }
362
363 #endif // wxUSE_CONSTRAINTS
364