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