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