Lots of updates for Unicode and GTK 2.0 support.
[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
11 #ifdef __GNUG__
12 #pragma implementation "checkbox.h"
13 #endif
14
15 #include "wx/defs.h"
16
17 #if wxUSE_CHECKBOX
18
19 #include "wx/checkbox.h"
20
21 #include "wx/gtk/private.h"
22
23 //-----------------------------------------------------------------------------
24 // idle system
25 //-----------------------------------------------------------------------------
26
27 extern void wxapp_install_idle_handler();
28 extern bool g_isIdle;
29
30 //-----------------------------------------------------------------------------
31 // data
32 //-----------------------------------------------------------------------------
33
34 extern bool g_blockEventsOnDrag;
35 extern wxCursor g_globalCursor;
36 extern wxWindowGTK *g_delayedFocus;
37
38 //-----------------------------------------------------------------------------
39 // "clicked"
40 //-----------------------------------------------------------------------------
41
42 static void gtk_checkbox_clicked_callback( GtkWidget *WXUNUSED(widget), wxCheckBox *cb )
43 {
44 if (g_isIdle) wxapp_install_idle_handler();
45
46 if (!cb->m_hasVMT) return;
47
48 if (g_blockEventsOnDrag) return;
49
50 if (cb->m_blockEvent) return;
51
52 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
53 event.SetInt( cb->GetValue() );
54 event.SetEventObject(cb);
55 cb->GetEventHandler()->ProcessEvent(event);
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 wxControl::SetLabel( label );
89
90 if ( style & wxALIGN_RIGHT )
91 {
92 // VZ: as I don't know a way to create a right aligned checkbox with
93 // GTK we will create a checkbox without label and a label at the
94 // left of it
95 m_widgetCheckbox = gtk_check_button_new();
96
97 m_widgetLabel = gtk_label_new( wxGTK_CONV( m_label ) );
98 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
99
100 m_widget = gtk_hbox_new(FALSE, 0);
101 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3);
102 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3);
103
104 gtk_widget_show( m_widgetLabel );
105 gtk_widget_show( m_widgetCheckbox );
106 }
107 else
108 {
109 m_widgetCheckbox = gtk_check_button_new_with_label( wxGTK_CONV( m_label ) );
110 m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox );
111 m_widget = m_widgetCheckbox;
112 }
113
114 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
115 "clicked",
116 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
117 (gpointer *)this );
118
119 m_parent->DoAddChild( this );
120
121 PostCreation();
122
123 SetFont( parent->GetFont() );
124
125 wxSize size_best( DoGetBestSize() );
126 wxSize new_size( size );
127 if (new_size.x == -1)
128 new_size.x = size_best.x;
129 if (new_size.y == -1)
130 new_size.y = size_best.y;
131 if ((new_size.x != size.x) || (new_size.y != size.y))
132 SetSize( new_size.x, new_size.y );
133
134 SetBackgroundColour( parent->GetBackgroundColour() );
135 SetForegroundColour( parent->GetForegroundColour() );
136
137 Show( TRUE );
138
139 return TRUE;
140 }
141
142 void wxCheckBox::SetValue( bool state )
143 {
144 wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") );
145
146 if (state == GetValue())
147 return;
148
149 m_blockEvent = TRUE;
150
151 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
152
153 m_blockEvent = FALSE;
154 }
155
156 bool wxCheckBox::GetValue() const
157 {
158 wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, wxT("invalid checkbox") );
159
160 return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active;
161 }
162
163 void wxCheckBox::SetLabel( const wxString& label )
164 {
165 wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") );
166
167 wxControl::SetLabel( label );
168
169 gtk_label_set( GTK_LABEL(m_widgetLabel), wxGTK_CONV( GetLabel() ) );
170 }
171
172 bool wxCheckBox::Enable( bool enable )
173 {
174 if ( !wxControl::Enable( enable ) )
175 return FALSE;
176
177 gtk_widget_set_sensitive( m_widgetLabel, enable );
178
179 return TRUE;
180 }
181
182 void wxCheckBox::ApplyWidgetStyle()
183 {
184 SetWidgetStyle();
185 gtk_widget_set_style( m_widgetCheckbox, m_widgetStyle );
186 gtk_widget_set_style( m_widgetLabel, m_widgetStyle );
187 }
188
189 bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window )
190 {
191 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
192 }
193
194 void wxCheckBox::OnInternalIdle()
195 {
196 wxCursor cursor = m_cursor;
197 if (g_globalCursor.Ok()) cursor = g_globalCursor;
198
199 GdkWindow *event_window = TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox);
200 if ( event_window && cursor.Ok() )
201 {
202 /* I now set the cursor the anew in every OnInternalIdle call
203 as setting the cursor in a parent window also effects the
204 windows above so that checking for the current cursor is
205 not possible. */
206
207 gdk_window_set_cursor( event_window, cursor.GetCursor() );
208 }
209
210 if (g_delayedFocus == this)
211 {
212 if (GTK_WIDGET_REALIZED(m_widget))
213 {
214 gtk_widget_grab_focus( m_widget );
215 g_delayedFocus = NULL;
216 }
217 }
218
219 UpdateWindowUI();
220 }
221
222 wxSize wxCheckBox::DoGetBestSize() const
223 {
224 return wxControl::DoGetBestSize();
225 }
226
227 #endif