]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/bmpbuttn.cpp
optimize Replace() for the common case of replacing one character with another one...
[wxWidgets.git] / src / gtk / bmpbuttn.cpp
... / ...
CommitLineData
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
23class wxBitmapButton;
24
25//-----------------------------------------------------------------------------
26// data
27//-----------------------------------------------------------------------------
28
29extern bool g_blockEventsOnDrag;
30
31//-----------------------------------------------------------------------------
32// "clicked"
33//-----------------------------------------------------------------------------
34
35extern "C" {
36static 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->HandleWindowEvent(event);
44}
45}
46
47//-----------------------------------------------------------------------------
48// "enter"
49//-----------------------------------------------------------------------------
50
51extern "C" {
52static 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->GTKMouseEnters();
58}
59}
60
61//-----------------------------------------------------------------------------
62// "leave"
63//-----------------------------------------------------------------------------
64
65extern "C" {
66static 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->GTKMouseLeaves();
72}
73}
74
75//-----------------------------------------------------------------------------
76// "pressed"
77//-----------------------------------------------------------------------------
78
79extern "C" {
80static 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->GTKPressed();
86}
87}
88
89//-----------------------------------------------------------------------------
90// "released"
91//-----------------------------------------------------------------------------
92
93extern "C" {
94static 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->GTKReleased();
100}
101}
102
103//-----------------------------------------------------------------------------
104// wxBitmapButton
105//-----------------------------------------------------------------------------
106
107IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxButton)
108
109BEGIN_EVENT_TABLE(wxBitmapButton, wxButton)
110 EVT_SET_FOCUS(wxBitmapButton::OnFocusChange)
111 EVT_KILL_FOCUS(wxBitmapButton::OnFocusChange)
112END_EVENT_TABLE()
113
114
115void wxBitmapButton::Init()
116{
117 m_mouseHovers =
118 m_isPressed = false;
119}
120
121bool wxBitmapButton::Create( wxWindow *parent,
122 wxWindowID id,
123 const wxBitmap& bitmap,
124 const wxPoint& pos,
125 const wxSize& size,
126 long style,
127 const wxValidator& validator,
128 const wxString &name )
129{
130 if (!PreCreation( parent, pos, size ) ||
131 !CreateBase( parent, id, pos, size, style, validator, name ))
132 {
133 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
134 return false;
135 }
136
137 m_bmpNormal = bitmap;
138
139 m_widget = gtk_button_new();
140
141 if (style & wxNO_BORDER)
142 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
143
144 if (m_bmpNormal.Ok())
145 {
146 OnSetBitmap();
147 }
148
149 g_signal_connect_after (m_widget, "clicked",
150 G_CALLBACK (gtk_bmpbutton_clicked_callback),
151 this);
152
153 g_signal_connect (m_widget, "enter",
154 G_CALLBACK (gtk_bmpbutton_enter_callback), this);
155 g_signal_connect (m_widget, "leave",
156 G_CALLBACK (gtk_bmpbutton_leave_callback), this);
157 g_signal_connect (m_widget, "pressed",
158 G_CALLBACK (gtk_bmpbutton_press_callback), this);
159 g_signal_connect (m_widget, "released",
160 G_CALLBACK (gtk_bmpbutton_release_callback), this);
161
162 m_parent->DoAddChild( this );
163
164 PostCreation(size);
165
166 return true;
167}
168
169void wxBitmapButton::SetLabel( const wxString &label )
170{
171 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
172
173 wxControl::SetLabel( label );
174}
175
176void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
177{
178 if (!GTK_BIN(m_widget)->child)
179 return;
180
181 wxButton::DoApplyWidgetStyle(style);
182}
183
184void wxBitmapButton::OnSetBitmap()
185{
186 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
187
188 InvalidateBestSize();
189
190 wxBitmap the_one;
191 if (!IsThisEnabled())
192 the_one = m_bmpDisabled;
193 else if (m_isPressed)
194 the_one = m_bmpSelected;
195 else if (m_mouseHovers)
196 the_one = m_bmpHover;
197 else if (HasFocus())
198 the_one = m_bmpFocus;
199 else
200 the_one = m_bmpNormal;
201
202 if (!the_one.Ok())
203 the_one = m_bmpNormal;
204 if (!the_one.Ok())
205 return;
206
207 GtkWidget* image = GTK_BIN(m_widget)->child;
208 if (image == NULL)
209 {
210 image = gtk_image_new();
211 gtk_widget_show(image);
212 gtk_container_add(GTK_CONTAINER(m_widget), image);
213 }
214 // always use pixbuf, because pixmap mask does not
215 // work with disabled images in some themes
216 gtk_image_set_from_pixbuf(GTK_IMAGE(image), the_one.GetPixbuf());
217}
218
219wxSize wxBitmapButton::DoGetBestSize() const
220{
221 return wxControl::DoGetBestSize();
222}
223
224bool wxBitmapButton::Enable( bool enable )
225{
226 if ( !wxWindow::Enable(enable) )
227 return false;
228
229 OnSetBitmap();
230
231 return true;
232}
233
234void wxBitmapButton::GTKMouseEnters()
235{
236 m_mouseHovers = true;
237 OnSetBitmap();
238}
239
240void wxBitmapButton::GTKMouseLeaves()
241{
242 m_mouseHovers = false;
243 OnSetBitmap();
244}
245
246void wxBitmapButton::GTKPressed()
247{
248 m_isPressed = true;
249 OnSetBitmap();
250}
251
252void wxBitmapButton::GTKReleased()
253{
254 m_isPressed = false;
255 OnSetBitmap();
256}
257
258void wxBitmapButton::OnFocusChange(wxFocusEvent& event)
259{
260 event.Skip();
261 OnSetBitmap();
262}
263
264#endif // wxUSE_BMPBUTTON