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 m_widget
= gtk_button_new_with_mnemonic("");
127 g_object_ref(m_widget
);
129 float x_alignment
= 0.5;
130 if (HasFlag(wxBU_LEFT
))
132 else if (HasFlag(wxBU_RIGHT
))
135 float y_alignment
= 0.5;
136 if (HasFlag(wxBU_TOP
))
138 else if (HasFlag(wxBU_BOTTOM
))
141 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
145 if (style
& wxNO_BORDER
)
146 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
148 g_signal_connect_after (m_widget
, "clicked",
149 G_CALLBACK (wxgtk_button_clicked_callback
),
152 g_signal_connect_after (m_widget
, "style_set",
153 G_CALLBACK (wxgtk_button_style_set_callback
),
156 m_parent
->DoAddChild( this );
164 wxWindow
*wxButton::SetDefault()
166 wxWindow
*oldDefault
= wxButtonBase::SetDefault();
168 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
169 gtk_widget_grab_default( m_widget
);
171 // resize for default border
172 wxgtk_button_style_set_callback( m_widget
, NULL
, this );
178 wxSize
wxButtonBase::GetDefaultSize()
180 static wxSize size
= wxDefaultSize
;
181 if (size
== wxDefaultSize
)
183 // NB: Default size of buttons should be same as size of stock
184 // buttons as used in most GTK+ apps. Unfortunately it's a little
185 // tricky to obtain this size: stock button's size may be smaller
186 // than size of button in GtkButtonBox and vice versa,
187 // GtkButtonBox's minimal button size may be smaller than stock
188 // button's size. We have to retrieve both values and combine them.
190 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
191 GtkWidget
*box
= gtk_hbutton_box_new();
192 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
193 gtk_container_add(GTK_CONTAINER(box
), btn
);
194 gtk_container_add(GTK_CONTAINER(wnd
), box
);
196 gtk_widget_size_request(btn
, &req
);
198 gint minwidth
, minheight
;
199 gtk_widget_style_get(box
,
200 "child-min-width", &minwidth
,
201 "child-min-height", &minheight
,
204 size
.x
= wxMax(minwidth
, req
.width
);
205 size
.y
= wxMax(minheight
, req
.height
);
207 gtk_widget_destroy(wnd
);
212 void wxButton::SetLabel( const wxString
&lbl
)
214 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
218 if (label
.empty() && wxIsStockID(m_windowId
))
219 label
= wxGetStockLabel(m_windowId
);
221 wxControl::SetLabel(label
);
223 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
225 const char *stock
= wxGetStockGtkID(m_windowId
);
228 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
229 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
234 const wxString labelGTK
= GTKConvertMnemonics(label
);
235 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
236 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
238 GTKApplyWidgetStyle( false );
241 bool wxButton::Enable( bool enable
)
243 bool isEnabled
= IsEnabled();
245 if ( !wxControl::Enable( enable
) )
248 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
250 if (!isEnabled
&& enable
)
260 GdkWindow
*wxButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
262 return GTK_BUTTON(m_widget
)->event_window
;
265 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
267 gtk_widget_modify_style(m_widget
, style
);
268 GtkWidget
*child
= GTK_BIN(m_widget
)->child
;
269 gtk_widget_modify_style(child
, style
);
271 // for buttons with images, the path to the label is (at least in 2.12)
272 // GtkButton -> GtkAlignment -> GtkHBox -> GtkLabel
273 if ( GTK_IS_ALIGNMENT(child
) )
275 GtkWidget
*box
= GTK_BIN(child
)->child
;
276 if ( GTK_IS_BOX(box
) )
278 for (GList
* item
= GTK_BOX(box
)->children
; item
; item
= item
->next
)
280 GtkBoxChild
* boxChild
= static_cast<GtkBoxChild
*>(item
->data
);
281 gtk_widget_modify_style(boxChild
->widget
, style
);
287 wxSize
wxButton::DoGetBestSize() const
289 // the default button in wxGTK is bigger than the other ones because of an
290 // extra border around it, but we don't want to take it into account in
291 // our size calculations (otherwise the result is visually ugly), so
292 // always return the size of non default button from here
293 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
296 // temporarily unset default flag
297 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
300 wxSize
ret( wxControl::DoGetBestSize() );
305 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
308 if (!HasFlag(wxBU_EXACTFIT
))
310 wxSize defaultSize
= GetDefaultSize();
311 if (ret
.x
< defaultSize
.x
)
312 ret
.x
= defaultSize
.x
;
313 if (ret
.y
< defaultSize
.y
)
314 ret
.y
= defaultSize
.y
;
323 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
325 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
332 void wxButton::GTKMouseEnters()
339 void wxButton::GTKMouseLeaves()
346 void wxButton::GTKPressed()
353 void wxButton::GTKReleased()
360 void wxButton::GTKOnFocus(wxFocusEvent
& event
)
367 wxButton::State
wxButton::GTKGetCurrentState() const
369 if ( !IsThisEnabled() )
370 return m_bitmaps
[State_Disabled
].IsOk() ? State_Disabled
: State_Normal
;
372 if ( m_isPressed
&& m_bitmaps
[State_Pressed
].IsOk() )
373 return State_Pressed
;
375 if ( m_isCurrent
&& m_bitmaps
[State_Current
].IsOk() )
376 return State_Current
;
378 if ( HasFocus() && m_bitmaps
[State_Focused
].IsOk() )
379 return State_Focused
;
384 void wxButton::GTKUpdateBitmap()
386 State state
= GTKGetCurrentState();
388 GTKDoShowBitmap(m_bitmaps
[state
]);
391 void wxButton::GTKDoShowBitmap(const wxBitmap
& bitmap
)
393 wxASSERT_MSG( bitmap
.IsOk(), "invalid bitmap" );
396 if ( !gtk_check_version(2,6,0) )
398 GtkWidget
*image
= gtk_button_get_image(GTK_BUTTON(m_widget
));
399 wxCHECK_RET( image
, "must have image widget" );
401 gtk_image_set_from_pixbuf(GTK_IMAGE(image
), bitmap
.GetPixbuf());
403 #endif // __WXGTK26__
406 wxBitmap
wxButton::DoGetBitmap(State which
) const
408 return m_bitmaps
[which
];
411 void wxButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
417 // normal image is special: setting it enables images for the button and
418 // resetting it to nothing disables all of them
419 if ( !gtk_check_version(2,6,0) )
421 GtkWidget
*image
= gtk_button_get_image(GTK_BUTTON(m_widget
));
422 if ( image
&& !bitmap
.IsOk() )
424 gtk_container_remove(GTK_CONTAINER(m_widget
), image
);
426 else if ( !image
&& bitmap
.IsOk() )
428 image
= gtk_image_new();
429 gtk_button_set_image(GTK_BUTTON(m_widget
), image
);
431 else // image presence or absence didn't change
433 // don't invalidate best size below
437 InvalidateBestSize();
445 if ( !m_bitmaps
[which
].IsOk() )
447 // we need to install the callbacks to be notified about
448 // the button pressed state change
453 G_CALLBACK(wxgtk_button_press_callback
),
461 G_CALLBACK(wxgtk_button_released_callback
),
466 else // no valid bitmap
468 if ( m_bitmaps
[which
].IsOk() )
470 // we don't need to be notified about the button pressed
471 // state changes any more
472 g_signal_handlers_disconnect_by_func
475 (gpointer
)wxgtk_button_press_callback
,
479 g_signal_handlers_disconnect_by_func
482 (gpointer
)wxgtk_button_released_callback
,
486 // also make sure we don't remain stuck in pressed state
497 // the logic here is the same as above for State_Pressed: we need
498 // to connect the handlers if we must be notified about the changes
499 // in the button current state and we disconnect them when/if we
500 // don't need them any more
503 if ( !m_bitmaps
[which
].IsOk() )
509 G_CALLBACK(wxgtk_button_enter_callback
),
517 G_CALLBACK(wxgtk_button_leave_callback
),
522 else // no valid bitmap
524 if ( m_bitmaps
[which
].IsOk() )
526 g_signal_handlers_disconnect_by_func
529 (gpointer
)wxgtk_button_enter_callback
,
533 g_signal_handlers_disconnect_by_func
536 (gpointer
)wxgtk_button_leave_callback
,
552 Connect(wxEVT_SET_FOCUS
,
553 wxFocusEventHandler(wxButton::GTKOnFocus
));
554 Connect(wxEVT_KILL_FOCUS
,
555 wxFocusEventHandler(wxButton::GTKOnFocus
));
557 else // no valid focused bitmap
559 Disconnect(wxEVT_SET_FOCUS
,
560 wxFocusEventHandler(wxButton::GTKOnFocus
));
561 Disconnect(wxEVT_KILL_FOCUS
,
562 wxFocusEventHandler(wxButton::GTKOnFocus
));
567 // no callbacks to connect/disconnect
571 m_bitmaps
[which
] = bitmap
;
573 // update the bitmap immediately if necessary, otherwise it will be done
574 // when the bitmap for the corresponding state is needed the next time by
576 if ( bitmap
.IsOk() && which
== GTKGetCurrentState() )
578 GTKDoShowBitmap(bitmap
);
582 void wxButton::DoSetBitmapPosition(wxDirection dir
)
585 if ( !gtk_check_version(2,10,0) )
587 GtkPositionType gtkpos
;
591 wxFAIL_MSG( "invalid position" );
595 gtkpos
= GTK_POS_LEFT
;
599 gtkpos
= GTK_POS_RIGHT
;
603 gtkpos
= GTK_POS_TOP
;
607 gtkpos
= GTK_POS_BOTTOM
;
611 gtk_button_set_image_position(GTK_BUTTON(m_widget
), gtkpos
);
616 #endif // wxUSE_BUTTON