The checkbox doesn't eat the first "toggled" event if SetValue(FALSE) was
[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 #ifdef __GNUG__
12 #pragma implementation "checkbox.h"
13 #endif
14
15 #include "wx/checkbox.h"
16
17 //-----------------------------------------------------------------------------
18 // data
19 //-----------------------------------------------------------------------------
20
21 extern bool g_blockEventsOnDrag;
22
23 //-----------------------------------------------------------------------------
24 // "clicked"
25 //-----------------------------------------------------------------------------
26
27 static void gtk_checkbox_clicked_callback( GtkWidget *WXUNUSED(widget), wxCheckBox *cb )
28 {
29 if (!cb->HasVMT()) return;
30
31 if (cb->m_blockFirstEvent)
32 {
33 cb->m_blockFirstEvent = FALSE;
34 return;
35 }
36
37 if (g_blockEventsOnDrag) return;
38
39 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
40 event.SetInt( cb->GetValue() );
41 event.SetEventObject(cb);
42 cb->GetEventHandler()->ProcessEvent(event);
43 }
44
45 //-----------------------------------------------------------------------------
46 // wxCheckBox
47 //-----------------------------------------------------------------------------
48
49 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
50
51 wxCheckBox::wxCheckBox(void)
52 {
53 }
54
55 bool wxCheckBox::Create( wxWindow *parent, wxWindowID id, const wxString &label,
56 const wxPoint &pos, const wxSize &size,
57 long style, const wxValidator& validator, const wxString &name )
58 {
59 m_needParent = TRUE;
60 m_acceptsFocus = TRUE;
61
62 PreCreation( parent, id, pos, size, style, name );
63
64 SetValidator( validator );
65
66 m_widget = gtk_check_button_new_with_label( m_label );
67
68 m_blockFirstEvent = FALSE;
69
70 wxSize newSize = size;
71 if (newSize.x == -1) newSize.x = 25+gdk_string_measure( m_widget->style->font, label );
72 if (newSize.y == -1) newSize.y = 26;
73 SetSize( newSize.x, newSize.y );
74
75 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
76 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback), (gpointer*)this );
77
78 m_parent->AddChild( this );
79
80 (m_parent->m_insertCallback)( m_parent, this );
81
82 PostCreation();
83
84 gtk_widget_realize( GTK_BUTTON( m_widget )->child );
85
86 SetLabel( label );
87
88 SetBackgroundColour( parent->GetBackgroundColour() );
89 SetForegroundColour( parent->GetForegroundColour() );
90
91 Show( TRUE );
92
93 return TRUE;
94 }
95
96 void wxCheckBox::SetValue( bool state )
97 {
98 wxCHECK_RET( m_widget != NULL, "invalid checkbox" );
99
100 if ( state == GetValue() )
101 return;
102
103 // for compatibility with wxMSW don't send notification when the check box
104 // state is changed programmatically
105 m_blockFirstEvent = TRUE;
106
107 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widget), state );
108 }
109
110 bool wxCheckBox::GetValue() const
111 {
112 wxCHECK_MSG( m_widget != NULL, FALSE, "invalid checkbox" );
113
114 return GTK_TOGGLE_BUTTON(m_widget)->active;
115 }
116
117 void wxCheckBox::SetLabel( const wxString& label )
118 {
119 wxCHECK_RET( m_widget != NULL, "invalid checkbox" );
120
121 wxControl::SetLabel( label );
122
123 gtk_label_set( GTK_LABEL( GTK_BUTTON(m_widget)->child ), GetLabel() );
124 }
125
126 void wxCheckBox::Enable( bool enable )
127 {
128 wxCHECK_RET( m_widget != NULL, "invalid checkbox" );
129
130 wxControl::Enable( enable );
131
132 gtk_widget_set_sensitive( GTK_BUTTON(m_widget)->child, enable );
133 }
134
135 void wxCheckBox::ApplyWidgetStyle()
136 {
137 SetWidgetStyle();
138 gtk_widget_set_style( m_widget, m_widgetStyle );
139 gtk_widget_set_style( GTK_BUTTON(m_widget)->child, m_widgetStyle );
140 }
141
142