]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/checkbox.cpp
fixing focus, fixes #9985
[wxWidgets.git] / src / gtk1 / checkbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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/gtk1/private.h"
18
19 //-----------------------------------------------------------------------------
20 // idle system
21 //-----------------------------------------------------------------------------
22
23 extern void wxapp_install_idle_handler();
24 extern bool g_isIdle;
25
26 //-----------------------------------------------------------------------------
27 // data
28 //-----------------------------------------------------------------------------
29
30 extern bool g_blockEventsOnDrag;
31 extern wxCursor g_globalCursor;
32 extern wxWindowGTK *g_delayedFocus;
33
34 //-----------------------------------------------------------------------------
35 // "clicked"
36 //-----------------------------------------------------------------------------
37
38 extern "C" {
39 static void gtk_checkbox_toggled_callback(GtkWidget *WXUNUSED(widget),
40 wxCheckBox *cb)
41 {
42 if (g_isIdle) wxapp_install_idle_handler();
43
44 if (!cb->m_hasVMT) return;
45
46 if (g_blockEventsOnDrag) return;
47
48 if (cb->m_blockEvent) return;
49
50 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
51 event.SetInt(cb->GetValue());
52 event.SetEventObject(cb);
53 cb->HandleWindowEvent(event);
54 }
55 }
56
57 //-----------------------------------------------------------------------------
58 // wxCheckBox
59 //-----------------------------------------------------------------------------
60
61 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
62
63 wxCheckBox::wxCheckBox()
64 {
65 }
66
67 bool wxCheckBox::Create(wxWindow *parent,
68 wxWindowID id,
69 const wxString &label,
70 const wxPoint &pos,
71 const wxSize &size,
72 long style,
73 const wxValidator& validator,
74 const wxString &name )
75 {
76 m_needParent = true;
77 m_acceptsFocus = true;
78 m_blockEvent = false;
79
80 if (!PreCreation( parent, pos, size ) ||
81 !CreateBase( parent, id, pos, size, style, validator, name ))
82 {
83 wxFAIL_MSG( wxT("wxCheckBox creation failed") );
84 return false;
85 }
86
87 wxASSERT_MSG( (style & wxCHK_ALLOW_3RD_STATE_FOR_USER) == 0 ||
88 (style & wxCHK_3STATE) != 0,
89 wxT("Using wxCHK_ALLOW_3RD_STATE_FOR_USER")
90 wxT(" style flag for a 2-state checkbox is useless") );
91
92 if ( style & wxALIGN_RIGHT )
93 {
94 // VZ: as I don't know a way to create a right aligned checkbox with
95 // GTK we will create a checkbox without label and a label at the
96 // left of it
97 m_widgetCheckbox = gtk_check_button_new();
98
99 m_widgetLabel = gtk_label_new("");
100 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
101
102 m_widget = gtk_hbox_new(FALSE, 0);
103 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3);
104 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3);
105
106 gtk_widget_show( m_widgetLabel );
107 gtk_widget_show( m_widgetCheckbox );
108 }
109 else
110 {
111 m_widgetCheckbox = gtk_check_button_new_with_label("");
112 m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox );
113 m_widget = m_widgetCheckbox;
114 }
115 SetLabel( label );
116
117 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
118 "toggled",
119 GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback),
120 (gpointer *)this );
121
122 m_parent->DoAddChild( this );
123
124 PostCreation(size);
125
126 return true;
127 }
128
129 void wxCheckBox::SetValue( bool state )
130 {
131 wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") );
132
133 if (state == GetValue())
134 return;
135
136 m_blockEvent = true;
137
138 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
139
140 m_blockEvent = false;
141 }
142
143 bool wxCheckBox::GetValue() const
144 {
145 wxCHECK_MSG( m_widgetCheckbox != NULL, false, wxT("invalid checkbox") );
146
147 return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active;
148 }
149
150 void wxCheckBox::SetLabel( const wxString& label )
151 {
152 wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") );
153
154 GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel), label);
155 }
156
157 bool wxCheckBox::Enable( bool enable )
158 {
159 if ( !wxControl::Enable( enable ) )
160 return false;
161
162 gtk_widget_set_sensitive( m_widgetLabel, enable );
163
164 return true;
165 }
166
167 void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle *style)
168 {
169 gtk_widget_modify_style(m_widgetCheckbox, style);
170 gtk_widget_modify_style(m_widgetLabel, style);
171 }
172
173 bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window )
174 {
175 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
176 }
177
178 void wxCheckBox::OnInternalIdle()
179 {
180 wxCursor cursor = m_cursor;
181 if (g_globalCursor.Ok()) cursor = g_globalCursor;
182
183 GdkWindow *event_window = TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox);
184 if ( event_window && cursor.Ok() )
185 {
186 /* I now set the cursor the anew in every OnInternalIdle call
187 as setting the cursor in a parent window also effects the
188 windows above so that checking for the current cursor is
189 not possible. */
190
191 gdk_window_set_cursor( event_window, cursor.GetCursor() );
192 }
193
194 if (g_delayedFocus == this)
195 {
196 if (GTK_WIDGET_REALIZED(m_widget))
197 {
198 gtk_widget_grab_focus( m_widget );
199 g_delayedFocus = NULL;
200 }
201 }
202
203 if (wxUpdateUIEvent::CanUpdate(this))
204 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
205 }
206
207 wxSize wxCheckBox::DoGetBestSize() const
208 {
209 return wxControl::DoGetBestSize();
210 }
211
212 // static
213 wxVisualAttributes
214 wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
215 {
216 return GetDefaultAttributesFromGTKWidget(gtk_check_button_new);
217 }
218
219 #endif