1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "checkbox.h"
15 #include "wx/checkbox.h"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 extern bool g_blockEventsOnDrag
;
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 static void gtk_checkbox_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxCheckBox
*cb
)
32 if (!cb
->HasVMT()) return;
34 if (cb
->m_blockFirstEvent
)
36 cb
->m_blockFirstEvent
= FALSE
;
40 if (g_blockEventsOnDrag
) return;
42 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, cb
->GetId());
43 event
.SetInt( cb
->GetValue() );
44 event
.SetEventObject(cb
);
45 cb
->GetEventHandler()->ProcessEvent(event
);
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
,wxControl
)
54 wxCheckBox::wxCheckBox()
58 bool wxCheckBox::Create(wxWindow
*parent
,
60 const wxString
&label
,
64 const wxValidator
& validator
,
65 const wxString
&name
)
68 m_acceptsFocus
= TRUE
;
70 PreCreation( parent
, id
, pos
, size
, style
, name
);
72 m_blockFirstEvent
= FALSE
;
74 SetValidator( validator
);
75 wxControl::SetLabel( label
);
77 if ( style
& wxALIGN_RIGHT
)
79 // VZ: as I don't know a way to create a right aligned checkbox with
80 // GTK we will create a checkbox without label and a label at the
82 m_widgetCheckbox
= gtk_check_button_new();
84 m_widgetLabel
= gtk_label_new(m_label
);
85 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
87 m_widget
= gtk_hbox_new(FALSE
, 0);
88 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
89 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
91 // VZ: why do I have to do this to make them appear?
92 gtk_widget_show( m_widgetLabel
);
93 gtk_widget_show( m_widgetCheckbox
);
97 m_widgetCheckbox
= gtk_check_button_new_with_label( m_label
);
98 m_widgetLabel
= GTK_BUTTON( m_widgetCheckbox
)->child
;
99 m_widget
= m_widgetCheckbox
;
102 wxSize
newSize(size
);
105 newSize
.x
= 25 + gdk_string_measure( m_widgetCheckbox
->style
->font
,
111 SetSize( newSize
.x
, newSize
.y
);
113 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox
),
115 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback
),
118 m_parent
->AddChild( this );
120 (m_parent
->m_insertCallback
)( m_parent
, this );
124 gtk_widget_realize( m_widgetLabel
);
125 gtk_widget_realize( m_widgetCheckbox
);
127 SetBackgroundColour( parent
->GetBackgroundColour() );
128 SetForegroundColour( parent
->GetForegroundColour() );
129 SetFont( parent
->GetFont() );
136 void wxCheckBox::SetValue( bool state
)
138 wxCHECK_RET( m_widgetCheckbox
!= NULL
, "invalid checkbox" );
140 if ( state
== GetValue() )
143 // for compatibility with wxMSW don't send notification when the check box
144 // state is changed programmatically
145 m_blockFirstEvent
= TRUE
;
147 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
150 bool wxCheckBox::GetValue() const
152 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, FALSE
, "invalid checkbox" );
154 return GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->active
;
157 void wxCheckBox::SetLabel( const wxString
& label
)
159 wxCHECK_RET( m_widgetLabel
!= NULL
, "invalid checkbox" );
161 wxControl::SetLabel( label
);
163 gtk_label_set( GTK_LABEL(m_widgetLabel
), GetLabel() );
166 void wxCheckBox::Enable( bool enable
)
168 wxCHECK_RET( m_widgetLabel
!= NULL
, "invalid checkbox" );
170 wxControl::Enable( enable
);
172 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
175 void wxCheckBox::ApplyWidgetStyle()
178 gtk_widget_set_style( m_widgetCheckbox
, m_widgetStyle
);
179 gtk_widget_set_style( m_widgetLabel
, m_widgetStyle
);