]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/radiobox.cpp
fix for menu separator
[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
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}