]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bmpbuttn.cpp
wxItemContainerImmutable::FindString unified.
[wxWidgets.git] / src / gtk / 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 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/defs.h"
14
15 #if wxUSE_BMPBUTTON
16
17 #include "wx/bmpbuttn.h"
18
19 #include "wx/gtk/private.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 extern "C" {
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 //-----------------------------------------------------------------------------
60 // "enter"
61 //-----------------------------------------------------------------------------
62
63 extern "C" {
64 static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
65 {
66 if (!button->m_hasVMT) return;
67 if (g_blockEventsOnDrag) return;
68
69 button->HasFocus();
70 }
71 }
72
73 //-----------------------------------------------------------------------------
74 // "leave"
75 //-----------------------------------------------------------------------------
76
77 extern "C" {
78 static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
79 {
80 if (!button->m_hasVMT) return;
81 if (g_blockEventsOnDrag) return;
82
83 button->NotFocus();
84 }
85 }
86
87 //-----------------------------------------------------------------------------
88 // "pressed"
89 //-----------------------------------------------------------------------------
90
91 extern "C" {
92 static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
93 {
94 if (!button->m_hasVMT) return;
95 if (g_blockEventsOnDrag) return;
96
97 button->StartSelect();
98 }
99 }
100
101 //-----------------------------------------------------------------------------
102 // "released"
103 //-----------------------------------------------------------------------------
104
105 extern "C" {
106 static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
107 {
108 if (!button->m_hasVMT) return;
109 if (g_blockEventsOnDrag) return;
110
111 button->EndSelect();
112 }
113 }
114
115 //-----------------------------------------------------------------------------
116 // wxBitmapButton
117 //-----------------------------------------------------------------------------
118
119 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxButton)
120
121 void wxBitmapButton::Init()
122 {
123 m_hasFocus =
124 m_isSelected = false;
125 }
126
127 bool wxBitmapButton::Create( wxWindow *parent,
128 wxWindowID id,
129 const wxBitmap& bitmap,
130 const wxPoint& pos,
131 const wxSize& size,
132 long style,
133 const wxValidator& validator,
134 const wxString &name )
135 {
136 m_needParent = true;
137 m_acceptsFocus = true;
138
139 if (!PreCreation( parent, pos, size ) ||
140 !CreateBase( parent, id, pos, size, style, validator, name ))
141 {
142 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
143 return false;
144 }
145
146 m_bmpNormal = bitmap;
147
148 m_widget = gtk_button_new();
149
150 if (style & wxNO_BORDER)
151 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
152
153 if (m_bmpNormal.Ok())
154 {
155 OnSetBitmap();
156 }
157
158 gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked",
159 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
160
161 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
162 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
163 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
164 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
165 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
166 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
167 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
168 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
169
170 m_parent->DoAddChild( this );
171
172 PostCreation(size);
173
174 return true;
175 }
176
177 void wxBitmapButton::SetDefault()
178 {
179 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
180 gtk_widget_grab_default( m_widget );
181
182 SetSize( m_x, m_y, m_width, m_height );
183 }
184
185 void wxBitmapButton::SetLabel( const wxString &label )
186 {
187 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
188
189 wxControl::SetLabel( label );
190 }
191
192 wxString wxBitmapButton::GetLabel() const
193 {
194 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid button") );
195
196 return wxControl::GetLabel();
197 }
198
199 void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
200 {
201 if ( !BUTTON_CHILD(m_widget) )
202 return;
203
204 wxButton::DoApplyWidgetStyle(style);
205 }
206
207 void wxBitmapButton::OnSetBitmap()
208 {
209 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
210
211 InvalidateBestSize();
212
213 wxBitmap the_one;
214 if (!m_isEnabled)
215 the_one = m_bmpDisabled;
216 else if (m_isSelected)
217 the_one = m_bmpSelected;
218 else if (m_hasFocus)
219 the_one = m_bmpFocus;
220 else
221 the_one = m_bmpNormal;
222
223 if (!the_one.Ok()) the_one = m_bmpNormal;
224 if (!the_one.Ok()) return;
225
226 GdkBitmap *mask = (GdkBitmap *) NULL;
227 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
228
229 GtkWidget *child = BUTTON_CHILD(m_widget);
230 if (child == NULL)
231 {
232 // initial bitmap
233 GtkWidget *pixmap;
234 #ifdef __WXGTK20__
235 if (the_one.HasPixbuf())
236 pixmap = gtk_image_new_from_pixbuf(the_one.GetPixbuf());
237 else
238 pixmap = gtk_image_new_from_pixmap(the_one.GetPixmap(), mask);
239 #else
240 pixmap = gtk_pixmap_new(the_one.GetPixmap(), mask);
241 #endif
242 gtk_widget_show(pixmap);
243 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
244 }
245 else
246 { // subsequent bitmaps
247 #ifdef __WXGTK20__
248 GtkImage *pixmap = GTK_IMAGE(child);
249 if (the_one.HasPixbuf())
250 gtk_image_set_from_pixbuf(pixmap, the_one.GetPixbuf());
251 else
252 gtk_image_set_from_pixmap(pixmap, the_one.GetPixmap(), mask);
253 #else
254 GtkPixmap *pixmap = GTK_PIXMAP(child);
255 gtk_pixmap_set(pixmap, the_one.GetPixmap(), mask);
256 #endif
257 }
258 }
259
260 wxSize wxBitmapButton::DoGetBestSize() const
261 {
262 return wxControl::DoGetBestSize();
263 }
264
265 bool wxBitmapButton::Enable( bool enable )
266 {
267 if ( !wxWindow::Enable(enable) )
268 return false;
269
270 OnSetBitmap();
271
272 return true;
273 }
274
275 void wxBitmapButton::HasFocus()
276 {
277 m_hasFocus = true;
278 OnSetBitmap();
279 }
280
281 void wxBitmapButton::NotFocus()
282 {
283 m_hasFocus = false;
284 OnSetBitmap();
285 }
286
287 void wxBitmapButton::StartSelect()
288 {
289 m_isSelected = true;
290 OnSetBitmap();
291 }
292
293 void wxBitmapButton::EndSelect()
294 {
295 m_isSelected = false;
296 OnSetBitmap();
297 }
298
299 #endif // wxUSE_BMPBUTTON
300