1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/checkbox.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
14 #include "wx/checkbox.h"
16 #include "wx/gtk1/private.h"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 extern void wxapp_install_idle_handler();
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern bool g_blockEventsOnDrag
;
30 extern wxCursor g_globalCursor
;
31 extern wxWindowGTK
*g_delayedFocus
;
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
38 static void gtk_checkbox_toggled_callback(GtkWidget
*WXUNUSED(widget
),
41 if (g_isIdle
) wxapp_install_idle_handler();
43 if (!cb
->m_hasVMT
) return;
45 if (g_blockEventsOnDrag
) return;
47 if (cb
->m_blockEvent
) return;
49 wxCommandEvent
event(wxEVT_CHECKBOX
, cb
->GetId());
50 event
.SetInt(cb
->GetValue());
51 event
.SetEventObject(cb
);
52 cb
->HandleWindowEvent(event
);
56 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
60 wxCheckBox::wxCheckBox()
64 bool wxCheckBox::Create(wxWindow
*parent
,
66 const wxString
&label
,
70 const wxValidator
& validator
,
71 const wxString
&name
)
74 m_acceptsFocus
= true;
77 WXValidateStyle(&style
);
78 if (!PreCreation( parent
, pos
, size
) ||
79 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
81 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
85 if ( style
& wxALIGN_RIGHT
)
87 // VZ: as I don't know a way to create a right aligned checkbox with
88 // GTK we will create a checkbox without label and a label at the
90 m_widgetCheckbox
= gtk_check_button_new();
92 m_widgetLabel
= gtk_label_new("");
93 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
95 m_widget
= gtk_hbox_new(FALSE
, 0);
96 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
97 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
99 gtk_widget_show( m_widgetLabel
);
100 gtk_widget_show( m_widgetCheckbox
);
104 m_widgetCheckbox
= gtk_check_button_new_with_label("");
105 m_widgetLabel
= BUTTON_CHILD( m_widgetCheckbox
);
106 m_widget
= m_widgetCheckbox
;
110 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox
),
112 GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback
),
115 m_parent
->DoAddChild( this );
122 void wxCheckBox::SetValue( bool state
)
124 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
126 if (state
== GetValue())
131 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
133 m_blockEvent
= false;
136 bool wxCheckBox::GetValue() const
138 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, false, wxT("invalid checkbox") );
140 return GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->active
;
143 void wxCheckBox::SetLabel( const wxString
& label
)
145 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
147 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel
), label
);
150 bool wxCheckBox::Enable( bool enable
)
152 if ( !wxControl::Enable( enable
) )
155 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
160 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
162 gtk_widget_modify_style(m_widgetCheckbox
, style
);
163 gtk_widget_modify_style(m_widgetLabel
, style
);
166 bool wxCheckBox::IsOwnGtkWindow( GdkWindow
*window
)
168 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
171 void wxCheckBox::OnInternalIdle()
173 wxCursor cursor
= m_cursor
;
174 if (g_globalCursor
.IsOk()) cursor
= g_globalCursor
;
176 GdkWindow
*event_window
= TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox
);
177 if ( event_window
&& cursor
.IsOk() )
179 /* I now set the cursor the anew in every OnInternalIdle call
180 as setting the cursor in a parent window also effects the
181 windows above so that checking for the current cursor is
184 gdk_window_set_cursor( event_window
, cursor
.GetCursor() );
187 if (g_delayedFocus
== this)
189 if (GTK_WIDGET_REALIZED(m_widget
))
191 gtk_widget_grab_focus( m_widget
);
192 g_delayedFocus
= NULL
;
196 if (wxUpdateUIEvent::CanUpdate(this))
197 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
200 wxSize
wxCheckBox::DoGetBestSize() const
202 return wxControl::DoGetBestSize();
207 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
209 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new
);