1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "radiobox.h"
16 #include "wx/radiobox.h"
17 #include "wx/dialog.h"
19 #include "wx/gtk/win_gtk.h"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 extern bool g_blockEventsOnDrag
;
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 static void gtk_radiobutton_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxRadioBox
*rb
)
33 if (!rb
->HasVMT()) return;
34 if (g_blockEventsOnDrag
) return;
36 if (rb
->m_alreadySent
)
38 rb
->m_alreadySent
= FALSE
;
42 rb
->m_alreadySent
= TRUE
;
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
);
52 //-----------------------------------------------------------------------------
54 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
56 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
57 EVT_SIZE(wxRadioBox::OnSize
)
60 wxRadioBox::wxRadioBox(void)
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
)
69 m_alreadySent
= FALSE
;
72 PreCreation( parent
, id
, pos
, size
, style
, name
);
74 SetValidator( validator
);
76 m_widget
= gtk_frame_new( title
);
83 // if (((m_style & wxRA_VERTICAL) == wxRA_VERTICAL) && (n > 0))
86 GSList
*radio_button_group
= (GSList
*) NULL
;
87 for (int i
= 0; i
< n
; i
++)
89 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
91 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
] ) );
93 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
95 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
96 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
98 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
), GTK_WIDGET(m_radio
), x
, y
);
100 int tmp
= 22+gdk_string_measure( GTK_WIDGET(m_radio
)->style
->font
, choices
[i
] );
101 if (tmp
> maxLen
) maxLen
= tmp
;
103 int width
= m_width
-10;
104 if (size
.x
== -1) width
= tmp
;
105 gtk_widget_set_usize( GTK_WIDGET(m_radio
), width
, 20 );
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
);
125 void wxRadioBox::OnSize( wxSizeEvent
&WXUNUSED(event
) )
130 GSList
*item
= gtk_radio_button_group( m_radio
);
133 GtkWidget
*button
= GTK_WIDGET( item
->data
);
135 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
143 bool wxRadioBox::Show( bool show
)
145 wxWindow::Show( show
);
147 GSList
*item
= gtk_radio_button_group( m_radio
);
150 GtkWidget
*w
= GTK_WIDGET( item
->data
);
151 if (show
) gtk_widget_show( w
); else gtk_widget_hide( w
);
158 int wxRadioBox::FindString( const wxString
&s
) const
160 GSList
*item
= gtk_radio_button_group( m_radio
);
162 int count
= g_slist_length(item
)-1;
166 GtkButton
*b
= GTK_BUTTON( item
->data
);
167 GtkLabel
*l
= GTK_LABEL( b
->child
);
168 if (s
== l
->label
) return count
;
176 void wxRadioBox::SetSelection( int n
)
178 GSList
*item
= gtk_radio_button_group( m_radio
);
179 item
= g_slist_nth( item
, g_slist_length(item
)-n
-1 );
183 wxFAIL_MSG( "wxRadioBox wrong index" );
187 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( item
->data
);
189 gtk_toggle_button_set_state( button
, 1 );
192 int wxRadioBox::GetSelection(void) const
194 GSList
*item
= gtk_radio_button_group( m_radio
);
199 GtkButton
*button
= GTK_BUTTON( item
->data
);
200 if (GTK_TOGGLE_BUTTON(button
)->active
) found
= count
;
205 return found
!= -1 ? count
-found
-1 : -1;
208 wxString
wxRadioBox::GetString( int n
) const
210 GSList
*item
= gtk_radio_button_group( m_radio
);
212 item
= g_slist_nth( item
, g_slist_length(item
)-n
-1 );
216 wxFAIL_MSG( "wxRadioBox wrong index" );
220 GtkButton
*button
= GTK_BUTTON( item
->data
);
221 GtkLabel
*label
= GTK_LABEL( button
->child
);
223 return wxString( label
->label
);
226 wxString
wxRadioBox::GetLabel( int item
) const
228 return GetString( item
);
231 void wxRadioBox::SetLabel( const wxString
& label
)
233 wxControl::SetLabel( label
);
234 GtkFrame
*frame
= GTK_FRAME( m_widget
);
235 gtk_frame_set_label( frame
, wxControl::GetLabel() );
238 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
240 GSList
*list
= gtk_radio_button_group( m_radio
);
241 list
= g_slist_nth( list
, g_slist_length(list
)-item
-1 );
245 wxFAIL_MSG( "wxRadioBox wrong index" );
249 GtkButton
*button
= GTK_BUTTON( list
->data
);
250 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
252 gtk_label_set( g_label
, label
);
255 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
257 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
260 void wxRadioBox::Enable( bool enable
)
262 wxControl::Enable( enable
);
264 GSList
*item
= gtk_radio_button_group( m_radio
);
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
);
275 void wxRadioBox::Enable( int item
, bool enable
)
277 GSList
*list
= gtk_radio_button_group( m_radio
);
278 list
= g_slist_nth( list
, g_slist_length(list
)-item
-1 );
282 wxFAIL_MSG( "wxRadioBox wrong index" );
286 GtkButton
*button
= GTK_BUTTON( list
->data
);
288 GtkWidget
*label
= button
->child
;
289 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
290 gtk_widget_set_sensitive( label
, enable
);
293 void wxRadioBox::Show( int item
, bool show
)
295 GSList
*list
= gtk_radio_button_group( m_radio
);
296 list
= g_slist_nth( list
, g_slist_length(list
)-item
-1 );
300 wxFAIL_MSG( "wxRadioBox wrong index" );
304 GtkWidget
*button
= GTK_WIDGET( list
->data
);
307 gtk_widget_show( button
);
309 gtk_widget_hide( button
);
312 wxString
wxRadioBox::GetStringSelection(void) const
314 GSList
*item
= gtk_radio_button_group( m_radio
);
317 GtkButton
*button
= GTK_BUTTON( item
->data
);
318 if (GTK_TOGGLE_BUTTON(button
)->active
)
320 GtkLabel
*label
= GTK_LABEL( button
->child
);
328 bool wxRadioBox::SetStringSelection( const wxString
&s
)
330 int res
= FindString( s
);
331 if (res
== -1) return FALSE
;
336 int wxRadioBox::Number(void) const
339 GSList
*item
= gtk_radio_button_group( m_radio
);
348 int wxRadioBox::GetNumberOfRowsOrCols(void) const
353 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
355 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
358 void wxRadioBox::SetFont( const wxFont
&font
)
360 wxWindow::SetFont( font
);
362 GSList
*item
= gtk_radio_button_group( m_radio
);
365 GtkButton
*button
= GTK_BUTTON( item
->data
);
367 gtk_widget_set_style( button
->child
,
369 gtk_widget_get_style( m_widget
) ) );