]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/radiobox.cpp
Added wxStaticBitmap::SetIcon()
[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
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
ee6db21e 169 wxFAIL_MSG( _T("dimension of radiobox should not be 0!") );
e3e717ec
VZ
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
953704c1
RR
331 DisableEvents();
332
907789a0 333 gtk_toggle_button_set_state( button, 1 );
953704c1
RR
334
335 EnableEvents();
6de97a3b 336}
c801d85f
KB
337
338int wxRadioBox::GetSelection(void) const
339{
b019151f 340 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid radiobox") );
29006414 341
907789a0 342 int count = 0;
29006414 343
907789a0
RR
344 wxNode *node = m_boxes.First();
345 while (node)
346 {
347 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
348 if (button->active) return count;
349 count++;
350 node = node->Next();
351 }
29006414 352
b019151f 353 wxFAIL_MSG( _T("wxRadioBox none selected") );
29006414 354
907789a0 355 return -1;
6de97a3b 356}
c801d85f 357
47908e25 358wxString wxRadioBox::GetString( int n ) const
c801d85f 359{
b019151f 360 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
29006414 361
907789a0 362 wxNode *node = m_boxes.Nth( n );
29006414 363
b019151f 364 wxCHECK_MSG( node, _T(""), _T("radiobox wrong index") );
29006414 365
907789a0
RR
366 GtkButton *button = GTK_BUTTON( node->Data() );
367 GtkLabel *label = GTK_LABEL( button->child );
29006414 368
907789a0 369 return wxString( label->label );
6de97a3b 370}
c801d85f 371
d3904ceb 372wxString wxRadioBox::GetLabel( int item ) const
c801d85f 373{
b019151f 374 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
29006414 375
907789a0 376 return GetString( item );
6de97a3b 377}
c801d85f 378
d3904ceb 379void wxRadioBox::SetLabel( const wxString& label )
c801d85f 380{
b019151f 381 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 382
907789a0 383 wxControl::SetLabel( label );
29006414 384
b019151f 385 gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel().mbc_str() );
6de97a3b 386}
c801d85f 387
d3904ceb 388void wxRadioBox::SetLabel( int item, const wxString& label )
c801d85f 389{
b019151f 390 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 391
907789a0 392 wxNode *node = m_boxes.Nth( item );
29006414 393
b019151f 394 wxCHECK_RET( node, _T("radiobox wrong index") );
29006414 395
907789a0
RR
396 GtkButton *button = GTK_BUTTON( node->Data() );
397 GtkLabel *g_label = GTK_LABEL( button->child );
29006414 398
b019151f 399 gtk_label_set( g_label, label.mbc_str() );
6de97a3b 400}
c801d85f 401
debe6624 402void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
c801d85f 403{
b019151f 404 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
6de97a3b 405}
c801d85f 406
f03fc89f 407bool wxRadioBox::Enable( bool enable )
c801d85f 408{
f03fc89f
VZ
409 if ( !wxControl::Enable( enable ) )
410 return FALSE;
29006414 411
907789a0
RR
412 wxNode *node = m_boxes.First();
413 while (node)
414 {
415 GtkButton *button = GTK_BUTTON( node->Data() );
416 GtkWidget *label = button->child;
417 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
418 gtk_widget_set_sensitive( label, enable );
419 node = node->Next();
420 }
f03fc89f
VZ
421
422 return TRUE;
6de97a3b 423}
c801d85f 424
d3904ceb 425void wxRadioBox::Enable( int item, bool enable )
c801d85f 426{
b019151f 427 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 428
907789a0 429 wxNode *node = m_boxes.Nth( item );
29006414 430
b019151f 431 wxCHECK_RET( node, _T("radiobox wrong index") );
29006414 432
907789a0
RR
433 GtkButton *button = GTK_BUTTON( node->Data() );
434 GtkWidget *label = button->child;
435 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
436 gtk_widget_set_sensitive( label, enable );
6de97a3b 437}
c801d85f 438
d3904ceb 439void wxRadioBox::Show( int item, bool show )
c801d85f 440{
b019151f 441 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 442
907789a0 443 wxNode *node = m_boxes.Nth( item );
29006414 444
b019151f 445 wxCHECK_RET( node, _T("radiobox wrong index") );
29006414 446
907789a0 447 GtkWidget *button = GTK_WIDGET( node->Data() );
c801d85f 448
907789a0
RR
449 if (show)
450 gtk_widget_show( button );
451 else
452 gtk_widget_hide( button );
6de97a3b 453}
c801d85f
KB
454
455wxString wxRadioBox::GetStringSelection(void) const
456{
b019151f 457 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
29006414 458
907789a0
RR
459 wxNode *node = m_boxes.First();
460 while (node)
c801d85f 461 {
907789a0
RR
462 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
463 if (button->active)
464 {
465 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
466 return label->label;
467 }
468 node = node->Next();
6de97a3b 469 }
29006414 470
b019151f
OK
471 wxFAIL_MSG( _T("wxRadioBox none selected") );
472 return _T("");
6de97a3b 473}
c801d85f 474
907789a0 475bool wxRadioBox::SetStringSelection( const wxString &s )
c801d85f 476{
b019151f 477 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobox") );
29006414 478
907789a0
RR
479 int res = FindString( s );
480 if (res == -1) return FALSE;
481 SetSelection( res );
29006414 482
907789a0 483 return TRUE;
6de97a3b 484}
c801d85f
KB
485
486int wxRadioBox::Number(void) const
487{
907789a0 488 return m_boxes.Number();
6de97a3b 489}
c801d85f
KB
490
491int wxRadioBox::GetNumberOfRowsOrCols(void) const
492{
907789a0 493 return 1;
6de97a3b 494}
c801d85f 495
debe6624 496void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
c801d85f 497{
b019151f 498 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
6de97a3b 499}
c801d85f 500
953704c1
RR
501void wxRadioBox::DisableEvents()
502{
503 wxNode *node = m_boxes.First();
504 while (node)
505 {
506 gtk_signal_disconnect_by_func( GTK_OBJECT(node->Data()),
507 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
508
509 node = node->Next();
510 }
511}
512
513void wxRadioBox::EnableEvents()
514{
515 wxNode *node = m_boxes.First();
516 while (node)
517 {
518 gtk_signal_connect( GTK_OBJECT(node->Data()), "clicked",
519 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
520
521 node = node->Next();
522 }
523}
524
58614078 525void wxRadioBox::ApplyWidgetStyle()
868a2826 526{
907789a0 527 SetWidgetStyle();
29006414 528
907789a0 529 gtk_widget_set_style( m_widget, m_widgetStyle );
29006414 530
907789a0
RR
531 wxNode *node = m_boxes.First();
532 while (node)
533 {
534 GtkWidget *widget = GTK_WIDGET( node->Data() );
535 gtk_widget_set_style( widget, m_widgetStyle );
29006414 536
907789a0
RR
537 GtkButton *button = GTK_BUTTON( node->Data() );
538 gtk_widget_set_style( button->child, m_widgetStyle );
29006414 539
907789a0
RR
540 node = node->Next();
541 }
868a2826 542}
b4071e91
RR
543
544bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
545{
907789a0 546 if (window == m_widget->window) return TRUE;
29006414 547
907789a0
RR
548 wxNode *node = m_boxes.First();
549 while (node)
550 {
551 GtkWidget *button = GTK_WIDGET( node->Data() );
29006414 552
907789a0 553 if (window == button->window) return TRUE;
29006414 554
907789a0
RR
555 node = node->Next();
556 }
29006414 557
907789a0 558 return FALSE;
b4071e91 559}
dcf924a3
RR
560
561#endif