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