Patch wxGTK1 for recent changes in bmpbuttn
[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->HandleWindowEvent(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->GTKSetHasFocus();
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->GTKSetNotFocus();
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_bitmaps[State_Normal] = 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 (bitmap.IsOk())
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 = GetBitmapDisabled();
199 else if (m_isSelected)
200 the_one = GetBitmapPressed();
201 else if (HasFocus())
202 the_one = GetBitmapFocus();
203
204 if (!the_one.IsOk())
205 {
206 the_one = GetBitmapLabel();
207 if (!the_one.IsOk())
208 return;
209 }
210
211 GdkBitmap *mask = NULL;
212 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
213
214 GtkWidget *child = BUTTON_CHILD(m_widget);
215 if (child == NULL)
216 {
217 // initial bitmap
218 GtkWidget *pixmap;
219 pixmap = gtk_pixmap_new(the_one.GetPixmap(), mask);
220 gtk_widget_show(pixmap);
221 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
222 }
223 else
224 { // subsequent bitmaps
225 GtkPixmap *pixmap = GTK_PIXMAP(child);
226 gtk_pixmap_set(pixmap, the_one.GetPixmap(), mask);
227 }
228 }
229
230 wxSize wxBitmapButton::DoGetBestSize() const
231 {
232 return wxControl::DoGetBestSize();
233 }
234
235 bool wxBitmapButton::Enable( bool enable )
236 {
237 if ( !wxWindow::Enable(enable) )
238 return false;
239
240 OnSetBitmap();
241
242 return true;
243 }
244
245 void wxBitmapButton::GTKSetHasFocus()
246 {
247 m_hasFocus = true;
248 OnSetBitmap();
249 }
250
251 void wxBitmapButton::GTKSetNotFocus()
252 {
253 m_hasFocus = false;
254 OnSetBitmap();
255 }
256
257 void wxBitmapButton::StartSelect()
258 {
259 m_isSelected = true;
260 OnSetBitmap();
261 }
262
263 void wxBitmapButton::EndSelect()
264 {
265 m_isSelected = false;
266 OnSetBitmap();
267 }
268
269 #endif // wxUSE_BMPBUTTON