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