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"
22 #include "wx/gtk/private/list.h"
24 // ----------------------------------------------------------------------------
26 // ----------------------------------------------------------------------------
32 wxgtk_button_clicked_callback(GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
34 if ( button
->GTKShouldIgnoreEvent() )
37 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
38 event
.SetEventObject(button
);
39 button
->HandleWindowEvent(event
);
42 //-----------------------------------------------------------------------------
43 // "style_set" from m_widget
44 //-----------------------------------------------------------------------------
47 wxgtk_button_style_set_callback(GtkWidget
* widget
, GtkStyle
*, wxButton
* win
)
49 /* the default button has a border around it */
50 wxWindow
* parent
= win
->GetParent();
51 if (parent
&& parent
->m_wxwindow
&& gtk_widget_get_can_default(widget
))
53 GtkBorder
* border
= NULL
;
54 gtk_widget_style_get(widget
, "default_border", &border
, NULL
);
58 win
->m_x
- border
->left
,
59 win
->m_y
- border
->top
,
60 win
->m_width
+ border
->left
+ border
->right
,
61 win
->m_height
+ border
->top
+ border
->bottom
);
62 gtk_border_free(border
);
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
73 bool wxButton::Create(wxWindow
*parent
,
75 const wxString
&label
,
79 const wxValidator
& validator
,
82 if (!PreCreation( parent
, pos
, size
) ||
83 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
85 wxFAIL_MSG( wxT("wxButton creation failed") );
89 // create either a standard button with text label (which may still contain
90 // an image under GTK+ 2.6+) or a bitmap-only button if we don't have any
93 useLabel
= !(style
& wxBU_NOTEXT
) && (!label
.empty() || wxIsStockID(id
));
96 m_widget
= gtk_button_new_with_mnemonic("");
98 else // no label, suppose we will have a bitmap
100 m_widget
= gtk_button_new();
102 GtkWidget
*image
= gtk_image_new();
103 gtk_widget_show(image
);
104 gtk_container_add(GTK_CONTAINER(m_widget
), image
);
107 g_object_ref(m_widget
);
109 float x_alignment
= 0.5;
110 if (HasFlag(wxBU_LEFT
))
112 else if (HasFlag(wxBU_RIGHT
))
115 float y_alignment
= 0.5;
116 if (HasFlag(wxBU_TOP
))
118 else if (HasFlag(wxBU_BOTTOM
))
121 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
126 if (style
& wxNO_BORDER
)
127 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
129 g_signal_connect_after (m_widget
, "clicked",
130 G_CALLBACK (wxgtk_button_clicked_callback
),
133 g_signal_connect_after (m_widget
, "style_set",
134 G_CALLBACK (wxgtk_button_style_set_callback
),
137 m_parent
->DoAddChild( this );
145 wxWindow
*wxButton::SetDefault()
147 wxWindow
*oldDefault
= wxButtonBase::SetDefault();
149 gtk_widget_set_can_default(m_widget
, TRUE
);
150 gtk_widget_grab_default( m_widget
);
152 // resize for default border
153 wxgtk_button_style_set_callback( m_widget
, NULL
, this );
159 wxSize
wxButtonBase::GetDefaultSize()
161 static wxSize size
= wxDefaultSize
;
162 if (size
== wxDefaultSize
)
164 // NB: Default size of buttons should be same as size of stock
165 // buttons as used in most GTK+ apps. Unfortunately it's a little
166 // tricky to obtain this size: stock button's size may be smaller
167 // than size of button in GtkButtonBox and vice versa,
168 // GtkButtonBox's minimal button size may be smaller than stock
169 // button's size. We have to retrieve both values and combine them.
171 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
172 GtkWidget
*box
= gtk_hbutton_box_new();
173 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
174 gtk_container_add(GTK_CONTAINER(box
), btn
);
175 gtk_container_add(GTK_CONTAINER(wnd
), box
);
177 gtk_widget_size_request(btn
, &req
);
179 gint minwidth
, minheight
;
180 gtk_widget_style_get(box
,
181 "child-min-width", &minwidth
,
182 "child-min-height", &minheight
,
185 size
.x
= wxMax(minwidth
, req
.width
);
186 size
.y
= wxMax(minheight
, req
.height
);
188 gtk_widget_destroy(wnd
);
193 void wxButton::SetLabel( const wxString
&lbl
)
195 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
199 if (label
.empty() && wxIsStockID(m_windowId
))
200 label
= wxGetStockLabel(m_windowId
);
202 wxAnyButton::SetLabel(label
);
204 // don't use label if it was explicitly disabled
205 if ( HasFlag(wxBU_NOTEXT
) )
208 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
210 const char *stock
= wxGetStockGtkID(m_windowId
);
213 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
214 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
219 // this call is necessary if the button had been initially created without
220 // a (text) label -- then we didn't use gtk_button_new_with_mnemonic() and
221 // so "use-underline" GtkButton property remained unset
222 gtk_button_set_use_underline(GTK_BUTTON(m_widget
), TRUE
);
223 const wxString labelGTK
= GTKConvertMnemonics(label
);
224 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
225 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
227 GTKApplyWidgetStyle( false );
231 bool wxButton::DoSetLabelMarkup(const wxString
& markup
)
233 wxCHECK_MSG( m_widget
!= NULL
, false, "invalid button" );
235 const wxString stripped
= RemoveMarkup(markup
);
236 if ( stripped
.empty() && !markup
.empty() )
239 wxControl::SetLabel(stripped
);
241 GtkLabel
* const label
= GTKGetLabel();
242 wxCHECK_MSG( label
, false, "no label in this button?" );
244 GTKSetLabelWithMarkupForLabel(label
, markup
);
249 GtkLabel
*wxButton::GTKGetLabel() const
251 GtkWidget
* child
= gtk_bin_get_child(GTK_BIN(m_widget
));
252 if ( GTK_IS_ALIGNMENT(child
) )
254 GtkWidget
* box
= gtk_bin_get_child(GTK_BIN(child
));
255 GtkLabel
* label
= NULL
;
256 wxGtkList
list(gtk_container_get_children(GTK_CONTAINER(box
)));
257 for (GList
* item
= list
; item
; item
= item
->next
)
259 if (GTK_IS_LABEL(item
->data
))
260 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 wxGtkList
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
);
292 wxSize
wxButton::DoGetBestSize() const
294 // the default button in wxGTK is bigger than the other ones because of an
295 // extra border around it, but we don't want to take it into account in
296 // our size calculations (otherwise the result is visually ugly), so
297 // always return the size of non default button from here
298 const bool isDefault
= gtk_widget_has_default(m_widget
);
301 // temporarily unset default flag
302 gtk_widget_set_can_default(m_widget
, FALSE
);
305 wxSize
ret( wxAnyButton::DoGetBestSize() );
310 gtk_widget_set_can_default(m_widget
, TRUE
);
313 if (!HasFlag(wxBU_EXACTFIT
))
315 wxSize defaultSize
= GetDefaultSize();
316 if (ret
.x
< defaultSize
.x
)
317 ret
.x
= defaultSize
.x
;
318 if (ret
.y
< defaultSize
.y
)
319 ret
.y
= defaultSize
.y
;
328 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
330 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
333 #endif // wxUSE_BUTTON