]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bmpbuttn.cpp
Rearrange code to fix some problems
[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 GtkWidget *child = GTK_BIN(m_widget)->child;
198 if (child == NULL)
199 {
200 // initial bitmap
201 GtkWidget *pixmap =
202 gtk_image_new_from_pixbuf(the_one.GetPixbuf());
203
204 gtk_widget_show(pixmap);
205 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
206 }
207 else
208 { // subsequent bitmaps
209 GtkImage *pixmap = GTK_IMAGE(child);
210 gtk_image_set_from_pixbuf(pixmap, the_one.GetPixbuf());
211 }
212 }
213
214 wxSize wxBitmapButton::DoGetBestSize() const
215 {
216 return wxControl::DoGetBestSize();
217 }
218
219 bool wxBitmapButton::Enable( bool enable )
220 {
221 if ( !wxWindow::Enable(enable) )
222 return false;
223
224 OnSetBitmap();
225
226 return true;
227 }
228
229 void wxBitmapButton::HasFocus()
230 {
231 m_hasFocus = true;
232 OnSetBitmap();
233 }
234
235 void wxBitmapButton::NotFocus()
236 {
237 m_hasFocus = false;
238 OnSetBitmap();
239 }
240
241 void wxBitmapButton::StartSelect()
242 {
243 m_isSelected = true;
244 OnSetBitmap();
245 }
246
247 void wxBitmapButton::EndSelect()
248 {
249 m_isSelected = false;
250 OnSetBitmap();
251 }
252
253 #endif // wxUSE_BMPBUTTON