]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/checkbox.cpp
wxDC::GetHandle support for wxMSW
[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{
a2053b27 33 if (!cb->m_hasVMT) return;
ff8bfdbb 34
b292e2f5 35 if (g_blockEventsOnDrag) return;
3d257b8d 36
4dcccda6
VS
37 // Transitions for 3state checkbox must be done manually, GTK's checkbox
38 // is 2state with additional "undetermined state" flag which isn't
39 // changed automatically:
40 if (cb->Is3State())
41 {
42 GtkToggleButton *toggle = GTK_TOGGLE_BUTTON(widget);
43
44 if (cb->Is3rdStateAllowedForUser())
45 {
46 // The 3 states cycle like this when clicked:
47 // checked -> undetermined -> unchecked -> checked -> ...
d5027818
PC
48 bool active = gtk_toggle_button_get_active(toggle) != 0;
49 bool inconsistent = gtk_toggle_button_get_inconsistent(toggle) != 0;
4dcccda6 50
c71ab7c1 51 cb->GTKDisableEvents();
3d257b8d 52
4dcccda6
VS
53 if (!active && !inconsistent)
54 {
55 // checked -> undetermined
56 gtk_toggle_button_set_active(toggle, true);
57 gtk_toggle_button_set_inconsistent(toggle, true);
58 }
59 else if (!active && inconsistent)
60 {
61 // undetermined -> unchecked
62 gtk_toggle_button_set_inconsistent(toggle, false);
63 }
64 else if (active && !inconsistent)
65 {
66 // unchecked -> checked
67 // nothing to do
68 }
69 else
70 {
9a83f860 71 wxFAIL_MSG(wxT("3state wxCheckBox in unexpected state!"));
4dcccda6 72 }
3d257b8d 73
c71ab7c1 74 cb->GTKEnableEvents();
4dcccda6
VS
75 }
76 else
77 {
78 // user's action unsets undetermined state:
79 gtk_toggle_button_set_inconsistent(toggle, false);
80 }
81 }
4dcccda6 82
b292e2f5 83 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
4dcccda6 84 event.SetInt(cb->Get3StateValue());
b292e2f5 85 event.SetEventObject(cb);
937013e0 86 cb->HandleWindowEvent(event);
6de97a3b 87}
865bb325 88}
c801d85f 89
e1e955e1
RR
90//-----------------------------------------------------------------------------
91// wxCheckBox
c801d85f
KB
92//-----------------------------------------------------------------------------
93
2cc0e28f 94wxCheckBox::wxCheckBox()
c801d85f 95{
6de97a3b 96}
c801d85f 97
2cc0e28f
VZ
98bool wxCheckBox::Create(wxWindow *parent,
99 wxWindowID id,
100 const wxString &label,
101 const wxPoint &pos,
102 const wxSize &size,
103 long style,
104 const wxValidator& validator,
105 const wxString &name )
c801d85f 106{
f254e242 107 WXValidateStyle( &style );
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
2cc0e28f
VZ
115 if ( style & wxALIGN_RIGHT )
116 {
117 // VZ: as I don't know a way to create a right aligned checkbox with
118 // GTK we will create a checkbox without label and a label at the
119 // left of it
120 m_widgetCheckbox = gtk_check_button_new();
6de97a3b 121
eaafd2f8 122 m_widgetLabel = gtk_label_new("");
2cc0e28f 123 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
ff8bfdbb 124
2cc0e28f
VZ
125 m_widget = gtk_hbox_new(FALSE, 0);
126 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3);
127 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3);
128
2cc0e28f
VZ
129 gtk_widget_show( m_widgetLabel );
130 gtk_widget_show( m_widgetCheckbox );
131 }
132 else
133 {
eaafd2f8 134 m_widgetCheckbox = gtk_check_button_new_with_label("");
385e8575 135 m_widgetLabel = gtk_bin_get_child(GTK_BIN(m_widgetCheckbox));
2cc0e28f
VZ
136 m_widget = m_widgetCheckbox;
137 }
9ff9d30c 138 g_object_ref(m_widget);
eaafd2f8 139 SetLabel( label );
2cc0e28f 140
9fa72bd2
MR
141 g_signal_connect (m_widgetCheckbox, "toggled",
142 G_CALLBACK (gtk_checkbox_toggled_callback), this);
ff8bfdbb 143
f03fc89f 144 m_parent->DoAddChild( this );
ff8bfdbb 145
abdeb9e7 146 PostCreation(size);
ff8bfdbb 147
93763ad5 148 return true;
6de97a3b 149}
c801d85f 150
c71ab7c1
RR
151void wxCheckBox::GTKDisableEvents()
152{
153 g_signal_handlers_block_by_func(m_widgetCheckbox,
154 (gpointer) gtk_checkbox_toggled_callback, this);
155}
156
157void wxCheckBox::GTKEnableEvents()
158{
159 g_signal_handlers_unblock_by_func(m_widgetCheckbox,
160 (gpointer) gtk_checkbox_toggled_callback, this);
161}
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 168 return;
03647350 169
c71ab7c1 170 GTKDisableEvents();
ae0bdb01 171
e343da37 172 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
1db8dc4a 173
c71ab7c1 174 GTKEnableEvents();
6de97a3b 175}
c801d85f 176
83058c58 177bool wxCheckBox::GetValue() const
c801d85f 178{
93763ad5 179 wxCHECK_MSG( m_widgetCheckbox != NULL, false, wxT("invalid checkbox") );
f96aa4d9 180
d5027818 181 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox)) != 0;
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
39bc0347
VZ
207 // save the label inside m_label in case user calls GetLabel() later
208 wxControl::SetLabel(label);
209
b2ff89d6 210 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel), label);
83058c58
RR
211}
212
f03fc89f 213bool wxCheckBox::Enable( bool enable )
d3904ceb 214{
b545684e 215 if (!base_type::Enable(enable))
93763ad5 216 return false;
ff8bfdbb 217
2cc0e28f 218 gtk_widget_set_sensitive( m_widgetLabel, enable );
f03fc89f 219
b545684e 220 if (enable)
ad60f9e7 221 GTKFixSensitivity();
ad60f9e7 222
93763ad5 223 return true;
d3904ceb
RR
224}
225
f40fdaa3 226void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 227{
9dc44eff
PC
228 GTKApplyStyle(m_widgetCheckbox, style);
229 GTKApplyStyle(m_widgetLabel, style);
f96aa4d9
RR
230}
231
ef5c70f9 232GdkWindow *wxCheckBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
2f073eb2 233{
9dc44eff 234 return gtk_button_get_event_window(GTK_BUTTON(m_widgetCheckbox));
2f073eb2
RR
235}
236
9d522606
RD
237// static
238wxVisualAttributes
239wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
240{
241 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new);
242}
243
dcf924a3 244#endif