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