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
130 useLabel
= !(style
& wxBU_NOTEXT
) && (!label
.empty() || wxIsStockID(id
));
133 m_widget
= gtk_button_new_with_mnemonic("");
135 else // no label, suppose we will have a bitmap
137 m_widget
= gtk_button_new();
139 GtkWidget
*image
= gtk_image_new();
140 gtk_widget_show(image
);
141 gtk_container_add(GTK_CONTAINER(m_widget
), image
);
144 g_object_ref(m_widget
);
146 float x_alignment
= 0.5;
147 if (HasFlag(wxBU_LEFT
))
149 else if (HasFlag(wxBU_RIGHT
))
152 float y_alignment
= 0.5;
153 if (HasFlag(wxBU_TOP
))
155 else if (HasFlag(wxBU_BOTTOM
))
158 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
163 if (style
& wxNO_BORDER
)
164 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
166 g_signal_connect_after (m_widget
, "clicked",
167 G_CALLBACK (wxgtk_button_clicked_callback
),
170 g_signal_connect_after (m_widget
, "style_set",
171 G_CALLBACK (wxgtk_button_style_set_callback
),
174 m_parent
->DoAddChild( this );
182 wxWindow
*wxButton::SetDefault()
184 wxWindow
*oldDefault
= wxButtonBase::SetDefault();
186 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
187 gtk_widget_grab_default( m_widget
);
189 // resize for default border
190 wxgtk_button_style_set_callback( m_widget
, NULL
, this );
196 wxSize
wxButtonBase::GetDefaultSize()
198 static wxSize size
= wxDefaultSize
;
199 if (size
== wxDefaultSize
)
201 // NB: Default size of buttons should be same as size of stock
202 // buttons as used in most GTK+ apps. Unfortunately it's a little
203 // tricky to obtain this size: stock button's size may be smaller
204 // than size of button in GtkButtonBox and vice versa,
205 // GtkButtonBox's minimal button size may be smaller than stock
206 // button's size. We have to retrieve both values and combine them.
208 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
209 GtkWidget
*box
= gtk_hbutton_box_new();
210 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
211 gtk_container_add(GTK_CONTAINER(box
), btn
);
212 gtk_container_add(GTK_CONTAINER(wnd
), box
);
214 gtk_widget_size_request(btn
, &req
);
216 gint minwidth
, minheight
;
217 gtk_widget_style_get(box
,
218 "child-min-width", &minwidth
,
219 "child-min-height", &minheight
,
222 size
.x
= wxMax(minwidth
, req
.width
);
223 size
.y
= wxMax(minheight
, req
.height
);
225 gtk_widget_destroy(wnd
);
230 void wxButton::SetLabel( const wxString
&lbl
)
232 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
236 if (label
.empty() && wxIsStockID(m_windowId
))
237 label
= wxGetStockLabel(m_windowId
);
239 wxControl::SetLabel(label
);
241 // don't use label if it was explicitly disabled
242 if ( HasFlag(wxBU_NOTEXT
) )
245 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
247 const char *stock
= wxGetStockGtkID(m_windowId
);
250 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
251 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
256 // this call is necessary if the button had been initially created without
257 // a (text) label -- then we didn't use gtk_button_new_with_mnemonic() and
258 // so "use-underline" GtkButton property remained unset
259 gtk_button_set_use_underline(GTK_BUTTON(m_widget
), TRUE
);
260 const wxString labelGTK
= GTKConvertMnemonics(label
);
261 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
262 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
264 GTKApplyWidgetStyle( false );
267 bool wxButton::Enable( bool enable
)
269 if (!base_type::Enable(enable
))
272 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
282 GdkWindow
*wxButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
284 return GTK_BUTTON(m_widget
)->event_window
;
287 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
289 gtk_widget_modify_style(m_widget
, style
);
290 GtkWidget
*child
= GTK_BIN(m_widget
)->child
;
291 gtk_widget_modify_style(child
, style
);
293 // for buttons with images, the path to the label is (at least in 2.12)
294 // GtkButton -> GtkAlignment -> GtkHBox -> GtkLabel
295 if ( GTK_IS_ALIGNMENT(child
) )
297 GtkWidget
*box
= GTK_BIN(child
)->child
;
298 if ( GTK_IS_BOX(box
) )
300 for (GList
* item
= GTK_BOX(box
)->children
; item
; item
= item
->next
)
302 GtkBoxChild
* boxChild
= static_cast<GtkBoxChild
*>(item
->data
);
303 gtk_widget_modify_style(boxChild
->widget
, style
);
309 wxSize
wxButton::DoGetBestSize() const
311 // the default button in wxGTK is bigger than the other ones because of an
312 // extra border around it, but we don't want to take it into account in
313 // our size calculations (otherwise the result is visually ugly), so
314 // always return the size of non default button from here
315 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
318 // temporarily unset default flag
319 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
322 wxSize
ret( wxControl::DoGetBestSize() );
327 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
330 if (!HasFlag(wxBU_EXACTFIT
))
332 wxSize defaultSize
= GetDefaultSize();
333 if (ret
.x
< defaultSize
.x
)
334 ret
.x
= defaultSize
.x
;
335 if (ret
.y
< defaultSize
.y
)
336 ret
.y
= defaultSize
.y
;
345 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
347 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
350 // ----------------------------------------------------------------------------
352 // ----------------------------------------------------------------------------
354 void wxButton::GTKMouseEnters()
361 void wxButton::GTKMouseLeaves()
368 void wxButton::GTKPressed()
375 void wxButton::GTKReleased()
382 void wxButton::GTKOnFocus(wxFocusEvent
& event
)
389 wxButton::State
wxButton::GTKGetCurrentState() const
391 if ( !IsThisEnabled() )
392 return m_bitmaps
[State_Disabled
].IsOk() ? State_Disabled
: State_Normal
;
394 if ( m_isPressed
&& m_bitmaps
[State_Pressed
].IsOk() )
395 return State_Pressed
;
397 if ( m_isCurrent
&& m_bitmaps
[State_Current
].IsOk() )
398 return State_Current
;
400 if ( HasFocus() && m_bitmaps
[State_Focused
].IsOk() )
401 return State_Focused
;
406 void wxButton::GTKUpdateBitmap()
408 // if we don't show bitmaps at all, there is nothing to update
409 if ( m_bitmaps
[State_Normal
].IsOk() )
411 // if we do show them, this will return a state for which we do have a
413 State state
= GTKGetCurrentState();
415 GTKDoShowBitmap(m_bitmaps
[state
]);
419 void wxButton::GTKDoShowBitmap(const wxBitmap
& bitmap
)
421 wxASSERT_MSG( bitmap
.IsOk(), "invalid bitmap" );
424 if ( DontShowLabel() )
426 image
= GTK_BIN(m_widget
)->child
;
428 else // have both label and bitmap
431 if ( !gtk_check_version(2,6,0) )
433 image
= gtk_button_get_image(GTK_BUTTON(m_widget
));
436 #endif // __WXGTK26__
438 // buttons with both label and bitmap are only supported with GTK+
441 // it shouldn't be difficult to implement them ourselves for the
442 // previous GTK+ versions by stuffing a container with a label and
443 // an image inside GtkButton but there doesn't seem to be much
444 // point in doing this for ancient GTK+ versions
449 wxCHECK_RET( image
&& GTK_IS_IMAGE(image
), "must have image widget" );
451 gtk_image_set_from_pixbuf(GTK_IMAGE(image
), bitmap
.GetPixbuf());
454 wxBitmap
wxButton::DoGetBitmap(State which
) const
456 return m_bitmaps
[which
];
459 void wxButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
464 if ( DontShowLabel() )
466 // we only have the bitmap in this button, never remove it but
467 // do invalidate the best size when the bitmap (and presumably
469 InvalidateBestSize();
472 // normal image is special: setting it enables images for the button and
473 // resetting it to nothing disables all of them
474 else if ( !gtk_check_version(2,6,0) )
476 GtkWidget
*image
= gtk_button_get_image(GTK_BUTTON(m_widget
));
477 if ( image
&& !bitmap
.IsOk() )
479 gtk_container_remove(GTK_CONTAINER(m_widget
), image
);
481 else if ( !image
&& bitmap
.IsOk() )
483 image
= gtk_image_new();
484 gtk_button_set_image(GTK_BUTTON(m_widget
), image
);
486 else // image presence or absence didn't change
488 // don't invalidate best size below
492 InvalidateBestSize();
500 if ( !m_bitmaps
[which
].IsOk() )
502 // we need to install the callbacks to be notified about
503 // the button pressed state change
508 G_CALLBACK(wxgtk_button_press_callback
),
516 G_CALLBACK(wxgtk_button_released_callback
),
521 else // no valid bitmap
523 if ( m_bitmaps
[which
].IsOk() )
525 // we don't need to be notified about the button pressed
526 // state changes any more
527 g_signal_handlers_disconnect_by_func
530 (gpointer
)wxgtk_button_press_callback
,
534 g_signal_handlers_disconnect_by_func
537 (gpointer
)wxgtk_button_released_callback
,
541 // also make sure we don't remain stuck in pressed state
552 // the logic here is the same as above for State_Pressed: we need
553 // to connect the handlers if we must be notified about the changes
554 // in the button current state and we disconnect them when/if we
555 // don't need them any more
558 if ( !m_bitmaps
[which
].IsOk() )
564 G_CALLBACK(wxgtk_button_enter_callback
),
572 G_CALLBACK(wxgtk_button_leave_callback
),
577 else // no valid bitmap
579 if ( m_bitmaps
[which
].IsOk() )
581 g_signal_handlers_disconnect_by_func
584 (gpointer
)wxgtk_button_enter_callback
,
588 g_signal_handlers_disconnect_by_func
591 (gpointer
)wxgtk_button_leave_callback
,
607 Connect(wxEVT_SET_FOCUS
,
608 wxFocusEventHandler(wxButton::GTKOnFocus
));
609 Connect(wxEVT_KILL_FOCUS
,
610 wxFocusEventHandler(wxButton::GTKOnFocus
));
612 else // no valid focused bitmap
614 Disconnect(wxEVT_SET_FOCUS
,
615 wxFocusEventHandler(wxButton::GTKOnFocus
));
616 Disconnect(wxEVT_KILL_FOCUS
,
617 wxFocusEventHandler(wxButton::GTKOnFocus
));
622 // no callbacks to connect/disconnect
626 m_bitmaps
[which
] = bitmap
;
628 // update the bitmap immediately if necessary, otherwise it will be done
629 // when the bitmap for the corresponding state is needed the next time by
631 if ( bitmap
.IsOk() && which
== GTKGetCurrentState() )
633 GTKDoShowBitmap(bitmap
);
637 void wxButton::DoSetBitmapPosition(wxDirection dir
)
640 if ( !gtk_check_version(2,10,0) )
642 GtkPositionType gtkpos
;
646 wxFAIL_MSG( "invalid position" );
650 gtkpos
= GTK_POS_LEFT
;
654 gtkpos
= GTK_POS_RIGHT
;
658 gtkpos
= GTK_POS_TOP
;
662 gtkpos
= GTK_POS_BOTTOM
;
666 gtk_button_set_image_position(GTK_BUTTON(m_widget
), gtkpos
);
667 InvalidateBestSize();
672 #endif // wxUSE_BUTTON