]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bmpbuttn.cpp
For consistency with other ports, send TEXT event after SELECTED event
[wxWidgets.git] / src / gtk / bmpbuttn.cpp
CommitLineData
151ccd11 1/////////////////////////////////////////////////////////////////////////////
f6bcfd97 2// Name: gtk/bmpbuttn.cpp
151ccd11
RR
3// Purpose:
4// Author: Robert Roebling
f96aa4d9 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
151ccd11
RR
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
1e6feb95 13#include "wx/defs.h"
151ccd11 14
dcf924a3
RR
15#if wxUSE_BMPBUTTON
16
1e6feb95
VZ
17#include "wx/bmpbuttn.h"
18
9e691f46 19#include "wx/gtk/private.h"
83624f79 20
151ccd11
RR
21//-----------------------------------------------------------------------------
22// classes
23//-----------------------------------------------------------------------------
24
25class wxBitmapButton;
26
acfd422a
RR
27//-----------------------------------------------------------------------------
28// idle system
29//-----------------------------------------------------------------------------
30
31extern void wxapp_install_idle_handler();
32extern bool g_isIdle;
33
66bd6b93
RR
34//-----------------------------------------------------------------------------
35// data
36//-----------------------------------------------------------------------------
37
38extern bool g_blockEventsOnDrag;
39
151ccd11 40//-----------------------------------------------------------------------------
e1e955e1 41// "clicked"
151ccd11
RR
42//-----------------------------------------------------------------------------
43
865bb325 44extern "C" {
66bd6b93 45static void gtk_bmpbutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
151ccd11 46{
f6bcfd97 47 if (g_isIdle)
bf9e3e73 48 wxapp_install_idle_handler();
acfd422a 49
a2053b27 50 if (!button->m_hasVMT) return;
43a18898 51 if (g_blockEventsOnDrag) return;
f6bcfd97 52
43a18898
RR
53 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
54 event.SetEventObject(button);
55 button->GetEventHandler()->ProcessEvent(event);
56}
865bb325 57}
43a18898
RR
58
59//-----------------------------------------------------------------------------
60// "enter"
61//-----------------------------------------------------------------------------
62
865bb325 63extern "C" {
43a18898
RR
64static void gtk_bmpbutton_enter_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
65{
a2053b27 66 if (!button->m_hasVMT) return;
43a18898
RR
67 if (g_blockEventsOnDrag) return;
68
f6bcfd97 69 button->HasFocus();
43a18898 70}
865bb325 71}
43a18898
RR
72
73//-----------------------------------------------------------------------------
74// "leave"
75//-----------------------------------------------------------------------------
76
865bb325 77extern "C" {
43a18898
RR
78static void gtk_bmpbutton_leave_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
79{
a2053b27 80 if (!button->m_hasVMT) return;
43a18898
RR
81 if (g_blockEventsOnDrag) return;
82
f6bcfd97 83 button->NotFocus();
43a18898 84}
865bb325 85}
43a18898
RR
86
87//-----------------------------------------------------------------------------
88// "pressed"
89//-----------------------------------------------------------------------------
90
865bb325 91extern "C" {
43a18898
RR
92static void gtk_bmpbutton_press_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
93{
a2053b27 94 if (!button->m_hasVMT) return;
43a18898
RR
95 if (g_blockEventsOnDrag) return;
96
f6bcfd97 97 button->StartSelect();
43a18898 98}
865bb325 99}
43a18898
RR
100
101//-----------------------------------------------------------------------------
102// "released"
103//-----------------------------------------------------------------------------
104
865bb325 105extern "C" {
43a18898
RR
106static void gtk_bmpbutton_release_callback( GtkWidget *WXUNUSED(widget), wxBitmapButton *button )
107{
a2053b27 108 if (!button->m_hasVMT) return;
43a18898
RR
109 if (g_blockEventsOnDrag) return;
110
f6bcfd97 111 button->EndSelect();
6de97a3b 112}
865bb325 113}
151ccd11
RR
114
115//-----------------------------------------------------------------------------
e1e955e1
RR
116// wxBitmapButton
117//-----------------------------------------------------------------------------
118
42b4e99e 119IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton,wxButton)
151ccd11 120
c3dfaa10 121void wxBitmapButton::Init()
151ccd11 122{
c3dfaa10 123 m_hasFocus =
902725ee 124 m_isSelected = false;
6de97a3b 125}
151ccd11 126
c3dfaa10
VZ
127bool wxBitmapButton::Create( wxWindow *parent,
128 wxWindowID id,
129 const wxBitmap& bitmap,
130 const wxPoint& pos,
131 const wxSize& size,
132 long style,
133 const wxValidator& validator,
134 const wxString &name )
151ccd11 135{
902725ee
WS
136 m_needParent = true;
137 m_acceptsFocus = true;
f6bcfd97 138
4dcaf11a
RR
139 if (!PreCreation( parent, pos, size ) ||
140 !CreateBase( parent, id, pos, size, style, validator, name ))
141 {
223d09f6 142 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
902725ee 143 return false;
4dcaf11a 144 }
6de97a3b 145
189f58fa 146 m_bmpNormal = bitmap;
f6bcfd97 147
43a18898 148 m_widget = gtk_button_new();
de1c750f 149
de1c750f
RR
150 if (style & wxNO_BORDER)
151 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 152
1e6feb95 153 if (m_bmpNormal.Ok())
43a18898 154 {
29149a64 155 OnSetBitmap();
43a18898 156 }
f6bcfd97 157
58b907f6 158 gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked",
43a18898 159 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback), (gpointer*)this );
151ccd11 160
f6bcfd97 161 gtk_signal_connect( GTK_OBJECT(m_widget), "enter",
43a18898 162 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback), (gpointer*)this );
f6bcfd97 163 gtk_signal_connect( GTK_OBJECT(m_widget), "leave",
43a18898 164 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback), (gpointer*)this );
f6bcfd97 165 gtk_signal_connect( GTK_OBJECT(m_widget), "pressed",
43a18898 166 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback), (gpointer*)this );
f6bcfd97 167 gtk_signal_connect( GTK_OBJECT(m_widget), "released",
43a18898 168 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback), (gpointer*)this );
f6bcfd97 169
eb082a08 170 m_parent->DoAddChild( this );
f6bcfd97 171
abdeb9e7 172 PostCreation(size);
f6bcfd97 173
902725ee 174 return true;
6de97a3b 175}
f6bcfd97 176
43a18898 177void wxBitmapButton::SetDefault()
151ccd11 178{
43a18898
RR
179 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
180 gtk_widget_grab_default( m_widget );
f6bcfd97 181
3502e687 182 SetSize( m_x, m_y, m_width, m_height );
6de97a3b 183}
151ccd11
RR
184
185void wxBitmapButton::SetLabel( const wxString &label )
186{
223d09f6 187 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
f96aa4d9 188
43a18898 189 wxControl::SetLabel( label );
6de97a3b 190}
151ccd11 191
43a18898 192wxString wxBitmapButton::GetLabel() const
151ccd11 193{
902725ee 194 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid button") );
f96aa4d9 195
43a18898 196 return wxControl::GetLabel();
6de97a3b 197}
903f689b 198
f40fdaa3 199void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
903f689b 200{
9e691f46
VZ
201 if ( !BUTTON_CHILD(m_widget) )
202 return;
29149a64 203
f40fdaa3 204 wxButton::DoApplyWidgetStyle(style);
43a18898
RR
205}
206
29149a64 207void wxBitmapButton::OnSetBitmap()
43a18898 208{
1e6feb95 209 wxCHECK_RET( m_widget != NULL, wxT("invalid bitmap button") );
f96aa4d9 210
9f884528 211 InvalidateBestSize();
902725ee 212
43a18898 213 wxBitmap the_one;
f6bcfd97 214 if (!m_isEnabled)
29149a64 215 the_one = m_bmpDisabled;
67a6726a 216 else if (m_isSelected)
29149a64 217 the_one = m_bmpSelected;
67a6726a 218 else if (m_hasFocus)
29149a64 219 the_one = m_bmpFocus;
f6bcfd97 220 else
902725ee 221 the_one = m_bmpNormal;
43a18898 222
1e6feb95 223 if (!the_one.Ok()) the_one = m_bmpNormal;
de1c750f 224 if (!the_one.Ok()) return;
f6bcfd97 225
43a18898
RR
226 GdkBitmap *mask = (GdkBitmap *) NULL;
227 if (the_one.GetMask()) mask = the_one.GetMask()->GetBitmap();
f6bcfd97 228
9e691f46
VZ
229 GtkWidget *child = BUTTON_CHILD(m_widget);
230 if (child == NULL)
29149a64
VZ
231 {
232 // initial bitmap
4fab7128
VS
233 GtkWidget *pixmap;
234#ifdef __WXGTK20__
235 if (the_one.HasPixbuf())
236 pixmap = gtk_image_new_from_pixbuf(the_one.GetPixbuf());
237 else
238 pixmap = gtk_image_new_from_pixmap(the_one.GetPixmap(), mask);
239#else
240 pixmap = gtk_pixmap_new(the_one.GetPixmap(), mask);
241#endif
67a6726a
RR
242 gtk_widget_show(pixmap);
243 gtk_container_add(GTK_CONTAINER(m_widget), pixmap);
29149a64
VZ
244 }
245 else
67a6726a 246 { // subsequent bitmaps
4fab7128
VS
247#ifdef __WXGTK20__
248 GtkImage *pixmap = GTK_IMAGE(child);
249 if (the_one.HasPixbuf())
250 gtk_image_set_from_pixbuf(pixmap, the_one.GetPixbuf());
251 else
252 gtk_image_set_from_pixmap(pixmap, the_one.GetPixmap(), mask);
253#else
254 GtkPixmap *pixmap = GTK_PIXMAP(child);
255 gtk_pixmap_set(pixmap, the_one.GetPixmap(), mask);
256#endif
67a6726a 257 }
903f689b
RR
258}
259
e0aeebed
VS
260wxSize wxBitmapButton::DoGetBestSize() const
261{
262 return wxControl::DoGetBestSize();
263}
264
f03fc89f 265bool wxBitmapButton::Enable( bool enable )
43a18898 266{
f03fc89f 267 if ( !wxWindow::Enable(enable) )
902725ee 268 return false;
43a18898 269
29149a64 270 OnSetBitmap();
f03fc89f 271
902725ee 272 return true;
43a18898
RR
273}
274
275void wxBitmapButton::HasFocus()
276{
902725ee 277 m_hasFocus = true;
29149a64 278 OnSetBitmap();
43a18898
RR
279}
280
281void wxBitmapButton::NotFocus()
282{
902725ee 283 m_hasFocus = false;
29149a64 284 OnSetBitmap();
43a18898
RR
285}
286
287void wxBitmapButton::StartSelect()
288{
902725ee 289 m_isSelected = true;
29149a64 290 OnSetBitmap();
43a18898
RR
291}
292
293void wxBitmapButton::EndSelect()
294{
902725ee 295 m_isSelected = false;
29149a64 296 OnSetBitmap();
43a18898 297}
f6bcfd97
BP
298
299#endif // wxUSE_BMPBUTTON
dcf924a3 300