Added Greg's wxBitmapButton patch, take 2.
[wxWidgets.git] / src / gtk1 / 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/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 m_marginX =
123 m_marginY = 0;
124
125 if (!PreCreation( parent, pos, size ) ||
126 !CreateBase( parent, id, pos, size, style, validator, name ))
127 {
128 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
129 return FALSE;
130 }
131
132 m_bitmap = bitmap;
133 m_disabled = bitmap;
134 m_focus = bitmap;
135 m_selected = bitmap;
136
137 m_widget = gtk_button_new();
138
139 #if (GTK_MINOR_VERSION > 0)
140 if (style & wxNO_BORDER)
141 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
142 #endif
143
144 if (m_bitmap.Ok())
145 {
146 wxSize newSize = size;
147 int border = (style & wxNO_BORDER) ? 4 : 10;
148 if (newSize.x == -1) newSize.x = m_bitmap.GetWidth()+border;
149 if (newSize.y == -1) newSize.y = m_bitmap.GetHeight()+border;
150 SetSize( newSize.x, newSize.y );
151 SetBitmap();
152 }
153
154 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
155 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
156
157 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
158 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
159 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
160 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
161 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
162 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
163 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
164 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
165
166 m_parent->DoAddChild( this );
167
168 PostCreation();
169
170 SetBackgroundColour( parent->GetBackgroundColour() );
171
172 Show( TRUE );
173
174 return TRUE;
175 }
176
177 void wxBitmapButton::SetDefault()
178 {
179 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
180 gtk_widget_grab_default( m_widget );
181
182 SetSize( m_x, m_y, m_width, m_height );
183 }
184
185 void wxBitmapButton::SetLabel( const wxString &label )
186 {
187 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
188
189 wxControl::SetLabel( label );
190 }
191
192 wxString wxBitmapButton::GetLabel() const
193 {
194 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid button") );
195
196 return wxControl::GetLabel();
197 }
198
199 void wxBitmapButton::ApplyWidgetStyle()
200 {
201 if (GTK_BUTTON(m_widget)->child == NULL) return;
202
203 wxButton::ApplyWidgetStyle();
204 }
205
206 void wxBitmapButton::SetBitmap()
207 {
208 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
209
210 wxBitmap the_one;
211 if (!m_isEnabled)
212 the_one = m_disabled;
213 else if (m_isSelected)
214 the_one = m_selected;
215 else if (m_hasFocus)
216 the_one = m_focus;
217 else
218 the_one = m_bitmap;
219
220 if (!the_one.Ok()) the_one = m_bitmap;
221 if (!the_one.Ok()) return;
222
223 GdkBitmap *mask = (GdkBitmap *) NULL;
224 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
225
226 GtkButton *bin = GTK_BUTTON(m_widget);
227 if (bin->child == NULL)
228 { // initial bitmap
229 GtkWidget *pixmap = gtk_pixmap_new(the_one.GetPixmap(), mask);
230 gtk_widget_show(pixmap);
231 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
232 }
233 else
234 { // subsequent bitmaps
235 GtkPixmap *g_pixmap = GTK_PIXMAP(bin->child);
236 gtk_pixmap_set(g_pixmap, the_one.GetPixmap(), mask);
237 }
238 }
239
240 void wxBitmapButton::SetBitmapDisabled( const wxBitmap& bitmap )
241 {
242 if (!bitmap.Ok()) return;
243 m_disabled = bitmap;
244
245 SetBitmap();
246 }
247
248 void wxBitmapButton::SetBitmapFocus( const wxBitmap& bitmap )
249 {
250 if (!bitmap.Ok()) return;
251 m_focus = bitmap;
252
253 SetBitmap();
254 }
255
256 void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap )
257 {
258 if (!bitmap.Ok()) return;
259 m_bitmap = bitmap;
260
261 SetBitmap();
262 }
263
264 void wxBitmapButton::SetBitmapSelected( const wxBitmap& bitmap )
265 {
266 if (!bitmap.Ok()) return;
267 m_selected = bitmap;
268
269 SetBitmap();
270 }
271
272 bool wxBitmapButton::Enable( bool enable )
273 {
274 if ( !wxWindow::Enable(enable) )
275 return FALSE;
276
277 SetBitmap();
278
279 return TRUE;
280 }
281
282 void wxBitmapButton::HasFocus()
283 {
284 m_hasFocus = TRUE;
285 SetBitmap();
286 }
287
288 void wxBitmapButton::NotFocus()
289 {
290 m_hasFocus = FALSE;
291 SetBitmap();
292 }
293
294 void wxBitmapButton::StartSelect()
295 {
296 m_isSelected = TRUE;
297 SetBitmap();
298 }
299
300 void wxBitmapButton::EndSelect()
301 {
302 m_isSelected = FALSE;
303 SetBitmap();
304 }
305
306 #endif // wxUSE_BMPBUTTON
307