1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/bmpbuttn.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "bmpbuttn.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
21 #include "wx/bmpbuttn.h"
23 #include "wx/gtk/private.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern void wxapp_install_idle_handler();
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 extern bool g_blockEventsOnDrag
;
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 static void gtk_bmpbutton_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxBitmapButton
*button
)
51 wxapp_install_idle_handler();
53 if (!button
->m_hasVMT
) return;
54 if (g_blockEventsOnDrag
) return;
56 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
57 event
.SetEventObject(button
);
58 button
->GetEventHandler()->ProcessEvent(event
);
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 static void gtk_bmpbutton_enter_callback( GtkWidget
*WXUNUSED(widget
), wxBitmapButton
*button
)
67 if (!button
->m_hasVMT
) return;
68 if (g_blockEventsOnDrag
) return;
73 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
77 static void gtk_bmpbutton_leave_callback( GtkWidget
*WXUNUSED(widget
), wxBitmapButton
*button
)
79 if (!button
->m_hasVMT
) return;
80 if (g_blockEventsOnDrag
) return;
85 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
89 static void gtk_bmpbutton_press_callback( GtkWidget
*WXUNUSED(widget
), wxBitmapButton
*button
)
91 if (!button
->m_hasVMT
) return;
92 if (g_blockEventsOnDrag
) return;
94 button
->StartSelect();
97 //-----------------------------------------------------------------------------
99 //-----------------------------------------------------------------------------
101 static void gtk_bmpbutton_release_callback( GtkWidget
*WXUNUSED(widget
), wxBitmapButton
*button
)
103 if (!button
->m_hasVMT
) return;
104 if (g_blockEventsOnDrag
) return;
109 //-----------------------------------------------------------------------------
111 //-----------------------------------------------------------------------------
113 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
,wxButton
)
115 void wxBitmapButton::Init()
118 m_isSelected
= FALSE
;
121 bool wxBitmapButton::Create( wxWindow
*parent
,
123 const wxBitmap
& bitmap
,
127 const wxValidator
& validator
,
128 const wxString
&name
)
131 m_acceptsFocus
= TRUE
;
133 if (!PreCreation( parent
, pos
, size
) ||
134 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
136 wxFAIL_MSG( wxT("wxBitmapButton creation failed") );
140 m_bmpNormal
= bitmap
;
142 m_widget
= gtk_button_new();
144 if (style
& wxNO_BORDER
)
145 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
147 if (m_bmpNormal
.Ok())
152 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
153 GTK_SIGNAL_FUNC(gtk_bmpbutton_clicked_callback
), (gpointer
*)this );
155 gtk_signal_connect( GTK_OBJECT(m_widget
), "enter",
156 GTK_SIGNAL_FUNC(gtk_bmpbutton_enter_callback
), (gpointer
*)this );
157 gtk_signal_connect( GTK_OBJECT(m_widget
), "leave",
158 GTK_SIGNAL_FUNC(gtk_bmpbutton_leave_callback
), (gpointer
*)this );
159 gtk_signal_connect( GTK_OBJECT(m_widget
), "pressed",
160 GTK_SIGNAL_FUNC(gtk_bmpbutton_press_callback
), (gpointer
*)this );
161 gtk_signal_connect( GTK_OBJECT(m_widget
), "released",
162 GTK_SIGNAL_FUNC(gtk_bmpbutton_release_callback
), (gpointer
*)this );
164 m_parent
->DoAddChild( this );
171 void wxBitmapButton::SetDefault()
173 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
174 gtk_widget_grab_default( m_widget
);
176 SetSize( m_x
, m_y
, m_width
, m_height
);
179 void wxBitmapButton::SetLabel( const wxString
&label
)
181 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
183 wxControl::SetLabel( label
);
186 wxString
wxBitmapButton::GetLabel() const
188 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid button") );
190 return wxControl::GetLabel();
193 void wxBitmapButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
195 if ( !BUTTON_CHILD(m_widget
) )
198 wxButton::DoApplyWidgetStyle(style
);
201 void wxBitmapButton::OnSetBitmap()
203 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid bitmap button") );
205 InvalidateBestSize();
209 the_one
= m_bmpDisabled
;
210 else if (m_isSelected
)
211 the_one
= m_bmpSelected
;
213 the_one
= m_bmpFocus
;
218 the_one
= m_bmpSelected
;
223 the_one
= m_bmpFocus
;
225 the_one
= m_bmpNormal
;
229 if (!the_one
.Ok()) the_one
= m_bmpNormal
;
230 if (!the_one
.Ok()) return;
232 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
233 if (the_one
.GetMask()) mask
= the_one
.GetMask()->GetBitmap();
235 GtkWidget
*child
= BUTTON_CHILD(m_widget
);
241 if (the_one
.HasPixbuf())
242 pixmap
= gtk_image_new_from_pixbuf(the_one
.GetPixbuf());
244 pixmap
= gtk_image_new_from_pixmap(the_one
.GetPixmap(), mask
);
246 pixmap
= gtk_pixmap_new(the_one
.GetPixmap(), mask
);
248 gtk_widget_show(pixmap
);
249 gtk_container_add(GTK_CONTAINER(m_widget
), pixmap
);
252 { // subsequent bitmaps
254 GtkImage
*pixmap
= GTK_IMAGE(child
);
255 if (the_one
.HasPixbuf())
256 gtk_image_set_from_pixbuf(pixmap
, the_one
.GetPixbuf());
258 gtk_image_set_from_pixmap(pixmap
, the_one
.GetPixmap(), mask
);
260 GtkPixmap
*pixmap
= GTK_PIXMAP(child
);
261 gtk_pixmap_set(pixmap
, the_one
.GetPixmap(), mask
);
266 wxSize
wxBitmapButton::DoGetBestSize() const
270 if (m_bmpNormal
.Ok())
272 int border
= HasFlag(wxNO_BORDER
) ? 4 : 10;
273 best
.x
= m_bmpNormal
.GetWidth()+border
;
274 best
.y
= m_bmpNormal
.GetHeight()+border
;
280 bool wxBitmapButton::Enable( bool enable
)
282 if ( !wxWindow::Enable(enable
) )
290 void wxBitmapButton::HasFocus()
296 void wxBitmapButton::NotFocus()
302 void wxBitmapButton::StartSelect()
308 void wxBitmapButton::EndSelect()
310 m_isSelected
= FALSE
;
314 #endif // wxUSE_BMPBUTTON