]>
Commit | Line | Data |
---|---|---|
151ccd11 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: bmpbuttn.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
f96aa4d9 | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
151ccd11 RR |
7 | // Licence: wxWindows licence |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "bmpbuttn.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/bmpbuttn.h" | |
15 | ||
16 | //----------------------------------------------------------------------------- | |
17 | // classes | |
18 | //----------------------------------------------------------------------------- | |
19 | ||
20 | class wxBitmapButton; | |
21 | ||
66bd6b93 RR |
22 | //----------------------------------------------------------------------------- |
23 | // data | |
24 | //----------------------------------------------------------------------------- | |
25 | ||
26 | extern bool g_blockEventsOnDrag; | |
27 | ||
151ccd11 | 28 | //----------------------------------------------------------------------------- |
e1e955e1 | 29 | // "clicked" |
151ccd11 RR |
30 | //----------------------------------------------------------------------------- |
31 | ||
66bd6b93 | 32 | static void gtk_bmpbutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button ) |
151ccd11 | 33 | { |
43a18898 RR |
34 | if (!button->HasVMT()) return; |
35 | if (g_blockEventsOnDrag) return; | |
66bd6b93 | 36 | |
43a18898 RR |
37 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId()); |
38 | event.SetEventObject(button); | |
39 | button->GetEventHandler()->ProcessEvent(event); | |
40 | } | |
41 | ||
42 | //----------------------------------------------------------------------------- | |
43 | // "enter" | |
44 | //----------------------------------------------------------------------------- | |
45 | ||
46 | static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button ) | |
47 | { | |
48 | if (!button->HasVMT()) return; | |
49 | if (g_blockEventsOnDrag) return; | |
50 | ||
51 | button->HasFocus(); | |
52 | } | |
53 | ||
54 | //----------------------------------------------------------------------------- | |
55 | // "leave" | |
56 | //----------------------------------------------------------------------------- | |
57 | ||
58 | static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button ) | |
59 | { | |
60 | if (!button->HasVMT()) return; | |
61 | if (g_blockEventsOnDrag) return; | |
62 | ||
63 | button->NotFocus(); | |
64 | } | |
65 | ||
66 | //----------------------------------------------------------------------------- | |
67 | // "pressed" | |
68 | //----------------------------------------------------------------------------- | |
69 | ||
70 | static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button ) | |
71 | { | |
72 | if (!button->HasVMT()) return; | |
73 | if (g_blockEventsOnDrag) return; | |
74 | ||
75 | button->StartSelect(); | |
76 | } | |
77 | ||
78 | //----------------------------------------------------------------------------- | |
79 | // "released" | |
80 | //----------------------------------------------------------------------------- | |
81 | ||
82 | static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button ) | |
83 | { | |
84 | if (!button->HasVMT()) return; | |
85 | if (g_blockEventsOnDrag) return; | |
86 | ||
87 | button->EndSelect(); | |
6de97a3b | 88 | } |
151ccd11 RR |
89 | |
90 | //----------------------------------------------------------------------------- | |
e1e955e1 RR |
91 | // wxBitmapButton |
92 | //----------------------------------------------------------------------------- | |
93 | ||
94 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxControl) | |
151ccd11 | 95 | |
43a18898 | 96 | wxBitmapButton::wxBitmapButton() |
151ccd11 | 97 | { |
6de97a3b | 98 | } |
151ccd11 | 99 | |
43a18898 RR |
100 | bool wxBitmapButton::Create( wxWindow *parent, wxWindowID id, const wxBitmap &bitmap, |
101 | const wxPoint &pos, const wxSize &size, | |
102 | long style, const wxValidator& validator, const wxString &name ) | |
151ccd11 | 103 | { |
43a18898 | 104 | m_needParent = TRUE; |
b292e2f5 | 105 | m_acceptsFocus = TRUE; |
151ccd11 | 106 | |
43a18898 | 107 | wxSize newSize = size; |
151ccd11 | 108 | |
43a18898 | 109 | PreCreation( parent, id, pos, newSize, style, name ); |
151ccd11 | 110 | |
43a18898 | 111 | SetValidator( validator ); |
6de97a3b | 112 | |
43a18898 RR |
113 | m_bitmap = bitmap; |
114 | m_disabled = bitmap; | |
115 | m_focus = bitmap; | |
116 | m_selected = bitmap; | |
151ccd11 | 117 | |
43a18898 | 118 | m_label = ""; |
151ccd11 | 119 | |
43a18898 RR |
120 | m_widget = gtk_button_new(); |
121 | ||
122 | if (m_bitmap.Ok()) | |
123 | { | |
124 | GdkBitmap *mask = (GdkBitmap *) NULL; | |
125 | if (m_bitmap.GetMask()) mask = m_bitmap.GetMask()->GetBitmap(); | |
126 | GtkWidget *pixmap = gtk_pixmap_new( m_bitmap.GetPixmap(), mask ); | |
151ccd11 | 127 | |
43a18898 RR |
128 | gtk_widget_show( pixmap ); |
129 | gtk_container_add( GTK_CONTAINER(m_widget), pixmap ); | |
130 | } | |
151ccd11 | 131 | |
43a18898 RR |
132 | if (newSize.x == -1) newSize.x = m_bitmap.GetHeight()+10; |
133 | if (newSize.y == -1) newSize.y = m_bitmap.GetWidth()+10; | |
134 | SetSize( newSize.x, newSize.y ); | |
151ccd11 | 135 | |
43a18898 RR |
136 | gtk_signal_connect( GTK_OBJECT(m_widget), "clicked", |
137 | GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this ); | |
151ccd11 | 138 | |
43a18898 RR |
139 | gtk_signal_connect( GTK_OBJECT(m_widget), "enter", |
140 | GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this ); | |
141 | gtk_signal_connect( GTK_OBJECT(m_widget), "leave", | |
142 | GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this ); | |
143 | gtk_signal_connect( GTK_OBJECT(m_widget), "pressed", | |
144 | GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this ); | |
145 | gtk_signal_connect( GTK_OBJECT(m_widget), "released", | |
146 | GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this ); | |
147 | ||
148 | m_parent->AddChild( this ); | |
6ca41e57 | 149 | |
43a18898 | 150 | (m_parent->m_insertCallback)( m_parent, this ); |
6ca41e57 | 151 | |
43a18898 | 152 | PostCreation(); |
151ccd11 | 153 | |
43a18898 | 154 | SetBackgroundColour( parent->GetBackgroundColour() ); |
f96aa4d9 | 155 | |
43a18898 | 156 | Show( TRUE ); |
151ccd11 | 157 | |
43a18898 | 158 | return TRUE; |
6de97a3b | 159 | } |
151ccd11 | 160 | |
43a18898 | 161 | void wxBitmapButton::SetDefault() |
151ccd11 | 162 | { |
903f689b | 163 | /* |
43a18898 RR |
164 | GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); |
165 | gtk_widget_grab_default( m_widget ); | |
903f689b | 166 | */ |
6de97a3b | 167 | } |
151ccd11 RR |
168 | |
169 | void wxBitmapButton::SetLabel( const wxString &label ) | |
170 | { | |
43a18898 | 171 | wxCHECK_RET( m_widget != NULL, "invalid button" ); |
f96aa4d9 | 172 | |
43a18898 | 173 | wxControl::SetLabel( label ); |
6de97a3b | 174 | } |
151ccd11 | 175 | |
43a18898 | 176 | wxString wxBitmapButton::GetLabel() const |
151ccd11 | 177 | { |
43a18898 | 178 | wxCHECK_MSG( m_widget != NULL, "", "invalid button" ); |
f96aa4d9 | 179 | |
43a18898 | 180 | return wxControl::GetLabel(); |
6de97a3b | 181 | } |
903f689b | 182 | |
43a18898 | 183 | void wxBitmapButton::ApplyWidgetStyle() |
903f689b | 184 | { |
43a18898 RR |
185 | } |
186 | ||
187 | void wxBitmapButton::SetBitmap() | |
188 | { | |
189 | wxCHECK_RET( m_widget != NULL, "invalid button" ); | |
f96aa4d9 | 190 | |
43a18898 | 191 | wxBitmap the_one; |
903f689b | 192 | |
43a18898 RR |
193 | if ( ! m_isEnabled ) |
194 | the_one = m_disabled; | |
195 | else | |
196 | { | |
197 | if ( m_isSelected ) | |
198 | { | |
199 | the_one = m_selected; | |
200 | } | |
201 | else | |
202 | { | |
203 | if ( m_hasFocus ) | |
204 | the_one = m_focus; | |
205 | else | |
206 | the_one = m_bitmap; | |
207 | } | |
208 | } | |
209 | ||
210 | if ( ! the_one.Ok() ) the_one = m_bitmap; | |
211 | if ( ! the_one.Ok() ) return; | |
903f689b | 212 | |
43a18898 RR |
213 | GtkButton *bin = GTK_BUTTON( m_widget ); |
214 | GtkPixmap *g_pixmap = GTK_PIXMAP( bin->child ); | |
903f689b | 215 | |
43a18898 RR |
216 | GdkBitmap *mask = (GdkBitmap *) NULL; |
217 | if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap(); | |
218 | ||
219 | gtk_pixmap_set( g_pixmap, the_one.GetPixmap(), mask ); | |
903f689b RR |
220 | } |
221 | ||
43a18898 | 222 | void wxBitmapButton::SetBitmapDisabled( const wxBitmap& bitmap ) |
58614078 | 223 | { |
43a18898 RR |
224 | wxCHECK_RET( m_widget != NULL, "invalid button" ); |
225 | ||
226 | if ( ! m_disabled.Ok() ) return; | |
227 | m_disabled = bitmap; | |
228 | ||
229 | SetBitmap(); | |
58614078 RR |
230 | } |
231 | ||
43a18898 RR |
232 | void wxBitmapButton::SetBitmapFocus( const wxBitmap& bitmap ) |
233 | { | |
234 | wxCHECK_RET( m_widget != NULL, "invalid button" ); | |
235 | ||
236 | if ( ! m_focus.Ok() ) return; | |
237 | m_focus = bitmap; | |
238 | ||
239 | SetBitmap(); | |
240 | } | |
241 | ||
242 | void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap ) | |
243 | { | |
244 | wxCHECK_RET( m_widget != NULL, "invalid button" ); | |
245 | ||
246 | if (!m_bitmap.Ok()) return; | |
247 | m_bitmap = bitmap; | |
248 | ||
249 | SetBitmap(); | |
250 | } | |
251 | ||
252 | void wxBitmapButton::SetBitmapSelected( const wxBitmap& bitmap ) | |
253 | { | |
254 | wxCHECK_RET( m_widget != NULL, "invalid button" ); | |
255 | ||
256 | if ( ! m_selected.Ok() ) return; | |
257 | m_selected = bitmap; | |
258 | ||
259 | SetBitmap(); | |
260 | } | |
261 | ||
262 | void wxBitmapButton::Enable( const bool enable ) | |
263 | { | |
264 | wxWindow::Enable(enable); | |
265 | ||
266 | SetBitmap(); | |
267 | } | |
268 | ||
269 | void wxBitmapButton::HasFocus() | |
270 | { | |
271 | m_hasFocus = TRUE; | |
272 | SetBitmap(); | |
273 | } | |
274 | ||
275 | void wxBitmapButton::NotFocus() | |
276 | { | |
277 | m_hasFocus = FALSE; | |
278 | SetBitmap(); | |
279 | } | |
280 | ||
281 | void wxBitmapButton::StartSelect() | |
282 | { | |
283 | m_isSelected = TRUE; | |
284 | SetBitmap(); | |
285 | } | |
286 | ||
287 | void wxBitmapButton::EndSelect() | |
288 | { | |
289 | m_isSelected = FALSE; | |
290 | SetBitmap(); | |
291 | } |