1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/button.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
16 #include "wx/button.h"
19 #include "wx/stockitem.h"
21 #include "wx/gtk/private.h"
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
31 wxgtk_button_clicked_callback(GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
33 if ( button
->GTKShouldIgnoreEvent() )
36 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
37 event
.SetEventObject(button
);
38 button
->HandleWindowEvent(event
);
42 wxgtk_button_enter_callback(GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
44 if ( button
->GTKShouldIgnoreEvent() )
47 button
->GTKMouseEnters();
51 wxgtk_button_leave_callback(GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
53 if ( button
->GTKShouldIgnoreEvent() )
56 button
->GTKMouseLeaves();
60 wxgtk_button_press_callback(GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
62 if ( button
->GTKShouldIgnoreEvent() )
69 wxgtk_button_released_callback(GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
71 if ( button
->GTKShouldIgnoreEvent() )
74 button
->GTKReleased();
77 //-----------------------------------------------------------------------------
78 // "style_set" from m_widget
79 //-----------------------------------------------------------------------------
82 wxgtk_button_style_set_callback(GtkWidget
* widget
, GtkStyle
*, wxButton
* win
)
84 /* the default button has a border around it */
85 wxWindow
* parent
= win
->GetParent();
86 if (parent
&& parent
->m_wxwindow
&& GTK_WIDGET_CAN_DEFAULT(widget
))
88 GtkBorder
* border
= NULL
;
89 gtk_widget_style_get(widget
, "default_border", &border
, NULL
);
93 win
->m_x
- border
->left
,
94 win
->m_y
- border
->top
,
95 win
->m_width
+ border
->left
+ border
->right
,
96 win
->m_height
+ border
->top
+ border
->bottom
);
97 gtk_border_free(border
);
104 //-----------------------------------------------------------------------------
106 //-----------------------------------------------------------------------------
108 IMPLEMENT_DYNAMIC_CLASS(wxButton
,wxControl
)
110 bool wxButton::Create(wxWindow
*parent
,
112 const wxString
&label
,
116 const wxValidator
& validator
,
117 const wxString
& name
)
119 if (!PreCreation( parent
, pos
, size
) ||
120 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
122 wxFAIL_MSG( wxT("wxButton creation failed") );
126 // create either a standard button with text label (which may still contain
127 // an image under GTK+ 2.6+) or a bitmap-only button if we don't have any
129 const bool useLabel
= !label
.empty() || wxIsStockID(id
);
132 m_widget
= gtk_button_new_with_mnemonic("");
134 else // no label, suppose we will have a bitmap
136 m_widget
= gtk_button_new();
138 GtkWidget
*image
= gtk_image_new();
139 gtk_widget_show(image
);
140 gtk_container_add(GTK_CONTAINER(m_widget
), image
);
143 g_object_ref(m_widget
);
145 float x_alignment
= 0.5;
146 if (HasFlag(wxBU_LEFT
))
148 else if (HasFlag(wxBU_RIGHT
))
151 float y_alignment
= 0.5;
152 if (HasFlag(wxBU_TOP
))
154 else if (HasFlag(wxBU_BOTTOM
))
157 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
162 if (style
& wxNO_BORDER
)
163 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
165 g_signal_connect_after (m_widget
, "clicked",
166 G_CALLBACK (wxgtk_button_clicked_callback
),
169 g_signal_connect_after (m_widget
, "style_set",
170 G_CALLBACK (wxgtk_button_style_set_callback
),
173 m_parent
->DoAddChild( this );
181 wxWindow
*wxButton::SetDefault()
183 wxWindow
*oldDefault
= wxButtonBase::SetDefault();
185 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
186 gtk_widget_grab_default( m_widget
);
188 // resize for default border
189 wxgtk_button_style_set_callback( m_widget
, NULL
, this );
195 wxSize
wxButtonBase::GetDefaultSize()
197 static wxSize size
= wxDefaultSize
;
198 if (size
== wxDefaultSize
)
200 // NB: Default size of buttons should be same as size of stock
201 // buttons as used in most GTK+ apps. Unfortunately it's a little
202 // tricky to obtain this size: stock button's size may be smaller
203 // than size of button in GtkButtonBox and vice versa,
204 // GtkButtonBox's minimal button size may be smaller than stock
205 // button's size. We have to retrieve both values and combine them.
207 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
208 GtkWidget
*box
= gtk_hbutton_box_new();
209 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
210 gtk_container_add(GTK_CONTAINER(box
), btn
);
211 gtk_container_add(GTK_CONTAINER(wnd
), box
);
213 gtk_widget_size_request(btn
, &req
);
215 gint minwidth
, minheight
;
216 gtk_widget_style_get(box
,
217 "child-min-width", &minwidth
,
218 "child-min-height", &minheight
,
221 size
.x
= wxMax(minwidth
, req
.width
);
222 size
.y
= wxMax(minheight
, req
.height
);
224 gtk_widget_destroy(wnd
);
229 void wxButton::SetLabel( const wxString
&lbl
)
231 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
235 if (label
.empty() && wxIsStockID(m_windowId
))
236 label
= wxGetStockLabel(m_windowId
);
238 wxControl::SetLabel(label
);
240 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
242 const char *stock
= wxGetStockGtkID(m_windowId
);
245 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
246 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
251 const wxString labelGTK
= GTKConvertMnemonics(label
);
252 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
253 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
255 GTKApplyWidgetStyle( false );
258 bool wxButton::Enable( bool enable
)
260 bool isEnabled
= IsEnabled();
262 if ( !wxControl::Enable( enable
) )
265 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
267 if (!isEnabled
&& enable
)
277 GdkWindow
*wxButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
279 return GTK_BUTTON(m_widget
)->event_window
;
282 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
284 gtk_widget_modify_style(m_widget
, style
);
285 GtkWidget
*child
= GTK_BIN(m_widget
)->child
;
286 gtk_widget_modify_style(child
, style
);
288 // for buttons with images, the path to the label is (at least in 2.12)
289 // GtkButton -> GtkAlignment -> GtkHBox -> GtkLabel
290 if ( GTK_IS_ALIGNMENT(child
) )
292 GtkWidget
*box
= GTK_BIN(child
)->child
;
293 if ( GTK_IS_BOX(box
) )
295 for (GList
* item
= GTK_BOX(box
)->children
; item
; item
= item
->next
)
297 GtkBoxChild
* boxChild
= static_cast<GtkBoxChild
*>(item
->data
);
298 gtk_widget_modify_style(boxChild
->widget
, style
);
304 wxSize
wxButton::DoGetBestSize() const
306 // the default button in wxGTK is bigger than the other ones because of an
307 // extra border around it, but we don't want to take it into account in
308 // our size calculations (otherwise the result is visually ugly), so
309 // always return the size of non default button from here
310 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
313 // temporarily unset default flag
314 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
317 wxSize
ret( wxControl::DoGetBestSize() );
322 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
325 if (!HasFlag(wxBU_EXACTFIT
))
327 wxSize defaultSize
= GetDefaultSize();
328 if (ret
.x
< defaultSize
.x
)
329 ret
.x
= defaultSize
.x
;
330 if (ret
.y
< defaultSize
.y
)
331 ret
.y
= defaultSize
.y
;
340 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
342 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
345 // ----------------------------------------------------------------------------
347 // ----------------------------------------------------------------------------
349 void wxButton::GTKMouseEnters()
356 void wxButton::GTKMouseLeaves()
363 void wxButton::GTKPressed()
370 void wxButton::GTKReleased()
377 void wxButton::GTKOnFocus(wxFocusEvent
& event
)
384 wxButton::State
wxButton::GTKGetCurrentState() const
386 if ( !IsThisEnabled() )
387 return m_bitmaps
[State_Disabled
].IsOk() ? State_Disabled
: State_Normal
;
389 if ( m_isPressed
&& m_bitmaps
[State_Pressed
].IsOk() )
390 return State_Pressed
;
392 if ( m_isCurrent
&& m_bitmaps
[State_Current
].IsOk() )
393 return State_Current
;
395 if ( HasFocus() && m_bitmaps
[State_Focused
].IsOk() )
396 return State_Focused
;
401 void wxButton::GTKUpdateBitmap()
403 State state
= GTKGetCurrentState();
405 GTKDoShowBitmap(m_bitmaps
[state
]);
408 void wxButton::GTKDoShowBitmap(const wxBitmap
& bitmap
)
410 wxASSERT_MSG( bitmap
.IsOk(), "invalid bitmap" );
413 if ( GetLabel().empty() )
415 image
= GTK_BIN(m_widget
)->child
;
417 else // have both label and bitmap
420 if ( !gtk_check_version(2,6,0) )
422 image
= gtk_button_get_image(GTK_BUTTON(m_widget
));
425 #endif // __WXGTK26__
427 // buttons with both label and bitmap are only supported with GTK+
430 // it shouldn't be difficult to implement them ourselves for the
431 // previous GTK+ versions by stuffing a container with a label and
432 // an image inside GtkButton but there doesn't seem to be much
433 // point in doing this for ancient GTK+ versions
438 wxCHECK_RET( image
&& GTK_IS_IMAGE(image
), "must have image widget" );
440 gtk_image_set_from_pixbuf(GTK_IMAGE(image
), bitmap
.GetPixbuf());
443 wxBitmap
wxButton::DoGetBitmap(State which
) const
445 return m_bitmaps
[which
];
448 void wxButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
453 if ( GetLabel().empty() )
455 // we only have the bitmap in this button, never remove it but
456 // do invalidate the best size when the bitmap (and presumably
458 InvalidateBestSize();
461 // normal image is special: setting it enables images for the button and
462 // resetting it to nothing disables all of them
463 else if ( !gtk_check_version(2,6,0) )
465 GtkWidget
*image
= gtk_button_get_image(GTK_BUTTON(m_widget
));
466 if ( image
&& !bitmap
.IsOk() )
468 gtk_container_remove(GTK_CONTAINER(m_widget
), image
);
470 else if ( !image
&& bitmap
.IsOk() )
472 image
= gtk_image_new();
473 gtk_button_set_image(GTK_BUTTON(m_widget
), image
);
475 else // image presence or absence didn't change
477 // don't invalidate best size below
481 InvalidateBestSize();
489 if ( !m_bitmaps
[which
].IsOk() )
491 // we need to install the callbacks to be notified about
492 // the button pressed state change
497 G_CALLBACK(wxgtk_button_press_callback
),
505 G_CALLBACK(wxgtk_button_released_callback
),
510 else // no valid bitmap
512 if ( m_bitmaps
[which
].IsOk() )
514 // we don't need to be notified about the button pressed
515 // state changes any more
516 g_signal_handlers_disconnect_by_func
519 (gpointer
)wxgtk_button_press_callback
,
523 g_signal_handlers_disconnect_by_func
526 (gpointer
)wxgtk_button_released_callback
,
530 // also make sure we don't remain stuck in pressed state
541 // the logic here is the same as above for State_Pressed: we need
542 // to connect the handlers if we must be notified about the changes
543 // in the button current state and we disconnect them when/if we
544 // don't need them any more
547 if ( !m_bitmaps
[which
].IsOk() )
553 G_CALLBACK(wxgtk_button_enter_callback
),
561 G_CALLBACK(wxgtk_button_leave_callback
),
566 else // no valid bitmap
568 if ( m_bitmaps
[which
].IsOk() )
570 g_signal_handlers_disconnect_by_func
573 (gpointer
)wxgtk_button_enter_callback
,
577 g_signal_handlers_disconnect_by_func
580 (gpointer
)wxgtk_button_leave_callback
,
596 Connect(wxEVT_SET_FOCUS
,
597 wxFocusEventHandler(wxButton::GTKOnFocus
));
598 Connect(wxEVT_KILL_FOCUS
,
599 wxFocusEventHandler(wxButton::GTKOnFocus
));
601 else // no valid focused bitmap
603 Disconnect(wxEVT_SET_FOCUS
,
604 wxFocusEventHandler(wxButton::GTKOnFocus
));
605 Disconnect(wxEVT_KILL_FOCUS
,
606 wxFocusEventHandler(wxButton::GTKOnFocus
));
611 // no callbacks to connect/disconnect
615 m_bitmaps
[which
] = bitmap
;
617 // update the bitmap immediately if necessary, otherwise it will be done
618 // when the bitmap for the corresponding state is needed the next time by
620 if ( bitmap
.IsOk() && which
== GTKGetCurrentState() )
622 GTKDoShowBitmap(bitmap
);
626 void wxButton::DoSetBitmapPosition(wxDirection dir
)
629 if ( !gtk_check_version(2,10,0) )
631 GtkPositionType gtkpos
;
635 wxFAIL_MSG( "invalid position" );
639 gtkpos
= GTK_POS_LEFT
;
643 gtkpos
= GTK_POS_RIGHT
;
647 gtkpos
= GTK_POS_TOP
;
651 gtkpos
= GTK_POS_BOTTOM
;
655 gtk_button_set_image_position(GTK_BUTTON(m_widget
), gtkpos
);
660 #endif // wxUSE_BUTTON