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