1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/button.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
15 #include "wx/button.h"
18 #include "wx/stockitem.h"
21 #include "wx/gtk/private.h"
22 #include "wx/gtk/private/gtk2-compat.h"
23 #include "wx/gtk/private/list.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
33 wxgtk_button_clicked_callback(GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
35 if ( button
->GTKShouldIgnoreEvent() )
38 wxCommandEvent
event(wxEVT_BUTTON
, button
->GetId());
39 event
.SetEventObject(button
);
40 button
->HandleWindowEvent(event
);
43 //-----------------------------------------------------------------------------
44 // "style_set" from m_widget
45 //-----------------------------------------------------------------------------
48 wxgtk_button_style_set_callback(GtkWidget
* widget
, GtkStyle
*, wxButton
* win
)
50 /* the default button has a border around it */
51 wxWindow
* parent
= win
->GetParent();
52 if (parent
&& parent
->m_wxwindow
&& gtk_widget_get_can_default(widget
))
54 GtkBorder
* border
= NULL
;
55 gtk_widget_style_get(widget
, "default_border", &border
, NULL
);
59 win
->m_x
- border
->left
,
60 win
->m_y
- border
->top
,
61 win
->m_width
+ border
->left
+ border
->right
,
62 win
->m_height
+ border
->top
+ border
->bottom
);
63 gtk_border_free(border
);
70 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
74 bool wxButton::Create(wxWindow
*parent
,
76 const wxString
&label
,
80 const wxValidator
& validator
,
83 if (!PreCreation( parent
, pos
, size
) ||
84 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
86 wxFAIL_MSG( wxT("wxButton creation failed") );
90 // create either a standard button with text label (which may still contain
91 // an image under GTK+ 2.6+) or a bitmap-only button if we don't have any
94 useLabel
= !(style
& wxBU_NOTEXT
) && (!label
.empty() || wxIsStockID(id
));
97 m_widget
= gtk_button_new_with_mnemonic("");
99 else // no label, suppose we will have a bitmap
101 m_widget
= gtk_button_new();
103 GtkWidget
*image
= gtk_image_new();
104 gtk_widget_show(image
);
105 gtk_container_add(GTK_CONTAINER(m_widget
), image
);
108 g_object_ref(m_widget
);
110 float x_alignment
= 0.5;
111 if (HasFlag(wxBU_LEFT
))
113 else if (HasFlag(wxBU_RIGHT
))
116 float y_alignment
= 0.5;
117 if (HasFlag(wxBU_TOP
))
119 else if (HasFlag(wxBU_BOTTOM
))
122 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
127 if (style
& wxNO_BORDER
)
128 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
130 g_signal_connect_after (m_widget
, "clicked",
131 G_CALLBACK (wxgtk_button_clicked_callback
),
134 g_signal_connect_after (m_widget
, "style_set",
135 G_CALLBACK (wxgtk_button_style_set_callback
),
138 m_parent
->DoAddChild( this );
146 wxWindow
*wxButton::SetDefault()
148 wxWindow
*oldDefault
= wxButtonBase::SetDefault();
150 gtk_widget_set_can_default(m_widget
, TRUE
);
151 gtk_widget_grab_default( m_widget
);
153 // resize for default border
154 wxgtk_button_style_set_callback( m_widget
, NULL
, this );
160 wxSize
wxButtonBase::GetDefaultSize()
162 static wxSize size
= wxDefaultSize
;
163 if (size
== wxDefaultSize
)
165 // NB: Default size of buttons should be same as size of stock
166 // buttons as used in most GTK+ apps. Unfortunately it's a little
167 // tricky to obtain this size: stock button's size may be smaller
168 // than size of button in GtkButtonBox and vice versa,
169 // GtkButtonBox's minimal button size may be smaller than stock
170 // button's size. We have to retrieve both values and combine them.
172 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
173 GtkWidget
*box
= gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL
);
174 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
175 gtk_container_add(GTK_CONTAINER(box
), btn
);
176 gtk_container_add(GTK_CONTAINER(wnd
), box
);
178 gtk_widget_get_preferred_size(btn
, NULL
, &req
);
180 gint minwidth
, minheight
;
181 gtk_widget_style_get(box
,
182 "child-min-width", &minwidth
,
183 "child-min-height", &minheight
,
186 size
.x
= wxMax(minwidth
, req
.width
);
187 size
.y
= wxMax(minheight
, req
.height
);
189 gtk_widget_destroy(wnd
);
194 void wxButton::SetLabel( const wxString
&lbl
)
196 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
200 if (label
.empty() && wxIsStockID(m_windowId
))
201 label
= wxGetStockLabel(m_windowId
);
203 wxAnyButton::SetLabel(label
);
205 // don't use label if it was explicitly disabled
206 if ( HasFlag(wxBU_NOTEXT
) )
209 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
211 const char *stock
= wxGetStockGtkID(m_windowId
);
214 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
215 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
220 // this call is necessary if the button had been initially created without
221 // a (text) label -- then we didn't use gtk_button_new_with_mnemonic() and
222 // so "use-underline" GtkButton property remained unset
223 gtk_button_set_use_underline(GTK_BUTTON(m_widget
), TRUE
);
224 const wxString labelGTK
= GTKConvertMnemonics(label
);
225 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
226 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
228 GTKApplyWidgetStyle( false );
232 bool wxButton::DoSetLabelMarkup(const wxString
& markup
)
234 wxCHECK_MSG( m_widget
!= NULL
, false, "invalid button" );
236 const wxString stripped
= RemoveMarkup(markup
);
237 if ( stripped
.empty() && !markup
.empty() )
240 wxControl::SetLabel(stripped
);
242 GtkLabel
* const label
= GTKGetLabel();
243 wxCHECK_MSG( label
, false, "no label in this button?" );
245 GTKSetLabelWithMarkupForLabel(label
, markup
);
250 GtkLabel
*wxButton::GTKGetLabel() const
252 GtkWidget
* child
= gtk_bin_get_child(GTK_BIN(m_widget
));
253 if ( GTK_IS_ALIGNMENT(child
) )
255 GtkWidget
* box
= gtk_bin_get_child(GTK_BIN(child
));
256 GtkLabel
* label
= NULL
;
257 wxGtkList
list(gtk_container_get_children(GTK_CONTAINER(box
)));
258 for (GList
* item
= list
; item
; item
= item
->next
)
260 if (GTK_IS_LABEL(item
->data
))
261 label
= GTK_LABEL(item
->data
);
267 return GTK_LABEL(child
);
269 #endif // wxUSE_MARKUP
271 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
273 GTKApplyStyle(m_widget
, style
);
274 GtkWidget
* child
= gtk_bin_get_child(GTK_BIN(m_widget
));
275 GTKApplyStyle(child
, style
);
277 // for buttons with images, the path to the label is (at least in 2.12)
278 // GtkButton -> GtkAlignment -> GtkHBox -> GtkLabel
279 if ( GTK_IS_ALIGNMENT(child
) )
281 GtkWidget
* box
= gtk_bin_get_child(GTK_BIN(child
));
282 if ( GTK_IS_BOX(box
) )
284 wxGtkList
list(gtk_container_get_children(GTK_CONTAINER(box
)));
285 for (GList
* item
= list
; item
; item
= item
->next
)
287 GTKApplyStyle(GTK_WIDGET(item
->data
), style
);
293 wxSize
wxButton::DoGetBestSize() const
295 // the default button in wxGTK is bigger than the other ones because of an
296 // extra border around it, but we don't want to take it into account in
297 // our size calculations (otherwise the result is visually ugly), so
298 // always return the size of non default button from here
299 const bool isDefault
= gtk_widget_has_default(m_widget
) != 0;
302 // temporarily unset default flag
303 gtk_widget_set_can_default(m_widget
, FALSE
);
306 wxSize
ret( wxAnyButton::DoGetBestSize() );
311 gtk_widget_set_can_default(m_widget
, TRUE
);
314 if (!HasFlag(wxBU_EXACTFIT
))
316 wxSize defaultSize
= GetDefaultSize();
317 if (ret
.x
< defaultSize
.x
)
318 ret
.x
= defaultSize
.x
;
319 if (ret
.y
< defaultSize
.y
)
320 ret
.y
= defaultSize
.y
;
329 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
331 return GetDefaultAttributesFromGTKWidget(gtk_button_new());
334 #endif // wxUSE_BUTTON