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