]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bmpbuttn.cpp
SetLabel() no longer invalidates the font which
[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 #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 static void gtk_bmpbutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
49 {
50 if (g_isIdle)
51 wxapp_install_idle_handler();
52
53 if (!button->m_hasVMT) return;
54 if (g_blockEventsOnDrag) return;
55
56 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
57 event.SetEventObject(button);
58 button->GetEventHandler()->ProcessEvent(event);
59 }
60
61 //-----------------------------------------------------------------------------
62 // "enter"
63 //-----------------------------------------------------------------------------
64
65 static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
66 {
67 if (!button->m_hasVMT) return;
68 if (g_blockEventsOnDrag) return;
69
70 button->HasFocus();
71 }
72
73 //-----------------------------------------------------------------------------
74 // "leave"
75 //-----------------------------------------------------------------------------
76
77 static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
78 {
79 if (!button->m_hasVMT) return;
80 if (g_blockEventsOnDrag) return;
81
82 button->NotFocus();
83 }
84
85 //-----------------------------------------------------------------------------
86 // "pressed"
87 //-----------------------------------------------------------------------------
88
89 static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
90 {
91 if (!button->m_hasVMT) return;
92 if (g_blockEventsOnDrag) return;
93
94 button->StartSelect();
95 }
96
97 //-----------------------------------------------------------------------------
98 // "released"
99 //-----------------------------------------------------------------------------
100
101 static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
102 {
103 if (!button->m_hasVMT) return;
104 if (g_blockEventsOnDrag) return;
105
106 button->EndSelect();
107 }
108
109 //-----------------------------------------------------------------------------
110 // wxBitmapButton
111 //-----------------------------------------------------------------------------
112
113 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxButton)
114
115 void wxBitmapButton::Init()
116 {
117 m_hasFocus =
118 m_isSelected = FALSE;
119 }
120
121 bool wxBitmapButton::Create( wxWindow *parent,
122 wxWindowID id,
123 const wxBitmap& bitmap,
124 const wxPoint& pos,
125 const wxSize& size,
126 long style,
127 const wxValidator& validator,
128 const wxString &name )
129 {
130 m_needParent = TRUE;
131 m_acceptsFocus = TRUE;
132
133 if (!PreCreation( parent, pos, size ) ||
134 !CreateBase( parent, id, pos, size, style, validator, name ))
135 {
136 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
137 return FALSE;
138 }
139
140 m_bmpNormal = bitmap;
141
142 m_widget = gtk_button_new();
143
144 if (style & wxNO_BORDER)
145 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
146
147 if (m_bmpNormal.Ok())
148 {
149 OnSetBitmap();
150 }
151
152 gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked",
153 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
154
155 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
156 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
157 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
158 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
159 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
160 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
161 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
162 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
163
164 m_parent->DoAddChild( this );
165
166 PostCreation(size);
167
168 return TRUE;
169 }
170
171 void wxBitmapButton::SetDefault()
172 {
173 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
174 gtk_widget_grab_default( m_widget );
175
176 SetSize( m_x, m_y, m_width, m_height );
177 }
178
179 void wxBitmapButton::SetLabel( const wxString &label )
180 {
181 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
182
183 wxControl::SetLabel( label );
184 }
185
186 wxString wxBitmapButton::GetLabel() const
187 {
188 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid button") );
189
190 return wxControl::GetLabel();
191 }
192
193 void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
194 {
195 if ( !BUTTON_CHILD(m_widget) )
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 {
216 if (m_isSelected)
217 {
218 the_one = m_bmpSelected;
219 }
220 else
221 {
222 if (m_hasFocus)
223 the_one = m_bmpFocus;
224 else
225 the_one = m_bmpNormal;
226 }
227 }
228
229 if (!the_one.Ok()) the_one = m_bmpNormal;
230 if (!the_one.Ok()) return;
231
232 GdkBitmap *mask = (GdkBitmap *) NULL;
233 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
234
235 GtkWidget *child = BUTTON_CHILD(m_widget);
236 if (child == NULL)
237 {
238 // initial bitmap
239 GtkWidget *pixmap;
240 #ifdef __WXGTK20__
241 if (the_one.HasPixbuf())
242 pixmap = gtk_image_new_from_pixbuf(the_one.GetPixbuf());
243 else
244 pixmap = gtk_image_new_from_pixmap(the_one.GetPixmap(), mask);
245 #else
246 pixmap = gtk_pixmap_new(the_one.GetPixmap(), mask);
247 #endif
248 gtk_widget_show(pixmap);
249 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
250 }
251 else
252 { // subsequent bitmaps
253 #ifdef __WXGTK20__
254 GtkImage *pixmap = GTK_IMAGE(child);
255 if (the_one.HasPixbuf())
256 gtk_image_set_from_pixbuf(pixmap, the_one.GetPixbuf());
257 else
258 gtk_image_set_from_pixmap(pixmap, the_one.GetPixmap(), mask);
259 #else
260 GtkPixmap *pixmap = GTK_PIXMAP(child);
261 gtk_pixmap_set(pixmap, the_one.GetPixmap(), mask);
262 #endif
263 }
264 }
265
266 wxSize wxBitmapButton::DoGetBestSize() const
267 {
268 return wxControl::DoGetBestSize();
269 }
270
271 bool wxBitmapButton::Enable( bool enable )
272 {
273 if ( !wxWindow::Enable(enable) )
274 return FALSE;
275
276 OnSetBitmap();
277
278 return TRUE;
279 }
280
281 void wxBitmapButton::HasFocus()
282 {
283 m_hasFocus = TRUE;
284 OnSetBitmap();
285 }
286
287 void wxBitmapButton::NotFocus()
288 {
289 m_hasFocus = FALSE;
290 OnSetBitmap();
291 }
292
293 void wxBitmapButton::StartSelect()
294 {
295 m_isSelected = TRUE;
296 OnSetBitmap();
297 }
298
299 void wxBitmapButton::EndSelect()
300 {
301 m_isSelected = FALSE;
302 OnSetBitmap();
303 }
304
305 #endif // wxUSE_BMPBUTTON
306