]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bmpbuttn.cpp
buttons now become default when they have focus
[wxWidgets.git] / src / gtk / bmpbuttn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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) wxapp_install_idle_handler();
47
48 if (!button->m_hasVMT) return;
49 if (g_blockEventsOnDrag) return;
50
51 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
52 event.SetEventObject(button);
53 button->GetEventHandler()->ProcessEvent(event);
54 }
55
56 //-----------------------------------------------------------------------------
57 // "enter"
58 //-----------------------------------------------------------------------------
59
60 static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
61 {
62 if (!button->m_hasVMT) return;
63 if (g_blockEventsOnDrag) return;
64
65 button->HasFocus();
66 }
67
68 //-----------------------------------------------------------------------------
69 // "leave"
70 //-----------------------------------------------------------------------------
71
72 static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
73 {
74 if (!button->m_hasVMT) return;
75 if (g_blockEventsOnDrag) return;
76
77 button->NotFocus();
78 }
79
80 //-----------------------------------------------------------------------------
81 // "pressed"
82 //-----------------------------------------------------------------------------
83
84 static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
85 {
86 if (!button->m_hasVMT) return;
87 if (g_blockEventsOnDrag) return;
88
89 button->StartSelect();
90 }
91
92 //-----------------------------------------------------------------------------
93 // "released"
94 //-----------------------------------------------------------------------------
95
96 static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
97 {
98 if (!button->m_hasVMT) return;
99 if (g_blockEventsOnDrag) return;
100
101 button->EndSelect();
102 }
103
104 //-----------------------------------------------------------------------------
105 // wxBitmapButton
106 //-----------------------------------------------------------------------------
107
108 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxControl)
109
110 wxBitmapButton::wxBitmapButton()
111 {
112 }
113
114 bool wxBitmapButton::Create( wxWindow *parent, wxWindowID id, const wxBitmap &bitmap,
115 const wxPoint &pos, const wxSize &size,
116 long style, const wxValidator& validator, const wxString &name )
117 {
118 m_needParent = TRUE;
119 m_acceptsFocus = TRUE;
120
121 wxSize newSize = size;
122
123 PreCreation( parent, id, pos, newSize, style, name );
124
125 #if wxUSE_VALIDATORS
126 SetValidator( validator );
127 #endif
128
129 m_bitmap = bitmap;
130 m_disabled = bitmap;
131 m_focus = bitmap;
132 m_selected = bitmap;
133
134 m_label = "";
135
136 m_widget = gtk_button_new();
137
138 #if (GTK_MINOR_VERSION > 0)
139 if (style & wxNO_BORDER)
140 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
141 #endif
142
143 if (m_bitmap.Ok())
144 {
145 GdkBitmap *mask = (GdkBitmap *) NULL;
146 if (m_bitmap.GetMask()) mask = m_bitmap.GetMask()->GetBitmap();
147 GtkWidget *pixmap = gtk_pixmap_new( m_bitmap.GetPixmap(), mask );
148
149 gtk_widget_show( pixmap );
150 gtk_container_add( GTK_CONTAINER(m_widget), pixmap );
151 }
152
153 int border = 10;
154 if (style & wxNO_BORDER) border = 4;
155 if (newSize.x == -1) newSize.x = m_bitmap.GetWidth()+border;
156 if (newSize.y == -1) newSize.y = m_bitmap.GetHeight()+border;
157 SetSize( newSize.x, newSize.y );
158
159 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
160 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
161
162 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
163 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
164 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
165 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
166 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
167 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
168 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
169 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
170
171 m_parent->DoAddChild( this );
172
173 PostCreation();
174
175 SetBackgroundColour( parent->GetBackgroundColour() );
176
177 Show( TRUE );
178
179 return TRUE;
180 }
181
182 void wxBitmapButton::SetDefault()
183 {
184 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
185 gtk_widget_grab_default( m_widget );
186
187 SetSize( m_x, m_y, m_width, m_height );
188 }
189
190 void wxBitmapButton::SetLabel( const wxString &label )
191 {
192 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
193
194 wxControl::SetLabel( label );
195 }
196
197 wxString wxBitmapButton::GetLabel() const
198 {
199 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid button") );
200
201 return wxControl::GetLabel();
202 }
203
204 void wxBitmapButton::ApplyWidgetStyle()
205 {
206 }
207
208 void wxBitmapButton::SetBitmap()
209 {
210 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
211
212 wxBitmap the_one;
213
214 if (!m_isEnabled)
215 the_one = m_disabled;
216 else
217 {
218 if (m_isSelected)
219 {
220 the_one = m_selected;
221 }
222 else
223 {
224 if (m_hasFocus)
225 the_one = m_focus;
226 else
227 the_one = m_bitmap;
228 }
229 }
230
231 if (!the_one.Ok()) the_one = m_bitmap;
232 if (!the_one.Ok()) return;
233
234 GtkButton *bin = GTK_BUTTON( m_widget );
235 GtkPixmap *g_pixmap = GTK_PIXMAP( bin->child );
236
237 GdkBitmap *mask = (GdkBitmap *) NULL;
238 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
239
240 gtk_pixmap_set( g_pixmap, the_one.GetPixmap(), mask );
241 }
242
243 void wxBitmapButton::SetBitmapDisabled( const wxBitmap& bitmap )
244 {
245 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
246
247 if ( ! m_disabled.Ok() ) return;
248 m_disabled = bitmap;
249
250 SetBitmap();
251 }
252
253 void wxBitmapButton::SetBitmapFocus( const wxBitmap& bitmap )
254 {
255 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
256
257 if ( ! m_focus.Ok() ) return;
258 m_focus = bitmap;
259
260 SetBitmap();
261 }
262
263 void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap )
264 {
265 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
266
267 if (!m_bitmap.Ok()) return;
268 m_bitmap = bitmap;
269
270 SetBitmap();
271 }
272
273 void wxBitmapButton::SetBitmapSelected( const wxBitmap& bitmap )
274 {
275 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
276
277 if ( ! m_selected.Ok() ) return;
278 m_selected = bitmap;
279
280 SetBitmap();
281 }
282
283 bool wxBitmapButton::Enable( bool enable )
284 {
285 if ( !wxWindow::Enable(enable) )
286 return FALSE;
287
288 SetBitmap();
289
290 return TRUE;
291 }
292
293 void wxBitmapButton::HasFocus()
294 {
295 m_hasFocus = TRUE;
296 SetBitmap();
297 }
298
299 void wxBitmapButton::NotFocus()
300 {
301 m_hasFocus = FALSE;
302 SetBitmap();
303 }
304
305 void wxBitmapButton::StartSelect()
306 {
307 m_isSelected = TRUE;
308 SetBitmap();
309 }
310
311 void wxBitmapButton::EndSelect()
312 {
313 m_isSelected = FALSE;
314 SetBitmap();
315 }
316
317 #endif