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 //-----------------------------------------------------------------------------
46 static void gtk_checkbox_toggled_callback(GtkWidget
*widget
, wxCheckBox
*cb
)
48 if (g_isIdle
) wxapp_install_idle_handler();
50 if (!cb
->m_hasVMT
) return;
52 if (g_blockEventsOnDrag
) return;
54 if (cb
->m_blockEvent
) return;
57 // Transitions for 3state checkbox must be done manually, GTK's checkbox
58 // is 2state with additional "undetermined state" flag which isn't
59 // changed automatically:
62 GtkToggleButton
*toggle
= GTK_TOGGLE_BUTTON(widget
);
64 if (cb
->Is3rdStateAllowedForUser())
66 // The 3 states cycle like this when clicked:
67 // checked -> undetermined -> unchecked -> checked -> ...
68 bool active
= gtk_toggle_button_get_active(toggle
);
69 bool inconsistent
= gtk_toggle_button_get_inconsistent(toggle
);
71 cb
->m_blockEvent
= true;
73 if (!active
&& !inconsistent
)
75 // checked -> undetermined
76 gtk_toggle_button_set_active(toggle
, true);
77 gtk_toggle_button_set_inconsistent(toggle
, true);
79 else if (!active
&& inconsistent
)
81 // undetermined -> unchecked
82 gtk_toggle_button_set_inconsistent(toggle
, false);
84 else if (active
&& !inconsistent
)
86 // unchecked -> checked
91 wxFAIL_MSG(_T("3state wxCheckBox in unexpected state!"));
94 cb
->m_blockEvent
= false;
98 // user's action unsets undetermined state:
99 gtk_toggle_button_set_inconsistent(toggle
, false);
104 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, cb
->GetId());
106 event
.SetInt(cb
->Get3StateValue());
108 event
.SetInt(cb
->GetValue());
110 event
.SetEventObject(cb
);
111 cb
->GetEventHandler()->ProcessEvent(event
);
115 //-----------------------------------------------------------------------------
117 //-----------------------------------------------------------------------------
119 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
,wxControl
)
121 wxCheckBox::wxCheckBox()
125 bool wxCheckBox::Create(wxWindow
*parent
,
127 const wxString
&label
,
131 const wxValidator
& validator
,
132 const wxString
&name
)
135 m_acceptsFocus
= TRUE
;
136 m_blockEvent
= FALSE
;
138 if (!PreCreation( parent
, pos
, size
) ||
139 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
141 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
145 wxASSERT_MSG( (style
& wxCHK_ALLOW_3RD_STATE_FOR_USER
) == 0 ||
146 (style
& wxCHK_3STATE
) != 0,
147 wxT("Using wxCHK_ALLOW_3RD_STATE_FOR_USER")
148 wxT(" style flag for a 2-state checkbox is useless") );
150 if ( style
& wxALIGN_RIGHT
)
152 // VZ: as I don't know a way to create a right aligned checkbox with
153 // GTK we will create a checkbox without label and a label at the
155 m_widgetCheckbox
= gtk_check_button_new();
157 m_widgetLabel
= gtk_label_new("");
158 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
160 m_widget
= gtk_hbox_new(FALSE
, 0);
161 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
162 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
164 gtk_widget_show( m_widgetLabel
);
165 gtk_widget_show( m_widgetCheckbox
);
169 m_widgetCheckbox
= gtk_check_button_new_with_label("");
170 m_widgetLabel
= BUTTON_CHILD( m_widgetCheckbox
);
171 m_widget
= m_widgetCheckbox
;
175 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox
),
177 GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback
),
180 m_parent
->DoAddChild( this );
187 void wxCheckBox::SetValue( bool state
)
189 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
191 if (state
== GetValue())
196 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
198 m_blockEvent
= FALSE
;
201 bool wxCheckBox::GetValue() const
203 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, FALSE
, wxT("invalid checkbox") );
206 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox
));
208 return GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->active
;
213 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state
)
215 SetValue(state
!= wxCHK_UNCHECKED
);
216 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox
),
217 state
== wxCHK_UNDETERMINED
);
220 wxCheckBoxState
wxCheckBox::DoGet3StateValue() const
222 if (gtk_toggle_button_get_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox
)))
224 return wxCHK_UNDETERMINED
;
228 return GetValue() ? wxCHK_CHECKED
: wxCHK_UNCHECKED
;
233 void wxCheckBox::SetLabel( const wxString
& label
)
235 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
237 wxControl::SetLabel( label
);
240 wxString label2
= PrepareLabelMnemonics( label
);
241 gtk_label_set_text_with_mnemonic( GTK_LABEL(m_widgetLabel
), wxGTK_CONV( label2
) );
243 gtk_label_set( GTK_LABEL(m_widgetLabel
), wxGTK_CONV( GetLabel() ) );
247 bool wxCheckBox::Enable( bool enable
)
249 if ( !wxControl::Enable( enable
) )
252 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
257 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
259 gtk_widget_modify_style(m_widgetCheckbox
, style
);
260 gtk_widget_modify_style(m_widgetLabel
, style
);
263 bool wxCheckBox::IsOwnGtkWindow( GdkWindow
*window
)
265 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
268 void wxCheckBox::OnInternalIdle()
270 wxCursor cursor
= m_cursor
;
271 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
273 GdkWindow
*event_window
= TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox
);
274 if ( event_window
&& cursor
.Ok() )
276 /* I now set the cursor the anew in every OnInternalIdle call
277 as setting the cursor in a parent window also effects the
278 windows above so that checking for the current cursor is
281 gdk_window_set_cursor( event_window
, cursor
.GetCursor() );
284 if (g_delayedFocus
== this)
286 if (GTK_WIDGET_REALIZED(m_widget
))
288 gtk_widget_grab_focus( m_widget
);
289 g_delayedFocus
= NULL
;
293 if (wxUpdateUIEvent::CanUpdate(this))
294 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
297 wxSize
wxCheckBox::DoGetBestSize() const
299 return wxControl::DoGetBestSize();
304 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
306 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new
);