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