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