]> git.saurik.com Git - wxWidgets.git/blame - src/generic/choicdgg.cpp
Suppose I should trust the headers in BC++5.02 more than BC++4.52
[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
d427503c 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
d427503c 13 #pragma implementation "choicdgg.h"
c801d85f
KB
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
d427503c 20 #pragma hdrstop
c801d85f
KB
21#endif
22
23#ifndef WX_PRECOMP
dcf924a3
RR
24 #include <stdio.h>
25 #include "wx/utils.h"
26 #include "wx/dialog.h"
27 #include "wx/button.h"
28 #include "wx/listbox.h"
29 #include "wx/stattext.h"
30 #include "wx/intl.h"
31#endif
32
33#if wxUSE_STATLINE
34 #include "wx/statline.h"
c801d85f
KB
35#endif
36
37#include "wx/generic/choicdgg.h"
38
dcf924a3
RR
39/* Split message, using constraints to position controls */
40static wxSize wxSplitMessage2( const wxString &message, wxWindow *parent )
dfad0599 41{
dcf924a3
RR
42 int y = 10;
43 int w = 50;
44 wxString line( _T("") );
1318fabe 45 for (size_t pos = 0; pos < message.Len(); pos++)
dcf924a3
RR
46 {
47 if (message[pos] == _T('\n'))
48 {
49 if (!line.IsEmpty())
50 {
51 wxStaticText *s1 = new wxStaticText( parent, -1, line, wxPoint(15,y) );
52 wxSize size1( s1->GetSize() );
53 if (size1.x > w) w = size1.x;
54 line = _T("");
55 }
56 y += 18;
57 }
58 else
59 {
60 line += message[pos];
61 }
62 }
63
64 if (!line.IsEmpty())
65 {
66 wxStaticText *s2 = new wxStaticText( parent, -1, line, wxPoint(15,y) );
67 wxSize size2( s2->GetSize() );
68 if (size2.x > w) w = size2.x;
69 }
70
71 y += 18;
72
73 return wxSize(w+30,y);
dfad0599 74}
c801d85f 75
dcf924a3 76
debe6624
JS
77wxString wxGetSingleChoice( const wxString& message, const wxString& caption, int n,
78 const wxString *choices, wxWindow *parent,
d427503c
VZ
79 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
80 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 81{
d427503c
VZ
82 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
83 if ( dialog.ShowModal() == wxID_OK )
d427503c 84 return dialog.GetStringSelection();
d427503c 85 else
dcf924a3 86 return _T("");
c801d85f
KB
87}
88
89// Overloaded for backward compatibility
debe6624
JS
90wxString wxGetSingleChoice( const wxString& message, const wxString& caption, int n,
91 char *choices[], wxWindow *parent,
d427503c
VZ
92 int x, int y, bool centre,
93 int width, int height )
c801d85f 94{
d427503c
VZ
95 wxString *strings = new wxString[n];
96 int i;
97 for ( i = 0; i < n; i++)
98 {
99 strings[i] = choices[i];
100 }
101 wxString ans(wxGetSingleChoice(message, caption, n, (const wxString *)strings, parent,
102 x, y, centre, width, height));
103 delete[] strings;
104 return ans;
c801d85f
KB
105}
106
debe6624
JS
107int wxGetSingleChoiceIndex( const wxString& message, const wxString& caption, int n,
108 const wxString *choices, wxWindow *parent,
d427503c
VZ
109 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
110 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 111{
d427503c
VZ
112 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
113 if ( dialog.ShowModal() == wxID_OK )
d427503c 114 return dialog.GetSelection();
d427503c
VZ
115 else
116 return -1;
c801d85f
KB
117}
118
119// Overloaded for backward compatibility
debe6624 120int wxGetSingleChoiceIndex( const wxString& message, const wxString& caption, int n,
87138c52 121 wxChar *choices[], wxWindow *parent,
d427503c
VZ
122 int x, int y, bool centre,
123 int width, int height )
c801d85f 124{
d427503c 125 wxString *strings = new wxString[n];
dcf924a3 126 for ( int i = 0; i < n; i++)
d427503c 127 strings[i] = choices[i];
d427503c
VZ
128 int ans = wxGetSingleChoiceIndex(message, caption, n, (const wxString *)strings, parent,
129 x, y, centre, width, height);
130 delete[] strings;
131 return ans;
c801d85f
KB
132}
133
87138c52 134wxChar *wxGetSingleChoiceData( const wxString& message, const wxString& caption, int n,
d427503c
VZ
135 const wxString *choices, wxChar **client_data, wxWindow *parent,
136 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
137 int WXUNUSED(width), int WXUNUSED(height) )
c801d85f 138{
d427503c
VZ
139 wxSingleChoiceDialog dialog(parent, message, caption, n, choices, client_data);
140 if ( dialog.ShowModal() == wxID_OK )
d427503c 141 return dialog.GetSelectionClientData();
d427503c
VZ
142 else
143 return NULL;
c801d85f
KB
144}
145
146// Overloaded for backward compatibility
87138c52 147wxChar *wxGetSingleChoiceData( const wxString& message, const wxString& caption, int n,
d427503c
VZ
148 wxChar *choices[], wxChar **client_data, wxWindow *parent,
149 int x, int y, bool centre,
150 int width, int height )
c801d85f 151{
d427503c
VZ
152 wxString *strings = new wxString[n];
153 int i;
154 for ( i = 0; i < n; i++)
155 {
156 strings[i] = choices[i];
157 }
158 wxChar *data = wxGetSingleChoiceData(message, caption, n, (const wxString *)strings, client_data, parent,
159 x, y, centre, width, height);
160 delete[] strings;
161 return data;
c801d85f
KB
162}
163
164
165/* Multiple choice dialog contributed by Robert Cowell
166 *
167
168The new data passed are in the "int nsel" and "int * selection"
169
170The idea is to make a multiple selection from list of strings.
171The returned value is the total number selected. initialily there
172are nsel selected, with indices stored in
173selection[0],...,selection[nsel-1] which appear highlighted to
174begin with. On exit with value i
175selection[0..i-1] contains the indices of the selected items.
176(Some prior selectecions might be deselected.)
177Thus selection must be as big as choices, in case all items are
178selected.
179
180*/
181/*
182int wxGetMultipleChoice(const wxString& message, const wxString& caption,
d427503c
VZ
183 int n, const wxString *choices,
184 int nsel, int * selection,
185 wxWindow *parent , int x , int y, bool centre,
186 int width, int height)
c801d85f 187{
d427503c 188 return -1;
c801d85f
KB
189}
190*/
191
192// wxSingleChoiceDialog
193
194#if !USE_SHARED_LIBRARY
195BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
d427503c
VZ
196 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
197 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
c801d85f
KB
198END_EVENT_TABLE()
199
200IMPLEMENT_CLASS(wxSingleChoiceDialog, wxDialog)
201#endif
202
203wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent, const wxString& message, const wxString& caption,
87138c52 204 int n, const wxString *choices, wxChar **clientData, long style, const wxPoint& pos):
d427503c 205 wxDialog(parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL|wxTAB_TRAVERSAL)
c801d85f
KB
206{
207 Create(parent, message, caption, n, choices, clientData, style);
208}
209
210wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent, const wxString& message, const wxString& caption,
87138c52 211 const wxStringList& choices, wxChar **clientData, long style, const wxPoint& pos):
d427503c 212 wxDialog(parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL)
c801d85f
KB
213{
214 Create(parent, message, caption, choices, clientData, style);
215}
216
217bool wxSingleChoiceDialog::Create(wxWindow *parent, const wxString& message, const wxString& caption,
87138c52 218 const wxStringList& choices, wxChar **clientData, long style, const wxPoint& pos)
c801d85f 219{
d427503c
VZ
220 wxString *strings = new wxString[choices.Number()];
221 int i;
222 for ( i = 0; i < choices.Number(); i++)
223 {
224 strings[i] = (char *)choices.Nth(i)->Data();
225 }
226 bool ans = Create(parent, message, caption, choices.Number(), strings, clientData, style, pos);
227 delete[] strings;
228 return ans;
c801d85f
KB
229}
230
231bool wxSingleChoiceDialog::Create( wxWindow *WXUNUSED(parent), const wxString& message,
debe6624 232 const wxString& WXUNUSED(caption), int n,
d427503c
VZ
233 const wxString *choices, wxChar **clientData, long style,
234 const wxPoint& WXUNUSED(pos) )
c801d85f 235{
d427503c
VZ
236 m_dialogStyle = style;
237 m_selection = 0;
238 m_stringSelection = _T("");
239 m_clientData = NULL;
c801d85f 240
d427503c 241 wxBeginBusyCursor();
dcf924a3
RR
242
243 wxSize message_size( wxSplitMessage2( message, this ) );
244
245 wxButton *ok = (wxButton *) NULL;
246 wxButton *cancel = (wxButton *) NULL;
247 wxList m_buttons;
248
249 int y = message_size.y + 15;
250
251 int listbox_height = wxMin( 160, n*20 );
252
253 wxListBox *listBox = new wxListBox( this, wxID_LISTBOX, wxPoint(10, y), wxSize(240, listbox_height),
254 n, choices, wxLB_ALWAYS_SB );
255 listBox->SetSelection( m_selection );
256 if (clientData)
d427503c 257 {
dcf924a3 258 for (int i = 0; i < n; i++)
d427503c 259 listBox->SetClientData(i, clientData[i]);
d427503c 260 }
dcf924a3
RR
261
262 y += listbox_height + 35;
d427503c 263
dcf924a3
RR
264 if (style & wxOK)
265 {
266 ok = new wxButton( this, wxID_OK, _("OK"), wxPoint(-1,y), wxSize(80,-1) );
267 m_buttons.Append( ok );
d427503c 268 }
c801d85f 269
dcf924a3
RR
270 if (style & wxCANCEL)
271 {
272 cancel = new wxButton( this, wxID_CANCEL, _("Cancel"), wxPoint(-1,y), wxSize(80,-1) );
273 m_buttons.Append( cancel );
d427503c 274 }
c801d85f 275
d427503c
VZ
276 if (ok)
277 {
278 ok->SetDefault();
279 ok->SetFocus();
280 }
c801d85f 281
dcf924a3
RR
282 int w = m_buttons.GetCount() * 100;
283 if (message_size.x > w) w = message_size.x;
284 int space = w / (m_buttons.GetCount()*2);
285
286 listBox->SetSize( 20, -1, w-10, listbox_height );
287
288 int m = 0;
289 wxNode *node = m_buttons.First();
290 while (node)
291 {
292 wxWindow *win = (wxWindow*)node->Data();
293 int x = (m*2+1)*space - 40 + 15;
294 win->Move( x, -1 );
295 node = node->Next();
296 m++;
297 }
298
299#if wxUSE_STATLINE
300 (void) new wxStaticLine( this, -1, wxPoint(0,y-20), wxSize(w+30, 5) );
301#endif
302
303 SetSize( w+30, y+40 );
304
305 Centre( wxBOTH );
c801d85f 306
d427503c 307 wxEndBusyCursor();
c801d85f 308
d427503c 309 return TRUE;
c801d85f
KB
310}
311
ef77f91e
JS
312// Set the selection
313void wxSingleChoiceDialog::SetSelection(int sel)
314{
d427503c 315 wxListBox *listBox = (wxListBox *)FindWindow(wxID_LISTBOX);
ef77f91e
JS
316 if (listBox)
317 {
318 listBox->SetSelection(sel);
319 }
320 m_selection = sel;
321}
322
c801d85f
KB
323void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
324{
d427503c
VZ
325 wxListBox *listBox = (wxListBox *)FindWindow(wxID_LISTBOX);
326 if ( listBox )
327 {
328 m_selection = listBox->GetSelection();
329 m_stringSelection = listBox->GetStringSelection();
330 m_clientData = (wxChar*)listBox->GetClientData(m_selection);
331 }
332
333 EndModal(wxID_OK);
c801d85f
KB
334}
335
debe6624
JS
336void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
337{
d427503c
VZ
338 wxListBox *listBox = (wxListBox *)FindWindow(wxID_LISTBOX);
339 if ( listBox )
340 {
341 m_selection = listBox->GetSelection();
342 m_stringSelection = listBox->GetStringSelection();
343 m_clientData = (wxChar*)listBox->GetClientData(m_selection);
344 }
345
346 EndModal(wxID_OK);
debe6624
JS
347}
348