]>
Commit | Line | Data |
---|---|---|
1db8dc4a | 1 | ///////////////////////////////////////////////////////////////////////////// |
f1e01716 | 2 | // Name: src/gtk/tglbtn.cpp |
1db8dc4a VZ |
3 | // Purpose: Definition of the wxToggleButton class, which implements a |
4 | // toggle button under wxGTK. | |
5 | // Author: John Norris, minor changes by Axel Schlueter | |
6 | // Modified by: | |
7 | // Created: 08.02.01 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 2000 Johnny C. Norris II | |
706fb893 | 10 | // License: wxWindows licence |
1db8dc4a VZ |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
14f355c2 VS |
13 | // For compilers that support precompilation, includes "wx.h". |
14 | #include "wx/wxprec.h" | |
15 | ||
f1e01716 WS |
16 | #if wxUSE_TOGGLEBTN |
17 | ||
1db8dc4a VZ |
18 | #include "wx/tglbtn.h" |
19 | ||
f1e01716 WS |
20 | #ifndef WX_PRECOMP |
21 | #include "wx/button.h" | |
22 | #endif | |
1db8dc4a | 23 | |
1efb5db8 MR |
24 | // FIXME: Use GtkImage instead of GtkPixmap. |
25 | #include <gtk/gtkversion.h> | |
26 | #ifdef GTK_DISABLE_DEPRECATED | |
27 | #undef GTK_DISABLE_DEPRECATED | |
28 | #endif | |
29 | ||
9e691f46 | 30 | #include "wx/gtk/private.h" |
1db8dc4a | 31 | |
1db8dc4a VZ |
32 | extern bool g_blockEventsOnDrag; |
33 | extern wxCursor g_globalCursor; | |
34 | ||
865bb325 | 35 | extern "C" { |
9864c56d | 36 | static void gtk_togglebutton_clicked_callback(GtkWidget *WXUNUSED(widget), wxToggleButton *cb) |
1db8dc4a | 37 | { |
91af0895 WS |
38 | if (g_isIdle) |
39 | wxapp_install_idle_handler(); | |
40 | ||
41 | if (!cb->m_hasVMT || g_blockEventsOnDrag) | |
42 | return; | |
43 | ||
44 | if (cb->m_blockEvent) return; | |
45 | ||
46 | // Generate a wx event. | |
47 | wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, cb->GetId()); | |
48 | event.SetInt(cb->GetValue()); | |
49 | event.SetEventObject(cb); | |
50 | cb->GetEventHandler()->ProcessEvent(event); | |
1db8dc4a | 51 | } |
865bb325 | 52 | } |
1db8dc4a | 53 | |
1db8dc4a VZ |
54 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED) |
55 | ||
4f856067 RR |
56 | // ------------------------------------------------------------------------ |
57 | // wxToggleBitmapButton | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
60 | IMPLEMENT_DYNAMIC_CLASS(wxToggleBitmapButton, wxControl) | |
61 | ||
62 | bool wxToggleBitmapButton::Create(wxWindow *parent, wxWindowID id, | |
63 | const wxBitmap &label, const wxPoint &pos, | |
64 | const wxSize &size, long style, | |
65 | const wxValidator& validator, | |
66 | const wxString &name) | |
67 | { | |
91af0895 WS |
68 | m_needParent = true; |
69 | m_acceptsFocus = true; | |
70 | ||
71 | m_blockEvent = false; | |
4f856067 RR |
72 | |
73 | if (!PreCreation(parent, pos, size) || | |
74 | !CreateBase(parent, id, pos, size, style, validator, name )) | |
75 | { | |
76 | wxFAIL_MSG(wxT("wxToggleBitmapButton creation failed")); | |
91af0895 | 77 | return false; |
4f856067 | 78 | } |
91af0895 | 79 | |
4f856067 RR |
80 | m_bitmap = label; |
81 | ||
82 | // Create the gtk widget. | |
83 | m_widget = gtk_toggle_button_new(); | |
84 | ||
85 | if (style & wxNO_BORDER) | |
86 | gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE ); | |
87 | ||
88 | if (m_bitmap.Ok()) | |
89 | { | |
4f856067 RR |
90 | OnSetBitmap(); |
91 | } | |
92 | ||
9fa72bd2 MR |
93 | g_signal_connect (m_widget, "clicked", |
94 | G_CALLBACK (gtk_togglebutton_clicked_callback), | |
95 | this); | |
4f856067 RR |
96 | |
97 | m_parent->DoAddChild(this); | |
98 | ||
abdeb9e7 | 99 | PostCreation(size); |
4f856067 | 100 | |
91af0895 | 101 | return true; |
4f856067 RR |
102 | } |
103 | ||
104 | // void SetValue(bool state) | |
105 | // Set the value of the toggle button. | |
106 | void wxToggleBitmapButton::SetValue(bool state) | |
107 | { | |
91af0895 | 108 | wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button")); |
4f856067 | 109 | |
91af0895 WS |
110 | if (state == GetValue()) |
111 | return; | |
4f856067 | 112 | |
91af0895 | 113 | m_blockEvent = true; |
4f856067 | 114 | |
91af0895 | 115 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state); |
4f856067 | 116 | |
91af0895 | 117 | m_blockEvent = false; |
4f856067 RR |
118 | } |
119 | ||
120 | // bool GetValue() const | |
121 | // Get the value of the toggle button. | |
122 | bool wxToggleBitmapButton::GetValue() const | |
123 | { | |
91af0895 | 124 | wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button")); |
4f856067 | 125 | |
91af0895 | 126 | return GTK_TOGGLE_BUTTON(m_widget)->active; |
4f856067 RR |
127 | } |
128 | ||
129 | void wxToggleBitmapButton::SetLabel(const wxBitmap& label) | |
130 | { | |
131 | wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button")); | |
132 | ||
133 | m_bitmap = label; | |
9f884528 | 134 | InvalidateBestSize(); |
91af0895 | 135 | |
4f856067 RR |
136 | OnSetBitmap(); |
137 | } | |
138 | ||
139 | void wxToggleBitmapButton::OnSetBitmap() | |
140 | { | |
141 | if (!m_bitmap.Ok()) return; | |
142 | ||
143 | GdkBitmap *mask = (GdkBitmap *) NULL; | |
144 | if (m_bitmap.GetMask()) mask = m_bitmap.GetMask()->GetBitmap(); | |
145 | ||
afa7bd1e | 146 | GtkWidget *child = GTK_BIN(m_widget)->child; |
4f856067 RR |
147 | if (child == NULL) |
148 | { | |
149 | // initial bitmap | |
150 | GtkWidget *pixmap = gtk_pixmap_new(m_bitmap.GetPixmap(), mask); | |
151 | gtk_widget_show(pixmap); | |
152 | gtk_container_add(GTK_CONTAINER(m_widget), pixmap); | |
153 | } | |
154 | else | |
155 | { // subsequent bitmaps | |
156 | GtkPixmap *g_pixmap = GTK_PIXMAP(child); | |
157 | gtk_pixmap_set(g_pixmap, m_bitmap.GetPixmap(), mask); | |
158 | } | |
159 | } | |
160 | ||
91af0895 | 161 | bool wxToggleBitmapButton::Enable(bool enable /*=true*/) |
4f856067 RR |
162 | { |
163 | if (!wxControl::Enable(enable)) | |
91af0895 | 164 | return false; |
4f856067 | 165 | |
afa7bd1e | 166 | gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable); |
4f856067 | 167 | |
91af0895 | 168 | return true; |
4f856067 RR |
169 | } |
170 | ||
f40fdaa3 | 171 | void wxToggleBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style) |
4f856067 | 172 | { |
f40fdaa3 | 173 | gtk_widget_modify_style(m_widget, style); |
afa7bd1e | 174 | gtk_widget_modify_style(GTK_BIN(m_widget)->child, style); |
4f856067 RR |
175 | } |
176 | ||
177 | bool wxToggleBitmapButton::IsOwnGtkWindow(GdkWindow *window) | |
178 | { | |
afa7bd1e | 179 | return window == GTK_BUTTON(m_widget)->event_window; |
4f856067 RR |
180 | } |
181 | ||
182 | void wxToggleBitmapButton::OnInternalIdle() | |
183 | { | |
184 | wxCursor cursor = m_cursor; | |
91af0895 | 185 | |
4f856067 RR |
186 | if (g_globalCursor.Ok()) |
187 | cursor = g_globalCursor; | |
188 | ||
afa7bd1e | 189 | GdkWindow *win = GTK_BUTTON(m_widget)->event_window; |
4f856067 RR |
190 | if ( win && cursor.Ok() ) |
191 | { | |
192 | /* I now set the cursor the anew in every OnInternalIdle call | |
193 | as setting the cursor in a parent window also effects the | |
194 | windows above so that checking for the current cursor is | |
195 | not possible. */ | |
196 | ||
197 | gdk_window_set_cursor(win, cursor.GetCursor()); | |
198 | } | |
199 | ||
200 | if (wxUpdateUIEvent::CanUpdate(this)) | |
201 | UpdateWindowUI(wxUPDATE_UI_FROMIDLE); | |
202 | } | |
203 | ||
9f884528 | 204 | |
4f856067 RR |
205 | // Get the "best" size for this control. |
206 | wxSize wxToggleBitmapButton::DoGetBestSize() const | |
207 | { | |
abdeb9e7 | 208 | wxSize best; |
91af0895 | 209 | |
abdeb9e7 | 210 | if (m_bitmap.Ok()) |
4f856067 | 211 | { |
abdeb9e7 RD |
212 | int border = HasFlag(wxNO_BORDER) ? 4 : 10; |
213 | best.x = m_bitmap.GetWidth()+border; | |
214 | best.y = m_bitmap.GetHeight()+border; | |
4f856067 | 215 | } |
9f884528 | 216 | CacheBestSize(best); |
abdeb9e7 | 217 | return best; |
4f856067 | 218 | } |
9d522606 RD |
219 | |
220 | ||
221 | // static | |
222 | wxVisualAttributes | |
223 | wxToggleBitmapButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
224 | { | |
225 | return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new); | |
226 | } | |
227 | ||
228 | ||
4f856067 RR |
229 | // ------------------------------------------------------------------------ |
230 | // wxToggleButton | |
231 | // ------------------------------------------------------------------------ | |
232 | ||
233 | IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl) | |
234 | ||
1db8dc4a VZ |
235 | bool wxToggleButton::Create(wxWindow *parent, wxWindowID id, |
236 | const wxString &label, const wxPoint &pos, | |
237 | const wxSize &size, long style, | |
238 | const wxValidator& validator, | |
239 | const wxString &name) | |
240 | { | |
91af0895 WS |
241 | m_needParent = true; |
242 | m_acceptsFocus = true; | |
1db8dc4a | 243 | |
91af0895 | 244 | m_blockEvent = false; |
1db8dc4a | 245 | |
91af0895 WS |
246 | if (!PreCreation(parent, pos, size) || |
247 | !CreateBase(parent, id, pos, size, style, validator, name )) { | |
248 | wxFAIL_MSG(wxT("wxToggleButton creation failed")); | |
249 | return false; | |
250 | } | |
1db8dc4a | 251 | |
91af0895 | 252 | wxControl::SetLabel(label); |
1db8dc4a | 253 | |
91af0895 WS |
254 | // Create the gtk widget. |
255 | m_widget = gtk_toggle_button_new_with_label( wxGTK_CONV( m_label ) ); | |
1db8dc4a | 256 | |
9fa72bd2 MR |
257 | g_signal_connect (m_widget, "clicked", |
258 | G_CALLBACK (gtk_togglebutton_clicked_callback), | |
259 | this); | |
1db8dc4a | 260 | |
91af0895 | 261 | m_parent->DoAddChild(this); |
1db8dc4a | 262 | |
91af0895 WS |
263 | PostCreation(size); |
264 | ||
265 | return true; | |
1db8dc4a VZ |
266 | } |
267 | ||
268 | // void SetValue(bool state) | |
269 | // Set the value of the toggle button. | |
270 | void wxToggleButton::SetValue(bool state) | |
271 | { | |
91af0895 | 272 | wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button")); |
1db8dc4a | 273 | |
91af0895 WS |
274 | if (state == GetValue()) |
275 | return; | |
1db8dc4a | 276 | |
91af0895 | 277 | m_blockEvent = true; |
1db8dc4a | 278 | |
91af0895 | 279 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state); |
1db8dc4a | 280 | |
91af0895 | 281 | m_blockEvent = false; |
1db8dc4a VZ |
282 | } |
283 | ||
284 | // bool GetValue() const | |
285 | // Get the value of the toggle button. | |
286 | bool wxToggleButton::GetValue() const | |
287 | { | |
91af0895 | 288 | wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button")); |
1db8dc4a | 289 | |
91af0895 | 290 | return GTK_TOGGLE_BUTTON(m_widget)->active; |
1db8dc4a VZ |
291 | } |
292 | ||
1db8dc4a VZ |
293 | void wxToggleButton::SetLabel(const wxString& label) |
294 | { | |
8ab696e0 | 295 | wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button")); |
1db8dc4a | 296 | |
8ab696e0 | 297 | wxControl::SetLabel(label); |
1db8dc4a | 298 | |
a7c12d28 | 299 | gtk_label_set_text(GTK_LABEL(GTK_BIN(m_widget)->child), wxGTK_CONV(GetLabel())); |
1db8dc4a VZ |
300 | } |
301 | ||
91af0895 | 302 | bool wxToggleButton::Enable(bool enable /*=true*/) |
1db8dc4a | 303 | { |
8ab696e0 | 304 | if (!wxControl::Enable(enable)) |
91af0895 | 305 | return false; |
1db8dc4a | 306 | |
afa7bd1e | 307 | gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable); |
1db8dc4a | 308 | |
91af0895 | 309 | return true; |
1db8dc4a VZ |
310 | } |
311 | ||
f40fdaa3 | 312 | void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle *style) |
1db8dc4a | 313 | { |
f40fdaa3 | 314 | gtk_widget_modify_style(m_widget, style); |
afa7bd1e | 315 | gtk_widget_modify_style(GTK_BIN(m_widget)->child, style); |
1db8dc4a VZ |
316 | } |
317 | ||
1db8dc4a VZ |
318 | bool wxToggleButton::IsOwnGtkWindow(GdkWindow *window) |
319 | { | |
afa7bd1e | 320 | return window == GTK_BUTTON(m_widget)->event_window; |
1db8dc4a VZ |
321 | } |
322 | ||
1db8dc4a VZ |
323 | void wxToggleButton::OnInternalIdle() |
324 | { | |
8ab696e0 | 325 | wxCursor cursor = m_cursor; |
91af0895 | 326 | |
8ab696e0 RR |
327 | if (g_globalCursor.Ok()) |
328 | cursor = g_globalCursor; | |
1db8dc4a | 329 | |
afa7bd1e | 330 | GdkWindow *win = GTK_BUTTON(m_widget)->event_window; |
9e691f46 VZ |
331 | if ( win && cursor.Ok() ) |
332 | { | |
1db8dc4a VZ |
333 | /* I now set the cursor the anew in every OnInternalIdle call |
334 | as setting the cursor in a parent window also effects the | |
335 | windows above so that checking for the current cursor is | |
336 | not possible. */ | |
337 | ||
9e691f46 | 338 | gdk_window_set_cursor(win, cursor.GetCursor()); |
8ab696e0 | 339 | } |
1db8dc4a | 340 | |
e39af974 JS |
341 | if (wxUpdateUIEvent::CanUpdate(this)) |
342 | UpdateWindowUI(wxUPDATE_UI_FROMIDLE); | |
1db8dc4a VZ |
343 | } |
344 | ||
9f884528 | 345 | |
1db8dc4a VZ |
346 | // Get the "best" size for this control. |
347 | wxSize wxToggleButton::DoGetBestSize() const | |
348 | { | |
8ab696e0 | 349 | wxSize ret(wxControl::DoGetBestSize()); |
91af0895 | 350 | |
8ab696e0 RR |
351 | if (!HasFlag(wxBU_EXACTFIT)) |
352 | { | |
353 | if (ret.x < 80) ret.x = 80; | |
354 | } | |
91af0895 | 355 | |
9f884528 RD |
356 | CacheBestSize(ret); |
357 | return ret; | |
1db8dc4a VZ |
358 | } |
359 | ||
9d522606 RD |
360 | // static |
361 | wxVisualAttributes | |
362 | wxToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
363 | { | |
364 | return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new); | |
365 | } | |
366 | ||
1db8dc4a | 367 | #endif // wxUSE_TOGGLEBTN |