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