1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "checkbox.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 #include "wx/checkbox.h"
24 #include "wx/gtk/private.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern void wxapp_install_idle_handler();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag
;
38 extern wxCursor g_globalCursor
;
39 extern wxWindowGTK
*g_delayedFocus
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 static void gtk_checkbox_toggled_callback(GtkWidget
*widget
, wxCheckBox
*cb
)
47 if (g_isIdle
) wxapp_install_idle_handler();
49 if (!cb
->m_hasVMT
) return;
51 if (g_blockEventsOnDrag
) return;
53 if (cb
->m_blockEvent
) return;
56 // Transitions for 3state checkbox must be done manually, GTK's checkbox
57 // is 2state with additional "undetermined state" flag which isn't
58 // changed automatically:
61 GtkToggleButton
*toggle
= GTK_TOGGLE_BUTTON(widget
);
63 if (cb
->Is3rdStateAllowedForUser())
65 // The 3 states cycle like this when clicked:
66 // checked -> undetermined -> unchecked -> checked -> ...
67 bool active
= gtk_toggle_button_get_active(toggle
);
68 bool inconsistent
= gtk_toggle_button_get_inconsistent(toggle
);
70 cb
->m_blockEvent
= true;
72 if (!active
&& !inconsistent
)
74 // checked -> undetermined
75 gtk_toggle_button_set_active(toggle
, true);
76 gtk_toggle_button_set_inconsistent(toggle
, true);
78 else if (!active
&& inconsistent
)
80 // undetermined -> unchecked
81 gtk_toggle_button_set_inconsistent(toggle
, false);
83 else if (active
&& !inconsistent
)
85 // unchecked -> checked
90 wxFAIL_MSG(_T("3state wxCheckBox in unexpected state!"));
93 cb
->m_blockEvent
= false;
97 // user's action unsets undetermined state:
98 gtk_toggle_button_set_inconsistent(toggle
, false);
103 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, cb
->GetId());
105 event
.SetInt(cb
->Get3StateValue());
107 event
.SetInt(cb
->GetValue());
109 event
.SetEventObject(cb
);
110 cb
->GetEventHandler()->ProcessEvent(event
);
113 //-----------------------------------------------------------------------------
115 //-----------------------------------------------------------------------------
117 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
,wxControl
)
119 wxCheckBox::wxCheckBox()
123 bool wxCheckBox::Create(wxWindow
*parent
,
125 const wxString
&label
,
129 const wxValidator
& validator
,
130 const wxString
&name
)
133 m_acceptsFocus
= TRUE
;
134 m_blockEvent
= FALSE
;
136 if (!PreCreation( parent
, pos
, size
) ||
137 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
139 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
143 wxASSERT_MSG( (style
& wxCHK_ALLOW_3RD_STATE_FOR_USER
) == 0 ||
144 (style
& wxCHK_3STATE
) != 0,
145 wxT("Using wxCHK_ALLOW_3RD_STATE_FOR_USER")
146 wxT(" style flag for a 2-state checkbox is useless") );
148 if ( style
& wxALIGN_RIGHT
)
150 // VZ: as I don't know a way to create a right aligned checkbox with
151 // GTK we will create a checkbox without label and a label at the
153 m_widgetCheckbox
= gtk_check_button_new();
155 m_widgetLabel
= gtk_label_new("");
156 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
158 m_widget
= gtk_hbox_new(FALSE
, 0);
159 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
160 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
162 gtk_widget_show( m_widgetLabel
);
163 gtk_widget_show( m_widgetCheckbox
);
167 m_widgetCheckbox
= gtk_check_button_new_with_label("");
168 m_widgetLabel
= BUTTON_CHILD( m_widgetCheckbox
);
169 m_widget
= m_widgetCheckbox
;
173 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox
),
175 GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback
),
178 m_parent
->DoAddChild( this );
185 void wxCheckBox::SetValue( bool state
)
187 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
189 if (state
== GetValue())
194 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
196 m_blockEvent
= FALSE
;
199 bool wxCheckBox::GetValue() const
201 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, FALSE
, wxT("invalid checkbox") );
204 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox
));
206 return GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->active
;
211 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
213 SetValue(state
!= wxCHK_UNCHECKED
);
214 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox
),
215 state
== wxCHK_UNDETERMINED
);
218 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
220 if (gtk_toggle_button_get_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox
)))
222 return wxCHK_UNDETERMINED
;
226 return GetValue() ? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
231 void wxCheckBox::SetLabel( const wxString
& label
)
233 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
235 wxControl::SetLabel( label
);
238 wxString label2
= PrepareLabelMnemonics( label
);
239 gtk_label_set_text_with_mnemonic( GTK_LABEL(m_widgetLabel
), wxGTK_CONV( label2
) );
241 gtk_label_set( GTK_LABEL(m_widgetLabel
), wxGTK_CONV( GetLabel() ) );
245 bool wxCheckBox::Enable( bool enable
)
247 if ( !wxControl::Enable( enable
) )
250 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
255 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
257 gtk_widget_modify_style(m_widgetCheckbox
, style
);
258 gtk_widget_modify_style(m_widgetLabel
, style
);
261 bool wxCheckBox::IsOwnGtkWindow( GdkWindow
*window
)
263 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
266 void wxCheckBox::OnInternalIdle()
268 wxCursor cursor
= m_cursor
;
269 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
271 GdkWindow
*event_window
= TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox
);
272 if ( event_window
&& cursor
.Ok() )
274 /* I now set the cursor the anew in every OnInternalIdle call
275 as setting the cursor in a parent window also effects the
276 windows above so that checking for the current cursor is
279 gdk_window_set_cursor( event_window
, cursor
.GetCursor() );
282 if (g_delayedFocus
== this)
284 if (GTK_WIDGET_REALIZED(m_widget
))
286 gtk_widget_grab_focus( m_widget
);
287 g_delayedFocus
= NULL
;
291 if (wxUpdateUIEvent::CanUpdate(this))
292 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
295 wxSize
wxCheckBox::DoGetBestSize() const
297 return wxControl::DoGetBestSize();
302 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
304 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new
);