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