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
))
79 GtkBorder
*default_border
= NULL
;
80 gtk_widget_style_get( m_widget
, "default_border", &default_border
, NULL
);
83 left_border
+= default_border
->left
;
84 right_border
+= default_border
->right
;
85 top_border
+= default_border
->top
;
86 bottom_border
+= default_border
->bottom
;
87 g_free( default_border
);
89 win
->DoMoveWindow( win
->m_x
-top_border
,
91 win
->m_width
+left_border
+right_border
,
92 win
->m_height
+top_border
+bottom_border
);
98 //-----------------------------------------------------------------------------
100 //-----------------------------------------------------------------------------
102 IMPLEMENT_DYNAMIC_CLASS(wxButton
,wxControl
)
108 wxButton::~wxButton()
112 bool wxButton::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&label
,
113 const wxPoint
&pos
, const wxSize
&size
,
114 long style
, const wxValidator
& validator
, const wxString
&name
)
117 m_acceptsFocus
= TRUE
;
119 if (!PreCreation( parent
, pos
, size
) ||
120 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
122 wxFAIL_MSG( wxT("wxButton creation failed") );
126 m_widget
= gtk_button_new_with_mnemonic("");
128 float x_alignment
= 0.5;
129 if (HasFlag(wxBU_LEFT
))
131 else if (HasFlag(wxBU_RIGHT
))
134 float y_alignment
= 0.5;
135 if (HasFlag(wxBU_TOP
))
137 else if (HasFlag(wxBU_BOTTOM
))
141 if (!gtk_check_version(2,4,0))
143 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
148 if (GTK_IS_MISC(GTK_BIN(m_widget
)->child
))
149 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(m_widget
)->child
),
150 x_alignment
, y_alignment
);
155 if (style
& wxNO_BORDER
)
156 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
158 g_signal_connect_after (m_widget
, "clicked",
159 G_CALLBACK (gtk_button_clicked_callback
),
162 g_signal_connect_after (m_widget
, "style_set",
163 G_CALLBACK (gtk_button_style_set_callback
),
166 m_parent
->DoAddChild( this );
174 void wxButton::SetDefault()
176 wxWindow
*parent
= GetParent();
177 wxCHECK_RET( parent
, _T("button without parent?") );
179 parent
->SetDefaultItem(this);
181 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
182 gtk_widget_grab_default( m_widget
);
184 // resize for default border
185 gtk_button_style_set_callback( m_widget
, NULL
, this );
189 wxSize
wxButtonBase::GetDefaultSize()
191 static wxSize size
= wxDefaultSize
;
192 if (size
== wxDefaultSize
)
194 // NB: Default size of buttons should be same as size of stock
195 // buttons as used in most GTK+ apps. Unfortunately it's a little
196 // tricky to obtain this size: stock button's size may be smaller
197 // than size of button in GtkButtonBox and vice versa,
198 // GtkButtonBox's minimal button size may be smaller than stock
199 // button's size. We have to retrieve both values and combine them.
201 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
202 GtkWidget
*box
= gtk_hbutton_box_new();
203 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
204 gtk_container_add(GTK_CONTAINER(box
), btn
);
205 gtk_container_add(GTK_CONTAINER(wnd
), box
);
207 gtk_widget_size_request(btn
, &req
);
209 gint minwidth
, minheight
;
210 gtk_widget_style_get(box
,
211 "child-min-width", &minwidth
,
212 "child-min-height", &minheight
,
215 size
.x
= wxMax(minwidth
, req
.width
);
216 size
.y
= wxMax(minheight
, req
.height
);
218 gtk_widget_destroy(wnd
);
223 void wxButton::SetLabel( const wxString
&lbl
)
225 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
229 if (label
.empty() && wxIsStockID(m_windowId
))
230 label
= wxGetStockLabel(m_windowId
);
232 wxControl::SetLabel(label
);
234 const wxString labelGTK
= GTKConvertMnemonics(label
);
236 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
238 const char *stock
= wxGetStockGtkID(m_windowId
);
241 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
242 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
247 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
248 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
250 ApplyWidgetStyle( false );
253 bool wxButton::Enable( bool enable
)
255 if ( !wxControl::Enable( enable
) )
258 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
263 bool wxButton::IsOwnGtkWindow( GdkWindow
*window
)
265 return GTK_BUTTON(m_widget
)->event_window
;
268 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
270 gtk_widget_modify_style(m_widget
, style
);
271 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
274 wxSize
wxButton::DoGetBestSize() const
276 // the default button in wxGTK is bigger than the other ones because of an
277 // extra border around it, but we don't want to take it into account in
278 // our size calculations (otherwsie the result is visually ugly), so
279 // always return the size of non default button from here
280 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
283 // temporarily unset default flag
284 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
287 wxSize
ret( wxControl::DoGetBestSize() );
292 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
295 if (!HasFlag(wxBU_EXACTFIT
))
297 wxSize defaultSize
= GetDefaultSize();
298 if (ret
.x
< defaultSize
.x
) ret
.x
= defaultSize
.x
;
299 if (ret
.y
< defaultSize
.y
) ret
.y
= defaultSize
.y
;
308 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
310 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
313 #endif // wxUSE_BUTTON