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