]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/radiobox.cpp
Much more aggressive for intercepting events and
[wxWidgets.git] / src / gtk / 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 // "clicked"
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 // wxRadioBox
54 //-----------------------------------------------------------------------------
55
56 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl)
57
58 BEGIN_EVENT_TABLE(wxRadioBox, wxControl)
59 EVT_SIZE(wxRadioBox::OnSize)
60 END_EVENT_TABLE()
61
62 wxRadioBox::wxRadioBox(void)
63 {
64 }
65
66 bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title,
67 const wxPoint &pos, const wxSize &size,
68 int n, const wxString choices[], int WXUNUSED(majorDim),
69 long style, const wxValidator& validator, const wxString &name )
70 {
71 m_alreadySent = FALSE;
72 m_needParent = TRUE;
73
74 PreCreation( parent, id, pos, size, style, name );
75
76 SetValidator( validator );
77
78 m_widget = gtk_frame_new( title );
79
80 int x = m_x+5;
81 int y = m_y+15;
82 int maxLen = 0;
83 int height = 20;
84
85 // if (((m_style & wxRA_VERTICAL) == wxRA_VERTICAL) && (n > 0))
86 if (n > 0)
87 {
88 GSList *radio_button_group = (GSList *) NULL;
89 for (int i = 0; i < n; i++)
90 {
91 if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
92
93 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i] ) );
94
95 ConnectWidget( GTK_WIDGET(m_radio) );
96
97 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
98
99 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
100 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
101
102 gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), GTK_WIDGET(m_radio), x, y );
103
104 int tmp = 22+gdk_string_measure( GTK_WIDGET(m_radio)->style->font, choices[i] );
105 if (tmp > maxLen) maxLen = tmp;
106
107 int width = m_width-10;
108 if (size.x == -1) width = tmp;
109 gtk_widget_set_usize( GTK_WIDGET(m_radio), width, 20 );
110
111 y += 20;
112 height += 20;
113
114 }
115 }
116
117 wxSize newSize = size;
118 if (newSize.x == -1) newSize.x = maxLen+10;
119 if (newSize.y == -1) newSize.y = height;
120 SetSize( newSize.x, newSize.y );
121
122 PostCreation();
123
124 Show( TRUE );
125
126 return TRUE;
127 }
128
129 void wxRadioBox::OnSize( wxSizeEvent &event )
130 {
131 wxControl::OnSize( event );
132
133 int x = m_x+5;
134 int y = m_y+15;
135
136 GSList *item = gtk_radio_button_group( m_radio );
137 while (item)
138 {
139 GtkWidget *button = GTK_WIDGET( item->data );
140
141 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y );
142
143 y += 20;
144
145 item = item->next;
146 }
147 }
148
149 bool wxRadioBox::Show( bool show )
150 {
151 wxWindow::Show( show );
152
153 GSList *item = gtk_radio_button_group( m_radio );
154 while (item)
155 {
156 GtkWidget *w = GTK_WIDGET( item->data );
157 if (show) gtk_widget_show( w ); else gtk_widget_hide( w );
158 item = item->next;
159 }
160
161 return TRUE;
162 }
163
164 int wxRadioBox::FindString( const wxString &s ) const
165 {
166 GSList *item = gtk_radio_button_group( m_radio );
167
168 int count = g_slist_length(item)-1;
169
170 while (item)
171 {
172 GtkButton *b = GTK_BUTTON( item->data );
173 GtkLabel *l = GTK_LABEL( b->child );
174 if (s == l->label) return count;
175 count--;
176 item = item->next;
177 }
178
179 return -1;
180 }
181
182 void wxRadioBox::SetSelection( int n )
183 {
184 GSList *item = gtk_radio_button_group( m_radio );
185 item = g_slist_nth( item, g_slist_length(item)-n-1 );
186
187 if (!item)
188 {
189 wxFAIL_MSG( "wxRadioBox wrong index" );
190 return;
191 }
192
193 GtkToggleButton *button = GTK_TOGGLE_BUTTON( item->data );
194
195 gtk_toggle_button_set_state( button, 1 );
196 }
197
198 int wxRadioBox::GetSelection(void) const
199 {
200 GSList *item = gtk_radio_button_group( m_radio );
201 int count = 0;
202 int found = -1;
203 while (item)
204 {
205 GtkButton *button = GTK_BUTTON( item->data );
206 if (GTK_TOGGLE_BUTTON(button)->active) found = count;
207 count++;
208 item = item->next;
209 }
210
211 return found != -1 ? count-found-1 : -1;
212 }
213
214 wxString wxRadioBox::GetString( int n ) const
215 {
216 GSList *item = gtk_radio_button_group( m_radio );
217
218 item = g_slist_nth( item, g_slist_length(item)-n-1 );
219
220 if (!item)
221 {
222 wxFAIL_MSG( "wxRadioBox wrong index" );
223 return "";
224 }
225
226 GtkButton *button = GTK_BUTTON( item->data );
227 GtkLabel *label = GTK_LABEL( button->child );
228
229 return wxString( label->label );
230 }
231
232 wxString wxRadioBox::GetLabel( int item ) const
233 {
234 return GetString( item );
235 }
236
237 void wxRadioBox::SetLabel( const wxString& label )
238 {
239 wxControl::SetLabel( label );
240 GtkFrame *frame = GTK_FRAME( m_widget );
241 gtk_frame_set_label( frame, wxControl::GetLabel() );
242 }
243
244 void wxRadioBox::SetLabel( int item, const wxString& label )
245 {
246 GSList *list = gtk_radio_button_group( m_radio );
247 list = g_slist_nth( list, g_slist_length(list)-item-1 );
248
249 if (!list)
250 {
251 wxFAIL_MSG( "wxRadioBox wrong index" );
252 return;
253 }
254
255 GtkButton *button = GTK_BUTTON( list->data );
256 GtkLabel *g_label = GTK_LABEL( button->child );
257
258 gtk_label_set( g_label, label );
259 }
260
261 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
262 {
263 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
264 }
265
266 void wxRadioBox::Enable( bool enable )
267 {
268 wxControl::Enable( enable );
269
270 GSList *item = gtk_radio_button_group( m_radio );
271 while (item)
272 {
273 GtkButton *button = GTK_BUTTON( item->data );
274 GtkWidget *label = button->child;
275 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
276 gtk_widget_set_sensitive( label, enable );
277 item = item->next;
278 }
279 }
280
281 void wxRadioBox::Enable( int item, bool enable )
282 {
283 GSList *list = gtk_radio_button_group( m_radio );
284 list = g_slist_nth( list, g_slist_length(list)-item-1 );
285
286 if (!list)
287 {
288 wxFAIL_MSG( "wxRadioBox wrong index" );
289 return;
290 }
291
292 GtkButton *button = GTK_BUTTON( list->data );
293
294 GtkWidget *label = button->child;
295 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
296 gtk_widget_set_sensitive( label, enable );
297 }
298
299 void wxRadioBox::Show( int item, bool show )
300 {
301 GSList *list = gtk_radio_button_group( m_radio );
302 list = g_slist_nth( list, g_slist_length(list)-item-1 );
303
304 if (!list)
305 {
306 wxFAIL_MSG( "wxRadioBox wrong index" );
307 return;
308 }
309
310 GtkWidget *button = GTK_WIDGET( list->data );
311
312 if (show)
313 gtk_widget_show( button );
314 else
315 gtk_widget_hide( button );
316 }
317
318 wxString wxRadioBox::GetStringSelection(void) const
319 {
320 GSList *item = gtk_radio_button_group( m_radio );
321 while (item)
322 {
323 GtkButton *button = GTK_BUTTON( item->data );
324 if (GTK_TOGGLE_BUTTON(button)->active)
325 {
326 GtkLabel *label = GTK_LABEL( button->child );
327 return label->label;
328 }
329 item = item->next;
330 }
331 return "";
332 }
333
334 bool wxRadioBox::SetStringSelection( const wxString&s )
335 {
336 int res = FindString( s );
337 if (res == -1) return FALSE;
338 SetSelection( res );
339 return TRUE;
340 }
341
342 int wxRadioBox::Number(void) const
343 {
344 int count = 0;
345 GSList *item = gtk_radio_button_group( m_radio );
346 while (item)
347 {
348 item = item->next;
349 count++;
350 }
351 return count;
352 }
353
354 int wxRadioBox::GetNumberOfRowsOrCols(void) const
355 {
356 return 1;
357 }
358
359 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
360 {
361 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
362 }
363
364 void wxRadioBox::SetFont( const wxFont &font )
365 {
366 wxWindow::SetFont( font );
367
368 GSList *item = gtk_radio_button_group( m_radio );
369 while (item)
370 {
371 GtkButton *button = GTK_BUTTON( item->data );
372
373 gtk_widget_set_style( button->child,
374 gtk_style_ref(
375 gtk_widget_get_style( m_widget ) ) );
376
377 item = item->next;
378 }
379 }
380
381 bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
382 {
383 if (window == m_widget->window) return TRUE;
384
385 GSList *item = gtk_radio_button_group( m_radio );
386 while (item)
387 {
388 GtkWidget *button = GTK_WIDGET( item->data );
389 if (window == button->window) return TRUE;
390 item = item->next;
391 }
392
393 return FALSE;
394 }