]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/radiobox.cpp
wxWave class for wxGTK (linux)
[wxWidgets.git] / src / gtk1 / radiobox.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: radiobox.cpp
3// Purpose:
4// Author: Robert Roebling
f96aa4d9
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
c801d85f
KB
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
66bd6b93
RR
20//-----------------------------------------------------------------------------
21// data
22//-----------------------------------------------------------------------------
23
24extern bool g_blockEventsOnDrag;
25
c801d85f 26//-----------------------------------------------------------------------------
b4071e91 27// "clicked"
c801d85f
KB
28//-----------------------------------------------------------------------------
29
66bd6b93 30static void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioBox *rb )
c801d85f 31{
66bd6b93
RR
32 if (!rb->HasVMT()) return;
33 if (g_blockEventsOnDrag) return;
34
47908e25
RR
35 if (rb->m_alreadySent)
36 {
37 rb->m_alreadySent = FALSE;
38 return;
39 }
40
41 rb->m_alreadySent = TRUE;
42
c801d85f
KB
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 );
47908e25 48 rb->GetEventHandler()->ProcessEvent(event);
6de97a3b 49}
c801d85f 50
b4071e91
RR
51//-----------------------------------------------------------------------------
52// wxRadioBox
c801d85f
KB
53//-----------------------------------------------------------------------------
54
55IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl)
56
3f659fd6
RR
57BEGIN_EVENT_TABLE(wxRadioBox, wxControl)
58 EVT_SIZE(wxRadioBox::OnSize)
59END_EVENT_TABLE()
60
c801d85f
KB
61wxRadioBox::wxRadioBox(void)
62{
6de97a3b 63}
c801d85f 64
debe6624
JS
65bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title,
66 const wxPoint &pos, const wxSize &size,
6de97a3b
RR
67 int n, const wxString choices[], int WXUNUSED(majorDim),
68 long style, const wxValidator& validator, const wxString &name )
c801d85f 69{
47908e25 70 m_alreadySent = FALSE;
c801d85f
KB
71 m_needParent = TRUE;
72
73 PreCreation( parent, id, pos, size, style, name );
74
6de97a3b
RR
75 SetValidator( validator );
76
c801d85f
KB
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;
e5403d7c 83 int width = 0;
c801d85f 84
d6d1892b
RR
85 GtkRadioButton *m_radio = (GtkRadioButton*) NULL;
86
e5403d7c 87 if (m_windowStyle & wxRA_VERTICAL)
c801d85f 88 {
c67daf87 89 GSList *radio_button_group = (GSList *) NULL;
c801d85f
KB
90 for (int i = 0; i < n; i++)
91 {
92 if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
47908e25 93
c801d85f
KB
94 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i] ) );
95
d6d1892b
RR
96 m_boxes.Append( (wxObject*) m_radio );
97
b4071e91
RR
98 ConnectWidget( GTK_WIDGET(m_radio) );
99
c801d85f
KB
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
e5403d7c 110 width = m_width-10;
c801d85f
KB
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
6de97a3b 117 }
e5403d7c
RR
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;
6de97a3b 155 }
d6d1892b 156
c801d85f 157 wxSize newSize = size;
e5403d7c 158 if (newSize.x == -1) newSize.x = width;
c801d85f
KB
159 if (newSize.y == -1) newSize.y = height;
160 SetSize( newSize.x, newSize.y );
161
162 PostCreation();
163
d6d1892b
RR
164 SetLabel( title );
165
f96aa4d9
RR
166 SetBackgroundColour( parent->GetBackgroundColour() );
167
c801d85f
KB
168 Show( TRUE );
169
170 return TRUE;
6de97a3b 171}
c801d85f 172
d6d1892b
RR
173wxRadioBox::~wxRadioBox(void)
174{
175 wxNode *node = m_boxes.First();
176 while (node)
177 {
178 GtkWidget *button = GTK_WIDGET( node->Data() );
179 gtk_widget_destroy( button );
180 node = node->Next();
181 }
182}
183
b4071e91 184void wxRadioBox::OnSize( wxSizeEvent &event )
3f659fd6 185{
b4071e91
RR
186 wxControl::OnSize( event );
187
3f659fd6
RR
188 int x = m_x+5;
189 int y = m_y+15;
190
e5403d7c 191 if (m_windowStyle & wxRA_VERTICAL)
3f659fd6 192 {
e5403d7c
RR
193 wxNode *node = m_boxes.First();
194 while (node)
195 {
196 GtkWidget *button = GTK_WIDGET( node->Data() );
3f659fd6 197
e5403d7c
RR
198 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y );
199 y += 20;
3f659fd6 200
e5403d7c
RR
201 int w = m_width-10;
202 if (w < 15) w = 15;
203 gtk_widget_set_usize( button, w, 20 );
e55ad60e 204
e5403d7c
RR
205 node = node->Next();
206 }
207 }
208 else
209 {
210 int max = 0;
211
212 wxNode *node = m_boxes.First();
213 while (node)
214 {
215 GtkButton *button = GTK_BUTTON( node->Data() );
216 GtkLabel *label = GTK_LABEL( button->child );
217
218 GdkFont *font = m_widget->style->font;
219 int len = 27+gdk_string_measure( font, label->label );
220 if (len > max) max = len;
221
222 node = node->Next();
223 }
224
225 node = m_boxes.First();
226 while (node)
227 {
228 GtkWidget *button = GTK_WIDGET( node->Data() );
229
230 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y );
231 x += max;
232 gtk_widget_set_usize( button, max, 20 );
233
234 node = node->Next();
235 }
3f659fd6
RR
236 }
237}
238
debe6624 239bool wxRadioBox::Show( bool show )
c801d85f 240{
f96aa4d9
RR
241 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid radiobox" );
242
c801d85f
KB
243 wxWindow::Show( show );
244
d6d1892b
RR
245 wxNode *node = m_boxes.First();
246 while (node)
c801d85f 247 {
d6d1892b
RR
248 GtkWidget *button = GTK_WIDGET( node->Data() );
249
250 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
251
252 node = node->Next();
6de97a3b 253 }
c801d85f
KB
254
255 return TRUE;
6de97a3b 256}
c801d85f 257
47908e25 258int wxRadioBox::FindString( const wxString &s ) const
c801d85f 259{
f96aa4d9
RR
260 wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" );
261
d6d1892b 262 int count = 0;
47908e25 263
d6d1892b
RR
264 wxNode *node = m_boxes.First();
265 while (node)
47908e25 266 {
d6d1892b
RR
267 GtkButton *button = GTK_BUTTON( node->Data() );
268
269 GtkLabel *label = GTK_LABEL( button->child );
270 if (s == label->label) return count;
271 count++;
272
273 node = node->Next();
6de97a3b 274 }
d6d1892b 275
47908e25 276 return -1;
6de97a3b 277}
c801d85f 278
47908e25 279void wxRadioBox::SetSelection( int n )
c801d85f 280{
f96aa4d9
RR
281 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
282
d6d1892b 283 wxNode *node = m_boxes.Nth( n );
d3904ceb 284
d6d1892b 285 if (!node)
d3904ceb
RR
286 {
287 wxFAIL_MSG( "wxRadioBox wrong index" );
288 return;
289 }
47908e25 290
d6d1892b 291 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
47908e25
RR
292
293 gtk_toggle_button_set_state( button, 1 );
6de97a3b 294}
c801d85f
KB
295
296int wxRadioBox::GetSelection(void) const
297{
f96aa4d9
RR
298 wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" );
299
c801d85f 300 int count = 0;
d6d1892b
RR
301
302 wxNode *node = m_boxes.First();
303 while (node)
c801d85f 304 {
d6d1892b
RR
305 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
306 if (button->active) return count;
c801d85f 307 count++;
d6d1892b 308 node = node->Next();
6de97a3b 309 }
2b5bd7fd 310
d6d1892b
RR
311 wxFAIL_MSG( "wxRadioBox none selected" );
312
313 return -1;
6de97a3b 314}
c801d85f 315
47908e25 316wxString wxRadioBox::GetString( int n ) const
c801d85f 317{
f96aa4d9
RR
318 wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" );
319
d6d1892b 320 wxNode *node = m_boxes.Nth( n );
47908e25 321
d6d1892b 322 if (!node)
d3904ceb
RR
323 {
324 wxFAIL_MSG( "wxRadioBox wrong index" );
325 return "";
326 }
47908e25 327
d6d1892b 328 GtkButton *button = GTK_BUTTON( node->Data() );
d3904ceb 329 GtkLabel *label = GTK_LABEL( button->child );
47908e25
RR
330
331 return wxString( label->label );
6de97a3b 332}
c801d85f 333
d3904ceb 334wxString wxRadioBox::GetLabel( int item ) const
c801d85f 335{
f96aa4d9
RR
336 wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" );
337
d3904ceb 338 return GetString( item );
6de97a3b 339}
c801d85f 340
d3904ceb 341void wxRadioBox::SetLabel( const wxString& label )
c801d85f 342{
f96aa4d9
RR
343 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
344
d3904ceb 345 wxControl::SetLabel( label );
f96aa4d9
RR
346
347 gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel() );
6de97a3b 348}
c801d85f 349
d3904ceb 350void wxRadioBox::SetLabel( int item, const wxString& label )
c801d85f 351{
f96aa4d9
RR
352 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
353
d6d1892b 354 wxNode *node = m_boxes.Nth( item );
d3904ceb 355
d6d1892b 356 if (!node)
d3904ceb
RR
357 {
358 wxFAIL_MSG( "wxRadioBox wrong index" );
359 return;
360 }
361
d6d1892b 362 GtkButton *button = GTK_BUTTON( node->Data() );
d3904ceb
RR
363 GtkLabel *g_label = GTK_LABEL( button->child );
364
365 gtk_label_set( g_label, label );
6de97a3b 366}
c801d85f 367
debe6624 368void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
c801d85f 369{
a3622daa 370 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
6de97a3b 371}
c801d85f 372
d3904ceb 373void wxRadioBox::Enable( bool enable )
c801d85f 374{
d3904ceb
RR
375 wxControl::Enable( enable );
376
d6d1892b
RR
377 wxNode *node = m_boxes.First();
378 while (node)
d3904ceb 379 {
d6d1892b 380 GtkButton *button = GTK_BUTTON( node->Data() );
d3904ceb
RR
381 GtkWidget *label = button->child;
382 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
383 gtk_widget_set_sensitive( label, enable );
d6d1892b 384 node = node->Next();
d3904ceb 385 }
6de97a3b 386}
c801d85f 387
d3904ceb 388void wxRadioBox::Enable( int item, bool enable )
c801d85f 389{
d6d1892b 390 wxNode *node = m_boxes.Nth( item );
d3904ceb 391
d6d1892b 392 if (!node)
d3904ceb
RR
393 {
394 wxFAIL_MSG( "wxRadioBox wrong index" );
395 return;
396 }
397
d6d1892b 398 GtkButton *button = GTK_BUTTON( node->Data() );
d3904ceb
RR
399 GtkWidget *label = button->child;
400 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
401 gtk_widget_set_sensitive( label, enable );
6de97a3b 402}
c801d85f 403
d3904ceb 404void wxRadioBox::Show( int item, bool show )
c801d85f 405{
d6d1892b 406 wxNode *node = m_boxes.Nth( item );
d3904ceb 407
d6d1892b 408 if (!node)
d3904ceb
RR
409 {
410 wxFAIL_MSG( "wxRadioBox wrong index" );
411 return;
412 }
413
d6d1892b 414 GtkWidget *button = GTK_WIDGET( node->Data() );
c801d85f 415
d3904ceb
RR
416 if (show)
417 gtk_widget_show( button );
418 else
419 gtk_widget_hide( button );
6de97a3b 420}
c801d85f
KB
421
422wxString wxRadioBox::GetStringSelection(void) const
423{
d6d1892b
RR
424 wxNode *node = m_boxes.First();
425 while (node)
c801d85f 426 {
d6d1892b
RR
427 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
428 if (button->active)
c801d85f 429 {
d6d1892b 430 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
c801d85f 431 return label->label;
6de97a3b 432 }
d6d1892b 433 node = node->Next();
6de97a3b 434 }
d6d1892b
RR
435
436 wxFAIL_MSG( "wxRadioBox none selected" );
c801d85f 437 return "";
6de97a3b 438}
c801d85f 439
47908e25 440bool wxRadioBox::SetStringSelection( const wxString&s )
c801d85f 441{
47908e25
RR
442 int res = FindString( s );
443 if (res == -1) return FALSE;
444 SetSelection( res );
c801d85f 445 return TRUE;
6de97a3b 446}
c801d85f
KB
447
448int wxRadioBox::Number(void) const
449{
d6d1892b 450 return m_boxes.Number();
6de97a3b 451}
c801d85f
KB
452
453int wxRadioBox::GetNumberOfRowsOrCols(void) const
454{
455 return 1;
6de97a3b 456}
c801d85f 457
debe6624 458void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
c801d85f 459{
a3622daa 460 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
6de97a3b 461}
c801d85f 462
868a2826
RR
463void wxRadioBox::SetFont( const wxFont &font )
464{
f96aa4d9
RR
465 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
466
467 wxControl::SetFont( font );
868a2826 468
a81258be
RR
469 gtk_widget_set_style( m_widget, m_widgetStyle );
470
d6d1892b
RR
471 wxNode *node = m_boxes.First();
472 while (node)
868a2826 473 {
d6d1892b 474 GtkButton *button = GTK_BUTTON( node->Data() );
868a2826 475
a81258be 476 gtk_widget_set_style( button->child, m_widgetStyle );
868a2826 477
d6d1892b 478 node = node->Next();
868a2826
RR
479 }
480}
b4071e91 481
f96aa4d9
RR
482void wxRadioBox::SetBackgroundColour( const wxColour &colour )
483{
f96aa4d9
RR
484 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
485
486 wxControl::SetBackgroundColour( colour );
487
488 if (!m_backgroundColour.Ok()) return;
489
a81258be
RR
490 gtk_widget_set_style( m_widget, m_widgetStyle );
491
f96aa4d9
RR
492 wxNode *node = m_boxes.First();
493 while (node)
494 {
495 GtkWidget *button = GTK_WIDGET( node->Data() );
496
a81258be 497 gtk_widget_set_style( button, m_widgetStyle );
f96aa4d9
RR
498
499 node = node->Next();
500 }
501}
502
b4071e91
RR
503bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
504{
505 if (window == m_widget->window) return TRUE;
506
d6d1892b
RR
507 wxNode *node = m_boxes.First();
508 while (node)
b4071e91 509 {
d6d1892b
RR
510 GtkWidget *button = GTK_WIDGET( node->Data() );
511
b4071e91 512 if (window == button->window) return TRUE;
d6d1892b
RR
513
514 node = node->Next();
b4071e91
RR
515 }
516
517 return FALSE;
518}