1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/anybutton.cpp
4 // Author: Robert Roebling
5 // Created: 1998-05-20 (extracted from button.cpp)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #ifdef wxHAS_ANY_BUTTON
17 #include "wx/anybutton.h"
20 #include "wx/stockitem.h"
23 #include "wx/gtk/private/gtk2-compat.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
33 wxgtk_button_enter_callback(GtkWidget
*WXUNUSED(widget
), wxAnyButton
*button
)
35 if ( button
->GTKShouldIgnoreEvent() )
38 button
->GTKMouseEnters();
42 wxgtk_button_leave_callback(GtkWidget
*WXUNUSED(widget
), wxAnyButton
*button
)
44 if ( button
->GTKShouldIgnoreEvent() )
47 button
->GTKMouseLeaves();
51 wxgtk_button_press_callback(GtkWidget
*WXUNUSED(widget
), wxAnyButton
*button
)
53 if ( button
->GTKShouldIgnoreEvent() )
60 wxgtk_button_released_callback(GtkWidget
*WXUNUSED(widget
), wxAnyButton
*button
)
62 if ( button
->GTKShouldIgnoreEvent() )
65 button
->GTKReleased();
70 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
74 bool wxAnyButton::Enable( bool enable
)
76 if (!base_type::Enable(enable
))
79 gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget
)), enable
);
89 GdkWindow
*wxAnyButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
91 return gtk_button_get_event_window(GTK_BUTTON(m_widget
));
96 wxAnyButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
98 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 void wxAnyButton::GTKMouseEnters()
112 void wxAnyButton::GTKMouseLeaves()
119 void wxAnyButton::GTKPressed()
126 void wxAnyButton::GTKReleased()
133 void wxAnyButton::GTKOnFocus(wxFocusEvent
& event
)
140 wxAnyButton::State
wxAnyButton::GTKGetCurrentState() const
142 if ( !IsThisEnabled() )
143 return m_bitmaps
[State_Disabled
].IsOk() ? State_Disabled
: State_Normal
;
145 if ( m_isPressed
&& m_bitmaps
[State_Pressed
].IsOk() )
146 return State_Pressed
;
148 if ( m_isCurrent
&& m_bitmaps
[State_Current
].IsOk() )
149 return State_Current
;
151 if ( HasFocus() && m_bitmaps
[State_Focused
].IsOk() )
152 return State_Focused
;
157 void wxAnyButton::GTKUpdateBitmap()
159 // if we don't show bitmaps at all, there is nothing to update
160 if ( m_bitmaps
[State_Normal
].IsOk() )
162 // if we do show them, this will return a state for which we do have a
164 State state
= GTKGetCurrentState();
166 GTKDoShowBitmap(m_bitmaps
[state
]);
170 void wxAnyButton::GTKDoShowBitmap(const wxBitmap
& bitmap
)
172 wxASSERT_MSG( bitmap
.IsOk(), "invalid bitmap" );
175 if ( DontShowLabel() )
177 image
= gtk_bin_get_child(GTK_BIN(m_widget
));
179 else // have both label and bitmap
182 if ( !gtk_check_version(2,6,0) )
184 image
= gtk_button_get_image(GTK_BUTTON(m_widget
));
187 #endif // __WXGTK26__
189 // buttons with both label and bitmap are only supported with GTK+
192 // it shouldn't be difficult to implement them ourselves for the
193 // previous GTK+ versions by stuffing a container with a label and
194 // an image inside GtkButton but there doesn't seem to be much
195 // point in doing this for ancient GTK+ versions
200 wxCHECK_RET( image
&& GTK_IS_IMAGE(image
), "must have image widget" );
202 gtk_image_set_from_pixbuf(GTK_IMAGE(image
), bitmap
.GetPixbuf());
205 wxBitmap
wxAnyButton::DoGetBitmap(State which
) const
207 return m_bitmaps
[which
];
210 void wxAnyButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
215 if ( DontShowLabel() )
217 // we only have the bitmap in this button, never remove it but
218 // do invalidate the best size when the bitmap (and presumably
220 InvalidateBestSize();
223 // normal image is special: setting it enables images for the button and
224 // resetting it to nothing disables all of them
225 else if ( !gtk_check_version(2,6,0) )
227 GtkWidget
*image
= gtk_button_get_image(GTK_BUTTON(m_widget
));
228 if ( image
&& !bitmap
.IsOk() )
230 gtk_container_remove(GTK_CONTAINER(m_widget
), image
);
232 else if ( !image
&& bitmap
.IsOk() )
234 image
= gtk_image_new();
235 gtk_button_set_image(GTK_BUTTON(m_widget
), image
);
237 else // image presence or absence didn't change
239 // don't invalidate best size below
243 InvalidateBestSize();
251 if ( !m_bitmaps
[which
].IsOk() )
253 // we need to install the callbacks to be notified about
254 // the button pressed state change
259 G_CALLBACK(wxgtk_button_press_callback
),
267 G_CALLBACK(wxgtk_button_released_callback
),
272 else // no valid bitmap
274 if ( m_bitmaps
[which
].IsOk() )
276 // we don't need to be notified about the button pressed
277 // state changes any more
278 g_signal_handlers_disconnect_by_func
281 (gpointer
)wxgtk_button_press_callback
,
285 g_signal_handlers_disconnect_by_func
288 (gpointer
)wxgtk_button_released_callback
,
292 // also make sure we don't remain stuck in pressed state
303 // the logic here is the same as above for State_Pressed: we need
304 // to connect the handlers if we must be notified about the changes
305 // in the button current state and we disconnect them when/if we
306 // don't need them any more
309 if ( !m_bitmaps
[which
].IsOk() )
315 G_CALLBACK(wxgtk_button_enter_callback
),
323 G_CALLBACK(wxgtk_button_leave_callback
),
328 else // no valid bitmap
330 if ( m_bitmaps
[which
].IsOk() )
332 g_signal_handlers_disconnect_by_func
335 (gpointer
)wxgtk_button_enter_callback
,
339 g_signal_handlers_disconnect_by_func
342 (gpointer
)wxgtk_button_leave_callback
,
358 Connect(wxEVT_SET_FOCUS
,
359 wxFocusEventHandler(wxAnyButton::GTKOnFocus
));
360 Connect(wxEVT_KILL_FOCUS
,
361 wxFocusEventHandler(wxAnyButton::GTKOnFocus
));
363 else // no valid focused bitmap
365 Disconnect(wxEVT_SET_FOCUS
,
366 wxFocusEventHandler(wxAnyButton::GTKOnFocus
));
367 Disconnect(wxEVT_KILL_FOCUS
,
368 wxFocusEventHandler(wxAnyButton::GTKOnFocus
));
373 // no callbacks to connect/disconnect
377 m_bitmaps
[which
] = bitmap
;
379 // update the bitmap immediately if necessary, otherwise it will be done
380 // when the bitmap for the corresponding state is needed the next time by
382 if ( bitmap
.IsOk() && which
== GTKGetCurrentState() )
384 GTKDoShowBitmap(bitmap
);
388 void wxAnyButton::DoSetBitmapPosition(wxDirection dir
)
391 if ( !gtk_check_version(2,10,0) )
393 GtkPositionType gtkpos
;
397 wxFAIL_MSG( "invalid position" );
401 gtkpos
= GTK_POS_LEFT
;
405 gtkpos
= GTK_POS_RIGHT
;
409 gtkpos
= GTK_POS_TOP
;
413 gtkpos
= GTK_POS_BOTTOM
;
417 gtk_button_set_image_position(GTK_BUTTON(m_widget
), gtkpos
);
418 InvalidateBestSize();
423 #endif // wxHAS_ANY_BUTTON