]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/radiobox.cpp
1. wxTextControl::SetFont() does something (good or bad - for you to judge)
[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 #ifdef __GNUG__
11 #pragma implementation "radiobox.h"
12 #endif
13
14 #include "wx/radiobox.h"
15
16 #if wxUSE_RADIOBOX
17
18 #include "wx/dialog.h"
19 #include "wx/frame.h"
20
21 #include "gdk/gdk.h"
22 #include "gtk/gtk.h"
23 #include "wx/gtk/win_gtk.h"
24
25 //-----------------------------------------------------------------------------
26 // idle system
27 //-----------------------------------------------------------------------------
28
29 extern void wxapp_install_idle_handler();
30 extern bool g_isIdle;
31
32 //-----------------------------------------------------------------------------
33 // data
34 //-----------------------------------------------------------------------------
35
36 extern bool g_blockEventsOnDrag;
37
38 //-----------------------------------------------------------------------------
39 // "clicked"
40 //-----------------------------------------------------------------------------
41
42 static void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioBox *rb )
43 {
44 if (g_isIdle) wxapp_install_idle_handler();
45
46 if (!rb->m_hasVMT) return;
47 if (g_blockEventsOnDrag) return;
48
49 if (rb->m_alreadySent)
50 {
51 rb->m_alreadySent = FALSE;
52 return;
53 }
54
55 rb->m_alreadySent = TRUE;
56
57 wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, rb->GetId() );
58 event.SetInt( rb->GetSelection() );
59 event.SetString( rb->GetStringSelection() );
60 event.SetEventObject( rb );
61 rb->GetEventHandler()->ProcessEvent(event);
62 }
63
64 //-----------------------------------------------------------------------------
65 // wxRadioBox
66 //-----------------------------------------------------------------------------
67
68 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl)
69
70 BEGIN_EVENT_TABLE(wxRadioBox, wxControl)
71 EVT_SIZE(wxRadioBox::OnSize)
72 END_EVENT_TABLE()
73
74 wxRadioBox::wxRadioBox()
75 {
76 }
77
78 bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title,
79 const wxPoint &pos, const wxSize &size,
80 int n, const wxString choices[], int majorDim,
81 long style, const wxValidator& validator,
82 const wxString &name )
83 {
84 m_alreadySent = FALSE;
85 m_needParent = TRUE;
86 m_acceptsFocus = TRUE;
87
88 PreCreation( parent, id, pos, size, style, name );
89
90 #if wxUSE_VALIDATORS
91 SetValidator( validator );
92 #endif
93
94 m_widget = gtk_frame_new( title.mbc_str() );
95
96 m_majorDim = majorDim;
97
98 GtkRadioButton *m_radio = (GtkRadioButton*) NULL;
99
100 GSList *radio_button_group = (GSList *) NULL;
101 for (int i = 0; i < n; i++)
102 {
103 if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
104
105 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i].mbc_str() ) );
106
107 m_boxes.Append( (wxObject*) m_radio );
108
109 ConnectWidget( GTK_WIDGET(m_radio) );
110
111 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
112
113 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
114 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
115
116 gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow),
117 GTK_WIDGET(m_radio),
118 m_x+10, m_y+10+(i*24), 10, 10 );
119 }
120
121 wxSize ls = LayoutItems();
122
123 wxSize newSize = size;
124 if (newSize.x == -1) newSize.x = ls.x;
125 if (newSize.y == -1) newSize.y = ls.y;
126 SetSize( newSize.x, newSize.y );
127
128 m_parent->DoAddChild( this );
129
130 PostCreation();
131
132 SetLabel( title );
133
134 SetBackgroundColour( parent->GetBackgroundColour() );
135 SetForegroundColour( parent->GetForegroundColour() );
136 SetFont( parent->GetFont() );
137
138 Show( TRUE );
139
140 return TRUE;
141 }
142
143 wxRadioBox::~wxRadioBox()
144 {
145 wxNode *node = m_boxes.First();
146 while (node)
147 {
148 GtkWidget *button = GTK_WIDGET( node->Data() );
149 gtk_widget_destroy( button );
150 node = node->Next();
151 }
152 }
153
154 void wxRadioBox::OnSize( wxSizeEvent &event )
155 {
156 LayoutItems();
157
158 event.Skip();
159 }
160
161 wxSize wxRadioBox::LayoutItems()
162 {
163 int x = 7;
164 int y = 15;
165
166 if ( m_majorDim == 0 )
167 {
168 // avoid dividing by 0 below
169 wxFAIL_MSG( "dimension of radiobox should not be 0!" );
170
171 m_majorDim = 1;
172 }
173
174 int num_per_major = (m_boxes.GetCount() - 1) / m_majorDim +1;
175
176 wxSize res( 0, 0 );
177
178 if (m_windowStyle & wxRA_HORIZONTAL)
179 {
180
181 for (int j = 0; j < m_majorDim; j++)
182 {
183 y = 15;
184
185 int max_len = 0;
186 wxNode *node = m_boxes.Nth( j*num_per_major );
187 for (int i1 = 0; i1< num_per_major; i1++)
188 {
189 GtkWidget *button = GTK_WIDGET( node->Data() );
190 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
191 GdkFont *font = m_widget->style->font;
192 int len = 22+gdk_string_measure( font, label->label );
193 if (len > max_len) max_len = len;
194
195 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y );
196 y += 22;
197
198 node = node->Next();
199 if (!node) break;
200 }
201
202 // we don't know the max_len before
203
204 node = m_boxes.Nth( j*num_per_major );
205 for (int i2 = 0; i2< num_per_major; i2++)
206 {
207 GtkWidget *button = GTK_WIDGET( node->Data() );
208
209 gtk_myfixed_resize( GTK_MYFIXED(m_parent->m_wxwindow), button, max_len, 20 );
210
211 node = node->Next();
212 if (!node) break;
213 }
214
215 if (y > res.y) res.y = y;
216
217 x += max_len + 2;
218 }
219
220 res.x = x+4;
221 res.y += 9;
222 }
223 else
224 {
225 int max = 0;
226
227 wxNode *node = m_boxes.First();
228 while (node)
229 {
230 GtkButton *button = GTK_BUTTON( node->Data() );
231 GtkLabel *label = GTK_LABEL( button->child );
232
233 GdkFont *font = m_widget->style->font;
234 int len = 22+gdk_string_measure( font, label->label );
235 if (len > max) max = len;
236
237 node = node->Next();
238 }
239
240 node = m_boxes.First();
241 while (node)
242 {
243 GtkWidget *button = GTK_WIDGET( node->Data() );
244
245 gtk_myfixed_set_size( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y, max, 20 );
246 x += max;
247
248 node = node->Next();
249 }
250 res.x = x+4;
251 res.y = 42;
252 }
253
254 return res;
255 }
256
257 bool wxRadioBox::Show( bool show )
258 {
259 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobox") );
260
261 wxWindow::Show( show );
262
263 if ((m_windowStyle & wxNO_BORDER) != 0)
264 gtk_widget_hide( m_widget );
265
266 wxNode *node = m_boxes.First();
267 while (node)
268 {
269 GtkWidget *button = GTK_WIDGET( node->Data() );
270
271 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
272
273 node = node->Next();
274 }
275
276 return TRUE;
277 }
278
279 int wxRadioBox::FindString( const wxString &s ) const
280 {
281 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid radiobox") );
282
283 int count = 0;
284
285 wxNode *node = m_boxes.First();
286 while (node)
287 {
288 GtkButton *button = GTK_BUTTON( node->Data() );
289
290 GtkLabel *label = GTK_LABEL( button->child );
291 if (s == label->label) return count;
292 count++;
293
294 node = node->Next();
295 }
296
297 return -1;
298 }
299
300 void wxRadioBox::SetFocus()
301 {
302 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
303
304 if (m_boxes.GetCount() == 0) return;
305
306 wxNode *node = m_boxes.First();
307 while (node)
308 {
309 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
310 if (button->active)
311 {
312 gtk_widget_grab_focus( GTK_WIDGET(button) );
313
314 return;
315 }
316 node = node->Next();
317 }
318
319 }
320
321 void wxRadioBox::SetSelection( int n )
322 {
323 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
324
325 wxNode *node = m_boxes.Nth( n );
326
327 wxCHECK_RET( node, _T("radiobox wrong index") );
328
329 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
330
331 gtk_toggle_button_set_state( button, 1 );
332 }
333
334 int wxRadioBox::GetSelection(void) const
335 {
336 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid radiobox") );
337
338 int count = 0;
339
340 wxNode *node = m_boxes.First();
341 while (node)
342 {
343 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
344 if (button->active) return count;
345 count++;
346 node = node->Next();
347 }
348
349 wxFAIL_MSG( _T("wxRadioBox none selected") );
350
351 return -1;
352 }
353
354 wxString wxRadioBox::GetString( int n ) const
355 {
356 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
357
358 wxNode *node = m_boxes.Nth( n );
359
360 wxCHECK_MSG( node, _T(""), _T("radiobox wrong index") );
361
362 GtkButton *button = GTK_BUTTON( node->Data() );
363 GtkLabel *label = GTK_LABEL( button->child );
364
365 return wxString( label->label );
366 }
367
368 wxString wxRadioBox::GetLabel( int item ) const
369 {
370 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
371
372 return GetString( item );
373 }
374
375 void wxRadioBox::SetLabel( const wxString& label )
376 {
377 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
378
379 wxControl::SetLabel( label );
380
381 gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel().mbc_str() );
382 }
383
384 void wxRadioBox::SetLabel( int item, const wxString& label )
385 {
386 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
387
388 wxNode *node = m_boxes.Nth( item );
389
390 wxCHECK_RET( node, _T("radiobox wrong index") );
391
392 GtkButton *button = GTK_BUTTON( node->Data() );
393 GtkLabel *g_label = GTK_LABEL( button->child );
394
395 gtk_label_set( g_label, label.mbc_str() );
396 }
397
398 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
399 {
400 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
401 }
402
403 bool wxRadioBox::Enable( bool enable )
404 {
405 if ( !wxControl::Enable( enable ) )
406 return FALSE;
407
408 wxNode *node = m_boxes.First();
409 while (node)
410 {
411 GtkButton *button = GTK_BUTTON( node->Data() );
412 GtkWidget *label = button->child;
413 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
414 gtk_widget_set_sensitive( label, enable );
415 node = node->Next();
416 }
417
418 return TRUE;
419 }
420
421 void wxRadioBox::Enable( int item, bool enable )
422 {
423 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
424
425 wxNode *node = m_boxes.Nth( item );
426
427 wxCHECK_RET( node, _T("radiobox wrong index") );
428
429 GtkButton *button = GTK_BUTTON( node->Data() );
430 GtkWidget *label = button->child;
431 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
432 gtk_widget_set_sensitive( label, enable );
433 }
434
435 void wxRadioBox::Show( int item, bool show )
436 {
437 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
438
439 wxNode *node = m_boxes.Nth( item );
440
441 wxCHECK_RET( node, _T("radiobox wrong index") );
442
443 GtkWidget *button = GTK_WIDGET( node->Data() );
444
445 if (show)
446 gtk_widget_show( button );
447 else
448 gtk_widget_hide( button );
449 }
450
451 wxString wxRadioBox::GetStringSelection(void) const
452 {
453 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
454
455 wxNode *node = m_boxes.First();
456 while (node)
457 {
458 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
459 if (button->active)
460 {
461 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
462 return label->label;
463 }
464 node = node->Next();
465 }
466
467 wxFAIL_MSG( _T("wxRadioBox none selected") );
468 return _T("");
469 }
470
471 bool wxRadioBox::SetStringSelection( const wxString &s )
472 {
473 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobox") );
474
475 int res = FindString( s );
476 if (res == -1) return FALSE;
477 SetSelection( res );
478
479 return TRUE;
480 }
481
482 int wxRadioBox::Number(void) const
483 {
484 return m_boxes.Number();
485 }
486
487 int wxRadioBox::GetNumberOfRowsOrCols(void) const
488 {
489 return 1;
490 }
491
492 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
493 {
494 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
495 }
496
497 void wxRadioBox::ApplyWidgetStyle()
498 {
499 SetWidgetStyle();
500
501 gtk_widget_set_style( m_widget, m_widgetStyle );
502
503 wxNode *node = m_boxes.First();
504 while (node)
505 {
506 GtkWidget *widget = GTK_WIDGET( node->Data() );
507 gtk_widget_set_style( widget, m_widgetStyle );
508
509 GtkButton *button = GTK_BUTTON( node->Data() );
510 gtk_widget_set_style( button->child, m_widgetStyle );
511
512 node = node->Next();
513 }
514 }
515
516 bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
517 {
518 if (window == m_widget->window) return TRUE;
519
520 wxNode *node = m_boxes.First();
521 while (node)
522 {
523 GtkWidget *button = GTK_WIDGET( node->Data() );
524
525 if (window == button->window) return TRUE;
526
527 node = node->Next();
528 }
529
530 return FALSE;
531 }
532
533 #endif