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