]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bmpbuttn.cpp
use native TAB traversal for GTK+ 2
[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
129 if (!PreCreation( parent, pos, size ) ||
130 !CreateBase( parent, id, pos, size, style, validator, name ))
131 {
132 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
133 return false;
134 }
135
136 m_bmpNormal = bitmap;
137
138 m_widget = gtk_button_new();
139
140 if (style & wxNO_BORDER)
141 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
142
143 if (m_bmpNormal.Ok())
144 {
145 OnSetBitmap();
146 }
147
148 g_signal_connect_after (m_widget, "clicked",
149 G_CALLBACK (gtk_bmpbutton_clicked_callback),
150 this);
151
152 g_signal_connect (m_widget, "enter",
153 G_CALLBACK (gtk_bmpbutton_enter_callback), this);
154 g_signal_connect (m_widget, "leave",
155 G_CALLBACK (gtk_bmpbutton_leave_callback), this);
156 g_signal_connect (m_widget, "pressed",
157 G_CALLBACK (gtk_bmpbutton_press_callback), this);
158 g_signal_connect (m_widget, "released",
159 G_CALLBACK (gtk_bmpbutton_release_callback), this);
160
161 m_parent->DoAddChild( this );
162
163 PostCreation(size);
164
165 return true;
166 }
167
168 void wxBitmapButton::SetDefault()
169 {
170 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
171 gtk_widget_grab_default( m_widget );
172
173 SetSize( m_x, m_y, m_width, m_height );
174 }
175
176 void wxBitmapButton::SetLabel( const wxString &label )
177 {
178 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
179
180 wxControl::SetLabel( label );
181 }
182
183 void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
184 {
185 if (!GTK_BIN(m_widget)->child)
186 return;
187
188 wxButton::DoApplyWidgetStyle(style);
189 }
190
191 void wxBitmapButton::OnSetBitmap()
192 {
193 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
194
195 InvalidateBestSize();
196
197 wxBitmap the_one;
198 if (!IsThisEnabled())
199 the_one = m_bmpDisabled;
200 else if (m_isSelected)
201 the_one = m_bmpSelected;
202 else if (m_hasFocus)
203 the_one = m_bmpFocus;
204 else
205 the_one = m_bmpNormal;
206
207 if (!the_one.Ok()) the_one = m_bmpNormal;
208 if (!the_one.Ok()) return;
209
210 GdkBitmap *mask = (GdkBitmap *) NULL;
211 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
212
213 GtkWidget *child = GTK_BIN(m_widget)->child;
214 if (child == NULL)
215 {
216 // initial bitmap
217 GtkWidget *pixmap;
218
219 if (the_one.HasPixbuf())
220 pixmap = gtk_image_new_from_pixbuf(the_one.GetPixbuf());
221 else
222 pixmap = gtk_image_new_from_pixmap(the_one.GetPixmap(), mask);
223
224 gtk_widget_show(pixmap);
225 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
226 }
227 else
228 { // subsequent bitmaps
229 GtkImage *pixmap = GTK_IMAGE(child);
230 if (the_one.HasPixbuf())
231 gtk_image_set_from_pixbuf(pixmap, the_one.GetPixbuf());
232 else
233 gtk_image_set_from_pixmap(pixmap, the_one.GetPixmap(), mask);
234 }
235 }
236
237 wxSize wxBitmapButton::DoGetBestSize() const
238 {
239 return wxControl::DoGetBestSize();
240 }
241
242 bool wxBitmapButton::Enable( bool enable )
243 {
244 if ( !wxWindow::Enable(enable) )
245 return false;
246
247 OnSetBitmap();
248
249 return true;
250 }
251
252 void wxBitmapButton::HasFocus()
253 {
254 m_hasFocus = true;
255 OnSetBitmap();
256 }
257
258 void wxBitmapButton::NotFocus()
259 {
260 m_hasFocus = false;
261 OnSetBitmap();
262 }
263
264 void wxBitmapButton::StartSelect()
265 {
266 m_isSelected = true;
267 OnSetBitmap();
268 }
269
270 void wxBitmapButton::EndSelect()
271 {
272 m_isSelected = false;
273 OnSetBitmap();
274 }
275
276 #endif // wxUSE_BMPBUTTON