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