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