Fillid in many more missing functions (such as Enable())
[wxWidgets.git] / src / gtk1 / radiobox.cpp
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
21 //-----------------------------------------------------------------------------
22 // data
23 //-----------------------------------------------------------------------------
24
25 extern bool g_blockEventsOnDrag;
26
27 //-----------------------------------------------------------------------------
28 // wxRadioBox
29 //-----------------------------------------------------------------------------
30
31 static void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioBox *rb )
32 {
33 if (!rb->HasVMT()) return;
34 if (g_blockEventsOnDrag) return;
35
36 if (rb->m_alreadySent)
37 {
38 rb->m_alreadySent = FALSE;
39 return;
40 }
41
42 rb->m_alreadySent = TRUE;
43
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 );
49 rb->GetEventHandler()->ProcessEvent(event);
50 }
51
52 //-----------------------------------------------------------------------------
53
54 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl)
55
56 BEGIN_EVENT_TABLE(wxRadioBox, wxControl)
57 EVT_SIZE(wxRadioBox::OnSize)
58 END_EVENT_TABLE()
59
60 wxRadioBox::wxRadioBox(void)
61 {
62 }
63
64 bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title,
65 const wxPoint &pos, const wxSize &size,
66 int n, const wxString choices[], int WXUNUSED(majorDim),
67 long style, const wxValidator& validator, const wxString &name )
68 {
69 m_alreadySent = FALSE;
70 m_needParent = TRUE;
71
72 PreCreation( parent, id, pos, size, style, name );
73
74 SetValidator( validator );
75
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 {
86 GSList *radio_button_group = (GSList *) NULL;
87 for (int i = 0; i < n; i++)
88 {
89 if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
90
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
110 }
111 }
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;
123 }
124
125 void 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
143 bool wxRadioBox::Show( bool show )
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;
153 }
154
155 return TRUE;
156 }
157
158 int wxRadioBox::FindString( const wxString &s ) const
159 {
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;
171 }
172
173 return -1;
174 }
175
176 void wxRadioBox::SetSelection( int n )
177 {
178 GSList *item = gtk_radio_button_group( m_radio );
179 item = g_slist_nth( item, g_slist_length(item)-n-1 );
180
181 if (!item)
182 {
183 wxFAIL_MSG( "wxRadioBox wrong index" );
184 return;
185 }
186
187 GtkToggleButton *button = GTK_TOGGLE_BUTTON( item->data );
188
189 gtk_toggle_button_set_state( button, 1 );
190 }
191
192 int wxRadioBox::GetSelection(void) const
193 {
194 GSList *item = gtk_radio_button_group( m_radio );
195 int count = 0;
196 int found = -1;
197 while (item)
198 {
199 GtkButton *button = GTK_BUTTON( item->data );
200 if (GTK_TOGGLE_BUTTON(button)->active) found = count;
201 count++;
202 item = item->next;
203 }
204
205 return found != -1 ? count-found-1 : -1;
206 }
207
208 wxString wxRadioBox::GetString( int n ) const
209 {
210 GSList *item = gtk_radio_button_group( m_radio );
211
212 item = g_slist_nth( item, g_slist_length(item)-n-1 );
213
214 if (!item)
215 {
216 wxFAIL_MSG( "wxRadioBox wrong index" );
217 return "";
218 }
219
220 GtkButton *button = GTK_BUTTON( item->data );
221 GtkLabel *label = GTK_LABEL( button->child );
222
223 return wxString( label->label );
224 }
225
226 wxString wxRadioBox::GetLabel( int item ) const
227 {
228 return GetString( item );
229 }
230
231 void wxRadioBox::SetLabel( const wxString& label )
232 {
233 wxControl::SetLabel( label );
234 GtkFrame *frame = GTK_FRAME( m_widget );
235 gtk_frame_set_label( frame, wxControl::GetLabel() );
236 }
237
238 void wxRadioBox::SetLabel( int item, const wxString& label )
239 {
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 );
253 }
254
255 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
256 {
257 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
258 }
259
260 void wxRadioBox::Enable( bool enable )
261 {
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 }
273 }
274
275 void wxRadioBox::Enable( int item, bool enable )
276 {
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 );
291 }
292
293 void wxRadioBox::Show( int item, bool show )
294 {
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 );
305
306 if (show)
307 gtk_widget_show( button );
308 else
309 gtk_widget_hide( button );
310 }
311
312 wxString 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;
322 }
323 item = item->next;
324 }
325 return "";
326 }
327
328 bool wxRadioBox::SetStringSelection( const wxString&s )
329 {
330 int res = FindString( s );
331 if (res == -1) return FALSE;
332 SetSelection( res );
333 return TRUE;
334 }
335
336 int 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++;
344 }
345 return count;
346 }
347
348 int wxRadioBox::GetNumberOfRowsOrCols(void) const
349 {
350 return 1;
351 }
352
353 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
354 {
355 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
356 }
357
358 void 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 }