]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bmpbuttn.cpp
fix wxStringOutputStream::Write() in Unicode build when the output overlaps a boundar...
[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 if (!PreCreation( parent, pos, size ) ||
125 !CreateBase( parent, id, pos, size, style, validator, name ))
126 {
127 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
128 return false;
129 }
130
131 m_bmpNormal = bitmap;
132
133 m_widget = gtk_button_new();
134
135 if (style & wxNO_BORDER)
136 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
137
138 if (m_bmpNormal.Ok())
139 {
140 OnSetBitmap();
141 }
142
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);
155
156 m_parent->DoAddChild( this );
157
158 PostCreation(size);
159
160 return true;
161 }
162
163 void wxBitmapButton::SetLabel( const wxString &label )
164 {
165 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
166
167 wxControl::SetLabel( label );
168 }
169
170 void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
171 {
172 if (!GTK_BIN(m_widget)->child)
173 return;
174
175 wxButton::DoApplyWidgetStyle(style);
176 }
177
178 void wxBitmapButton::OnSetBitmap()
179 {
180 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
181
182 InvalidateBestSize();
183
184 wxBitmap the_one;
185 if (!IsThisEnabled())
186 the_one = m_bmpDisabled;
187 else if (m_isSelected)
188 the_one = m_bmpSelected;
189 else if (m_hasFocus)
190 the_one = m_bmpFocus;
191 else
192 the_one = m_bmpNormal;
193
194 if (!the_one.Ok()) the_one = m_bmpNormal;
195 if (!the_one.Ok()) return;
196
197 GdkBitmap *mask = (GdkBitmap *) NULL;
198 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
199
200 GtkWidget *child = GTK_BIN(m_widget)->child;
201 if (child == NULL)
202 {
203 // initial bitmap
204 GtkWidget *pixmap;
205
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);
210
211 gtk_widget_show(pixmap);
212 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
213 }
214 else
215 { // subsequent bitmaps
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);
221 }
222 }
223
224 wxSize wxBitmapButton::DoGetBestSize() const
225 {
226 return wxControl::DoGetBestSize();
227 }
228
229 bool wxBitmapButton::Enable( bool enable )
230 {
231 if ( !wxWindow::Enable(enable) )
232 return false;
233
234 OnSetBitmap();
235
236 return true;
237 }
238
239 void wxBitmapButton::HasFocus()
240 {
241 m_hasFocus = true;
242 OnSetBitmap();
243 }
244
245 void wxBitmapButton::NotFocus()
246 {
247 m_hasFocus = false;
248 OnSetBitmap();
249 }
250
251 void wxBitmapButton::StartSelect()
252 {
253 m_isSelected = true;
254 OnSetBitmap();
255 }
256
257 void wxBitmapButton::EndSelect()
258 {
259 m_isSelected = false;
260 OnSetBitmap();
261 }
262
263 #endif // wxUSE_BMPBUTTON