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