]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/tglbtn.cpp
remove also wxT(); it's confusing, too; do load wxstd.mo catalog after calling wxLoca...
[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 #include "wx/gtk/private.h"
25
26 extern bool g_blockEventsOnDrag;
27
28 extern "C" {
29 static void gtk_togglebutton_clicked_callback(GtkWidget *WXUNUSED(widget), wxToggleButton *cb)
30 {
31 if (!cb->m_hasVMT || g_blockEventsOnDrag)
32 return;
33
34 // Generate a wx event.
35 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, cb->GetId());
36 event.SetInt(cb->GetValue());
37 event.SetEventObject(cb);
38 cb->HandleWindowEvent(event);
39 }
40 }
41
42 wxDEFINE_EVENT( wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, wxCommandEvent )
43
44 // ------------------------------------------------------------------------
45 // wxBitmapToggleButton
46 // ------------------------------------------------------------------------
47
48 IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton, wxControl)
49
50 bool wxBitmapToggleButton::Create(wxWindow *parent, wxWindowID id,
51 const wxBitmap &label, const wxPoint &pos,
52 const wxSize &size, long style,
53 const wxValidator& validator,
54 const wxString &name)
55 {
56 if (!PreCreation(parent, pos, size) ||
57 !CreateBase(parent, id, pos, size, style, validator, name ))
58 {
59 wxFAIL_MSG(wxT("wxBitmapToggleButton creation failed"));
60 return false;
61 }
62
63 // Create the gtk widget.
64 m_widget = gtk_toggle_button_new();
65 g_object_ref(m_widget);
66
67 if (style & wxNO_BORDER)
68 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
69
70 m_bitmap = label;
71 OnSetBitmap();
72
73 g_signal_connect (m_widget, "clicked",
74 G_CALLBACK (gtk_togglebutton_clicked_callback),
75 this);
76
77 m_parent->DoAddChild(this);
78
79 PostCreation(size);
80
81 return true;
82 }
83
84 void wxBitmapToggleButton::GTKDisableEvents()
85 {
86 g_signal_handlers_block_by_func(m_widget,
87 (gpointer) gtk_togglebutton_clicked_callback, this);
88 }
89
90 void wxBitmapToggleButton::GTKEnableEvents()
91 {
92 g_signal_handlers_unblock_by_func(m_widget,
93 (gpointer) gtk_togglebutton_clicked_callback, this);
94 }
95
96 // void SetValue(bool state)
97 // Set the value of the toggle button.
98 void wxBitmapToggleButton::SetValue(bool state)
99 {
100 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
101
102 if (state == GetValue())
103 return;
104
105 GTKDisableEvents();
106
107 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
108
109 GTKEnableEvents();
110 }
111
112 // bool GetValue() const
113 // Get the value of the toggle button.
114 bool wxBitmapToggleButton::GetValue() const
115 {
116 wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button"));
117
118 return gtk_toggle_button_get_active((GtkToggleButton*)m_widget);
119 }
120
121 void wxBitmapToggleButton::SetLabel(const wxBitmap& label)
122 {
123 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
124
125 m_bitmap = label;
126 InvalidateBestSize();
127
128 OnSetBitmap();
129 }
130
131 void wxBitmapToggleButton::OnSetBitmap()
132 {
133 if (!m_bitmap.Ok()) return;
134
135 GtkWidget* image = ((GtkBin*)m_widget)->child;
136 if (image == NULL)
137 {
138 image = gtk_image_new();
139 gtk_widget_show(image);
140 gtk_container_add((GtkContainer*)m_widget, image);
141 }
142 // always use pixbuf, because pixmap mask does not
143 // work with disabled images in some themes
144 gtk_image_set_from_pixbuf((GtkImage*)image, m_bitmap.GetPixbuf());
145 }
146
147 bool wxBitmapToggleButton::Enable(bool enable /*=true*/)
148 {
149 if (!wxControl::Enable(enable))
150 return false;
151
152 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
153
154 return true;
155 }
156
157 void wxBitmapToggleButton::DoApplyWidgetStyle(GtkRcStyle *style)
158 {
159 gtk_widget_modify_style(m_widget, style);
160 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
161 }
162
163 GdkWindow *
164 wxBitmapToggleButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
165 {
166 return GTK_BUTTON(m_widget)->event_window;
167 }
168
169 // Get the "best" size for this control.
170 wxSize wxBitmapToggleButton::DoGetBestSize() const
171 {
172 wxSize best;
173
174 if (m_bitmap.Ok())
175 {
176 int border = HasFlag(wxNO_BORDER) ? 4 : 10;
177 best.x = m_bitmap.GetWidth()+border;
178 best.y = m_bitmap.GetHeight()+border;
179 }
180 CacheBestSize(best);
181 return best;
182 }
183
184
185 // static
186 wxVisualAttributes
187 wxBitmapToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
188 {
189 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new);
190 }
191
192
193 // ------------------------------------------------------------------------
194 // wxToggleButton
195 // ------------------------------------------------------------------------
196
197 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
198
199 bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
200 const wxString &label, const wxPoint &pos,
201 const wxSize &size, long style,
202 const wxValidator& validator,
203 const wxString &name)
204 {
205 if (!PreCreation(parent, pos, size) ||
206 !CreateBase(parent, id, pos, size, style, validator, name ))
207 {
208 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
209 return false;
210 }
211
212 // Create the gtk widget.
213 m_widget = gtk_toggle_button_new_with_mnemonic("");
214 g_object_ref(m_widget);
215
216 SetLabel(label);
217
218 g_signal_connect (m_widget, "clicked",
219 G_CALLBACK (gtk_togglebutton_clicked_callback),
220 this);
221
222 m_parent->DoAddChild(this);
223
224 PostCreation(size);
225
226 return true;
227 }
228
229 void wxToggleButton::GTKDisableEvents()
230 {
231 g_signal_handlers_block_by_func(m_widget,
232 (gpointer) gtk_togglebutton_clicked_callback, this);
233 }
234
235 void wxToggleButton::GTKEnableEvents()
236 {
237 g_signal_handlers_unblock_by_func(m_widget,
238 (gpointer) gtk_togglebutton_clicked_callback, this);
239 }
240
241 // void SetValue(bool state)
242 // Set the value of the toggle button.
243 void wxToggleButton::SetValue(bool state)
244 {
245 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
246
247 if (state == GetValue())
248 return;
249
250 GTKDisableEvents();
251
252 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
253
254 GTKEnableEvents();
255 }
256
257 // bool GetValue() const
258 // Get the value of the toggle button.
259 bool wxToggleButton::GetValue() const
260 {
261 wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button"));
262
263 return GTK_TOGGLE_BUTTON(m_widget)->active;
264 }
265
266 void wxToggleButton::SetLabel(const wxString& label)
267 {
268 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
269
270 wxControl::SetLabel(label);
271
272 const wxString labelGTK = GTKConvertMnemonics(label);
273
274 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
275
276 ApplyWidgetStyle( false );
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