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 if (cb
->m_blockEvent
) return;
38 // Transitions for 3state checkbox must be done manually, GTK's checkbox
39 // is 2state with additional "undetermined state" flag which isn't
40 // changed automatically:
43 GtkToggleButton
*toggle
= GTK_TOGGLE_BUTTON(widget
);
45 if (cb
->Is3rdStateAllowedForUser())
47 // The 3 states cycle like this when clicked:
48 // checked -> undetermined -> unchecked -> checked -> ...
49 bool active
= gtk_toggle_button_get_active(toggle
);
50 bool inconsistent
= gtk_toggle_button_get_inconsistent(toggle
);
52 cb
->m_blockEvent
= true;
54 if (!active
&& !inconsistent
)
56 // checked -> undetermined
57 gtk_toggle_button_set_active(toggle
, true);
58 gtk_toggle_button_set_inconsistent(toggle
, true);
60 else if (!active
&& inconsistent
)
62 // undetermined -> unchecked
63 gtk_toggle_button_set_inconsistent(toggle
, false);
65 else if (active
&& !inconsistent
)
67 // unchecked -> checked
72 wxFAIL_MSG(_T("3state wxCheckBox in unexpected state!"));
75 cb
->m_blockEvent
= false;
79 // user's action unsets undetermined state:
80 gtk_toggle_button_set_inconsistent(toggle
, false);
84 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, cb
->GetId());
85 event
.SetInt(cb
->Get3StateValue());
86 event
.SetEventObject(cb
);
87 cb
->HandleWindowEvent(event
);
91 //-----------------------------------------------------------------------------
93 //-----------------------------------------------------------------------------
95 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
,wxControl
)
97 wxCheckBox::wxCheckBox()
101 bool wxCheckBox::Create(wxWindow
*parent
,
103 const wxString
&label
,
107 const wxValidator
& validator
,
108 const wxString
&name
)
110 m_blockEvent
= false;
112 if (!PreCreation( parent
, pos
, size
) ||
113 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
115 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
119 wxASSERT_MSG( (style
& wxCHK_ALLOW_3RD_STATE_FOR_USER
) == 0 ||
120 (style
& wxCHK_3STATE
) != 0,
121 wxT("Using wxCHK_ALLOW_3RD_STATE_FOR_USER")
122 wxT(" style flag for a 2-state checkbox is useless") );
124 if ( style
& wxALIGN_RIGHT
)
126 // VZ: as I don't know a way to create a right aligned checkbox with
127 // GTK we will create a checkbox without label and a label at the
129 m_widgetCheckbox
= gtk_check_button_new();
131 m_widgetLabel
= gtk_label_new("");
132 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
134 m_widget
= gtk_hbox_new(FALSE
, 0);
135 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
136 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
138 gtk_widget_show( m_widgetLabel
);
139 gtk_widget_show( m_widgetCheckbox
);
143 m_widgetCheckbox
= gtk_check_button_new_with_label("");
144 m_widgetLabel
= GTK_BIN(m_widgetCheckbox
)->child
;
145 m_widget
= m_widgetCheckbox
;
149 g_signal_connect (m_widgetCheckbox
, "toggled",
150 G_CALLBACK (gtk_checkbox_toggled_callback
), this);
152 m_parent
->DoAddChild( this );
159 void wxCheckBox::SetValue( bool state
)
161 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
163 if (state
== GetValue())
168 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
170 m_blockEvent
= false;
173 bool wxCheckBox::GetValue() const
175 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, false, wxT("invalid checkbox") );
177 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox
));
180 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
182 SetValue(state
!= wxCHK_UNCHECKED
);
183 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox
),
184 state
== wxCHK_UNDETERMINED
);
187 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
189 if (gtk_toggle_button_get_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox
)))
191 return wxCHK_UNDETERMINED
;
195 return GetValue() ? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
199 void wxCheckBox::SetLabel( const wxString
& label
)
201 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
203 // save the label inside m_label in case user calls GetLabel() later
204 wxControl::SetLabel(label
);
206 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel
), label
);
209 bool wxCheckBox::Enable( bool enable
)
211 if ( !wxControl::Enable( enable
) )
214 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
219 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
221 gtk_widget_modify_style(m_widgetCheckbox
, style
);
222 gtk_widget_modify_style(m_widgetLabel
, style
);
225 GdkWindow
*wxCheckBox::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
227 return GTK_BUTTON(m_widgetCheckbox
)->event_window
;
230 wxSize
wxCheckBox::DoGetBestSize() const
232 return wxControl::DoGetBestSize();
237 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
239 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new
);