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