]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/radiobox.cpp
compilation fixes
[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
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
e3e717ec
VZ
116 gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow),
117 GTK_WIDGET(m_radio),
f03fc89f 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
e3e717ec
VZ
166 if ( m_majorDim == 0 )
167 {
168 // avoid dividing by 0 below
169 wxFAIL_MSG( "dimension of radiobox should not be 0!" );
170
171 m_majorDim = 1;
172 }
173
d3b4d113 174 int num_per_major = (m_boxes.GetCount() - 1) / m_majorDim +1;
29006414 175
d3b4d113 176 wxSize res( 0, 0 );
29006414 177
d3b4d113
RR
178 if (m_windowStyle & wxRA_HORIZONTAL)
179 {
29006414 180
d3b4d113 181 for (int j = 0; j < m_majorDim; j++)
29006414 182 {
bbe0af5b 183 y = 15;
29006414 184
d3b4d113
RR
185 int max_len = 0;
186 wxNode *node = m_boxes.Nth( j*num_per_major );
29006414
VZ
187 for (int i1 = 0; i1< num_per_major; i1++)
188 {
d3b4d113
RR
189 GtkWidget *button = GTK_WIDGET( node->Data() );
190 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
191 GdkFont *font = m_widget->style->font;
bbe0af5b 192 int len = 22+gdk_string_measure( font, label->label );
d3b4d113 193 if (len > max_len) max_len = len;
29006414 194
a2053b27 195 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y );
dcf924a3 196 y += 22;
29006414
VZ
197
198 node = node->Next();
199 if (!node) break;
200 }
201
202 // we don't know the max_len before
203
d3b4d113 204 node = m_boxes.Nth( j*num_per_major );
29006414
VZ
205 for (int i2 = 0; i2< num_per_major; i2++)
206 {
d3b4d113 207 GtkWidget *button = GTK_WIDGET( node->Data() );
29006414 208
a2053b27 209 gtk_myfixed_resize( GTK_MYFIXED(m_parent->m_wxwindow), button, max_len, 20 );
29006414
VZ
210
211 node = node->Next();
212 if (!node) break;
213 }
214
215 if (y > res.y) res.y = y;
216
217 x += max_len + 2;
d3b4d113 218 }
29006414
VZ
219
220 res.x = x+4;
221 res.y += 9;
e5403d7c 222 }
d3b4d113 223 else
e5403d7c 224 {
d3b4d113
RR
225 int max = 0;
226
227 wxNode *node = m_boxes.First();
228 while (node)
229 {
230 GtkButton *button = GTK_BUTTON( node->Data() );
231 GtkLabel *label = GTK_LABEL( button->child );
29006414 232
d3b4d113 233 GdkFont *font = m_widget->style->font;
bbe0af5b 234 int len = 22+gdk_string_measure( font, label->label );
d3b4d113 235 if (len > max) max = len;
29006414 236
d3b4d113
RR
237 node = node->Next();
238 }
29006414 239
d3b4d113
RR
240 node = m_boxes.First();
241 while (node)
242 {
243 GtkWidget *button = GTK_WIDGET( node->Data() );
29006414 244
a2053b27 245 gtk_myfixed_set_size( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y, max, 20 );
d3b4d113 246 x += max;
29006414 247
d3b4d113
RR
248 node = node->Next();
249 }
29006414
VZ
250 res.x = x+4;
251 res.y = 42;
e5403d7c 252 }
29006414 253
d3b4d113 254 return res;
3f659fd6
RR
255}
256
debe6624 257bool wxRadioBox::Show( bool show )
c801d85f 258{
b019151f 259 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobox") );
29006414 260
907789a0 261 wxWindow::Show( show );
c801d85f 262
ba2a0103 263 if ((m_windowStyle & wxNO_BORDER) != 0)
b0351fc9 264 gtk_widget_hide( m_widget );
e3e717ec 265
907789a0
RR
266 wxNode *node = m_boxes.First();
267 while (node)
268 {
269 GtkWidget *button = GTK_WIDGET( node->Data() );
29006414 270
907789a0 271 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
29006414 272
907789a0
RR
273 node = node->Next();
274 }
c801d85f 275
907789a0 276 return TRUE;
6de97a3b 277}
c801d85f 278
47908e25 279int wxRadioBox::FindString( const wxString &s ) const
c801d85f 280{
b019151f 281 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid radiobox") );
29006414 282
907789a0 283 int count = 0;
29006414 284
907789a0
RR
285 wxNode *node = m_boxes.First();
286 while (node)
287 {
288 GtkButton *button = GTK_BUTTON( node->Data() );
29006414 289
907789a0
RR
290 GtkLabel *label = GTK_LABEL( button->child );
291 if (s == label->label) return count;
292 count++;
29006414 293
907789a0
RR
294 node = node->Next();
295 }
29006414 296
907789a0 297 return -1;
6de97a3b 298}
c801d85f 299
b292e2f5
RR
300void wxRadioBox::SetFocus()
301{
b019151f 302 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 303
b292e2f5 304 if (m_boxes.GetCount() == 0) return;
29006414 305
b292e2f5
RR
306 wxNode *node = m_boxes.First();
307 while (node)
308 {
309 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
310 if (button->active)
29006414 311 {
b292e2f5 312 gtk_widget_grab_focus( GTK_WIDGET(button) );
29006414
VZ
313
314 return;
315 }
b292e2f5
RR
316 node = node->Next();
317 }
29006414 318
b292e2f5
RR
319}
320
47908e25 321void wxRadioBox::SetSelection( int n )
c801d85f 322{
b019151f 323 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 324
907789a0 325 wxNode *node = m_boxes.Nth( n );
29006414 326
b019151f 327 wxCHECK_RET( node, _T("radiobox wrong index") );
29006414 328
907789a0 329 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
29006414 330
907789a0 331 gtk_toggle_button_set_state( button, 1 );
6de97a3b 332}
c801d85f
KB
333
334int wxRadioBox::GetSelection(void) const
335{
b019151f 336 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid radiobox") );
29006414 337
907789a0 338 int count = 0;
29006414 339
907789a0
RR
340 wxNode *node = m_boxes.First();
341 while (node)
342 {
343 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
344 if (button->active) return count;
345 count++;
346 node = node->Next();
347 }
29006414 348
b019151f 349 wxFAIL_MSG( _T("wxRadioBox none selected") );
29006414 350
907789a0 351 return -1;
6de97a3b 352}
c801d85f 353
47908e25 354wxString wxRadioBox::GetString( int n ) const
c801d85f 355{
b019151f 356 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
29006414 357
907789a0 358 wxNode *node = m_boxes.Nth( n );
29006414 359
b019151f 360 wxCHECK_MSG( node, _T(""), _T("radiobox wrong index") );
29006414 361
907789a0
RR
362 GtkButton *button = GTK_BUTTON( node->Data() );
363 GtkLabel *label = GTK_LABEL( button->child );
29006414 364
907789a0 365 return wxString( label->label );
6de97a3b 366}
c801d85f 367
d3904ceb 368wxString wxRadioBox::GetLabel( int item ) const
c801d85f 369{
b019151f 370 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
29006414 371
907789a0 372 return GetString( item );
6de97a3b 373}
c801d85f 374
d3904ceb 375void wxRadioBox::SetLabel( const wxString& label )
c801d85f 376{
b019151f 377 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 378
907789a0 379 wxControl::SetLabel( label );
29006414 380
b019151f 381 gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel().mbc_str() );
6de97a3b 382}
c801d85f 383
d3904ceb 384void wxRadioBox::SetLabel( int item, const wxString& label )
c801d85f 385{
b019151f 386 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 387
907789a0 388 wxNode *node = m_boxes.Nth( item );
29006414 389
b019151f 390 wxCHECK_RET( node, _T("radiobox wrong index") );
29006414 391
907789a0
RR
392 GtkButton *button = GTK_BUTTON( node->Data() );
393 GtkLabel *g_label = GTK_LABEL( button->child );
29006414 394
b019151f 395 gtk_label_set( g_label, label.mbc_str() );
6de97a3b 396}
c801d85f 397
debe6624 398void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
c801d85f 399{
b019151f 400 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
6de97a3b 401}
c801d85f 402
f03fc89f 403bool wxRadioBox::Enable( bool enable )
c801d85f 404{
f03fc89f
VZ
405 if ( !wxControl::Enable( enable ) )
406 return FALSE;
29006414 407
907789a0
RR
408 wxNode *node = m_boxes.First();
409 while (node)
410 {
411 GtkButton *button = GTK_BUTTON( node->Data() );
412 GtkWidget *label = button->child;
413 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
414 gtk_widget_set_sensitive( label, enable );
415 node = node->Next();
416 }
f03fc89f
VZ
417
418 return TRUE;
6de97a3b 419}
c801d85f 420
d3904ceb 421void wxRadioBox::Enable( int item, bool enable )
c801d85f 422{
b019151f 423 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 424
907789a0 425 wxNode *node = m_boxes.Nth( item );
29006414 426
b019151f 427 wxCHECK_RET( node, _T("radiobox wrong index") );
29006414 428
907789a0
RR
429 GtkButton *button = GTK_BUTTON( node->Data() );
430 GtkWidget *label = button->child;
431 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
432 gtk_widget_set_sensitive( label, enable );
6de97a3b 433}
c801d85f 434
d3904ceb 435void wxRadioBox::Show( int item, bool show )
c801d85f 436{
b019151f 437 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 438
907789a0 439 wxNode *node = m_boxes.Nth( item );
29006414 440
b019151f 441 wxCHECK_RET( node, _T("radiobox wrong index") );
29006414 442
907789a0 443 GtkWidget *button = GTK_WIDGET( node->Data() );
c801d85f 444
907789a0
RR
445 if (show)
446 gtk_widget_show( button );
447 else
448 gtk_widget_hide( button );
6de97a3b 449}
c801d85f
KB
450
451wxString wxRadioBox::GetStringSelection(void) const
452{
b019151f 453 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
29006414 454
907789a0
RR
455 wxNode *node = m_boxes.First();
456 while (node)
c801d85f 457 {
907789a0
RR
458 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
459 if (button->active)
460 {
461 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
462 return label->label;
463 }
464 node = node->Next();
6de97a3b 465 }
29006414 466
b019151f
OK
467 wxFAIL_MSG( _T("wxRadioBox none selected") );
468 return _T("");
6de97a3b 469}
c801d85f 470
907789a0 471bool wxRadioBox::SetStringSelection( const wxString &s )
c801d85f 472{
b019151f 473 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobox") );
29006414 474
907789a0
RR
475 int res = FindString( s );
476 if (res == -1) return FALSE;
477 SetSelection( res );
29006414 478
907789a0 479 return TRUE;
6de97a3b 480}
c801d85f
KB
481
482int wxRadioBox::Number(void) const
483{
907789a0 484 return m_boxes.Number();
6de97a3b 485}
c801d85f
KB
486
487int wxRadioBox::GetNumberOfRowsOrCols(void) const
488{
907789a0 489 return 1;
6de97a3b 490}
c801d85f 491
debe6624 492void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
c801d85f 493{
b019151f 494 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
6de97a3b 495}
c801d85f 496
58614078 497void wxRadioBox::ApplyWidgetStyle()
868a2826 498{
907789a0 499 SetWidgetStyle();
29006414 500
907789a0 501 gtk_widget_set_style( m_widget, m_widgetStyle );
29006414 502
907789a0
RR
503 wxNode *node = m_boxes.First();
504 while (node)
505 {
506 GtkWidget *widget = GTK_WIDGET( node->Data() );
507 gtk_widget_set_style( widget, m_widgetStyle );
29006414 508
907789a0
RR
509 GtkButton *button = GTK_BUTTON( node->Data() );
510 gtk_widget_set_style( button->child, m_widgetStyle );
29006414 511
907789a0
RR
512 node = node->Next();
513 }
868a2826 514}
b4071e91
RR
515
516bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
517{
907789a0 518 if (window == m_widget->window) return TRUE;
29006414 519
907789a0
RR
520 wxNode *node = m_boxes.First();
521 while (node)
522 {
523 GtkWidget *button = GTK_WIDGET( node->Data() );
29006414 524
907789a0 525 if (window == button->window) return TRUE;
29006414 526
907789a0
RR
527 node = node->Next();
528 }
29006414 529
907789a0 530 return FALSE;
b4071e91 531}
dcf924a3
RR
532
533#endif