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