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"
15 #include "wx/button.h"
16 #include "wx/stockitem.h"
18 #include "wx/gtk/private.h"
19 #include "wx/gtk/win_gtk.h"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern bool g_blockEventsOnDrag
;
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
38 static void gtk_button_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
41 wxapp_install_idle_handler();
43 if (!button
->m_hasVMT
) return;
44 if (g_blockEventsOnDrag
) return;
46 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
47 event
.SetEventObject(button
);
48 button
->GetEventHandler()->ProcessEvent(event
);
52 //-----------------------------------------------------------------------------
53 // "style_set" from m_widget
54 //-----------------------------------------------------------------------------
57 gtk_button_style_set_callback( GtkWidget
*m_widget
, GtkStyle
*WXUNUSED(style
), wxButton
*win
)
60 wxapp_install_idle_handler();
65 int bottom_border
= 0;
67 /* the default button has a border around it */
68 if (GTK_WIDGET_CAN_DEFAULT(m_widget
))
70 GtkBorder
*default_border
= NULL
;
71 gtk_widget_style_get( m_widget
, "default_border", &default_border
, NULL
);
74 left_border
+= default_border
->left
;
75 right_border
+= default_border
->right
;
76 top_border
+= default_border
->top
;
77 bottom_border
+= default_border
->bottom
;
78 g_free( default_border
);
81 win
->m_x
- top_border
,
82 win
->m_y
- left_border
,
83 win
->m_width
+ left_border
+ right_border
,
84 win
->m_height
+ top_border
+ bottom_border
);
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
94 IMPLEMENT_DYNAMIC_CLASS(wxButton
,wxControl
)
100 wxButton::~wxButton()
104 bool wxButton::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&label
,
105 const wxPoint
&pos
, const wxSize
&size
,
106 long style
, const wxValidator
& validator
, const wxString
&name
)
109 m_acceptsFocus
= true;
111 if (!PreCreation( parent
, pos
, size
) ||
112 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
114 wxFAIL_MSG( wxT("wxButton creation failed") );
118 m_widget
= gtk_button_new_with_mnemonic("");
120 float x_alignment
= 0.5;
121 if (HasFlag(wxBU_LEFT
))
123 else if (HasFlag(wxBU_RIGHT
))
126 float y_alignment
= 0.5;
127 if (HasFlag(wxBU_TOP
))
129 else if (HasFlag(wxBU_BOTTOM
))
133 if (!gtk_check_version(2,4,0))
135 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
140 if (GTK_IS_MISC(GTK_BIN(m_widget
)->child
))
141 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(m_widget
)->child
),
142 x_alignment
, y_alignment
);
147 if (style
& wxNO_BORDER
)
148 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
150 g_signal_connect_after (m_widget
, "clicked",
151 G_CALLBACK (gtk_button_clicked_callback
),
154 g_signal_connect_after (m_widget
, "style_set",
155 G_CALLBACK (gtk_button_style_set_callback
),
158 m_parent
->DoAddChild( this );
166 void wxButton::SetDefault()
168 wxWindow
*parent
= GetParent();
169 wxCHECK_RET( parent
, _T("button without parent?") );
171 parent
->SetDefaultItem(this);
173 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
174 gtk_widget_grab_default( m_widget
);
176 // resize for default border
177 gtk_button_style_set_callback( m_widget
, NULL
, this );
181 wxSize
wxButtonBase::GetDefaultSize()
183 static wxSize size
= wxDefaultSize
;
184 if (size
== wxDefaultSize
)
186 // NB: Default size of buttons should be same as size of stock
187 // buttons as used in most GTK+ apps. Unfortunately it's a little
188 // tricky to obtain this size: stock button's size may be smaller
189 // than size of button in GtkButtonBox and vice versa,
190 // GtkButtonBox's minimal button size may be smaller than stock
191 // button's size. We have to retrieve both values and combine them.
193 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
194 GtkWidget
*box
= gtk_hbutton_box_new();
195 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
196 gtk_container_add(GTK_CONTAINER(box
), btn
);
197 gtk_container_add(GTK_CONTAINER(wnd
), box
);
199 gtk_widget_size_request(btn
, &req
);
201 gint minwidth
, minheight
;
202 gtk_widget_style_get(box
,
203 "child-min-width", &minwidth
,
204 "child-min-height", &minheight
,
207 size
.x
= wxMax(minwidth
, req
.width
);
208 size
.y
= wxMax(minheight
, req
.height
);
210 gtk_widget_destroy(wnd
);
215 void wxButton::SetLabel( const wxString
&lbl
)
217 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
221 if (label
.empty() && wxIsStockID(m_windowId
))
222 label
= wxGetStockLabel(m_windowId
);
224 wxControl::SetLabel(label
);
226 const wxString labelGTK
= GTKConvertMnemonics(label
);
228 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
230 const char *stock
= wxGetStockGtkID(m_windowId
);
233 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
234 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
239 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
240 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
242 ApplyWidgetStyle( false );
245 bool wxButton::Enable( bool enable
)
247 if ( !wxControl::Enable( enable
) )
250 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
255 bool wxButton::IsOwnGtkWindow( GdkWindow
*window
)
257 return GTK_BUTTON(m_widget
)->event_window
;
260 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
262 gtk_widget_modify_style(m_widget
, style
);
263 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
266 wxSize
wxButton::DoGetBestSize() const
268 // the default button in wxGTK is bigger than the other ones because of an
269 // extra border around it, but we don't want to take it into account in
270 // our size calculations (otherwsie the result is visually ugly), so
271 // always return the size of non default button from here
272 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
275 // temporarily unset default flag
276 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
279 wxSize
ret( wxControl::DoGetBestSize() );
284 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
287 if (!HasFlag(wxBU_EXACTFIT
))
289 wxSize defaultSize
= GetDefaultSize();
290 if (ret
.x
< defaultSize
.x
) ret
.x
= defaultSize
.x
;
291 if (ret
.y
< defaultSize
.y
) ret
.y
= defaultSize
.y
;
300 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
302 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
305 #endif // wxUSE_BUTTON