]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/checkbox.cpp
Use GetFont not m_font for GetCharHeight
[wxWidgets.git] / src / gtk / checkbox.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: checkbox.cpp
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
1e6feb95 13#include "wx/defs.h"
c801d85f 14
dcf924a3
RR
15#if wxUSE_CHECKBOX
16
1e6feb95
VZ
17#include "wx/checkbox.h"
18
9e691f46 19#include "wx/gtk/private.h"
83624f79 20
acfd422a
RR
21//-----------------------------------------------------------------------------
22// idle system
23//-----------------------------------------------------------------------------
24
25extern void wxapp_install_idle_handler();
26extern bool g_isIdle;
27
66bd6b93
RR
28//-----------------------------------------------------------------------------
29// data
30//-----------------------------------------------------------------------------
31
d7fa7eaa
RR
32extern bool g_blockEventsOnDrag;
33extern wxCursor g_globalCursor;
34extern wxWindowGTK *g_delayedFocus;
66bd6b93 35
c801d85f 36//-----------------------------------------------------------------------------
e1e955e1 37// "clicked"
c801d85f
KB
38//-----------------------------------------------------------------------------
39
865bb325 40extern "C" {
4dcccda6 41static void gtk_checkbox_toggled_callback(GtkWidget *widget, wxCheckBox *cb)
c801d85f 42{
acfd422a
RR
43 if (g_isIdle) wxapp_install_idle_handler();
44
a2053b27 45 if (!cb->m_hasVMT) return;
ff8bfdbb 46
b292e2f5 47 if (g_blockEventsOnDrag) return;
9864c56d
RR
48
49 if (cb->m_blockEvent) return;
ff8bfdbb 50
4dcccda6
VS
51#ifdef __WXGTK20__
52 // Transitions for 3state checkbox must be done manually, GTK's checkbox
53 // is 2state with additional "undetermined state" flag which isn't
54 // changed automatically:
55 if (cb->Is3State())
56 {
57 GtkToggleButton *toggle = GTK_TOGGLE_BUTTON(widget);
58
59 if (cb->Is3rdStateAllowedForUser())
60 {
61 // The 3 states cycle like this when clicked:
62 // checked -> undetermined -> unchecked -> checked -> ...
63 bool active = gtk_toggle_button_get_active(toggle);
64 bool inconsistent = gtk_toggle_button_get_inconsistent(toggle);
65
66 cb->m_blockEvent = true;
67
68 if (!active && !inconsistent)
69 {
70 // checked -> undetermined
71 gtk_toggle_button_set_active(toggle, true);
72 gtk_toggle_button_set_inconsistent(toggle, true);
73 }
74 else if (!active && inconsistent)
75 {
76 // undetermined -> unchecked
77 gtk_toggle_button_set_inconsistent(toggle, false);
78 }
79 else if (active && !inconsistent)
80 {
81 // unchecked -> checked
82 // nothing to do
83 }
84 else
85 {
86 wxFAIL_MSG(_T("3state wxCheckBox in unexpected state!"));
87 }
88
89 cb->m_blockEvent = false;
90 }
91 else
92 {
93 // user's action unsets undetermined state:
94 gtk_toggle_button_set_inconsistent(toggle, false);
95 }
96 }
97#endif
98
b292e2f5 99 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
4dcccda6
VS
100#ifdef __WXGTK20__
101 event.SetInt(cb->Get3StateValue());
102#else
103 event.SetInt(cb->GetValue());
104#endif
b292e2f5
RR
105 event.SetEventObject(cb);
106 cb->GetEventHandler()->ProcessEvent(event);
6de97a3b 107}
865bb325 108}
c801d85f 109
e1e955e1
RR
110//-----------------------------------------------------------------------------
111// wxCheckBox
c801d85f
KB
112//-----------------------------------------------------------------------------
113
114IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
115
2cc0e28f 116wxCheckBox::wxCheckBox()
c801d85f 117{
6de97a3b 118}
c801d85f 119
2cc0e28f
VZ
120bool wxCheckBox::Create(wxWindow *parent,
121 wxWindowID id,
122 const wxString &label,
123 const wxPoint &pos,
124 const wxSize &size,
125 long style,
126 const wxValidator& validator,
127 const wxString &name )
c801d85f 128{
b292e2f5
RR
129 m_needParent = TRUE;
130 m_acceptsFocus = TRUE;
9864c56d 131 m_blockEvent = FALSE;
ff8bfdbb 132
4dcaf11a
RR
133 if (!PreCreation( parent, pos, size ) ||
134 !CreateBase( parent, id, pos, size, style, validator, name ))
135 {
223d09f6 136 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
1db8dc4a 137 return FALSE;
4dcaf11a 138 }
ce4169a4 139
4dcccda6
VS
140 wxASSERT_MSG( (style & wxCHK_ALLOW_3RD_STATE_FOR_USER) == 0 ||
141 (style & wxCHK_3STATE) != 0,
142 wxT("Using wxCHK_ALLOW_3RD_STATE_FOR_USER")
143 wxT(" style flag for a 2-state checkbox is useless") );
144
2cc0e28f
VZ
145 if ( style & wxALIGN_RIGHT )
146 {
147 // VZ: as I don't know a way to create a right aligned checkbox with
148 // GTK we will create a checkbox without label and a label at the
149 // left of it
150 m_widgetCheckbox = gtk_check_button_new();
6de97a3b 151
eaafd2f8 152 m_widgetLabel = gtk_label_new("");
2cc0e28f 153 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
ff8bfdbb 154
2cc0e28f
VZ
155 m_widget = gtk_hbox_new(FALSE, 0);
156 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3);
157 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3);
158
2cc0e28f
VZ
159 gtk_widget_show( m_widgetLabel );
160 gtk_widget_show( m_widgetCheckbox );
161 }
162 else
163 {
eaafd2f8 164 m_widgetCheckbox = gtk_check_button_new_with_label("");
9e691f46 165 m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox );
2cc0e28f
VZ
166 m_widget = m_widgetCheckbox;
167 }
eaafd2f8 168 SetLabel( label );
2cc0e28f 169
2cc0e28f 170 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
4dcccda6
VS
171 "toggled",
172 GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback),
2cc0e28f 173 (gpointer *)this );
ff8bfdbb 174
f03fc89f 175 m_parent->DoAddChild( this );
ff8bfdbb 176
abdeb9e7 177 PostCreation(size);
ff8bfdbb 178
b292e2f5 179 return TRUE;
6de97a3b 180}
c801d85f 181
debe6624 182void wxCheckBox::SetValue( bool state )
c801d85f 183{
223d09f6 184 wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") );
ff8bfdbb 185
953704c1 186 if (state == GetValue())
8bf45ed1
VZ
187 return;
188
9864c56d 189 m_blockEvent = TRUE;
ae0bdb01 190
2cc0e28f 191 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
1db8dc4a 192
9864c56d 193 m_blockEvent = FALSE;
6de97a3b 194}
c801d85f 195
83058c58 196bool wxCheckBox::GetValue() const
c801d85f 197{
223d09f6 198 wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, wxT("invalid checkbox") );
f96aa4d9 199
4dcccda6
VS
200#ifdef __WXGTK20__
201 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widgetCheckbox));
202#else
2cc0e28f 203 return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active;
4dcccda6 204#endif
6de97a3b 205}
c801d85f 206
4dcccda6
VS
207#ifdef __WXGTK20__
208void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
209{
210 SetValue(state != wxCHK_UNCHECKED);
211 gtk_toggle_button_set_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox),
212 state == wxCHK_UNDETERMINED);
213}
214
215wxCheckBoxState wxCheckBox::DoGet3StateValue() const
216{
217 if (gtk_toggle_button_get_inconsistent(GTK_TOGGLE_BUTTON(m_widgetCheckbox)))
218 {
219 return wxCHK_UNDETERMINED;
220 }
221 else
222 {
223 return GetValue() ? wxCHK_CHECKED : wxCHK_UNCHECKED;
224 }
225}
226#endif
227
83058c58
RR
228void wxCheckBox::SetLabel( const wxString& label )
229{
223d09f6 230 wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") );
f96aa4d9 231
b292e2f5 232 wxControl::SetLabel( label );
ff8bfdbb 233
eaafd2f8
VS
234#ifdef __WXGTK20__
235 wxString label2 = PrepareLabelMnemonics( label );
236 gtk_label_set_text_with_mnemonic( GTK_LABEL(m_widgetLabel), wxGTK_CONV( label2 ) );
237#else
fab591c5 238 gtk_label_set( GTK_LABEL(m_widgetLabel), wxGTK_CONV( GetLabel() ) );
eaafd2f8 239#endif
83058c58
RR
240}
241
f03fc89f 242bool wxCheckBox::Enable( bool enable )
d3904ceb 243{
f03fc89f
VZ
244 if ( !wxControl::Enable( enable ) )
245 return FALSE;
ff8bfdbb 246
2cc0e28f 247 gtk_widget_set_sensitive( m_widgetLabel, enable );
f03fc89f
VZ
248
249 return TRUE;
d3904ceb
RR
250}
251
f40fdaa3 252void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 253{
f40fdaa3
VS
254 gtk_widget_modify_style(m_widgetCheckbox, style);
255 gtk_widget_modify_style(m_widgetLabel, style);
f96aa4d9
RR
256}
257
2f073eb2
RR
258bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window )
259{
9e691f46 260 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
2f073eb2
RR
261}
262
263void wxCheckBox::OnInternalIdle()
264{
265 wxCursor cursor = m_cursor;
266 if (g_globalCursor.Ok()) cursor = g_globalCursor;
267
9e691f46
VZ
268 GdkWindow *event_window = TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox);
269 if ( event_window && cursor.Ok() )
2f073eb2
RR
270 {
271 /* I now set the cursor the anew in every OnInternalIdle call
1db8dc4a
VZ
272 as setting the cursor in a parent window also effects the
273 windows above so that checking for the current cursor is
274 not possible. */
275
9e691f46 276 gdk_window_set_cursor( event_window, cursor.GetCursor() );
2f073eb2
RR
277 }
278
d7fa7eaa
RR
279 if (g_delayedFocus == this)
280 {
281 if (GTK_WIDGET_REALIZED(m_widget))
282 {
283 gtk_widget_grab_focus( m_widget );
284 g_delayedFocus = NULL;
285 }
286 }
287
e39af974
JS
288 if (wxUpdateUIEvent::CanUpdate(this))
289 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2f073eb2
RR
290}
291
f68586e5
VZ
292wxSize wxCheckBox::DoGetBestSize() const
293{
db434467 294 return wxControl::DoGetBestSize();
f68586e5
VZ
295}
296
9d522606
RD
297// static
298wxVisualAttributes
299wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
300{
301 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new);
302}
303
dcf924a3 304#endif