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