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