]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/radiobox.cpp
Implementation of wxFopen, wxFreopen, wxStrftime
[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 #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 DisableEvents();
332
333 gtk_toggle_button_set_state( button, 1 );
334
335 EnableEvents();
336 }
337
338 int wxRadioBox::GetSelection(void) const
339 {
340 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid radiobox") );
341
342 int count = 0;
343
344 wxNode *node = m_boxes.First();
345 while (node)
346 {
347 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
348 if (button->active) return count;
349 count++;
350 node = node->Next();
351 }
352
353 wxFAIL_MSG( _T("wxRadioBox none selected") );
354
355 return -1;
356 }
357
358 wxString wxRadioBox::GetString( int n ) const
359 {
360 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
361
362 wxNode *node = m_boxes.Nth( n );
363
364 wxCHECK_MSG( node, _T(""), _T("radiobox wrong index") );
365
366 GtkButton *button = GTK_BUTTON( node->Data() );
367 GtkLabel *label = GTK_LABEL( button->child );
368
369 return wxString( label->label );
370 }
371
372 wxString wxRadioBox::GetLabel( int item ) const
373 {
374 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
375
376 return GetString( item );
377 }
378
379 void wxRadioBox::SetLabel( const wxString& label )
380 {
381 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
382
383 wxControl::SetLabel( label );
384
385 gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel().mbc_str() );
386 }
387
388 void wxRadioBox::SetLabel( int item, const wxString& label )
389 {
390 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
391
392 wxNode *node = m_boxes.Nth( item );
393
394 wxCHECK_RET( node, _T("radiobox wrong index") );
395
396 GtkButton *button = GTK_BUTTON( node->Data() );
397 GtkLabel *g_label = GTK_LABEL( button->child );
398
399 gtk_label_set( g_label, label.mbc_str() );
400 }
401
402 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
403 {
404 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
405 }
406
407 bool wxRadioBox::Enable( bool enable )
408 {
409 if ( !wxControl::Enable( enable ) )
410 return FALSE;
411
412 wxNode *node = m_boxes.First();
413 while (node)
414 {
415 GtkButton *button = GTK_BUTTON( node->Data() );
416 GtkWidget *label = button->child;
417 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
418 gtk_widget_set_sensitive( label, enable );
419 node = node->Next();
420 }
421
422 return TRUE;
423 }
424
425 void wxRadioBox::Enable( int item, bool enable )
426 {
427 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
428
429 wxNode *node = m_boxes.Nth( item );
430
431 wxCHECK_RET( node, _T("radiobox wrong index") );
432
433 GtkButton *button = GTK_BUTTON( node->Data() );
434 GtkWidget *label = button->child;
435 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
436 gtk_widget_set_sensitive( label, enable );
437 }
438
439 void wxRadioBox::Show( int item, bool show )
440 {
441 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
442
443 wxNode *node = m_boxes.Nth( item );
444
445 wxCHECK_RET( node, _T("radiobox wrong index") );
446
447 GtkWidget *button = GTK_WIDGET( node->Data() );
448
449 if (show)
450 gtk_widget_show( button );
451 else
452 gtk_widget_hide( button );
453 }
454
455 wxString wxRadioBox::GetStringSelection(void) const
456 {
457 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
458
459 wxNode *node = m_boxes.First();
460 while (node)
461 {
462 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
463 if (button->active)
464 {
465 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
466 return label->label;
467 }
468 node = node->Next();
469 }
470
471 wxFAIL_MSG( _T("wxRadioBox none selected") );
472 return _T("");
473 }
474
475 bool wxRadioBox::SetStringSelection( const wxString &s )
476 {
477 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobox") );
478
479 int res = FindString( s );
480 if (res == -1) return FALSE;
481 SetSelection( res );
482
483 return TRUE;
484 }
485
486 int wxRadioBox::Number(void) const
487 {
488 return m_boxes.Number();
489 }
490
491 int wxRadioBox::GetNumberOfRowsOrCols(void) const
492 {
493 return 1;
494 }
495
496 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
497 {
498 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
499 }
500
501 void wxRadioBox::DisableEvents()
502 {
503 wxNode *node = m_boxes.First();
504 while (node)
505 {
506 gtk_signal_disconnect_by_func( GTK_OBJECT(node->Data()),
507 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
508
509 node = node->Next();
510 }
511 }
512
513 void wxRadioBox::EnableEvents()
514 {
515 wxNode *node = m_boxes.First();
516 while (node)
517 {
518 gtk_signal_connect( GTK_OBJECT(node->Data()), "clicked",
519 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
520
521 node = node->Next();
522 }
523 }
524
525 void wxRadioBox::ApplyWidgetStyle()
526 {
527 SetWidgetStyle();
528
529 gtk_widget_set_style( m_widget, m_widgetStyle );
530
531 wxNode *node = m_boxes.First();
532 while (node)
533 {
534 GtkWidget *widget = GTK_WIDGET( node->Data() );
535 gtk_widget_set_style( widget, m_widgetStyle );
536
537 GtkButton *button = GTK_BUTTON( node->Data() );
538 gtk_widget_set_style( button->child, m_widgetStyle );
539
540 node = node->Next();
541 }
542 }
543
544 bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
545 {
546 if (window == m_widget->window) return TRUE;
547
548 wxNode *node = m_boxes.First();
549 while (node)
550 {
551 GtkWidget *button = GTK_WIDGET( node->Data() );
552
553 if (window == button->window) return TRUE;
554
555 node = node->Next();
556 }
557
558 return FALSE;
559 }
560
561 #endif