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