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