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