]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/radiobox.cpp
Rewrote wxRadioBox (recompile)
[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 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 node = node->Next();
162 }
163 }
164
165 bool wxRadioBox::Show( bool show )
166 {
167 wxWindow::Show( show );
168
169 wxNode *node = m_boxes.First();
170 while (node)
171 {
172 GtkWidget *button = GTK_WIDGET( node->Data() );
173
174 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
175
176 node = node->Next();
177 }
178
179 return TRUE;
180 }
181
182 int wxRadioBox::FindString( const wxString &s ) const
183 {
184 int count = 0;
185
186 wxNode *node = m_boxes.First();
187 while (node)
188 {
189 GtkButton *button = GTK_BUTTON( node->Data() );
190
191 GtkLabel *label = GTK_LABEL( button->child );
192 if (s == label->label) return count;
193 count++;
194
195 node = node->Next();
196 }
197
198 return -1;
199 }
200
201 void wxRadioBox::SetSelection( int n )
202 {
203 wxNode *node = m_boxes.Nth( n );
204
205 if (!node)
206 {
207 wxFAIL_MSG( "wxRadioBox wrong index" );
208 return;
209 }
210
211 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
212
213 gtk_toggle_button_set_state( button, 1 );
214 }
215
216 int wxRadioBox::GetSelection(void) const
217 {
218 int count = 0;
219
220 wxNode *node = m_boxes.First();
221 while (node)
222 {
223 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
224 if (button->active) return count;
225 count++;
226 node = node->Next();
227 }
228
229 wxFAIL_MSG( "wxRadioBox none selected" );
230
231 return -1;
232 }
233
234 wxString wxRadioBox::GetString( int n ) const
235 {
236 wxNode *node = m_boxes.Nth( n );
237
238 if (!node)
239 {
240 wxFAIL_MSG( "wxRadioBox wrong index" );
241 return "";
242 }
243
244 GtkButton *button = GTK_BUTTON( node->Data() );
245 GtkLabel *label = GTK_LABEL( button->child );
246
247 return wxString( label->label );
248 }
249
250 wxString wxRadioBox::GetLabel( int item ) const
251 {
252 return GetString( item );
253 }
254
255 void wxRadioBox::SetLabel( const wxString& label )
256 {
257 wxControl::SetLabel( label );
258 GtkFrame *frame = GTK_FRAME( m_widget );
259 gtk_frame_set_label( frame, wxControl::GetLabel() );
260 }
261
262 void wxRadioBox::SetLabel( int item, const wxString& label )
263 {
264 wxNode *node = m_boxes.Nth( item );
265
266 if (!node)
267 {
268 wxFAIL_MSG( "wxRadioBox wrong index" );
269 return;
270 }
271
272 GtkButton *button = GTK_BUTTON( node->Data() );
273 GtkLabel *g_label = GTK_LABEL( button->child );
274
275 gtk_label_set( g_label, label );
276 }
277
278 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
279 {
280 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
281 }
282
283 void wxRadioBox::Enable( bool enable )
284 {
285 wxControl::Enable( enable );
286
287 wxNode *node = m_boxes.First();
288 while (node)
289 {
290 GtkButton *button = GTK_BUTTON( node->Data() );
291 GtkWidget *label = button->child;
292 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
293 gtk_widget_set_sensitive( label, enable );
294 node = node->Next();
295 }
296 }
297
298 void wxRadioBox::Enable( int item, bool enable )
299 {
300 wxNode *node = m_boxes.Nth( item );
301
302 if (!node)
303 {
304 wxFAIL_MSG( "wxRadioBox wrong index" );
305 return;
306 }
307
308 GtkButton *button = GTK_BUTTON( node->Data() );
309 GtkWidget *label = button->child;
310 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
311 gtk_widget_set_sensitive( label, enable );
312 }
313
314 void wxRadioBox::Show( int item, bool show )
315 {
316 wxNode *node = m_boxes.Nth( item );
317
318 if (!node)
319 {
320 wxFAIL_MSG( "wxRadioBox wrong index" );
321 return;
322 }
323
324 GtkWidget *button = GTK_WIDGET( node->Data() );
325
326 if (show)
327 gtk_widget_show( button );
328 else
329 gtk_widget_hide( button );
330 }
331
332 wxString wxRadioBox::GetStringSelection(void) const
333 {
334 wxNode *node = m_boxes.First();
335 while (node)
336 {
337 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
338 if (button->active)
339 {
340 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
341 return label->label;
342 }
343 node = node->Next();
344 }
345
346 wxFAIL_MSG( "wxRadioBox none selected" );
347 return "";
348 }
349
350 bool wxRadioBox::SetStringSelection( const wxString&s )
351 {
352 int res = FindString( s );
353 if (res == -1) return FALSE;
354 SetSelection( res );
355 return TRUE;
356 }
357
358 int wxRadioBox::Number(void) const
359 {
360 return m_boxes.Number();
361 }
362
363 int wxRadioBox::GetNumberOfRowsOrCols(void) const
364 {
365 return 1;
366 }
367
368 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
369 {
370 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
371 }
372
373 void wxRadioBox::SetFont( const wxFont &font )
374 {
375 wxWindow::SetFont( font );
376
377 wxNode *node = m_boxes.First();
378 while (node)
379 {
380 GtkButton *button = GTK_BUTTON( node->Data() );
381
382 gtk_widget_set_style( button->child,
383 gtk_style_ref(
384 gtk_widget_get_style( m_widget ) ) );
385
386 node = node->Next();
387 }
388 }
389
390 bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
391 {
392 if (window == m_widget->window) return TRUE;
393
394 wxNode *node = m_boxes.First();
395 while (node)
396 {
397 GtkWidget *button = GTK_WIDGET( node->Data() );
398
399 if (window == button->window) return TRUE;
400
401 node = node->Next();
402 }
403
404 return FALSE;
405 }