]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/choicdlg.cpp
wxWindowDC now uses its window's font
[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 #endif
32
33 #if wxUSE_STATLINE
34 #include "wx/statline.h"
35 #endif
36
37 #include "wx/gtk/choicdlg.h"
38
39 /* Split message, using constraints to position controls */
40 static wxSize wxSplitMessage2( const wxString &message, wxWindow *parent )
41 {
42 int y = 10;
43 int w = 50;
44 wxString line( _T("") );
45 for (size_t pos = 0; pos < message.Len(); pos++)
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);
74 }
75
76
77 wxString wxGetSingleChoice( const wxString& message, const wxString& caption, int n,
78 const wxString *choices, wxWindow *parent,
79 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
80 int WXUNUSED(width), int WXUNUSED(height) )
81 {
82 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
83 if ( dialog.ShowModal() == wxID_OK )
84 return dialog.GetStringSelection();
85 else
86 return _T("");
87 }
88
89 // Overloaded for backward compatibility
90 wxString wxGetSingleChoice( const wxString& message, const wxString& caption, int n,
91 char *choices[], wxWindow *parent,
92 int x, int y, bool centre,
93 int width, int height )
94 {
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;
105 }
106
107 int wxGetSingleChoiceIndex( const wxString& message, const wxString& caption, int n,
108 const wxString *choices, wxWindow *parent,
109 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
110 int WXUNUSED(width), int WXUNUSED(height) )
111 {
112 wxSingleChoiceDialog dialog(parent, message, caption, n, choices);
113 if ( dialog.ShowModal() == wxID_OK )
114 return dialog.GetSelection();
115 else
116 return -1;
117 }
118
119 // Overloaded for backward compatibility
120 int wxGetSingleChoiceIndex( const wxString& message, const wxString& caption, int n,
121 wxChar *choices[], wxWindow *parent,
122 int x, int y, bool centre,
123 int width, int height )
124 {
125 wxString *strings = new wxString[n];
126 for ( int i = 0; i < n; i++)
127 strings[i] = choices[i];
128 int ans = wxGetSingleChoiceIndex(message, caption, n, (const wxString *)strings, parent,
129 x, y, centre, width, height);
130 delete[] strings;
131 return ans;
132 }
133
134 wxChar *wxGetSingleChoiceData( const wxString& message, const wxString& caption, int n,
135 const wxString *choices, char **client_data, wxWindow *parent,
136 int WXUNUSED(x), int WXUNUSED(y), bool WXUNUSED(centre),
137 int WXUNUSED(width), int WXUNUSED(height) )
138 {
139 wxSingleChoiceDialog dialog(parent, message, caption, n, choices, client_data);
140 if ( dialog.ShowModal() == wxID_OK )
141 return (wxChar *)dialog.GetSelectionClientData();
142 else
143 return NULL;
144 }
145
146 // Overloaded for backward compatibility
147 wxChar *wxGetSingleChoiceData( const wxString& message, const wxString& caption, int n,
148 wxChar *choices[], char **client_data, wxWindow *parent,
149 int x, int y, bool centre,
150 int width, int height )
151 {
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;
162 }
163
164
165 /* Multiple choice dialog contributed by Robert Cowell
166 *
167
168 The new data passed are in the "int nsel" and "int * selection"
169
170 The idea is to make a multiple selection from list of strings.
171 The returned value is the total number selected. initialily there
172 are nsel selected, with indices stored in
173 selection[0],...,selection[nsel-1] which appear highlighted to
174 begin with. On exit with value i
175 selection[0..i-1] contains the indices of the selected items.
176 (Some prior selectecions might be deselected.)
177 Thus selection must be as big as choices, in case all items are
178 selected.
179
180 */
181 /*
182 int wxGetMultipleChoice(const wxString& message, const wxString& caption,
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)
187 {
188 return -1;
189 }
190 */
191
192 // wxSingleChoiceDialog
193
194 #if !USE_SHARED_LIBRARY
195 BEGIN_EVENT_TABLE(wxSingleChoiceDialog, wxDialog)
196 EVT_BUTTON(wxID_OK, wxSingleChoiceDialog::OnOK)
197 EVT_LISTBOX_DCLICK(wxID_LISTBOX, wxSingleChoiceDialog::OnListBoxDClick)
198 END_EVENT_TABLE()
199
200 IMPLEMENT_CLASS(wxSingleChoiceDialog, wxDialog)
201 #endif
202
203 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent, const wxString& message, const wxString& caption,
204 int n, const wxString *choices, char **clientData, long style, const wxPoint& pos):
205 wxDialog(parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL|wxTAB_TRAVERSAL)
206 {
207 Create(parent, message, caption, n, choices, clientData, style);
208 }
209
210 wxSingleChoiceDialog::wxSingleChoiceDialog(wxWindow *parent, const wxString& message, const wxString& caption,
211 const wxStringList& choices, char **clientData, long style, const wxPoint& pos):
212 wxDialog(parent, -1, caption, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL)
213 {
214 Create(parent, message, caption, choices, clientData, style);
215 }
216
217 bool wxSingleChoiceDialog::Create(wxWindow *parent, const wxString& message, const wxString& caption,
218 const wxStringList& choices, char **clientData, long style, 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), const wxString& message,
232 const wxString& WXUNUSED(caption), int n,
233 const wxString *choices, char **clientData, long style,
234 const wxPoint& WXUNUSED(pos) )
235 {
236 m_dialogStyle = style;
237 m_selection = 0;
238 m_stringSelection = _T("");
239 m_clientData = NULL;
240
241 wxBeginBusyCursor();
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 = 100;
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)
257 {
258 for (int i = 0; i < n; i++)
259 listBox->SetClientData(i, clientData[i]);
260 }
261
262 y += listbox_height + 35;
263
264 if (style & wxOK)
265 {
266 ok = new wxButton( this, wxID_OK, _("OK"), wxPoint(-1,y), wxSize(80,-1) );
267 m_buttons.Append( ok );
268 }
269
270 if (style & wxCANCEL)
271 {
272 cancel = new wxButton( this, wxID_CANCEL, _("Cancel"), wxPoint(-1,y), wxSize(80,-1) );
273 m_buttons.Append( cancel );
274 }
275
276 if (ok)
277 {
278 ok->SetDefault();
279 ok->SetFocus();
280 }
281
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 );
306
307 wxEndBusyCursor();
308
309 return TRUE;
310 }
311
312 // Set the selection
313 void wxSingleChoiceDialog::SetSelection(int sel)
314 {
315 wxListBox *listBox = (wxListBox *)FindWindow(wxID_LISTBOX);
316 if (listBox)
317 {
318 listBox->SetSelection(sel);
319 }
320 m_selection = sel;
321 }
322
323 void wxSingleChoiceDialog::OnOK(wxCommandEvent& WXUNUSED(event))
324 {
325 wxListBox *listBox = (wxListBox *)FindWindow(wxID_LISTBOX);
326 if ( listBox )
327 {
328 m_selection = listBox->GetSelection();
329 m_stringSelection = listBox->GetStringSelection();
330 m_clientData = (char*)listBox->GetClientData(m_selection);
331 }
332
333 EndModal(wxID_OK);
334 }
335
336 void wxSingleChoiceDialog::OnListBoxDClick(wxCommandEvent& WXUNUSED(event))
337 {
338 wxListBox *listBox = (wxListBox *)FindWindow(wxID_LISTBOX);
339 if ( listBox )
340 {
341 m_selection = listBox->GetSelection();
342 m_stringSelection = listBox->GetStringSelection();
343 m_clientData = (char*)listBox->GetClientData(m_selection);
344 }
345
346 EndModal(wxID_OK);
347 }
348