]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bmpbuttn.cpp
Reorganize idle system code.
[wxWidgets.git] / src / gtk / bmpbuttn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/bmpbuttn.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_BMPBUTTON
14
15 #include "wx/bmpbuttn.h"
16
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 extern "C" {
36 static void gtk_bmpbutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
37 {
38 if (!button->m_hasVMT) return;
39 if (g_blockEventsOnDrag) return;
40
41 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
42 event.SetEventObject(button);
43 button->GetEventHandler()->ProcessEvent(event);
44 }
45 }
46
47 //-----------------------------------------------------------------------------
48 // "enter"
49 //-----------------------------------------------------------------------------
50
51 extern "C" {
52 static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
53 {
54 if (!button->m_hasVMT) return;
55 if (g_blockEventsOnDrag) return;
56
57 button->HasFocus();
58 }
59 }
60
61 //-----------------------------------------------------------------------------
62 // "leave"
63 //-----------------------------------------------------------------------------
64
65 extern "C" {
66 static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
67 {
68 if (!button->m_hasVMT) return;
69 if (g_blockEventsOnDrag) return;
70
71 button->NotFocus();
72 }
73 }
74
75 //-----------------------------------------------------------------------------
76 // "pressed"
77 //-----------------------------------------------------------------------------
78
79 extern "C" {
80 static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
81 {
82 if (!button->m_hasVMT) return;
83 if (g_blockEventsOnDrag) return;
84
85 button->StartSelect();
86 }
87 }
88
89 //-----------------------------------------------------------------------------
90 // "released"
91 //-----------------------------------------------------------------------------
92
93 extern "C" {
94 static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
95 {
96 if (!button->m_hasVMT) return;
97 if (g_blockEventsOnDrag) return;
98
99 button->EndSelect();
100 }
101 }
102
103 //-----------------------------------------------------------------------------
104 // wxBitmapButton
105 //-----------------------------------------------------------------------------
106
107 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxButton)
108
109 void wxBitmapButton::Init()
110 {
111 m_hasFocus =
112 m_isSelected = false;
113 }
114
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 )
123 {
124 m_needParent = true;
125
126 if (!PreCreation( parent, pos, size ) ||
127 !CreateBase( parent, id, pos, size, style, validator, name ))
128 {
129 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
130 return false;
131 }
132
133 m_bmpNormal = bitmap;
134
135 m_widget = gtk_button_new();
136
137 if (style & wxNO_BORDER)
138 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
139
140 if (m_bmpNormal.Ok())
141 {
142 OnSetBitmap();
143 }
144
145 g_signal_connect_after (m_widget, "clicked",
146 G_CALLBACK (gtk_bmpbutton_clicked_callback),
147 this);
148
149 g_signal_connect (m_widget, "enter",
150 G_CALLBACK (gtk_bmpbutton_enter_callback), this);
151 g_signal_connect (m_widget, "leave",
152 G_CALLBACK (gtk_bmpbutton_leave_callback), this);
153 g_signal_connect (m_widget, "pressed",
154 G_CALLBACK (gtk_bmpbutton_press_callback), this);
155 g_signal_connect (m_widget, "released",
156 G_CALLBACK (gtk_bmpbutton_release_callback), this);
157
158 m_parent->DoAddChild( this );
159
160 PostCreation(size);
161
162 return true;
163 }
164
165 void wxBitmapButton::SetLabel( const wxString &label )
166 {
167 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
168
169 wxControl::SetLabel( label );
170 }
171
172 void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
173 {
174 if (!GTK_BIN(m_widget)->child)
175 return;
176
177 wxButton::DoApplyWidgetStyle(style);
178 }
179
180 void wxBitmapButton::OnSetBitmap()
181 {
182 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
183
184 InvalidateBestSize();
185
186 wxBitmap the_one;
187 if (!IsThisEnabled())
188 the_one = m_bmpDisabled;
189 else if (m_isSelected)
190 the_one = m_bmpSelected;
191 else if (m_hasFocus)
192 the_one = m_bmpFocus;
193 else
194 the_one = m_bmpNormal;
195
196 if (!the_one.Ok()) the_one = m_bmpNormal;
197 if (!the_one.Ok()) return;
198
199 GdkBitmap *mask = (GdkBitmap *) NULL;
200 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
201
202 GtkWidget *child = GTK_BIN(m_widget)->child;
203 if (child == NULL)
204 {
205 // initial bitmap
206 GtkWidget *pixmap;
207
208 if (the_one.HasPixbuf())
209 pixmap = gtk_image_new_from_pixbuf(the_one.GetPixbuf());
210 else
211 pixmap = gtk_image_new_from_pixmap(the_one.GetPixmap(), mask);
212
213 gtk_widget_show(pixmap);
214 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
215 }
216 else
217 { // subsequent bitmaps
218 GtkImage *pixmap = GTK_IMAGE(child);
219 if (the_one.HasPixbuf())
220 gtk_image_set_from_pixbuf(pixmap, the_one.GetPixbuf());
221 else
222 gtk_image_set_from_pixmap(pixmap, the_one.GetPixmap(), mask);
223 }
224 }
225
226 wxSize wxBitmapButton::DoGetBestSize() const
227 {
228 return wxControl::DoGetBestSize();
229 }
230
231 bool wxBitmapButton::Enable( bool enable )
232 {
233 if ( !wxWindow::Enable(enable) )
234 return false;
235
236 OnSetBitmap();
237
238 return true;
239 }
240
241 void wxBitmapButton::HasFocus()
242 {
243 m_hasFocus = true;
244 OnSetBitmap();
245 }
246
247 void wxBitmapButton::NotFocus()
248 {
249 m_hasFocus = false;
250 OnSetBitmap();
251 }
252
253 void wxBitmapButton::StartSelect()
254 {
255 m_isSelected = true;
256 OnSetBitmap();
257 }
258
259 void wxBitmapButton::EndSelect()
260 {
261 m_isSelected = false;
262 OnSetBitmap();
263 }
264
265 #endif // wxUSE_BMPBUTTON