]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bmpbuttn.cpp
Workaround for wxMac since it is unable to dismiss the edit control
[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 void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
193 {
194 if ( !BUTTON_CHILD(m_widget) )
195 return;
196
197 wxButton::DoApplyWidgetStyle(style);
198 }
199
200 void wxBitmapButton::OnSetBitmap()
201 {
202 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
203
204 InvalidateBestSize();
205
206 wxBitmap the_one;
207 if (!m_isEnabled)
208 the_one = m_bmpDisabled;
209 else if (m_isSelected)
210 the_one = m_bmpSelected;
211 else if (m_hasFocus)
212 the_one = m_bmpFocus;
213 else
214 the_one = m_bmpNormal;
215
216 if (!the_one.Ok()) the_one = m_bmpNormal;
217 if (!the_one.Ok()) return;
218
219 GdkBitmap *mask = (GdkBitmap *) NULL;
220 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
221
222 GtkWidget *child = BUTTON_CHILD(m_widget);
223 if (child == NULL)
224 {
225 // initial bitmap
226 GtkWidget *pixmap;
227 #ifdef __WXGTK20__
228 if (the_one.HasPixbuf())
229 pixmap = gtk_image_new_from_pixbuf(the_one.GetPixbuf());
230 else
231 pixmap = gtk_image_new_from_pixmap(the_one.GetPixmap(), mask);
232 #else
233 pixmap = gtk_pixmap_new(the_one.GetPixmap(), mask);
234 #endif
235 gtk_widget_show(pixmap);
236 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
237 }
238 else
239 { // subsequent bitmaps
240 #ifdef __WXGTK20__
241 GtkImage *pixmap = GTK_IMAGE(child);
242 if (the_one.HasPixbuf())
243 gtk_image_set_from_pixbuf(pixmap, the_one.GetPixbuf());
244 else
245 gtk_image_set_from_pixmap(pixmap, the_one.GetPixmap(), mask);
246 #else
247 GtkPixmap *pixmap = GTK_PIXMAP(child);
248 gtk_pixmap_set(pixmap, the_one.GetPixmap(), mask);
249 #endif
250 }
251 }
252
253 wxSize wxBitmapButton::DoGetBestSize() const
254 {
255 return wxControl::DoGetBestSize();
256 }
257
258 bool wxBitmapButton::Enable( bool enable )
259 {
260 if ( !wxWindow::Enable(enable) )
261 return false;
262
263 OnSetBitmap();
264
265 return true;
266 }
267
268 void wxBitmapButton::HasFocus()
269 {
270 m_hasFocus = true;
271 OnSetBitmap();
272 }
273
274 void wxBitmapButton::NotFocus()
275 {
276 m_hasFocus = false;
277 OnSetBitmap();
278 }
279
280 void wxBitmapButton::StartSelect()
281 {
282 m_isSelected = true;
283 OnSetBitmap();
284 }
285
286 void wxBitmapButton::EndSelect()
287 {
288 m_isSelected = false;
289 OnSetBitmap();
290 }
291
292 #endif // wxUSE_BMPBUTTON
293