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 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern bool g_blockEventsOnDrag
;
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
40 static void gtk_button_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
42 if (!button
->m_hasVMT
) return;
43 if (g_blockEventsOnDrag
) return;
45 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
46 event
.SetEventObject(button
);
47 button
->GetEventHandler()->ProcessEvent(event
);
51 //-----------------------------------------------------------------------------
52 // "style_set" from m_widget
53 //-----------------------------------------------------------------------------
56 gtk_button_style_set_callback( GtkWidget
*m_widget
, GtkStyle
*WXUNUSED(style
), wxButton
*win
)
61 int bottom_border
= 0;
63 /* the default button has a border around it */
64 if (GTK_WIDGET_CAN_DEFAULT(m_widget
))
66 GtkBorder
*default_border
= NULL
;
67 gtk_widget_style_get( m_widget
, "default_border", &default_border
, NULL
);
70 left_border
+= default_border
->left
;
71 right_border
+= default_border
->right
;
72 top_border
+= default_border
->top
;
73 bottom_border
+= default_border
->bottom
;
74 g_free( default_border
);
77 win
->m_x
- top_border
,
78 win
->m_y
- left_border
,
79 win
->m_width
+ left_border
+ right_border
,
80 win
->m_height
+ top_border
+ bottom_border
);
86 //-----------------------------------------------------------------------------
88 //-----------------------------------------------------------------------------
90 IMPLEMENT_DYNAMIC_CLASS(wxButton
,wxControl
)
100 bool wxButton::Create(wxWindow
*parent
,
102 const wxString
&label
,
106 const wxValidator
& validator
,
107 const wxString
& name
)
109 if (!PreCreation( parent
, pos
, size
) ||
110 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
112 wxFAIL_MSG( wxT("wxButton creation failed") );
116 m_widget
= gtk_button_new_with_mnemonic("");
118 float x_alignment
= 0.5;
119 if (HasFlag(wxBU_LEFT
))
121 else if (HasFlag(wxBU_RIGHT
))
124 float y_alignment
= 0.5;
125 if (HasFlag(wxBU_TOP
))
127 else if (HasFlag(wxBU_BOTTOM
))
131 if (!gtk_check_version(2,4,0))
133 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
138 if (GTK_IS_MISC(GTK_BIN(m_widget
)->child
))
139 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(m_widget
)->child
),
140 x_alignment
, y_alignment
);
145 if (style
& wxNO_BORDER
)
146 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
148 g_signal_connect_after (m_widget
, "clicked",
149 G_CALLBACK (gtk_button_clicked_callback
),
152 g_signal_connect_after (m_widget
, "style_set",
153 G_CALLBACK (gtk_button_style_set_callback
),
156 m_parent
->DoAddChild( this );
164 wxWindow
*wxButton::SetDefault()
166 wxWindow
*oldDefault
= wxButtonBase::SetDefault();
168 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
169 gtk_widget_grab_default( m_widget
);
171 // resize for default border
172 gtk_button_style_set_callback( m_widget
, NULL
, this );
178 wxSize
wxButtonBase::GetDefaultSize()
180 static wxSize size
= wxDefaultSize
;
181 if (size
== wxDefaultSize
)
183 // NB: Default size of buttons should be same as size of stock
184 // buttons as used in most GTK+ apps. Unfortunately it's a little
185 // tricky to obtain this size: stock button's size may be smaller
186 // than size of button in GtkButtonBox and vice versa,
187 // GtkButtonBox's minimal button size may be smaller than stock
188 // button's size. We have to retrieve both values and combine them.
190 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
191 GtkWidget
*box
= gtk_hbutton_box_new();
192 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
193 gtk_container_add(GTK_CONTAINER(box
), btn
);
194 gtk_container_add(GTK_CONTAINER(wnd
), box
);
196 gtk_widget_size_request(btn
, &req
);
198 gint minwidth
, minheight
;
199 gtk_widget_style_get(box
,
200 "child-min-width", &minwidth
,
201 "child-min-height", &minheight
,
204 size
.x
= wxMax(minwidth
, req
.width
);
205 size
.y
= wxMax(minheight
, req
.height
);
207 gtk_widget_destroy(wnd
);
212 void wxButton::SetLabel( const wxString
&lbl
)
214 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
218 if (label
.empty() && wxIsStockID(m_windowId
))
219 label
= wxGetStockLabel(m_windowId
);
221 wxControl::SetLabel(label
);
223 const wxString labelGTK
= GTKConvertMnemonics(label
);
225 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
227 const char *stock
= wxGetStockGtkID(m_windowId
);
230 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
231 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
236 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
237 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
239 ApplyWidgetStyle( false );
242 bool wxButton::Enable( bool enable
)
244 if ( !wxControl::Enable( enable
) )
247 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
252 GdkWindow
*wxButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
254 return GTK_BUTTON(m_widget
)->event_window
;
257 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
259 gtk_widget_modify_style(m_widget
, style
);
260 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
263 wxSize
wxButton::DoGetBestSize() const
265 // the default button in wxGTK is bigger than the other ones because of an
266 // extra border around it, but we don't want to take it into account in
267 // our size calculations (otherwsie the result is visually ugly), so
268 // always return the size of non default button from here
269 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
272 // temporarily unset default flag
273 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
276 wxSize
ret( wxControl::DoGetBestSize() );
281 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
284 if (!HasFlag(wxBU_EXACTFIT
))
286 wxSize defaultSize
= GetDefaultSize();
287 if (ret
.x
< defaultSize
.x
) ret
.x
= defaultSize
.x
;
288 if (ret
.y
< defaultSize
.y
) ret
.y
= defaultSize
.y
;
297 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
299 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
302 #endif // wxUSE_BUTTON