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