]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bmpbuttn.cpp
avoiding nesting dcs on the same window concurrently
[wxWidgets.git] / src / gtk / bmpbuttn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 #if wxUSE_BMPBUTTON
14
15 #include "wx/bmpbuttn.h"
16
17 #include "wx/gtk/private.h"
18
19 //-----------------------------------------------------------------------------
20 // classes
21 //-----------------------------------------------------------------------------
22
23 class wxBitmapButton;
24
25 //-----------------------------------------------------------------------------
26 // data
27 //-----------------------------------------------------------------------------
28
29 extern bool g_blockEventsOnDrag;
30
31 //-----------------------------------------------------------------------------
32 // "clicked"
33 //-----------------------------------------------------------------------------
34
35 extern "C" {
36 static void gtk_bmpbutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
37 {
38 if (g_isIdle)
39 wxapp_install_idle_handler();
40
41 if (!button->m_hasVMT) return;
42 if (g_blockEventsOnDrag) return;
43
44 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
45 event.SetEventObject(button);
46 button->GetEventHandler()->ProcessEvent(event);
47 }
48 }
49
50 //-----------------------------------------------------------------------------
51 // "enter"
52 //-----------------------------------------------------------------------------
53
54 extern "C" {
55 static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
56 {
57 if (!button->m_hasVMT) return;
58 if (g_blockEventsOnDrag) return;
59
60 button->HasFocus();
61 }
62 }
63
64 //-----------------------------------------------------------------------------
65 // "leave"
66 //-----------------------------------------------------------------------------
67
68 extern "C" {
69 static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
70 {
71 if (!button->m_hasVMT) return;
72 if (g_blockEventsOnDrag) return;
73
74 button->NotFocus();
75 }
76 }
77
78 //-----------------------------------------------------------------------------
79 // "pressed"
80 //-----------------------------------------------------------------------------
81
82 extern "C" {
83 static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
84 {
85 if (!button->m_hasVMT) return;
86 if (g_blockEventsOnDrag) return;
87
88 button->StartSelect();
89 }
90 }
91
92 //-----------------------------------------------------------------------------
93 // "released"
94 //-----------------------------------------------------------------------------
95
96 extern "C" {
97 static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
98 {
99 if (!button->m_hasVMT) return;
100 if (g_blockEventsOnDrag) return;
101
102 button->EndSelect();
103 }
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 = bitmap;
138
139 m_widget = gtk_button_new();
140
141 if (style & wxNO_BORDER)
142 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
143
144 if (m_bmpNormal.Ok())
145 {
146 OnSetBitmap();
147 }
148
149 g_signal_connect_after (m_widget, "clicked",
150 G_CALLBACK (gtk_bmpbutton_clicked_callback),
151 this);
152
153 g_signal_connect (m_widget, "enter",
154 G_CALLBACK (gtk_bmpbutton_enter_callback), this);
155 g_signal_connect (m_widget, "leave",
156 G_CALLBACK (gtk_bmpbutton_leave_callback), this);
157 g_signal_connect (m_widget, "pressed",
158 G_CALLBACK (gtk_bmpbutton_press_callback), this);
159 g_signal_connect (m_widget, "released",
160 G_CALLBACK (gtk_bmpbutton_release_callback), this);
161
162 m_parent->DoAddChild( this );
163
164 PostCreation(size);
165
166 return true;
167 }
168
169 void wxBitmapButton::SetDefault()
170 {
171 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
172 gtk_widget_grab_default( m_widget );
173
174 SetSize( m_x, m_y, m_width, m_height );
175 }
176
177 void wxBitmapButton::SetLabel( const wxString &label )
178 {
179 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
180
181 wxControl::SetLabel( label );
182 }
183
184 void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
185 {
186 if (!GTK_BIN(m_widget)->child)
187 return;
188
189 wxButton::DoApplyWidgetStyle(style);
190 }
191
192 void wxBitmapButton::OnSetBitmap()
193 {
194 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
195
196 InvalidateBestSize();
197
198 wxBitmap the_one;
199 if (!m_isEnabled)
200 the_one = m_bmpDisabled;
201 else if (m_isSelected)
202 the_one = m_bmpSelected;
203 else if (m_hasFocus)
204 the_one = m_bmpFocus;
205 else
206 the_one = m_bmpNormal;
207
208 if (!the_one.Ok()) the_one = m_bmpNormal;
209 if (!the_one.Ok()) return;
210
211 GdkBitmap *mask = (GdkBitmap *) NULL;
212 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
213
214 GtkWidget *child = GTK_BIN(m_widget)->child;
215 if (child == NULL)
216 {
217 // initial bitmap
218 GtkWidget *pixmap;
219
220 if (the_one.HasPixbuf())
221 pixmap = gtk_image_new_from_pixbuf(the_one.GetPixbuf());
222 else
223 pixmap = gtk_image_new_from_pixmap(the_one.GetPixmap(), mask);
224
225 gtk_widget_show(pixmap);
226 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
227 }
228 else
229 { // subsequent bitmaps
230 GtkImage *pixmap = GTK_IMAGE(child);
231 if (the_one.HasPixbuf())
232 gtk_image_set_from_pixbuf(pixmap, the_one.GetPixbuf());
233 else
234 gtk_image_set_from_pixmap(pixmap, the_one.GetPixmap(), mask);
235 }
236 }
237
238 wxSize wxBitmapButton::DoGetBestSize() const
239 {
240 return wxControl::DoGetBestSize();
241 }
242
243 bool wxBitmapButton::Enable( bool enable )
244 {
245 if ( !wxWindow::Enable(enable) )
246 return false;
247
248 OnSetBitmap();
249
250 return true;
251 }
252
253 void wxBitmapButton::HasFocus()
254 {
255 m_hasFocus = true;
256 OnSetBitmap();
257 }
258
259 void wxBitmapButton::NotFocus()
260 {
261 m_hasFocus = false;
262 OnSetBitmap();
263 }
264
265 void wxBitmapButton::StartSelect()
266 {
267 m_isSelected = true;
268 OnSetBitmap();
269 }
270
271 void wxBitmapButton::EndSelect()
272 {
273 m_isSelected = false;
274 OnSetBitmap();
275 }
276
277 #endif // wxUSE_BMPBUTTON