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
);
90 win
->m_x
- top_border
,
91 win
->m_y
- left_border
,
92 win
->m_width
+ left_border
+ right_border
,
93 win
->m_height
+ top_border
+ bottom_border
);
99 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
103 IMPLEMENT_DYNAMIC_CLASS(wxButton
,wxControl
)
109 wxButton::~wxButton()
113 bool wxButton::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&label
,
114 const wxPoint
&pos
, const wxSize
&size
,
115 long style
, const wxValidator
& validator
, const wxString
&name
)
118 m_acceptsFocus
= TRUE
;
120 if (!PreCreation( parent
, pos
, size
) ||
121 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
123 wxFAIL_MSG( wxT("wxButton creation failed") );
127 m_widget
= gtk_button_new_with_mnemonic("");
129 float x_alignment
= 0.5;
130 if (HasFlag(wxBU_LEFT
))
132 else if (HasFlag(wxBU_RIGHT
))
135 float y_alignment
= 0.5;
136 if (HasFlag(wxBU_TOP
))
138 else if (HasFlag(wxBU_BOTTOM
))
142 if (!gtk_check_version(2,4,0))
144 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
149 if (GTK_IS_MISC(GTK_BIN(m_widget
)->child
))
150 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(m_widget
)->child
),
151 x_alignment
, y_alignment
);
156 if (style
& wxNO_BORDER
)
157 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
159 g_signal_connect_after (m_widget
, "clicked",
160 G_CALLBACK (gtk_button_clicked_callback
),
163 g_signal_connect_after (m_widget
, "style_set",
164 G_CALLBACK (gtk_button_style_set_callback
),
167 m_parent
->DoAddChild( this );
175 void wxButton::SetDefault()
177 wxWindow
*parent
= GetParent();
178 wxCHECK_RET( parent
, _T("button without parent?") );
180 parent
->SetDefaultItem(this);
182 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
183 gtk_widget_grab_default( m_widget
);
185 // resize for default border
186 gtk_button_style_set_callback( m_widget
, NULL
, this );
190 wxSize
wxButtonBase::GetDefaultSize()
192 static wxSize size
= wxDefaultSize
;
193 if (size
== wxDefaultSize
)
195 // NB: Default size of buttons should be same as size of stock
196 // buttons as used in most GTK+ apps. Unfortunately it's a little
197 // tricky to obtain this size: stock button's size may be smaller
198 // than size of button in GtkButtonBox and vice versa,
199 // GtkButtonBox's minimal button size may be smaller than stock
200 // button's size. We have to retrieve both values and combine them.
202 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
203 GtkWidget
*box
= gtk_hbutton_box_new();
204 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
205 gtk_container_add(GTK_CONTAINER(box
), btn
);
206 gtk_container_add(GTK_CONTAINER(wnd
), box
);
208 gtk_widget_size_request(btn
, &req
);
210 gint minwidth
, minheight
;
211 gtk_widget_style_get(box
,
212 "child-min-width", &minwidth
,
213 "child-min-height", &minheight
,
216 size
.x
= wxMax(minwidth
, req
.width
);
217 size
.y
= wxMax(minheight
, req
.height
);
219 gtk_widget_destroy(wnd
);
224 void wxButton::SetLabel( const wxString
&lbl
)
226 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
230 if (label
.empty() && wxIsStockID(m_windowId
))
231 label
= wxGetStockLabel(m_windowId
);
233 wxControl::SetLabel(label
);
235 const wxString labelGTK
= GTKConvertMnemonics(label
);
237 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
239 const char *stock
= wxGetStockGtkID(m_windowId
);
242 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
243 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
248 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
249 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
251 ApplyWidgetStyle( false );
254 bool wxButton::Enable( bool enable
)
256 if ( !wxControl::Enable( enable
) )
259 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
264 bool wxButton::IsOwnGtkWindow( GdkWindow
*window
)
266 return GTK_BUTTON(m_widget
)->event_window
;
269 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
271 gtk_widget_modify_style(m_widget
, style
);
272 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
275 wxSize
wxButton::DoGetBestSize() const
277 // the default button in wxGTK is bigger than the other ones because of an
278 // extra border around it, but we don't want to take it into account in
279 // our size calculations (otherwsie the result is visually ugly), so
280 // always return the size of non default button from here
281 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
284 // temporarily unset default flag
285 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
288 wxSize
ret( wxControl::DoGetBestSize() );
293 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
296 if (!HasFlag(wxBU_EXACTFIT
))
298 wxSize defaultSize
= GetDefaultSize();
299 if (ret
.x
< defaultSize
.x
) ret
.x
= defaultSize
.x
;
300 if (ret
.y
< defaultSize
.y
) ret
.y
= defaultSize
.y
;
309 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
311 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
314 #endif // wxUSE_BUTTON