]>
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; |
151ccd11 | 105 | |
43a18898 | 106 | wxSize newSize = size; |
151ccd11 | 107 | |
43a18898 | 108 | PreCreation( parent, id, pos, newSize, style, name ); |
151ccd11 | 109 | |
43a18898 | 110 | SetValidator( validator ); |
6de97a3b | 111 | |
43a18898 RR |
112 | m_bitmap = bitmap; |
113 | m_disabled = bitmap; | |
114 | m_focus = bitmap; | |
115 | m_selected = bitmap; | |
151ccd11 | 116 | |
43a18898 | 117 | m_label = ""; |
151ccd11 | 118 | |
43a18898 RR |
119 | m_widget = gtk_button_new(); |
120 | ||
121 | if (m_bitmap.Ok()) | |
122 | { | |
123 | GdkBitmap *mask = (GdkBitmap *) NULL; | |
124 | if (m_bitmap.GetMask()) mask = m_bitmap.GetMask()->GetBitmap(); | |
125 | GtkWidget *pixmap = gtk_pixmap_new( m_bitmap.GetPixmap(), mask ); | |
151ccd11 | 126 | |
43a18898 RR |
127 | gtk_widget_show( pixmap ); |
128 | gtk_container_add( GTK_CONTAINER(m_widget), pixmap ); | |
129 | } | |
151ccd11 | 130 | |
43a18898 RR |
131 | if (newSize.x == -1) newSize.x = m_bitmap.GetHeight()+10; |
132 | if (newSize.y == -1) newSize.y = m_bitmap.GetWidth()+10; | |
133 | SetSize( newSize.x, newSize.y ); | |
151ccd11 | 134 | |
43a18898 RR |
135 | gtk_signal_connect( GTK_OBJECT(m_widget), "clicked", |
136 | GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this ); | |
151ccd11 | 137 | |
43a18898 RR |
138 | gtk_signal_connect( GTK_OBJECT(m_widget), "enter", |
139 | GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this ); | |
140 | gtk_signal_connect( GTK_OBJECT(m_widget), "leave", | |
141 | GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this ); | |
142 | gtk_signal_connect( GTK_OBJECT(m_widget), "pressed", | |
143 | GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this ); | |
144 | gtk_signal_connect( GTK_OBJECT(m_widget), "released", | |
145 | GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this ); | |
146 | ||
147 | m_parent->AddChild( this ); | |
6ca41e57 | 148 | |
43a18898 | 149 | (m_parent->m_insertCallback)( m_parent, this ); |
6ca41e57 | 150 | |
43a18898 | 151 | PostCreation(); |
151ccd11 | 152 | |
43a18898 | 153 | SetBackgroundColour( parent->GetBackgroundColour() ); |
f96aa4d9 | 154 | |
43a18898 | 155 | Show( TRUE ); |
151ccd11 | 156 | |
43a18898 | 157 | return TRUE; |
6de97a3b | 158 | } |
151ccd11 | 159 | |
43a18898 | 160 | void wxBitmapButton::SetDefault() |
151ccd11 | 161 | { |
903f689b | 162 | /* |
43a18898 RR |
163 | GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); |
164 | gtk_widget_grab_default( m_widget ); | |
903f689b | 165 | */ |
6de97a3b | 166 | } |
151ccd11 RR |
167 | |
168 | void wxBitmapButton::SetLabel( const wxString &label ) | |
169 | { | |
43a18898 | 170 | wxCHECK_RET( m_widget != NULL, "invalid button" ); |
f96aa4d9 | 171 | |
43a18898 | 172 | wxControl::SetLabel( label ); |
6de97a3b | 173 | } |
151ccd11 | 174 | |
43a18898 | 175 | wxString wxBitmapButton::GetLabel() const |
151ccd11 | 176 | { |
43a18898 | 177 | wxCHECK_MSG( m_widget != NULL, "", "invalid button" ); |
f96aa4d9 | 178 | |
43a18898 | 179 | return wxControl::GetLabel(); |
6de97a3b | 180 | } |
903f689b | 181 | |
43a18898 | 182 | void wxBitmapButton::ApplyWidgetStyle() |
903f689b | 183 | { |
43a18898 RR |
184 | } |
185 | ||
186 | void wxBitmapButton::SetBitmap() | |
187 | { | |
188 | wxCHECK_RET( m_widget != NULL, "invalid button" ); | |
f96aa4d9 | 189 | |
43a18898 | 190 | wxBitmap the_one; |
903f689b | 191 | |
43a18898 RR |
192 | if ( ! m_isEnabled ) |
193 | the_one = m_disabled; | |
194 | else | |
195 | { | |
196 | if ( m_isSelected ) | |
197 | { | |
198 | the_one = m_selected; | |
199 | } | |
200 | else | |
201 | { | |
202 | if ( m_hasFocus ) | |
203 | the_one = m_focus; | |
204 | else | |
205 | the_one = m_bitmap; | |
206 | } | |
207 | } | |
208 | ||
209 | if ( ! the_one.Ok() ) the_one = m_bitmap; | |
210 | if ( ! the_one.Ok() ) return; | |
903f689b | 211 | |
43a18898 RR |
212 | GtkButton *bin = GTK_BUTTON( m_widget ); |
213 | GtkPixmap *g_pixmap = GTK_PIXMAP( bin->child ); | |
903f689b | 214 | |
43a18898 RR |
215 | GdkBitmap *mask = (GdkBitmap *) NULL; |
216 | if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap(); | |
217 | ||
218 | gtk_pixmap_set( g_pixmap, the_one.GetPixmap(), mask ); | |
903f689b RR |
219 | } |
220 | ||
43a18898 | 221 | void wxBitmapButton::SetBitmapDisabled( const wxBitmap& bitmap ) |
58614078 | 222 | { |
43a18898 RR |
223 | wxCHECK_RET( m_widget != NULL, "invalid button" ); |
224 | ||
225 | if ( ! m_disabled.Ok() ) return; | |
226 | m_disabled = bitmap; | |
227 | ||
228 | SetBitmap(); | |
58614078 RR |
229 | } |
230 | ||
43a18898 RR |
231 | void wxBitmapButton::SetBitmapFocus( const wxBitmap& bitmap ) |
232 | { | |
233 | wxCHECK_RET( m_widget != NULL, "invalid button" ); | |
234 | ||
235 | if ( ! m_focus.Ok() ) return; | |
236 | m_focus = bitmap; | |
237 | ||
238 | SetBitmap(); | |
239 | } | |
240 | ||
241 | void wxBitmapButton::SetBitmapLabel( const wxBitmap& bitmap ) | |
242 | { | |
243 | wxCHECK_RET( m_widget != NULL, "invalid button" ); | |
244 | ||
245 | if (!m_bitmap.Ok()) return; | |
246 | m_bitmap = bitmap; | |
247 | ||
248 | SetBitmap(); | |
249 | } | |
250 | ||
251 | void wxBitmapButton::SetBitmapSelected( const wxBitmap& bitmap ) | |
252 | { | |
253 | wxCHECK_RET( m_widget != NULL, "invalid button" ); | |
254 | ||
255 | if ( ! m_selected.Ok() ) return; | |
256 | m_selected = bitmap; | |
257 | ||
258 | SetBitmap(); | |
259 | } | |
260 | ||
261 | void wxBitmapButton::Enable( const bool enable ) | |
262 | { | |
263 | wxWindow::Enable(enable); | |
264 | ||
265 | SetBitmap(); | |
266 | } | |
267 | ||
268 | void wxBitmapButton::HasFocus() | |
269 | { | |
270 | m_hasFocus = TRUE; | |
271 | SetBitmap(); | |
272 | } | |
273 | ||
274 | void wxBitmapButton::NotFocus() | |
275 | { | |
276 | m_hasFocus = FALSE; | |
277 | SetBitmap(); | |
278 | } | |
279 | ||
280 | void wxBitmapButton::StartSelect() | |
281 | { | |
282 | m_isSelected = TRUE; | |
283 | SetBitmap(); | |
284 | } | |
285 | ||
286 | void wxBitmapButton::EndSelect() | |
287 | { | |
288 | m_isSelected = FALSE; | |
289 | SetBitmap(); | |
290 | } |