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