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