1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "button.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
21 #include "wx/button.h"
22 #include "wx/stockitem.h"
24 #include "wx/gtk/private.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern void wxapp_install_idle_handler();
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 extern bool g_blockEventsOnDrag
;
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 static void gtk_button_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
52 wxapp_install_idle_handler();
54 if (!button
->m_hasVMT
) return;
55 if (g_blockEventsOnDrag
) return;
57 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
58 event
.SetEventObject(button
);
59 button
->GetEventHandler()->ProcessEvent(event
);
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
66 IMPLEMENT_DYNAMIC_CLASS(wxButton
,wxControl
)
76 bool wxButton::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&label
,
77 const wxPoint
&pos
, const wxSize
&size
,
78 long style
, const wxValidator
& validator
, const wxString
&name
)
81 m_acceptsFocus
= TRUE
;
83 if (!PreCreation( parent
, pos
, size
) ||
84 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
86 wxFAIL_MSG( wxT("wxButton creation failed") );
91 wxString label2( label );
92 for (size_t i = 0; i < label2.Len(); i++)
94 if (label2.GetChar(i) == wxT('&'))
95 label2.SetChar(i,wxT('_'));
98 GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() );
99 gtk_widget_show( accel_label );
101 m_widget = gtk_button_new();
102 gtk_container_add( GTK_CONTAINER(m_widget), accel_label );
104 gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget );
106 guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() );
107 gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) );
109 wxControl::SetLabel( label );
113 m_widget
= gtk_button_new_with_mnemonic("");
115 m_widget
= gtk_button_new_with_label("");
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(BUTTON_CHILD(m_widget
)))
139 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget
)),
140 x_alignment
, y_alignment
);
145 if (style
& wxNO_BORDER
)
146 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
148 gtk_signal_connect_after( GTK_OBJECT(m_widget
), "clicked",
149 GTK_SIGNAL_FUNC(gtk_button_clicked_callback
), (gpointer
*)this );
151 m_parent
->DoAddChild( this );
159 void wxButton::SetDefault()
161 wxWindow
*parent
= GetParent();
162 wxCHECK_RET( parent
, _T("button without parent?") );
164 parent
->SetDefaultItem(this);
166 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
167 gtk_widget_grab_default( m_widget
);
169 SetSize( m_x
, m_y
, m_width
, m_height
);
173 wxSize
wxButtonBase::GetDefaultSize()
176 static wxSize size
= wxDefaultSize
;
177 if (size
== wxDefaultSize
)
179 // NB: Default size of buttons should be same as size of stock
180 // buttons as used in most GTK+ apps. Unfortunately it's a little
181 // tricky to obtain this size: stock button's size may be smaller
182 // than size of button in GtkButtonBox and vice versa,
183 // GtkButtonBox's minimal button size may be smaller than stock
184 // button's size. We have to retrieve both values and combine them.
186 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
187 GtkWidget
*box
= gtk_hbutton_box_new();
188 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
189 gtk_container_add(GTK_CONTAINER(box
), btn
);
190 gtk_container_add(GTK_CONTAINER(wnd
), box
);
192 gtk_widget_size_request(btn
, &req
);
194 gint minwidth
, minheight
;
195 gtk_widget_style_get(box
,
196 "child-min-width", &minwidth
,
197 "child-min-height", &minheight
,
200 size
.x
= wxMax(minwidth
, req
.width
);
201 size
.y
= wxMax(minheight
, req
.height
);
203 gtk_widget_destroy(wnd
);
207 return wxSize(80,26);
211 void wxButton::SetLabel( const wxString
&lbl
)
213 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
217 if (label
.empty() && wxIsStockID(m_windowId
))
218 label
= wxGetStockLabel(m_windowId
);
220 wxControl::SetLabel(label
);
223 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
225 const char *stock
= wxGetStockGtkID(m_windowId
);
228 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
229 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
234 wxString label2
= PrepareLabelMnemonics(label
);
235 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(label2
));
236 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
238 ApplyWidgetStyle( false );
241 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget
)), wxGTK_CONV(GetLabel()));
245 bool wxButton::Enable( bool enable
)
247 if ( !wxControl::Enable( enable
) )
250 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget
), enable
);
255 bool wxButton::IsOwnGtkWindow( GdkWindow
*window
)
258 return GTK_BUTTON(m_widget
)->event_window
;
260 return (window
== m_widget
->window
);
264 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
266 gtk_widget_modify_style(m_widget
, style
);
267 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
270 wxSize
wxButton::DoGetBestSize() const
272 // the default button in wxGTK is bigger than the other ones because of an
273 // extra border around it, but we don't want to take it into account in
274 // our size calculations (otherwsie the result is visually ugly), so
275 // always return the size of non default button from here
276 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
279 // temporarily unset default flag
280 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
283 wxSize
ret( wxControl::DoGetBestSize() );
288 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
292 ret
.x
+= 10; // add a few pixels for sloppy (but common) themes
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