]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/radiobox.cpp
Next take at radioboxes
[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 wxString label;
101 GSList *radio_button_group = (GSList *) NULL;
102 for (int i = 0; i < n; i++)
103 {
104 if ( i != 0 )
105 radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
106
107 label.Empty();
108 for ( const wxChar *pc = choices[i]; *pc; pc++ )
109 {
110 if ( *pc != _T('&') )
111 label += *pc;
112 }
113
114 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, label.mbc_str() ) );
115
116 m_boxes.Append( (wxObject*) m_radio );
117
118 ConnectWidget( GTK_WIDGET(m_radio) );
119
120 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
121
122 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
123 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
124
125 gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow),
126 GTK_WIDGET(m_radio),
127 m_x+10, m_y+10+(i*24), 10, 10 );
128 }
129
130 wxSize ls = LayoutItems();
131
132 wxSize newSize = size;
133 if (newSize.x == -1) newSize.x = ls.x;
134 if (newSize.y == -1) newSize.y = ls.y;
135 SetSize( newSize.x, newSize.y );
136
137 m_parent->DoAddChild( this );
138
139 PostCreation();
140
141 SetLabel( title );
142
143 SetBackgroundColour( parent->GetBackgroundColour() );
144 SetForegroundColour( parent->GetForegroundColour() );
145 SetFont( parent->GetFont() );
146
147 Show( TRUE );
148
149 return TRUE;
150 }
151
152 wxRadioBox::~wxRadioBox()
153 {
154 wxNode *node = m_boxes.First();
155 while (node)
156 {
157 GtkWidget *button = GTK_WIDGET( node->Data() );
158 gtk_widget_destroy( button );
159 node = node->Next();
160 }
161 }
162
163 void wxRadioBox::OnSize( wxSizeEvent &event )
164 {
165 LayoutItems();
166
167 event.Skip();
168 }
169
170 wxSize wxRadioBox::LayoutItems()
171 {
172 int x = 7;
173 int y = 15;
174
175 if ( m_majorDim == 0 )
176 {
177 // avoid dividing by 0 below
178 wxFAIL_MSG( _T("dimension of radiobox should not be 0!") );
179
180 m_majorDim = 1;
181 }
182
183 int num_per_major = (m_boxes.GetCount() - 1) / m_majorDim +1;
184
185 wxSize res( 0, 0 );
186
187 int num_of_cols = 0;
188 int num_of_rows = 0;
189 if (HasFlag(wxRA_SPECIFY_COLS))
190 {
191 num_of_cols = m_majorDim;
192 num_of_rows = num_per_major;
193 }
194 else
195 {
196 num_of_cols = num_per_major;
197 num_of_rows = m_majorDim;
198 }
199
200 if ( HasFlag(wxRA_SPECIFY_COLS) ||
201 (HasFlag(wxRA_SPECIFY_ROWS) && (num_of_cols > 1)) )
202 {
203 for (int j = 0; j < num_of_cols; j++)
204 {
205 y = 15;
206
207 int max_len = 0;
208 wxNode *node = m_boxes.Nth( j*num_of_rows );
209 for (int i1 = 0; i1< num_of_rows; i1++)
210 {
211 GtkWidget *button = GTK_WIDGET( node->Data() );
212 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
213 GdkFont *font = m_widget->style->font;
214 int len = 22+gdk_string_measure( font, label->label );
215 if (len > max_len) max_len = len;
216
217 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y );
218 y += 22;
219
220 node = node->Next();
221 if (!node) break;
222 }
223
224 // we don't know the max_len before
225
226 node = m_boxes.Nth( j*num_of_rows );
227 for (int i2 = 0; i2< num_of_rows; i2++)
228 {
229 GtkWidget *button = GTK_WIDGET( node->Data() );
230
231 gtk_myfixed_resize( GTK_MYFIXED(m_parent->m_wxwindow), button, max_len, 20 );
232
233 node = node->Next();
234 if (!node) break;
235 }
236
237 if (y > res.y) res.y = y;
238
239 x += max_len + 2;
240 }
241
242 res.x = x+4;
243 res.y += 9;
244 }
245 else
246 {
247 int max = 0;
248
249 wxNode *node = m_boxes.First();
250 while (node)
251 {
252 GtkButton *button = GTK_BUTTON( node->Data() );
253 GtkLabel *label = GTK_LABEL( button->child );
254
255 GdkFont *font = m_widget->style->font;
256 int len = 22+gdk_string_measure( font, label->label );
257 if (len > max) max = len;
258
259 node = node->Next();
260 }
261
262 node = m_boxes.First();
263 while (node)
264 {
265 GtkWidget *button = GTK_WIDGET( node->Data() );
266
267 gtk_myfixed_set_size( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y, max, 20 );
268 x += max;
269
270 node = node->Next();
271 }
272 res.x = x+4;
273 res.y = 42;
274 }
275
276 return res;
277 }
278
279 bool wxRadioBox::Show( bool show )
280 {
281 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobox") );
282
283 wxWindow::Show( show );
284
285 if ((m_windowStyle & wxNO_BORDER) != 0)
286 gtk_widget_hide( m_widget );
287
288 wxNode *node = m_boxes.First();
289 while (node)
290 {
291 GtkWidget *button = GTK_WIDGET( node->Data() );
292
293 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
294
295 node = node->Next();
296 }
297
298 return TRUE;
299 }
300
301 int wxRadioBox::FindString( const wxString &s ) const
302 {
303 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid radiobox") );
304
305 int count = 0;
306
307 wxNode *node = m_boxes.First();
308 while (node)
309 {
310 GtkButton *button = GTK_BUTTON( node->Data() );
311
312 GtkLabel *label = GTK_LABEL( button->child );
313 if (s == label->label) return count;
314 count++;
315
316 node = node->Next();
317 }
318
319 return -1;
320 }
321
322 void wxRadioBox::SetFocus()
323 {
324 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
325
326 if (m_boxes.GetCount() == 0) return;
327
328 wxNode *node = m_boxes.First();
329 while (node)
330 {
331 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
332 if (button->active)
333 {
334 gtk_widget_grab_focus( GTK_WIDGET(button) );
335
336 return;
337 }
338 node = node->Next();
339 }
340
341 }
342
343 void wxRadioBox::SetSelection( int n )
344 {
345 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
346
347 wxNode *node = m_boxes.Nth( n );
348
349 wxCHECK_RET( node, _T("radiobox wrong index") );
350
351 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
352
353 DisableEvents();
354
355 gtk_toggle_button_set_state( button, 1 );
356
357 EnableEvents();
358 }
359
360 int wxRadioBox::GetSelection(void) const
361 {
362 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid radiobox") );
363
364 int count = 0;
365
366 wxNode *node = m_boxes.First();
367 while (node)
368 {
369 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
370 if (button->active) return count;
371 count++;
372 node = node->Next();
373 }
374
375 wxFAIL_MSG( _T("wxRadioBox none selected") );
376
377 return -1;
378 }
379
380 wxString wxRadioBox::GetString( int n ) const
381 {
382 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
383
384 wxNode *node = m_boxes.Nth( n );
385
386 wxCHECK_MSG( node, _T(""), _T("radiobox wrong index") );
387
388 GtkButton *button = GTK_BUTTON( node->Data() );
389 GtkLabel *label = GTK_LABEL( button->child );
390
391 return wxString( label->label );
392 }
393
394 wxString wxRadioBox::GetLabel( int item ) const
395 {
396 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
397
398 return GetString( item );
399 }
400
401 void wxRadioBox::SetLabel( const wxString& label )
402 {
403 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
404
405 wxControl::SetLabel( label );
406
407 gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel().mbc_str() );
408 }
409
410 void wxRadioBox::SetLabel( int item, const wxString& label )
411 {
412 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
413
414 wxNode *node = m_boxes.Nth( item );
415
416 wxCHECK_RET( node, _T("radiobox wrong index") );
417
418 GtkButton *button = GTK_BUTTON( node->Data() );
419 GtkLabel *g_label = GTK_LABEL( button->child );
420
421 gtk_label_set( g_label, label.mbc_str() );
422 }
423
424 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
425 {
426 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
427 }
428
429 bool wxRadioBox::Enable( bool enable )
430 {
431 if ( !wxControl::Enable( enable ) )
432 return FALSE;
433
434 wxNode *node = m_boxes.First();
435 while (node)
436 {
437 GtkButton *button = GTK_BUTTON( node->Data() );
438 GtkWidget *label = button->child;
439 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
440 gtk_widget_set_sensitive( label, enable );
441 node = node->Next();
442 }
443
444 return TRUE;
445 }
446
447 void wxRadioBox::Enable( int item, bool enable )
448 {
449 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
450
451 wxNode *node = m_boxes.Nth( item );
452
453 wxCHECK_RET( node, _T("radiobox wrong index") );
454
455 GtkButton *button = GTK_BUTTON( node->Data() );
456 GtkWidget *label = button->child;
457 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
458 gtk_widget_set_sensitive( label, enable );
459 }
460
461 void wxRadioBox::Show( int item, bool show )
462 {
463 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
464
465 wxNode *node = m_boxes.Nth( item );
466
467 wxCHECK_RET( node, _T("radiobox wrong index") );
468
469 GtkWidget *button = GTK_WIDGET( node->Data() );
470
471 if (show)
472 gtk_widget_show( button );
473 else
474 gtk_widget_hide( button );
475 }
476
477 wxString wxRadioBox::GetStringSelection(void) const
478 {
479 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
480
481 wxNode *node = m_boxes.First();
482 while (node)
483 {
484 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
485 if (button->active)
486 {
487 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
488 return label->label;
489 }
490 node = node->Next();
491 }
492
493 wxFAIL_MSG( _T("wxRadioBox none selected") );
494 return _T("");
495 }
496
497 bool wxRadioBox::SetStringSelection( const wxString &s )
498 {
499 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobox") );
500
501 int res = FindString( s );
502 if (res == -1) return FALSE;
503 SetSelection( res );
504
505 return TRUE;
506 }
507
508 int wxRadioBox::Number(void) const
509 {
510 return m_boxes.Number();
511 }
512
513 int wxRadioBox::GetNumberOfRowsOrCols(void) const
514 {
515 return 1;
516 }
517
518 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
519 {
520 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
521 }
522
523 void wxRadioBox::DisableEvents()
524 {
525 wxNode *node = m_boxes.First();
526 while (node)
527 {
528 gtk_signal_disconnect_by_func( GTK_OBJECT(node->Data()),
529 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
530
531 node = node->Next();
532 }
533 }
534
535 void wxRadioBox::EnableEvents()
536 {
537 wxNode *node = m_boxes.First();
538 while (node)
539 {
540 gtk_signal_connect( GTK_OBJECT(node->Data()), "clicked",
541 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
542
543 node = node->Next();
544 }
545 }
546
547 void wxRadioBox::ApplyWidgetStyle()
548 {
549 SetWidgetStyle();
550
551 gtk_widget_set_style( m_widget, m_widgetStyle );
552
553 wxNode *node = m_boxes.First();
554 while (node)
555 {
556 GtkWidget *widget = GTK_WIDGET( node->Data() );
557 gtk_widget_set_style( widget, m_widgetStyle );
558
559 GtkButton *button = GTK_BUTTON( node->Data() );
560 gtk_widget_set_style( button->child, m_widgetStyle );
561
562 node = node->Next();
563 }
564 }
565
566 bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
567 {
568 if (window == m_widget->window) return TRUE;
569
570 wxNode *node = m_boxes.First();
571 while (node)
572 {
573 GtkWidget *button = GTK_WIDGET( node->Data() );
574
575 if (window == button->window) return TRUE;
576
577 node = node->Next();
578 }
579
580 return FALSE;
581 }
582
583 #endif