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