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