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 WXValidateStyle(&style
);
81 if (!PreCreation( parent
, pos
, size
) ||
82 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
84 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
88 if ( style
& wxALIGN_RIGHT
)
90 // VZ: as I don't know a way to create a right aligned checkbox with
91 // GTK we will create a checkbox without label and a label at the
93 m_widgetCheckbox
= gtk_check_button_new();
95 m_widgetLabel
= gtk_label_new("");
96 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
98 m_widget
= gtk_hbox_new(FALSE
, 0);
99 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
100 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
102 gtk_widget_show( m_widgetLabel
);
103 gtk_widget_show( m_widgetCheckbox
);
107 m_widgetCheckbox
= gtk_check_button_new_with_label("");
108 m_widgetLabel
= BUTTON_CHILD( m_widgetCheckbox
);
109 m_widget
= m_widgetCheckbox
;
113 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox
),
115 GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback
),
118 m_parent
->DoAddChild( this );
125 void wxCheckBox::SetValue( bool state
)
127 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
129 if (state
== GetValue())
134 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
136 m_blockEvent
= false;
139 bool wxCheckBox::GetValue() const
141 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, false, wxT("invalid checkbox") );
143 return GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->active
;
146 void wxCheckBox::SetLabel( const wxString
& label
)
148 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
150 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel
), label
);
153 bool wxCheckBox::Enable( bool enable
)
155 if ( !wxControl::Enable( enable
) )
158 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
163 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
165 gtk_widget_modify_style(m_widgetCheckbox
, style
);
166 gtk_widget_modify_style(m_widgetLabel
, style
);
169 bool wxCheckBox::IsOwnGtkWindow( GdkWindow
*window
)
171 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
174 void wxCheckBox::OnInternalIdle()
176 wxCursor cursor
= m_cursor
;
177 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
179 GdkWindow
*event_window
= TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox
);
180 if ( event_window
&& cursor
.Ok() )
182 /* I now set the cursor the anew in every OnInternalIdle call
183 as setting the cursor in a parent window also effects the
184 windows above so that checking for the current cursor is
187 gdk_window_set_cursor( event_window
, cursor
.GetCursor() );
190 if (g_delayedFocus
== this)
192 if (GTK_WIDGET_REALIZED(m_widget
))
194 gtk_widget_grab_focus( m_widget
);
195 g_delayedFocus
= NULL
;
199 if (wxUpdateUIEvent::CanUpdate(this))
200 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
203 wxSize
wxCheckBox::DoGetBestSize() const
205 return wxControl::DoGetBestSize();
210 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
212 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new
);