1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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"
18 #include "wx/toplevel.h"
21 #include "wx/stockitem.h"
23 #include "wx/gtk1/private.h"
24 #include "wx/gtk1/win_gtk.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern void wxapp_install_idle_handler();
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 extern bool g_blockEventsOnDrag
;
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
50 static void gtk_button_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
53 wxapp_install_idle_handler();
55 if (!button
->m_hasVMT
) return;
56 if (g_blockEventsOnDrag
) return;
58 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
59 event
.SetEventObject(button
);
60 button
->GetEventHandler()->ProcessEvent(event
);
64 //-----------------------------------------------------------------------------
65 // "style_set" from m_widget
66 //-----------------------------------------------------------------------------
69 gtk_button_style_set_callback( GtkWidget
*m_widget
, GtkStyle
*WXUNUSED(style
), wxButton
*win
)
72 wxapp_install_idle_handler();
77 int bottom_border
= 0;
79 /* the default button has a border around it */
80 if (GTK_WIDGET_CAN_DEFAULT(m_widget
))
86 win
->DoMoveWindow( win
->m_x
-top_border
,
88 win
->m_width
+left_border
+right_border
,
89 win
->m_height
+top_border
+bottom_border
);
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
99 IMPLEMENT_DYNAMIC_CLASS(wxButton
,wxControl
)
105 wxButton::~wxButton()
109 bool wxButton::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&label
,
110 const wxPoint
&pos
, const wxSize
&size
,
111 long style
, const wxValidator
& validator
, const wxString
&name
)
114 m_acceptsFocus
= true;
116 if (!PreCreation( parent
, pos
, size
) ||
117 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
119 wxFAIL_MSG( wxT("wxButton creation failed") );
123 m_widget
= gtk_button_new_with_label("");
125 float x_alignment
= 0.5;
126 if (HasFlag(wxBU_LEFT
))
128 else if (HasFlag(wxBU_RIGHT
))
131 float y_alignment
= 0.5;
132 if (HasFlag(wxBU_TOP
))
134 else if (HasFlag(wxBU_BOTTOM
))
137 if (GTK_IS_MISC(BUTTON_CHILD(m_widget
)))
138 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget
)),
139 x_alignment
, y_alignment
);
143 if (style
& wxNO_BORDER
)
144 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
146 gtk_signal_connect_after( GTK_OBJECT(m_widget
), "clicked",
147 GTK_SIGNAL_FUNC(gtk_button_clicked_callback
), (gpointer
*)this );
149 gtk_signal_connect_after( GTK_OBJECT(m_widget
), "style_set",
150 GTK_SIGNAL_FUNC(gtk_button_style_set_callback
), (gpointer
*) this );
152 m_parent
->DoAddChild( this );
160 void wxButton::SetDefault()
162 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
163 wxCHECK_RET( tlw
, _T("button without top level window?") );
165 tlw
->SetDefaultItem(this);
167 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
168 gtk_widget_grab_default( m_widget
);
170 // resize for default border
171 gtk_button_style_set_callback( m_widget
, NULL
, this );
175 wxSize
wxButtonBase::GetDefaultSize()
177 return wxSize(80,26);
180 void wxButton::SetLabel( const wxString
&lbl
)
182 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
186 if (label
.empty() && wxIsStockID(m_windowId
))
187 label
= wxGetStockLabel(m_windowId
);
189 wxControl::SetLabel(label
);
191 const wxString labelGTK
= GTKRemoveMnemonics(label
);
193 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget
)), wxGTK_CONV(labelGTK
));
196 bool wxButton::Enable( bool enable
)
198 if ( !wxControl::Enable( enable
) )
201 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget
), enable
);
206 bool wxButton::IsOwnGtkWindow( GdkWindow
*window
)
208 return (window
== m_widget
->window
);
211 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
213 gtk_widget_modify_style(m_widget
, style
);
214 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
217 wxSize
wxButton::DoGetBestSize() const
219 // the default button in wxGTK is bigger than the other ones because of an
220 // extra border around it, but we don't want to take it into account in
221 // our size calculations (otherwsie the result is visually ugly), so
222 // always return the size of non default button from here
223 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
226 // temporarily unset default flag
227 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
230 wxSize
ret( wxControl::DoGetBestSize() );
235 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
238 ret
.x
+= 10; // add a few pixels for sloppy (but common) themes
240 if (!HasFlag(wxBU_EXACTFIT
))
242 wxSize defaultSize
= GetDefaultSize();
243 if (ret
.x
< defaultSize
.x
) ret
.x
= defaultSize
.x
;
244 if (ret
.y
< defaultSize
.y
) ret
.y
= defaultSize
.y
;
253 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
255 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
258 #endif // wxUSE_BUTTON