]> git.saurik.com Git - wxWidgets.git/blob - src/generic/choicdgg.cpp
Visible functions implemented
[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 char *message, wxList *messageList, wxWindow *parent, wxRowColSizer *sizer)
38 {
39 char *copyMessage = copystring(message);
40 size_t i = 0;
41 size_t len = strlen(copyMessage);
42 char *currentMessage = copyMessage;
43
44 // wxWindow *lastWindow = parent;
45
46 while (i < len) {
47 while ((i < len) && (copyMessage[i] != '\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 char *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 char *wxGetSingleChoiceData( const wxString& message, const wxString& caption, int n,
134 const wxString *choices, char **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 char *wxGetSingleChoiceData( const wxString& message, const wxString& caption, int n,
149 char *choices[], char **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 char *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, char **clientData, long style, const wxPoint& pos):
206 wxDialog(parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL)
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, char **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, char **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, char **clientData, long style,
235 const wxPoint& WXUNUSED(pos) )
236 {
237 m_dialogStyle = style;
238 m_selection = 0;
239 m_stringSelection = "";
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("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("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("spacingSizer2");
283
284 // Insert a sizer for the buttons
285 wxRowColSizer *buttonSizer = new wxRowColSizer(topSizer, wxSIZER_ROWS);
286 buttonSizer->SetName("buttonSizer");
287
288 // Specify constraints for the button sizer
289 wxLayoutConstraints *c = new wxLayoutConstraints;
290 c->width.AsIs ();
291 c->height.AsIs ();
292 c->top.Below (spacingSizer2);
293 c->centreX.SameAs (listBoxSizer, wxCentreX);
294 buttonSizer->SetConstraints(c);
295
296 wxButton *ok = NULL;
297 wxButton *cancel = NULL;
298
299 if (style & wxOK) {
300 ok = new wxButton(this, wxID_OK, _("OK"));
301 buttonSizer->AddSizerChild(ok);
302 }
303
304 if (style & wxCANCEL) {
305 cancel = new wxButton(this, wxID_CANCEL, _("Cancel"));
306 buttonSizer->AddSizerChild(cancel);
307 }
308
309 if (ok)
310 {
311 ok->SetDefault();
312 ok->SetFocus();
313 }
314
315 Layout();
316 Centre(wxBOTH);
317
318 wxEndBusyCursor();
319
320 return TRUE;
321 }
322
323 // Set the selection
324 void wxSingleChoiceDialog::SetSelection(int sel)
325 {
326 wxListBox *listBox = (wxListBox *)FindWindow(wxID_LISTBOX);
327 if (listBox)
328 {
329 listBox->SetSelection(sel);
330 }
331 m_selection = sel;
332 }
333
334 void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
335 {
336 wxListBox *listBox = (wxListBox *)FindWindow(wxID_LISTBOX);
337 if ( listBox )
338 {
339 m_selection = listBox->GetSelection();
340 m_stringSelection = listBox->GetStringSelection();
341 m_clientData = listBox->GetClientData(m_selection);
342 }
343
344 EndModal(wxID_OK);
345 }
346
347 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
348 {
349 wxListBox *listBox = (wxListBox *)FindWindow(wxID_LISTBOX);
350 if ( listBox )
351 {
352 m_selection = listBox->GetSelection();
353 m_stringSelection = listBox->GetStringSelection();
354 m_clientData = listBox->GetClientData(m_selection);
355 }
356
357 EndModal(wxID_OK);
358 }
359
360