]> git.saurik.com Git - wxWidgets.git/blob - src/generic/choicdgg.cpp
give frame a reasonable default size if none specified
[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/button.h"
28 #include "wx/listbox.h"
29 #include "wx/stattext.h"
30 #include "wx/intl.h"
31 #include "wx/sizer.h"
32 #endif
33
34 #if wxUSE_STATLINE
35 #include "wx/statline.h"
36 #endif
37
38 #include "wx/generic/choicdgg.h"
39
40 #define wxID_LISTBOX 3000
41
42 wxString wxGetSingleChoice( const wxString& message, const wxString& caption, int n,
43 const wxString *choices, wxWindow *parent,
44 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
45 int WXUNUSED(width), int WXUNUSED(height) )
46 {
47 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
48 wxString choice;
49 if ( dialog.ShowModal() == wxID_OK )
50 choice = dialog.GetStringSelection();
51
52 return choice;
53 }
54
55 // Overloaded for backward compatibility
56 wxString wxGetSingleChoice( const wxString& message, const wxString& caption, int n,
57 char *choices[], wxWindow *parent,
58 int x, int y, bool centre,
59 int width, int height )
60 {
61 wxString *strings = new wxString[n];
62 int i;
63 for ( i = 0; i < n; i++)
64 {
65 strings[i] = choices[i];
66 }
67 wxString ans(wxGetSingleChoice(message, caption, n, (const wxString *)strings, parent,
68 x, y, centre, width, height));
69 delete[] strings;
70 return ans;
71 }
72
73 int wxGetSingleChoiceIndex( const wxString& message, const wxString& caption, int n,
74 const wxString *choices, wxWindow *parent,
75 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
76 int WXUNUSED(width), int WXUNUSED(height) )
77 {
78 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
79 int choice;
80 if ( dialog.ShowModal() == wxID_OK )
81 choice = dialog.GetSelection();
82 else
83 choice = -1;
84
85 return choice;
86 }
87
88 // Overloaded for backward compatibility
89 int wxGetSingleChoiceIndex( const wxString& message, const wxString& caption, int n,
90 wxChar *choices[], wxWindow *parent,
91 int x, int y, bool centre,
92 int width, int height )
93 {
94 wxString *strings = new wxString[n];
95 for ( int i = 0; i < n; i++)
96 strings[i] = choices[i];
97 int ans = wxGetSingleChoiceIndex(message, caption, n, (const wxString *)strings, parent,
98 x, y, centre, width, height);
99 delete[] strings;
100 return ans;
101 }
102
103 void *wxGetSingleChoiceData( const wxString& message, const wxString& caption, int n,
104 const wxString *choices, void **client_data, wxWindow *parent,
105 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
106 int WXUNUSED(width), int WXUNUSED(height) )
107 {
108 wxSingleChoiceDialog dialog(parent, message, caption, n, choices, (char **)client_data);
109 void *data;
110 if ( dialog.ShowModal() == wxID_OK )
111 data = dialog.GetSelectionClientData();
112 else
113 data = NULL;
114
115 return data;
116 }
117
118 // Overloaded for backward compatibility
119 void *wxGetSingleChoiceData( const wxString& message, const wxString& caption, int n,
120 wxChar *choices[], void **client_data, wxWindow *parent,
121 int x, int y, bool centre,
122 int width, int height )
123 {
124 wxString *strings = new wxString[n];
125 int i;
126 for ( i = 0; i < n; i++)
127 {
128 strings[i] = choices[i];
129 }
130 void *data = wxGetSingleChoiceData(message, caption, n, (const wxString *)strings, client_data, parent,
131 x, y, centre, width, height);
132 delete[] strings;
133 return data;
134 }
135
136
137 /* Multiple choice dialog contributed by Robert Cowell
138 *
139
140 The new data passed are in the "int nsel" and "int * selection"
141
142 The idea is to make a multiple selection from list of strings.
143 The returned value is the total number selected. initialily there
144 are nsel selected, with indices stored in
145 selection[0],...,selection[nsel-1] which appear highlighted to
146 begin with. On exit with value i
147 selection[0..i-1] contains the indices of the selected items.
148 (Some prior selectecions might be deselected.)
149 Thus selection must be as big as choices, in case all items are
150 selected.
151
152 */
153 /*
154 int wxGetMultipleChoice(const wxString& message, const wxString& caption,
155 int n, const wxString *choices,
156 int nsel, int * selection,
157 wxWindow *parent , int x , int y, bool centre,
158 int width, int height)
159 {
160 return -1;
161 }
162 */
163
164 // wxSingleChoiceDialog
165
166 BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
167 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
168 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
169 END_EVENT_TABLE()
170
171 IMPLEMENT_CLASS(wxSingleChoiceDialog, wxDialog)
172
173 #if defined(__WXMSW__) || defined(__WXMAC__)
174 #define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
175 wxDIALOG_MODAL | \
176 wxTAB_TRAVERSAL)
177 #else
178 #define wxCHOICEDLG_DIALOG_STYLE (wxDEFAULT_DIALOG_STYLE | \
179 wxDIALOG_MODAL | \
180 wxRESIZE_BORDER | \
181 wxTAB_TRAVERSAL)
182 #endif
183
184
185 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
186 const wxString& message,
187 const wxString& caption,
188 int n,
189 const wxString *choices,
190 char **clientData,
191 long style,
192 const wxPoint& pos)
193 : wxDialog(parent, -1, caption, pos, wxDefaultSize,
194 wxCHOICEDLG_DIALOG_STYLE)
195 {
196 Create(parent, message, caption, n, choices, clientData, style);
197 }
198
199 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent,
200 const wxString& message,
201 const wxString& caption,
202 const wxStringList& choices,
203 char **clientData,
204 long style,
205 const wxPoint& pos)
206 : wxDialog(parent, -1, caption, pos, wxDefaultSize,
207 wxCHOICEDLG_DIALOG_STYLE)
208 {
209 Create(parent, message, caption, choices, clientData, style);
210 }
211
212 bool wxSingleChoiceDialog::Create(wxWindow *parent,
213 const wxString& message,
214 const wxString& caption,
215 const wxStringList& choices,
216 char **clientData,
217 long style,
218 const wxPoint& pos)
219 {
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;
229 }
230
231 bool wxSingleChoiceDialog::Create( wxWindow *WXUNUSED(parent),
232 const wxString& message,
233 const wxString& WXUNUSED(caption),
234 int n,
235 const wxString *choices,
236 char **clientData,
237 long style,
238 const wxPoint& WXUNUSED(pos) )
239 {
240 m_selection = 0;
241
242 m_dialogStyle = style;
243
244 wxBeginBusyCursor();
245
246 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
247
248 // 1) text message
249 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
250
251 // 2) list box
252 m_listbox = new wxListBox( this, wxID_LISTBOX, wxDefaultPosition, wxSize(160,100) ,
253 n, choices, wxLB_ALWAYS_SB );
254 m_listbox->SetSelection( m_selection );
255 if (clientData)
256 {
257 for (int i = 0; i < n; i++)
258 m_listbox->SetClientData(i, clientData[i]);
259 }
260 topsizer->Add( m_listbox, 1, wxEXPAND | wxLEFT|wxRIGHT, 15 );
261
262 #if wxUSE_STATLINE
263 // 3) static line
264 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
265 #endif
266
267 // 4) buttons
268 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxCENTRE | wxALL, 10 );
269
270 SetAutoLayout( TRUE );
271 SetSizer( topsizer );
272
273 topsizer->SetSizeHints( this );
274 topsizer->Fit( this );
275
276 Centre( wxBOTH );
277
278 m_listbox->SetFocus();
279
280 wxEndBusyCursor();
281
282 return TRUE;
283 }
284
285 // Set the selection
286 void wxSingleChoiceDialog::SetSelection(int sel)
287 {
288 m_listbox->SetSelection(sel);
289 m_selection = sel;
290 }
291
292 void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
293 {
294 m_selection = m_listbox->GetSelection();
295 m_stringSelection = m_listbox->GetStringSelection();
296 // TODO!
297 #ifndef __WXMOTIF__
298 if ( m_listbox->HasClientUntypedData() )
299 SetClientData(m_listbox->GetClientData(m_selection));
300 #endif
301 EndModal(wxID_OK);
302 }
303
304 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
305 {
306 m_selection = m_listbox->GetSelection();
307 m_stringSelection = m_listbox->GetStringSelection();
308
309 // TODO!
310 #ifndef __WXMOTIF__
311 if ( m_listbox->HasClientUntypedData() )
312 SetClientData(m_listbox->GetClientData(m_selection));
313 #endif
314
315 EndModal(wxID_OK);
316 }
317