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