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