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 gtk_border_free( default_border
);
77 win
->m_x
- left_border
,
78 win
->m_y
- top_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
))
130 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
134 if (style
& wxNO_BORDER
)
135 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
137 g_signal_connect_after (m_widget
, "clicked",
138 G_CALLBACK (gtk_button_clicked_callback
),
141 g_signal_connect_after (m_widget
, "style_set",
142 G_CALLBACK (gtk_button_style_set_callback
),
145 m_parent
->DoAddChild( this );
153 wxWindow
*wxButton::SetDefault()
155 wxWindow
*oldDefault
= wxButtonBase::SetDefault();
157 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
158 gtk_widget_grab_default( m_widget
);
160 // resize for default border
161 gtk_button_style_set_callback( m_widget
, NULL
, this );
167 wxSize
wxButtonBase::GetDefaultSize()
169 static wxSize size
= wxDefaultSize
;
170 if (size
== wxDefaultSize
)
172 // NB: Default size of buttons should be same as size of stock
173 // buttons as used in most GTK+ apps. Unfortunately it's a little
174 // tricky to obtain this size: stock button's size may be smaller
175 // than size of button in GtkButtonBox and vice versa,
176 // GtkButtonBox's minimal button size may be smaller than stock
177 // button's size. We have to retrieve both values and combine them.
179 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
180 GtkWidget
*box
= gtk_hbutton_box_new();
181 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
182 gtk_container_add(GTK_CONTAINER(box
), btn
);
183 gtk_container_add(GTK_CONTAINER(wnd
), box
);
185 gtk_widget_size_request(btn
, &req
);
187 gint minwidth
, minheight
;
188 gtk_widget_style_get(box
,
189 "child-min-width", &minwidth
,
190 "child-min-height", &minheight
,
193 size
.x
= wxMax(minwidth
, req
.width
);
194 size
.y
= wxMax(minheight
, req
.height
);
196 gtk_widget_destroy(wnd
);
201 void wxButton::SetLabel( const wxString
&lbl
)
203 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
207 if (label
.empty() && wxIsStockID(m_windowId
))
208 label
= wxGetStockLabel(m_windowId
);
210 wxControl::SetLabel(label
);
212 const wxString labelGTK
= GTKConvertMnemonics(label
);
214 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
216 const char *stock
= wxGetStockGtkID(m_windowId
);
219 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
220 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
225 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
226 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
228 ApplyWidgetStyle( false );
231 bool wxButton::Enable( bool enable
)
233 if ( !wxControl::Enable( enable
) )
236 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
241 GdkWindow
*wxButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
243 return GTK_BUTTON(m_widget
)->event_window
;
246 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
248 gtk_widget_modify_style(m_widget
, style
);
249 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
252 wxSize
wxButton::DoGetBestSize() const
254 // the default button in wxGTK is bigger than the other ones because of an
255 // extra border around it, but we don't want to take it into account in
256 // our size calculations (otherwsie the result is visually ugly), so
257 // always return the size of non default button from here
258 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
261 // temporarily unset default flag
262 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
265 wxSize
ret( wxControl::DoGetBestSize() );
270 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
273 if (!HasFlag(wxBU_EXACTFIT
))
275 wxSize defaultSize
= GetDefaultSize();
276 if (ret
.x
< defaultSize
.x
) ret
.x
= defaultSize
.x
;
277 if (ret
.y
< defaultSize
.y
) ret
.y
= defaultSize
.y
;
286 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
288 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
291 #endif // wxUSE_BUTTON