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