Added zillions of #if wxUSE_XXX
[wxWidgets.git] / src / gtk1 / 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 SetValidator( validator );
126
127 m_bitmap = bitmap;
128 m_disabled = bitmap;
129 m_focus = bitmap;
130 m_selected = bitmap;
131
132 m_label = "";
133
134 m_widget = gtk_button_new();
135
136 #if (GTK_MINOR_VERSION > 0)
137 if (style & wxNO_BORDER)
138 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
139 #endif
140
141 if (m_bitmap.Ok())
142 {
143 GdkBitmap *mask = (GdkBitmap *) NULL;
144 if (m_bitmap.GetMask()) mask = m_bitmap.GetMask()->GetBitmap();
145 GtkWidget *pixmap = gtk_pixmap_new( m_bitmap.GetPixmap(), mask );
146
147 gtk_widget_show( pixmap );
148 gtk_container_add( GTK_CONTAINER(m_widget), pixmap );
149 }
150
151 int border = 10;
152 if (style & wxNO_BORDER) border = 4;
153 if (newSize.x == -1) newSize.x = m_bitmap.GetWidth()+border;
154 if (newSize.y == -1) newSize.y = m_bitmap.GetHeight()+border;
155 SetSize( newSize.x, newSize.y );
156
157 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
158 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
159
160 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
161 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
162 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
163 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
164 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
165 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
166 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
167 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
168
169 m_parent->DoAddChild( this );
170
171 PostCreation();
172
173 SetBackgroundColour( parent->GetBackgroundColour() );
174
175 Show( TRUE );
176
177 return TRUE;
178 }
179
180 void wxBitmapButton::SetDefault()
181 {
182 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
183 gtk_widget_grab_default( m_widget );
184
185 SetSize( m_x, m_y, m_width, m_height );
186 }
187
188 void wxBitmapButton::SetLabel( const wxString &label )
189 {
190 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
191
192 wxControl::SetLabel( label );
193 }
194
195 wxString wxBitmapButton::GetLabel() const
196 {
197 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid button") );
198
199 return wxControl::GetLabel();
200 }
201
202 void wxBitmapButton::ApplyWidgetStyle()
203 {
204 }
205
206 void wxBitmapButton::SetBitmap()
207 {
208 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
209
210 wxBitmap the_one;
211
212 if (!m_isEnabled)
213 the_one = m_disabled;
214 else
215 {
216 if (m_isSelected)
217 {
218 the_one = m_selected;
219 }
220 else
221 {
222 if (m_hasFocus)
223 the_one = m_focus;
224 else
225 the_one = m_bitmap;
226 }
227 }
228
229 if (!the_one.Ok()) the_one = m_bitmap;
230 if (!the_one.Ok()) return;
231
232 GtkButton *bin = GTK_BUTTON( m_widget );
233 GtkPixmap *g_pixmap = GTK_PIXMAP( bin->child );
234
235 GdkBitmap *mask = (GdkBitmap *) NULL;
236 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
237
238 gtk_pixmap_set( g_pixmap, the_one.GetPixmap(), mask );
239 }
240
241 void wxBitmapButton::SetBitmapDisabled( const wxBitmap& bitmap )
242 {
243 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
244
245 if ( ! m_disabled.Ok() ) return;
246 m_disabled = bitmap;
247
248 SetBitmap();
249 }
250
251 void wxBitmapButton::SetBitmapFocus( const wxBitmap& bitmap )
252 {
253 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
254
255 if ( ! m_focus.Ok() ) return;
256 m_focus = bitmap;
257
258 SetBitmap();
259 }
260
261 void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap )
262 {
263 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
264
265 if (!m_bitmap.Ok()) return;
266 m_bitmap = bitmap;
267
268 SetBitmap();
269 }
270
271 void wxBitmapButton::SetBitmapSelected( const wxBitmap& bitmap )
272 {
273 wxCHECK_RET( m_widget != NULL, _T("invalid button") );
274
275 if ( ! m_selected.Ok() ) return;
276 m_selected = bitmap;
277
278 SetBitmap();
279 }
280
281 bool wxBitmapButton::Enable( bool enable )
282 {
283 if ( !wxWindow::Enable(enable) )
284 return FALSE;
285
286 SetBitmap();
287
288 return TRUE;
289 }
290
291 void wxBitmapButton::HasFocus()
292 {
293 m_hasFocus = TRUE;
294 SetBitmap();
295 }
296
297 void wxBitmapButton::NotFocus()
298 {
299 m_hasFocus = FALSE;
300 SetBitmap();
301 }
302
303 void wxBitmapButton::StartSelect()
304 {
305 m_isSelected = TRUE;
306 SetBitmap();
307 }
308
309 void wxBitmapButton::EndSelect()
310 {
311 m_isSelected = FALSE;
312 SetBitmap();
313 }
314
315 #endif