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