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