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