Added new wxBitmapButton implementation
[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 //-----------------------------------------------------------------------------
17 // classes
18 //-----------------------------------------------------------------------------
19
20 class wxBitmapButton;
21
22 //-----------------------------------------------------------------------------
23 // data
24 //-----------------------------------------------------------------------------
25
26 extern bool g_blockEventsOnDrag;
27
28 //-----------------------------------------------------------------------------
29 // "clicked"
30 //-----------------------------------------------------------------------------
31
32 static void gtk_bmpbutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
33 {
34 if (!button->HasVMT()) return;
35 if (g_blockEventsOnDrag) return;
36
37 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
38 event.SetEventObject(button);
39 button->GetEventHandler()->ProcessEvent(event);
40 }
41
42 //-----------------------------------------------------------------------------
43 // "enter"
44 //-----------------------------------------------------------------------------
45
46 static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
47 {
48 if (!button->HasVMT()) return;
49 if (g_blockEventsOnDrag) return;
50
51 button->HasFocus();
52 }
53
54 //-----------------------------------------------------------------------------
55 // "leave"
56 //-----------------------------------------------------------------------------
57
58 static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
59 {
60 if (!button->HasVMT()) return;
61 if (g_blockEventsOnDrag) return;
62
63 button->NotFocus();
64 }
65
66 //-----------------------------------------------------------------------------
67 // "pressed"
68 //-----------------------------------------------------------------------------
69
70 static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
71 {
72 if (!button->HasVMT()) return;
73 if (g_blockEventsOnDrag) return;
74
75 button->StartSelect();
76 }
77
78 //-----------------------------------------------------------------------------
79 // "released"
80 //-----------------------------------------------------------------------------
81
82 static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
83 {
84 if (!button->HasVMT()) return;
85 if (g_blockEventsOnDrag) return;
86
87 button->EndSelect();
88 }
89
90 //-----------------------------------------------------------------------------
91 // wxBitmapButton
92 //-----------------------------------------------------------------------------
93
94 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxControl)
95
96 wxBitmapButton::wxBitmapButton()
97 {
98 }
99
100 bool wxBitmapButton::Create( wxWindow *parent, wxWindowID id, const wxBitmap &bitmap,
101 const wxPoint &pos, const wxSize &size,
102 long style, const wxValidator& validator, const wxString &name )
103 {
104 m_needParent = TRUE;
105
106 wxSize newSize = size;
107
108 PreCreation( parent, id, pos, newSize, style, name );
109
110 SetValidator( validator );
111
112 m_bitmap = bitmap;
113 m_disabled = bitmap;
114 m_focus = bitmap;
115 m_selected = bitmap;
116
117 m_label = "";
118
119 m_widget = gtk_button_new();
120
121 if (m_bitmap.Ok())
122 {
123 GdkBitmap *mask = (GdkBitmap *) NULL;
124 if (m_bitmap.GetMask()) mask = m_bitmap.GetMask()->GetBitmap();
125 GtkWidget *pixmap = gtk_pixmap_new( m_bitmap.GetPixmap(), mask );
126
127 gtk_widget_show( pixmap );
128 gtk_container_add( GTK_CONTAINER(m_widget), pixmap );
129 }
130
131 if (newSize.x == -1) newSize.x = m_bitmap.GetHeight()+10;
132 if (newSize.y == -1) newSize.y = m_bitmap.GetWidth()+10;
133 SetSize( newSize.x, newSize.y );
134
135 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
136 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
137
138 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
139 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
140 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
141 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
142 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
143 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
144 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
145 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
146
147 m_parent->AddChild( this );
148
149 (m_parent->m_insertCallback)( m_parent, this );
150
151 PostCreation();
152
153 SetBackgroundColour( parent->GetBackgroundColour() );
154
155 Show( TRUE );
156
157 return TRUE;
158 }
159
160 void wxBitmapButton::SetDefault()
161 {
162 /*
163 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
164 gtk_widget_grab_default( m_widget );
165 */
166 }
167
168 void wxBitmapButton::SetLabel( const wxString &label )
169 {
170 wxCHECK_RET( m_widget != NULL, "invalid button" );
171
172 wxControl::SetLabel( label );
173 }
174
175 wxString wxBitmapButton::GetLabel() const
176 {
177 wxCHECK_MSG( m_widget != NULL, "", "invalid button" );
178
179 return wxControl::GetLabel();
180 }
181
182 void wxBitmapButton::ApplyWidgetStyle()
183 {
184 }
185
186 void wxBitmapButton::SetBitmap()
187 {
188 wxCHECK_RET( m_widget != NULL, "invalid button" );
189
190 wxBitmap the_one;
191
192 if ( ! m_isEnabled )
193 the_one = m_disabled;
194 else
195 {
196 if ( m_isSelected )
197 {
198 the_one = m_selected;
199 }
200 else
201 {
202 if ( m_hasFocus )
203 the_one = m_focus;
204 else
205 the_one = m_bitmap;
206 }
207 }
208
209 if ( ! the_one.Ok() ) the_one = m_bitmap;
210 if ( ! the_one.Ok() ) return;
211
212 GtkButton *bin = GTK_BUTTON( m_widget );
213 GtkPixmap *g_pixmap = GTK_PIXMAP( bin->child );
214
215 GdkBitmap *mask = (GdkBitmap *) NULL;
216 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
217
218 gtk_pixmap_set( g_pixmap, the_one.GetPixmap(), mask );
219 }
220
221 void wxBitmapButton::SetBitmapDisabled( const wxBitmap& bitmap )
222 {
223 wxCHECK_RET( m_widget != NULL, "invalid button" );
224
225 if ( ! m_disabled.Ok() ) return;
226 m_disabled = bitmap;
227
228 SetBitmap();
229 }
230
231 void wxBitmapButton::SetBitmapFocus( const wxBitmap& bitmap )
232 {
233 wxCHECK_RET( m_widget != NULL, "invalid button" );
234
235 if ( ! m_focus.Ok() ) return;
236 m_focus = bitmap;
237
238 SetBitmap();
239 }
240
241 void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap )
242 {
243 wxCHECK_RET( m_widget != NULL, "invalid button" );
244
245 if (!m_bitmap.Ok()) return;
246 m_bitmap = bitmap;
247
248 SetBitmap();
249 }
250
251 void wxBitmapButton::SetBitmapSelected( const wxBitmap& bitmap )
252 {
253 wxCHECK_RET( m_widget != NULL, "invalid button" );
254
255 if ( ! m_selected.Ok() ) return;
256 m_selected = bitmap;
257
258 SetBitmap();
259 }
260
261 void wxBitmapButton::Enable( const bool enable )
262 {
263 wxWindow::Enable(enable);
264
265 SetBitmap();
266 }
267
268 void wxBitmapButton::HasFocus()
269 {
270 m_hasFocus = TRUE;
271 SetBitmap();
272 }
273
274 void wxBitmapButton::NotFocus()
275 {
276 m_hasFocus = FALSE;
277 SetBitmap();
278 }
279
280 void wxBitmapButton::StartSelect()
281 {
282 m_isSelected = TRUE;
283 SetBitmap();
284 }
285
286 void wxBitmapButton::EndSelect()
287 {
288 m_isSelected = FALSE;
289 SetBitmap();
290 }