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;
51 // Transitions for 3state checkbox must be done manually, GTK's checkbox
52 // is 2state with additional "undetermined state" flag which isn't
53 // changed automatically:
56 GtkToggleButton
*toggle
= GTK_TOGGLE_BUTTON(widget
);
58 if (cb
->Is3rdStateAllowedForUser())
60 // The 3 states cycle like this when clicked:
61 // checked -> undetermined -> unchecked -> checked -> ...
62 bool active
= gtk_toggle_button_get_active(toggle
);
63 bool inconsistent
= gtk_toggle_button_get_inconsistent(toggle
);
65 cb
->m_blockEvent
= true;
67 if (!active
&& !inconsistent
)
69 // checked -> undetermined
70 gtk_toggle_button_set_active(toggle
, true);
71 gtk_toggle_button_set_inconsistent(toggle
, true);
73 else if (!active
&& inconsistent
)
75 // undetermined -> unchecked
76 gtk_toggle_button_set_inconsistent(toggle
, false);
78 else if (active
&& !inconsistent
)
80 // unchecked -> checked
85 wxFAIL_MSG(_T("3state wxCheckBox in unexpected state!"));
88 cb
->m_blockEvent
= false;
92 // user's action unsets undetermined state:
93 gtk_toggle_button_set_inconsistent(toggle
, false);
97 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, cb
->GetId());
98 event
.SetInt(cb
->Get3StateValue());
99 event
.SetEventObject(cb
);
100 cb
->GetEventHandler()->ProcessEvent(event
);
104 //-----------------------------------------------------------------------------
106 //-----------------------------------------------------------------------------
108 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
,wxControl
)
110 wxCheckBox::wxCheckBox()
114 bool wxCheckBox::Create(wxWindow
*parent
,
116 const wxString
&label
,
120 const wxValidator
& validator
,
121 const wxString
&name
)
124 m_acceptsFocus
= TRUE
;
125 m_blockEvent
= FALSE
;
127 if (!PreCreation( parent
, pos
, size
) ||
128 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
130 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
134 wxASSERT_MSG( (style
& wxCHK_ALLOW_3RD_STATE_FOR_USER
) == 0 ||
135 (style
& wxCHK_3STATE
) != 0,
136 wxT("Using wxCHK_ALLOW_3RD_STATE_FOR_USER")
137 wxT(" style flag for a 2-state checkbox is useless") );
139 if ( style
& wxALIGN_RIGHT
)
141 // VZ: as I don't know a way to create a right aligned checkbox with
142 // GTK we will create a checkbox without label and a label at the
144 m_widgetCheckbox
= gtk_check_button_new();
146 m_widgetLabel
= gtk_label_new("");
147 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
149 m_widget
= gtk_hbox_new(FALSE
, 0);
150 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
151 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
153 gtk_widget_show( m_widgetLabel
);
154 gtk_widget_show( m_widgetCheckbox
);
158 m_widgetCheckbox
= gtk_check_button_new_with_label("");
159 m_widgetLabel
= GTK_BIN(m_widgetCheckbox
)->child
;
160 m_widget
= m_widgetCheckbox
;
164 g_signal_connect (m_widgetCheckbox
, "toggled",
165 G_CALLBACK (gtk_checkbox_toggled_callback
), this);
167 m_parent
->DoAddChild( this );
174 void wxCheckBox::SetValue( bool state
)
176 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
178 if (state
== GetValue())
183 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
185 m_blockEvent
= FALSE
;
188 bool wxCheckBox::GetValue() const
190 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, FALSE
, wxT("invalid checkbox") );
192 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox
));
195 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
197 SetValue(state
!= wxCHK_UNCHECKED
);
198 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox
),
199 state
== wxCHK_UNDETERMINED
);
202 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
204 if (gtk_toggle_button_get_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox
)))
206 return wxCHK_UNDETERMINED
;
210 return GetValue() ? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
214 void wxCheckBox::SetLabel( const wxString
& label
)
216 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
218 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel
), label
);
221 bool wxCheckBox::Enable( bool enable
)
223 if ( !wxControl::Enable( enable
) )
226 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
231 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
233 gtk_widget_modify_style(m_widgetCheckbox
, style
);
234 gtk_widget_modify_style(m_widgetLabel
, style
);
237 bool wxCheckBox::IsOwnGtkWindow( GdkWindow
*window
)
239 return window
== GTK_BUTTON(m_widget
)->event_window
;
242 void wxCheckBox::OnInternalIdle()
244 wxCursor cursor
= m_cursor
;
245 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
247 GdkWindow
*event_window
= GTK_BUTTON(m_widgetCheckbox
)->event_window
;
248 if ( event_window
&& cursor
.Ok() )
250 /* I now set the cursor the anew in every OnInternalIdle call
251 as setting the cursor in a parent window also effects the
252 windows above so that checking for the current cursor is
255 gdk_window_set_cursor( event_window
, cursor
.GetCursor() );
258 if (g_delayedFocus
== this)
260 if (GTK_WIDGET_REALIZED(m_widget
))
262 gtk_widget_grab_focus( m_widget
);
263 g_delayedFocus
= NULL
;
267 if (wxUpdateUIEvent::CanUpdate(this))
268 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
271 wxSize
wxCheckBox::DoGetBestSize() const
273 return wxControl::DoGetBestSize();
278 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
280 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new
);