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