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