1 /////////////////////////////////////////////////////////////////////////////
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"
17 #include "wx/button.h"
18 #include "wx/stockitem.h"
20 #include "wx/gtk/private.h"
21 #include "wx/gtk/win_gtk.h"
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern void wxapp_install_idle_handler();
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern bool g_blockEventsOnDrag
;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
47 static void gtk_button_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
50 wxapp_install_idle_handler();
52 if (!button
->m_hasVMT
) return;
53 if (g_blockEventsOnDrag
) return;
55 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
56 event
.SetEventObject(button
);
57 button
->GetEventHandler()->ProcessEvent(event
);
61 //-----------------------------------------------------------------------------
62 // "style_set" from m_widget
63 //-----------------------------------------------------------------------------
66 gtk_button_style_set_callback( GtkWidget
*m_widget
, GtkStyle
*WXUNUSED(style
), wxButton
*win
)
69 wxapp_install_idle_handler();
74 int bottom_border
= 0;
76 /* the default button has a border around it */
77 if (GTK_WIDGET_CAN_DEFAULT(m_widget
))
80 GtkBorder
*default_border
= NULL
;
81 gtk_widget_style_get( m_widget
, "default_border", &default_border
, NULL
);
84 left_border
+= default_border
->left
;
85 right_border
+= default_border
->right
;
86 top_border
+= default_border
->top
;
87 bottom_border
+= default_border
->bottom
;
88 g_free( default_border
);
96 win
->DoMoveWindow( win
->m_x
-top_border
,
98 win
->m_width
+left_border
+right_border
,
99 win
->m_height
+top_border
+bottom_border
);
105 //-----------------------------------------------------------------------------
107 //-----------------------------------------------------------------------------
109 IMPLEMENT_DYNAMIC_CLASS(wxButton
,wxControl
)
115 wxButton::~wxButton()
119 bool wxButton::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&label
,
120 const wxPoint
&pos
, const wxSize
&size
,
121 long style
, const wxValidator
& validator
, const wxString
&name
)
124 m_acceptsFocus
= TRUE
;
126 if (!PreCreation( parent
, pos
, size
) ||
127 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
129 wxFAIL_MSG( wxT("wxButton creation failed") );
134 wxString label2( label );
135 for (size_t i = 0; i < label2.Len(); i++)
137 if (label2.GetChar(i) == wxT('&'))
138 label2.SetChar(i,wxT('_'));
141 GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() );
142 gtk_widget_show( accel_label );
144 m_widget = gtk_button_new();
145 gtk_container_add( GTK_CONTAINER(m_widget), accel_label );
147 gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget );
149 guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() );
150 gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) );
152 wxControl::SetLabel( label );
156 m_widget
= gtk_button_new_with_mnemonic("");
158 m_widget
= gtk_button_new_with_label("");
161 float x_alignment
= 0.5;
162 if (HasFlag(wxBU_LEFT
))
164 else if (HasFlag(wxBU_RIGHT
))
167 float y_alignment
= 0.5;
168 if (HasFlag(wxBU_TOP
))
170 else if (HasFlag(wxBU_BOTTOM
))
174 if (!gtk_check_version(2,4,0))
176 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
181 if (GTK_IS_MISC(BUTTON_CHILD(m_widget
)))
182 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget
)),
183 x_alignment
, y_alignment
);
188 if (style
& wxNO_BORDER
)
189 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
191 gtk_signal_connect_after( GTK_OBJECT(m_widget
), "clicked",
192 GTK_SIGNAL_FUNC(gtk_button_clicked_callback
), (gpointer
*)this );
194 gtk_signal_connect_after( GTK_OBJECT(m_widget
), "style_set",
195 GTK_SIGNAL_FUNC(gtk_button_style_set_callback
), (gpointer
*) this );
197 m_parent
->DoAddChild( this );
205 void wxButton::SetDefault()
207 wxWindow
*parent
= GetParent();
208 wxCHECK_RET( parent
, _T("button without parent?") );
210 parent
->SetDefaultItem(this);
212 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
213 gtk_widget_grab_default( m_widget
);
215 // resize for default border
216 gtk_button_style_set_callback( m_widget
, NULL
, this );
220 wxSize
wxButtonBase::GetDefaultSize()
223 static wxSize size
= wxDefaultSize
;
224 if (size
== wxDefaultSize
)
226 // NB: Default size of buttons should be same as size of stock
227 // buttons as used in most GTK+ apps. Unfortunately it's a little
228 // tricky to obtain this size: stock button's size may be smaller
229 // than size of button in GtkButtonBox and vice versa,
230 // GtkButtonBox's minimal button size may be smaller than stock
231 // button's size. We have to retrieve both values and combine them.
233 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
234 GtkWidget
*box
= gtk_hbutton_box_new();
235 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
236 gtk_container_add(GTK_CONTAINER(box
), btn
);
237 gtk_container_add(GTK_CONTAINER(wnd
), box
);
239 gtk_widget_size_request(btn
, &req
);
241 gint minwidth
, minheight
;
242 gtk_widget_style_get(box
,
243 "child-min-width", &minwidth
,
244 "child-min-height", &minheight
,
247 size
.x
= wxMax(minwidth
, req
.width
);
248 size
.y
= wxMax(minheight
, req
.height
);
250 gtk_widget_destroy(wnd
);
254 return wxSize(80,26);
258 void wxButton::SetLabel( const wxString
&lbl
)
260 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
264 if (label
.empty() && wxIsStockID(m_windowId
))
265 label
= wxGetStockLabel(m_windowId
);
267 wxControl::SetLabel(label
);
270 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
272 const char *stock
= wxGetStockGtkID(m_windowId
);
275 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
276 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
281 wxString label2
= PrepareLabelMnemonics(label
);
282 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(label2
));
283 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
285 ApplyWidgetStyle( false );
288 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget
)), wxGTK_CONV(GetLabel()));
292 bool wxButton::Enable( bool enable
)
294 if ( !wxControl::Enable( enable
) )
297 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget
), enable
);
302 bool wxButton::IsOwnGtkWindow( GdkWindow
*window
)
305 return GTK_BUTTON(m_widget
)->event_window
;
307 return (window
== m_widget
->window
);
311 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
313 gtk_widget_modify_style(m_widget
, style
);
314 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
317 wxSize
wxButton::DoGetBestSize() const
319 // the default button in wxGTK is bigger than the other ones because of an
320 // extra border around it, but we don't want to take it into account in
321 // our size calculations (otherwsie the result is visually ugly), so
322 // always return the size of non default button from here
323 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
326 // temporarily unset default flag
327 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
330 wxSize
ret( wxControl::DoGetBestSize() );
335 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
339 ret
.x
+= 10; // add a few pixels for sloppy (but common) themes
342 if (!HasFlag(wxBU_EXACTFIT
))
344 wxSize defaultSize
= GetDefaultSize();
345 if (ret
.x
< defaultSize
.x
) ret
.x
= defaultSize
.x
;
346 if (ret
.y
< defaultSize
.y
) ret
.y
= defaultSize
.y
;
355 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
357 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
360 #endif // wxUSE_BUTTON