many changes; major ones:
[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 (g_blockEventsOnDrag) return;
46
47 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, cb->GetId());
48 event.SetInt( cb->GetValue() );
49 event.SetEventObject(cb);
50 cb->GetEventHandler()->ProcessEvent(event);
51 }
52
53 //-----------------------------------------------------------------------------
54 // wxCheckBox
55 //-----------------------------------------------------------------------------
56
57 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox,wxControl)
58
59 wxCheckBox::wxCheckBox()
60 {
61 }
62
63 bool wxCheckBox::Create(wxWindow *parent,
64 wxWindowID id,
65 const wxString &label,
66 const wxPoint &pos,
67 const wxSize &size,
68 long style,
69 const wxValidator& validator,
70 const wxString &name )
71 {
72 m_needParent = TRUE;
73 m_acceptsFocus = TRUE;
74
75 if (!PreCreation( parent, pos, size ) ||
76 !CreateBase( parent, id, pos, size, style, validator, name ))
77 {
78 wxFAIL_MSG( T("wxCheckBox creation failed") );
79 return FALSE;
80 }
81
82 wxControl::SetLabel( label );
83
84 if ( style & wxALIGN_RIGHT )
85 {
86 // VZ: as I don't know a way to create a right aligned checkbox with
87 // GTK we will create a checkbox without label and a label at the
88 // left of it
89 m_widgetCheckbox = gtk_check_button_new();
90
91 m_widgetLabel = gtk_label_new(m_label.mbc_str());
92 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
93
94 m_widget = gtk_hbox_new(FALSE, 0);
95 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3);
96 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3);
97
98 gtk_widget_show( m_widgetLabel );
99 gtk_widget_show( m_widgetCheckbox );
100 }
101 else
102 {
103 m_widgetCheckbox = gtk_check_button_new_with_label( m_label.mbc_str() );
104 m_widgetLabel = GTK_BUTTON( m_widgetCheckbox )->child;
105 m_widget = m_widgetCheckbox;
106 }
107
108 wxSize newSize(size);
109 if (newSize.x == -1)
110 {
111 newSize.x = 25 + gdk_string_measure( m_widgetCheckbox->style->font,
112 m_label.mbc_str() );
113 }
114 if (newSize.y == -1)
115 newSize.y = 26;
116
117 SetSize( newSize.x, newSize.y );
118
119 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
120 "clicked",
121 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
122 (gpointer *)this );
123
124 m_parent->DoAddChild( this );
125
126 PostCreation();
127
128 SetBackgroundColour( parent->GetBackgroundColour() );
129 SetForegroundColour( parent->GetForegroundColour() );
130 SetFont( parent->GetFont() );
131
132 Show( TRUE );
133
134 return TRUE;
135 }
136
137 void wxCheckBox::SetValue( bool state )
138 {
139 wxCHECK_RET( m_widgetCheckbox != NULL, T("invalid checkbox") );
140
141 if (state == GetValue())
142 return;
143
144 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widgetCheckbox),
145 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
146 (gpointer *)this );
147
148 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
149
150 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
151 "clicked",
152 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
153 (gpointer *)this );
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