]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
93763ad5 | 2 | // Name: src/gtk1/checkbox.cpp |
c801d85f KB |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
01111366 | 5 | // Copyright: (c) 1998 Robert Roebling |
65571936 | 6 | // Licence: wxWindows licence |
c801d85f KB |
7 | ///////////////////////////////////////////////////////////////////////////// |
8 | ||
14f355c2 VS |
9 | // For compilers that support precompilation, includes "wx.h". |
10 | #include "wx/wxprec.h" | |
11 | ||
dcf924a3 RR |
12 | #if wxUSE_CHECKBOX |
13 | ||
1e6feb95 VZ |
14 | #include "wx/checkbox.h" |
15 | ||
3cbab641 | 16 | #include "wx/gtk1/private.h" |
83624f79 | 17 | |
acfd422a RR |
18 | //----------------------------------------------------------------------------- |
19 | // idle system | |
20 | //----------------------------------------------------------------------------- | |
21 | ||
22 | extern void wxapp_install_idle_handler(); | |
23 | extern bool g_isIdle; | |
24 | ||
66bd6b93 RR |
25 | //----------------------------------------------------------------------------- |
26 | // data | |
27 | //----------------------------------------------------------------------------- | |
28 | ||
d7fa7eaa RR |
29 | extern bool g_blockEventsOnDrag; |
30 | extern wxCursor g_globalCursor; | |
31 | extern wxWindowGTK *g_delayedFocus; | |
66bd6b93 | 32 | |
c801d85f | 33 | //----------------------------------------------------------------------------- |
e1e955e1 | 34 | // "clicked" |
c801d85f KB |
35 | //----------------------------------------------------------------------------- |
36 | ||
865bb325 | 37 | extern "C" { |
89954433 VZ |
38 | static void gtk_checkbox_toggled_callback(GtkWidget *WXUNUSED(widget), |
39 | wxCheckBox *cb) | |
c801d85f | 40 | { |
acfd422a RR |
41 | if (g_isIdle) wxapp_install_idle_handler(); |
42 | ||
a2053b27 | 43 | if (!cb->m_hasVMT) return; |
ff8bfdbb | 44 | |
b292e2f5 | 45 | if (g_blockEventsOnDrag) return; |
3d257b8d | 46 | |
9864c56d | 47 | if (cb->m_blockEvent) return; |
ff8bfdbb | 48 | |
ce7fe42e | 49 | wxCommandEvent event(wxEVT_CHECKBOX, cb->GetId()); |
4dcccda6 | 50 | event.SetInt(cb->GetValue()); |
b292e2f5 | 51 | event.SetEventObject(cb); |
937013e0 | 52 | cb->HandleWindowEvent(event); |
6de97a3b | 53 | } |
865bb325 | 54 | } |
c801d85f | 55 | |
e1e955e1 RR |
56 | //----------------------------------------------------------------------------- |
57 | // wxCheckBox | |
c801d85f KB |
58 | //----------------------------------------------------------------------------- |
59 | ||
2cc0e28f | 60 | wxCheckBox::wxCheckBox() |
c801d85f | 61 | { |
6de97a3b | 62 | } |
c801d85f | 63 | |
2cc0e28f VZ |
64 | bool wxCheckBox::Create(wxWindow *parent, |
65 | wxWindowID id, | |
66 | const wxString &label, | |
67 | const wxPoint &pos, | |
68 | const wxSize &size, | |
69 | long style, | |
70 | const wxValidator& validator, | |
71 | const wxString &name ) | |
c801d85f | 72 | { |
93763ad5 WS |
73 | m_needParent = true; |
74 | m_acceptsFocus = true; | |
75 | m_blockEvent = false; | |
ff8bfdbb | 76 | |
f254e242 | 77 | WXValidateStyle(&style); |
4dcaf11a RR |
78 | if (!PreCreation( parent, pos, size ) || |
79 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
80 | { | |
223d09f6 | 81 | wxFAIL_MSG( wxT("wxCheckBox creation failed") ); |
93763ad5 | 82 | return false; |
4dcaf11a | 83 | } |
ce4169a4 | 84 | |
2cc0e28f VZ |
85 | if ( style & wxALIGN_RIGHT ) |
86 | { | |
87 | // VZ: as I don't know a way to create a right aligned checkbox with | |
88 | // GTK we will create a checkbox without label and a label at the | |
89 | // left of it | |
90 | m_widgetCheckbox = gtk_check_button_new(); | |
6de97a3b | 91 | |
eaafd2f8 | 92 | m_widgetLabel = gtk_label_new(""); |
2cc0e28f | 93 | gtk_misc_set_alignment(GTK_MISC(m_widgetLabel), 0.0, 0.5); |
ff8bfdbb | 94 | |
2cc0e28f VZ |
95 | m_widget = gtk_hbox_new(FALSE, 0); |
96 | gtk_box_pack_start(GTK_BOX(m_widget), m_widgetLabel, FALSE, FALSE, 3); | |
97 | gtk_box_pack_start(GTK_BOX(m_widget), m_widgetCheckbox, FALSE, FALSE, 3); | |
98 | ||
2cc0e28f VZ |
99 | gtk_widget_show( m_widgetLabel ); |
100 | gtk_widget_show( m_widgetCheckbox ); | |
101 | } | |
102 | else | |
103 | { | |
eaafd2f8 | 104 | m_widgetCheckbox = gtk_check_button_new_with_label(""); |
9e691f46 | 105 | m_widgetLabel = BUTTON_CHILD( m_widgetCheckbox ); |
2cc0e28f VZ |
106 | m_widget = m_widgetCheckbox; |
107 | } | |
eaafd2f8 | 108 | SetLabel( label ); |
2cc0e28f | 109 | |
2cc0e28f | 110 | gtk_signal_connect( GTK_OBJECT(m_widgetCheckbox), |
4dcccda6 VS |
111 | "toggled", |
112 | GTK_SIGNAL_FUNC(gtk_checkbox_toggled_callback), | |
2cc0e28f | 113 | (gpointer *)this ); |
ff8bfdbb | 114 | |
f03fc89f | 115 | m_parent->DoAddChild( this ); |
ff8bfdbb | 116 | |
abdeb9e7 | 117 | PostCreation(size); |
ff8bfdbb | 118 | |
93763ad5 | 119 | return true; |
6de97a3b | 120 | } |
c801d85f | 121 | |
debe6624 | 122 | void wxCheckBox::SetValue( bool state ) |
c801d85f | 123 | { |
223d09f6 | 124 | wxCHECK_RET( m_widgetCheckbox != NULL, wxT("invalid checkbox") ); |
ff8bfdbb | 125 | |
953704c1 | 126 | if (state == GetValue()) |
8bf45ed1 VZ |
127 | return; |
128 | ||
93763ad5 | 129 | m_blockEvent = true; |
ae0bdb01 | 130 | |
2cc0e28f | 131 | gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widgetCheckbox), state ); |
1db8dc4a | 132 | |
93763ad5 | 133 | m_blockEvent = false; |
6de97a3b | 134 | } |
c801d85f | 135 | |
83058c58 | 136 | bool wxCheckBox::GetValue() const |
c801d85f | 137 | { |
93763ad5 | 138 | wxCHECK_MSG( m_widgetCheckbox != NULL, false, wxT("invalid checkbox") ); |
f96aa4d9 | 139 | |
2cc0e28f | 140 | return GTK_TOGGLE_BUTTON(m_widgetCheckbox)->active; |
4dcccda6 | 141 | } |
4dcccda6 | 142 | |
83058c58 RR |
143 | void wxCheckBox::SetLabel( const wxString& label ) |
144 | { | |
223d09f6 | 145 | wxCHECK_RET( m_widgetLabel != NULL, wxT("invalid checkbox") ); |
f96aa4d9 | 146 | |
b2ff89d6 | 147 | GTKSetLabelForLabel(GTK_LABEL(m_widgetLabel), label); |
83058c58 RR |
148 | } |
149 | ||
f03fc89f | 150 | bool wxCheckBox::Enable( bool enable ) |
d3904ceb | 151 | { |
f03fc89f | 152 | if ( !wxControl::Enable( enable ) ) |
93763ad5 | 153 | return false; |
ff8bfdbb | 154 | |
2cc0e28f | 155 | gtk_widget_set_sensitive( m_widgetLabel, enable ); |
f03fc89f | 156 | |
93763ad5 | 157 | return true; |
d3904ceb RR |
158 | } |
159 | ||
f40fdaa3 | 160 | void wxCheckBox::DoApplyWidgetStyle(GtkRcStyle *style) |
868a2826 | 161 | { |
f40fdaa3 VS |
162 | gtk_widget_modify_style(m_widgetCheckbox, style); |
163 | gtk_widget_modify_style(m_widgetLabel, style); | |
f96aa4d9 RR |
164 | } |
165 | ||
2f073eb2 RR |
166 | bool wxCheckBox::IsOwnGtkWindow( GdkWindow *window ) |
167 | { | |
9e691f46 | 168 | return window == TOGGLE_BUTTON_EVENT_WIN(m_widget); |
2f073eb2 RR |
169 | } |
170 | ||
171 | void wxCheckBox::OnInternalIdle() | |
172 | { | |
173 | wxCursor cursor = m_cursor; | |
a1b806b9 | 174 | if (g_globalCursor.IsOk()) cursor = g_globalCursor; |
2f073eb2 | 175 | |
9e691f46 | 176 | GdkWindow *event_window = TOGGLE_BUTTON_EVENT_WIN(m_widgetCheckbox); |
a1b806b9 | 177 | if ( event_window && cursor.IsOk() ) |
2f073eb2 RR |
178 | { |
179 | /* I now set the cursor the anew in every OnInternalIdle call | |
1db8dc4a VZ |
180 | as setting the cursor in a parent window also effects the |
181 | windows above so that checking for the current cursor is | |
182 | not possible. */ | |
183 | ||
9e691f46 | 184 | gdk_window_set_cursor( event_window, cursor.GetCursor() ); |
2f073eb2 RR |
185 | } |
186 | ||
d7fa7eaa RR |
187 | if (g_delayedFocus == this) |
188 | { | |
189 | if (GTK_WIDGET_REALIZED(m_widget)) | |
190 | { | |
191 | gtk_widget_grab_focus( m_widget ); | |
192 | g_delayedFocus = NULL; | |
193 | } | |
194 | } | |
3d257b8d | 195 | |
e39af974 JS |
196 | if (wxUpdateUIEvent::CanUpdate(this)) |
197 | UpdateWindowUI(wxUPDATE_UI_FROMIDLE); | |
2f073eb2 RR |
198 | } |
199 | ||
f68586e5 VZ |
200 | wxSize wxCheckBox::DoGetBestSize() const |
201 | { | |
db434467 | 202 | return wxControl::DoGetBestSize(); |
f68586e5 VZ |
203 | } |
204 | ||
9d522606 RD |
205 | // static |
206 | wxVisualAttributes | |
207 | wxCheckBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
208 | { | |
209 | return GetDefaultAttributesFromGTKWidget(gtk_check_button_new); | |
210 | } | |
211 | ||
dcf924a3 | 212 | #endif |