]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/radiobox.cpp
Added wxFontEnumerator class for wxMSW, and fixed text validator for French
[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
4dcaf11a
RR
88 if (!PreCreation( parent, pos, size ) ||
89 !CreateBase( parent, id, pos, size, style, validator, name ))
90 {
91 wxFAIL_MSG( _T("wxRadioBox creation failed") );
92 return FALSE;
93 }
6de97a3b 94
b019151f 95 m_widget = gtk_frame_new( title.mbc_str() );
29006414 96
d3b4d113 97 m_majorDim = majorDim;
29006414 98
907789a0 99 GtkRadioButton *m_radio = (GtkRadioButton*) NULL;
29006414 100
26333898 101 wxString label;
d3b4d113
RR
102 GSList *radio_button_group = (GSList *) NULL;
103 for (int i = 0; i < n; i++)
c801d85f 104 {
26333898
VZ
105 if ( i != 0 )
106 radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
29006414 107
26333898
VZ
108 label.Empty();
109 for ( const wxChar *pc = choices[i]; *pc; pc++ )
110 {
111 if ( *pc != _T('&') )
112 label += *pc;
113 }
114
115 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, label.mbc_str() ) );
29006414 116
d3b4d113 117 m_boxes.Append( (wxObject*) m_radio );
29006414 118
d3b4d113 119 ConnectWidget( GTK_WIDGET(m_radio) );
29006414 120
d3b4d113 121 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
29006414
VZ
122
123 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
d3b4d113 124 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
29006414 125
e3e717ec
VZ
126 gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow),
127 GTK_WIDGET(m_radio),
f03fc89f 128 m_x+10, m_y+10+(i*24), 10, 10 );
6de97a3b 129 }
29006414 130
d3b4d113 131 wxSize ls = LayoutItems();
29006414 132
907789a0 133 wxSize newSize = size;
d3b4d113
RR
134 if (newSize.x == -1) newSize.x = ls.x;
135 if (newSize.y == -1) newSize.y = ls.y;
907789a0 136 SetSize( newSize.x, newSize.y );
29006414 137
f03fc89f 138 m_parent->DoAddChild( this );
29006414 139
907789a0 140 PostCreation();
29006414 141
907789a0 142 SetLabel( title );
29006414 143
907789a0
RR
144 SetBackgroundColour( parent->GetBackgroundColour() );
145 SetForegroundColour( parent->GetForegroundColour() );
a7ac4461 146 SetFont( parent->GetFont() );
f96aa4d9 147
907789a0 148 Show( TRUE );
29006414 149
907789a0 150 return TRUE;
6de97a3b 151}
c801d85f 152
f03fc89f 153wxRadioBox::~wxRadioBox()
d6d1892b 154{
907789a0
RR
155 wxNode *node = m_boxes.First();
156 while (node)
157 {
158 GtkWidget *button = GTK_WIDGET( node->Data() );
159 gtk_widget_destroy( button );
160 node = node->Next();
161 }
d6d1892b
RR
162}
163
b4071e91 164void wxRadioBox::OnSize( wxSizeEvent &event )
3f659fd6 165{
d3b4d113 166 LayoutItems();
f03fc89f
VZ
167
168 event.Skip();
d3b4d113
RR
169}
170
171wxSize wxRadioBox::LayoutItems()
172{
bbe0af5b
RR
173 int x = 7;
174 int y = 15;
29006414 175
e3e717ec
VZ
176 if ( m_majorDim == 0 )
177 {
178 // avoid dividing by 0 below
ee6db21e 179 wxFAIL_MSG( _T("dimension of radiobox should not be 0!") );
e3e717ec
VZ
180
181 m_majorDim = 1;
182 }
183
d3b4d113 184 int num_per_major = (m_boxes.GetCount() - 1) / m_majorDim +1;
29006414 185
d3b4d113 186 wxSize res( 0, 0 );
29006414 187
e9158f7d
RR
188 int num_of_cols = 0;
189 int num_of_rows = 0;
190 if (HasFlag(wxRA_SPECIFY_COLS))
d3b4d113 191 {
e9158f7d
RR
192 num_of_cols = m_majorDim;
193 num_of_rows = num_per_major;
194 }
195 else
196 {
197 num_of_cols = num_per_major;
198 num_of_rows = m_majorDim;
199 }
200
201 if ( HasFlag(wxRA_SPECIFY_COLS) ||
202 (HasFlag(wxRA_SPECIFY_ROWS) && (num_of_cols > 1)) )
203 {
204 for (int j = 0; j < num_of_cols; j++)
29006414 205 {
bbe0af5b 206 y = 15;
29006414 207
d3b4d113 208 int max_len = 0;
e9158f7d
RR
209 wxNode *node = m_boxes.Nth( j*num_of_rows );
210 for (int i1 = 0; i1< num_of_rows; i1++)
29006414 211 {
d3b4d113
RR
212 GtkWidget *button = GTK_WIDGET( node->Data() );
213 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
214 GdkFont *font = m_widget->style->font;
bbe0af5b 215 int len = 22+gdk_string_measure( font, label->label );
d3b4d113 216 if (len > max_len) max_len = len;
29006414 217
a2053b27 218 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y );
dcf924a3 219 y += 22;
29006414
VZ
220
221 node = node->Next();
222 if (!node) break;
223 }
224
225 // we don't know the max_len before
226
e9158f7d
RR
227 node = m_boxes.Nth( j*num_of_rows );
228 for (int i2 = 0; i2< num_of_rows; i2++)
29006414 229 {
d3b4d113 230 GtkWidget *button = GTK_WIDGET( node->Data() );
29006414 231
a2053b27 232 gtk_myfixed_resize( GTK_MYFIXED(m_parent->m_wxwindow), button, max_len, 20 );
29006414
VZ
233
234 node = node->Next();
235 if (!node) break;
236 }
237
238 if (y > res.y) res.y = y;
239
240 x += max_len + 2;
d3b4d113 241 }
29006414
VZ
242
243 res.x = x+4;
244 res.y += 9;
e5403d7c 245 }
d3b4d113 246 else
e5403d7c 247 {
d3b4d113
RR
248 int max = 0;
249
250 wxNode *node = m_boxes.First();
251 while (node)
252 {
253 GtkButton *button = GTK_BUTTON( node->Data() );
254 GtkLabel *label = GTK_LABEL( button->child );
29006414 255
d3b4d113 256 GdkFont *font = m_widget->style->font;
bbe0af5b 257 int len = 22+gdk_string_measure( font, label->label );
d3b4d113 258 if (len > max) max = len;
29006414 259
d3b4d113
RR
260 node = node->Next();
261 }
29006414 262
d3b4d113
RR
263 node = m_boxes.First();
264 while (node)
265 {
266 GtkWidget *button = GTK_WIDGET( node->Data() );
29006414 267
a2053b27 268 gtk_myfixed_set_size( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y, max, 20 );
d3b4d113 269 x += max;
29006414 270
d3b4d113
RR
271 node = node->Next();
272 }
29006414 273 res.x = x+4;
3417c2cd 274 res.y = 40;
e5403d7c 275 }
29006414 276
d3b4d113 277 return res;
3f659fd6
RR
278}
279
debe6624 280bool wxRadioBox::Show( bool show )
c801d85f 281{
b019151f 282 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobox") );
29006414 283
907789a0 284 wxWindow::Show( show );
c801d85f 285
ba2a0103 286 if ((m_windowStyle & wxNO_BORDER) != 0)
b0351fc9 287 gtk_widget_hide( m_widget );
e3e717ec 288
907789a0
RR
289 wxNode *node = m_boxes.First();
290 while (node)
291 {
292 GtkWidget *button = GTK_WIDGET( node->Data() );
29006414 293
907789a0 294 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
29006414 295
907789a0
RR
296 node = node->Next();
297 }
c801d85f 298
907789a0 299 return TRUE;
6de97a3b 300}
c801d85f 301
47908e25 302int wxRadioBox::FindString( const wxString &s ) const
c801d85f 303{
b019151f 304 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid radiobox") );
29006414 305
907789a0 306 int count = 0;
29006414 307
907789a0
RR
308 wxNode *node = m_boxes.First();
309 while (node)
310 {
311 GtkButton *button = GTK_BUTTON( node->Data() );
29006414 312
907789a0
RR
313 GtkLabel *label = GTK_LABEL( button->child );
314 if (s == label->label) return count;
315 count++;
29006414 316
907789a0
RR
317 node = node->Next();
318 }
29006414 319
907789a0 320 return -1;
6de97a3b 321}
c801d85f 322
b292e2f5
RR
323void wxRadioBox::SetFocus()
324{
b019151f 325 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 326
b292e2f5 327 if (m_boxes.GetCount() == 0) return;
29006414 328
b292e2f5
RR
329 wxNode *node = m_boxes.First();
330 while (node)
331 {
332 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
333 if (button->active)
29006414 334 {
b292e2f5 335 gtk_widget_grab_focus( GTK_WIDGET(button) );
29006414
VZ
336
337 return;
338 }
b292e2f5
RR
339 node = node->Next();
340 }
29006414 341
b292e2f5
RR
342}
343
47908e25 344void wxRadioBox::SetSelection( int n )
c801d85f 345{
b019151f 346 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 347
907789a0 348 wxNode *node = m_boxes.Nth( n );
29006414 349
b019151f 350 wxCHECK_RET( node, _T("radiobox wrong index") );
29006414 351
907789a0 352 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
29006414 353
953704c1
RR
354 DisableEvents();
355
907789a0 356 gtk_toggle_button_set_state( button, 1 );
953704c1
RR
357
358 EnableEvents();
6de97a3b 359}
c801d85f
KB
360
361int wxRadioBox::GetSelection(void) const
362{
b019151f 363 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid radiobox") );
29006414 364
907789a0 365 int count = 0;
29006414 366
907789a0
RR
367 wxNode *node = m_boxes.First();
368 while (node)
369 {
370 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
371 if (button->active) return count;
372 count++;
373 node = node->Next();
374 }
29006414 375
b019151f 376 wxFAIL_MSG( _T("wxRadioBox none selected") );
29006414 377
907789a0 378 return -1;
6de97a3b 379}
c801d85f 380
47908e25 381wxString wxRadioBox::GetString( int n ) const
c801d85f 382{
b019151f 383 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
29006414 384
907789a0 385 wxNode *node = m_boxes.Nth( n );
29006414 386
b019151f 387 wxCHECK_MSG( node, _T(""), _T("radiobox wrong index") );
29006414 388
907789a0
RR
389 GtkButton *button = GTK_BUTTON( node->Data() );
390 GtkLabel *label = GTK_LABEL( button->child );
29006414 391
907789a0 392 return wxString( label->label );
6de97a3b 393}
c801d85f 394
d3904ceb 395wxString wxRadioBox::GetLabel( int item ) const
c801d85f 396{
b019151f 397 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
29006414 398
907789a0 399 return GetString( item );
6de97a3b 400}
c801d85f 401
d3904ceb 402void wxRadioBox::SetLabel( const wxString& label )
c801d85f 403{
b019151f 404 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 405
907789a0 406 wxControl::SetLabel( label );
29006414 407
b019151f 408 gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel().mbc_str() );
6de97a3b 409}
c801d85f 410
d3904ceb 411void wxRadioBox::SetLabel( int item, const wxString& label )
c801d85f 412{
b019151f 413 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 414
907789a0 415 wxNode *node = m_boxes.Nth( item );
29006414 416
b019151f 417 wxCHECK_RET( node, _T("radiobox wrong index") );
29006414 418
907789a0
RR
419 GtkButton *button = GTK_BUTTON( node->Data() );
420 GtkLabel *g_label = GTK_LABEL( button->child );
29006414 421
b019151f 422 gtk_label_set( g_label, label.mbc_str() );
6de97a3b 423}
c801d85f 424
debe6624 425void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
c801d85f 426{
b019151f 427 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
6de97a3b 428}
c801d85f 429
f03fc89f 430bool wxRadioBox::Enable( bool enable )
c801d85f 431{
f03fc89f
VZ
432 if ( !wxControl::Enable( enable ) )
433 return FALSE;
29006414 434
907789a0
RR
435 wxNode *node = m_boxes.First();
436 while (node)
437 {
438 GtkButton *button = GTK_BUTTON( node->Data() );
439 GtkWidget *label = button->child;
440 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
441 gtk_widget_set_sensitive( label, enable );
442 node = node->Next();
443 }
f03fc89f
VZ
444
445 return TRUE;
6de97a3b 446}
c801d85f 447
d3904ceb 448void wxRadioBox::Enable( int item, bool enable )
c801d85f 449{
b019151f 450 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 451
907789a0 452 wxNode *node = m_boxes.Nth( item );
29006414 453
b019151f 454 wxCHECK_RET( node, _T("radiobox wrong index") );
29006414 455
907789a0
RR
456 GtkButton *button = GTK_BUTTON( node->Data() );
457 GtkWidget *label = button->child;
458 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
459 gtk_widget_set_sensitive( label, enable );
6de97a3b 460}
c801d85f 461
d3904ceb 462void wxRadioBox::Show( int item, bool show )
c801d85f 463{
b019151f 464 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 465
907789a0 466 wxNode *node = m_boxes.Nth( item );
29006414 467
b019151f 468 wxCHECK_RET( node, _T("radiobox wrong index") );
29006414 469
907789a0 470 GtkWidget *button = GTK_WIDGET( node->Data() );
c801d85f 471
907789a0
RR
472 if (show)
473 gtk_widget_show( button );
474 else
475 gtk_widget_hide( button );
6de97a3b 476}
c801d85f 477
9c884972 478wxString wxRadioBox::GetStringSelection() const
c801d85f 479{
b019151f 480 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
29006414 481
907789a0
RR
482 wxNode *node = m_boxes.First();
483 while (node)
c801d85f 484 {
907789a0
RR
485 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
486 if (button->active)
487 {
488 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
489 return label->label;
490 }
491 node = node->Next();
6de97a3b 492 }
29006414 493
b019151f
OK
494 wxFAIL_MSG( _T("wxRadioBox none selected") );
495 return _T("");
6de97a3b 496}
c801d85f 497
907789a0 498bool wxRadioBox::SetStringSelection( const wxString &s )
c801d85f 499{
b019151f 500 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobox") );
29006414 501
907789a0
RR
502 int res = FindString( s );
503 if (res == -1) return FALSE;
504 SetSelection( res );
29006414 505
907789a0 506 return TRUE;
6de97a3b 507}
c801d85f 508
9c884972 509int wxRadioBox::Number() const
c801d85f 510{
907789a0 511 return m_boxes.Number();
6de97a3b 512}
c801d85f 513
9c884972 514int wxRadioBox::GetNumberOfRowsOrCols() const
c801d85f 515{
907789a0 516 return 1;
6de97a3b 517}
c801d85f 518
debe6624 519void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
c801d85f 520{
b019151f 521 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
6de97a3b 522}
c801d85f 523
953704c1
RR
524void wxRadioBox::DisableEvents()
525{
526 wxNode *node = m_boxes.First();
527 while (node)
528 {
529 gtk_signal_disconnect_by_func( GTK_OBJECT(node->Data()),
530 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
531
532 node = node->Next();
533 }
534}
535
536void wxRadioBox::EnableEvents()
537{
538 wxNode *node = m_boxes.First();
539 while (node)
540 {
541 gtk_signal_connect( GTK_OBJECT(node->Data()), "clicked",
542 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
543
544 node = node->Next();
545 }
546}
547
58614078 548void wxRadioBox::ApplyWidgetStyle()
868a2826 549{
907789a0 550 SetWidgetStyle();
29006414 551
907789a0 552 gtk_widget_set_style( m_widget, m_widgetStyle );
29006414 553
907789a0
RR
554 wxNode *node = m_boxes.First();
555 while (node)
556 {
557 GtkWidget *widget = GTK_WIDGET( node->Data() );
558 gtk_widget_set_style( widget, m_widgetStyle );
29006414 559
907789a0
RR
560 GtkButton *button = GTK_BUTTON( node->Data() );
561 gtk_widget_set_style( button->child, m_widgetStyle );
29006414 562
907789a0
RR
563 node = node->Next();
564 }
868a2826 565}
b4071e91
RR
566
567bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
568{
907789a0 569 if (window == m_widget->window) return TRUE;
29006414 570
907789a0
RR
571 wxNode *node = m_boxes.First();
572 while (node)
573 {
574 GtkWidget *button = GTK_WIDGET( node->Data() );
29006414 575
907789a0 576 if (window == button->window) return TRUE;
29006414 577
907789a0
RR
578 node = node->Next();
579 }
29006414 580
907789a0 581 return FALSE;
b4071e91 582}
dcf924a3
RR
583
584#endif