]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/bmpbuttn.cpp
Only send keypress events to focus widget, not its parents
[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->GetEventHandler()->ProcessEvent(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->HasFocus();
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->NotFocus();
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->StartSelect();
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->EndSelect();
100}
101}
102
103//-----------------------------------------------------------------------------
104// wxBitmapButton
105//-----------------------------------------------------------------------------
106
107IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxButton)
108
109void wxBitmapButton::Init()
110{
111 m_hasFocus =
112 m_isSelected = false;
113}
114
115bool 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
163void wxBitmapButton::SetLabel( const wxString &label )
164{
165 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
166
167 wxControl::SetLabel( label );
168}
169
170void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
171{
172 if (!GTK_BIN(m_widget)->child)
173 return;
174
175 wxButton::DoApplyWidgetStyle(style);
176}
177
178void 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
214wxSize wxBitmapButton::DoGetBestSize() const
215{
216 return wxControl::DoGetBestSize();
217}
218
219bool wxBitmapButton::Enable( bool enable )
220{
221 if ( !wxWindow::Enable(enable) )
222 return false;
223
224 OnSetBitmap();
225
226 return true;
227}
228
229void wxBitmapButton::HasFocus()
230{
231 m_hasFocus = true;
232 OnSetBitmap();
233}
234
235void wxBitmapButton::NotFocus()
236{
237 m_hasFocus = false;
238 OnSetBitmap();
239}
240
241void wxBitmapButton::StartSelect()
242{
243 m_isSelected = true;
244 OnSetBitmap();
245}
246
247void wxBitmapButton::EndSelect()
248{
249 m_isSelected = false;
250 OnSetBitmap();
251}
252
253#endif // wxUSE_BMPBUTTON