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/win_gtk.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern bool g_blockEventsOnDrag
;
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
41 static void gtk_button_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
44 wxapp_install_idle_handler();
46 if (!button
->m_hasVMT
) return;
47 if (g_blockEventsOnDrag
) return;
49 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
50 event
.SetEventObject(button
);
51 button
->GetEventHandler()->ProcessEvent(event
);
55 //-----------------------------------------------------------------------------
56 // "style_set" from m_widget
57 //-----------------------------------------------------------------------------
60 gtk_button_style_set_callback( GtkWidget
*m_widget
, GtkStyle
*WXUNUSED(style
), wxButton
*win
)
63 wxapp_install_idle_handler();
68 int bottom_border
= 0;
70 /* the default button has a border around it */
71 if (GTK_WIDGET_CAN_DEFAULT(m_widget
))
73 GtkBorder
*default_border
= NULL
;
74 gtk_widget_style_get( m_widget
, "default_border", &default_border
, NULL
);
77 left_border
+= default_border
->left
;
78 right_border
+= default_border
->right
;
79 top_border
+= default_border
->top
;
80 bottom_border
+= default_border
->bottom
;
81 g_free( default_border
);
84 win
->m_x
- top_border
,
85 win
->m_y
- left_border
,
86 win
->m_width
+ left_border
+ right_border
,
87 win
->m_height
+ top_border
+ bottom_border
);
93 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
97 IMPLEMENT_DYNAMIC_CLASS(wxButton
,wxControl
)
103 wxButton::~wxButton()
107 bool wxButton::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&label
,
108 const wxPoint
&pos
, const wxSize
&size
,
109 long style
, const wxValidator
& validator
, const wxString
&name
)
113 if (!PreCreation( parent
, pos
, size
) ||
114 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
116 wxFAIL_MSG( wxT("wxButton creation failed") );
120 m_widget
= gtk_button_new_with_mnemonic("");
122 float x_alignment
= 0.5;
123 if (HasFlag(wxBU_LEFT
))
125 else if (HasFlag(wxBU_RIGHT
))
128 float y_alignment
= 0.5;
129 if (HasFlag(wxBU_TOP
))
131 else if (HasFlag(wxBU_BOTTOM
))
135 if (!gtk_check_version(2,4,0))
137 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
142 if (GTK_IS_MISC(GTK_BIN(m_widget
)->child
))
143 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(m_widget
)->child
),
144 x_alignment
, y_alignment
);
149 if (style
& wxNO_BORDER
)
150 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
152 g_signal_connect_after (m_widget
, "clicked",
153 G_CALLBACK (gtk_button_clicked_callback
),
156 g_signal_connect_after (m_widget
, "style_set",
157 G_CALLBACK (gtk_button_style_set_callback
),
160 m_parent
->DoAddChild( this );
168 wxWindow
*wxButton::SetDefault()
170 wxWindow
*oldDefault
= wxButtonBase::SetDefault();
172 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
173 gtk_widget_grab_default( m_widget
);
175 // resize for default border
176 gtk_button_style_set_callback( m_widget
, NULL
, this );
182 wxSize
wxButtonBase::GetDefaultSize()
184 static wxSize size
= wxDefaultSize
;
185 if (size
== wxDefaultSize
)
187 // NB: Default size of buttons should be same as size of stock
188 // buttons as used in most GTK+ apps. Unfortunately it's a little
189 // tricky to obtain this size: stock button's size may be smaller
190 // than size of button in GtkButtonBox and vice versa,
191 // GtkButtonBox's minimal button size may be smaller than stock
192 // button's size. We have to retrieve both values and combine them.
194 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
195 GtkWidget
*box
= gtk_hbutton_box_new();
196 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
197 gtk_container_add(GTK_CONTAINER(box
), btn
);
198 gtk_container_add(GTK_CONTAINER(wnd
), box
);
200 gtk_widget_size_request(btn
, &req
);
202 gint minwidth
, minheight
;
203 gtk_widget_style_get(box
,
204 "child-min-width", &minwidth
,
205 "child-min-height", &minheight
,
208 size
.x
= wxMax(minwidth
, req
.width
);
209 size
.y
= wxMax(minheight
, req
.height
);
211 gtk_widget_destroy(wnd
);
216 void wxButton::SetLabel( const wxString
&lbl
)
218 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
222 if (label
.empty() && wxIsStockID(m_windowId
))
223 label
= wxGetStockLabel(m_windowId
);
225 wxControl::SetLabel(label
);
227 const wxString labelGTK
= GTKConvertMnemonics(label
);
229 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
231 const char *stock
= wxGetStockGtkID(m_windowId
);
234 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
235 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
240 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
241 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
243 ApplyWidgetStyle( false );
246 bool wxButton::Enable( bool enable
)
248 if ( !wxControl::Enable( enable
) )
251 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
256 GdkWindow
*wxButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
258 return GTK_BUTTON(m_widget
)->event_window
;
261 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
263 gtk_widget_modify_style(m_widget
, style
);
264 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
267 wxSize
wxButton::DoGetBestSize() const
269 // the default button in wxGTK is bigger than the other ones because of an
270 // extra border around it, but we don't want to take it into account in
271 // our size calculations (otherwsie the result is visually ugly), so
272 // always return the size of non default button from here
273 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
276 // temporarily unset default flag
277 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
280 wxSize
ret( wxControl::DoGetBestSize() );
285 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
288 if (!HasFlag(wxBU_EXACTFIT
))
290 wxSize defaultSize
= GetDefaultSize();
291 if (ret
.x
< defaultSize
.x
) ret
.x
= defaultSize
.x
;
292 if (ret
.y
< defaultSize
.y
) ret
.y
= defaultSize
.y
;
301 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
303 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
306 #endif // wxUSE_BUTTON