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