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 //-----------------------------------------------------------------------------
27 extern bool g_blockEventsOnDrag
;
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
34 static void gtk_button_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
36 if (!button
->m_hasVMT
) return;
37 if (g_blockEventsOnDrag
) return;
39 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
40 event
.SetEventObject(button
);
41 button
->HandleWindowEvent(event
);
45 //-----------------------------------------------------------------------------
46 // "style_set" from m_widget
47 //-----------------------------------------------------------------------------
51 gtk_button_style_set_callback(GtkWidget
* widget
, GtkStyle
*, wxButton
* win
)
53 /* the default button has a border around it */
54 wxWindow
* parent
= win
->GetParent();
55 if (parent
&& parent
->m_wxwindow
&& GTK_WIDGET_CAN_DEFAULT(widget
))
57 GtkBorder
* border
= NULL
;
58 gtk_widget_style_get(widget
, "default_border", &border
, NULL
);
62 win
->m_x
- border
->left
,
63 win
->m_y
- border
->top
,
64 win
->m_width
+ border
->left
+ border
->right
,
65 win
->m_height
+ border
->top
+ border
->bottom
);
66 gtk_border_free(border
);
72 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
76 IMPLEMENT_DYNAMIC_CLASS(wxButton
,wxControl
)
86 bool wxButton::Create(wxWindow
*parent
,
88 const wxString
&label
,
92 const wxValidator
& validator
,
95 if (!PreCreation( parent
, pos
, size
) ||
96 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
98 wxFAIL_MSG( wxT("wxButton creation failed") );
102 m_widget
= gtk_button_new_with_mnemonic("");
103 g_object_ref(m_widget
);
105 float x_alignment
= 0.5;
106 if (HasFlag(wxBU_LEFT
))
108 else if (HasFlag(wxBU_RIGHT
))
111 float y_alignment
= 0.5;
112 if (HasFlag(wxBU_TOP
))
114 else if (HasFlag(wxBU_BOTTOM
))
117 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
121 if (style
& wxNO_BORDER
)
122 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
124 g_signal_connect_after (m_widget
, "clicked",
125 G_CALLBACK (gtk_button_clicked_callback
),
128 g_signal_connect_after (m_widget
, "style_set",
129 G_CALLBACK (gtk_button_style_set_callback
),
132 m_parent
->DoAddChild( this );
140 wxWindow
*wxButton::SetDefault()
142 wxWindow
*oldDefault
= wxButtonBase::SetDefault();
144 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
145 gtk_widget_grab_default( m_widget
);
147 // resize for default border
148 gtk_button_style_set_callback( m_widget
, NULL
, this );
154 wxSize
wxButtonBase::GetDefaultSize()
156 static wxSize size
= wxDefaultSize
;
157 if (size
== wxDefaultSize
)
159 // NB: Default size of buttons should be same as size of stock
160 // buttons as used in most GTK+ apps. Unfortunately it's a little
161 // tricky to obtain this size: stock button's size may be smaller
162 // than size of button in GtkButtonBox and vice versa,
163 // GtkButtonBox's minimal button size may be smaller than stock
164 // button's size. We have to retrieve both values and combine them.
166 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
167 GtkWidget
*box
= gtk_hbutton_box_new();
168 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
169 gtk_container_add(GTK_CONTAINER(box
), btn
);
170 gtk_container_add(GTK_CONTAINER(wnd
), box
);
172 gtk_widget_size_request(btn
, &req
);
174 gint minwidth
, minheight
;
175 gtk_widget_style_get(box
,
176 "child-min-width", &minwidth
,
177 "child-min-height", &minheight
,
180 size
.x
= wxMax(minwidth
, req
.width
);
181 size
.y
= wxMax(minheight
, req
.height
);
183 gtk_widget_destroy(wnd
);
188 void wxButton::SetLabel( const wxString
&lbl
)
190 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
194 if (label
.empty() && wxIsStockID(m_windowId
))
195 label
= wxGetStockLabel(m_windowId
);
197 wxControl::SetLabel(label
);
199 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
201 const char *stock
= wxGetStockGtkID(m_windowId
);
204 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
205 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
210 const wxString labelGTK
= GTKConvertMnemonics(label
);
211 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
212 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
214 GTKApplyWidgetStyle( false );
217 bool wxButton::Enable( bool enable
)
219 bool isEnabled
= IsEnabled();
221 if ( !wxControl::Enable( enable
) )
224 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
226 if (!isEnabled
&& enable
)
234 GdkWindow
*wxButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
236 return GTK_BUTTON(m_widget
)->event_window
;
239 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
241 gtk_widget_modify_style(m_widget
, style
);
242 GtkWidget
*child
= GTK_BIN(m_widget
)->child
;
243 gtk_widget_modify_style(child
, style
);
245 // for buttons with images, the path to the label is (at least in 2.12)
246 // GtkButton -> GtkAlignment -> GtkHBox -> GtkLabel
247 if ( GTK_IS_ALIGNMENT(child
) )
249 GtkWidget
*box
= GTK_BIN(child
)->child
;
250 if ( GTK_IS_BOX(box
) )
252 for (GList
* item
= GTK_BOX(box
)->children
; item
; item
= item
->next
)
254 GtkBoxChild
* boxChild
= static_cast<GtkBoxChild
*>(item
->data
);
255 gtk_widget_modify_style(boxChild
->widget
, style
);
261 wxSize
wxButton::DoGetBestSize() const
263 // the default button in wxGTK is bigger than the other ones because of an
264 // extra border around it, but we don't want to take it into account in
265 // our size calculations (otherwsie the result is visually ugly), so
266 // always return the size of non default button from here
267 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
270 // temporarily unset default flag
271 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
274 wxSize
ret( wxControl::DoGetBestSize() );
279 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
282 if (!HasFlag(wxBU_EXACTFIT
))
284 wxSize defaultSize
= GetDefaultSize();
285 if (ret
.x
< defaultSize
.x
) ret
.x
= defaultSize
.x
;
286 if (ret
.y
< defaultSize
.y
) ret
.y
= defaultSize
.y
;
295 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
297 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
300 #endif // wxUSE_BUTTON