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