1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "checkbox.h"
19 #include "wx/checkbox.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern void wxapp_install_idle_handler();
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
36 extern wxCursor g_globalCursor
;
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 static void gtk_checkbox_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxCheckBox
*cb
)
44 if (g_isIdle
) wxapp_install_idle_handler();
46 if (!cb
->m_hasVMT
) return;
48 if (g_blockEventsOnDrag
) return;
50 wxCommandEvent
event(wxEVT_COMMAND_CHECKBOX_CLICKED
, cb
->GetId());
51 event
.SetInt( cb
->GetValue() );
52 event
.SetEventObject(cb
);
53 cb
->GetEventHandler()->ProcessEvent(event
);
56 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
60 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox
,wxControl
)
62 wxCheckBox::wxCheckBox()
66 bool wxCheckBox::Create(wxWindow
*parent
,
68 const wxString
&label
,
72 const wxValidator
& validator
,
73 const wxString
&name
)
76 m_acceptsFocus
= TRUE
;
78 if (!PreCreation( parent
, pos
, size
) ||
79 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
81 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
85 wxControl::SetLabel( label
);
87 if ( style
& wxALIGN_RIGHT
)
89 // VZ: as I don't know a way to create a right aligned checkbox with
90 // GTK we will create a checkbox without label and a label at the
92 m_widgetCheckbox
= gtk_check_button_new();
94 m_widgetLabel
= gtk_label_new(m_label
.mbc_str());
95 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel
), 0.0, 0.5);
97 m_widget
= gtk_hbox_new(FALSE
, 0);
98 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetLabel
, FALSE
, FALSE
, 3);
99 gtk_box_pack_start(GTK_BOX(m_widget
), m_widgetCheckbox
, FALSE
, FALSE
, 3);
101 gtk_widget_show( m_widgetLabel
);
102 gtk_widget_show( m_widgetCheckbox
);
106 m_widgetCheckbox
= gtk_check_button_new_with_label( m_label
.mbc_str() );
107 m_widgetLabel
= GTK_BUTTON( m_widgetCheckbox
)->child
;
108 m_widget
= m_widgetCheckbox
;
111 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox
),
113 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback
),
116 m_parent
->DoAddChild( this );
120 SetFont( parent
->GetFont() );
122 wxSize
size_best( DoGetBestSize() );
123 wxSize
new_size( size
);
124 if (new_size
.x
== -1)
125 new_size
.x
= size_best
.x
;
126 if (new_size
.y
== -1)
127 new_size
.y
= size_best
.y
;
128 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
129 SetSize( new_size
.x
, new_size
.y
);
131 SetBackgroundColour( parent
->GetBackgroundColour() );
132 SetForegroundColour( parent
->GetForegroundColour() );
139 void wxCheckBox::SetValue( bool state
)
141 wxCHECK_RET( m_widgetCheckbox
!= NULL
, wxT("invalid checkbox") );
143 if (state
== GetValue())
146 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widgetCheckbox
),
147 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback
),
150 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox
), state
);
152 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox
),
154 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback
),
158 bool wxCheckBox::GetValue() const
160 wxCHECK_MSG( m_widgetCheckbox
!= NULL
, FALSE
, wxT("invalid checkbox") );
162 return GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->active
;
165 void wxCheckBox::SetLabel( const wxString
& label
)
167 wxCHECK_RET( m_widgetLabel
!= NULL
, wxT("invalid checkbox") );
169 wxControl::SetLabel( label
);
171 gtk_label_set( GTK_LABEL(m_widgetLabel
), GetLabel().mbc_str() );
174 bool wxCheckBox::Enable( bool enable
)
176 if ( !wxControl::Enable( enable
) )
179 gtk_widget_set_sensitive( m_widgetLabel
, enable
);
184 void wxCheckBox::ApplyWidgetStyle()
187 gtk_widget_set_style( m_widgetCheckbox
, m_widgetStyle
);
188 gtk_widget_set_style( m_widgetLabel
, m_widgetStyle
);
191 bool wxCheckBox::IsOwnGtkWindow( GdkWindow
*window
)
193 return (window
== GTK_TOGGLE_BUTTON(m_widget
)->event_window
);
196 void wxCheckBox::OnInternalIdle()
198 wxCursor cursor
= m_cursor
;
199 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
201 if (GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->event_window
&& cursor
.Ok())
203 /* I now set the cursor the anew in every OnInternalIdle call
204 as setting the cursor in a parent window also effects the
205 windows above so that checking for the current cursor is
208 gdk_window_set_cursor( GTK_TOGGLE_BUTTON(m_widgetCheckbox
)->event_window
, cursor
.GetCursor() );
214 wxSize
wxCheckBox::DoGetBestSize() const
216 return wxControl::DoGetBestSize();