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 image
= gtk_button_get_image(GTK_BUTTON(m_widget
));
186 wxCHECK_RET( image
&& GTK_IS_IMAGE(image
), "must have image widget" );
188 gtk_image_set_from_pixbuf(GTK_IMAGE(image
), bitmap
.GetPixbuf());
191 wxBitmap
wxAnyButton::DoGetBitmap(State which
) const
193 return m_bitmaps
[which
];
196 void wxAnyButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
201 if ( DontShowLabel() )
203 // we only have the bitmap in this button, never remove it but
204 // do invalidate the best size when the bitmap (and presumably
206 InvalidateBestSize();
208 // normal image is special: setting it enables images for the button and
209 // resetting it to nothing disables all of them
212 GtkWidget
*image
= gtk_button_get_image(GTK_BUTTON(m_widget
));
213 if ( image
&& !bitmap
.IsOk() )
215 gtk_container_remove(GTK_CONTAINER(m_widget
), image
);
217 else if ( !image
&& bitmap
.IsOk() )
219 image
= gtk_image_new();
220 gtk_button_set_image(GTK_BUTTON(m_widget
), image
);
222 else // image presence or absence didn't change
224 // don't invalidate best size below
228 InvalidateBestSize();
235 if ( !m_bitmaps
[which
].IsOk() )
237 // we need to install the callbacks to be notified about
238 // the button pressed state change
243 G_CALLBACK(wxgtk_button_press_callback
),
251 G_CALLBACK(wxgtk_button_released_callback
),
256 else // no valid bitmap
258 if ( m_bitmaps
[which
].IsOk() )
260 // we don't need to be notified about the button pressed
261 // state changes any more
262 g_signal_handlers_disconnect_by_func
265 (gpointer
)wxgtk_button_press_callback
,
269 g_signal_handlers_disconnect_by_func
272 (gpointer
)wxgtk_button_released_callback
,
276 // also make sure we don't remain stuck in pressed state
287 // the logic here is the same as above for State_Pressed: we need
288 // to connect the handlers if we must be notified about the changes
289 // in the button current state and we disconnect them when/if we
290 // don't need them any more
293 if ( !m_bitmaps
[which
].IsOk() )
299 G_CALLBACK(wxgtk_button_enter_callback
),
307 G_CALLBACK(wxgtk_button_leave_callback
),
312 else // no valid bitmap
314 if ( m_bitmaps
[which
].IsOk() )
316 g_signal_handlers_disconnect_by_func
319 (gpointer
)wxgtk_button_enter_callback
,
323 g_signal_handlers_disconnect_by_func
326 (gpointer
)wxgtk_button_leave_callback
,
342 Connect(wxEVT_SET_FOCUS
,
343 wxFocusEventHandler(wxAnyButton::GTKOnFocus
));
344 Connect(wxEVT_KILL_FOCUS
,
345 wxFocusEventHandler(wxAnyButton::GTKOnFocus
));
347 else // no valid focused bitmap
349 Disconnect(wxEVT_SET_FOCUS
,
350 wxFocusEventHandler(wxAnyButton::GTKOnFocus
));
351 Disconnect(wxEVT_KILL_FOCUS
,
352 wxFocusEventHandler(wxAnyButton::GTKOnFocus
));
357 // no callbacks to connect/disconnect
361 m_bitmaps
[which
] = bitmap
;
363 // update the bitmap immediately if necessary, otherwise it will be done
364 // when the bitmap for the corresponding state is needed the next time by
366 if ( bitmap
.IsOk() && which
== GTKGetCurrentState() )
368 GTKDoShowBitmap(bitmap
);
372 void wxAnyButton::DoSetBitmapPosition(wxDirection dir
)
375 if ( !gtk_check_version(2,10,0) )
377 GtkPositionType gtkpos
;
381 wxFAIL_MSG( "invalid position" );
385 gtkpos
= GTK_POS_LEFT
;
389 gtkpos
= GTK_POS_RIGHT
;
393 gtkpos
= GTK_POS_TOP
;
397 gtkpos
= GTK_POS_BOTTOM
;
401 gtk_button_set_image_position(GTK_BUTTON(m_widget
), gtkpos
);
402 InvalidateBestSize();
407 #endif // wxHAS_ANY_BUTTON