]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bmpbuttn.cpp
Removed bug that made wxWindow call OnPaint
[wxWidgets.git] / src / gtk / bmpbuttn.cpp
CommitLineData
151ccd11
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: bmpbuttn.cpp
3// Purpose:
4// Author: Robert Roebling
f96aa4d9 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
151ccd11
RR
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "bmpbuttn.h"
12#endif
13
14#include "wx/bmpbuttn.h"
15
dcf924a3
RR
16#if wxUSE_BMPBUTTON
17
83624f79
RR
18#include "gdk/gdk.h"
19#include "gtk/gtk.h"
20
151ccd11
RR
21//-----------------------------------------------------------------------------
22// classes
23//-----------------------------------------------------------------------------
24
25class wxBitmapButton;
26
acfd422a
RR
27//-----------------------------------------------------------------------------
28// idle system
29//-----------------------------------------------------------------------------
30
31extern void wxapp_install_idle_handler();
32extern bool g_isIdle;
33
66bd6b93
RR
34//-----------------------------------------------------------------------------
35// data
36//-----------------------------------------------------------------------------
37
38extern bool g_blockEventsOnDrag;
39
151ccd11 40//-----------------------------------------------------------------------------
e1e955e1 41// "clicked"
151ccd11
RR
42//-----------------------------------------------------------------------------
43
66bd6b93 44static void gtk_bmpbutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
151ccd11 45{
acfd422a
RR
46 if (g_isIdle) wxapp_install_idle_handler();
47
a2053b27 48 if (!button->m_hasVMT) return;
43a18898 49 if (g_blockEventsOnDrag) return;
66bd6b93 50
43a18898
RR
51 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
52 event.SetEventObject(button);
53 button->GetEventHandler()->ProcessEvent(event);
54}
55
56//-----------------------------------------------------------------------------
57// "enter"
58//-----------------------------------------------------------------------------
59
60static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
61{
a2053b27 62 if (!button->m_hasVMT) return;
43a18898
RR
63 if (g_blockEventsOnDrag) return;
64
65 button->HasFocus();
66}
67
68//-----------------------------------------------------------------------------
69// "leave"
70//-----------------------------------------------------------------------------
71
72static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
73{
a2053b27 74 if (!button->m_hasVMT) return;
43a18898
RR
75 if (g_blockEventsOnDrag) return;
76
77 button->NotFocus();
78}
79
80//-----------------------------------------------------------------------------
81// "pressed"
82//-----------------------------------------------------------------------------
83
84static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
85{
a2053b27 86 if (!button->m_hasVMT) return;
43a18898
RR
87 if (g_blockEventsOnDrag) return;
88
89 button->StartSelect();
90}
91
92//-----------------------------------------------------------------------------
93// "released"
94//-----------------------------------------------------------------------------
95
96static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
97{
a2053b27 98 if (!button->m_hasVMT) return;
43a18898
RR
99 if (g_blockEventsOnDrag) return;
100
101 button->EndSelect();
6de97a3b 102}
151ccd11
RR
103
104//-----------------------------------------------------------------------------
e1e955e1
RR
105// wxBitmapButton
106//-----------------------------------------------------------------------------
107
108IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxControl)
151ccd11 109
43a18898 110wxBitmapButton::wxBitmapButton()
151ccd11 111{
6de97a3b 112}
151ccd11 113
43a18898
RR
114bool wxBitmapButton::Create( wxWindow *parent, wxWindowID id, const wxBitmap &bitmap,
115 const wxPoint &pos, const wxSize &size,
116 long style, const wxValidator& validator, const wxString &name )
151ccd11 117{
43a18898 118 m_needParent = TRUE;
b292e2f5 119 m_acceptsFocus = TRUE;
151ccd11 120
43a18898 121 wxSize newSize = size;
151ccd11 122
43a18898 123 PreCreation( parent, id, pos, newSize, style, name );
151ccd11 124
ce4169a4 125#if wxUSE_VALIDATORS
43a18898 126 SetValidator( validator );
ce4169a4 127#endif
6de97a3b 128
43a18898
RR
129 m_bitmap = bitmap;
130 m_disabled = bitmap;
131 m_focus = bitmap;
132 m_selected = bitmap;
151ccd11 133
43a18898 134 m_label = "";
151ccd11 135
43a18898 136 m_widget = gtk_button_new();
de1c750f
RR
137
138#if (GTK_MINOR_VERSION > 0)
139 if (style & wxNO_BORDER)
140 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
141#endif
142
43a18898
RR
143 if (m_bitmap.Ok())
144 {
145 GdkBitmap *mask = (GdkBitmap *) NULL;
146 if (m_bitmap.GetMask()) mask = m_bitmap.GetMask()->GetBitmap();
147 GtkWidget *pixmap = gtk_pixmap_new( m_bitmap.GetPixmap(), mask );
151ccd11 148
43a18898
RR
149 gtk_widget_show( pixmap );
150 gtk_container_add( GTK_CONTAINER(m_widget), pixmap );
d1af991f
RR
151
152 int border = 10;
153 if (style & wxNO_BORDER) border = 4;
154 if (newSize.x == -1) newSize.x = m_bitmap.GetWidth()+border;
155 if (newSize.y == -1) newSize.y = m_bitmap.GetHeight()+border;
156 SetSize( newSize.x, newSize.y );
43a18898 157 }
151ccd11 158
43a18898
RR
159 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
160 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
151ccd11 161
43a18898
RR
162 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
163 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
164 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
165 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
166 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
167 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
168 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
169 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
170
eb082a08 171 m_parent->DoAddChild( this );
6ca41e57 172
43a18898 173 PostCreation();
151ccd11 174
43a18898 175 SetBackgroundColour( parent->GetBackgroundColour() );
f96aa4d9 176
43a18898 177 Show( TRUE );
151ccd11 178
43a18898 179 return TRUE;
6de97a3b 180}
151ccd11 181
43a18898 182void wxBitmapButton::SetDefault()
151ccd11 183{
43a18898
RR
184 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
185 gtk_widget_grab_default( m_widget );
3502e687
RR
186
187 SetSize( m_x, m_y, m_width, m_height );
6de97a3b 188}
151ccd11
RR
189
190void wxBitmapButton::SetLabel( const wxString &label )
191{
62bd5cf0 192 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
f96aa4d9 193
43a18898 194 wxControl::SetLabel( label );
6de97a3b 195}
151ccd11 196
43a18898 197wxString wxBitmapButton::GetLabel() const
151ccd11 198{
62bd5cf0 199 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid button") );
f96aa4d9 200
43a18898 201 return wxControl::GetLabel();
6de97a3b 202}
903f689b 203
43a18898 204void wxBitmapButton::ApplyWidgetStyle()
903f689b 205{
43a18898
RR
206}
207
208void wxBitmapButton::SetBitmap()
209{
62bd5cf0 210 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
f96aa4d9 211
43a18898 212 wxBitmap the_one;
903f689b 213
de1c750f 214 if (!m_isEnabled)
43a18898
RR
215 the_one = m_disabled;
216 else
217 {
de1c750f 218 if (m_isSelected)
43a18898
RR
219 {
220 the_one = m_selected;
221 }
222 else
223 {
de1c750f 224 if (m_hasFocus)
43a18898
RR
225 the_one = m_focus;
226 else
227 the_one = m_bitmap;
228 }
229 }
230
de1c750f
RR
231 if (!the_one.Ok()) the_one = m_bitmap;
232 if (!the_one.Ok()) return;
903f689b 233
43a18898
RR
234 GtkButton *bin = GTK_BUTTON( m_widget );
235 GtkPixmap *g_pixmap = GTK_PIXMAP( bin->child );
903f689b 236
43a18898
RR
237 GdkBitmap *mask = (GdkBitmap *) NULL;
238 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
239
240 gtk_pixmap_set( g_pixmap, the_one.GetPixmap(), mask );
903f689b
RR
241}
242
43a18898 243void wxBitmapButton::SetBitmapDisabled( const wxBitmap& bitmap )
58614078 244{
62bd5cf0 245 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
43a18898
RR
246
247 if ( ! m_disabled.Ok() ) return;
248 m_disabled = bitmap;
249
250 SetBitmap();
58614078
RR
251}
252
43a18898
RR
253void wxBitmapButton::SetBitmapFocus( const wxBitmap& bitmap )
254{
62bd5cf0 255 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
43a18898
RR
256
257 if ( ! m_focus.Ok() ) return;
258 m_focus = bitmap;
259
260 SetBitmap();
261}
262
263void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap )
264{
62bd5cf0 265 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
43a18898
RR
266
267 if (!m_bitmap.Ok()) return;
268 m_bitmap = bitmap;
269
270 SetBitmap();
271}
272
273void wxBitmapButton::SetBitmapSelected( const wxBitmap& bitmap )
274{
62bd5cf0 275 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
43a18898
RR
276
277 if ( ! m_selected.Ok() ) return;
278 m_selected = bitmap;
279
280 SetBitmap();
281}
282
f03fc89f 283bool wxBitmapButton::Enable( bool enable )
43a18898 284{
f03fc89f
VZ
285 if ( !wxWindow::Enable(enable) )
286 return FALSE;
43a18898 287
f03fc89f
VZ
288 SetBitmap();
289
290 return TRUE;
43a18898
RR
291}
292
293void wxBitmapButton::HasFocus()
294{
295 m_hasFocus = TRUE;
296 SetBitmap();
297}
298
299void wxBitmapButton::NotFocus()
300{
301 m_hasFocus = FALSE;
302 SetBitmap();
303}
304
305void wxBitmapButton::StartSelect()
306{
307 m_isSelected = TRUE;
308 SetBitmap();
309}
310
311void wxBitmapButton::EndSelect()
312{
313 m_isSelected = FALSE;
314 SetBitmap();
315}
dcf924a3
RR
316
317#endif