]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicdgg.cpp
compile fix in debug mode
[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
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
dfad0599 36// Split message, using constraints to position controls
87138c52 37static void wxSplitMessage2(const wxChar *message, wxList *messageList, wxWindow *parent, wxRowColSizer *sizer)
dfad0599 38{
87138c52 39 wxChar *copyMessage = copystring(message);
dfad0599 40 size_t i = 0;
87138c52
OK
41 size_t len = wxStrlen(copyMessage);
42 wxChar *currentMessage = copyMessage;
dfad0599
JS
43
44// wxWindow *lastWindow = parent;
45
46 while (i < len) {
87138c52 47 while ((i < len) && (copyMessage[i] != _T('\n'))) i++;
dfad0599
JS
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}
c801d85f 68
debe6624
JS
69wxString 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) )
c801d85f
KB
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
debe6624
JS
84wxString 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 )
c801d85f
KB
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
debe6624
JS
101int 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) )
c801d85f
KB
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
debe6624 116int wxGetSingleChoiceIndex( const wxString& message, const wxString& caption, int n,
87138c52 117 wxChar *choices[], wxWindow *parent,
debe6624
JS
118 int x, int y, bool centre,
119 int width, int height )
c801d85f
KB
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
87138c52
OK
133wxChar *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) )
c801d85f
KB
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
87138c52
OK
148wxChar *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 )
c801d85f
KB
152{
153 wxString *strings = new wxString[n];
154 int i;
155 for ( i = 0; i < n; i++)
156 {
157 strings[i] = choices[i];
158 }
87138c52 159 wxChar *data = wxGetSingleChoiceData(message, caption, n, (const wxString *)strings, client_data, parent,
c801d85f
KB
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
169The new data passed are in the "int nsel" and "int * selection"
170
171The idea is to make a multiple selection from list of strings.
172The returned value is the total number selected. initialily there
173are nsel selected, with indices stored in
174selection[0],...,selection[nsel-1] which appear highlighted to
175begin with. On exit with value i
176selection[0..i-1] contains the indices of the selected items.
177(Some prior selectecions might be deselected.)
178Thus selection must be as big as choices, in case all items are
179selected.
180
181*/
182/*
183int wxGetMultipleChoice(const wxString& message, const wxString& caption,
debe6624
JS
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)
c801d85f
KB
188{
189 return -1;
190}
191*/
192
193// wxSingleChoiceDialog
194
195#if !USE_SHARED_LIBRARY
196BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
197 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
debe6624 198 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
c801d85f
KB
199END_EVENT_TABLE()
200
201IMPLEMENT_CLASS(wxSingleChoiceDialog, wxDialog)
202#endif
203
204wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent, const wxString& message, const wxString& caption,
87138c52 205 int n, const wxString *choices, wxChar **clientData, long style, const wxPoint& pos):
2a47d3c1 206 wxDialog(parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL|wxTAB_TRAVERSAL)
c801d85f
KB
207{
208 Create(parent, message, caption, n, choices, clientData, style);
209}
210
211wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent, const wxString& message, const wxString& caption,
87138c52 212 const wxStringList& choices, wxChar **clientData, long style, const wxPoint& pos):
c801d85f
KB
213 wxDialog(parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL)
214{
215 Create(parent, message, caption, choices, clientData, style);
216}
217
218bool wxSingleChoiceDialog::Create(wxWindow *parent, const wxString& message, const wxString& caption,
87138c52 219 const wxStringList& choices, wxChar **clientData, long style, const wxPoint& pos)
c801d85f
KB
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
232bool wxSingleChoiceDialog::Create( wxWindow *WXUNUSED(parent), const wxString& message,
debe6624 233 const wxString& WXUNUSED(caption), int n,
87138c52 234 const wxString *choices, wxChar **clientData, long style,
c801d85f
KB
235 const wxPoint& WXUNUSED(pos) )
236{
237 m_dialogStyle = style;
238 m_selection = 0;
87138c52 239 m_stringSelection = _T("");
c801d85f
KB
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);
87138c52 248 messageSizer->SetName(_T("messageSizer"));
c801d85f
KB
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);
ef77f91e 260 listBox->SetSelection(m_selection);
c801d85f
KB
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);
87138c52 272 listBoxSizer->SetName(_T("listBoxSizer"));
c801d85f
KB
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);
87138c52 282 spacingSizer->SetName(_T("spacingSizer2"));
c801d85f
KB
283
284 // Insert a sizer for the buttons
285 wxRowColSizer *buttonSizer = new wxRowColSizer(topSizer, wxSIZER_ROWS);
87138c52 286 buttonSizer->SetName(_T("buttonSizer"));
3502e687 287 buttonSizer->SetSpacing(12,0);
c801d85f
KB
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) {
3502e687 301 ok = new wxButton(this, wxID_OK, _("OK"), wxDefaultPosition, wxSize(75,-1) );
c801d85f
KB
302 buttonSizer->AddSizerChild(ok);
303 }
304
305 if (style & wxCANCEL) {
3502e687 306 cancel = new wxButton(this, wxID_CANCEL, _("Cancel"), wxDefaultPosition, wxSize(75,-1));
c801d85f
KB
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
ef77f91e
JS
324// Set the selection
325void 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
c801d85f
KB
335void 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();
87138c52 342 m_clientData = (wxChar*)listBox->GetClientData(m_selection);
c801d85f
KB
343 }
344
345 EndModal(wxID_OK);
346}
347
debe6624
JS
348void 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();
87138c52 355 m_clientData = (wxChar*)listBox->GetClientData(m_selection);
debe6624
JS
356 }
357
358 EndModal(wxID_OK);
359}
360
c801d85f 361