]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/radiobox.cpp
tree ctrl sorting shouldn't crash when items don't have data
[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"
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
fdd3ed7a
RR
111 gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow),
112 GTK_WIDGET(m_radio),
113 m_x+10, m_y+10+(i*24), 10, 10 );
6de97a3b 114 }
29006414 115
d3b4d113 116 wxSize ls = LayoutItems();
29006414 117
907789a0 118 wxSize newSize = size;
d3b4d113
RR
119 if (newSize.x == -1) newSize.x = ls.x;
120 if (newSize.y == -1) newSize.y = ls.y;
907789a0 121 SetSize( newSize.x, newSize.y );
29006414 122
907789a0 123 m_parent->AddChild( this );
e6b5bded 124
907789a0 125 (m_parent->m_insertCallback)( m_parent, this );
29006414 126
907789a0 127 PostCreation();
29006414 128
907789a0 129 SetLabel( title );
29006414 130
907789a0
RR
131 SetBackgroundColour( parent->GetBackgroundColour() );
132 SetForegroundColour( parent->GetForegroundColour() );
a7ac4461 133 SetFont( parent->GetFont() );
f96aa4d9 134
907789a0 135 Show( TRUE );
29006414 136
907789a0 137 return TRUE;
6de97a3b 138}
c801d85f 139
d6d1892b
RR
140wxRadioBox::~wxRadioBox(void)
141{
907789a0
RR
142 wxNode *node = m_boxes.First();
143 while (node)
144 {
145 GtkWidget *button = GTK_WIDGET( node->Data() );
146 gtk_widget_destroy( button );
147 node = node->Next();
148 }
d6d1892b
RR
149}
150
b4071e91 151void wxRadioBox::OnSize( wxSizeEvent &event )
3f659fd6 152{
d3b4d113 153 wxControl::OnSize( event );
29006414 154
d3b4d113
RR
155 LayoutItems();
156}
157
158wxSize wxRadioBox::LayoutItems()
159{
bbe0af5b
RR
160 int x = 7;
161 int y = 15;
29006414 162
d3b4d113 163 int num_per_major = (m_boxes.GetCount() - 1) / m_majorDim +1;
29006414 164
d3b4d113 165 wxSize res( 0, 0 );
29006414 166
d3b4d113
RR
167 if (m_windowStyle & wxRA_HORIZONTAL)
168 {
29006414 169
d3b4d113 170 for (int j = 0; j < m_majorDim; j++)
29006414 171 {
bbe0af5b 172 y = 15;
29006414 173
d3b4d113
RR
174 int max_len = 0;
175 wxNode *node = m_boxes.Nth( j*num_per_major );
29006414
VZ
176 for (int i1 = 0; i1< num_per_major; i1++)
177 {
d3b4d113
RR
178 GtkWidget *button = GTK_WIDGET( node->Data() );
179 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
180 GdkFont *font = m_widget->style->font;
bbe0af5b 181 int len = 22+gdk_string_measure( font, label->label );
d3b4d113 182 if (len > max_len) max_len = len;
29006414 183
bbe0af5b 184 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y );
c46554be 185 y += 20;
29006414
VZ
186
187 node = node->Next();
188 if (!node) break;
189 }
190
191 // we don't know the max_len before
192
d3b4d113 193 node = m_boxes.Nth( j*num_per_major );
29006414
VZ
194 for (int i2 = 0; i2< num_per_major; i2++)
195 {
d3b4d113 196 GtkWidget *button = GTK_WIDGET( node->Data() );
29006414 197
fdd3ed7a 198 gtk_myfixed_resize( GTK_MYFIXED(m_parent->m_wxwindow), button, max_len, 20 );
29006414
VZ
199
200 node = node->Next();
201 if (!node) break;
202 }
203
204 if (y > res.y) res.y = y;
205
206 x += max_len + 2;
d3b4d113 207 }
29006414
VZ
208
209 res.x = x+4;
210 res.y += 9;
e5403d7c 211 }
d3b4d113 212 else
e5403d7c 213 {
d3b4d113
RR
214 int max = 0;
215
216 wxNode *node = m_boxes.First();
217 while (node)
218 {
219 GtkButton *button = GTK_BUTTON( node->Data() );
220 GtkLabel *label = GTK_LABEL( button->child );
29006414 221
d3b4d113 222 GdkFont *font = m_widget->style->font;
bbe0af5b 223 int len = 22+gdk_string_measure( font, label->label );
d3b4d113 224 if (len > max) max = len;
29006414 225
d3b4d113
RR
226 node = node->Next();
227 }
29006414 228
d3b4d113
RR
229 node = m_boxes.First();
230 while (node)
231 {
232 GtkWidget *button = GTK_WIDGET( node->Data() );
29006414 233
fdd3ed7a 234 gtk_myfixed_set_size( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y, max, 20 );
d3b4d113 235 x += max;
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
b0351fc9
RR
252 if ((m_windowStyle & wxNO_BORDER) == 0)
253 gtk_widget_hide( m_widget );
254
907789a0
RR
255 wxNode *node = m_boxes.First();
256 while (node)
257 {
258 GtkWidget *button = GTK_WIDGET( node->Data() );
29006414 259
907789a0 260 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
29006414 261
907789a0
RR
262 node = node->Next();
263 }
c801d85f 264
907789a0 265 return TRUE;
6de97a3b 266}
c801d85f 267
47908e25 268int wxRadioBox::FindString( const wxString &s ) const
c801d85f 269{
b019151f 270 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid radiobox") );
29006414 271
907789a0 272 int count = 0;
29006414 273
907789a0
RR
274 wxNode *node = m_boxes.First();
275 while (node)
276 {
277 GtkButton *button = GTK_BUTTON( node->Data() );
29006414 278
907789a0
RR
279 GtkLabel *label = GTK_LABEL( button->child );
280 if (s == label->label) return count;
281 count++;
29006414 282
907789a0
RR
283 node = node->Next();
284 }
29006414 285
907789a0 286 return -1;
6de97a3b 287}
c801d85f 288
b292e2f5
RR
289void wxRadioBox::SetFocus()
290{
b019151f 291 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 292
b292e2f5 293 if (m_boxes.GetCount() == 0) return;
29006414 294
b292e2f5
RR
295 wxNode *node = m_boxes.First();
296 while (node)
297 {
298 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
299 if (button->active)
29006414 300 {
b292e2f5 301 gtk_widget_grab_focus( GTK_WIDGET(button) );
29006414
VZ
302
303 return;
304 }
b292e2f5
RR
305 node = node->Next();
306 }
29006414 307
b292e2f5
RR
308}
309
47908e25 310void wxRadioBox::SetSelection( int n )
c801d85f 311{
b019151f 312 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 313
907789a0 314 wxNode *node = m_boxes.Nth( n );
29006414 315
b019151f 316 wxCHECK_RET( node, _T("radiobox wrong index") );
29006414 317
907789a0 318 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
29006414 319
907789a0 320 gtk_toggle_button_set_state( button, 1 );
6de97a3b 321}
c801d85f
KB
322
323int wxRadioBox::GetSelection(void) const
324{
b019151f 325 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid radiobox") );
29006414 326
907789a0 327 int count = 0;
29006414 328
907789a0
RR
329 wxNode *node = m_boxes.First();
330 while (node)
331 {
332 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
333 if (button->active) return count;
334 count++;
335 node = node->Next();
336 }
29006414 337
b019151f 338 wxFAIL_MSG( _T("wxRadioBox none selected") );
29006414 339
907789a0 340 return -1;
6de97a3b 341}
c801d85f 342
47908e25 343wxString wxRadioBox::GetString( int n ) const
c801d85f 344{
b019151f 345 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
29006414 346
907789a0 347 wxNode *node = m_boxes.Nth( n );
29006414 348
b019151f 349 wxCHECK_MSG( node, _T(""), _T("radiobox wrong index") );
29006414 350
907789a0
RR
351 GtkButton *button = GTK_BUTTON( node->Data() );
352 GtkLabel *label = GTK_LABEL( button->child );
29006414 353
907789a0 354 return wxString( label->label );
6de97a3b 355}
c801d85f 356
d3904ceb 357wxString wxRadioBox::GetLabel( int item ) const
c801d85f 358{
b019151f 359 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
29006414 360
907789a0 361 return GetString( item );
6de97a3b 362}
c801d85f 363
d3904ceb 364void wxRadioBox::SetLabel( const wxString& label )
c801d85f 365{
b019151f 366 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 367
907789a0 368 wxControl::SetLabel( label );
29006414 369
b019151f 370 gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel().mbc_str() );
6de97a3b 371}
c801d85f 372
d3904ceb 373void wxRadioBox::SetLabel( int item, const wxString& label )
c801d85f 374{
b019151f 375 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 376
907789a0 377 wxNode *node = m_boxes.Nth( item );
29006414 378
b019151f 379 wxCHECK_RET( node, _T("radiobox wrong index") );
29006414 380
907789a0
RR
381 GtkButton *button = GTK_BUTTON( node->Data() );
382 GtkLabel *g_label = GTK_LABEL( button->child );
29006414 383
b019151f 384 gtk_label_set( g_label, label.mbc_str() );
6de97a3b 385}
c801d85f 386
debe6624 387void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
c801d85f 388{
b019151f 389 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
6de97a3b 390}
c801d85f 391
d3904ceb 392void wxRadioBox::Enable( bool enable )
c801d85f 393{
907789a0 394 wxControl::Enable( enable );
29006414 395
907789a0
RR
396 wxNode *node = m_boxes.First();
397 while (node)
398 {
399 GtkButton *button = GTK_BUTTON( node->Data() );
400 GtkWidget *label = button->child;
401 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
402 gtk_widget_set_sensitive( label, enable );
403 node = node->Next();
404 }
6de97a3b 405}
c801d85f 406
d3904ceb 407void wxRadioBox::Enable( int item, bool enable )
c801d85f 408{
b019151f 409 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 410
907789a0 411 wxNode *node = m_boxes.Nth( item );
29006414 412
b019151f 413 wxCHECK_RET( node, _T("radiobox wrong index") );
29006414 414
907789a0
RR
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 );
6de97a3b 419}
c801d85f 420
d3904ceb 421void wxRadioBox::Show( int item, bool show )
c801d85f 422{
b019151f 423 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
29006414 424
907789a0 425 wxNode *node = m_boxes.Nth( item );
29006414 426
b019151f 427 wxCHECK_RET( node, _T("radiobox wrong index") );
29006414 428
907789a0 429 GtkWidget *button = GTK_WIDGET( node->Data() );
c801d85f 430
907789a0
RR
431 if (show)
432 gtk_widget_show( button );
433 else
434 gtk_widget_hide( button );
6de97a3b 435}
c801d85f
KB
436
437wxString wxRadioBox::GetStringSelection(void) const
438{
b019151f 439 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
29006414 440
907789a0
RR
441 wxNode *node = m_boxes.First();
442 while (node)
c801d85f 443 {
907789a0
RR
444 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
445 if (button->active)
446 {
447 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
448 return label->label;
449 }
450 node = node->Next();
6de97a3b 451 }
29006414 452
b019151f
OK
453 wxFAIL_MSG( _T("wxRadioBox none selected") );
454 return _T("");
6de97a3b 455}
c801d85f 456
907789a0 457bool wxRadioBox::SetStringSelection( const wxString &s )
c801d85f 458{
b019151f 459 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobox") );
29006414 460
907789a0
RR
461 int res = FindString( s );
462 if (res == -1) return FALSE;
463 SetSelection( res );
29006414 464
907789a0 465 return TRUE;
6de97a3b 466}
c801d85f
KB
467
468int wxRadioBox::Number(void) const
469{
907789a0 470 return m_boxes.Number();
6de97a3b 471}
c801d85f
KB
472
473int wxRadioBox::GetNumberOfRowsOrCols(void) const
474{
907789a0 475 return 1;
6de97a3b 476}
c801d85f 477
debe6624 478void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
c801d85f 479{
b019151f 480 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
6de97a3b 481}
c801d85f 482
58614078 483void wxRadioBox::ApplyWidgetStyle()
868a2826 484{
907789a0 485 SetWidgetStyle();
29006414 486
907789a0 487 gtk_widget_set_style( m_widget, m_widgetStyle );
29006414 488
907789a0
RR
489 wxNode *node = m_boxes.First();
490 while (node)
491 {
492 GtkWidget *widget = GTK_WIDGET( node->Data() );
493 gtk_widget_set_style( widget, m_widgetStyle );
29006414 494
907789a0
RR
495 GtkButton *button = GTK_BUTTON( node->Data() );
496 gtk_widget_set_style( button->child, m_widgetStyle );
29006414 497
907789a0
RR
498 node = node->Next();
499 }
868a2826 500}
b4071e91
RR
501
502bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
503{
907789a0 504 if (window == m_widget->window) return TRUE;
29006414 505
907789a0
RR
506 wxNode *node = m_boxes.First();
507 while (node)
508 {
509 GtkWidget *button = GTK_WIDGET( node->Data() );
29006414 510
907789a0 511 if (window == button->window) return TRUE;
29006414 512
907789a0
RR
513 node = node->Next();
514 }
29006414 515
907789a0 516 return FALSE;
b4071e91 517}