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