Fix for TextCtrl problem as reported by Vegh
[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 = 25+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 gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), m_widget, m_x, m_y );
163 gtk_widget_set_usize( m_widget, m_width, m_height );
164
165 PostCreation();
166
167 SetLabel( title );
168
169 SetBackgroundColour( parent->GetBackgroundColour() );
170 SetForegroundColour( parent->GetForegroundColour() );
171
172 Show( TRUE );
173
174 return TRUE;
175 }
176
177 wxRadioBox::~wxRadioBox(void)
178 {
179 wxNode *node = m_boxes.First();
180 while (node)
181 {
182 GtkWidget *button = GTK_WIDGET( node->Data() );
183 gtk_widget_destroy( button );
184 node = node->Next();
185 }
186 }
187
188 void wxRadioBox::OnSize( wxSizeEvent &event )
189 {
190 wxControl::OnSize( event );
191
192 int x = m_x+5;
193 int y = m_y+15;
194
195 if (m_windowStyle & wxRA_VERTICAL)
196 {
197 wxNode *node = m_boxes.First();
198 while (node)
199 {
200 GtkWidget *button = GTK_WIDGET( node->Data() );
201
202 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y );
203 y += 20;
204
205 int w = m_width-10;
206 if (w < 15) w = 15;
207 gtk_widget_set_usize( button, w, 20 );
208
209 node = node->Next();
210 }
211 }
212 else
213 {
214 int max = 0;
215
216 wxNode *node = m_boxes.First();
217 while (node)
218 {
219 GtkButton *button = GTK_BUTTON( node->Data() );
220 GtkLabel *label = GTK_LABEL( button->child );
221
222 GdkFont *font = m_widget->style->font;
223 int len = 27+gdk_string_measure( font, label->label );
224 if (len > max) max = len;
225
226 node = node->Next();
227 }
228
229 node = m_boxes.First();
230 while (node)
231 {
232 GtkWidget *button = GTK_WIDGET( node->Data() );
233
234 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y );
235 x += max;
236 gtk_widget_set_usize( button, max, 20 );
237
238 node = node->Next();
239 }
240 }
241 }
242
243 bool wxRadioBox::Show( bool show )
244 {
245 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid radiobox" );
246
247 wxWindow::Show( show );
248
249 wxNode *node = m_boxes.First();
250 while (node)
251 {
252 GtkWidget *button = GTK_WIDGET( node->Data() );
253
254 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
255
256 node = node->Next();
257 }
258
259 return TRUE;
260 }
261
262 int wxRadioBox::FindString( const wxString &s ) const
263 {
264 wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" );
265
266 int count = 0;
267
268 wxNode *node = m_boxes.First();
269 while (node)
270 {
271 GtkButton *button = GTK_BUTTON( node->Data() );
272
273 GtkLabel *label = GTK_LABEL( button->child );
274 if (s == label->label) return count;
275 count++;
276
277 node = node->Next();
278 }
279
280 return -1;
281 }
282
283 void wxRadioBox::SetSelection( int n )
284 {
285 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
286
287 wxNode *node = m_boxes.Nth( n );
288
289 if (!node)
290 {
291 wxFAIL_MSG( "wxRadioBox wrong index" );
292 return;
293 }
294
295 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
296
297 gtk_toggle_button_set_state( button, 1 );
298 }
299
300 int wxRadioBox::GetSelection(void) const
301 {
302 wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" );
303
304 int count = 0;
305
306 wxNode *node = m_boxes.First();
307 while (node)
308 {
309 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
310 if (button->active) return count;
311 count++;
312 node = node->Next();
313 }
314
315 wxFAIL_MSG( "wxRadioBox none selected" );
316
317 return -1;
318 }
319
320 wxString wxRadioBox::GetString( int n ) const
321 {
322 wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" );
323
324 wxNode *node = m_boxes.Nth( n );
325
326 if (!node)
327 {
328 wxFAIL_MSG( "wxRadioBox wrong index" );
329 return "";
330 }
331
332 GtkButton *button = GTK_BUTTON( node->Data() );
333 GtkLabel *label = GTK_LABEL( button->child );
334
335 return wxString( label->label );
336 }
337
338 wxString wxRadioBox::GetLabel( int item ) const
339 {
340 wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" );
341
342 return GetString( item );
343 }
344
345 void wxRadioBox::SetLabel( const wxString& label )
346 {
347 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
348
349 wxControl::SetLabel( label );
350
351 gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel() );
352 }
353
354 void wxRadioBox::SetLabel( int item, const wxString& label )
355 {
356 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
357
358 wxNode *node = m_boxes.Nth( item );
359
360 if (!node)
361 {
362 wxFAIL_MSG( "wxRadioBox wrong index" );
363 return;
364 }
365
366 GtkButton *button = GTK_BUTTON( node->Data() );
367 GtkLabel *g_label = GTK_LABEL( button->child );
368
369 gtk_label_set( g_label, label );
370 }
371
372 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
373 {
374 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
375 }
376
377 void wxRadioBox::Enable( bool enable )
378 {
379 wxControl::Enable( enable );
380
381 wxNode *node = m_boxes.First();
382 while (node)
383 {
384 GtkButton *button = GTK_BUTTON( node->Data() );
385 GtkWidget *label = button->child;
386 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
387 gtk_widget_set_sensitive( label, enable );
388 node = node->Next();
389 }
390 }
391
392 void wxRadioBox::Enable( int item, bool enable )
393 {
394 wxNode *node = m_boxes.Nth( item );
395
396 if (!node)
397 {
398 wxFAIL_MSG( "wxRadioBox wrong index" );
399 return;
400 }
401
402 GtkButton *button = GTK_BUTTON( node->Data() );
403 GtkWidget *label = button->child;
404 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
405 gtk_widget_set_sensitive( label, enable );
406 }
407
408 void wxRadioBox::Show( int item, bool show )
409 {
410 wxNode *node = m_boxes.Nth( item );
411
412 if (!node)
413 {
414 wxFAIL_MSG( "wxRadioBox wrong index" );
415 return;
416 }
417
418 GtkWidget *button = GTK_WIDGET( node->Data() );
419
420 if (show)
421 gtk_widget_show( button );
422 else
423 gtk_widget_hide( button );
424 }
425
426 wxString wxRadioBox::GetStringSelection(void) const
427 {
428 wxNode *node = m_boxes.First();
429 while (node)
430 {
431 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
432 if (button->active)
433 {
434 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
435 return label->label;
436 }
437 node = node->Next();
438 }
439
440 wxFAIL_MSG( "wxRadioBox none selected" );
441 return "";
442 }
443
444 bool wxRadioBox::SetStringSelection( const wxString&s )
445 {
446 int res = FindString( s );
447 if (res == -1) return FALSE;
448 SetSelection( res );
449 return TRUE;
450 }
451
452 int wxRadioBox::Number(void) const
453 {
454 return m_boxes.Number();
455 }
456
457 int wxRadioBox::GetNumberOfRowsOrCols(void) const
458 {
459 return 1;
460 }
461
462 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
463 {
464 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
465 }
466
467 void wxRadioBox::ApplyWidgetStyle()
468 {
469 SetWidgetStyle();
470
471 gtk_widget_set_style( m_widget, m_widgetStyle );
472
473 wxNode *node = m_boxes.First();
474 while (node)
475 {
476 GtkWidget *widget = GTK_WIDGET( node->Data() );
477 gtk_widget_set_style( widget, m_widgetStyle );
478
479 GtkButton *button = GTK_BUTTON( node->Data() );
480 gtk_widget_set_style( button->child, m_widgetStyle );
481
482 node = node->Next();
483 }
484 }
485
486 bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
487 {
488 if (window == m_widget->window) return TRUE;
489
490 wxNode *node = m_boxes.First();
491 while (node)
492 {
493 GtkWidget *button = GTK_WIDGET( node->Data() );
494
495 if (window == button->window) return TRUE;
496
497 node = node->Next();
498 }
499
500 return FALSE;
501 }