Added wxWakeUpIdle() to wxGTK,
[wxWidgets.git] / src / gtk / 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)
47 wxapp_install_idle_handler();
48
49 if (!button->m_hasVMT) return;
50 if (g_blockEventsOnDrag) return;
51
52 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
53 event.SetEventObject(button);
54 button->GetEventHandler()->ProcessEvent(event);
55 }
56
57 //-----------------------------------------------------------------------------
58 // "enter"
59 //-----------------------------------------------------------------------------
60
61 static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
62 {
63 if (!button->m_hasVMT) return;
64 if (g_blockEventsOnDrag) return;
65
66 button->HasFocus();
67 }
68
69 //-----------------------------------------------------------------------------
70 // "leave"
71 //-----------------------------------------------------------------------------
72
73 static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
74 {
75 if (!button->m_hasVMT) return;
76 if (g_blockEventsOnDrag) return;
77
78 button->NotFocus();
79 }
80
81 //-----------------------------------------------------------------------------
82 // "pressed"
83 //-----------------------------------------------------------------------------
84
85 static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
86 {
87 if (!button->m_hasVMT) return;
88 if (g_blockEventsOnDrag) return;
89
90 button->StartSelect();
91 }
92
93 //-----------------------------------------------------------------------------
94 // "released"
95 //-----------------------------------------------------------------------------
96
97 static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
98 {
99 if (!button->m_hasVMT) return;
100 if (g_blockEventsOnDrag) return;
101
102 button->EndSelect();
103 }
104
105 //-----------------------------------------------------------------------------
106 // wxBitmapButton
107 //-----------------------------------------------------------------------------
108
109 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxButton)
110
111 wxBitmapButton::wxBitmapButton()
112 {
113 }
114
115 bool wxBitmapButton::Create( wxWindow *parent, wxWindowID id, const wxBitmap &bitmap,
116 const wxPoint &pos, const wxSize &size,
117 long style, const wxValidator& validator, const wxString &name )
118 {
119 m_needParent = TRUE;
120 m_acceptsFocus = TRUE;
121
122 if (!PreCreation( parent, pos, size ) ||
123 !CreateBase( parent, id, pos, size, style, validator, name ))
124 {
125 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
126 return FALSE;
127 }
128
129 m_bitmap = bitmap;
130 m_disabled = bitmap;
131 m_focus = bitmap;
132 m_selected = bitmap;
133
134 m_label = "";
135
136 m_widget = gtk_button_new();
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
143 if (m_bitmap.Ok())
144 {
145 wxSize newSize = size;
146
147 GdkBitmap *mask = (GdkBitmap *) NULL;
148 if (m_bitmap.GetMask()) mask = m_bitmap.GetMask()->GetBitmap();
149 GtkWidget *pixmap = gtk_pixmap_new( m_bitmap.GetPixmap(), mask );
150
151 gtk_widget_show( pixmap );
152 gtk_container_add( GTK_CONTAINER(m_widget), pixmap );
153
154 int border = 10;
155 if (style & wxNO_BORDER) border = 4;
156 if (newSize.x == -1) newSize.x = m_bitmap.GetWidth()+border;
157 if (newSize.y == -1) newSize.y = m_bitmap.GetHeight()+border;
158 SetSize( newSize.x, newSize.y );
159 }
160
161 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
162 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
163
164 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
165 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
166 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
167 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
168 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
169 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
170 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
171 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
172
173 m_parent->DoAddChild( this );
174
175 PostCreation();
176
177 SetBackgroundColour( parent->GetBackgroundColour() );
178
179 Show( TRUE );
180
181 return TRUE;
182 }
183
184 void wxBitmapButton::SetDefault()
185 {
186 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
187 gtk_widget_grab_default( m_widget );
188
189 SetSize( m_x, m_y, m_width, m_height );
190 }
191
192 void wxBitmapButton::SetLabel( const wxString &label )
193 {
194 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
195
196 wxControl::SetLabel( label );
197 }
198
199 wxString wxBitmapButton::GetLabel() const
200 {
201 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid button") );
202
203 return wxControl::GetLabel();
204 }
205
206 void wxBitmapButton::ApplyWidgetStyle()
207 {
208 }
209
210 void wxBitmapButton::SetBitmap()
211 {
212 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
213
214 wxBitmap the_one;
215
216 if (!m_isEnabled)
217 the_one = m_disabled;
218 else
219 {
220 if (m_isSelected)
221 {
222 the_one = m_selected;
223 }
224 else
225 {
226 if (m_hasFocus)
227 the_one = m_focus;
228 else
229 the_one = m_bitmap;
230 }
231 }
232
233 if (!the_one.Ok()) the_one = m_bitmap;
234 if (!the_one.Ok()) return;
235
236 GtkButton *bin = GTK_BUTTON( m_widget );
237 GtkPixmap *g_pixmap = GTK_PIXMAP( bin->child );
238
239 GdkBitmap *mask = (GdkBitmap *) NULL;
240 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
241
242 gtk_pixmap_set( g_pixmap, the_one.GetPixmap(), mask );
243 }
244
245 void wxBitmapButton::SetBitmapDisabled( const wxBitmap& bitmap )
246 {
247 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
248
249 if ( ! m_disabled.Ok() ) return;
250 m_disabled = bitmap;
251
252 SetBitmap();
253 }
254
255 void wxBitmapButton::SetBitmapFocus( const wxBitmap& bitmap )
256 {
257 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
258
259 if ( ! m_focus.Ok() ) return;
260 m_focus = bitmap;
261
262 SetBitmap();
263 }
264
265 void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap )
266 {
267 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
268
269 if (!m_bitmap.Ok()) return;
270 m_bitmap = bitmap;
271
272 SetBitmap();
273 }
274
275 void wxBitmapButton::SetBitmapSelected( const wxBitmap& bitmap )
276 {
277 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
278
279 if ( ! m_selected.Ok() ) return;
280 m_selected = bitmap;
281
282 SetBitmap();
283 }
284
285 bool wxBitmapButton::Enable( bool enable )
286 {
287 if ( !wxWindow::Enable(enable) )
288 return FALSE;
289
290 SetBitmap();
291
292 return TRUE;
293 }
294
295 void wxBitmapButton::HasFocus()
296 {
297 m_hasFocus = TRUE;
298 SetBitmap();
299 }
300
301 void wxBitmapButton::NotFocus()
302 {
303 m_hasFocus = FALSE;
304 SetBitmap();
305 }
306
307 void wxBitmapButton::StartSelect()
308 {
309 m_isSelected = TRUE;
310 SetBitmap();
311 }
312
313 void wxBitmapButton::EndSelect()
314 {
315 m_isSelected = FALSE;
316 SetBitmap();
317 }
318 #endif
319