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