Had to change the time point, when widgets are
[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 // data
22 //-----------------------------------------------------------------------------
23
24 extern bool g_blockEventsOnDrag;
25
26 //-----------------------------------------------------------------------------
27 // "clicked"
28 //-----------------------------------------------------------------------------
29
30 static void gtk_checkbox_clicked_callback( GtkWidget *WXUNUSED(widget), wxCheckBox *cb )
31 {
32 if (!cb->HasVMT()) return;
33
34 if (cb->m_blockFirstEvent)
35 {
36 cb->m_blockFirstEvent = FALSE;
37 return;
38 }
39
40 if (g_blockEventsOnDrag) return;
41
42 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
43 event.SetInt( cb->GetValue() );
44 event.SetEventObject(cb);
45 cb->GetEventHandler()->ProcessEvent(event);
46 }
47
48 //-----------------------------------------------------------------------------
49 // wxCheckBox
50 //-----------------------------------------------------------------------------
51
52 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
53
54 wxCheckBox::wxCheckBox()
55 {
56 }
57
58 bool wxCheckBox::Create(wxWindow *parent,
59 wxWindowID id,
60 const wxString &label,
61 const wxPoint &pos,
62 const wxSize &size,
63 long style,
64 const wxValidator& validator,
65 const wxString &name )
66 {
67 m_needParent = TRUE;
68 m_acceptsFocus = TRUE;
69
70 PreCreation( parent, id, pos, size, style, name );
71
72 m_blockFirstEvent = FALSE;
73
74 SetValidator( validator );
75 wxControl::SetLabel( label );
76
77 if ( style & wxALIGN_RIGHT )
78 {
79 // VZ: as I don't know a way to create a right aligned checkbox with
80 // GTK we will create a checkbox without label and a label at the
81 // left of it
82 m_widgetCheckbox = gtk_check_button_new();
83
84 m_widgetLabel = gtk_label_new(m_label.mbc_str());
85 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
86
87 m_widget = gtk_hbox_new(FALSE, 0);
88 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3);
89 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3);
90
91 // VZ: why do I have to do this to make them appear?
92 gtk_widget_show( m_widgetLabel );
93 gtk_widget_show( m_widgetCheckbox );
94 }
95 else
96 {
97 m_widgetCheckbox = gtk_check_button_new_with_label( m_label.mbc_str() );
98 m_widgetLabel = GTK_BUTTON( m_widgetCheckbox )->child;
99 m_widget = m_widgetCheckbox;
100 }
101
102 wxSize newSize(size);
103 if (newSize.x == -1)
104 {
105 newSize.x = 25 + gdk_string_measure( m_widgetCheckbox->style->font,
106 m_label.mbc_str() );
107 }
108 if (newSize.y == -1)
109 newSize.y = 26;
110
111 SetSize( newSize.x, newSize.y );
112
113 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
114 "clicked",
115 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
116 (gpointer *)this );
117
118 m_parent->AddChild( this );
119
120 (m_parent->m_insertCallback)( m_parent, this );
121
122 PostCreation();
123
124 SetBackgroundColour( parent->GetBackgroundColour() );
125 SetForegroundColour( parent->GetForegroundColour() );
126 SetFont( parent->GetFont() );
127
128 Show( TRUE );
129
130 return TRUE;
131 }
132
133 void wxCheckBox::SetValue( bool state )
134 {
135 wxCHECK_RET( m_widgetCheckbox != NULL, _T("invalid checkbox") );
136
137 if ( state == GetValue() )
138 return;
139
140 // for compatibility with wxMSW don't send notification when the check box
141 // state is changed programmatically
142 m_blockFirstEvent = TRUE;
143
144 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
145 }
146
147 bool wxCheckBox::GetValue() const
148 {
149 wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, _T("invalid checkbox") );
150
151 return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active;
152 }
153
154 void wxCheckBox::SetLabel( const wxString& label )
155 {
156 wxCHECK_RET( m_widgetLabel != NULL, _T("invalid checkbox") );
157
158 wxControl::SetLabel( label );
159
160 gtk_label_set( GTK_LABEL(m_widgetLabel), GetLabel().mbc_str() );
161 }
162
163 void wxCheckBox::Enable( bool enable )
164 {
165 wxCHECK_RET( m_widgetLabel != NULL, _T("invalid checkbox") );
166
167 wxControl::Enable( enable );
168
169 gtk_widget_set_sensitive( m_widgetLabel, enable );
170 }
171
172 void wxCheckBox::ApplyWidgetStyle()
173 {
174 SetWidgetStyle();
175 gtk_widget_set_style( m_widgetCheckbox, m_widgetStyle );
176 gtk_widget_set_style( m_widgetLabel, m_widgetStyle );
177 }
178