]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: radiobox.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "radiobox.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/radiobox.h" | |
16 | #include "wx/dialog.h" | |
17 | #include "wx/frame.h" | |
18 | #include "wx/gtk/win_gtk.h" | |
19 | ||
20 | //----------------------------------------------------------------------------- | |
21 | // data | |
22 | //----------------------------------------------------------------------------- | |
23 | ||
24 | extern bool g_blockEventsOnDrag; | |
25 | ||
26 | //----------------------------------------------------------------------------- | |
27 | // "clicked" | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | static void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioBox *rb ) | |
31 | { | |
32 | if (!rb->HasVMT()) return; | |
33 | if (g_blockEventsOnDrag) return; | |
34 | ||
35 | if (rb->m_alreadySent) | |
36 | { | |
37 | rb->m_alreadySent = FALSE; | |
38 | return; | |
39 | } | |
40 | ||
41 | rb->m_alreadySent = TRUE; | |
42 | ||
43 | wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, rb->GetId() ); | |
44 | event.SetInt( rb->GetSelection() ); | |
45 | wxString tmp( rb->GetStringSelection() ); | |
46 | event.SetString( WXSTRINGCAST(tmp) ); | |
47 | event.SetEventObject( rb ); | |
48 | rb->GetEventHandler()->ProcessEvent(event); | |
49 | } | |
50 | ||
51 | //----------------------------------------------------------------------------- | |
52 | // wxRadioBox | |
53 | //----------------------------------------------------------------------------- | |
54 | ||
55 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl) | |
56 | ||
57 | BEGIN_EVENT_TABLE(wxRadioBox, wxControl) | |
58 | EVT_SIZE(wxRadioBox::OnSize) | |
59 | END_EVENT_TABLE() | |
60 | ||
61 | wxRadioBox::wxRadioBox(void) | |
62 | { | |
63 | } | |
64 | ||
65 | bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title, | |
66 | const wxPoint &pos, const wxSize &size, | |
67 | int n, const wxString choices[], int WXUNUSED(majorDim), | |
68 | long style, const wxValidator& validator, const wxString &name ) | |
69 | { | |
70 | m_alreadySent = FALSE; | |
71 | m_needParent = TRUE; | |
72 | ||
73 | PreCreation( parent, id, pos, size, style, name ); | |
74 | ||
75 | SetValidator( validator ); | |
76 | ||
77 | m_widget = gtk_frame_new( title ); | |
78 | ||
79 | int x = m_x+5; | |
80 | int y = m_y+15; | |
81 | int maxLen = 0; | |
82 | int height = 20; | |
83 | ||
84 | GtkRadioButton *m_radio = (GtkRadioButton*) NULL; | |
85 | ||
86 | // if (((m_style & wxRA_VERTICAL) == wxRA_VERTICAL) && (n > 0)) | |
87 | if (n > 0) | |
88 | { | |
89 | GSList *radio_button_group = (GSList *) NULL; | |
90 | for (int i = 0; i < n; i++) | |
91 | { | |
92 | if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) ); | |
93 | ||
94 | m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i] ) ); | |
95 | ||
96 | m_boxes.Append( (wxObject*) m_radio ); | |
97 | ||
98 | ConnectWidget( GTK_WIDGET(m_radio) ); | |
99 | ||
100 | if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE ); | |
101 | ||
102 | gtk_signal_connect( GTK_OBJECT(m_radio), "clicked", | |
103 | GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this ); | |
104 | ||
105 | gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), GTK_WIDGET(m_radio), x, y ); | |
106 | ||
107 | int tmp = 22+gdk_string_measure( GTK_WIDGET(m_radio)->style->font, choices[i] ); | |
108 | if (tmp > maxLen) maxLen = tmp; | |
109 | ||
110 | int width = m_width-10; | |
111 | if (size.x == -1) width = tmp; | |
112 | gtk_widget_set_usize( GTK_WIDGET(m_radio), width, 20 ); | |
113 | ||
114 | y += 20; | |
115 | height += 20; | |
116 | ||
117 | } | |
118 | } | |
119 | ||
120 | wxSize newSize = size; | |
121 | if (newSize.x == -1) newSize.x = maxLen+10; | |
122 | if (newSize.y == -1) newSize.y = height; | |
123 | SetSize( newSize.x, newSize.y ); | |
124 | ||
125 | PostCreation(); | |
126 | ||
127 | SetLabel( title ); | |
128 | ||
129 | SetBackgroundColour( parent->GetBackgroundColour() ); | |
130 | ||
131 | Show( TRUE ); | |
132 | ||
133 | return TRUE; | |
134 | } | |
135 | ||
136 | wxRadioBox::~wxRadioBox(void) | |
137 | { | |
138 | wxNode *node = m_boxes.First(); | |
139 | while (node) | |
140 | { | |
141 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
142 | gtk_widget_destroy( button ); | |
143 | node = node->Next(); | |
144 | } | |
145 | } | |
146 | ||
147 | void wxRadioBox::OnSize( wxSizeEvent &event ) | |
148 | { | |
149 | wxControl::OnSize( event ); | |
150 | ||
151 | int x = m_x+5; | |
152 | int y = m_y+15; | |
153 | ||
154 | wxNode *node = m_boxes.First(); | |
155 | while (node) | |
156 | { | |
157 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
158 | ||
159 | gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y ); | |
160 | y += 20; | |
161 | ||
162 | int w = m_width-10; | |
163 | if (w < 15) w = 15; | |
164 | gtk_widget_set_usize( button, w, 20 ); | |
165 | ||
166 | node = node->Next(); | |
167 | } | |
168 | } | |
169 | ||
170 | bool wxRadioBox::Show( bool show ) | |
171 | { | |
172 | wxCHECK_MSG( m_widget != NULL, FALSE, "invalid radiobox" ); | |
173 | ||
174 | wxWindow::Show( show ); | |
175 | ||
176 | wxNode *node = m_boxes.First(); | |
177 | while (node) | |
178 | { | |
179 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
180 | ||
181 | if (show) gtk_widget_show( button ); else gtk_widget_hide( button ); | |
182 | ||
183 | node = node->Next(); | |
184 | } | |
185 | ||
186 | return TRUE; | |
187 | } | |
188 | ||
189 | int wxRadioBox::FindString( const wxString &s ) const | |
190 | { | |
191 | wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" ); | |
192 | ||
193 | int count = 0; | |
194 | ||
195 | wxNode *node = m_boxes.First(); | |
196 | while (node) | |
197 | { | |
198 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
199 | ||
200 | GtkLabel *label = GTK_LABEL( button->child ); | |
201 | if (s == label->label) return count; | |
202 | count++; | |
203 | ||
204 | node = node->Next(); | |
205 | } | |
206 | ||
207 | return -1; | |
208 | } | |
209 | ||
210 | void wxRadioBox::SetSelection( int n ) | |
211 | { | |
212 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); | |
213 | ||
214 | wxNode *node = m_boxes.Nth( n ); | |
215 | ||
216 | if (!node) | |
217 | { | |
218 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
219 | return; | |
220 | } | |
221 | ||
222 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); | |
223 | ||
224 | gtk_toggle_button_set_state( button, 1 ); | |
225 | } | |
226 | ||
227 | int wxRadioBox::GetSelection(void) const | |
228 | { | |
229 | wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" ); | |
230 | ||
231 | int count = 0; | |
232 | ||
233 | wxNode *node = m_boxes.First(); | |
234 | while (node) | |
235 | { | |
236 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); | |
237 | if (button->active) return count; | |
238 | count++; | |
239 | node = node->Next(); | |
240 | } | |
241 | ||
242 | wxFAIL_MSG( "wxRadioBox none selected" ); | |
243 | ||
244 | return -1; | |
245 | } | |
246 | ||
247 | wxString wxRadioBox::GetString( int n ) const | |
248 | { | |
249 | wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" ); | |
250 | ||
251 | wxNode *node = m_boxes.Nth( n ); | |
252 | ||
253 | if (!node) | |
254 | { | |
255 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
256 | return ""; | |
257 | } | |
258 | ||
259 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
260 | GtkLabel *label = GTK_LABEL( button->child ); | |
261 | ||
262 | return wxString( label->label ); | |
263 | } | |
264 | ||
265 | wxString wxRadioBox::GetLabel( int item ) const | |
266 | { | |
267 | wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" ); | |
268 | ||
269 | return GetString( item ); | |
270 | } | |
271 | ||
272 | void wxRadioBox::SetLabel( const wxString& label ) | |
273 | { | |
274 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); | |
275 | ||
276 | wxControl::SetLabel( label ); | |
277 | ||
278 | gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel() ); | |
279 | } | |
280 | ||
281 | void wxRadioBox::SetLabel( int item, const wxString& label ) | |
282 | { | |
283 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); | |
284 | ||
285 | wxNode *node = m_boxes.Nth( item ); | |
286 | ||
287 | if (!node) | |
288 | { | |
289 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
290 | return; | |
291 | } | |
292 | ||
293 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
294 | GtkLabel *g_label = GTK_LABEL( button->child ); | |
295 | ||
296 | gtk_label_set( g_label, label ); | |
297 | } | |
298 | ||
299 | void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) ) | |
300 | { | |
301 | wxFAIL_MSG("wxRadioBox::SetLabel not implemented."); | |
302 | } | |
303 | ||
304 | void wxRadioBox::Enable( bool enable ) | |
305 | { | |
306 | wxControl::Enable( enable ); | |
307 | ||
308 | wxNode *node = m_boxes.First(); | |
309 | while (node) | |
310 | { | |
311 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
312 | GtkWidget *label = button->child; | |
313 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
314 | gtk_widget_set_sensitive( label, enable ); | |
315 | node = node->Next(); | |
316 | } | |
317 | } | |
318 | ||
319 | void wxRadioBox::Enable( int item, bool enable ) | |
320 | { | |
321 | wxNode *node = m_boxes.Nth( item ); | |
322 | ||
323 | if (!node) | |
324 | { | |
325 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
326 | return; | |
327 | } | |
328 | ||
329 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
330 | GtkWidget *label = button->child; | |
331 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
332 | gtk_widget_set_sensitive( label, enable ); | |
333 | } | |
334 | ||
335 | void wxRadioBox::Show( int item, bool show ) | |
336 | { | |
337 | wxNode *node = m_boxes.Nth( item ); | |
338 | ||
339 | if (!node) | |
340 | { | |
341 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
342 | return; | |
343 | } | |
344 | ||
345 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
346 | ||
347 | if (show) | |
348 | gtk_widget_show( button ); | |
349 | else | |
350 | gtk_widget_hide( button ); | |
351 | } | |
352 | ||
353 | wxString wxRadioBox::GetStringSelection(void) const | |
354 | { | |
355 | wxNode *node = m_boxes.First(); | |
356 | while (node) | |
357 | { | |
358 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); | |
359 | if (button->active) | |
360 | { | |
361 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child ); | |
362 | return label->label; | |
363 | } | |
364 | node = node->Next(); | |
365 | } | |
366 | ||
367 | wxFAIL_MSG( "wxRadioBox none selected" ); | |
368 | return ""; | |
369 | } | |
370 | ||
371 | bool wxRadioBox::SetStringSelection( const wxString&s ) | |
372 | { | |
373 | int res = FindString( s ); | |
374 | if (res == -1) return FALSE; | |
375 | SetSelection( res ); | |
376 | return TRUE; | |
377 | } | |
378 | ||
379 | int wxRadioBox::Number(void) const | |
380 | { | |
381 | return m_boxes.Number(); | |
382 | } | |
383 | ||
384 | int wxRadioBox::GetNumberOfRowsOrCols(void) const | |
385 | { | |
386 | return 1; | |
387 | } | |
388 | ||
389 | void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) ) | |
390 | { | |
391 | wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented."); | |
392 | } | |
393 | ||
394 | void wxRadioBox::SetFont( const wxFont &font ) | |
395 | { | |
396 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); | |
397 | ||
398 | wxControl::SetFont( font ); | |
399 | ||
400 | wxNode *node = m_boxes.First(); | |
401 | while (node) | |
402 | { | |
403 | GtkButton *button = GTK_BUTTON( node->Data() ); | |
404 | ||
405 | gtk_widget_set_style( button->child, | |
406 | gtk_style_ref( | |
407 | gtk_widget_get_style( m_widget ) ) ); | |
408 | ||
409 | node = node->Next(); | |
410 | } | |
411 | } | |
412 | ||
413 | void wxRadioBox::SetBackgroundColour( const wxColour &colour ) | |
414 | { | |
415 | return; | |
416 | ||
417 | wxCHECK_RET( m_widget != NULL, "invalid radiobox" ); | |
418 | ||
419 | wxControl::SetBackgroundColour( colour ); | |
420 | ||
421 | if (!m_backgroundColour.Ok()) return; | |
422 | ||
423 | wxNode *node = m_boxes.First(); | |
424 | while (node) | |
425 | { | |
426 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
427 | ||
428 | gtk_widget_set_style( button, | |
429 | gtk_style_ref( | |
430 | gtk_widget_get_style( m_widget ) ) ); | |
431 | ||
432 | node = node->Next(); | |
433 | } | |
434 | } | |
435 | ||
436 | bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window ) | |
437 | { | |
438 | if (window == m_widget->window) return TRUE; | |
439 | ||
440 | wxNode *node = m_boxes.First(); | |
441 | while (node) | |
442 | { | |
443 | GtkWidget *button = GTK_WIDGET( node->Data() ); | |
444 | ||
445 | if (window == button->window) return TRUE; | |
446 | ||
447 | node = node->Next(); | |
448 | } | |
449 | ||
450 | return FALSE; | |
451 | } |