]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/checkbox.cpp
Fix typo in last commit
[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 <gtk/gtk.h>
18
19 //-----------------------------------------------------------------------------
20 // data
21 //-----------------------------------------------------------------------------
22
23 extern bool g_blockEventsOnDrag;
24
25 //-----------------------------------------------------------------------------
26 // "clicked"
27 //-----------------------------------------------------------------------------
28
29 extern "C" {
30 static void gtk_checkbox_toggled_callback(GtkWidget *widget, wxCheckBox *cb)
31 {
32 if (!cb->m_hasVMT) return;
33
34 if (g_blockEventsOnDrag) return;
35
36 // Transitions for 3state checkbox must be done manually, GTK's checkbox
37 // is 2state with additional "undetermined state" flag which isn't
38 // changed automatically:
39 if (cb->Is3State())
40 {
41 GtkToggleButton *toggle = GTK_TOGGLE_BUTTON(widget);
42
43 if (cb->Is3rdStateAllowedForUser())
44 {
45 // The 3 states cycle like this when clicked:
46 // checked -> undetermined -> unchecked -> checked -> ...
47 bool active = gtk_toggle_button_get_active(toggle);
48 bool inconsistent = gtk_toggle_button_get_inconsistent(toggle);
49
50 cb->GTKDisableEvents();
51
52 if (!active && !inconsistent)
53 {
54 // checked -> undetermined
55 gtk_toggle_button_set_active(toggle, true);
56 gtk_toggle_button_set_inconsistent(toggle, true);
57 }
58 else if (!active && inconsistent)
59 {
60 // undetermined -> unchecked
61 gtk_toggle_button_set_inconsistent(toggle, false);
62 }
63 else if (active && !inconsistent)
64 {
65 // unchecked -> checked
66 // nothing to do
67 }
68 else
69 {
70 wxFAIL_MSG(wxT("3state wxCheckBox in unexpected state!"));
71 }
72
73 cb->GTKEnableEvents();
74 }
75 else
76 {
77 // user's action unsets undetermined state:
78 gtk_toggle_button_set_inconsistent(toggle, false);
79 }
80 }
81
82 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
83 event.SetInt(cb->Get3StateValue());
84 event.SetEventObject(cb);
85 cb->HandleWindowEvent(event);
86 }
87 }
88
89 //-----------------------------------------------------------------------------
90 // wxCheckBox
91 //-----------------------------------------------------------------------------
92
93 wxCheckBox::wxCheckBox()
94 {
95 }
96
97 bool wxCheckBox::Create(wxWindow *parent,
98 wxWindowID id,
99 const wxString &label,
100 const wxPoint &pos,
101 const wxSize &size,
102 long style,
103 const wxValidator& validator,
104 const wxString &name )
105 {
106 WXValidateStyle( &style );
107 if (!PreCreation( parent, pos, size ) ||
108 !CreateBase( parent, id, pos, size, style, validator, name ))
109 {
110 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
111 return false;
112 }
113
114 if ( style & wxALIGN_RIGHT )
115 {
116 // VZ: as I don't know a way to create a right aligned checkbox with
117 // GTK we will create a checkbox without label and a label at the
118 // left of it
119 m_widgetCheckbox = gtk_check_button_new();
120
121 m_widgetLabel = gtk_label_new("");
122 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
123
124 m_widget = gtk_hbox_new(FALSE, 0);
125 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3);
126 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3);
127
128 gtk_widget_show( m_widgetLabel );
129 gtk_widget_show( m_widgetCheckbox );
130 }
131 else
132 {
133 m_widgetCheckbox = gtk_check_button_new_with_label("");
134 m_widgetLabel = gtk_bin_get_child(GTK_BIN(m_widgetCheckbox));
135 m_widget = m_widgetCheckbox;
136 }
137 g_object_ref(m_widget);
138 SetLabel( label );
139
140 g_signal_connect (m_widgetCheckbox, "toggled",
141 G_CALLBACK (gtk_checkbox_toggled_callback), this);
142
143 m_parent->DoAddChild( this );
144
145 PostCreation(size);
146
147 return true;
148 }
149
150 void wxCheckBox::GTKDisableEvents()
151 {
152 g_signal_handlers_block_by_func(m_widgetCheckbox,
153 (gpointer) gtk_checkbox_toggled_callback, this);
154 }
155
156 void wxCheckBox::GTKEnableEvents()
157 {
158 g_signal_handlers_unblock_by_func(m_widgetCheckbox,
159 (gpointer) gtk_checkbox_toggled_callback, this);
160 }
161
162 void wxCheckBox::SetValue( bool state )
163 {
164 wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") );
165
166 if (state == GetValue())
167 return;
168
169 GTKDisableEvents();
170
171 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
172
173 GTKEnableEvents();
174 }
175
176 bool wxCheckBox::GetValue() const
177 {
178 wxCHECK_MSG( m_widgetCheckbox != NULL, false, wxT("invalid checkbox") );
179
180 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox));
181 }
182
183 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
184 {
185 SetValue(state != wxCHK_UNCHECKED);
186 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox),
187 state == wxCHK_UNDETERMINED);
188 }
189
190 wxCheckBoxState wxCheckBox::DoGet3StateValue() const
191 {
192 if (gtk_toggle_button_get_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox)))
193 {
194 return wxCHK_UNDETERMINED;
195 }
196 else
197 {
198 return GetValue() ? wxCHK_CHECKED : wxCHK_UNCHECKED;
199 }
200 }
201
202 void wxCheckBox::SetLabel( const wxString& label )
203 {
204 wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") );
205
206 // save the label inside m_label in case user calls GetLabel() later
207 wxControl::SetLabel(label);
208
209 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel), label);
210 }
211
212 bool wxCheckBox::Enable( bool enable )
213 {
214 if (!base_type::Enable(enable))
215 return false;
216
217 gtk_widget_set_sensitive( m_widgetLabel, enable );
218
219 if (enable)
220 GTKFixSensitivity();
221
222 return true;
223 }
224
225 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle *style)
226 {
227 gtk_widget_modify_style(m_widgetCheckbox, style);
228 gtk_widget_modify_style(m_widgetLabel, style);
229 }
230
231 GdkWindow *wxCheckBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
232 {
233 return GTK_BUTTON(m_widgetCheckbox)->event_window;
234 }
235
236 // static
237 wxVisualAttributes
238 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
239 {
240 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new);
241 }
242
243 #endif