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