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