]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/radiobox.cpp
Added wxDirDialog
[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
10
11#ifdef __GNUG__
12#pragma implementation "radiobox.h"
13#endif
14
15#include "wx/radiobox.h"
16#include "wx/dialog.h"
17#include "wx/frame.h"
18#include "wx/gtk/win_gtk.h"
19
66bd6b93
RR
20//-----------------------------------------------------------------------------
21// data
22//-----------------------------------------------------------------------------
23
24extern bool g_blockEventsOnDrag;
25
c801d85f 26//-----------------------------------------------------------------------------
b4071e91 27// "clicked"
c801d85f
KB
28//-----------------------------------------------------------------------------
29
66bd6b93 30static void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioBox *rb )
c801d85f 31{
907789a0
RR
32 if (!rb->HasVMT()) return;
33 if (g_blockEventsOnDrag) return;
66bd6b93 34
907789a0
RR
35 if (rb->m_alreadySent)
36 {
37 rb->m_alreadySent = FALSE;
38 return;
39 }
47908e25 40
907789a0 41 rb->m_alreadySent = TRUE;
47908e25 42
907789a0
RR
43 wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, rb->GetId() );
44 event.SetInt( rb->GetSelection() );
45 wxString tmp( rb->GetStringSelection() );
46 event.SetString( WXSTRINGCAST(tmp) );
47 event.SetEventObject( rb );
48 rb->GetEventHandler()->ProcessEvent(event);
6de97a3b 49}
c801d85f 50
b4071e91
RR
51//-----------------------------------------------------------------------------
52// wxRadioBox
c801d85f
KB
53//-----------------------------------------------------------------------------
54
55IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl)
56
3f659fd6 57BEGIN_EVENT_TABLE(wxRadioBox, wxControl)
907789a0 58 EVT_SIZE(wxRadioBox::OnSize)
3f659fd6
RR
59END_EVENT_TABLE()
60
c801d85f
KB
61wxRadioBox::wxRadioBox(void)
62{
6de97a3b 63}
c801d85f 64
debe6624 65bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title,
907789a0
RR
66 const wxPoint &pos, const wxSize &size,
67 int n, const wxString choices[],
68 int WXUNUSED(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;
c801d85f 75
907789a0 76 PreCreation( parent, id, pos, size, style, name );
c801d85f 77
907789a0 78 SetValidator( validator );
6de97a3b 79
907789a0 80 m_widget = gtk_frame_new( title );
c801d85f 81
907789a0
RR
82 int x = m_x+5;
83 int y = m_y+15;
84 int maxLen = 0;
85 int height = 20;
86 int width = 0;
c801d85f 87
907789a0 88 GtkRadioButton *m_radio = (GtkRadioButton*) NULL;
d6d1892b 89
92976ab6 90 if (m_windowStyle & wxRA_HORIZONTAL)
c801d85f 91 {
907789a0
RR
92 GSList *radio_button_group = (GSList *) NULL;
93 for (int i = 0; i < n; i++)
94 {
95 if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
47908e25 96
907789a0 97 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i] ) );
c801d85f 98
907789a0 99 m_boxes.Append( (wxObject*) m_radio );
d6d1892b 100
907789a0 101 ConnectWidget( GTK_WIDGET(m_radio) );
b4071e91 102
907789a0 103 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
c801d85f 104
907789a0
RR
105 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
106 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
c801d85f 107
907789a0 108 gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), GTK_WIDGET(m_radio), x, y );
c801d85f 109
907789a0
RR
110 int tmp = 25+gdk_string_measure( GTK_WIDGET(m_radio)->style->font, choices[i] );
111 if (tmp > maxLen) maxLen = tmp;
c801d85f 112
907789a0
RR
113 width = m_width-10;
114 if (size.x == -1) width = tmp;
115 gtk_widget_set_usize( GTK_WIDGET(m_radio), width, 20 );
c801d85f 116
907789a0
RR
117 y += 20;
118 height += 20;
119 }
120 width = maxLen + 10;
6de97a3b 121 }
907789a0 122 else
e5403d7c 123 {
907789a0
RR
124 int max = 0;
125 for (int i = 0; i < n; i++)
126 {
127 GdkFont *font = m_widget->style->font;
128 int len = 27+gdk_string_measure( font, choices[i] );
129 if (len > max) max = len;
130 }
131
132 GSList *radio_button_group = (GSList *) NULL;
133 for (int i = 0; i < n; i++)
134 {
135 if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
e5403d7c 136
907789a0 137 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i] ) );
e5403d7c 138
907789a0 139 m_boxes.Append( (wxObject*) m_radio );
e5403d7c 140
907789a0 141 ConnectWidget( GTK_WIDGET(m_radio) );
e5403d7c 142
907789a0 143 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
e5403d7c 144
907789a0
RR
145 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
146 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
e5403d7c 147
907789a0 148 gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), GTK_WIDGET(m_radio), x, y );
e5403d7c 149
907789a0 150 gtk_widget_set_usize( GTK_WIDGET(m_radio), max, 20 );
e5403d7c 151
907789a0
RR
152 x += max;
153 }
e5403d7c 154
907789a0
RR
155 width = max*n + 10;
156 height = 40;
157 }
d6d1892b 158
907789a0
RR
159 wxSize newSize = size;
160 if (newSize.x == -1) newSize.x = width;
161 if (newSize.y == -1) newSize.y = height;
162 SetSize( newSize.x, newSize.y );
c801d85f 163
907789a0 164 m_parent->AddChild( this );
e6b5bded 165
907789a0 166 (m_parent->m_insertCallback)( m_parent, this );
6ca41e57 167
907789a0 168 PostCreation();
c801d85f 169
907789a0 170 SetLabel( title );
d6d1892b 171
907789a0
RR
172 SetBackgroundColour( parent->GetBackgroundColour() );
173 SetForegroundColour( parent->GetForegroundColour() );
f96aa4d9 174
907789a0 175 Show( TRUE );
c801d85f 176
907789a0 177 return TRUE;
6de97a3b 178}
c801d85f 179
d6d1892b
RR
180wxRadioBox::~wxRadioBox(void)
181{
907789a0
RR
182 wxNode *node = m_boxes.First();
183 while (node)
184 {
185 GtkWidget *button = GTK_WIDGET( node->Data() );
186 gtk_widget_destroy( button );
187 node = node->Next();
188 }
d6d1892b
RR
189}
190
b4071e91 191void wxRadioBox::OnSize( wxSizeEvent &event )
3f659fd6 192{
b4071e91
RR
193 wxControl::OnSize( event );
194
3f659fd6
RR
195 int x = m_x+5;
196 int y = m_y+15;
197
92976ab6 198 if (m_windowStyle & wxRA_HORIZONTAL)
3f659fd6 199 {
e5403d7c
RR
200 wxNode *node = m_boxes.First();
201 while (node)
202 {
203 GtkWidget *button = GTK_WIDGET( node->Data() );
3f659fd6 204
e5403d7c
RR
205 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y );
206 y += 20;
3f659fd6 207
e5403d7c
RR
208 int w = m_width-10;
209 if (w < 15) w = 15;
210 gtk_widget_set_usize( button, w, 20 );
e55ad60e 211
e5403d7c
RR
212 node = node->Next();
213 }
214 }
215 else
216 {
217 int max = 0;
218
219 wxNode *node = m_boxes.First();
220 while (node)
221 {
222 GtkButton *button = GTK_BUTTON( node->Data() );
223 GtkLabel *label = GTK_LABEL( button->child );
224
225 GdkFont *font = m_widget->style->font;
226 int len = 27+gdk_string_measure( font, label->label );
227 if (len > max) max = len;
228
229 node = node->Next();
230 }
231
232 node = m_boxes.First();
233 while (node)
234 {
235 GtkWidget *button = GTK_WIDGET( node->Data() );
236
237 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y );
238 x += max;
239 gtk_widget_set_usize( button, max, 20 );
240
241 node = node->Next();
242 }
3f659fd6
RR
243 }
244}
245
debe6624 246bool wxRadioBox::Show( bool show )
c801d85f 247{
907789a0 248 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid radiobox" );
f96aa4d9 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() );
d6d1892b 256
907789a0 257 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
d6d1892b 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{
907789a0 267 wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" );
f96aa4d9 268
907789a0 269 int count = 0;
47908e25 270
907789a0
RR
271 wxNode *node = m_boxes.First();
272 while (node)
273 {
274 GtkButton *button = GTK_BUTTON( node->Data() );
d6d1892b 275
907789a0
RR
276 GtkLabel *label = GTK_LABEL( button->child );
277 if (s == label->label) return count;
278 count++;
d6d1892b 279
907789a0
RR
280 node = node->Next();
281 }
d6d1892b 282
907789a0 283 return -1;
6de97a3b 284}
c801d85f 285
b292e2f5
RR
286void wxRadioBox::SetFocus()
287{
288 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
289
290 if (m_boxes.GetCount() == 0) return;
291
292 wxNode *node = m_boxes.First();
293 while (node)
294 {
295 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
296 if (button->active)
297 {
298 gtk_widget_grab_focus( GTK_WIDGET(button) );
299
300 return;
301 }
302 node = node->Next();
303 }
304
305}
306
47908e25 307void wxRadioBox::SetSelection( int n )
c801d85f 308{
907789a0 309 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
f96aa4d9 310
907789a0 311 wxNode *node = m_boxes.Nth( n );
d3904ceb 312
907789a0 313 wxCHECK_RET( node, "radiobox wrong index" );
47908e25 314
907789a0 315 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
47908e25 316
907789a0 317 gtk_toggle_button_set_state( button, 1 );
6de97a3b 318}
c801d85f
KB
319
320int wxRadioBox::GetSelection(void) const
321{
907789a0 322 wxCHECK_MSG( m_widget != NULL, -1, "invalid radiobox" );
f96aa4d9 323
907789a0 324 int count = 0;
d6d1892b 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 }
2b5bd7fd 334
907789a0 335 wxFAIL_MSG( "wxRadioBox none selected" );
d6d1892b 336
907789a0 337 return -1;
6de97a3b 338}
c801d85f 339
47908e25 340wxString wxRadioBox::GetString( int n ) const
c801d85f 341{
907789a0 342 wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" );
f96aa4d9 343
907789a0 344 wxNode *node = m_boxes.Nth( n );
47908e25 345
907789a0 346 wxCHECK_MSG( node, "", "radiobox wrong index" );
47908e25 347
907789a0
RR
348 GtkButton *button = GTK_BUTTON( node->Data() );
349 GtkLabel *label = GTK_LABEL( button->child );
47908e25 350
907789a0 351 return wxString( label->label );
6de97a3b 352}
c801d85f 353
d3904ceb 354wxString wxRadioBox::GetLabel( int item ) const
c801d85f 355{
907789a0 356 wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" );
f96aa4d9 357
907789a0 358 return GetString( item );
6de97a3b 359}
c801d85f 360
d3904ceb 361void wxRadioBox::SetLabel( const wxString& label )
c801d85f 362{
907789a0 363 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
f96aa4d9 364
907789a0 365 wxControl::SetLabel( label );
f96aa4d9 366
907789a0 367 gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel() );
6de97a3b 368}
c801d85f 369
d3904ceb 370void wxRadioBox::SetLabel( int item, const wxString& label )
c801d85f 371{
907789a0 372 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
f96aa4d9 373
907789a0 374 wxNode *node = m_boxes.Nth( item );
d3904ceb 375
907789a0 376 wxCHECK_RET( node, "radiobox wrong index" );
d3904ceb 377
907789a0
RR
378 GtkButton *button = GTK_BUTTON( node->Data() );
379 GtkLabel *g_label = GTK_LABEL( button->child );
d3904ceb 380
907789a0 381 gtk_label_set( g_label, label );
6de97a3b 382}
c801d85f 383
debe6624 384void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
c801d85f 385{
907789a0 386 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
6de97a3b 387}
c801d85f 388
d3904ceb 389void wxRadioBox::Enable( bool enable )
c801d85f 390{
907789a0 391 wxControl::Enable( enable );
d3904ceb 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{
907789a0 406 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
d3904ceb 407
907789a0 408 wxNode *node = m_boxes.Nth( item );
d3904ceb 409
907789a0
RR
410 wxCHECK_RET( node, "radiobox wrong index" );
411
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{
907789a0 420 wxCHECK_RET( m_widget != NULL, "invalid radiobox" );
d3904ceb 421
907789a0
RR
422 wxNode *node = m_boxes.Nth( item );
423
424 wxCHECK_RET( node, "radiobox wrong index" );
d3904ceb 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{
907789a0
RR
436 wxCHECK_MSG( m_widget != NULL, "", "invalid radiobox" );
437
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 }
d6d1892b 449
907789a0
RR
450 wxFAIL_MSG( "wxRadioBox none selected" );
451 return "";
6de97a3b 452}
c801d85f 453
907789a0 454bool wxRadioBox::SetStringSelection( const wxString &s )
c801d85f 455{
907789a0
RR
456 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid radiobox" );
457
458 int res = FindString( s );
459 if (res == -1) return FALSE;
460 SetSelection( res );
461
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{
907789a0 477 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
6de97a3b 478}
c801d85f 479
58614078 480void wxRadioBox::ApplyWidgetStyle()
868a2826 481{
907789a0 482 SetWidgetStyle();
f96aa4d9 483
907789a0 484 gtk_widget_set_style( m_widget, m_widgetStyle );
a81258be 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 );
868a2826 491
907789a0
RR
492 GtkButton *button = GTK_BUTTON( node->Data() );
493 gtk_widget_set_style( button->child, m_widgetStyle );
868a2826 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;
b4071e91 502
907789a0
RR
503 wxNode *node = m_boxes.First();
504 while (node)
505 {
506 GtkWidget *button = GTK_WIDGET( node->Data() );
d6d1892b 507
907789a0 508 if (window == button->window) return TRUE;
d6d1892b 509
907789a0
RR
510 node = node->Next();
511 }
b4071e91 512
907789a0 513 return FALSE;
b4071e91 514}