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/gtk1/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 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, cb
->GetId());
52 event
.SetInt(cb
->GetValue());
53 event
.SetEventObject(cb
);
54 cb
->GetEventHandler()->ProcessEvent(event
);
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
,wxControl
)
64 wxCheckBox::wxCheckBox()
68 bool wxCheckBox::Create(wxWindow
*parent
,
70 const wxString
&label
,
74 const wxValidator
& validator
,
75 const wxString
&name
)
78 m_acceptsFocus
= TRUE
;
81 if (!PreCreation( parent
, pos
, size
) ||
82 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
84 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
88 wxASSERT_MSG( (style
& wxCHK_ALLOW_3RD_STATE_FOR_USER
) == 0 ||
89 (style
& wxCHK_3STATE
) != 0,
90 wxT("Using wxCHK_ALLOW_3RD_STATE_FOR_USER")
91 wxT(" style flag for a 2-state checkbox is useless") );
93 if ( style
& wxALIGN_RIGHT
)
95 // VZ: as I don't know a way to create a right aligned checkbox with
96 // GTK we will create a checkbox without label and a label at the
98 m_widgetCheckbox
= gtk_check_button_new();
100 m_widgetLabel
= gtk_label_new("");
101 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
103 m_widget
= gtk_hbox_new(FALSE
, 0);
104 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
105 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
107 gtk_widget_show( m_widgetLabel
);
108 gtk_widget_show( m_widgetCheckbox
);
112 m_widgetCheckbox
= gtk_check_button_new_with_label("");
113 m_widgetLabel
= BUTTON_CHILD( m_widgetCheckbox
);
114 m_widget
= m_widgetCheckbox
;
118 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox
),
120 GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback
),
123 m_parent
->DoAddChild( this );
130 void wxCheckBox::SetValue( bool state
)
132 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
134 if (state
== GetValue())
139 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
141 m_blockEvent
= FALSE
;
144 bool wxCheckBox::GetValue() const
146 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, FALSE
, wxT("invalid checkbox") );
148 return GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->active
;
151 void wxCheckBox::SetLabel( const wxString
& label
)
153 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
155 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel
), label
);
158 bool wxCheckBox::Enable( bool enable
)
160 if ( !wxControl::Enable( enable
) )
163 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
168 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
170 gtk_widget_modify_style(m_widgetCheckbox
, style
);
171 gtk_widget_modify_style(m_widgetLabel
, style
);
174 bool wxCheckBox::IsOwnGtkWindow( GdkWindow
*window
)
176 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
179 void wxCheckBox::OnInternalIdle()
181 wxCursor cursor
= m_cursor
;
182 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
184 GdkWindow
*event_window
= TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox
);
185 if ( event_window
&& cursor
.Ok() )
187 /* I now set the cursor the anew in every OnInternalIdle call
188 as setting the cursor in a parent window also effects the
189 windows above so that checking for the current cursor is
192 gdk_window_set_cursor( event_window
, cursor
.GetCursor() );
195 if (g_delayedFocus
== this)
197 if (GTK_WIDGET_REALIZED(m_widget
))
199 gtk_widget_grab_focus( m_widget
);
200 g_delayedFocus
= NULL
;
204 if (wxUpdateUIEvent::CanUpdate(this))
205 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
208 wxSize
wxCheckBox::DoGetBestSize() const
210 return wxControl::DoGetBestSize();
215 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
217 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new
);