]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/bmpbuttn.cpp
applied patch 528960 (a few minor bug fixes)
[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/defs.h"
15
16 #if wxUSE_BMPBUTTON
17
18 #include "wx/bmpbuttn.h"
19
20 #include "wx/gtk/private.h"
21
22 //-----------------------------------------------------------------------------
23 // classes
24 //-----------------------------------------------------------------------------
25
26 class wxBitmapButton;
27
28 //-----------------------------------------------------------------------------
29 // idle system
30 //-----------------------------------------------------------------------------
31
32 extern void wxapp_install_idle_handler();
33 extern bool g_isIdle;
34
35 //-----------------------------------------------------------------------------
36 // data
37 //-----------------------------------------------------------------------------
38
39 extern bool g_blockEventsOnDrag;
40
41 //-----------------------------------------------------------------------------
42 // "clicked"
43 //-----------------------------------------------------------------------------
44
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 // "enter"
60 //-----------------------------------------------------------------------------
61
62 static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
63 {
64 if (!button->m_hasVMT) return;
65 if (g_blockEventsOnDrag) return;
66
67 button->HasFocus();
68 }
69
70 //-----------------------------------------------------------------------------
71 // "leave"
72 //-----------------------------------------------------------------------------
73
74 static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
75 {
76 if (!button->m_hasVMT) return;
77 if (g_blockEventsOnDrag) return;
78
79 button->NotFocus();
80 }
81
82 //-----------------------------------------------------------------------------
83 // "pressed"
84 //-----------------------------------------------------------------------------
85
86 static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
87 {
88 if (!button->m_hasVMT) return;
89 if (g_blockEventsOnDrag) return;
90
91 button->StartSelect();
92 }
93
94 //-----------------------------------------------------------------------------
95 // "released"
96 //-----------------------------------------------------------------------------
97
98 static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
99 {
100 if (!button->m_hasVMT) return;
101 if (g_blockEventsOnDrag) return;
102
103 button->EndSelect();
104 }
105
106 //-----------------------------------------------------------------------------
107 // wxBitmapButton
108 //-----------------------------------------------------------------------------
109
110 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxButton)
111
112 wxBitmapButton::wxBitmapButton()
113 {
114 }
115
116 bool wxBitmapButton::Create( wxWindow *parent, wxWindowID id, const wxBitmap &bitmap,
117 const wxPoint &pos, const wxSize &size,
118 long style, const wxValidator& validator, const wxString &name )
119 {
120 m_needParent = TRUE;
121 m_acceptsFocus = TRUE;
122
123 if (!PreCreation( parent, pos, size ) ||
124 !CreateBase( parent, id, pos, size, style, validator, name ))
125 {
126 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
127 return FALSE;
128 }
129
130 m_bmpNormal =
131 m_bmpDisabled =
132 m_bmpFocus =
133 m_bmpSelected = bitmap;
134
135 m_widget = gtk_button_new();
136
137 #if (GTK_MINOR_VERSION > 0)
138 if (style & wxNO_BORDER)
139 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
140 #endif
141
142 if (m_bmpNormal.Ok())
143 {
144 wxSize newSize = size;
145 int border = (style & wxNO_BORDER) ? 4 : 10;
146 if (newSize.x == -1)
147 newSize.x = m_bmpNormal.GetWidth()+border;
148 if (newSize.y == -1)
149 newSize.y = m_bmpNormal.GetHeight()+border;
150 SetSize( newSize.x, newSize.y );
151 OnSetBitmap();
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 ( !BUTTON_CHILD(m_widget) )
202 return;
203
204 wxButton::ApplyWidgetStyle();
205 }
206
207 void wxBitmapButton::OnSetBitmap()
208 {
209 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
210
211 wxBitmap the_one;
212 if (!m_isEnabled)
213 the_one = m_bmpDisabled;
214 else if (m_isSelected)
215 the_one = m_bmpSelected;
216 else if (m_hasFocus)
217 the_one = m_bmpFocus;
218 else
219 {
220 if (m_isSelected)
221 {
222 the_one = m_bmpSelected;
223 }
224 else
225 {
226 if (m_hasFocus)
227 the_one = m_bmpFocus;
228 else
229 the_one = m_bmpNormal;
230 }
231 }
232
233 if (!the_one.Ok()) the_one = m_bmpNormal;
234 if (!the_one.Ok()) return;
235
236 GdkBitmap *mask = (GdkBitmap *) NULL;
237 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
238
239 GtkWidget *child = BUTTON_CHILD(m_widget);
240 if (child == NULL)
241 {
242 // initial bitmap
243 GtkWidget *pixmap = gtk_pixmap_new(the_one.GetPixmap(), mask);
244 gtk_widget_show(pixmap);
245 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
246 }
247 else
248 { // subsequent bitmaps
249 GtkPixmap *g_pixmap = GTK_PIXMAP(child);
250 gtk_pixmap_set(g_pixmap, the_one.GetPixmap(), mask);
251 }
252 }
253
254 bool wxBitmapButton::Enable( bool enable )
255 {
256 if ( !wxWindow::Enable(enable) )
257 return FALSE;
258
259 OnSetBitmap();
260
261 return TRUE;
262 }
263
264 void wxBitmapButton::HasFocus()
265 {
266 m_hasFocus = TRUE;
267 OnSetBitmap();
268 }
269
270 void wxBitmapButton::NotFocus()
271 {
272 m_hasFocus = FALSE;
273 OnSetBitmap();
274 }
275
276 void wxBitmapButton::StartSelect()
277 {
278 m_isSelected = TRUE;
279 OnSetBitmap();
280 }
281
282 void wxBitmapButton::EndSelect()
283 {
284 m_isSelected = FALSE;
285 OnSetBitmap();
286 }
287
288 #endif // wxUSE_BMPBUTTON
289