]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/tglbtn.cpp
!__WX_DC_BLIT_FIXED__ case
[wxWidgets.git] / src / gtk / tglbtn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
15
16 #if wxUSE_TOGGLEBTN
17
18 #include "wx/tglbtn.h"
19
20 #ifndef WX_PRECOMP
21 #include "wx/button.h"
22 #endif
23
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
30 #include "wx/gtk/private.h"
31
32 extern bool g_blockEventsOnDrag;
33
34 extern "C" {
35 static void gtk_togglebutton_clicked_callback(GtkWidget *WXUNUSED(widget), wxToggleButton *cb)
36 {
37 if (g_isIdle)
38 wxapp_install_idle_handler();
39
40 if (!cb->m_hasVMT || g_blockEventsOnDrag)
41 return;
42
43 if (cb->m_blockEvent) return;
44
45 // Generate a wx event.
46 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, cb->GetId());
47 event.SetInt(cb->GetValue());
48 event.SetEventObject(cb);
49 cb->GetEventHandler()->ProcessEvent(event);
50 }
51 }
52
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
54
55 // ------------------------------------------------------------------------
56 // wxToggleBitmapButton
57 // ------------------------------------------------------------------------
58
59 IMPLEMENT_DYNAMIC_CLASS(wxToggleBitmapButton, wxControl)
60
61 bool wxToggleBitmapButton::Create(wxWindow *parent, wxWindowID id,
62 const wxBitmap &label, const wxPoint &pos,
63 const wxSize &size, long style,
64 const wxValidator& validator,
65 const wxString &name)
66 {
67 m_needParent = true;
68 m_acceptsFocus = true;
69
70 m_blockEvent = false;
71
72 if (!PreCreation(parent, pos, size) ||
73 !CreateBase(parent, id, pos, size, style, validator, name ))
74 {
75 wxFAIL_MSG(wxT("wxToggleBitmapButton creation failed"));
76 return false;
77 }
78
79 m_bitmap = label;
80
81 // Create the gtk widget.
82 m_widget = gtk_toggle_button_new();
83
84 if (style & wxNO_BORDER)
85 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
86
87 if (m_bitmap.Ok())
88 {
89 OnSetBitmap();
90 }
91
92 g_signal_connect (m_widget, "clicked",
93 G_CALLBACK (gtk_togglebutton_clicked_callback),
94 this);
95
96 m_parent->DoAddChild(this);
97
98 PostCreation(size);
99
100 return true;
101 }
102
103 // void SetValue(bool state)
104 // Set the value of the toggle button.
105 void wxToggleBitmapButton::SetValue(bool state)
106 {
107 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
108
109 if (state == GetValue())
110 return;
111
112 m_blockEvent = true;
113
114 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
115
116 m_blockEvent = false;
117 }
118
119 // bool GetValue() const
120 // Get the value of the toggle button.
121 bool wxToggleBitmapButton::GetValue() const
122 {
123 wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button"));
124
125 return GTK_TOGGLE_BUTTON(m_widget)->active;
126 }
127
128 void wxToggleBitmapButton::SetLabel(const wxBitmap& label)
129 {
130 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
131
132 m_bitmap = label;
133 InvalidateBestSize();
134
135 OnSetBitmap();
136 }
137
138 void wxToggleBitmapButton::OnSetBitmap()
139 {
140 if (!m_bitmap.Ok()) return;
141
142 GdkBitmap *mask = (GdkBitmap *) NULL;
143 if (m_bitmap.GetMask()) mask = m_bitmap.GetMask()->GetBitmap();
144
145 GtkWidget *child = GTK_BIN(m_widget)->child;
146 if (child == NULL)
147 {
148 // initial bitmap
149 GtkWidget *pixmap = gtk_pixmap_new(m_bitmap.GetPixmap(), mask);
150 gtk_widget_show(pixmap);
151 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
152 }
153 else
154 { // subsequent bitmaps
155 GtkPixmap *g_pixmap = GTK_PIXMAP(child);
156 gtk_pixmap_set(g_pixmap, m_bitmap.GetPixmap(), mask);
157 }
158 }
159
160 bool wxToggleBitmapButton::Enable(bool enable /*=true*/)
161 {
162 if (!wxControl::Enable(enable))
163 return false;
164
165 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
166
167 return true;
168 }
169
170 void wxToggleBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
171 {
172 gtk_widget_modify_style(m_widget, style);
173 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
174 }
175
176 GdkWindow *
177 wxToggleBitmapButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
178 {
179 return GTK_BUTTON(m_widget)->event_window;
180 }
181
182 // Get the "best" size for this control.
183 wxSize wxToggleBitmapButton::DoGetBestSize() const
184 {
185 wxSize best;
186
187 if (m_bitmap.Ok())
188 {
189 int border = HasFlag(wxNO_BORDER) ? 4 : 10;
190 best.x = m_bitmap.GetWidth()+border;
191 best.y = m_bitmap.GetHeight()+border;
192 }
193 CacheBestSize(best);
194 return best;
195 }
196
197
198 // static
199 wxVisualAttributes
200 wxToggleBitmapButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
201 {
202 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new);
203 }
204
205
206 // ------------------------------------------------------------------------
207 // wxToggleButton
208 // ------------------------------------------------------------------------
209
210 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
211
212 bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
213 const wxString &label, const wxPoint &pos,
214 const wxSize &size, long style,
215 const wxValidator& validator,
216 const wxString &name)
217 {
218 m_needParent = true;
219 m_acceptsFocus = true;
220
221 m_blockEvent = false;
222
223 if (!PreCreation(parent, pos, size) ||
224 !CreateBase(parent, id, pos, size, style, validator, name )) {
225 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
226 return false;
227 }
228
229 wxControl::SetLabel(label);
230
231 // Create the gtk widget.
232 m_widget = gtk_toggle_button_new_with_label( wxGTK_CONV( m_label ) );
233
234 g_signal_connect (m_widget, "clicked",
235 G_CALLBACK (gtk_togglebutton_clicked_callback),
236 this);
237
238 m_parent->DoAddChild(this);
239
240 PostCreation(size);
241
242 return true;
243 }
244
245 // void SetValue(bool state)
246 // Set the value of the toggle button.
247 void wxToggleButton::SetValue(bool state)
248 {
249 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
250
251 if (state == GetValue())
252 return;
253
254 m_blockEvent = true;
255
256 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
257
258 m_blockEvent = false;
259 }
260
261 // bool GetValue() const
262 // Get the value of the toggle button.
263 bool wxToggleButton::GetValue() const
264 {
265 wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button"));
266
267 return GTK_TOGGLE_BUTTON(m_widget)->active;
268 }
269
270 void wxToggleButton::SetLabel(const wxString& label)
271 {
272 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
273
274 wxControl::SetLabel(label);
275
276 gtk_label_set_text(GTK_LABEL(GTK_BIN(m_widget)->child), wxGTK_CONV(GetLabel()));
277 }
278
279 bool wxToggleButton::Enable(bool enable /*=true*/)
280 {
281 if (!wxControl::Enable(enable))
282 return false;
283
284 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
285
286 return true;
287 }
288
289 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle *style)
290 {
291 gtk_widget_modify_style(m_widget, style);
292 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
293 }
294
295 GdkWindow *
296 wxToggleButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
297 {
298 return GTK_BUTTON(m_widget)->event_window;
299 }
300
301 // Get the "best" size for this control.
302 wxSize wxToggleButton::DoGetBestSize() const
303 {
304 wxSize ret(wxControl::DoGetBestSize());
305
306 if (!HasFlag(wxBU_EXACTFIT))
307 {
308 if (ret.x < 80) ret.x = 80;
309 }
310
311 CacheBestSize(ret);
312 return ret;
313 }
314
315 // static
316 wxVisualAttributes
317 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
318 {
319 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new);
320 }
321
322 #endif // wxUSE_TOGGLEBTN