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
);
41 //-----------------------------------------------------------------------------
42 // "style_set" from m_widget
43 //-----------------------------------------------------------------------------
46 wxgtk_button_style_set_callback(GtkWidget
* widget
, GtkStyle
*, wxButton
* win
)
48 /* the default button has a border around it */
49 wxWindow
* parent
= win
->GetParent();
50 if (parent
&& parent
->m_wxwindow
&& gtk_widget_get_can_default(widget
))
52 GtkBorder
* border
= NULL
;
53 gtk_widget_style_get(widget
, "default_border", &border
, NULL
);
57 win
->m_x
- border
->left
,
58 win
->m_y
- border
->top
,
59 win
->m_width
+ border
->left
+ border
->right
,
60 win
->m_height
+ border
->top
+ border
->bottom
);
61 gtk_border_free(border
);
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
72 bool wxButton::Create(wxWindow
*parent
,
74 const wxString
&label
,
78 const wxValidator
& validator
,
81 if (!PreCreation( parent
, pos
, size
) ||
82 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
84 wxFAIL_MSG( wxT("wxButton creation failed") );
88 // create either a standard button with text label (which may still contain
89 // an image under GTK+ 2.6+) or a bitmap-only button if we don't have any
92 useLabel
= !(style
& wxBU_NOTEXT
) && (!label
.empty() || wxIsStockID(id
));
95 m_widget
= gtk_button_new_with_mnemonic("");
97 else // no label, suppose we will have a bitmap
99 m_widget
= gtk_button_new();
101 GtkWidget
*image
= gtk_image_new();
102 gtk_widget_show(image
);
103 gtk_container_add(GTK_CONTAINER(m_widget
), image
);
106 g_object_ref(m_widget
);
108 float x_alignment
= 0.5;
109 if (HasFlag(wxBU_LEFT
))
111 else if (HasFlag(wxBU_RIGHT
))
114 float y_alignment
= 0.5;
115 if (HasFlag(wxBU_TOP
))
117 else if (HasFlag(wxBU_BOTTOM
))
120 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
125 if (style
& wxNO_BORDER
)
126 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
128 g_signal_connect_after (m_widget
, "clicked",
129 G_CALLBACK (wxgtk_button_clicked_callback
),
132 g_signal_connect_after (m_widget
, "style_set",
133 G_CALLBACK (wxgtk_button_style_set_callback
),
136 m_parent
->DoAddChild( this );
144 wxWindow
*wxButton::SetDefault()
146 wxWindow
*oldDefault
= wxButtonBase::SetDefault();
148 gtk_widget_set_can_default(m_widget
, TRUE
);
149 gtk_widget_grab_default( m_widget
);
151 // resize for default border
152 wxgtk_button_style_set_callback( m_widget
, NULL
, this );
158 wxSize
wxButtonBase::GetDefaultSize()
160 static wxSize size
= wxDefaultSize
;
161 if (size
== wxDefaultSize
)
163 // NB: Default size of buttons should be same as size of stock
164 // buttons as used in most GTK+ apps. Unfortunately it's a little
165 // tricky to obtain this size: stock button's size may be smaller
166 // than size of button in GtkButtonBox and vice versa,
167 // GtkButtonBox's minimal button size may be smaller than stock
168 // button's size. We have to retrieve both values and combine them.
170 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
171 GtkWidget
*box
= gtk_hbutton_box_new();
172 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
173 gtk_container_add(GTK_CONTAINER(box
), btn
);
174 gtk_container_add(GTK_CONTAINER(wnd
), box
);
176 gtk_widget_size_request(btn
, &req
);
178 gint minwidth
, minheight
;
179 gtk_widget_style_get(box
,
180 "child-min-width", &minwidth
,
181 "child-min-height", &minheight
,
184 size
.x
= wxMax(minwidth
, req
.width
);
185 size
.y
= wxMax(minheight
, req
.height
);
187 gtk_widget_destroy(wnd
);
192 void wxButton::SetLabel( const wxString
&lbl
)
194 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
198 if (label
.empty() && wxIsStockID(m_windowId
))
199 label
= wxGetStockLabel(m_windowId
);
201 wxAnyButton::SetLabel(label
);
203 // don't use label if it was explicitly disabled
204 if ( HasFlag(wxBU_NOTEXT
) )
207 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
209 const char *stock
= wxGetStockGtkID(m_windowId
);
212 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
213 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
218 // this call is necessary if the button had been initially created without
219 // a (text) label -- then we didn't use gtk_button_new_with_mnemonic() and
220 // so "use-underline" GtkButton property remained unset
221 gtk_button_set_use_underline(GTK_BUTTON(m_widget
), TRUE
);
222 const wxString labelGTK
= GTKConvertMnemonics(label
);
223 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
224 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
226 GTKApplyWidgetStyle( false );
230 bool wxButton::DoSetLabelMarkup(const wxString
& markup
)
232 wxCHECK_MSG( m_widget
!= NULL
, false, "invalid button" );
234 const wxString stripped
= RemoveMarkup(markup
);
235 if ( stripped
.empty() && !markup
.empty() )
238 wxControl::SetLabel(stripped
);
240 GtkLabel
* const label
= GTKGetLabel();
241 wxCHECK_MSG( label
, false, "no label in this button?" );
243 GTKSetLabelWithMarkupForLabel(label
, markup
);
248 GtkLabel
*wxButton::GTKGetLabel() const
250 GtkWidget
* child
= gtk_bin_get_child(GTK_BIN(m_widget
));
251 if ( GTK_IS_ALIGNMENT(child
) )
253 GtkWidget
* box
= gtk_bin_get_child(GTK_BIN(child
));
254 GtkLabel
* label
= NULL
;
255 GList
* list
= gtk_container_get_children(GTK_CONTAINER(box
));
256 for (GList
* item
= list
; item
; item
= item
->next
)
258 if (GTK_IS_LABEL(item
->data
))
259 label
= GTK_LABEL(item
->data
);
266 return GTK_LABEL(child
);
268 #endif // wxUSE_MARKUP
270 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
272 gtk_widget_modify_style(m_widget
, style
);
273 GtkWidget
* child
= gtk_bin_get_child(GTK_BIN(m_widget
));
274 gtk_widget_modify_style(child
, style
);
276 // for buttons with images, the path to the label is (at least in 2.12)
277 // GtkButton -> GtkAlignment -> GtkHBox -> GtkLabel
278 if ( GTK_IS_ALIGNMENT(child
) )
280 GtkWidget
* box
= gtk_bin_get_child(GTK_BIN(child
));
281 if ( GTK_IS_BOX(box
) )
283 GList
* list
= gtk_container_get_children(GTK_CONTAINER(box
));
284 for (GList
* item
= list
; item
; item
= item
->next
)
286 gtk_widget_modify_style(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
);
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