1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/anybutton.cpp
4 // Author: Robert Roebling
5 // Created: 1998-05-20 (extracted from button.cpp)
6 // Id: $Id: anybutton.cpp 67326 2011-03-28 06:27:49Z PC $
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"
22 #include "wx/gtk/private.h"
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
32 wxgtk_button_enter_callback(GtkWidget
*WXUNUSED(widget
), wxAnyButton
*button
)
34 if ( button
->GTKShouldIgnoreEvent() )
37 button
->GTKMouseEnters();
41 wxgtk_button_leave_callback(GtkWidget
*WXUNUSED(widget
), wxAnyButton
*button
)
43 if ( button
->GTKShouldIgnoreEvent() )
46 button
->GTKMouseLeaves();
50 wxgtk_button_press_callback(GtkWidget
*WXUNUSED(widget
), wxAnyButton
*button
)
52 if ( button
->GTKShouldIgnoreEvent() )
59 wxgtk_button_released_callback(GtkWidget
*WXUNUSED(widget
), wxAnyButton
*button
)
61 if ( button
->GTKShouldIgnoreEvent() )
64 button
->GTKReleased();
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
73 bool wxAnyButton::Enable( bool enable
)
75 if (!base_type::Enable(enable
))
78 gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget
)), enable
);
88 GdkWindow
*wxAnyButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
90 return GTK_BUTTON(m_widget
)->event_window
;
95 wxAnyButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
97 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 void wxAnyButton::GTKMouseEnters()
111 void wxAnyButton::GTKMouseLeaves()
118 void wxAnyButton::GTKPressed()
125 void wxAnyButton::GTKReleased()
132 void wxAnyButton::GTKOnFocus(wxFocusEvent
& event
)
139 wxAnyButton::State
wxAnyButton::GTKGetCurrentState() const
141 if ( !IsThisEnabled() )
142 return m_bitmaps
[State_Disabled
].IsOk() ? State_Disabled
: State_Normal
;
144 if ( m_isPressed
&& m_bitmaps
[State_Pressed
].IsOk() )
145 return State_Pressed
;
147 if ( m_isCurrent
&& m_bitmaps
[State_Current
].IsOk() )
148 return State_Current
;
150 if ( HasFocus() && m_bitmaps
[State_Focused
].IsOk() )
151 return State_Focused
;
156 void wxAnyButton::GTKUpdateBitmap()
158 // if we don't show bitmaps at all, there is nothing to update
159 if ( m_bitmaps
[State_Normal
].IsOk() )
161 // if we do show them, this will return a state for which we do have a
163 State state
= GTKGetCurrentState();
165 GTKDoShowBitmap(m_bitmaps
[state
]);
169 void wxAnyButton::GTKDoShowBitmap(const wxBitmap
& bitmap
)
171 wxASSERT_MSG( bitmap
.IsOk(), "invalid bitmap" );
174 if ( DontShowLabel() )
176 image
= gtk_bin_get_child(GTK_BIN(m_widget
));
178 else // have both label and bitmap
181 if ( !gtk_check_version(2,6,0) )
183 image
= gtk_button_get_image(GTK_BUTTON(m_widget
));
186 #endif // __WXGTK26__
188 // buttons with both label and bitmap are only supported with GTK+
191 // it shouldn't be difficult to implement them ourselves for the
192 // previous GTK+ versions by stuffing a container with a label and
193 // an image inside GtkButton but there doesn't seem to be much
194 // point in doing this for ancient GTK+ versions
199 wxCHECK_RET( image
&& GTK_IS_IMAGE(image
), "must have image widget" );
201 gtk_image_set_from_pixbuf(GTK_IMAGE(image
), bitmap
.GetPixbuf());
204 wxBitmap
wxAnyButton::DoGetBitmap(State which
) const
206 return m_bitmaps
[which
];
209 void wxAnyButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
214 if ( DontShowLabel() )
216 // we only have the bitmap in this button, never remove it but
217 // do invalidate the best size when the bitmap (and presumably
219 InvalidateBestSize();
222 // normal image is special: setting it enables images for the button and
223 // resetting it to nothing disables all of them
224 else if ( !gtk_check_version(2,6,0) )
226 GtkWidget
*image
= gtk_button_get_image(GTK_BUTTON(m_widget
));
227 if ( image
&& !bitmap
.IsOk() )
229 gtk_container_remove(GTK_CONTAINER(m_widget
), image
);
231 else if ( !image
&& bitmap
.IsOk() )
233 image
= gtk_image_new();
234 gtk_button_set_image(GTK_BUTTON(m_widget
), image
);
236 else // image presence or absence didn't change
238 // don't invalidate best size below
242 InvalidateBestSize();
250 if ( !m_bitmaps
[which
].IsOk() )
252 // we need to install the callbacks to be notified about
253 // the button pressed state change
258 G_CALLBACK(wxgtk_button_press_callback
),
266 G_CALLBACK(wxgtk_button_released_callback
),
271 else // no valid bitmap
273 if ( m_bitmaps
[which
].IsOk() )
275 // we don't need to be notified about the button pressed
276 // state changes any more
277 g_signal_handlers_disconnect_by_func
280 (gpointer
)wxgtk_button_press_callback
,
284 g_signal_handlers_disconnect_by_func
287 (gpointer
)wxgtk_button_released_callback
,
291 // also make sure we don't remain stuck in pressed state
302 // the logic here is the same as above for State_Pressed: we need
303 // to connect the handlers if we must be notified about the changes
304 // in the button current state and we disconnect them when/if we
305 // don't need them any more
308 if ( !m_bitmaps
[which
].IsOk() )
314 G_CALLBACK(wxgtk_button_enter_callback
),
322 G_CALLBACK(wxgtk_button_leave_callback
),
327 else // no valid bitmap
329 if ( m_bitmaps
[which
].IsOk() )
331 g_signal_handlers_disconnect_by_func
334 (gpointer
)wxgtk_button_enter_callback
,
338 g_signal_handlers_disconnect_by_func
341 (gpointer
)wxgtk_button_leave_callback
,
357 Connect(wxEVT_SET_FOCUS
,
358 wxFocusEventHandler(wxAnyButton::GTKOnFocus
));
359 Connect(wxEVT_KILL_FOCUS
,
360 wxFocusEventHandler(wxAnyButton::GTKOnFocus
));
362 else // no valid focused bitmap
364 Disconnect(wxEVT_SET_FOCUS
,
365 wxFocusEventHandler(wxAnyButton::GTKOnFocus
));
366 Disconnect(wxEVT_KILL_FOCUS
,
367 wxFocusEventHandler(wxAnyButton::GTKOnFocus
));
372 // no callbacks to connect/disconnect
376 m_bitmaps
[which
] = bitmap
;
378 // update the bitmap immediately if necessary, otherwise it will be done
379 // when the bitmap for the corresponding state is needed the next time by
381 if ( bitmap
.IsOk() && which
== GTKGetCurrentState() )
383 GTKDoShowBitmap(bitmap
);
387 void wxAnyButton::DoSetBitmapPosition(wxDirection dir
)
390 if ( !gtk_check_version(2,10,0) )
392 GtkPositionType gtkpos
;
396 wxFAIL_MSG( "invalid position" );
400 gtkpos
= GTK_POS_LEFT
;
404 gtkpos
= GTK_POS_RIGHT
;
408 gtkpos
= GTK_POS_TOP
;
412 gtkpos
= GTK_POS_BOTTOM
;
416 gtk_button_set_image_position(GTK_BUTTON(m_widget
), gtkpos
);
417 InvalidateBestSize();
422 #endif // wxHAS_ANY_BUTTON