wxWindow split into wxWindowBase and wxWindow (wxGTK part)
[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 #include "gdk/gdk.h"
17 #include "gtk/gtk.h"
18
19 //-----------------------------------------------------------------------------
20 // classes
21 //-----------------------------------------------------------------------------
22
23 class wxBitmapButton;
24
25 //-----------------------------------------------------------------------------
26 // idle system
27 //-----------------------------------------------------------------------------
28
29 extern void wxapp_install_idle_handler();
30 extern bool g_isIdle;
31
32 //-----------------------------------------------------------------------------
33 // data
34 //-----------------------------------------------------------------------------
35
36 extern bool g_blockEventsOnDrag;
37
38 //-----------------------------------------------------------------------------
39 // "clicked"
40 //-----------------------------------------------------------------------------
41
42 static void gtk_bmpbutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
43 {
44 if (g_isIdle) wxapp_install_idle_handler();
45
46 if (!button->HasVMT()) return;
47 if (g_blockEventsOnDrag) return;
48
49 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
50 event.SetEventObject(button);
51 button->GetEventHandler()->ProcessEvent(event);
52 }
53
54 //-----------------------------------------------------------------------------
55 // "enter"
56 //-----------------------------------------------------------------------------
57
58 static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
59 {
60 if (!button->HasVMT()) return;
61 if (g_blockEventsOnDrag) return;
62
63 button->HasFocus();
64 }
65
66 //-----------------------------------------------------------------------------
67 // "leave"
68 //-----------------------------------------------------------------------------
69
70 static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
71 {
72 if (!button->HasVMT()) return;
73 if (g_blockEventsOnDrag) return;
74
75 button->NotFocus();
76 }
77
78 //-----------------------------------------------------------------------------
79 // "pressed"
80 //-----------------------------------------------------------------------------
81
82 static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
83 {
84 if (!button->HasVMT()) return;
85 if (g_blockEventsOnDrag) return;
86
87 button->StartSelect();
88 }
89
90 //-----------------------------------------------------------------------------
91 // "released"
92 //-----------------------------------------------------------------------------
93
94 static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
95 {
96 if (!button->HasVMT()) return;
97 if (g_blockEventsOnDrag) return;
98
99 button->EndSelect();
100 }
101
102 //-----------------------------------------------------------------------------
103 // wxBitmapButton
104 //-----------------------------------------------------------------------------
105
106 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxControl)
107
108 wxBitmapButton::wxBitmapButton()
109 {
110 }
111
112 bool wxBitmapButton::Create( wxWindow *parent, wxWindowID id, const wxBitmap &bitmap,
113 const wxPoint &pos, const wxSize &size,
114 long style, const wxValidator& validator, const wxString &name )
115 {
116 m_needParent = TRUE;
117 m_acceptsFocus = TRUE;
118
119 wxSize newSize = size;
120
121 PreCreation( parent, id, pos, newSize, style, name );
122
123 SetValidator( validator );
124
125 m_bitmap = bitmap;
126 m_disabled = bitmap;
127 m_focus = bitmap;
128 m_selected = bitmap;
129
130 m_label = "";
131
132 m_widget = gtk_button_new();
133
134 #if (GTK_MINOR_VERSION > 0)
135 if (style & wxNO_BORDER)
136 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
137 #endif
138
139 if (m_bitmap.Ok())
140 {
141 GdkBitmap *mask = (GdkBitmap *) NULL;
142 if (m_bitmap.GetMask()) mask = m_bitmap.GetMask()->GetBitmap();
143 GtkWidget *pixmap = gtk_pixmap_new( m_bitmap.GetPixmap(), mask );
144
145 gtk_widget_show( pixmap );
146 gtk_container_add( GTK_CONTAINER(m_widget), pixmap );
147 }
148
149 int border = 10;
150 if (style & wxNO_BORDER) border = 4;
151 if (newSize.x == -1) newSize.x = m_bitmap.GetWidth()+border;
152 if (newSize.y == -1) newSize.y = m_bitmap.GetHeight()+border;
153 SetSize( newSize.x, newSize.y );
154
155 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
156 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
157
158 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
159 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
160 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
161 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
162 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
163 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
164 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
165 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
166
167 m_parent->AddChild( this );
168
169 m_parent->InsertChild( 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 }