1. changed wxControl::GetLabel() to return the originally provided label and
[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 // 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 #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;
67
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 }
88
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
99 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
100 #ifdef __WXGTK20__
101 event.SetInt(cb->Get3StateValue());
102 #else
103 event.SetInt(cb->GetValue());
104 #endif
105 event.SetEventObject(cb);
106 cb->GetEventHandler()->ProcessEvent(event);
107 }
108 }
109
110 //-----------------------------------------------------------------------------
111 // wxCheckBox
112 //-----------------------------------------------------------------------------
113
114 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
115
116 wxCheckBox::wxCheckBox()
117 {
118 }
119
120 bool 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 )
128 {
129 m_needParent = TRUE;
130 m_acceptsFocus = TRUE;
131 m_blockEvent = FALSE;
132
133 if (!PreCreation( parent, pos, size ) ||
134 !CreateBase( parent, id, pos, size, style, validator, name ))
135 {
136 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
137 return FALSE;
138 }
139
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
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();
151
152 m_widgetLabel = gtk_label_new("");
153 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
154
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
159 gtk_widget_show( m_widgetLabel );
160 gtk_widget_show( m_widgetCheckbox );
161 }
162 else
163 {
164 m_widgetCheckbox = gtk_check_button_new_with_label("");
165 m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox );
166 m_widget = m_widgetCheckbox;
167 }
168 SetLabel( label );
169
170 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
171 "toggled",
172 GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback),
173 (gpointer *)this );
174
175 m_parent->DoAddChild( this );
176
177 PostCreation(size);
178
179 return TRUE;
180 }
181
182 void wxCheckBox::SetValue( bool state )
183 {
184 wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") );
185
186 if (state == GetValue())
187 return;
188
189 m_blockEvent = TRUE;
190
191 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
192
193 m_blockEvent = FALSE;
194 }
195
196 bool wxCheckBox::GetValue() const
197 {
198 wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, wxT("invalid checkbox") );
199
200 #ifdef __WXGTK20__
201 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox));
202 #else
203 return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active;
204 #endif
205 }
206
207 #ifdef __WXGTK20__
208 void 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
215 wxCheckBoxState 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
228 void wxCheckBox::SetLabel( const wxString& label )
229 {
230 wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") );
231
232 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel), label);
233 }
234
235 bool wxCheckBox::Enable( bool enable )
236 {
237 if ( !wxControl::Enable( enable ) )
238 return FALSE;
239
240 gtk_widget_set_sensitive( m_widgetLabel, enable );
241
242 return TRUE;
243 }
244
245 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle *style)
246 {
247 gtk_widget_modify_style(m_widgetCheckbox, style);
248 gtk_widget_modify_style(m_widgetLabel, style);
249 }
250
251 bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window )
252 {
253 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
254 }
255
256 void wxCheckBox::OnInternalIdle()
257 {
258 wxCursor cursor = m_cursor;
259 if (g_globalCursor.Ok()) cursor = g_globalCursor;
260
261 GdkWindow *event_window = TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox);
262 if ( event_window && cursor.Ok() )
263 {
264 /* I now set the cursor the anew in every OnInternalIdle call
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
269 gdk_window_set_cursor( event_window, cursor.GetCursor() );
270 }
271
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 }
280
281 if (wxUpdateUIEvent::CanUpdate(this))
282 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
283 }
284
285 wxSize wxCheckBox::DoGetBestSize() const
286 {
287 return wxControl::DoGetBestSize();
288 }
289
290 // static
291 wxVisualAttributes
292 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
293 {
294 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new);
295 }
296
297 #endif