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