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