]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/checkbox.cpp
Use conversion class in native GTK+ file dialog.
[wxWidgets.git] / src / gtk / checkbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: checkbox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "checkbox.h"
13 #endif
14
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17
18 #include "wx/defs.h"
19
20 #if wxUSE_CHECKBOX
21
22 #include "wx/checkbox.h"
23
24 #include "wx/gtk/private.h"
25
26 //-----------------------------------------------------------------------------
27 // idle system
28 //-----------------------------------------------------------------------------
29
30 extern void wxapp_install_idle_handler();
31 extern bool g_isIdle;
32
33 //-----------------------------------------------------------------------------
34 // data
35 //-----------------------------------------------------------------------------
36
37 extern bool g_blockEventsOnDrag;
38 extern wxCursor g_globalCursor;
39 extern wxWindowGTK *g_delayedFocus;
40
41 //-----------------------------------------------------------------------------
42 // "clicked"
43 //-----------------------------------------------------------------------------
44
45 extern "C" {
46 static void gtk_checkbox_toggled_callback(GtkWidget *widget, wxCheckBox *cb)
47 {
48 if (g_isIdle) wxapp_install_idle_handler();
49
50 if (!cb->m_hasVMT) return;
51
52 if (g_blockEventsOnDrag) return;
53
54 if (cb->m_blockEvent) return;
55
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
104 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
105 #ifdef __WXGTK20__
106 event.SetInt(cb->Get3StateValue());
107 #else
108 event.SetInt(cb->GetValue());
109 #endif
110 event.SetEventObject(cb);
111 cb->GetEventHandler()->ProcessEvent(event);
112 }
113 }
114
115 //-----------------------------------------------------------------------------
116 // wxCheckBox
117 //-----------------------------------------------------------------------------
118
119 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
120
121 wxCheckBox::wxCheckBox()
122 {
123 }
124
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 )
133 {
134 m_needParent = TRUE;
135 m_acceptsFocus = TRUE;
136 m_blockEvent = FALSE;
137
138 if (!PreCreation( parent, pos, size ) ||
139 !CreateBase( parent, id, pos, size, style, validator, name ))
140 {
141 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
142 return FALSE;
143 }
144
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
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();
156
157 m_widgetLabel = gtk_label_new("");
158 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
159
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
164 gtk_widget_show( m_widgetLabel );
165 gtk_widget_show( m_widgetCheckbox );
166 }
167 else
168 {
169 m_widgetCheckbox = gtk_check_button_new_with_label("");
170 m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox );
171 m_widget = m_widgetCheckbox;
172 }
173 SetLabel( label );
174
175 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
176 "toggled",
177 GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback),
178 (gpointer *)this );
179
180 m_parent->DoAddChild( this );
181
182 PostCreation(size);
183
184 return TRUE;
185 }
186
187 void wxCheckBox::SetValue( bool state )
188 {
189 wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") );
190
191 if (state == GetValue())
192 return;
193
194 m_blockEvent = TRUE;
195
196 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
197
198 m_blockEvent = FALSE;
199 }
200
201 bool wxCheckBox::GetValue() const
202 {
203 wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, wxT("invalid checkbox") );
204
205 #ifdef __WXGTK20__
206 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox));
207 #else
208 return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active;
209 #endif
210 }
211
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
233 void wxCheckBox::SetLabel( const wxString& label )
234 {
235 wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") );
236
237 wxControl::SetLabel( label );
238
239 #ifdef __WXGTK20__
240 wxString label2 = PrepareLabelMnemonics( label );
241 gtk_label_set_text_with_mnemonic( GTK_LABEL(m_widgetLabel), wxGTK_CONV( label2 ) );
242 #else
243 gtk_label_set( GTK_LABEL(m_widgetLabel), wxGTK_CONV( GetLabel() ) );
244 #endif
245 }
246
247 bool wxCheckBox::Enable( bool enable )
248 {
249 if ( !wxControl::Enable( enable ) )
250 return FALSE;
251
252 gtk_widget_set_sensitive( m_widgetLabel, enable );
253
254 return TRUE;
255 }
256
257 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle *style)
258 {
259 gtk_widget_modify_style(m_widgetCheckbox, style);
260 gtk_widget_modify_style(m_widgetLabel, style);
261 }
262
263 bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window )
264 {
265 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
266 }
267
268 void wxCheckBox::OnInternalIdle()
269 {
270 wxCursor cursor = m_cursor;
271 if (g_globalCursor.Ok()) cursor = g_globalCursor;
272
273 GdkWindow *event_window = TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox);
274 if ( event_window && cursor.Ok() )
275 {
276 /* I now set the cursor the anew in every OnInternalIdle call
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
281 gdk_window_set_cursor( event_window, cursor.GetCursor() );
282 }
283
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
293 if (wxUpdateUIEvent::CanUpdate(this))
294 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
295 }
296
297 wxSize wxCheckBox::DoGetBestSize() const
298 {
299 return wxControl::DoGetBestSize();
300 }
301
302 // static
303 wxVisualAttributes
304 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
305 {
306 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new);
307 }
308
309 #endif