]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/checkbox.cpp
Committing in .
[wxWidgets.git] / src / gtk1 / checkbox.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: checkbox.cpp
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
1e6feb95 13#include "wx/defs.h"
c801d85f 14
dcf924a3
RR
15#if wxUSE_CHECKBOX
16
1e6feb95
VZ
17#include "wx/checkbox.h"
18
9e691f46 19#include "wx/gtk/private.h"
83624f79 20
acfd422a
RR
21//-----------------------------------------------------------------------------
22// idle system
23//-----------------------------------------------------------------------------
24
25extern void wxapp_install_idle_handler();
26extern bool g_isIdle;
27
66bd6b93
RR
28//-----------------------------------------------------------------------------
29// data
30//-----------------------------------------------------------------------------
31
d7fa7eaa
RR
32extern bool g_blockEventsOnDrag;
33extern wxCursor g_globalCursor;
34extern wxWindowGTK *g_delayedFocus;
66bd6b93 35
c801d85f 36//-----------------------------------------------------------------------------
e1e955e1 37// "clicked"
c801d85f
KB
38//-----------------------------------------------------------------------------
39
865bb325 40extern "C" {
4dcccda6 41static void gtk_checkbox_toggled_callback(GtkWidget *widget, wxCheckBox *cb)
c801d85f 42{
acfd422a
RR
43 if (g_isIdle) wxapp_install_idle_handler();
44
a2053b27 45 if (!cb->m_hasVMT) return;
ff8bfdbb 46
b292e2f5 47 if (g_blockEventsOnDrag) return;
3d257b8d 48
9864c56d 49 if (cb->m_blockEvent) return;
ff8bfdbb 50
4dcccda6
VS
51#ifdef __WXGTK20__
52 // Transitions for 3state checkbox must be done manually, GTK's checkbox
53 // is 2state with additional "undetermined state" flag which isn't
54 // changed automatically:
55 if (cb->Is3State())
56 {
57 GtkToggleButton *toggle = GTK_TOGGLE_BUTTON(widget);
58
59 if (cb->Is3rdStateAllowedForUser())
60 {
61 // The 3 states cycle like this when clicked:
62 // checked -> undetermined -> unchecked -> checked -> ...
63 bool active = gtk_toggle_button_get_active(toggle);
64 bool inconsistent = gtk_toggle_button_get_inconsistent(toggle);
65
66 cb->m_blockEvent = true;
3d257b8d 67
4dcccda6
VS
68 if (!active && !inconsistent)
69 {
70 // checked -> undetermined
71 gtk_toggle_button_set_active(toggle, true);
72 gtk_toggle_button_set_inconsistent(toggle, true);
73 }
74 else if (!active && inconsistent)
75 {
76 // undetermined -> unchecked
77 gtk_toggle_button_set_inconsistent(toggle, false);
78 }
79 else if (active && !inconsistent)
80 {
81 // unchecked -> checked
82 // nothing to do
83 }
84 else
85 {
86 wxFAIL_MSG(_T("3state wxCheckBox in unexpected state!"));
87 }
3d257b8d 88
4dcccda6
VS
89 cb->m_blockEvent = false;
90 }
91 else
92 {
93 // user's action unsets undetermined state:
94 gtk_toggle_button_set_inconsistent(toggle, false);
95 }
96 }
97#endif
98
b292e2f5 99 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
4dcccda6
VS
100#ifdef __WXGTK20__
101 event.SetInt(cb->Get3StateValue());
102#else
103 event.SetInt(cb->GetValue());
104#endif
b292e2f5
RR
105 event.SetEventObject(cb);
106 cb->GetEventHandler()->ProcessEvent(event);
6de97a3b 107}
865bb325 108}
c801d85f 109
e1e955e1
RR
110//-----------------------------------------------------------------------------
111// wxCheckBox
c801d85f
KB
112//-----------------------------------------------------------------------------
113
114IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
115
2cc0e28f 116wxCheckBox::wxCheckBox()
c801d85f 117{
6de97a3b 118}
c801d85f 119
2cc0e28f
VZ
120bool wxCheckBox::Create(wxWindow *parent,
121 wxWindowID id,
122 const wxString &label,
123 const wxPoint &pos,
124 const wxSize &size,
125 long style,
126 const wxValidator& validator,
127 const wxString &name )
c801d85f 128{
b292e2f5
RR
129 m_needParent = TRUE;
130 m_acceptsFocus = TRUE;
9864c56d 131 m_blockEvent = FALSE;
ff8bfdbb 132
4dcaf11a
RR
133 if (!PreCreation( parent, pos, size ) ||
134 !CreateBase( parent, id, pos, size, style, validator, name ))
135 {
223d09f6 136 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
1db8dc4a 137 return FALSE;
4dcaf11a 138 }
ce4169a4 139
4dcccda6
VS
140 wxASSERT_MSG( (style & wxCHK_ALLOW_3RD_STATE_FOR_USER) == 0 ||
141 (style & wxCHK_3STATE) != 0,
142 wxT("Using wxCHK_ALLOW_3RD_STATE_FOR_USER")
143 wxT(" style flag for a 2-state checkbox is useless") );
144
2cc0e28f
VZ
145 if ( style & wxALIGN_RIGHT )
146 {
147 // VZ: as I don't know a way to create a right aligned checkbox with
148 // GTK we will create a checkbox without label and a label at the
149 // left of it
150 m_widgetCheckbox = gtk_check_button_new();
6de97a3b 151
eaafd2f8 152 m_widgetLabel = gtk_label_new("");
2cc0e28f 153 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
ff8bfdbb 154
2cc0e28f
VZ
155 m_widget = gtk_hbox_new(FALSE, 0);
156 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3);
157 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3);
158
2cc0e28f
VZ
159 gtk_widget_show( m_widgetLabel );
160 gtk_widget_show( m_widgetCheckbox );
161 }
162 else
163 {
eaafd2f8 164 m_widgetCheckbox = gtk_check_button_new_with_label("");
9e691f46 165 m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox );
2cc0e28f
VZ
166 m_widget = m_widgetCheckbox;
167 }
eaafd2f8 168 SetLabel( label );
2cc0e28f 169
2cc0e28f 170 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
4dcccda6
VS
171 "toggled",
172 GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback),
2cc0e28f 173 (gpointer *)this );
ff8bfdbb 174
f03fc89f 175 m_parent->DoAddChild( this );
ff8bfdbb 176
abdeb9e7 177 PostCreation(size);
ff8bfdbb 178
b292e2f5 179 return TRUE;
6de97a3b 180}
c801d85f 181
debe6624 182void wxCheckBox::SetValue( bool state )
c801d85f 183{
223d09f6 184 wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") );
ff8bfdbb 185
953704c1 186 if (state == GetValue())
8bf45ed1
VZ
187 return;
188
9864c56d 189 m_blockEvent = TRUE;
ae0bdb01 190
2cc0e28f 191 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
1db8dc4a 192
9864c56d 193 m_blockEvent = FALSE;
6de97a3b 194}
c801d85f 195
83058c58 196bool wxCheckBox::GetValue() const
c801d85f 197{
223d09f6 198 wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, wxT("invalid checkbox") );
f96aa4d9 199
4dcccda6
VS
200#ifdef __WXGTK20__
201 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox));
202#else
2cc0e28f 203 return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active;
4dcccda6 204#endif
6de97a3b 205}
c801d85f 206
4dcccda6
VS
207#ifdef __WXGTK20__
208void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
209{
210 SetValue(state != wxCHK_UNCHECKED);
211 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox),
212 state == wxCHK_UNDETERMINED);
213}
214
215wxCheckBoxState wxCheckBox::DoGet3StateValue() const
216{
217 if (gtk_toggle_button_get_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox)))
218 {
219 return wxCHK_UNDETERMINED;
220 }
221 else
222 {
223 return GetValue() ? wxCHK_CHECKED : wxCHK_UNCHECKED;
224 }
225}
226#endif
227
83058c58
RR
228void wxCheckBox::SetLabel( const wxString& label )
229{
223d09f6 230 wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") );
f96aa4d9 231
b2ff89d6 232 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel), label);
83058c58
RR
233}
234
f03fc89f 235bool wxCheckBox::Enable( bool enable )
d3904ceb 236{
f03fc89f
VZ
237 if ( !wxControl::Enable( enable ) )
238 return FALSE;
ff8bfdbb 239
2cc0e28f 240 gtk_widget_set_sensitive( m_widgetLabel, enable );
f03fc89f
VZ
241
242 return TRUE;
d3904ceb
RR
243}
244
f40fdaa3 245void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 246{
f40fdaa3
VS
247 gtk_widget_modify_style(m_widgetCheckbox, style);
248 gtk_widget_modify_style(m_widgetLabel, style);
f96aa4d9
RR
249}
250
2f073eb2
RR
251bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window )
252{
9e691f46 253 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
2f073eb2
RR
254}
255
256void wxCheckBox::OnInternalIdle()
257{
258 wxCursor cursor = m_cursor;
259 if (g_globalCursor.Ok()) cursor = g_globalCursor;
260
9e691f46
VZ
261 GdkWindow *event_window = TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox);
262 if ( event_window && cursor.Ok() )
2f073eb2
RR
263 {
264 /* I now set the cursor the anew in every OnInternalIdle call
1db8dc4a
VZ
265 as setting the cursor in a parent window also effects the
266 windows above so that checking for the current cursor is
267 not possible. */
268
9e691f46 269 gdk_window_set_cursor( event_window, cursor.GetCursor() );
2f073eb2
RR
270 }
271
d7fa7eaa
RR
272 if (g_delayedFocus == this)
273 {
274 if (GTK_WIDGET_REALIZED(m_widget))
275 {
276 gtk_widget_grab_focus( m_widget );
277 g_delayedFocus = NULL;
278 }
279 }
3d257b8d 280
e39af974
JS
281 if (wxUpdateUIEvent::CanUpdate(this))
282 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2f073eb2
RR
283}
284
f68586e5
VZ
285wxSize wxCheckBox::DoGetBestSize() const
286{
db434467 287 return wxControl::DoGetBestSize();
f68586e5
VZ
288}
289
9d522606
RD
290// static
291wxVisualAttributes
292wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
293{
294 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new);
295}
296
dcf924a3 297#endif