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