]> git.saurik.com Git - wxWidgets.git/blame - 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
CommitLineData
151ccd11 1/////////////////////////////////////////////////////////////////////////////
f6bcfd97 2// Name: gtk/bmpbuttn.cpp
151ccd11
RR
3// Purpose:
4// Author: Robert Roebling
f96aa4d9 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
151ccd11
RR
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2 10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
151ccd11
RR
11#pragma implementation "bmpbuttn.h"
12#endif
13
14f355c2
VS
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
1e6feb95 17#include "wx/defs.h"
151ccd11 18
dcf924a3
RR
19#if wxUSE_BMPBUTTON
20
1e6feb95
VZ
21#include "wx/bmpbuttn.h"
22
9e691f46 23#include "wx/gtk/private.h"
83624f79 24
151ccd11
RR
25//-----------------------------------------------------------------------------
26// classes
27//-----------------------------------------------------------------------------
28
29class wxBitmapButton;
30
acfd422a
RR
31//-----------------------------------------------------------------------------
32// idle system
33//-----------------------------------------------------------------------------
34
35extern void wxapp_install_idle_handler();
36extern bool g_isIdle;
37
66bd6b93
RR
38//-----------------------------------------------------------------------------
39// data
40//-----------------------------------------------------------------------------
41
42extern bool g_blockEventsOnDrag;
43
151ccd11 44//-----------------------------------------------------------------------------
e1e955e1 45// "clicked"
151ccd11
RR
46//-----------------------------------------------------------------------------
47
865bb325 48extern "C" {
66bd6b93 49static void gtk_bmpbutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
151ccd11 50{
f6bcfd97 51 if (g_isIdle)
bf9e3e73 52 wxapp_install_idle_handler();
acfd422a 53
a2053b27 54 if (!button->m_hasVMT) return;
43a18898 55 if (g_blockEventsOnDrag) return;
f6bcfd97 56
43a18898
RR
57 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
58 event.SetEventObject(button);
59 button->GetEventHandler()->ProcessEvent(event);
60}
865bb325 61}
43a18898
RR
62
63//-----------------------------------------------------------------------------
64// "enter"
65//-----------------------------------------------------------------------------
66
865bb325 67extern "C" {
43a18898
RR
68static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
69{
a2053b27 70 if (!button->m_hasVMT) return;
43a18898
RR
71 if (g_blockEventsOnDrag) return;
72
f6bcfd97 73 button->HasFocus();
43a18898 74}
865bb325 75}
43a18898
RR
76
77//-----------------------------------------------------------------------------
78// "leave"
79//-----------------------------------------------------------------------------
80
865bb325 81extern "C" {
43a18898
RR
82static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
83{
a2053b27 84 if (!button->m_hasVMT) return;
43a18898
RR
85 if (g_blockEventsOnDrag) return;
86
f6bcfd97 87 button->NotFocus();
43a18898 88}
865bb325 89}
43a18898
RR
90
91//-----------------------------------------------------------------------------
92// "pressed"
93//-----------------------------------------------------------------------------
94
865bb325 95extern "C" {
43a18898
RR
96static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
97{
a2053b27 98 if (!button->m_hasVMT) return;
43a18898
RR
99 if (g_blockEventsOnDrag) return;
100
f6bcfd97 101 button->StartSelect();
43a18898 102}
865bb325 103}
43a18898
RR
104
105//-----------------------------------------------------------------------------
106// "released"
107//-----------------------------------------------------------------------------
108
865bb325 109extern "C" {
43a18898
RR
110static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
111{
a2053b27 112 if (!button->m_hasVMT) return;
43a18898
RR
113 if (g_blockEventsOnDrag) return;
114
f6bcfd97 115 button->EndSelect();
6de97a3b 116}
865bb325 117}
151ccd11
RR
118
119//-----------------------------------------------------------------------------
e1e955e1
RR
120// wxBitmapButton
121//-----------------------------------------------------------------------------
122
42b4e99e 123IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxButton)
151ccd11 124
c3dfaa10 125void wxBitmapButton::Init()
151ccd11 126{
c3dfaa10
VZ
127 m_hasFocus =
128 m_isSelected = FALSE;
6de97a3b 129}
151ccd11 130
c3dfaa10
VZ
131bool 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 )
151ccd11 139{
43a18898 140 m_needParent = TRUE;
b292e2f5 141 m_acceptsFocus = TRUE;
f6bcfd97 142
4dcaf11a
RR
143 if (!PreCreation( parent, pos, size ) ||
144 !CreateBase( parent, id, pos, size, style, validator, name ))
145 {
223d09f6 146 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
f6bcfd97 147 return FALSE;
4dcaf11a 148 }
6de97a3b 149
189f58fa 150 m_bmpNormal = bitmap;
f6bcfd97 151
43a18898 152 m_widget = gtk_button_new();
de1c750f 153
de1c750f
RR
154 if (style & wxNO_BORDER)
155 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 156
1e6feb95 157 if (m_bmpNormal.Ok())
43a18898 158 {
29149a64 159 OnSetBitmap();
43a18898 160 }
f6bcfd97 161
58b907f6 162 gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked",
43a18898 163 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
151ccd11 164
f6bcfd97 165 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
43a18898 166 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
f6bcfd97 167 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
43a18898 168 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
f6bcfd97 169 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
43a18898 170 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
f6bcfd97 171 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
43a18898 172 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
f6bcfd97 173
eb082a08 174 m_parent->DoAddChild( this );
f6bcfd97 175
abdeb9e7 176 PostCreation(size);
f6bcfd97 177
43a18898 178 return TRUE;
6de97a3b 179}
f6bcfd97 180
43a18898 181void wxBitmapButton::SetDefault()
151ccd11 182{
43a18898
RR
183 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
184 gtk_widget_grab_default( m_widget );
f6bcfd97 185
3502e687 186 SetSize( m_x, m_y, m_width, m_height );
6de97a3b 187}
151ccd11
RR
188
189void wxBitmapButton::SetLabel( const wxString &label )
190{
223d09f6 191 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
f96aa4d9 192
43a18898 193 wxControl::SetLabel( label );
6de97a3b 194}
151ccd11 195
43a18898 196wxString wxBitmapButton::GetLabel() const
151ccd11 197{
223d09f6 198 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid button") );
f96aa4d9 199
43a18898 200 return wxControl::GetLabel();
6de97a3b 201}
903f689b 202
f40fdaa3 203void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
903f689b 204{
9e691f46
VZ
205 if ( !BUTTON_CHILD(m_widget) )
206 return;
29149a64 207
f40fdaa3 208 wxButton::DoApplyWidgetStyle(style);
43a18898
RR
209}
210
29149a64 211void wxBitmapButton::OnSetBitmap()
43a18898 212{
1e6feb95 213 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
f96aa4d9 214
9f884528
RD
215 InvalidateBestSize();
216
43a18898 217 wxBitmap the_one;
f6bcfd97 218 if (!m_isEnabled)
29149a64 219 the_one = m_bmpDisabled;
67a6726a 220 else if (m_isSelected)
29149a64 221 the_one = m_bmpSelected;
67a6726a 222 else if (m_hasFocus)
29149a64 223 the_one = m_bmpFocus;
f6bcfd97 224 else
1e6feb95
VZ
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 }
43a18898 238
1e6feb95 239 if (!the_one.Ok()) the_one = m_bmpNormal;
de1c750f 240 if (!the_one.Ok()) return;
f6bcfd97 241
43a18898
RR
242 GdkBitmap *mask = (GdkBitmap *) NULL;
243 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
f6bcfd97 244
9e691f46
VZ
245 GtkWidget *child = BUTTON_CHILD(m_widget);
246 if (child == NULL)
29149a64
VZ
247 {
248 // initial bitmap
4fab7128
VS
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
67a6726a
RR
258 gtk_widget_show(pixmap);
259 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
29149a64
VZ
260 }
261 else
67a6726a 262 { // subsequent bitmaps
4fab7128
VS
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
67a6726a 273 }
903f689b
RR
274}
275
e0aeebed
VS
276wxSize wxBitmapButton::DoGetBestSize() const
277{
278 return wxControl::DoGetBestSize();
279}
280
f03fc89f 281bool wxBitmapButton::Enable( bool enable )
43a18898 282{
f03fc89f
VZ
283 if ( !wxWindow::Enable(enable) )
284 return FALSE;
43a18898 285
29149a64 286 OnSetBitmap();
f03fc89f
VZ
287
288 return TRUE;
43a18898
RR
289}
290
291void wxBitmapButton::HasFocus()
292{
293 m_hasFocus = TRUE;
29149a64 294 OnSetBitmap();
43a18898
RR
295}
296
297void wxBitmapButton::NotFocus()
298{
299 m_hasFocus = FALSE;
29149a64 300 OnSetBitmap();
43a18898
RR
301}
302
303void wxBitmapButton::StartSelect()
304{
305 m_isSelected = TRUE;
29149a64 306 OnSetBitmap();
43a18898
RR
307}
308
309void wxBitmapButton::EndSelect()
310{
311 m_isSelected = FALSE;
29149a64 312 OnSetBitmap();
43a18898 313}
f6bcfd97
BP
314
315#endif // wxUSE_BMPBUTTON
dcf924a3 316