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