1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/checkbox.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/checkbox.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 extern bool g_blockEventsOnDrag
;
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
30 static void gtk_checkbox_toggled_callback(GtkWidget
*widget
, wxCheckBox
*cb
)
32 if (!cb
->m_hasVMT
) return;
34 if (g_blockEventsOnDrag
) return;
36 // Transitions for 3state checkbox must be done manually, GTK's checkbox
37 // is 2state with additional "undetermined state" flag which isn't
38 // changed automatically:
41 GtkToggleButton
*toggle
= GTK_TOGGLE_BUTTON(widget
);
43 if (cb
->Is3rdStateAllowedForUser())
45 // The 3 states cycle like this when clicked:
46 // checked -> undetermined -> unchecked -> checked -> ...
47 bool active
= gtk_toggle_button_get_active(toggle
);
48 bool inconsistent
= gtk_toggle_button_get_inconsistent(toggle
);
50 cb
->GTKDisableEvents();
52 if (!active
&& !inconsistent
)
54 // checked -> undetermined
55 gtk_toggle_button_set_active(toggle
, true);
56 gtk_toggle_button_set_inconsistent(toggle
, true);
58 else if (!active
&& inconsistent
)
60 // undetermined -> unchecked
61 gtk_toggle_button_set_inconsistent(toggle
, false);
63 else if (active
&& !inconsistent
)
65 // unchecked -> checked
70 wxFAIL_MSG(wxT("3state wxCheckBox in unexpected state!"));
73 cb
->GTKEnableEvents();
77 // user's action unsets undetermined state:
78 gtk_toggle_button_set_inconsistent(toggle
, false);
82 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, cb
->GetId());
83 event
.SetInt(cb
->Get3StateValue());
84 event
.SetEventObject(cb
);
85 cb
->HandleWindowEvent(event
);
89 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
93 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
,wxControl
)
95 wxCheckBox::wxCheckBox()
99 bool wxCheckBox::Create(wxWindow
*parent
,
101 const wxString
&label
,
105 const wxValidator
& validator
,
106 const wxString
&name
)
108 WXValidateStyle( &style
);
109 if (!PreCreation( parent
, pos
, size
) ||
110 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
112 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
116 if ( style
& wxALIGN_RIGHT
)
118 // VZ: as I don't know a way to create a right aligned checkbox with
119 // GTK we will create a checkbox without label and a label at the
121 m_widgetCheckbox
= gtk_check_button_new();
123 m_widgetLabel
= gtk_label_new("");
124 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
126 m_widget
= gtk_hbox_new(FALSE
, 0);
127 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
128 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
130 gtk_widget_show( m_widgetLabel
);
131 gtk_widget_show( m_widgetCheckbox
);
135 m_widgetCheckbox
= gtk_check_button_new_with_label("");
136 m_widgetLabel
= GTK_BIN(m_widgetCheckbox
)->child
;
137 m_widget
= m_widgetCheckbox
;
139 g_object_ref(m_widget
);
142 g_signal_connect (m_widgetCheckbox
, "toggled",
143 G_CALLBACK (gtk_checkbox_toggled_callback
), this);
145 m_parent
->DoAddChild( this );
152 void wxCheckBox::GTKDisableEvents()
154 g_signal_handlers_block_by_func(m_widgetCheckbox
,
155 (gpointer
) gtk_checkbox_toggled_callback
, this);
158 void wxCheckBox::GTKEnableEvents()
160 g_signal_handlers_unblock_by_func(m_widgetCheckbox
,
161 (gpointer
) gtk_checkbox_toggled_callback
, this);
164 void wxCheckBox::SetValue( bool state
)
166 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
168 if (state
== GetValue())
173 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
178 bool wxCheckBox::GetValue() const
180 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, false, wxT("invalid checkbox") );
182 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox
));
185 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
187 SetValue(state
!= wxCHK_UNCHECKED
);
188 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox
),
189 state
== wxCHK_UNDETERMINED
);
192 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
194 if (gtk_toggle_button_get_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox
)))
196 return wxCHK_UNDETERMINED
;
200 return GetValue() ? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
204 void wxCheckBox::SetLabel( const wxString
& label
)
206 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
208 // save the label inside m_label in case user calls GetLabel() later
209 wxControl::SetLabel(label
);
211 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel
), label
);
214 bool wxCheckBox::Enable( bool enable
)
216 if (!base_type::Enable(enable
))
219 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
227 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
229 gtk_widget_modify_style(m_widgetCheckbox
, style
);
230 gtk_widget_modify_style(m_widgetLabel
, style
);
233 GdkWindow
*wxCheckBox::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
235 return GTK_BUTTON(m_widgetCheckbox
)->event_window
;
240 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
242 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new
);