Corrected wxlistBox, wxRadioBox, wxComboBox and
[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 #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 PreCreation( parent, id, pos, size, style, name );
76
77 #if wxUSE_VALIDATORS
78 SetValidator( validator );
79 #endif
80
81 wxControl::SetLabel( label );
82
83 if ( style & wxALIGN_RIGHT )
84 {
85 // VZ: as I don't know a way to create a right aligned checkbox with
86 // GTK we will create a checkbox without label and a label at the
87 // left of it
88 m_widgetCheckbox = gtk_check_button_new();
89
90 m_widgetLabel = gtk_label_new(m_label.mbc_str());
91 gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5);
92
93 m_widget = gtk_hbox_new(FALSE, 0);
94 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3);
95 gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3);
96
97 gtk_widget_show( m_widgetLabel );
98 gtk_widget_show( m_widgetCheckbox );
99 }
100 else
101 {
102 m_widgetCheckbox = gtk_check_button_new_with_label( m_label.mbc_str() );
103 m_widgetLabel = GTK_BUTTON( m_widgetCheckbox )->child;
104 m_widget = m_widgetCheckbox;
105 }
106
107 wxSize newSize(size);
108 if (newSize.x == -1)
109 {
110 newSize.x = 25 + gdk_string_measure( m_widgetCheckbox->style->font,
111 m_label.mbc_str() );
112 }
113 if (newSize.y == -1)
114 newSize.y = 26;
115
116 SetSize( newSize.x, newSize.y );
117
118 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
119 "clicked",
120 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
121 (gpointer *)this );
122
123 m_parent->DoAddChild( this );
124
125 PostCreation();
126
127 SetBackgroundColour( parent->GetBackgroundColour() );
128 SetForegroundColour( parent->GetForegroundColour() );
129 SetFont( parent->GetFont() );
130
131 Show( TRUE );
132
133 return TRUE;
134 }
135
136 void wxCheckBox::SetValue( bool state )
137 {
138 wxCHECK_RET( m_widgetCheckbox != NULL, _T("invalid checkbox") );
139
140 if (state == GetValue())
141 return;
142
143 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widgetCheckbox),
144 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
145 (gpointer *)this );
146
147 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state );
148
149 gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox),
150 "clicked",
151 GTK_SIGNAL_FUNC(gtk_checkbox_clicked_callback),
152 (gpointer *)this );
153 }
154
155 bool wxCheckBox::GetValue() const
156 {
157 wxCHECK_MSG( m_widgetCheckbox != NULL, FALSE, _T("invalid checkbox") );
158
159 return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active;
160 }
161
162 void wxCheckBox::SetLabel( const wxString& label )
163 {
164 wxCHECK_RET( m_widgetLabel != NULL, _T("invalid checkbox") );
165
166 wxControl::SetLabel( label );
167
168 gtk_label_set( GTK_LABEL(m_widgetLabel), GetLabel().mbc_str() );
169 }
170
171 bool wxCheckBox::Enable( bool enable )
172 {
173 if ( !wxControl::Enable( enable ) )
174 return FALSE;
175
176 gtk_widget_set_sensitive( m_widgetLabel, enable );
177
178 return TRUE;
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
188 #endif