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"
18 #include "wx/gtk/private/gtk2-compat.h"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 extern bool g_blockEventsOnDrag
;
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
31 static void gtk_checkbox_toggled_callback(GtkWidget
*widget
, wxCheckBox
*cb
)
33 if (!cb
->m_hasVMT
) return;
35 if (g_blockEventsOnDrag
) return;
37 // Transitions for 3state checkbox must be done manually, GTK's checkbox
38 // is 2state with additional "undetermined state" flag which isn't
39 // changed automatically:
42 GtkToggleButton
*toggle
= GTK_TOGGLE_BUTTON(widget
);
44 if (cb
->Is3rdStateAllowedForUser())
46 // The 3 states cycle like this when clicked:
47 // checked -> undetermined -> unchecked -> checked -> ...
48 bool active
= gtk_toggle_button_get_active(toggle
) != 0;
49 bool inconsistent
= gtk_toggle_button_get_inconsistent(toggle
) != 0;
51 cb
->GTKDisableEvents();
53 if (!active
&& !inconsistent
)
55 // checked -> undetermined
56 gtk_toggle_button_set_active(toggle
, true);
57 gtk_toggle_button_set_inconsistent(toggle
, true);
59 else if (!active
&& inconsistent
)
61 // undetermined -> unchecked
62 gtk_toggle_button_set_inconsistent(toggle
, false);
64 else if (active
&& !inconsistent
)
66 // unchecked -> checked
71 wxFAIL_MSG(wxT("3state wxCheckBox in unexpected state!"));
74 cb
->GTKEnableEvents();
78 // user's action unsets undetermined state:
79 gtk_toggle_button_set_inconsistent(toggle
, false);
83 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, cb
->GetId());
84 event
.SetInt(cb
->Get3StateValue());
85 event
.SetEventObject(cb
);
86 cb
->HandleWindowEvent(event
);
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
94 wxCheckBox::wxCheckBox()
98 bool wxCheckBox::Create(wxWindow
*parent
,
100 const wxString
&label
,
104 const wxValidator
& validator
,
105 const wxString
&name
)
107 WXValidateStyle( &style
);
108 if (!PreCreation( parent
, pos
, size
) ||
109 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
111 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
115 if ( style
& wxALIGN_RIGHT
)
117 // VZ: as I don't know a way to create a right aligned checkbox with
118 // GTK we will create a checkbox without label and a label at the
120 m_widgetCheckbox
= gtk_check_button_new();
122 m_widgetLabel
= gtk_label_new("");
123 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
125 m_widget
= gtk_hbox_new(FALSE
, 0);
126 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
127 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
129 gtk_widget_show( m_widgetLabel
);
130 gtk_widget_show( m_widgetCheckbox
);
134 m_widgetCheckbox
= gtk_check_button_new_with_label("");
135 m_widgetLabel
= gtk_bin_get_child(GTK_BIN(m_widgetCheckbox
));
136 m_widget
= m_widgetCheckbox
;
138 g_object_ref(m_widget
);
141 g_signal_connect (m_widgetCheckbox
, "toggled",
142 G_CALLBACK (gtk_checkbox_toggled_callback
), this);
144 m_parent
->DoAddChild( this );
151 void wxCheckBox::GTKDisableEvents()
153 g_signal_handlers_block_by_func(m_widgetCheckbox
,
154 (gpointer
) gtk_checkbox_toggled_callback
, this);
157 void wxCheckBox::GTKEnableEvents()
159 g_signal_handlers_unblock_by_func(m_widgetCheckbox
,
160 (gpointer
) gtk_checkbox_toggled_callback
, this);
163 void wxCheckBox::SetValue( bool state
)
165 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
167 if (state
== GetValue())
172 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
177 bool wxCheckBox::GetValue() const
179 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, false, wxT("invalid checkbox") );
181 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox
)) != 0;
184 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
186 SetValue(state
!= wxCHK_UNCHECKED
);
187 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox
),
188 state
== wxCHK_UNDETERMINED
);
191 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
193 if (gtk_toggle_button_get_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox
)))
195 return wxCHK_UNDETERMINED
;
199 return GetValue() ? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
203 void wxCheckBox::SetLabel( const wxString
& label
)
205 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
207 // save the label inside m_label in case user calls GetLabel() later
208 wxControl::SetLabel(label
);
210 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel
), label
);
213 bool wxCheckBox::Enable( bool enable
)
215 if (!base_type::Enable(enable
))
218 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
226 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
228 GTKApplyStyle(m_widgetCheckbox
, style
);
229 GTKApplyStyle(m_widgetLabel
, style
);
232 GdkWindow
*wxCheckBox::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
234 return gtk_button_get_event_window(GTK_BUTTON(m_widgetCheckbox
));
239 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
241 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new
);