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"
23 #include "wx/gtk/private.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern void wxapp_install_idle_handler();
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 extern bool g_blockEventsOnDrag
;
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 static void gtk_button_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
51 wxapp_install_idle_handler();
53 if (!button
->m_hasVMT
) return;
54 if (g_blockEventsOnDrag
) return;
56 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
57 event
.SetEventObject(button
);
58 button
->GetEventHandler()->ProcessEvent(event
);
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 IMPLEMENT_DYNAMIC_CLASS(wxButton
,wxControl
)
75 bool wxButton::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&label
,
76 const wxPoint
&pos
, const wxSize
&size
,
77 long style
, const wxValidator
& validator
, const wxString
&name
)
80 m_acceptsFocus
= TRUE
;
82 if (!PreCreation( parent
, pos
, size
) ||
83 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
85 wxFAIL_MSG( wxT("wxButton creation failed") );
90 wxString label2( label );
91 for (size_t i = 0; i < label2.Len(); i++)
93 if (label2.GetChar(i) == wxT('&'))
94 label2.SetChar(i,wxT('_'));
97 GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() );
98 gtk_widget_show( accel_label );
100 m_widget = gtk_button_new();
101 gtk_container_add( GTK_CONTAINER(m_widget), accel_label );
103 gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget );
105 guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() );
106 gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) );
108 wxControl::SetLabel( label );
112 if (m_widget
== NULL
) // may be !=NULL if stock form of Create was used
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 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
133 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget
)),
134 x_alignment
, y_alignment
);
140 if (style
& wxNO_BORDER
)
141 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
143 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
144 GTK_SIGNAL_FUNC(gtk_button_clicked_callback
), (gpointer
*)this );
146 m_parent
->DoAddChild( this );
153 bool wxButton::Create(wxWindow
*parent
, wxWindowID id
, wxStockItemID stock
,
154 const wxString
& descriptiveLabel
,
155 const wxPoint
& pos
, long style
,
156 const wxValidator
& validator
, const wxString
& name
)
159 m_widget
= gtk_button_new_from_stock(wxStockItemToGTK(stock
));
160 return Create(parent
, id
, wxEmptyString
,
161 pos
, wxDefaultSize
, style
, validator
, name
);
163 return CreateStock(parent
, id
, stock
, descriptiveLabel
,
164 pos
, style
, validator
, name
);
168 void wxButton::SetDefault()
170 wxWindow
*parent
= GetParent();
171 wxCHECK_RET( parent
, _T("button without parent?") );
173 parent
->SetDefaultItem(this);
175 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
176 gtk_widget_grab_default( m_widget
);
178 SetSize( m_x
, m_y
, m_width
, m_height
);
182 wxSize
wxButtonBase::GetDefaultSize()
185 static wxSize size
= wxDefaultSize
;
186 if (size
== wxDefaultSize
)
188 // NB: Default size of buttons should be same as size of stock
189 // buttons as used in most GTK+ apps. Unfortunately it's a little
190 // tricky to obtain this size: stock button's size may be smaller
191 // than size of button in GtkButtonBox and vice versa,
192 // GtkButtonBox's minimal button size may be smaller than stock
193 // button's size. We have to retrieve both values and combine them.
195 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
196 GtkWidget
*box
= gtk_hbutton_box_new();
197 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
198 gtk_container_add(GTK_CONTAINER(box
), btn
);
199 gtk_container_add(GTK_CONTAINER(wnd
), box
);
201 gtk_widget_size_request(btn
, &req
);
203 gint minwidth
, minheight
;
204 gtk_widget_style_get(box
,
205 "child-min-width", &minwidth
,
206 "child-min-height", &minheight
,
209 size
.x
= wxMax(minwidth
, req
.width
);
210 size
.y
= wxMax(minheight
, req
.height
);
212 gtk_widget_destroy(wnd
);
216 return wxSize(80,26);
220 void wxButton::SetLabel( const wxString
&label
)
222 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
224 wxControl::SetLabel( label
);
227 wxString label2
= PrepareLabelMnemonics( label
);
228 gtk_button_set_label( GTK_BUTTON(m_widget
), wxGTK_CONV(label2
) );
230 gtk_label_set( GTK_LABEL( BUTTON_CHILD(m_widget
) ), wxGTK_CONV( GetLabel() ) );
234 bool wxButton::Enable( bool enable
)
236 if ( !wxControl::Enable( enable
) )
239 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget
), enable
);
244 bool wxButton::IsOwnGtkWindow( GdkWindow
*window
)
247 return GTK_BUTTON(m_widget
)->event_window
;
249 return (window
== m_widget
->window
);
253 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
255 gtk_widget_modify_style(m_widget
, style
);
256 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
259 wxSize
wxButton::DoGetBestSize() const
261 // the default button in wxGTK is bigger than the other ones because of an
262 // extra border around it, but we don't want to take it into account in
263 // our size calculations (otherwsie the result is visually ugly), so
264 // always return the size of non default button from here
265 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
268 // temporarily unset default flag
269 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
272 wxSize
ret( wxControl::DoGetBestSize() );
277 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
281 ret
.x
+= 10; // add a few pixels for sloppy (but common) themes
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