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