]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/checkbox.cpp
removed extra membersections (patch 1702329)
[wxWidgets.git] / src / gtk / checkbox.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/checkbox.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#if wxUSE_CHECKBOX
14
15#include "wx/checkbox.h"
16
17#include <gtk/gtk.h>
18
19//-----------------------------------------------------------------------------
20// data
21//-----------------------------------------------------------------------------
22
23extern bool g_blockEventsOnDrag;
24
25//-----------------------------------------------------------------------------
26// "clicked"
27//-----------------------------------------------------------------------------
28
29extern "C" {
30static void gtk_checkbox_toggled_callback(GtkWidget *widget, wxCheckBox *cb)
31{
32 if (!cb->m_hasVMT) return;
33
34 if (g_blockEventsOnDrag) return;
35
36 if (cb->m_blockEvent) return;
37
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;
53
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 }
74
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 }
83
84 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
85 event.SetInt(cb->Get3StateValue());
86 event.SetEventObject(cb);
87 cb->GetEventHandler()->ProcessEvent(event);
88}
89}
90
91//-----------------------------------------------------------------------------
92// wxCheckBox
93//-----------------------------------------------------------------------------
94
95IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
96
97wxCheckBox::wxCheckBox()
98{
99}
100
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 )
109{
110 m_needParent = true;
111 m_blockEvent = false;
112
113 if (!PreCreation( parent, pos, size ) ||
114 !CreateBase( parent, id, pos, size, style, validator, name ))
115 {
116 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
117 return false;
118 }
119
120 wxASSERT_MSG( (style & wxCHK_ALLOW_3RD_STATE_FOR_USER) == 0 ||
121 (style & wxCHK_3STATE) != 0,
122 wxT("Using wxCHK_ALLOW_3RD_STATE_FOR_USER")
123 wxT(" style flag for a 2-state checkbox is useless") );
124
125 if ( style & wxALIGN_RIGHT )
126 {
127 // VZ: as I don't know a way to create a right aligned checkbox with
128 // GTK we will create a checkbox without label and a label at the
129 // left of it
130 m_widgetCheckbox = gtk_check_button_new();
131
132 m_widgetLabel = gtk_label_new("");
133 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
134
135 m_widget = gtk_hbox_new(FALSE, 0);
136 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3);
137 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3);
138
139 gtk_widget_show( m_widgetLabel );
140 gtk_widget_show( m_widgetCheckbox );
141 }
142 else
143 {
144 m_widgetCheckbox = gtk_check_button_new_with_label("");
145 m_widgetLabel = GTK_BIN(m_widgetCheckbox)->child;
146 m_widget = m_widgetCheckbox;
147 }
148 SetLabel( label );
149
150 g_signal_connect (m_widgetCheckbox, "toggled",
151 G_CALLBACK (gtk_checkbox_toggled_callback), this);
152
153 m_parent->DoAddChild( this );
154
155 PostCreation(size);
156
157 return true;
158}
159
160void wxCheckBox::SetValue( bool state )
161{
162 wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") );
163
164 if (state == GetValue())
165 return;
166
167 m_blockEvent = true;
168
169 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
170
171 m_blockEvent = false;
172}
173
174bool wxCheckBox::GetValue() const
175{
176 wxCHECK_MSG( m_widgetCheckbox != NULL, false, wxT("invalid checkbox") );
177
178 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox));
179}
180
181void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
182{
183 SetValue(state != wxCHK_UNCHECKED);
184 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox),
185 state == wxCHK_UNDETERMINED);
186}
187
188wxCheckBoxState wxCheckBox::DoGet3StateValue() const
189{
190 if (gtk_toggle_button_get_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox)))
191 {
192 return wxCHK_UNDETERMINED;
193 }
194 else
195 {
196 return GetValue() ? wxCHK_CHECKED : wxCHK_UNCHECKED;
197 }
198}
199
200void wxCheckBox::SetLabel( const wxString& label )
201{
202 wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") );
203
204 // save the label inside m_label in case user calls GetLabel() later
205 wxControl::SetLabel(label);
206
207 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel), label);
208}
209
210bool wxCheckBox::Enable( bool enable )
211{
212 if ( !wxControl::Enable( enable ) )
213 return false;
214
215 gtk_widget_set_sensitive( m_widgetLabel, enable );
216
217 return true;
218}
219
220void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle *style)
221{
222 gtk_widget_modify_style(m_widgetCheckbox, style);
223 gtk_widget_modify_style(m_widgetLabel, style);
224}
225
226GdkWindow *wxCheckBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
227{
228 return GTK_BUTTON(m_widgetCheckbox)->event_window;
229}
230
231wxSize wxCheckBox::DoGetBestSize() const
232{
233 return wxControl::DoGetBestSize();
234}
235
236// static
237wxVisualAttributes
238wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
239{
240 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new);
241}
242
243#endif