]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/bmpbuttn.cpp
Fix memory leak when a spacer is added, and crash when a window is added before wxSiz...
[wxWidgets.git] / src / gtk1 / bmpbuttn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/bmpbuttn.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "bmpbuttn.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #include "wx/defs.h"
18
19 #if wxUSE_BMPBUTTON
20
21 #include "wx/bmpbuttn.h"
22
23 #include "wx/gtk/private.h"
24
25 //-----------------------------------------------------------------------------
26 // classes
27 //-----------------------------------------------------------------------------
28
29 class wxBitmapButton;
30
31 //-----------------------------------------------------------------------------
32 // idle system
33 //-----------------------------------------------------------------------------
34
35 extern void wxapp_install_idle_handler();
36 extern bool g_isIdle;
37
38 //-----------------------------------------------------------------------------
39 // data
40 //-----------------------------------------------------------------------------
41
42 extern bool g_blockEventsOnDrag;
43
44 //-----------------------------------------------------------------------------
45 // "clicked"
46 //-----------------------------------------------------------------------------
47
48 extern "C" {
49 static void gtk_bmpbutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
50 {
51 if (g_isIdle)
52 wxapp_install_idle_handler();
53
54 if (!button->m_hasVMT) return;
55 if (g_blockEventsOnDrag) return;
56
57 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
58 event.SetEventObject(button);
59 button->GetEventHandler()->ProcessEvent(event);
60 }
61 }
62
63 //-----------------------------------------------------------------------------
64 // "enter"
65 //-----------------------------------------------------------------------------
66
67 extern "C" {
68 static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
69 {
70 if (!button->m_hasVMT) return;
71 if (g_blockEventsOnDrag) return;
72
73 button->HasFocus();
74 }
75 }
76
77 //-----------------------------------------------------------------------------
78 // "leave"
79 //-----------------------------------------------------------------------------
80
81 extern "C" {
82 static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
83 {
84 if (!button->m_hasVMT) return;
85 if (g_blockEventsOnDrag) return;
86
87 button->NotFocus();
88 }
89 }
90
91 //-----------------------------------------------------------------------------
92 // "pressed"
93 //-----------------------------------------------------------------------------
94
95 extern "C" {
96 static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
97 {
98 if (!button->m_hasVMT) return;
99 if (g_blockEventsOnDrag) return;
100
101 button->StartSelect();
102 }
103 }
104
105 //-----------------------------------------------------------------------------
106 // "released"
107 //-----------------------------------------------------------------------------
108
109 extern "C" {
110 static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
111 {
112 if (!button->m_hasVMT) return;
113 if (g_blockEventsOnDrag) return;
114
115 button->EndSelect();
116 }
117 }
118
119 //-----------------------------------------------------------------------------
120 // wxBitmapButton
121 //-----------------------------------------------------------------------------
122
123 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxButton)
124
125 void wxBitmapButton::Init()
126 {
127 m_hasFocus =
128 m_isSelected = FALSE;
129 }
130
131 bool wxBitmapButton::Create( wxWindow *parent,
132 wxWindowID id,
133 const wxBitmap& bitmap,
134 const wxPoint& pos,
135 const wxSize& size,
136 long style,
137 const wxValidator& validator,
138 const wxString &name )
139 {
140 m_needParent = TRUE;
141 m_acceptsFocus = TRUE;
142
143 if (!PreCreation( parent, pos, size ) ||
144 !CreateBase( parent, id, pos, size, style, validator, name ))
145 {
146 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
147 return FALSE;
148 }
149
150 m_bmpNormal = bitmap;
151
152 m_widget = gtk_button_new();
153
154 if (style & wxNO_BORDER)
155 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
156
157 if (m_bmpNormal.Ok())
158 {
159 OnSetBitmap();
160 }
161
162 gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked",
163 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
164
165 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
166 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
167 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
168 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
169 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
170 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
171 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
172 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
173
174 m_parent->DoAddChild( this );
175
176 PostCreation(size);
177
178 return TRUE;
179 }
180
181 void wxBitmapButton::SetDefault()
182 {
183 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
184 gtk_widget_grab_default( m_widget );
185
186 SetSize( m_x, m_y, m_width, m_height );
187 }
188
189 void wxBitmapButton::SetLabel( const wxString &label )
190 {
191 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
192
193 wxControl::SetLabel( label );
194 }
195
196 wxString wxBitmapButton::GetLabel() const
197 {
198 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid button") );
199
200 return wxControl::GetLabel();
201 }
202
203 void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
204 {
205 if ( !BUTTON_CHILD(m_widget) )
206 return;
207
208 wxButton::DoApplyWidgetStyle(style);
209 }
210
211 void wxBitmapButton::OnSetBitmap()
212 {
213 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
214
215 InvalidateBestSize();
216
217 wxBitmap the_one;
218 if (!m_isEnabled)
219 the_one = m_bmpDisabled;
220 else if (m_isSelected)
221 the_one = m_bmpSelected;
222 else if (m_hasFocus)
223 the_one = m_bmpFocus;
224 else
225 {
226 if (m_isSelected)
227 {
228 the_one = m_bmpSelected;
229 }
230 else
231 {
232 if (m_hasFocus)
233 the_one = m_bmpFocus;
234 else
235 the_one = m_bmpNormal;
236 }
237 }
238
239 if (!the_one.Ok()) the_one = m_bmpNormal;
240 if (!the_one.Ok()) return;
241
242 GdkBitmap *mask = (GdkBitmap *) NULL;
243 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
244
245 GtkWidget *child = BUTTON_CHILD(m_widget);
246 if (child == NULL)
247 {
248 // initial bitmap
249 GtkWidget *pixmap;
250 #ifdef __WXGTK20__
251 if (the_one.HasPixbuf())
252 pixmap = gtk_image_new_from_pixbuf(the_one.GetPixbuf());
253 else
254 pixmap = gtk_image_new_from_pixmap(the_one.GetPixmap(), mask);
255 #else
256 pixmap = gtk_pixmap_new(the_one.GetPixmap(), mask);
257 #endif
258 gtk_widget_show(pixmap);
259 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
260 }
261 else
262 { // subsequent bitmaps
263 #ifdef __WXGTK20__
264 GtkImage *pixmap = GTK_IMAGE(child);
265 if (the_one.HasPixbuf())
266 gtk_image_set_from_pixbuf(pixmap, the_one.GetPixbuf());
267 else
268 gtk_image_set_from_pixmap(pixmap, the_one.GetPixmap(), mask);
269 #else
270 GtkPixmap *pixmap = GTK_PIXMAP(child);
271 gtk_pixmap_set(pixmap, the_one.GetPixmap(), mask);
272 #endif
273 }
274 }
275
276 wxSize wxBitmapButton::DoGetBestSize() const
277 {
278 return wxControl::DoGetBestSize();
279 }
280
281 bool wxBitmapButton::Enable( bool enable )
282 {
283 if ( !wxWindow::Enable(enable) )
284 return FALSE;
285
286 OnSetBitmap();
287
288 return TRUE;
289 }
290
291 void wxBitmapButton::HasFocus()
292 {
293 m_hasFocus = TRUE;
294 OnSetBitmap();
295 }
296
297 void wxBitmapButton::NotFocus()
298 {
299 m_hasFocus = FALSE;
300 OnSetBitmap();
301 }
302
303 void wxBitmapButton::StartSelect()
304 {
305 m_isSelected = TRUE;
306 OnSetBitmap();
307 }
308
309 void wxBitmapButton::EndSelect()
310 {
311 m_isSelected = FALSE;
312 OnSetBitmap();
313 }
314
315 #endif // wxUSE_BMPBUTTON
316