1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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/gtk1/private.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 extern void wxapp_install_idle_handler();
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern bool g_blockEventsOnDrag
;
31 extern wxCursor g_globalCursor
;
32 extern wxWindowGTK
*g_delayedFocus
;
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
39 static void gtk_checkbox_toggled_callback(GtkWidget
*WXUNUSED(widget
),
42 if (g_isIdle
) wxapp_install_idle_handler();
44 if (!cb
->m_hasVMT
) return;
46 if (g_blockEventsOnDrag
) return;
48 if (cb
->m_blockEvent
) return;
50 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, cb
->GetId());
51 event
.SetInt(cb
->GetValue());
52 event
.SetEventObject(cb
);
53 cb
->HandleWindowEvent(event
);
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
,wxControl
)
63 wxCheckBox::wxCheckBox()
67 bool wxCheckBox::Create(wxWindow
*parent
,
69 const wxString
&label
,
73 const wxValidator
& validator
,
74 const wxString
&name
)
77 m_acceptsFocus
= true;
80 if (!PreCreation( parent
, pos
, size
) ||
81 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
83 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
87 wxASSERT_MSG( (style
& wxCHK_ALLOW_3RD_STATE_FOR_USER
) == 0 ||
88 (style
& wxCHK_3STATE
) != 0,
89 wxT("Using wxCHK_ALLOW_3RD_STATE_FOR_USER")
90 wxT(" style flag for a 2-state checkbox is useless") );
92 if ( style
& wxALIGN_RIGHT
)
94 // VZ: as I don't know a way to create a right aligned checkbox with
95 // GTK we will create a checkbox without label and a label at the
97 m_widgetCheckbox
= gtk_check_button_new();
99 m_widgetLabel
= gtk_label_new("");
100 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
102 m_widget
= gtk_hbox_new(FALSE
, 0);
103 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
104 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
106 gtk_widget_show( m_widgetLabel
);
107 gtk_widget_show( m_widgetCheckbox
);
111 m_widgetCheckbox
= gtk_check_button_new_with_label("");
112 m_widgetLabel
= BUTTON_CHILD( m_widgetCheckbox
);
113 m_widget
= m_widgetCheckbox
;
117 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox
),
119 GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback
),
122 m_parent
->DoAddChild( this );
129 void wxCheckBox::SetValue( bool state
)
131 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
133 if (state
== GetValue())
138 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
140 m_blockEvent
= false;
143 bool wxCheckBox::GetValue() const
145 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, false, wxT("invalid checkbox") );
147 return GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->active
;
150 void wxCheckBox::SetLabel( const wxString
& label
)
152 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
154 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel
), label
);
157 bool wxCheckBox::Enable( bool enable
)
159 if ( !wxControl::Enable( enable
) )
162 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
167 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
169 gtk_widget_modify_style(m_widgetCheckbox
, style
);
170 gtk_widget_modify_style(m_widgetLabel
, style
);
173 bool wxCheckBox::IsOwnGtkWindow( GdkWindow
*window
)
175 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
178 void wxCheckBox::OnInternalIdle()
180 wxCursor cursor
= m_cursor
;
181 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
183 GdkWindow
*event_window
= TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox
);
184 if ( event_window
&& cursor
.Ok() )
186 /* I now set the cursor the anew in every OnInternalIdle call
187 as setting the cursor in a parent window also effects the
188 windows above so that checking for the current cursor is
191 gdk_window_set_cursor( event_window
, cursor
.GetCursor() );
194 if (g_delayedFocus
== this)
196 if (GTK_WIDGET_REALIZED(m_widget
))
198 gtk_widget_grab_focus( m_widget
);
199 g_delayedFocus
= NULL
;
203 if (wxUpdateUIEvent::CanUpdate(this))
204 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
207 wxSize
wxCheckBox::DoGetBestSize() const
209 return wxControl::DoGetBestSize();
214 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
216 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new
);