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 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
87 // if (((m_style & wxRA_VERTICAL) == wxRA_VERTICAL) && (n > 0))
90 GSList
*radio_button_group
= (GSList
*) NULL
;
91 for (int i
= 0; i
< n
; i
++)
93 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
95 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
] ) );
97 m_boxes
.Append( (wxObject
*) m_radio
);
99 ConnectWidget( GTK_WIDGET(m_radio
) );
101 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
103 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
104 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
106 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
), GTK_WIDGET(m_radio
), x
, y
);
108 int tmp
= 22+gdk_string_measure( GTK_WIDGET(m_radio
)->style
->font
, choices
[i
] );
109 if (tmp
> maxLen
) maxLen
= tmp
;
111 int width
= m_width
-10;
112 if (size
.x
== -1) width
= tmp
;
113 gtk_widget_set_usize( GTK_WIDGET(m_radio
), width
, 20 );
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
);
135 wxRadioBox::~wxRadioBox(void)
137 wxNode
*node
= m_boxes
.First();
140 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
141 gtk_widget_destroy( button
);
146 void wxRadioBox::OnSize( wxSizeEvent
&event
)
148 wxControl::OnSize( event
);
153 wxNode
*node
= m_boxes
.First();
156 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
158 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
163 gtk_widget_set_usize( button
, w
, 20 );
169 bool wxRadioBox::Show( bool show
)
171 wxWindow::Show( show
);
173 wxNode
*node
= m_boxes
.First();
176 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
178 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
186 int wxRadioBox::FindString( const wxString
&s
) const
190 wxNode
*node
= m_boxes
.First();
193 GtkButton
*button
= GTK_BUTTON( node
->Data() );
195 GtkLabel
*label
= GTK_LABEL( button
->child
);
196 if (s
== label
->label
) return count
;
205 void wxRadioBox::SetSelection( int n
)
207 wxNode
*node
= m_boxes
.Nth( n
);
211 wxFAIL_MSG( "wxRadioBox wrong index" );
215 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
217 gtk_toggle_button_set_state( button
, 1 );
220 int wxRadioBox::GetSelection(void) const
224 wxNode
*node
= m_boxes
.First();
227 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
228 if (button
->active
) return count
;
233 wxFAIL_MSG( "wxRadioBox none selected" );
238 wxString
wxRadioBox::GetString( int n
) const
240 wxNode
*node
= m_boxes
.Nth( n
);
244 wxFAIL_MSG( "wxRadioBox wrong index" );
248 GtkButton
*button
= GTK_BUTTON( node
->Data() );
249 GtkLabel
*label
= GTK_LABEL( button
->child
);
251 return wxString( label
->label
);
254 wxString
wxRadioBox::GetLabel( int item
) const
256 return GetString( item
);
259 void wxRadioBox::SetLabel( const wxString
& label
)
261 wxControl::SetLabel( label
);
262 GtkFrame
*frame
= GTK_FRAME( m_widget
);
263 gtk_frame_set_label( frame
, wxControl::GetLabel() );
266 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
268 wxNode
*node
= m_boxes
.Nth( item
);
272 wxFAIL_MSG( "wxRadioBox wrong index" );
276 GtkButton
*button
= GTK_BUTTON( node
->Data() );
277 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
279 gtk_label_set( g_label
, label
);
282 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
284 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
287 void wxRadioBox::Enable( bool enable
)
289 wxControl::Enable( enable
);
291 wxNode
*node
= m_boxes
.First();
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
);
302 void wxRadioBox::Enable( int item
, bool enable
)
304 wxNode
*node
= m_boxes
.Nth( item
);
308 wxFAIL_MSG( "wxRadioBox wrong index" );
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
);
318 void wxRadioBox::Show( int item
, bool show
)
320 wxNode
*node
= m_boxes
.Nth( item
);
324 wxFAIL_MSG( "wxRadioBox wrong index" );
328 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
331 gtk_widget_show( button
);
333 gtk_widget_hide( button
);
336 wxString
wxRadioBox::GetStringSelection(void) const
338 wxNode
*node
= m_boxes
.First();
341 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
344 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
350 wxFAIL_MSG( "wxRadioBox none selected" );
354 bool wxRadioBox::SetStringSelection( const wxString
&s
)
356 int res
= FindString( s
);
357 if (res
== -1) return FALSE
;
362 int wxRadioBox::Number(void) const
364 return m_boxes
.Number();
367 int wxRadioBox::GetNumberOfRowsOrCols(void) const
372 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
374 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
377 void wxRadioBox::SetFont( const wxFont
&font
)
379 wxWindow::SetFont( font
);
381 wxNode
*node
= m_boxes
.First();
384 GtkButton
*button
= GTK_BUTTON( node
->Data() );
386 gtk_widget_set_style( button
->child
,
388 gtk_widget_get_style( m_widget
) ) );
394 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
396 if (window
== m_widget
->window
) return TRUE
;
398 wxNode
*node
= m_boxes
.First();
401 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
403 if (window
== button
->window
) return TRUE
;