]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/radiobox.cpp
Re-added corrupt binary files; small change to filefn.cpp for Cygwin.
[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 #include "wx/gtk/win_gtk.h"
19
20 //-----------------------------------------------------------------------------
21 // data
22 //-----------------------------------------------------------------------------
23
24 extern bool g_blockEventsOnDrag;
25
26 //-----------------------------------------------------------------------------
27 // "clicked"
28 //-----------------------------------------------------------------------------
29
30 static void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioBox *rb )
31 {
32 if (!rb->HasVMT()) return;
33 if (g_blockEventsOnDrag) return;
34
35 if (rb->m_alreadySent)
36 {
37 rb->m_alreadySent = FALSE;
38 return;
39 }
40
41 rb->m_alreadySent = TRUE;
42
43 wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, rb->GetId() );
44 event.SetInt( rb->GetSelection() );
45 wxString tmp( rb->GetStringSelection() );
46 event.SetString( WXSTRINGCAST(tmp) );
47 event.SetEventObject( rb );
48 rb->GetEventHandler()->ProcessEvent(event);
49 }
50
51 //-----------------------------------------------------------------------------
52 // wxRadioBox
53 //-----------------------------------------------------------------------------
54
55 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl)
56
57 BEGIN_EVENT_TABLE(wxRadioBox, wxControl)
58 EVT_SIZE(wxRadioBox::OnSize)
59 END_EVENT_TABLE()
60
61 wxRadioBox::wxRadioBox(void)
62 {
63 }
64
65 bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title,
66 const wxPoint &pos, const wxSize &size,
67 int n, const wxString choices[],
68 int WXUNUSED(majorDim),
69 long style, const wxValidator& validator,
70 const wxString &name )
71 {
72 m_alreadySent = FALSE;
73 m_needParent = TRUE;
74 m_acceptsFocus = TRUE;
75
76 PreCreation( parent, id, pos, size, style, name );
77
78 SetValidator( validator );
79
80 m_widget = gtk_frame_new( title );
81
82 int x = m_x+5;
83 int y = m_y+15;
84 int maxLen = 0;
85 int height = 20;
86 int width = 0;
87
88 GtkRadioButton *m_radio = (GtkRadioButton*) NULL;
89
90 if (m_windowStyle & wxRA_HORIZONTAL)
91 {
92 GSList *radio_button_group = (GSList *) NULL;
93 for (int i = 0; i < n; i++)
94 {
95 if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
96
97 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i] ) );
98
99 m_boxes.Append( (wxObject*) m_radio );
100
101 ConnectWidget( GTK_WIDGET(m_radio) );
102
103 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
104
105 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
106 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
107
108 gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), GTK_WIDGET(m_radio), x, y );
109
110 int tmp = 25+gdk_string_measure( GTK_WIDGET(m_radio)->style->font, choices[i] );
111 if (tmp > maxLen) maxLen = tmp;
112
113 width = m_width-10;
114 if (size.x == -1) width = tmp;
115 gtk_widget_set_usize( GTK_WIDGET(m_radio), width, 20 );
116
117 y += 20;
118 height += 20;
119 }
120 width = maxLen + 10;
121 }
122 else
123 {
124 int max = 0;
125 for (int i = 0; i < n; i++)
126 {
127 GdkFont *font = m_widget->style->font;
128 int len = 27+gdk_string_measure( font, choices[i] );
129 if (len > max) max = len;
130 }
131
132 GSList *radio_button_group = (GSList *) NULL;
133 for (int i = 0; i < n; i++)
134 {
135 if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
136
137 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i] ) );
138
139 m_boxes.Append( (wxObject*) m_radio );
140
141 ConnectWidget( GTK_WIDGET(m_radio) );
142
143 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
144
145 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
146 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
147
148 gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), GTK_WIDGET(m_radio), x, y );
149
150 gtk_widget_set_usize( GTK_WIDGET(m_radio), max, 20 );
151
152 x += max;
153 }
154
155 width = max*n + 10;
156 height = 40;
157 }
158
159 wxSize newSize = size;
160 if (newSize.x == -1) newSize.x = width;
161 if (newSize.y == -1) newSize.y = height;
162 SetSize( newSize.x, newSize.y );
163
164 m_parent->AddChild( this );
165
166 (m_parent->m_insertCallback)( m_parent, this );
167
168 PostCreation();
169
170 SetLabel( title );
171
172 SetBackgroundColour( parent->GetBackgroundColour() );
173 SetForegroundColour( parent->GetForegroundColour() );
174
175 Show( TRUE );
176
177 return TRUE;
178 }
179
180 wxRadioBox::~wxRadioBox(void)
181 {
182 wxNode *node = m_boxes.First();
183 while (node)
184 {
185 GtkWidget *button = GTK_WIDGET( node->Data() );
186 gtk_widget_destroy( button );
187 node = node->Next();
188 }
189 }
190
191 void wxRadioBox::OnSize( wxSizeEvent &event )
192 {
193 wxControl::OnSize( event );
194
195 int x = m_x+5;
196 int y = m_y+15;
197
198 if (m_windowStyle & wxRA_HORIZONTAL)
199 {
200 wxNode *node = m_boxes.First();
201 while (node)
202 {
203 GtkWidget *button = GTK_WIDGET( node->Data() );
204
205 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y );
206 y += 20;
207
208 int w = m_width-10;
209 if (w < 15) w = 15;
210 gtk_widget_set_usize( button, w, 20 );
211
212 node = node->Next();
213 }
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 = 27+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_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y );
238 x += max;
239 gtk_widget_set_usize( button, max, 20 );
240
241 node = node->Next();
242 }
243 }
244 }
245
246 bool wxRadioBox::Show( bool show )
247 {
248 wxCHECK_MSG( m_widget != NULL, FALSE, "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, "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, "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, "invalid radiobox" );
310
311 wxNode *node = m_boxes.Nth( n );
312
313 wxCHECK_RET( node, "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, "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( "wxRadioBox none selected" );
336
337 return -1;
338 }
339
340 wxString wxRadioBox::GetString( int n ) const
341 {
342 wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" );
343
344 wxNode *node = m_boxes.Nth( n );
345
346 wxCHECK_MSG( node, "", "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, "", "invalid radiobox" );
357
358 return GetString( item );
359 }
360
361 void wxRadioBox::SetLabel( const wxString& label )
362 {
363 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
364
365 wxControl::SetLabel( label );
366
367 gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel() );
368 }
369
370 void wxRadioBox::SetLabel( int item, const wxString& label )
371 {
372 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
373
374 wxNode *node = m_boxes.Nth( item );
375
376 wxCHECK_RET( node, "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 );
382 }
383
384 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
385 {
386 wxFAIL_MSG("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, "invalid radiobox" );
407
408 wxNode *node = m_boxes.Nth( item );
409
410 wxCHECK_RET( node, "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, "invalid radiobox" );
421
422 wxNode *node = m_boxes.Nth( item );
423
424 wxCHECK_RET( node, "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, "", "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( "wxRadioBox none selected" );
451 return "";
452 }
453
454 bool wxRadioBox::SetStringSelection( const wxString &s )
455 {
456 wxCHECK_MSG( m_widget != NULL, FALSE, "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("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 }