]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/bmpbuttn.cpp
added wxDIB::Save() and wxDIB to/from wxBitmap) conversions, use them instead of...
[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 void wxBitmapButton::Init()
113 {
114 m_hasFocus =
115 m_isSelected = FALSE;
116 }
117
118 bool wxBitmapButton::Create( wxWindow *parent,
119 wxWindowID id,
120 const wxBitmap& bitmap,
121 const wxPoint& pos,
122 const wxSize& size,
123 long style,
124 const wxValidator& validator,
125 const wxString &name )
126 {
127 m_needParent = TRUE;
128 m_acceptsFocus = TRUE;
129
130 if (!PreCreation( parent, pos, size ) ||
131 !CreateBase( parent, id, pos, size, style, validator, name ))
132 {
133 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
134 return FALSE;
135 }
136
137 m_bmpNormal =
138 m_bmpDisabled =
139 m_bmpFocus =
140 m_bmpSelected = bitmap;
141
142 m_widget = gtk_button_new();
143
144 #if (GTK_MINOR_VERSION > 0)
145 if (style & wxNO_BORDER)
146 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
147 #endif
148
149 if (m_bmpNormal.Ok())
150 {
151 wxSize newSize = size;
152 int border = (style & wxNO_BORDER) ? 4 : 10;
153 if (newSize.x == -1)
154 newSize.x = m_bmpNormal.GetWidth()+border;
155 if (newSize.y == -1)
156 newSize.y = m_bmpNormal.GetHeight()+border;
157 SetSize( newSize.x, newSize.y );
158 OnSetBitmap();
159 }
160
161 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
162 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
163
164 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
165 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
166 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
167 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
168 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
169 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
170 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
171 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
172
173 m_parent->DoAddChild( this );
174
175 PostCreation();
176
177 SetBackgroundColour( parent->GetBackgroundColour() );
178
179 Show( TRUE );
180
181 return TRUE;
182 }
183
184 void wxBitmapButton::SetDefault()
185 {
186 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
187 gtk_widget_grab_default( m_widget );
188
189 SetSize( m_x, m_y, m_width, m_height );
190 }
191
192 void wxBitmapButton::SetLabel( const wxString &label )
193 {
194 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
195
196 wxControl::SetLabel( label );
197 }
198
199 wxString wxBitmapButton::GetLabel() const
200 {
201 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid button") );
202
203 return wxControl::GetLabel();
204 }
205
206 void wxBitmapButton::ApplyWidgetStyle()
207 {
208 if ( !BUTTON_CHILD(m_widget) )
209 return;
210
211 wxButton::ApplyWidgetStyle();
212 }
213
214 void wxBitmapButton::OnSetBitmap()
215 {
216 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
217
218 wxBitmap the_one;
219 if (!m_isEnabled)
220 the_one = m_bmpDisabled;
221 else if (m_isSelected)
222 the_one = m_bmpSelected;
223 else if (m_hasFocus)
224 the_one = m_bmpFocus;
225 else
226 {
227 if (m_isSelected)
228 {
229 the_one = m_bmpSelected;
230 }
231 else
232 {
233 if (m_hasFocus)
234 the_one = m_bmpFocus;
235 else
236 the_one = m_bmpNormal;
237 }
238 }
239
240 if (!the_one.Ok()) the_one = m_bmpNormal;
241 if (!the_one.Ok()) return;
242
243 GdkBitmap *mask = (GdkBitmap *) NULL;
244 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
245
246 GtkWidget *child = BUTTON_CHILD(m_widget);
247 if (child == NULL)
248 {
249 // initial bitmap
250 GtkWidget *pixmap = gtk_pixmap_new(the_one.GetPixmap(), mask);
251 gtk_widget_show(pixmap);
252 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
253 }
254 else
255 { // subsequent bitmaps
256 GtkPixmap *g_pixmap = GTK_PIXMAP(child);
257 gtk_pixmap_set(g_pixmap, the_one.GetPixmap(), mask);
258 }
259 }
260
261 bool wxBitmapButton::Enable( bool enable )
262 {
263 if ( !wxWindow::Enable(enable) )
264 return FALSE;
265
266 OnSetBitmap();
267
268 return TRUE;
269 }
270
271 void wxBitmapButton::HasFocus()
272 {
273 m_hasFocus = TRUE;
274 OnSetBitmap();
275 }
276
277 void wxBitmapButton::NotFocus()
278 {
279 m_hasFocus = FALSE;
280 OnSetBitmap();
281 }
282
283 void wxBitmapButton::StartSelect()
284 {
285 m_isSelected = TRUE;
286 OnSetBitmap();
287 }
288
289 void wxBitmapButton::EndSelect()
290 {
291 m_isSelected = FALSE;
292 OnSetBitmap();
293 }
294
295 #endif // wxUSE_BMPBUTTON
296