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 //-----------------------------------------------------------------------------
56 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
58 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
59 EVT_SIZE(wxRadioBox::OnSize
)
62 wxRadioBox::wxRadioBox(void)
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
)
71 m_alreadySent
= FALSE
;
74 PreCreation( parent
, id
, pos
, size
, style
, name
);
76 SetValidator( validator
);
78 m_widget
= gtk_frame_new( title
);
85 // if (((m_style & wxRA_VERTICAL) == wxRA_VERTICAL) && (n > 0))
88 GSList
*radio_button_group
= (GSList
*) NULL
;
89 for (int i
= 0; i
< n
; i
++)
91 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
93 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
] ) );
95 ConnectWidget( GTK_WIDGET(m_radio
) );
97 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
99 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
100 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
102 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
), GTK_WIDGET(m_radio
), x
, y
);
104 int tmp
= 22+gdk_string_measure( GTK_WIDGET(m_radio
)->style
->font
, choices
[i
] );
105 if (tmp
> maxLen
) maxLen
= tmp
;
107 int width
= m_width
-10;
108 if (size
.x
== -1) width
= tmp
;
109 gtk_widget_set_usize( GTK_WIDGET(m_radio
), width
, 20 );
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
);
129 void wxRadioBox::OnSize( wxSizeEvent
&event
)
131 wxControl::OnSize( event
);
136 GSList
*item
= gtk_radio_button_group( m_radio
);
139 GtkWidget
*button
= GTK_WIDGET( item
->data
);
141 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
149 bool wxRadioBox::Show( bool show
)
151 wxWindow::Show( show
);
153 GSList
*item
= gtk_radio_button_group( m_radio
);
156 GtkWidget
*w
= GTK_WIDGET( item
->data
);
157 if (show
) gtk_widget_show( w
); else gtk_widget_hide( w
);
164 int wxRadioBox::FindString( const wxString
&s
) const
166 GSList
*item
= gtk_radio_button_group( m_radio
);
168 int count
= g_slist_length(item
)-1;
172 GtkButton
*b
= GTK_BUTTON( item
->data
);
173 GtkLabel
*l
= GTK_LABEL( b
->child
);
174 if (s
== l
->label
) return count
;
182 void wxRadioBox::SetSelection( int n
)
184 GSList
*item
= gtk_radio_button_group( m_radio
);
185 item
= g_slist_nth( item
, g_slist_length(item
)-n
-1 );
189 wxFAIL_MSG( "wxRadioBox wrong index" );
193 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( item
->data
);
195 gtk_toggle_button_set_state( button
, 1 );
198 int wxRadioBox::GetSelection(void) const
200 GSList
*item
= gtk_radio_button_group( m_radio
);
205 GtkButton
*button
= GTK_BUTTON( item
->data
);
206 if (GTK_TOGGLE_BUTTON(button
)->active
) found
= count
;
211 return found
!= -1 ? count
-found
-1 : -1;
214 wxString
wxRadioBox::GetString( int n
) const
216 GSList
*item
= gtk_radio_button_group( m_radio
);
218 item
= g_slist_nth( item
, g_slist_length(item
)-n
-1 );
222 wxFAIL_MSG( "wxRadioBox wrong index" );
226 GtkButton
*button
= GTK_BUTTON( item
->data
);
227 GtkLabel
*label
= GTK_LABEL( button
->child
);
229 return wxString( label
->label
);
232 wxString
wxRadioBox::GetLabel( int item
) const
234 return GetString( item
);
237 void wxRadioBox::SetLabel( const wxString
& label
)
239 wxControl::SetLabel( label
);
240 GtkFrame
*frame
= GTK_FRAME( m_widget
);
241 gtk_frame_set_label( frame
, wxControl::GetLabel() );
244 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
246 GSList
*list
= gtk_radio_button_group( m_radio
);
247 list
= g_slist_nth( list
, g_slist_length(list
)-item
-1 );
251 wxFAIL_MSG( "wxRadioBox wrong index" );
255 GtkButton
*button
= GTK_BUTTON( list
->data
);
256 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
258 gtk_label_set( g_label
, label
);
261 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
263 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
266 void wxRadioBox::Enable( bool enable
)
268 wxControl::Enable( enable
);
270 GSList
*item
= gtk_radio_button_group( m_radio
);
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
);
281 void wxRadioBox::Enable( int item
, bool enable
)
283 GSList
*list
= gtk_radio_button_group( m_radio
);
284 list
= g_slist_nth( list
, g_slist_length(list
)-item
-1 );
288 wxFAIL_MSG( "wxRadioBox wrong index" );
292 GtkButton
*button
= GTK_BUTTON( list
->data
);
294 GtkWidget
*label
= button
->child
;
295 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
296 gtk_widget_set_sensitive( label
, enable
);
299 void wxRadioBox::Show( int item
, bool show
)
301 GSList
*list
= gtk_radio_button_group( m_radio
);
302 list
= g_slist_nth( list
, g_slist_length(list
)-item
-1 );
306 wxFAIL_MSG( "wxRadioBox wrong index" );
310 GtkWidget
*button
= GTK_WIDGET( list
->data
);
313 gtk_widget_show( button
);
315 gtk_widget_hide( button
);
318 wxString
wxRadioBox::GetStringSelection(void) const
320 GSList
*item
= gtk_radio_button_group( m_radio
);
323 GtkButton
*button
= GTK_BUTTON( item
->data
);
324 if (GTK_TOGGLE_BUTTON(button
)->active
)
326 GtkLabel
*label
= GTK_LABEL( button
->child
);
334 bool wxRadioBox::SetStringSelection( const wxString
&s
)
336 int res
= FindString( s
);
337 if (res
== -1) return FALSE
;
342 int wxRadioBox::Number(void) const
345 GSList
*item
= gtk_radio_button_group( m_radio
);
354 int wxRadioBox::GetNumberOfRowsOrCols(void) const
359 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
361 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
364 void wxRadioBox::SetFont( const wxFont
&font
)
366 wxWindow::SetFont( font
);
368 GSList
*item
= gtk_radio_button_group( m_radio
);
371 GtkButton
*button
= GTK_BUTTON( item
->data
);
373 gtk_widget_set_style( button
->child
,
375 gtk_widget_get_style( m_widget
) ) );
381 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
383 if (window
== m_widget
->window
) return TRUE
;
385 GSList
*item
= gtk_radio_button_group( m_radio
);
388 GtkWidget
*button
= GTK_WIDGET( item
->data
);
389 if (window
== button
->window
) return TRUE
;