]>
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 | g_object_ref(m_widget); | |
141 | ||
142 | if (style & wxNO_BORDER) | |
143 | gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE ); | |
144 | ||
145 | if (m_bmpNormal.Ok()) | |
146 | { | |
147 | OnSetBitmap(); | |
148 | } | |
149 | ||
150 | g_signal_connect_after (m_widget, "clicked", | |
151 | G_CALLBACK (gtk_bmpbutton_clicked_callback), | |
152 | this); | |
153 | ||
154 | g_signal_connect (m_widget, "enter", | |
155 | G_CALLBACK (gtk_bmpbutton_enter_callback), this); | |
156 | g_signal_connect (m_widget, "leave", | |
157 | G_CALLBACK (gtk_bmpbutton_leave_callback), this); | |
158 | g_signal_connect (m_widget, "pressed", | |
159 | G_CALLBACK (gtk_bmpbutton_press_callback), this); | |
160 | g_signal_connect (m_widget, "released", | |
161 | G_CALLBACK (gtk_bmpbutton_release_callback), this); | |
162 | ||
163 | m_parent->DoAddChild( this ); | |
164 | ||
165 | PostCreation(size); | |
166 | ||
167 | return true; | |
168 | } | |
169 | ||
170 | void wxBitmapButton::SetLabel( const wxString &label ) | |
171 | { | |
172 | wxCHECK_RET( m_widget != NULL, wxT("invalid button") ); | |
173 | ||
174 | wxControl::SetLabel( label ); | |
175 | } | |
176 | ||
177 | void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style) | |
178 | { | |
179 | if (!GTK_BIN(m_widget)->child) | |
180 | return; | |
181 | ||
182 | wxButton::DoApplyWidgetStyle(style); | |
183 | } | |
184 | ||
185 | void wxBitmapButton::OnSetBitmap() | |
186 | { | |
187 | wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") ); | |
188 | ||
189 | InvalidateBestSize(); | |
190 | ||
191 | wxBitmap the_one; | |
192 | if (!IsThisEnabled()) | |
193 | the_one = m_bmpDisabled; | |
194 | else if (m_isPressed) | |
195 | the_one = m_bmpSelected; | |
196 | else if (m_mouseHovers) | |
197 | the_one = m_bmpHover; | |
198 | else if (HasFocus()) | |
199 | the_one = m_bmpFocus; | |
200 | else | |
201 | the_one = m_bmpNormal; | |
202 | ||
203 | if (!the_one.Ok()) | |
204 | the_one = m_bmpNormal; | |
205 | if (!the_one.Ok()) | |
206 | return; | |
207 | ||
208 | GtkWidget* image = GTK_BIN(m_widget)->child; | |
209 | if (image == NULL) | |
210 | { | |
211 | image = gtk_image_new(); | |
212 | gtk_widget_show(image); | |
213 | gtk_container_add(GTK_CONTAINER(m_widget), image); | |
214 | } | |
215 | // always use pixbuf, because pixmap mask does not | |
216 | // work with disabled images in some themes | |
217 | gtk_image_set_from_pixbuf(GTK_IMAGE(image), the_one.GetPixbuf()); | |
218 | } | |
219 | ||
220 | wxSize wxBitmapButton::DoGetBestSize() const | |
221 | { | |
222 | return wxControl::DoGetBestSize(); | |
223 | } | |
224 | ||
225 | bool wxBitmapButton::Enable( bool enable ) | |
226 | { | |
227 | if ( !wxWindow::Enable(enable) ) | |
228 | return false; | |
229 | ||
230 | OnSetBitmap(); | |
231 | ||
232 | return true; | |
233 | } | |
234 | ||
235 | void wxBitmapButton::GTKMouseEnters() | |
236 | { | |
237 | m_mouseHovers = true; | |
238 | OnSetBitmap(); | |
239 | } | |
240 | ||
241 | void wxBitmapButton::GTKMouseLeaves() | |
242 | { | |
243 | m_mouseHovers = false; | |
244 | OnSetBitmap(); | |
245 | } | |
246 | ||
247 | void wxBitmapButton::GTKPressed() | |
248 | { | |
249 | m_isPressed = true; | |
250 | OnSetBitmap(); | |
251 | } | |
252 | ||
253 | void wxBitmapButton::GTKReleased() | |
254 | { | |
255 | m_isPressed = false; | |
256 | OnSetBitmap(); | |
257 | } | |
258 | ||
259 | void wxBitmapButton::OnFocusChange(wxFocusEvent& event) | |
260 | { | |
261 | event.Skip(); | |
262 | OnSetBitmap(); | |
263 | } | |
264 | ||
265 | #endif // wxUSE_BMPBUTTON |