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