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 wxCheckBox::wxCheckBox()
65 bool wxCheckBox::Create(wxWindow
*parent
,
67 const wxString
&label
,
71 const wxValidator
& validator
,
72 const wxString
&name
)
75 m_acceptsFocus
= true;
78 WXValidateStyle(&style
);
79 if (!PreCreation( parent
, pos
, size
) ||
80 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
82 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
86 if ( style
& wxALIGN_RIGHT
)
88 // VZ: as I don't know a way to create a right aligned checkbox with
89 // GTK we will create a checkbox without label and a label at the
91 m_widgetCheckbox
= gtk_check_button_new();
93 m_widgetLabel
= gtk_label_new("");
94 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
96 m_widget
= gtk_hbox_new(FALSE
, 0);
97 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
98 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
100 gtk_widget_show( m_widgetLabel
);
101 gtk_widget_show( m_widgetCheckbox
);
105 m_widgetCheckbox
= gtk_check_button_new_with_label("");
106 m_widgetLabel
= BUTTON_CHILD( m_widgetCheckbox
);
107 m_widget
= m_widgetCheckbox
;
111 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox
),
113 GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback
),
116 m_parent
->DoAddChild( this );
123 void wxCheckBox::SetValue( bool state
)
125 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
127 if (state
== GetValue())
132 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
134 m_blockEvent
= false;
137 bool wxCheckBox::GetValue() const
139 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, false, wxT("invalid checkbox") );
141 return GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->active
;
144 void wxCheckBox::SetLabel( const wxString
& label
)
146 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
148 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel
), label
);
151 bool wxCheckBox::Enable( bool enable
)
153 if ( !wxControl::Enable( enable
) )
156 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
161 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
163 gtk_widget_modify_style(m_widgetCheckbox
, style
);
164 gtk_widget_modify_style(m_widgetLabel
, style
);
167 bool wxCheckBox::IsOwnGtkWindow( GdkWindow
*window
)
169 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
172 void wxCheckBox::OnInternalIdle()
174 wxCursor cursor
= m_cursor
;
175 if (g_globalCursor
.IsOk()) cursor
= g_globalCursor
;
177 GdkWindow
*event_window
= TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox
);
178 if ( event_window
&& cursor
.IsOk() )
180 /* I now set the cursor the anew in every OnInternalIdle call
181 as setting the cursor in a parent window also effects the
182 windows above so that checking for the current cursor is
185 gdk_window_set_cursor( event_window
, cursor
.GetCursor() );
188 if (g_delayedFocus
== this)
190 if (GTK_WIDGET_REALIZED(m_widget
))
192 gtk_widget_grab_focus( m_widget
);
193 g_delayedFocus
= NULL
;
197 if (wxUpdateUIEvent::CanUpdate(this))
198 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
201 wxSize
wxCheckBox::DoGetBestSize() const
203 return wxControl::DoGetBestSize();
208 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
210 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new
);