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