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
);
165 bool wxRadioBox::Show( bool show
)
167 wxWindow::Show( show
);
169 wxNode
*node
= m_boxes
.First();
172 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
174 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
182 int wxRadioBox::FindString( const wxString
&s
) const
186 wxNode
*node
= m_boxes
.First();
189 GtkButton
*button
= GTK_BUTTON( node
->Data() );
191 GtkLabel
*label
= GTK_LABEL( button
->child
);
192 if (s
== label
->label
) return count
;
201 void wxRadioBox::SetSelection( int n
)
203 wxNode
*node
= m_boxes
.Nth( n
);
207 wxFAIL_MSG( "wxRadioBox wrong index" );
211 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
213 gtk_toggle_button_set_state( button
, 1 );
216 int wxRadioBox::GetSelection(void) const
220 wxNode
*node
= m_boxes
.First();
223 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
224 if (button
->active
) return count
;
229 wxFAIL_MSG( "wxRadioBox none selected" );
234 wxString
wxRadioBox::GetString( int n
) const
236 wxNode
*node
= m_boxes
.Nth( n
);
240 wxFAIL_MSG( "wxRadioBox wrong index" );
244 GtkButton
*button
= GTK_BUTTON( node
->Data() );
245 GtkLabel
*label
= GTK_LABEL( button
->child
);
247 return wxString( label
->label
);
250 wxString
wxRadioBox::GetLabel( int item
) const
252 return GetString( item
);
255 void wxRadioBox::SetLabel( const wxString
& label
)
257 wxControl::SetLabel( label
);
258 GtkFrame
*frame
= GTK_FRAME( m_widget
);
259 gtk_frame_set_label( frame
, wxControl::GetLabel() );
262 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
264 wxNode
*node
= m_boxes
.Nth( item
);
268 wxFAIL_MSG( "wxRadioBox wrong index" );
272 GtkButton
*button
= GTK_BUTTON( node
->Data() );
273 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
275 gtk_label_set( g_label
, label
);
278 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
280 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
283 void wxRadioBox::Enable( bool enable
)
285 wxControl::Enable( enable
);
287 wxNode
*node
= m_boxes
.First();
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
);
298 void wxRadioBox::Enable( int item
, bool enable
)
300 wxNode
*node
= m_boxes
.Nth( item
);
304 wxFAIL_MSG( "wxRadioBox wrong index" );
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
);
314 void wxRadioBox::Show( int item
, bool show
)
316 wxNode
*node
= m_boxes
.Nth( item
);
320 wxFAIL_MSG( "wxRadioBox wrong index" );
324 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
327 gtk_widget_show( button
);
329 gtk_widget_hide( button
);
332 wxString
wxRadioBox::GetStringSelection(void) const
334 wxNode
*node
= m_boxes
.First();
337 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
340 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
346 wxFAIL_MSG( "wxRadioBox none selected" );
350 bool wxRadioBox::SetStringSelection( const wxString
&s
)
352 int res
= FindString( s
);
353 if (res
== -1) return FALSE
;
358 int wxRadioBox::Number(void) const
360 return m_boxes
.Number();
363 int wxRadioBox::GetNumberOfRowsOrCols(void) const
368 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
370 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
373 void wxRadioBox::SetFont( const wxFont
&font
)
375 wxWindow::SetFont( font
);
377 wxNode
*node
= m_boxes
.First();
380 GtkButton
*button
= GTK_BUTTON( node
->Data() );
382 gtk_widget_set_style( button
->child
,
384 gtk_widget_get_style( m_widget
) ) );
390 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
392 if (window
== m_widget
->window
) return TRUE
;
394 wxNode
*node
= m_boxes
.First();
397 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
399 if (window
== button
->window
) return TRUE
;