1 /////////////////////////////////////////////////////////////////////////////
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"
17 #include "wx/checkbox.h"
19 #include "wx/gtk/private.h"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 extern void wxapp_install_idle_handler();
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern bool g_blockEventsOnDrag
;
33 extern wxCursor g_globalCursor
;
34 extern wxWindowGTK
*g_delayedFocus
;
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
41 static void gtk_checkbox_toggled_callback(GtkWidget
*widget
, wxCheckBox
*cb
)
43 if (g_isIdle
) wxapp_install_idle_handler();
45 if (!cb
->m_hasVMT
) return;
47 if (g_blockEventsOnDrag
) return;
49 if (cb
->m_blockEvent
) return;
52 // Transitions for 3state checkbox must be done manually, GTK's checkbox
53 // is 2state with additional "undetermined state" flag which isn't
54 // changed automatically:
57 GtkToggleButton
*toggle
= GTK_TOGGLE_BUTTON(widget
);
59 if (cb
->Is3rdStateAllowedForUser())
61 // The 3 states cycle like this when clicked:
62 // checked -> undetermined -> unchecked -> checked -> ...
63 bool active
= gtk_toggle_button_get_active(toggle
);
64 bool inconsistent
= gtk_toggle_button_get_inconsistent(toggle
);
66 cb
->m_blockEvent
= true;
68 if (!active
&& !inconsistent
)
70 // checked -> undetermined
71 gtk_toggle_button_set_active(toggle
, true);
72 gtk_toggle_button_set_inconsistent(toggle
, true);
74 else if (!active
&& inconsistent
)
76 // undetermined -> unchecked
77 gtk_toggle_button_set_inconsistent(toggle
, false);
79 else if (active
&& !inconsistent
)
81 // unchecked -> checked
86 wxFAIL_MSG(_T("3state wxCheckBox in unexpected state!"));
89 cb
->m_blockEvent
= false;
93 // user's action unsets undetermined state:
94 gtk_toggle_button_set_inconsistent(toggle
, false);
99 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, cb
->GetId());
101 event
.SetInt(cb
->Get3StateValue());
103 event
.SetInt(cb
->GetValue());
105 event
.SetEventObject(cb
);
106 cb
->GetEventHandler()->ProcessEvent(event
);
110 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
114 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
,wxControl
)
116 wxCheckBox::wxCheckBox()
120 bool wxCheckBox::Create(wxWindow
*parent
,
122 const wxString
&label
,
126 const wxValidator
& validator
,
127 const wxString
&name
)
130 m_acceptsFocus
= TRUE
;
131 m_blockEvent
= FALSE
;
133 if (!PreCreation( parent
, pos
, size
) ||
134 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
136 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
140 wxASSERT_MSG( (style
& wxCHK_ALLOW_3RD_STATE_FOR_USER
) == 0 ||
141 (style
& wxCHK_3STATE
) != 0,
142 wxT("Using wxCHK_ALLOW_3RD_STATE_FOR_USER")
143 wxT(" style flag for a 2-state checkbox is useless") );
145 if ( style
& wxALIGN_RIGHT
)
147 // VZ: as I don't know a way to create a right aligned checkbox with
148 // GTK we will create a checkbox without label and a label at the
150 m_widgetCheckbox
= gtk_check_button_new();
152 m_widgetLabel
= gtk_label_new("");
153 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
155 m_widget
= gtk_hbox_new(FALSE
, 0);
156 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
157 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
159 gtk_widget_show( m_widgetLabel
);
160 gtk_widget_show( m_widgetCheckbox
);
164 m_widgetCheckbox
= gtk_check_button_new_with_label("");
165 m_widgetLabel
= BUTTON_CHILD( m_widgetCheckbox
);
166 m_widget
= m_widgetCheckbox
;
170 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox
),
172 GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback
),
175 m_parent
->DoAddChild( this );
182 void wxCheckBox::SetValue( bool state
)
184 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
186 if (state
== GetValue())
191 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
193 m_blockEvent
= FALSE
;
196 bool wxCheckBox::GetValue() const
198 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, FALSE
, wxT("invalid checkbox") );
201 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox
));
203 return GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->active
;
208 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
210 SetValue(state
!= wxCHK_UNCHECKED
);
211 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox
),
212 state
== wxCHK_UNDETERMINED
);
215 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
217 if (gtk_toggle_button_get_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox
)))
219 return wxCHK_UNDETERMINED
;
223 return GetValue() ? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
228 void wxCheckBox::SetLabel( const wxString
& label
)
230 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
232 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel
), label
);
235 bool wxCheckBox::Enable( bool enable
)
237 if ( !wxControl::Enable( enable
) )
240 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
245 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
247 gtk_widget_modify_style(m_widgetCheckbox
, style
);
248 gtk_widget_modify_style(m_widgetLabel
, style
);
251 bool wxCheckBox::IsOwnGtkWindow( GdkWindow
*window
)
253 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
256 void wxCheckBox::OnInternalIdle()
258 wxCursor cursor
= m_cursor
;
259 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
261 GdkWindow
*event_window
= TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox
);
262 if ( event_window
&& cursor
.Ok() )
264 /* I now set the cursor the anew in every OnInternalIdle call
265 as setting the cursor in a parent window also effects the
266 windows above so that checking for the current cursor is
269 gdk_window_set_cursor( event_window
, cursor
.GetCursor() );
272 if (g_delayedFocus
== this)
274 if (GTK_WIDGET_REALIZED(m_widget
))
276 gtk_widget_grab_focus( m_widget
);
277 g_delayedFocus
= NULL
;
281 if (wxUpdateUIEvent::CanUpdate(this))
282 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
285 wxSize
wxCheckBox::DoGetBestSize() const
287 return wxControl::DoGetBestSize();
292 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
294 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new
);