]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/bmpbuttn.cpp
Add wx.Rect.CenterIn
[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 // 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/gtk1/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 pixmap = gtk_pixmap_new(the_one.GetPixmap(), mask);
228 gtk_widget_show(pixmap);
229 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
230 }
231 else
232 { // subsequent bitmaps
233 GtkPixmap *pixmap = GTK_PIXMAP(child);
234 gtk_pixmap_set(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
278