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"
22 #include "wx/gtk/private.h"
23 #include "wx/gtk/private/gtk2-compat.h"
24 #include "wx/gtk/private/list.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
34 wxgtk_button_clicked_callback(GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
36 if ( button
->GTKShouldIgnoreEvent() )
39 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
40 event
.SetEventObject(button
);
41 button
->HandleWindowEvent(event
);
44 //-----------------------------------------------------------------------------
45 // "style_set" from m_widget
46 //-----------------------------------------------------------------------------
49 wxgtk_button_style_set_callback(GtkWidget
* widget
, GtkStyle
*, wxButton
* win
)
51 /* the default button has a border around it */
52 wxWindow
* parent
= win
->GetParent();
53 if (parent
&& parent
->m_wxwindow
&& gtk_widget_get_can_default(widget
))
55 GtkBorder
* border
= NULL
;
56 gtk_widget_style_get(widget
, "default_border", &border
, NULL
);
60 win
->m_x
- border
->left
,
61 win
->m_y
- border
->top
,
62 win
->m_width
+ border
->left
+ border
->right
,
63 win
->m_height
+ border
->top
+ border
->bottom
);
64 gtk_border_free(border
);
71 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
75 bool wxButton::Create(wxWindow
*parent
,
77 const wxString
&label
,
81 const wxValidator
& validator
,
84 if (!PreCreation( parent
, pos
, size
) ||
85 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
87 wxFAIL_MSG( wxT("wxButton creation failed") );
91 // create either a standard button with text label (which may still contain
92 // an image under GTK+ 2.6+) or a bitmap-only button if we don't have any
95 useLabel
= !(style
& wxBU_NOTEXT
) && (!label
.empty() || wxIsStockID(id
));
98 m_widget
= gtk_button_new_with_mnemonic("");
100 else // no label, suppose we will have a bitmap
102 m_widget
= gtk_button_new();
104 GtkWidget
*image
= gtk_image_new();
105 gtk_widget_show(image
);
106 gtk_container_add(GTK_CONTAINER(m_widget
), image
);
109 g_object_ref(m_widget
);
111 float x_alignment
= 0.5;
112 if (HasFlag(wxBU_LEFT
))
114 else if (HasFlag(wxBU_RIGHT
))
117 float y_alignment
= 0.5;
118 if (HasFlag(wxBU_TOP
))
120 else if (HasFlag(wxBU_BOTTOM
))
123 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
128 if (style
& wxNO_BORDER
)
129 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
131 g_signal_connect_after (m_widget
, "clicked",
132 G_CALLBACK (wxgtk_button_clicked_callback
),
135 g_signal_connect_after (m_widget
, "style_set",
136 G_CALLBACK (wxgtk_button_style_set_callback
),
139 m_parent
->DoAddChild( this );
147 wxWindow
*wxButton::SetDefault()
149 wxWindow
*oldDefault
= wxButtonBase::SetDefault();
151 gtk_widget_set_can_default(m_widget
, TRUE
);
152 gtk_widget_grab_default( m_widget
);
154 // resize for default border
155 wxgtk_button_style_set_callback( m_widget
, NULL
, this );
161 wxSize
wxButtonBase::GetDefaultSize()
163 static wxSize size
= wxDefaultSize
;
164 if (size
== wxDefaultSize
)
166 // NB: Default size of buttons should be same as size of stock
167 // buttons as used in most GTK+ apps. Unfortunately it's a little
168 // tricky to obtain this size: stock button's size may be smaller
169 // than size of button in GtkButtonBox and vice versa,
170 // GtkButtonBox's minimal button size may be smaller than stock
171 // button's size. We have to retrieve both values and combine them.
173 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
174 GtkWidget
*box
= gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL
);
175 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
176 gtk_container_add(GTK_CONTAINER(box
), btn
);
177 gtk_container_add(GTK_CONTAINER(wnd
), box
);
179 gtk_widget_get_preferred_size(btn
, NULL
, &req
);
181 gint minwidth
, minheight
;
182 gtk_widget_style_get(box
,
183 "child-min-width", &minwidth
,
184 "child-min-height", &minheight
,
187 size
.x
= wxMax(minwidth
, req
.width
);
188 size
.y
= wxMax(minheight
, req
.height
);
190 gtk_widget_destroy(wnd
);
195 void wxButton::SetLabel( const wxString
&lbl
)
197 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
201 if (label
.empty() && wxIsStockID(m_windowId
))
202 label
= wxGetStockLabel(m_windowId
);
204 wxAnyButton::SetLabel(label
);
206 // don't use label if it was explicitly disabled
207 if ( HasFlag(wxBU_NOTEXT
) )
210 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
212 const char *stock
= wxGetStockGtkID(m_windowId
);
215 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
216 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
221 // this call is necessary if the button had been initially created without
222 // a (text) label -- then we didn't use gtk_button_new_with_mnemonic() and
223 // so "use-underline" GtkButton property remained unset
224 gtk_button_set_use_underline(GTK_BUTTON(m_widget
), TRUE
);
225 const wxString labelGTK
= GTKConvertMnemonics(label
);
226 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
227 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
229 GTKApplyWidgetStyle( false );
233 bool wxButton::DoSetLabelMarkup(const wxString
& markup
)
235 wxCHECK_MSG( m_widget
!= NULL
, false, "invalid button" );
237 const wxString stripped
= RemoveMarkup(markup
);
238 if ( stripped
.empty() && !markup
.empty() )
241 wxControl::SetLabel(stripped
);
243 GtkLabel
* const label
= GTKGetLabel();
244 wxCHECK_MSG( label
, false, "no label in this button?" );
246 GTKSetLabelWithMarkupForLabel(label
, markup
);
251 GtkLabel
*wxButton::GTKGetLabel() const
253 GtkWidget
* child
= gtk_bin_get_child(GTK_BIN(m_widget
));
254 if ( GTK_IS_ALIGNMENT(child
) )
256 GtkWidget
* box
= gtk_bin_get_child(GTK_BIN(child
));
257 GtkLabel
* label
= NULL
;
258 wxGtkList
list(gtk_container_get_children(GTK_CONTAINER(box
)));
259 for (GList
* item
= list
; item
; item
= item
->next
)
261 if (GTK_IS_LABEL(item
->data
))
262 label
= GTK_LABEL(item
->data
);
268 return GTK_LABEL(child
);
270 #endif // wxUSE_MARKUP
272 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
274 GTKApplyStyle(m_widget
, style
);
275 GtkWidget
* child
= gtk_bin_get_child(GTK_BIN(m_widget
));
276 GTKApplyStyle(child
, style
);
278 // for buttons with images, the path to the label is (at least in 2.12)
279 // GtkButton -> GtkAlignment -> GtkHBox -> GtkLabel
280 if ( GTK_IS_ALIGNMENT(child
) )
282 GtkWidget
* box
= gtk_bin_get_child(GTK_BIN(child
));
283 if ( GTK_IS_BOX(box
) )
285 wxGtkList
list(gtk_container_get_children(GTK_CONTAINER(box
)));
286 for (GList
* item
= list
; item
; item
= item
->next
)
288 GTKApplyStyle(GTK_WIDGET(item
->data
), style
);
294 wxSize
wxButton::DoGetBestSize() const
296 // the default button in wxGTK is bigger than the other ones because of an
297 // extra border around it, but we don't want to take it into account in
298 // our size calculations (otherwise the result is visually ugly), so
299 // always return the size of non default button from here
300 const bool isDefault
= gtk_widget_has_default(m_widget
) != 0;
303 // temporarily unset default flag
304 gtk_widget_set_can_default(m_widget
, FALSE
);
307 wxSize
ret( wxAnyButton::DoGetBestSize() );
312 gtk_widget_set_can_default(m_widget
, TRUE
);
315 if (!HasFlag(wxBU_EXACTFIT
))
317 wxSize defaultSize
= GetDefaultSize();
318 if (ret
.x
< defaultSize
.x
)
319 ret
.x
= defaultSize
.x
;
320 if (ret
.y
< defaultSize
.y
)
321 ret
.y
= defaultSize
.y
;
330 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
332 return GetDefaultAttributesFromGTKWidget(gtk_button_new());
335 #endif // wxUSE_BUTTON