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