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 wxRadioBox::wxRadioBox(void)
60 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
61 const wxPoint
&pos
, const wxSize
&size
,
62 int n
, const wxString choices
[], int WXUNUSED(majorDim
),
63 long style
, const wxValidator
& validator
, const wxString
&name
)
65 m_alreadySent
= FALSE
;
68 PreCreation( parent
, id
, pos
, size
, style
, name
);
70 SetValidator( validator
);
72 m_widget
= gtk_frame_new( title
);
79 // if (((m_style & wxRA_VERTICAL) == wxRA_VERTICAL) && (n > 0))
82 GSList
*radio_button_group
= (GSList
*) NULL
;
83 for (int i
= 0; i
< n
; i
++)
85 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
87 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
] ) );
89 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
91 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
92 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
94 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
), GTK_WIDGET(m_radio
), x
, y
);
96 int tmp
= 22+gdk_string_measure( GTK_WIDGET(m_radio
)->style
->font
, choices
[i
] );
97 if (tmp
> maxLen
) maxLen
= tmp
;
99 int width
= m_width
-10;
100 if (size
.x
== -1) width
= tmp
;
101 gtk_widget_set_usize( GTK_WIDGET(m_radio
), width
, 20 );
109 wxSize newSize
= size
;
110 if (newSize
.x
== -1) newSize
.x
= maxLen
+10;
111 if (newSize
.y
== -1) newSize
.y
= height
;
112 SetSize( newSize
.x
, newSize
.y
);
121 bool wxRadioBox::Show( bool show
)
123 wxWindow::Show( show
);
125 GSList
*item
= gtk_radio_button_group( m_radio
);
128 GtkWidget
*w
= GTK_WIDGET( item
->data
);
129 if (show
) gtk_widget_show( w
); else gtk_widget_hide( w
);
136 int wxRadioBox::FindString( const wxString
&s
) const
138 GSList
*item
= gtk_radio_button_group( m_radio
);
140 int count
= g_slist_length(item
)-1;
144 GtkButton
*b
= GTK_BUTTON( item
->data
);
145 GtkLabel
*l
= GTK_LABEL( b
->child
);
146 if (s
== l
->label
) return count
;
154 void wxRadioBox::SetSelection( int n
)
156 GSList
*item
= gtk_radio_button_group( m_radio
);
157 item
= g_slist_nth( item
, g_slist_length(item
)-n
-1 );
160 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( item
->data
);
162 gtk_toggle_button_set_state( button
, 1 );
165 int wxRadioBox::GetSelection(void) const
167 GSList
*item
= gtk_radio_button_group( m_radio
);
172 GtkButton
*button
= GTK_BUTTON( item
->data
);
173 if (GTK_TOGGLE_BUTTON(button
)->active
) found
= count
;
178 return found
!= -1 ? count
-found
-1 : -1;
181 wxString
wxRadioBox::GetString( int n
) const
183 GSList
*item
= gtk_radio_button_group( m_radio
);
185 item
= g_slist_nth( item
, g_slist_length(item
)-n
-1 );
186 if (!item
) return "";
188 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( item
->data
);
190 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
192 return wxString( label
->label
);
195 wxString
wxRadioBox::GetLabel(void) const
197 return wxControl::GetLabel();
200 void wxRadioBox::SetLabel( const wxString
& WXUNUSED(label
) )
202 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
205 void wxRadioBox::SetLabel( int WXUNUSED(item
), const wxString
& WXUNUSED(label
) )
207 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
210 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
212 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
215 wxString
wxRadioBox::GetLabel( int WXUNUSED(item
) ) const
217 wxFAIL_MSG("wxRadioBox::GetLabel not implemented.");
221 void wxRadioBox::Enable( bool WXUNUSED(enable
) )
223 wxFAIL_MSG("wxRadioBox::Enable not implemented.");
226 void wxRadioBox::Enable( int WXUNUSED(item
), bool WXUNUSED(enable
) )
228 wxFAIL_MSG("wxRadioBox::Enable not implemented.");
231 void wxRadioBox::Show( int WXUNUSED(item
), bool WXUNUSED(show
) )
233 wxFAIL_MSG("wxRadioBox::Show not implemented.");
236 wxString
wxRadioBox::GetStringSelection(void) const
238 GSList
*item
= gtk_radio_button_group( m_radio
);
241 GtkButton
*button
= GTK_BUTTON( item
->data
);
242 if (GTK_TOGGLE_BUTTON(button
)->active
)
244 GtkLabel
*label
= GTK_LABEL( button
->child
);
252 bool wxRadioBox::SetStringSelection( const wxString
&s
)
254 int res
= FindString( s
);
255 if (res
== -1) return FALSE
;
260 int wxRadioBox::Number(void) const
263 GSList
*item
= gtk_radio_button_group( m_radio
);
272 int wxRadioBox::GetNumberOfRowsOrCols(void) const
277 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
279 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
282 void wxRadioBox::SetFont( const wxFont
&font
)
284 wxWindow::SetFont( font
);
286 GSList
*item
= gtk_radio_button_group( m_radio
);
289 GtkButton
*button
= GTK_BUTTON( item
->data
);
291 gtk_widget_set_style( button
->child
,
293 gtk_widget_get_style( m_widget
) ) );