New idle handling. Only that.
[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/checkbox.h"
16
17 #include "gdk/gdk.h"
18 #include "gtk/gtk.h"
19
20 //-----------------------------------------------------------------------------
21 // idle system
22 //-----------------------------------------------------------------------------
23
24 extern void wxapp_install_idle_handler();
25 extern bool g_isIdle;
26
27 //-----------------------------------------------------------------------------
28 // data
29 //-----------------------------------------------------------------------------
30
31 extern bool g_blockEventsOnDrag;
32
33 //-----------------------------------------------------------------------------
34 // "clicked"
35 //-----------------------------------------------------------------------------
36
37 static void gtk_checkbox_clicked_callback( GtkWidget *WXUNUSED(widget), wxCheckBox *cb )
38 {
39 if (g_isIdle) wxapp_install_idle_handler();
40
41 if (!cb->HasVMT()) return;
42
43 if (cb->m_blockFirstEvent)
44 {
45 cb->m_blockFirstEvent = FALSE;
46 return;
47 }
48
49 if (g_blockEventsOnDrag) 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 // 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
79 PreCreation( parent, id, pos, size, style, name );
80
81 m_blockFirstEvent = FALSE;
82
83 SetValidator( validator );
84 wxControl::SetLabel( label );
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(m_label.mbc_str());
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 // VZ: why do I have to do this to make them appear?
101 gtk_widget_show( m_widgetLabel );
102 gtk_widget_show( m_widgetCheckbox );
103 }
104 else
105 {
106 m_widgetCheckbox = gtk_check_button_new_with_label( m_label.mbc_str() );
107 m_widgetLabel = GTK_BUTTON( m_widgetCheckbox )->child;
108 m_widget = m_widgetCheckbox;
109 }
110
111 wxSize newSize(size);
112 if (newSize.x == -1)
113 {
114 newSize.x = 25 + gdk_string_measure( m_widgetCheckbox->style->font,
115 m_label.mbc_str() );
116 }
117 if (newSize.y == -1)
118 newSize.y = 26;
119
120 SetSize( newSize.x, newSize.y );
121
122 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
123 "clicked",
124 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
125 (gpointer *)this );
126
127 m_parent->AddChild( this );
128
129 (m_parent->m_insertCallback)( m_parent, this );
130
131 PostCreation();
132
133 SetBackgroundColour( parent->GetBackgroundColour() );
134 SetForegroundColour( parent->GetForegroundColour() );
135 SetFont( parent->GetFont() );
136
137 Show( TRUE );
138
139 return TRUE;
140 }
141
142 void wxCheckBox::SetValue( bool state )
143 {
144 wxCHECK_RET( m_widgetCheckbox != NULL, _T("invalid checkbox") );
145
146 if ( state == GetValue() )
147 return;
148
149 // for compatibility with wxMSW don't send notification when the check box
150 // state is changed programmatically
151 m_blockFirstEvent = TRUE;
152
153 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
154 }
155
156 bool wxCheckBox::GetValue() const
157 {
158 wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, _T("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, _T("invalid checkbox") );
166
167 wxControl::SetLabel( label );
168
169 gtk_label_set( GTK_LABEL(m_widgetLabel), GetLabel().mbc_str() );
170 }
171
172 void wxCheckBox::Enable( bool enable )
173 {
174 wxCHECK_RET( m_widgetLabel != NULL, _T("invalid checkbox") );
175
176 wxControl::Enable( enable );
177
178 gtk_widget_set_sensitive( m_widgetLabel, enable );
179 }
180
181 void wxCheckBox::ApplyWidgetStyle()
182 {
183 SetWidgetStyle();
184 gtk_widget_set_style( m_widgetCheckbox, m_widgetStyle );
185 gtk_widget_set_style( m_widgetLabel, m_widgetStyle );
186 }
187