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