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"
17 #include "wx/gtk/private.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 extern bool g_blockEventsOnDrag
;
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
30 static void gtk_checkbox_toggled_callback(GtkWidget
*widget
, wxCheckBox
*cb
)
32 if (g_isIdle
) wxapp_install_idle_handler();
34 if (!cb
->m_hasVMT
) return;
36 if (g_blockEventsOnDrag
) return;
38 if (cb
->m_blockEvent
) return;
40 // Transitions for 3state checkbox must be done manually, GTK's checkbox
41 // is 2state with additional "undetermined state" flag which isn't
42 // changed automatically:
45 GtkToggleButton
*toggle
= GTK_TOGGLE_BUTTON(widget
);
47 if (cb
->Is3rdStateAllowedForUser())
49 // The 3 states cycle like this when clicked:
50 // checked -> undetermined -> unchecked -> checked -> ...
51 bool active
= gtk_toggle_button_get_active(toggle
);
52 bool inconsistent
= gtk_toggle_button_get_inconsistent(toggle
);
54 cb
->m_blockEvent
= true;
56 if (!active
&& !inconsistent
)
58 // checked -> undetermined
59 gtk_toggle_button_set_active(toggle
, true);
60 gtk_toggle_button_set_inconsistent(toggle
, true);
62 else if (!active
&& inconsistent
)
64 // undetermined -> unchecked
65 gtk_toggle_button_set_inconsistent(toggle
, false);
67 else if (active
&& !inconsistent
)
69 // unchecked -> checked
74 wxFAIL_MSG(_T("3state wxCheckBox in unexpected state!"));
77 cb
->m_blockEvent
= false;
81 // user's action unsets undetermined state:
82 gtk_toggle_button_set_inconsistent(toggle
, false);
86 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, cb
->GetId());
87 event
.SetInt(cb
->Get3StateValue());
88 event
.SetEventObject(cb
);
89 cb
->GetEventHandler()->ProcessEvent(event
);
93 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
97 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
,wxControl
)
99 wxCheckBox::wxCheckBox()
103 bool wxCheckBox::Create(wxWindow
*parent
,
105 const wxString
&label
,
109 const wxValidator
& validator
,
110 const wxString
&name
)
113 m_acceptsFocus
= true;
114 m_blockEvent
= false;
116 if (!PreCreation( parent
, pos
, size
) ||
117 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
119 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
123 wxASSERT_MSG( (style
& wxCHK_ALLOW_3RD_STATE_FOR_USER
) == 0 ||
124 (style
& wxCHK_3STATE
) != 0,
125 wxT("Using wxCHK_ALLOW_3RD_STATE_FOR_USER")
126 wxT(" style flag for a 2-state checkbox is useless") );
128 if ( style
& wxALIGN_RIGHT
)
130 // VZ: as I don't know a way to create a right aligned checkbox with
131 // GTK we will create a checkbox without label and a label at the
133 m_widgetCheckbox
= gtk_check_button_new();
135 m_widgetLabel
= gtk_label_new("");
136 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
138 m_widget
= gtk_hbox_new(FALSE
, 0);
139 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
140 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
142 gtk_widget_show( m_widgetLabel
);
143 gtk_widget_show( m_widgetCheckbox
);
147 m_widgetCheckbox
= gtk_check_button_new_with_label("");
148 m_widgetLabel
= GTK_BIN(m_widgetCheckbox
)->child
;
149 m_widget
= m_widgetCheckbox
;
153 g_signal_connect (m_widgetCheckbox
, "toggled",
154 G_CALLBACK (gtk_checkbox_toggled_callback
), this);
156 m_parent
->DoAddChild( 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
);
174 m_blockEvent
= false;
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
));
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 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel
), label
);
210 bool wxCheckBox::Enable( bool enable
)
212 if ( !wxControl::Enable( enable
) )
215 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
220 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
222 gtk_widget_modify_style(m_widgetCheckbox
, style
);
223 gtk_widget_modify_style(m_widgetLabel
, style
);
226 GdkWindow
*wxCheckBox::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
228 return GTK_BUTTON(m_widgetCheckbox
)->event_window
;
231 wxSize
wxCheckBox::DoGetBestSize() const
233 return wxControl::DoGetBestSize();
238 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
240 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new
);