]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/checkbox.cpp
make some reference handling a little more direct and less obscure
[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>
9dc44eff 18#include "wx/gtk/private/gtk2-compat.h"
83624f79 19
66bd6b93
RR
20//-----------------------------------------------------------------------------
21// data
22//-----------------------------------------------------------------------------
23
d7fa7eaa 24extern bool g_blockEventsOnDrag;
66bd6b93 25
c801d85f 26//-----------------------------------------------------------------------------
e1e955e1 27// "clicked"
c801d85f
KB
28//-----------------------------------------------------------------------------
29
865bb325 30extern "C" {
4dcccda6 31static void gtk_checkbox_toggled_callback(GtkWidget *widget, wxCheckBox *cb)
c801d85f 32{
b292e2f5 33 if (g_blockEventsOnDrag) return;
3d257b8d 34
4dcccda6
VS
35 // Transitions for 3state checkbox must be done manually, GTK's checkbox
36 // is 2state with additional "undetermined state" flag which isn't
37 // changed automatically:
38 if (cb->Is3State())
39 {
40 GtkToggleButton *toggle = GTK_TOGGLE_BUTTON(widget);
41
42 if (cb->Is3rdStateAllowedForUser())
43 {
44 // The 3 states cycle like this when clicked:
45 // checked -> undetermined -> unchecked -> checked -> ...
d5027818
PC
46 bool active = gtk_toggle_button_get_active(toggle) != 0;
47 bool inconsistent = gtk_toggle_button_get_inconsistent(toggle) != 0;
4dcccda6 48
c71ab7c1 49 cb->GTKDisableEvents();
3d257b8d 50
4dcccda6
VS
51 if (!active && !inconsistent)
52 {
53 // checked -> undetermined
54 gtk_toggle_button_set_active(toggle, true);
55 gtk_toggle_button_set_inconsistent(toggle, true);
56 }
57 else if (!active && inconsistent)
58 {
59 // undetermined -> unchecked
60 gtk_toggle_button_set_inconsistent(toggle, false);
61 }
62 else if (active && !inconsistent)
63 {
64 // unchecked -> checked
65 // nothing to do
66 }
67 else
68 {
9a83f860 69 wxFAIL_MSG(wxT("3state wxCheckBox in unexpected state!"));
4dcccda6 70 }
3d257b8d 71
c71ab7c1 72 cb->GTKEnableEvents();
4dcccda6
VS
73 }
74 else
75 {
76 // user's action unsets undetermined state:
77 gtk_toggle_button_set_inconsistent(toggle, false);
78 }
79 }
4dcccda6 80
b292e2f5 81 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
4dcccda6 82 event.SetInt(cb->Get3StateValue());
b292e2f5 83 event.SetEventObject(cb);
937013e0 84 cb->HandleWindowEvent(event);
6de97a3b 85}
865bb325 86}
c801d85f 87
e1e955e1
RR
88//-----------------------------------------------------------------------------
89// wxCheckBox
c801d85f
KB
90//-----------------------------------------------------------------------------
91
2cc0e28f 92wxCheckBox::wxCheckBox()
c801d85f 93{
7af131ab
PC
94 m_widgetCheckbox = NULL;
95}
96
97wxCheckBox::~wxCheckBox()
98{
99 if (m_widgetCheckbox && m_widgetCheckbox != m_widget)
100 GTKDisconnect(m_widgetCheckbox);
6de97a3b 101}
c801d85f 102
2cc0e28f
VZ
103bool wxCheckBox::Create(wxWindow *parent,
104 wxWindowID id,
105 const wxString &label,
106 const wxPoint &pos,
107 const wxSize &size,
108 long style,
109 const wxValidator& validator,
110 const wxString &name )
c801d85f 111{
f254e242 112 WXValidateStyle( &style );
4dcaf11a
RR
113 if (!PreCreation( parent, pos, size ) ||
114 !CreateBase( parent, id, pos, size, style, validator, name ))
115 {
223d09f6 116 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
93763ad5 117 return false;
4dcaf11a 118 }
ce4169a4 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
1897abe1 130 m_widget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
2cc0e28f
VZ
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("");
385e8575 140 m_widgetLabel = gtk_bin_get_child(GTK_BIN(m_widgetCheckbox));
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;
03647350 174
c71ab7c1 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
d5027818 186 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox)) != 0;
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{
b545684e 220 if (!base_type::Enable(enable))
93763ad5 221 return false;
ff8bfdbb 222
2cc0e28f 223 gtk_widget_set_sensitive( m_widgetLabel, enable );
f03fc89f 224
b545684e 225 if (enable)
ad60f9e7 226 GTKFixSensitivity();
ad60f9e7 227
93763ad5 228 return true;
d3904ceb
RR
229}
230
f40fdaa3 231void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 232{
9dc44eff
PC
233 GTKApplyStyle(m_widgetCheckbox, style);
234 GTKApplyStyle(m_widgetLabel, style);
f96aa4d9
RR
235}
236
ef5c70f9 237GdkWindow *wxCheckBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
2f073eb2 238{
9dc44eff 239 return gtk_button_get_event_window(GTK_BUTTON(m_widgetCheckbox));
2f073eb2
RR
240}
241
9d522606
RD
242// static
243wxVisualAttributes
244wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
245{
7fff16b8 246 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new());
9d522606
RD
247}
248
dcf924a3 249#endif