]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/radiobox.cpp
tab traversal now works better (using uninitialized variable was a bad idea :-)
[wxWidgets.git] / src / gtk1 / radiobox.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: radiobox.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
6// Id:
7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11
12#ifdef __GNUG__
13#pragma implementation "radiobox.h"
14#endif
15
16#include "wx/radiobox.h"
17#include "wx/dialog.h"
18#include "wx/frame.h"
19#include "wx/gtk/win_gtk.h"
20
66bd6b93
RR
21//-----------------------------------------------------------------------------
22// data
23//-----------------------------------------------------------------------------
24
25extern bool g_blockEventsOnDrag;
26
c801d85f
KB
27//-----------------------------------------------------------------------------
28// wxRadioBox
29//-----------------------------------------------------------------------------
30
66bd6b93 31static void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioBox *rb )
c801d85f 32{
66bd6b93
RR
33 if (!rb->HasVMT()) return;
34 if (g_blockEventsOnDrag) return;
35
47908e25
RR
36 if (rb->m_alreadySent)
37 {
38 rb->m_alreadySent = FALSE;
39 return;
40 }
41
42 rb->m_alreadySent = TRUE;
43
c801d85f
KB
44 wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, rb->GetId() );
45 event.SetInt( rb->GetSelection() );
46 wxString tmp( rb->GetStringSelection() );
47 event.SetString( WXSTRINGCAST(tmp) );
48 event.SetEventObject( rb );
47908e25 49 rb->GetEventHandler()->ProcessEvent(event);
6de97a3b 50}
c801d85f
KB
51
52//-----------------------------------------------------------------------------
53
54IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl)
55
3f659fd6
RR
56BEGIN_EVENT_TABLE(wxRadioBox, wxControl)
57 EVT_SIZE(wxRadioBox::OnSize)
58END_EVENT_TABLE()
59
c801d85f
KB
60wxRadioBox::wxRadioBox(void)
61{
6de97a3b 62}
c801d85f 63
debe6624
JS
64bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title,
65 const wxPoint &pos, const wxSize &size,
6de97a3b
RR
66 int n, const wxString choices[], int WXUNUSED(majorDim),
67 long style, const wxValidator& validator, const wxString &name )
c801d85f 68{
47908e25 69 m_alreadySent = FALSE;
c801d85f
KB
70 m_needParent = TRUE;
71
72 PreCreation( parent, id, pos, size, style, name );
73
6de97a3b
RR
74 SetValidator( validator );
75
c801d85f
KB
76 m_widget = gtk_frame_new( title );
77
78 int x = m_x+5;
79 int y = m_y+15;
80 int maxLen = 0;
81 int height = 20;
82
83// if (((m_style & wxRA_VERTICAL) == wxRA_VERTICAL) && (n > 0))
84 if (n > 0)
85 {
c67daf87 86 GSList *radio_button_group = (GSList *) NULL;
c801d85f
KB
87 for (int i = 0; i < n; i++)
88 {
89 if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
47908e25 90
c801d85f
KB
91 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i] ) );
92
93 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
94
95 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
96 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
97
98 gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), GTK_WIDGET(m_radio), x, y );
99
100 int tmp = 22+gdk_string_measure( GTK_WIDGET(m_radio)->style->font, choices[i] );
101 if (tmp > maxLen) maxLen = tmp;
102
103 int width = m_width-10;
104 if (size.x == -1) width = tmp;
105 gtk_widget_set_usize( GTK_WIDGET(m_radio), width, 20 );
106
107 y += 20;
108 height += 20;
109
6de97a3b
RR
110 }
111 }
c801d85f
KB
112
113 wxSize newSize = size;
114 if (newSize.x == -1) newSize.x = maxLen+10;
115 if (newSize.y == -1) newSize.y = height;
116 SetSize( newSize.x, newSize.y );
117
118 PostCreation();
119
120 Show( TRUE );
121
122 return TRUE;
6de97a3b 123}
c801d85f 124
3f659fd6
RR
125void wxRadioBox::OnSize( wxSizeEvent &WXUNUSED(event) )
126{
127 int x = m_x+5;
128 int y = m_y+15;
129
130 GSList *item = gtk_radio_button_group( m_radio );
131 while (item)
132 {
133 GtkWidget *button = GTK_WIDGET( item->data );
134
135 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y );
136
137 y += 20;
138
139 item = item->next;
140 }
141}
142
debe6624 143bool wxRadioBox::Show( bool show )
c801d85f
KB
144{
145 wxWindow::Show( show );
146
147 GSList *item = gtk_radio_button_group( m_radio );
148 while (item)
149 {
150 GtkWidget *w = GTK_WIDGET( item->data );
151 if (show) gtk_widget_show( w ); else gtk_widget_hide( w );
152 item = item->next;
6de97a3b 153 }
c801d85f
KB
154
155 return TRUE;
6de97a3b 156}
c801d85f 157
47908e25 158int wxRadioBox::FindString( const wxString &s ) const
c801d85f 159{
47908e25
RR
160 GSList *item = gtk_radio_button_group( m_radio );
161
162 int count = g_slist_length(item)-1;
163
164 while (item)
165 {
166 GtkButton *b = GTK_BUTTON( item->data );
167 GtkLabel *l = GTK_LABEL( b->child );
168 if (s == l->label) return count;
169 count--;
170 item = item->next;
6de97a3b 171 }
47908e25
RR
172
173 return -1;
6de97a3b 174}
c801d85f 175
47908e25 176void wxRadioBox::SetSelection( int n )
c801d85f 177{
47908e25
RR
178 GSList *item = gtk_radio_button_group( m_radio );
179 item = g_slist_nth( item, g_slist_length(item)-n-1 );
d3904ceb
RR
180
181 if (!item)
182 {
183 wxFAIL_MSG( "wxRadioBox wrong index" );
184 return;
185 }
47908e25
RR
186
187 GtkToggleButton *button = GTK_TOGGLE_BUTTON( item->data );
188
189 gtk_toggle_button_set_state( button, 1 );
6de97a3b 190}
c801d85f
KB
191
192int wxRadioBox::GetSelection(void) const
193{
194 GSList *item = gtk_radio_button_group( m_radio );
195 int count = 0;
2b5bd7fd 196 int found = -1;
c801d85f
KB
197 while (item)
198 {
199 GtkButton *button = GTK_BUTTON( item->data );
2b5bd7fd 200 if (GTK_TOGGLE_BUTTON(button)->active) found = count;
c801d85f
KB
201 count++;
202 item = item->next;
6de97a3b 203 }
2b5bd7fd
KB
204
205 return found != -1 ? count-found-1 : -1;
6de97a3b 206}
c801d85f 207
47908e25 208wxString wxRadioBox::GetString( int n ) const
c801d85f 209{
47908e25
RR
210 GSList *item = gtk_radio_button_group( m_radio );
211
212 item = g_slist_nth( item, g_slist_length(item)-n-1 );
47908e25 213
d3904ceb
RR
214 if (!item)
215 {
216 wxFAIL_MSG( "wxRadioBox wrong index" );
217 return "";
218 }
47908e25 219
d3904ceb
RR
220 GtkButton *button = GTK_BUTTON( item->data );
221 GtkLabel *label = GTK_LABEL( button->child );
47908e25
RR
222
223 return wxString( label->label );
6de97a3b 224}
c801d85f 225
d3904ceb 226wxString wxRadioBox::GetLabel( int item ) const
c801d85f 227{
d3904ceb 228 return GetString( item );
6de97a3b 229}
c801d85f 230
d3904ceb 231void wxRadioBox::SetLabel( const wxString& label )
c801d85f 232{
d3904ceb
RR
233 wxControl::SetLabel( label );
234 GtkFrame *frame = GTK_FRAME( m_widget );
235 gtk_frame_set_label( frame, wxControl::GetLabel() );
6de97a3b 236}
c801d85f 237
d3904ceb 238void wxRadioBox::SetLabel( int item, const wxString& label )
c801d85f 239{
d3904ceb
RR
240 GSList *list = gtk_radio_button_group( m_radio );
241 list = g_slist_nth( list, g_slist_length(list)-item-1 );
242
243 if (!list)
244 {
245 wxFAIL_MSG( "wxRadioBox wrong index" );
246 return;
247 }
248
249 GtkButton *button = GTK_BUTTON( list->data );
250 GtkLabel *g_label = GTK_LABEL( button->child );
251
252 gtk_label_set( g_label, label );
6de97a3b 253}
c801d85f 254
debe6624 255void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
c801d85f 256{
a3622daa 257 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
6de97a3b 258}
c801d85f 259
d3904ceb 260void wxRadioBox::Enable( bool enable )
c801d85f 261{
d3904ceb
RR
262 wxControl::Enable( enable );
263
264 GSList *item = gtk_radio_button_group( m_radio );
265 while (item)
266 {
267 GtkButton *button = GTK_BUTTON( item->data );
268 GtkWidget *label = button->child;
269 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
270 gtk_widget_set_sensitive( label, enable );
271 item = item->next;
272 }
6de97a3b 273}
c801d85f 274
d3904ceb 275void wxRadioBox::Enable( int item, bool enable )
c801d85f 276{
d3904ceb
RR
277 GSList *list = gtk_radio_button_group( m_radio );
278 list = g_slist_nth( list, g_slist_length(list)-item-1 );
279
280 if (!list)
281 {
282 wxFAIL_MSG( "wxRadioBox wrong index" );
283 return;
284 }
285
286 GtkButton *button = GTK_BUTTON( list->data );
287
288 GtkWidget *label = button->child;
289 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
290 gtk_widget_set_sensitive( label, enable );
6de97a3b 291}
c801d85f 292
d3904ceb 293void wxRadioBox::Show( int item, bool show )
c801d85f 294{
d3904ceb
RR
295 GSList *list = gtk_radio_button_group( m_radio );
296 list = g_slist_nth( list, g_slist_length(list)-item-1 );
297
298 if (!list)
299 {
300 wxFAIL_MSG( "wxRadioBox wrong index" );
301 return;
302 }
303
304 GtkWidget *button = GTK_WIDGET( list->data );
c801d85f 305
d3904ceb
RR
306 if (show)
307 gtk_widget_show( button );
308 else
309 gtk_widget_hide( button );
6de97a3b 310}
c801d85f
KB
311
312wxString wxRadioBox::GetStringSelection(void) const
313{
314 GSList *item = gtk_radio_button_group( m_radio );
315 while (item)
316 {
317 GtkButton *button = GTK_BUTTON( item->data );
318 if (GTK_TOGGLE_BUTTON(button)->active)
319 {
320 GtkLabel *label = GTK_LABEL( button->child );
321 return label->label;
6de97a3b 322 }
c801d85f 323 item = item->next;
6de97a3b 324 }
c801d85f 325 return "";
6de97a3b 326}
c801d85f 327
47908e25 328bool wxRadioBox::SetStringSelection( const wxString&s )
c801d85f 329{
47908e25
RR
330 int res = FindString( s );
331 if (res == -1) return FALSE;
332 SetSelection( res );
c801d85f 333 return TRUE;
6de97a3b 334}
c801d85f
KB
335
336int wxRadioBox::Number(void) const
337{
338 int count = 0;
339 GSList *item = gtk_radio_button_group( m_radio );
340 while (item)
341 {
342 item = item->next;
343 count++;
6de97a3b 344 }
c801d85f 345 return count;
6de97a3b 346}
c801d85f
KB
347
348int wxRadioBox::GetNumberOfRowsOrCols(void) const
349{
350 return 1;
6de97a3b 351}
c801d85f 352
debe6624 353void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
c801d85f 354{
a3622daa 355 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
6de97a3b 356}
c801d85f 357
868a2826
RR
358void wxRadioBox::SetFont( const wxFont &font )
359{
360 wxWindow::SetFont( font );
361
362 GSList *item = gtk_radio_button_group( m_radio );
363 while (item)
364 {
365 GtkButton *button = GTK_BUTTON( item->data );
366
367 gtk_widget_set_style( button->child,
368 gtk_style_ref(
369 gtk_widget_get_style( m_widget ) ) );
370
371 item = item->next;
372 }
373}