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