]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/checkbox.cpp
f7d4fa484076438abc481a3fba0f31aff362fa57
[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 #if wxUSE_CHECKBOX
18
19 #include "gdk/gdk.h"
20 #include "gtk/gtk.h"
21
22 //-----------------------------------------------------------------------------
23 // idle system
24 //-----------------------------------------------------------------------------
25
26 extern void wxapp_install_idle_handler();
27 extern bool g_isIdle;
28
29 //-----------------------------------------------------------------------------
30 // data
31 //-----------------------------------------------------------------------------
32
33 extern bool g_blockEventsOnDrag;
34
35 //-----------------------------------------------------------------------------
36 // "clicked"
37 //-----------------------------------------------------------------------------
38
39 static void gtk_checkbox_clicked_callback( GtkWidget *WXUNUSED(widget), wxCheckBox *cb )
40 {
41 if (g_isIdle) wxapp_install_idle_handler();
42
43 if (!cb->m_hasVMT) return;
44
45 if (cb->m_blockFirstEvent)
46 {
47 cb->m_blockFirstEvent = FALSE;
48 return;
49 }
50
51 if (g_blockEventsOnDrag) return;
52
53 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
54 event.SetInt( cb->GetValue() );
55 event.SetEventObject(cb);
56 cb->GetEventHandler()->ProcessEvent(event);
57 }
58
59 //-----------------------------------------------------------------------------
60 // wxCheckBox
61 //-----------------------------------------------------------------------------
62
63 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
64
65 wxCheckBox::wxCheckBox()
66 {
67 }
68
69 bool wxCheckBox::Create(wxWindow *parent,
70 wxWindowID id,
71 const wxString &label,
72 const wxPoint &pos,
73 const wxSize &size,
74 long style,
75 const wxValidator& validator,
76 const wxString &name )
77 {
78 m_needParent = TRUE;
79 m_acceptsFocus = TRUE;
80
81 PreCreation( parent, id, pos, size, style, name );
82
83 m_blockFirstEvent = FALSE;
84
85 SetValidator( validator );
86 wxControl::SetLabel( label );
87
88 if ( style & wxALIGN_RIGHT )
89 {
90 // VZ: as I don't know a way to create a right aligned checkbox with
91 // GTK we will create a checkbox without label and a label at the
92 // left of it
93 m_widgetCheckbox = gtk_check_button_new();
94
95 m_widgetLabel = gtk_label_new(m_label.mbc_str());
96 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
97
98 m_widget = gtk_hbox_new(FALSE, 0);
99 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3);
100 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3);
101
102 // VZ: why do I have to do this to make them appear?
103 gtk_widget_show( m_widgetLabel );
104 gtk_widget_show( m_widgetCheckbox );
105 }
106 else
107 {
108 m_widgetCheckbox = gtk_check_button_new_with_label( m_label.mbc_str() );
109 m_widgetLabel = GTK_BUTTON( m_widgetCheckbox )->child;
110 m_widget = m_widgetCheckbox;
111 }
112
113 wxSize newSize(size);
114 if (newSize.x == -1)
115 {
116 newSize.x = 25 + gdk_string_measure( m_widgetCheckbox->style->font,
117 m_label.mbc_str() );
118 }
119 if (newSize.y == -1)
120 newSize.y = 26;
121
122 SetSize( newSize.x, newSize.y );
123
124 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
125 "clicked",
126 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
127 (gpointer *)this );
128
129 m_parent->DoAddChild( 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 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 #endif