]>
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 | ||
66bd6b93 | 48 | static void gtk_bmpbutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button ) |
151ccd11 | 49 | { |
f6bcfd97 | 50 | if (g_isIdle) |
bf9e3e73 | 51 | wxapp_install_idle_handler(); |
acfd422a | 52 | |
a2053b27 | 53 | if (!button->m_hasVMT) return; |
43a18898 | 54 | if (g_blockEventsOnDrag) return; |
f6bcfd97 | 55 | |
43a18898 RR |
56 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId()); |
57 | event.SetEventObject(button); | |
58 | button->GetEventHandler()->ProcessEvent(event); | |
59 | } | |
60 | ||
61 | //----------------------------------------------------------------------------- | |
62 | // "enter" | |
63 | //----------------------------------------------------------------------------- | |
64 | ||
65 | static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button ) | |
66 | { | |
a2053b27 | 67 | if (!button->m_hasVMT) return; |
43a18898 RR |
68 | if (g_blockEventsOnDrag) return; |
69 | ||
f6bcfd97 | 70 | button->HasFocus(); |
43a18898 RR |
71 | } |
72 | ||
73 | //----------------------------------------------------------------------------- | |
74 | // "leave" | |
75 | //----------------------------------------------------------------------------- | |
76 | ||
77 | static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button ) | |
78 | { | |
a2053b27 | 79 | if (!button->m_hasVMT) return; |
43a18898 RR |
80 | if (g_blockEventsOnDrag) return; |
81 | ||
f6bcfd97 | 82 | button->NotFocus(); |
43a18898 RR |
83 | } |
84 | ||
85 | //----------------------------------------------------------------------------- | |
86 | // "pressed" | |
87 | //----------------------------------------------------------------------------- | |
88 | ||
89 | static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button ) | |
90 | { | |
a2053b27 | 91 | if (!button->m_hasVMT) return; |
43a18898 RR |
92 | if (g_blockEventsOnDrag) return; |
93 | ||
f6bcfd97 | 94 | button->StartSelect(); |
43a18898 RR |
95 | } |
96 | ||
97 | //----------------------------------------------------------------------------- | |
98 | // "released" | |
99 | //----------------------------------------------------------------------------- | |
100 | ||
101 | static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button ) | |
102 | { | |
a2053b27 | 103 | if (!button->m_hasVMT) return; |
43a18898 RR |
104 | if (g_blockEventsOnDrag) return; |
105 | ||
f6bcfd97 | 106 | button->EndSelect(); |
6de97a3b | 107 | } |
151ccd11 RR |
108 | |
109 | //----------------------------------------------------------------------------- | |
e1e955e1 RR |
110 | // wxBitmapButton |
111 | //----------------------------------------------------------------------------- | |
112 | ||
42b4e99e | 113 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxButton) |
151ccd11 | 114 | |
c3dfaa10 | 115 | void wxBitmapButton::Init() |
151ccd11 | 116 | { |
c3dfaa10 VZ |
117 | m_hasFocus = |
118 | m_isSelected = FALSE; | |
6de97a3b | 119 | } |
151ccd11 | 120 | |
c3dfaa10 VZ |
121 | bool wxBitmapButton::Create( wxWindow *parent, |
122 | wxWindowID id, | |
123 | const wxBitmap& bitmap, | |
124 | const wxPoint& pos, | |
125 | const wxSize& size, | |
126 | long style, | |
127 | const wxValidator& validator, | |
128 | const wxString &name ) | |
151ccd11 | 129 | { |
43a18898 | 130 | m_needParent = TRUE; |
b292e2f5 | 131 | m_acceptsFocus = TRUE; |
f6bcfd97 | 132 | |
4dcaf11a RR |
133 | if (!PreCreation( parent, pos, size ) || |
134 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
135 | { | |
223d09f6 | 136 | wxFAIL_MSG( wxT("wxBitmapButton creation failed") ); |
f6bcfd97 | 137 | return FALSE; |
4dcaf11a | 138 | } |
6de97a3b | 139 | |
189f58fa | 140 | m_bmpNormal = bitmap; |
f6bcfd97 | 141 | |
43a18898 | 142 | m_widget = gtk_button_new(); |
de1c750f | 143 | |
de1c750f RR |
144 | if (style & wxNO_BORDER) |
145 | gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE ); | |
de1c750f | 146 | |
1e6feb95 | 147 | if (m_bmpNormal.Ok()) |
43a18898 | 148 | { |
29149a64 | 149 | OnSetBitmap(); |
43a18898 | 150 | } |
f6bcfd97 BP |
151 | |
152 | gtk_signal_connect( GTK_OBJECT(m_widget), "clicked", | |
43a18898 | 153 | GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this ); |
151ccd11 | 154 | |
f6bcfd97 | 155 | gtk_signal_connect( GTK_OBJECT(m_widget), "enter", |
43a18898 | 156 | GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this ); |
f6bcfd97 | 157 | gtk_signal_connect( GTK_OBJECT(m_widget), "leave", |
43a18898 | 158 | GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this ); |
f6bcfd97 | 159 | gtk_signal_connect( GTK_OBJECT(m_widget), "pressed", |
43a18898 | 160 | GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this ); |
f6bcfd97 | 161 | gtk_signal_connect( GTK_OBJECT(m_widget), "released", |
43a18898 | 162 | GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this ); |
f6bcfd97 | 163 | |
eb082a08 | 164 | m_parent->DoAddChild( this ); |
f6bcfd97 | 165 | |
abdeb9e7 | 166 | PostCreation(size); |
f6bcfd97 | 167 | |
43a18898 | 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 | |
43a18898 | 186 | wxString wxBitmapButton::GetLabel() const |
151ccd11 | 187 | { |
223d09f6 | 188 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid button") ); |
f96aa4d9 | 189 | |
43a18898 | 190 | return wxControl::GetLabel(); |
6de97a3b | 191 | } |
903f689b | 192 | |
f40fdaa3 | 193 | void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style) |
903f689b | 194 | { |
9e691f46 VZ |
195 | if ( !BUTTON_CHILD(m_widget) ) |
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 RD |
205 | InvalidateBestSize(); |
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 |
1e6feb95 VZ |
215 | { |
216 | if (m_isSelected) | |
217 | { | |
218 | the_one = m_bmpSelected; | |
219 | } | |
220 | else | |
221 | { | |
222 | if (m_hasFocus) | |
223 | the_one = m_bmpFocus; | |
224 | else | |
225 | the_one = m_bmpNormal; | |
226 | } | |
227 | } | |
43a18898 | 228 | |
1e6feb95 | 229 | if (!the_one.Ok()) the_one = m_bmpNormal; |
de1c750f | 230 | if (!the_one.Ok()) return; |
f6bcfd97 | 231 | |
43a18898 RR |
232 | GdkBitmap *mask = (GdkBitmap *) NULL; |
233 | if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap(); | |
f6bcfd97 | 234 | |
9e691f46 VZ |
235 | GtkWidget *child = BUTTON_CHILD(m_widget); |
236 | if (child == NULL) | |
29149a64 VZ |
237 | { |
238 | // initial bitmap | |
67a6726a RR |
239 | GtkWidget *pixmap = gtk_pixmap_new(the_one.GetPixmap(), mask); |
240 | gtk_widget_show(pixmap); | |
241 | gtk_container_add(GTK_CONTAINER(m_widget), pixmap); | |
29149a64 VZ |
242 | } |
243 | else | |
67a6726a | 244 | { // subsequent bitmaps |
9e691f46 | 245 | GtkPixmap *g_pixmap = GTK_PIXMAP(child); |
67a6726a RR |
246 | gtk_pixmap_set(g_pixmap, the_one.GetPixmap(), mask); |
247 | } | |
903f689b RR |
248 | } |
249 | ||
178a1220 RD |
250 | wxSize wxBitmapButton::DoGetBestSize() const |
251 | { | |
252 | wxSize best; | |
253 | ||
254 | if (m_bmpNormal.Ok()) | |
255 | { | |
256 | int border = HasFlag(wxNO_BORDER) ? 4 : 10; | |
257 | best.x = m_bmpNormal.GetWidth()+border; | |
258 | best.y = m_bmpNormal.GetHeight()+border; | |
259 | } | |
9f884528 | 260 | CacheBestSize(best); |
178a1220 RD |
261 | return best; |
262 | } | |
263 | ||
f03fc89f | 264 | bool wxBitmapButton::Enable( bool enable ) |
43a18898 | 265 | { |
f03fc89f VZ |
266 | if ( !wxWindow::Enable(enable) ) |
267 | return FALSE; | |
43a18898 | 268 | |
29149a64 | 269 | OnSetBitmap(); |
f03fc89f VZ |
270 | |
271 | return TRUE; | |
43a18898 RR |
272 | } |
273 | ||
274 | void wxBitmapButton::HasFocus() | |
275 | { | |
276 | m_hasFocus = TRUE; | |
29149a64 | 277 | OnSetBitmap(); |
43a18898 RR |
278 | } |
279 | ||
280 | void wxBitmapButton::NotFocus() | |
281 | { | |
282 | m_hasFocus = FALSE; | |
29149a64 | 283 | OnSetBitmap(); |
43a18898 RR |
284 | } |
285 | ||
286 | void wxBitmapButton::StartSelect() | |
287 | { | |
288 | m_isSelected = TRUE; | |
29149a64 | 289 | OnSetBitmap(); |
43a18898 RR |
290 | } |
291 | ||
292 | void wxBitmapButton::EndSelect() | |
293 | { | |
294 | m_isSelected = FALSE; | |
29149a64 | 295 | OnSetBitmap(); |
43a18898 | 296 | } |
f6bcfd97 BP |
297 | |
298 | #endif // wxUSE_BMPBUTTON | |
dcf924a3 | 299 |