]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
93763ad5 | 2 | // Name: src/gtk/checkbox.cpp |
c801d85f KB |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
01111366 | 5 | // Copyright: (c) 1998 Robert Roebling |
65571936 | 6 | // Licence: wxWindows licence |
c801d85f KB |
7 | ///////////////////////////////////////////////////////////////////////////// |
8 | ||
14f355c2 VS |
9 | // For compilers that support precompilation, includes "wx.h". |
10 | #include "wx/wxprec.h" | |
11 | ||
dcf924a3 RR |
12 | #if wxUSE_CHECKBOX |
13 | ||
1e6feb95 VZ |
14 | #include "wx/checkbox.h" |
15 | ||
a1abca32 | 16 | #include <gtk/gtk.h> |
9dc44eff | 17 | #include "wx/gtk/private/gtk2-compat.h" |
83624f79 | 18 | |
66bd6b93 RR |
19 | //----------------------------------------------------------------------------- |
20 | // data | |
21 | //----------------------------------------------------------------------------- | |
22 | ||
d7fa7eaa | 23 | extern bool g_blockEventsOnDrag; |
66bd6b93 | 24 | |
c801d85f | 25 | //----------------------------------------------------------------------------- |
e1e955e1 | 26 | // "clicked" |
c801d85f KB |
27 | //----------------------------------------------------------------------------- |
28 | ||
865bb325 | 29 | extern "C" { |
4dcccda6 | 30 | static void gtk_checkbox_toggled_callback(GtkWidget *widget, wxCheckBox *cb) |
c801d85f | 31 | { |
b292e2f5 | 32 | if (g_blockEventsOnDrag) return; |
3d257b8d | 33 | |
4dcccda6 VS |
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 -> ... | |
d5027818 PC |
45 | bool active = gtk_toggle_button_get_active(toggle) != 0; |
46 | bool inconsistent = gtk_toggle_button_get_inconsistent(toggle) != 0; | |
4dcccda6 | 47 | |
c71ab7c1 | 48 | cb->GTKDisableEvents(); |
3d257b8d | 49 | |
4dcccda6 VS |
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 | { | |
9a83f860 | 68 | wxFAIL_MSG(wxT("3state wxCheckBox in unexpected state!")); |
4dcccda6 | 69 | } |
3d257b8d | 70 | |
c71ab7c1 | 71 | cb->GTKEnableEvents(); |
4dcccda6 VS |
72 | } |
73 | else | |
74 | { | |
75 | // user's action unsets undetermined state: | |
76 | gtk_toggle_button_set_inconsistent(toggle, false); | |
77 | } | |
78 | } | |
4dcccda6 | 79 | |
ce7fe42e | 80 | wxCommandEvent event(wxEVT_CHECKBOX, cb->GetId()); |
4dcccda6 | 81 | event.SetInt(cb->Get3StateValue()); |
b292e2f5 | 82 | event.SetEventObject(cb); |
937013e0 | 83 | cb->HandleWindowEvent(event); |
6de97a3b | 84 | } |
865bb325 | 85 | } |
c801d85f | 86 | |
e1e955e1 RR |
87 | //----------------------------------------------------------------------------- |
88 | // wxCheckBox | |
c801d85f KB |
89 | //----------------------------------------------------------------------------- |
90 | ||
2cc0e28f | 91 | wxCheckBox::wxCheckBox() |
c801d85f | 92 | { |
7af131ab PC |
93 | m_widgetCheckbox = NULL; |
94 | } | |
95 | ||
96 | wxCheckBox::~wxCheckBox() | |
97 | { | |
98 | if (m_widgetCheckbox && m_widgetCheckbox != m_widget) | |
99 | GTKDisconnect(m_widgetCheckbox); | |
6de97a3b | 100 | } |
c801d85f | 101 | |
2cc0e28f VZ |
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 ) | |
c801d85f | 110 | { |
f254e242 | 111 | WXValidateStyle( &style ); |
4dcaf11a RR |
112 | if (!PreCreation( parent, pos, size ) || |
113 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
114 | { | |
223d09f6 | 115 | wxFAIL_MSG( wxT("wxCheckBox creation failed") ); |
93763ad5 | 116 | return false; |
4dcaf11a | 117 | } |
ce4169a4 | 118 | |
2cc0e28f VZ |
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(); | |
6de97a3b | 125 | |
eaafd2f8 | 126 | m_widgetLabel = gtk_label_new(""); |
2cc0e28f | 127 | gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5); |
ff8bfdbb | 128 | |
1897abe1 | 129 | m_widget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); |
2cc0e28f VZ |
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 | ||
2cc0e28f VZ |
133 | gtk_widget_show( m_widgetLabel ); |
134 | gtk_widget_show( m_widgetCheckbox ); | |
135 | } | |
136 | else | |
137 | { | |
eaafd2f8 | 138 | m_widgetCheckbox = gtk_check_button_new_with_label(""); |
385e8575 | 139 | m_widgetLabel = gtk_bin_get_child(GTK_BIN(m_widgetCheckbox)); |
2cc0e28f VZ |
140 | m_widget = m_widgetCheckbox; |
141 | } | |
9ff9d30c | 142 | g_object_ref(m_widget); |
eaafd2f8 | 143 | SetLabel( label ); |
2cc0e28f | 144 | |
9fa72bd2 MR |
145 | g_signal_connect (m_widgetCheckbox, "toggled", |
146 | G_CALLBACK (gtk_checkbox_toggled_callback), this); | |
ff8bfdbb | 147 | |
f03fc89f | 148 | m_parent->DoAddChild( this ); |
ff8bfdbb | 149 | |
abdeb9e7 | 150 | PostCreation(size); |
ff8bfdbb | 151 | |
93763ad5 | 152 | return true; |
6de97a3b | 153 | } |
c801d85f | 154 | |
c71ab7c1 RR |
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 | ||
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 | 172 | return; |
03647350 | 173 | |
c71ab7c1 | 174 | GTKDisableEvents(); |
ae0bdb01 | 175 | |
e343da37 | 176 | gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state ); |
1db8dc4a | 177 | |
c71ab7c1 | 178 | GTKEnableEvents(); |
6de97a3b | 179 | } |
c801d85f | 180 | |
83058c58 | 181 | bool wxCheckBox::GetValue() const |
c801d85f | 182 | { |
93763ad5 | 183 | wxCHECK_MSG( m_widgetCheckbox != NULL, false, wxT("invalid checkbox") ); |
f96aa4d9 | 184 | |
d5027818 | 185 | return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox)) != 0; |
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 | |
39bc0347 VZ |
211 | // save the label inside m_label in case user calls GetLabel() later |
212 | wxControl::SetLabel(label); | |
213 | ||
b2ff89d6 | 214 | GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel), label); |
83058c58 RR |
215 | } |
216 | ||
f03fc89f | 217 | bool wxCheckBox::Enable( bool enable ) |
d3904ceb | 218 | { |
b545684e | 219 | if (!base_type::Enable(enable)) |
93763ad5 | 220 | return false; |
ff8bfdbb | 221 | |
2cc0e28f | 222 | gtk_widget_set_sensitive( m_widgetLabel, enable ); |
f03fc89f | 223 | |
b545684e | 224 | if (enable) |
ad60f9e7 | 225 | GTKFixSensitivity(); |
ad60f9e7 | 226 | |
93763ad5 | 227 | return true; |
d3904ceb RR |
228 | } |
229 | ||
f40fdaa3 | 230 | void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle *style) |
868a2826 | 231 | { |
9dc44eff PC |
232 | GTKApplyStyle(m_widgetCheckbox, style); |
233 | GTKApplyStyle(m_widgetLabel, style); | |
f96aa4d9 RR |
234 | } |
235 | ||
ef5c70f9 | 236 | GdkWindow *wxCheckBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const |
2f073eb2 | 237 | { |
9dc44eff | 238 | return gtk_button_get_event_window(GTK_BUTTON(m_widgetCheckbox)); |
2f073eb2 RR |
239 | } |
240 | ||
9d522606 RD |
241 | // static |
242 | wxVisualAttributes | |
243 | wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
244 | { | |
7fff16b8 | 245 | return GetDefaultAttributesFromGTKWidget(gtk_check_button_new()); |
9d522606 RD |
246 | } |
247 | ||
dcf924a3 | 248 | #endif |