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