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