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