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