1 /////////////////////////////////////////////////////////////////////////////
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"
17 #include "wx/button.h"
18 #include "wx/stockitem.h"
20 #include "wx/gtk1/private.h"
21 #include "wx/gtk1/win_gtk.h"
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern void wxapp_install_idle_handler();
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern bool g_blockEventsOnDrag
;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
47 static void gtk_button_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
50 wxapp_install_idle_handler();
52 if (!button
->m_hasVMT
) return;
53 if (g_blockEventsOnDrag
) return;
55 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
56 event
.SetEventObject(button
);
57 button
->GetEventHandler()->ProcessEvent(event
);
61 //-----------------------------------------------------------------------------
62 // "style_set" from m_widget
63 //-----------------------------------------------------------------------------
66 gtk_button_style_set_callback( GtkWidget
*m_widget
, GtkStyle
*WXUNUSED(style
), wxButton
*win
)
69 wxapp_install_idle_handler();
74 int bottom_border
= 0;
76 /* the default button has a border around it */
77 if (GTK_WIDGET_CAN_DEFAULT(m_widget
))
83 win
->DoMoveWindow( win
->m_x
-top_border
,
85 win
->m_width
+left_border
+right_border
,
86 win
->m_height
+top_border
+bottom_border
);
92 //-----------------------------------------------------------------------------
94 //-----------------------------------------------------------------------------
96 IMPLEMENT_DYNAMIC_CLASS(wxButton
,wxControl
)
102 wxButton::~wxButton()
106 bool wxButton::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&label
,
107 const wxPoint
&pos
, const wxSize
&size
,
108 long style
, const wxValidator
& validator
, const wxString
&name
)
111 m_acceptsFocus
= TRUE
;
113 if (!PreCreation( parent
, pos
, size
) ||
114 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
116 wxFAIL_MSG( wxT("wxButton creation failed") );
120 m_widget
= gtk_button_new_with_label("");
122 float x_alignment
= 0.5;
123 if (HasFlag(wxBU_LEFT
))
125 else if (HasFlag(wxBU_RIGHT
))
128 float y_alignment
= 0.5;
129 if (HasFlag(wxBU_TOP
))
131 else if (HasFlag(wxBU_BOTTOM
))
134 if (GTK_IS_MISC(BUTTON_CHILD(m_widget
)))
135 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget
)),
136 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_after( GTK_OBJECT(m_widget
), "clicked",
144 GTK_SIGNAL_FUNC(gtk_button_clicked_callback
), (gpointer
*)this );
146 gtk_signal_connect_after( GTK_OBJECT(m_widget
), "style_set",
147 GTK_SIGNAL_FUNC(gtk_button_style_set_callback
), (gpointer
*) this );
149 m_parent
->DoAddChild( this );
157 void wxButton::SetDefault()
159 wxWindow
*parent
= GetParent();
160 wxCHECK_RET( parent
, _T("button without parent?") );
162 parent
->SetDefaultItem(this);
164 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
165 gtk_widget_grab_default( m_widget
);
167 // resize for default border
168 gtk_button_style_set_callback( m_widget
, NULL
, this );
172 wxSize
wxButtonBase::GetDefaultSize()
174 return wxSize(80,26);
177 void wxButton::SetLabel( const wxString
&lbl
)
179 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
183 if (label
.empty() && wxIsStockID(m_windowId
))
184 label
= wxGetStockLabel(m_windowId
);
186 wxControl::SetLabel(label
);
188 const wxString labelGTK
= GTKRemoveMnemonics(label
);
190 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget
)), wxGTK_CONV(labelGTK
));
193 bool wxButton::Enable( bool enable
)
195 if ( !wxControl::Enable( enable
) )
198 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget
), enable
);
203 bool wxButton::IsOwnGtkWindow( GdkWindow
*window
)
205 return (window
== m_widget
->window
);
208 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
210 gtk_widget_modify_style(m_widget
, style
);
211 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
214 wxSize
wxButton::DoGetBestSize() const
216 // the default button in wxGTK is bigger than the other ones because of an
217 // extra border around it, but we don't want to take it into account in
218 // our size calculations (otherwsie the result is visually ugly), so
219 // always return the size of non default button from here
220 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
223 // temporarily unset default flag
224 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
227 wxSize
ret( wxControl::DoGetBestSize() );
232 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
235 ret
.x
+= 10; // add a few pixels for sloppy (but common) themes
237 if (!HasFlag(wxBU_EXACTFIT
))
239 wxSize defaultSize
= GetDefaultSize();
240 if (ret
.x
< defaultSize
.x
) ret
.x
= defaultSize
.x
;
241 if (ret
.y
< defaultSize
.y
) ret
.y
= defaultSize
.y
;
250 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
252 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
255 #endif // wxUSE_BUTTON