]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/radiobox.cpp
wxIsNumeric for values < 0
[wxWidgets.git] / src / gtk / radiobox.cpp
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[],
68 int WXUNUSED(majorDim),
69 long style, const wxValidator& validator,
70 const wxString &name )
71 {
72 m_alreadySent = FALSE;
73 m_needParent = TRUE;
74
75 PreCreation( parent, id, pos, size, style, name );
76
77 SetValidator( validator );
78
79 m_widget = gtk_frame_new( title );
80
81 int x = m_x+5;
82 int y = m_y+15;
83 int maxLen = 0;
84 int height = 20;
85 int width = 0;
86
87 GtkRadioButton *m_radio = (GtkRadioButton*) NULL;
88
89 if (m_windowStyle & wxRA_HORIZONTAL)
90 {
91 GSList *radio_button_group = (GSList *) NULL;
92 for (int i = 0; i < n; i++)
93 {
94 if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
95
96 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i] ) );
97
98 m_boxes.Append( (wxObject*) m_radio );
99
100 ConnectWidget( GTK_WIDGET(m_radio) );
101
102 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
103
104 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
105 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
106
107 gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), GTK_WIDGET(m_radio), x, y );
108
109 int tmp = 25+gdk_string_measure( GTK_WIDGET(m_radio)->style->font, choices[i] );
110 if (tmp > maxLen) maxLen = tmp;
111
112 width = m_width-10;
113 if (size.x == -1) width = tmp;
114 gtk_widget_set_usize( GTK_WIDGET(m_radio), width, 20 );
115
116 y += 20;
117 height += 20;
118 }
119 width = maxLen + 10;
120 }
121 else
122 {
123 int max = 0;
124 for (int i = 0; i < n; i++)
125 {
126 GdkFont *font = m_widget->style->font;
127 int len = 27+gdk_string_measure( font, choices[i] );
128 if (len > max) max = len;
129 }
130
131 GSList *radio_button_group = (GSList *) NULL;
132 for (int i = 0; i < n; i++)
133 {
134 if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
135
136 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i] ) );
137
138 m_boxes.Append( (wxObject*) m_radio );
139
140 ConnectWidget( GTK_WIDGET(m_radio) );
141
142 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
143
144 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
145 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
146
147 gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), GTK_WIDGET(m_radio), x, y );
148
149 gtk_widget_set_usize( GTK_WIDGET(m_radio), max, 20 );
150
151 x += max;
152 }
153
154 width = max*n + 10;
155 height = 40;
156 }
157
158 wxSize newSize = size;
159 if (newSize.x == -1) newSize.x = width;
160 if (newSize.y == -1) newSize.y = height;
161 SetSize( newSize.x, newSize.y );
162
163 m_parent->AddChild( this );
164
165 (m_parent->m_insertCallback)( m_parent, this );
166
167 PostCreation();
168
169 SetLabel( title );
170
171 SetBackgroundColour( parent->GetBackgroundColour() );
172 SetForegroundColour( parent->GetForegroundColour() );
173
174 Show( TRUE );
175
176 return TRUE;
177 }
178
179 wxRadioBox::~wxRadioBox(void)
180 {
181 wxNode *node = m_boxes.First();
182 while (node)
183 {
184 GtkWidget *button = GTK_WIDGET( node->Data() );
185 gtk_widget_destroy( button );
186 node = node->Next();
187 }
188 }
189
190 void wxRadioBox::OnSize( wxSizeEvent &event )
191 {
192 wxControl::OnSize( event );
193
194 int x = m_x+5;
195 int y = m_y+15;
196
197 if (m_windowStyle & wxRA_HORIZONTAL)
198 {
199 wxNode *node = m_boxes.First();
200 while (node)
201 {
202 GtkWidget *button = GTK_WIDGET( node->Data() );
203
204 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y );
205 y += 20;
206
207 int w = m_width-10;
208 if (w < 15) w = 15;
209 gtk_widget_set_usize( button, w, 20 );
210
211 node = node->Next();
212 }
213 }
214 else
215 {
216 int max = 0;
217
218 wxNode *node = m_boxes.First();
219 while (node)
220 {
221 GtkButton *button = GTK_BUTTON( node->Data() );
222 GtkLabel *label = GTK_LABEL( button->child );
223
224 GdkFont *font = m_widget->style->font;
225 int len = 27+gdk_string_measure( font, label->label );
226 if (len > max) max = len;
227
228 node = node->Next();
229 }
230
231 node = m_boxes.First();
232 while (node)
233 {
234 GtkWidget *button = GTK_WIDGET( node->Data() );
235
236 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y );
237 x += max;
238 gtk_widget_set_usize( button, max, 20 );
239
240 node = node->Next();
241 }
242 }
243 }
244
245 bool wxRadioBox::Show( bool show )
246 {
247 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid radiobox" );
248
249 wxWindow::Show( show );
250
251 wxNode *node = m_boxes.First();
252 while (node)
253 {
254 GtkWidget *button = GTK_WIDGET( node->Data() );
255
256 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
257
258 node = node->Next();
259 }
260
261 return TRUE;
262 }
263
264 int wxRadioBox::FindString( const wxString &s ) const
265 {
266 wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" );
267
268 int count = 0;
269
270 wxNode *node = m_boxes.First();
271 while (node)
272 {
273 GtkButton *button = GTK_BUTTON( node->Data() );
274
275 GtkLabel *label = GTK_LABEL( button->child );
276 if (s == label->label) return count;
277 count++;
278
279 node = node->Next();
280 }
281
282 return -1;
283 }
284
285 void wxRadioBox::SetSelection( int n )
286 {
287 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
288
289 wxNode *node = m_boxes.Nth( n );
290
291 wxCHECK_RET( node, "radiobox wrong index" );
292
293 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
294
295 gtk_toggle_button_set_state( button, 1 );
296 }
297
298 int wxRadioBox::GetSelection(void) const
299 {
300 wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" );
301
302 int count = 0;
303
304 wxNode *node = m_boxes.First();
305 while (node)
306 {
307 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
308 if (button->active) return count;
309 count++;
310 node = node->Next();
311 }
312
313 wxFAIL_MSG( "wxRadioBox none selected" );
314
315 return -1;
316 }
317
318 wxString wxRadioBox::GetString( int n ) const
319 {
320 wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" );
321
322 wxNode *node = m_boxes.Nth( n );
323
324 wxCHECK_MSG( node, "", "radiobox wrong index" );
325
326 GtkButton *button = GTK_BUTTON( node->Data() );
327 GtkLabel *label = GTK_LABEL( button->child );
328
329 return wxString( label->label );
330 }
331
332 wxString wxRadioBox::GetLabel( int item ) const
333 {
334 wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" );
335
336 return GetString( item );
337 }
338
339 void wxRadioBox::SetLabel( const wxString& label )
340 {
341 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
342
343 wxControl::SetLabel( label );
344
345 gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel() );
346 }
347
348 void wxRadioBox::SetLabel( int item, const wxString& label )
349 {
350 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
351
352 wxNode *node = m_boxes.Nth( item );
353
354 wxCHECK_RET( node, "radiobox wrong index" );
355
356 GtkButton *button = GTK_BUTTON( node->Data() );
357 GtkLabel *g_label = GTK_LABEL( button->child );
358
359 gtk_label_set( g_label, label );
360 }
361
362 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
363 {
364 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
365 }
366
367 void wxRadioBox::Enable( bool enable )
368 {
369 wxControl::Enable( enable );
370
371 wxNode *node = m_boxes.First();
372 while (node)
373 {
374 GtkButton *button = GTK_BUTTON( node->Data() );
375 GtkWidget *label = button->child;
376 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
377 gtk_widget_set_sensitive( label, enable );
378 node = node->Next();
379 }
380 }
381
382 void wxRadioBox::Enable( int item, bool enable )
383 {
384 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
385
386 wxNode *node = m_boxes.Nth( item );
387
388 wxCHECK_RET( node, "radiobox wrong index" );
389
390 GtkButton *button = GTK_BUTTON( node->Data() );
391 GtkWidget *label = button->child;
392 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
393 gtk_widget_set_sensitive( label, enable );
394 }
395
396 void wxRadioBox::Show( int item, bool show )
397 {
398 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
399
400 wxNode *node = m_boxes.Nth( item );
401
402 wxCHECK_RET( node, "radiobox wrong index" );
403
404 GtkWidget *button = GTK_WIDGET( node->Data() );
405
406 if (show)
407 gtk_widget_show( button );
408 else
409 gtk_widget_hide( button );
410 }
411
412 wxString wxRadioBox::GetStringSelection(void) const
413 {
414 wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" );
415
416 wxNode *node = m_boxes.First();
417 while (node)
418 {
419 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
420 if (button->active)
421 {
422 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
423 return label->label;
424 }
425 node = node->Next();
426 }
427
428 wxFAIL_MSG( "wxRadioBox none selected" );
429 return "";
430 }
431
432 bool wxRadioBox::SetStringSelection( const wxString &s )
433 {
434 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid radiobox" );
435
436 int res = FindString( s );
437 if (res == -1) return FALSE;
438 SetSelection( res );
439
440 return TRUE;
441 }
442
443 int wxRadioBox::Number(void) const
444 {
445 return m_boxes.Number();
446 }
447
448 int wxRadioBox::GetNumberOfRowsOrCols(void) const
449 {
450 return 1;
451 }
452
453 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
454 {
455 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
456 }
457
458 void wxRadioBox::ApplyWidgetStyle()
459 {
460 SetWidgetStyle();
461
462 gtk_widget_set_style( m_widget, m_widgetStyle );
463
464 wxNode *node = m_boxes.First();
465 while (node)
466 {
467 GtkWidget *widget = GTK_WIDGET( node->Data() );
468 gtk_widget_set_style( widget, m_widgetStyle );
469
470 GtkButton *button = GTK_BUTTON( node->Data() );
471 gtk_widget_set_style( button->child, m_widgetStyle );
472
473 node = node->Next();
474 }
475 }
476
477 bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
478 {
479 if (window == m_widget->window) return TRUE;
480
481 wxNode *node = m_boxes.First();
482 while (node)
483 {
484 GtkWidget *button = GTK_WIDGET( node->Data() );
485
486 if (window == button->window) return TRUE;
487
488 node = node->Next();
489 }
490
491 return FALSE;
492 }