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