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