]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/bmpbuttn.cpp
X property data is long for format 32
[wxWidgets.git] / src / gtk1 / bmpbuttn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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/gtk1/private.h"
18
19 //-----------------------------------------------------------------------------
20 // classes
21 //-----------------------------------------------------------------------------
22
23 class wxBitmapButton;
24
25 //-----------------------------------------------------------------------------
26 // idle system
27 //-----------------------------------------------------------------------------
28
29 extern void wxapp_install_idle_handler();
30 extern bool g_isIdle;
31
32 //-----------------------------------------------------------------------------
33 // data
34 //-----------------------------------------------------------------------------
35
36 extern bool g_blockEventsOnDrag;
37
38 //-----------------------------------------------------------------------------
39 // "clicked"
40 //-----------------------------------------------------------------------------
41
42 extern "C" {
43 static void gtk_bmpbutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
44 {
45 if (g_isIdle)
46 wxapp_install_idle_handler();
47
48 if (!button->m_hasVMT) return;
49 if (g_blockEventsOnDrag) return;
50
51 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
52 event.SetEventObject(button);
53 button->GetEventHandler()->ProcessEvent(event);
54 }
55 }
56
57 //-----------------------------------------------------------------------------
58 // "enter"
59 //-----------------------------------------------------------------------------
60
61 extern "C" {
62 static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
63 {
64 if (!button->m_hasVMT) return;
65 if (g_blockEventsOnDrag) return;
66
67 button->HasFocus();
68 }
69 }
70
71 //-----------------------------------------------------------------------------
72 // "leave"
73 //-----------------------------------------------------------------------------
74
75 extern "C" {
76 static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
77 {
78 if (!button->m_hasVMT) return;
79 if (g_blockEventsOnDrag) return;
80
81 button->NotFocus();
82 }
83 }
84
85 //-----------------------------------------------------------------------------
86 // "pressed"
87 //-----------------------------------------------------------------------------
88
89 extern "C" {
90 static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
91 {
92 if (!button->m_hasVMT) return;
93 if (g_blockEventsOnDrag) return;
94
95 button->StartSelect();
96 }
97 }
98
99 //-----------------------------------------------------------------------------
100 // "released"
101 //-----------------------------------------------------------------------------
102
103 extern "C" {
104 static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
105 {
106 if (!button->m_hasVMT) return;
107 if (g_blockEventsOnDrag) return;
108
109 button->EndSelect();
110 }
111 }
112
113 //-----------------------------------------------------------------------------
114 // wxBitmapButton
115 //-----------------------------------------------------------------------------
116
117 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxButton)
118
119 void wxBitmapButton::Init()
120 {
121 m_hasFocus =
122 m_isSelected = false;
123 }
124
125 bool wxBitmapButton::Create( wxWindow *parent,
126 wxWindowID id,
127 const wxBitmap& bitmap,
128 const wxPoint& pos,
129 const wxSize& size,
130 long style,
131 const wxValidator& validator,
132 const wxString &name )
133 {
134 m_needParent = true;
135 m_acceptsFocus = true;
136
137 if (!PreCreation( parent, pos, size ) ||
138 !CreateBase( parent, id, pos, size, style, validator, name ))
139 {
140 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
141 return false;
142 }
143
144 m_bmpNormal = bitmap;
145
146 m_widget = gtk_button_new();
147
148 if (style & wxNO_BORDER)
149 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
150
151 if (m_bmpNormal.Ok())
152 {
153 OnSetBitmap();
154 }
155
156 gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked",
157 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
158
159 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
160 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
161 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
162 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
163 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
164 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
165 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
166 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
167
168 m_parent->DoAddChild( this );
169
170 PostCreation(size);
171
172 return true;
173 }
174
175 void wxBitmapButton::SetLabel( const wxString &label )
176 {
177 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
178
179 wxControl::SetLabel( label );
180 }
181
182 void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
183 {
184 if ( !BUTTON_CHILD(m_widget) )
185 return;
186
187 wxButton::DoApplyWidgetStyle(style);
188 }
189
190 void wxBitmapButton::OnSetBitmap()
191 {
192 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
193
194 InvalidateBestSize();
195
196 wxBitmap the_one;
197 if (!IsThisEnabled())
198 the_one = m_bmpDisabled;
199 else if (m_isSelected)
200 the_one = m_bmpSelected;
201 else if (m_hasFocus)
202 the_one = m_bmpFocus;
203 else
204 the_one = m_bmpNormal;
205
206 if (!the_one.Ok()) the_one = m_bmpNormal;
207 if (!the_one.Ok()) return;
208
209 GdkBitmap *mask = (GdkBitmap *) NULL;
210 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
211
212 GtkWidget *child = BUTTON_CHILD(m_widget);
213 if (child == NULL)
214 {
215 // initial bitmap
216 GtkWidget *pixmap;
217 pixmap = gtk_pixmap_new(the_one.GetPixmap(), mask);
218 gtk_widget_show(pixmap);
219 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
220 }
221 else
222 { // subsequent bitmaps
223 GtkPixmap *pixmap = GTK_PIXMAP(child);
224 gtk_pixmap_set(pixmap, the_one.GetPixmap(), mask);
225 }
226 }
227
228 wxSize wxBitmapButton::DoGetBestSize() const
229 {
230 return wxControl::DoGetBestSize();
231 }
232
233 bool wxBitmapButton::Enable( bool enable )
234 {
235 if ( !wxWindow::Enable(enable) )
236 return false;
237
238 OnSetBitmap();
239
240 return true;
241 }
242
243 void wxBitmapButton::HasFocus()
244 {
245 m_hasFocus = true;
246 OnSetBitmap();
247 }
248
249 void wxBitmapButton::NotFocus()
250 {
251 m_hasFocus = false;
252 OnSetBitmap();
253 }
254
255 void wxBitmapButton::StartSelect()
256 {
257 m_isSelected = true;
258 OnSetBitmap();
259 }
260
261 void wxBitmapButton::EndSelect()
262 {
263 m_isSelected = false;
264 OnSetBitmap();
265 }
266
267 #endif // wxUSE_BMPBUTTON