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