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