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
;
24 extern wxCursor g_globalCursor
;
25 extern wxWindowGTK
*g_delayedFocus
;
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
32 static void gtk_checkbox_toggled_callback(GtkWidget
*widget
, wxCheckBox
*cb
)
34 if (g_isIdle
) wxapp_install_idle_handler();
36 if (!cb
->m_hasVMT
) return;
38 if (g_blockEventsOnDrag
) return;
40 if (cb
->m_blockEvent
) return;
42 // Transitions for 3state checkbox must be done manually, GTK's checkbox
43 // is 2state with additional "undetermined state" flag which isn't
44 // changed automatically:
47 GtkToggleButton
*toggle
= GTK_TOGGLE_BUTTON(widget
);
49 if (cb
->Is3rdStateAllowedForUser())
51 // The 3 states cycle like this when clicked:
52 // checked -> undetermined -> unchecked -> checked -> ...
53 bool active
= gtk_toggle_button_get_active(toggle
);
54 bool inconsistent
= gtk_toggle_button_get_inconsistent(toggle
);
56 cb
->m_blockEvent
= true;
58 if (!active
&& !inconsistent
)
60 // checked -> undetermined
61 gtk_toggle_button_set_active(toggle
, true);
62 gtk_toggle_button_set_inconsistent(toggle
, true);
64 else if (!active
&& inconsistent
)
66 // undetermined -> unchecked
67 gtk_toggle_button_set_inconsistent(toggle
, false);
69 else if (active
&& !inconsistent
)
71 // unchecked -> checked
76 wxFAIL_MSG(_T("3state wxCheckBox in unexpected state!"));
79 cb
->m_blockEvent
= false;
83 // user's action unsets undetermined state:
84 gtk_toggle_button_set_inconsistent(toggle
, false);
88 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, cb
->GetId());
89 event
.SetInt(cb
->Get3StateValue());
90 event
.SetEventObject(cb
);
91 cb
->GetEventHandler()->ProcessEvent(event
);
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
99 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
,wxControl
)
101 wxCheckBox::wxCheckBox()
105 bool wxCheckBox::Create(wxWindow
*parent
,
107 const wxString
&label
,
111 const wxValidator
& validator
,
112 const wxString
&name
)
115 m_acceptsFocus
= true;
116 m_blockEvent
= false;
118 if (!PreCreation( parent
, pos
, size
) ||
119 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
121 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
125 wxASSERT_MSG( (style
& wxCHK_ALLOW_3RD_STATE_FOR_USER
) == 0 ||
126 (style
& wxCHK_3STATE
) != 0,
127 wxT("Using wxCHK_ALLOW_3RD_STATE_FOR_USER")
128 wxT(" style flag for a 2-state checkbox is useless") );
130 if ( style
& wxALIGN_RIGHT
)
132 // VZ: as I don't know a way to create a right aligned checkbox with
133 // GTK we will create a checkbox without label and a label at the
135 m_widgetCheckbox
= gtk_check_button_new();
137 m_widgetLabel
= gtk_label_new("");
138 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
140 m_widget
= gtk_hbox_new(FALSE
, 0);
141 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
142 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
144 gtk_widget_show( m_widgetLabel
);
145 gtk_widget_show( m_widgetCheckbox
);
149 m_widgetCheckbox
= gtk_check_button_new_with_label("");
150 m_widgetLabel
= GTK_BIN(m_widgetCheckbox
)->child
;
151 m_widget
= m_widgetCheckbox
;
155 g_signal_connect (m_widgetCheckbox
, "toggled",
156 G_CALLBACK (gtk_checkbox_toggled_callback
), this);
158 m_parent
->DoAddChild( this );
165 void wxCheckBox::SetValue( bool state
)
167 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
169 if (state
== GetValue())
174 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
176 m_blockEvent
= false;
179 bool wxCheckBox::GetValue() const
181 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, false, wxT("invalid checkbox") );
183 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox
));
186 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
188 SetValue(state
!= wxCHK_UNCHECKED
);
189 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox
),
190 state
== wxCHK_UNDETERMINED
);
193 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
195 if (gtk_toggle_button_get_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox
)))
197 return wxCHK_UNDETERMINED
;
201 return GetValue() ? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
205 void wxCheckBox::SetLabel( const wxString
& label
)
207 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
209 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel
), label
);
212 bool wxCheckBox::Enable( bool enable
)
214 if ( !wxControl::Enable( enable
) )
217 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
222 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
224 gtk_widget_modify_style(m_widgetCheckbox
, style
);
225 gtk_widget_modify_style(m_widgetLabel
, style
);
228 bool wxCheckBox::IsOwnGtkWindow( GdkWindow
*window
)
230 return window
== GTK_BUTTON(m_widget
)->event_window
;
233 void wxCheckBox::OnInternalIdle()
235 // Check if we have to show window now
236 if (GtkShowFromOnIdle()) return;
238 wxCursor cursor
= m_cursor
;
239 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
241 GdkWindow
*event_window
= GTK_BUTTON(m_widgetCheckbox
)->event_window
;
242 if ( event_window
&& cursor
.Ok() )
244 /* I now set the cursor the anew in every OnInternalIdle call
245 as setting the cursor in a parent window also effects the
246 windows above so that checking for the current cursor is
249 gdk_window_set_cursor( event_window
, cursor
.GetCursor() );
252 if (g_delayedFocus
== this)
254 if (GTK_WIDGET_REALIZED(m_widget
))
256 gtk_widget_grab_focus( m_widget
);
257 g_delayedFocus
= NULL
;
261 if (wxUpdateUIEvent::CanUpdate(this))
262 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
265 wxSize
wxCheckBox::DoGetBestSize() const
267 return wxControl::DoGetBestSize();
272 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
274 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new
);