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