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 );
111 m_widget
= gtk_button_new_with_label("");
113 float x_alignment
= 0.5;
114 if (HasFlag(wxBU_LEFT
))
116 else if (HasFlag(wxBU_RIGHT
))
119 float y_alignment
= 0.5;
120 if (HasFlag(wxBU_TOP
))
122 else if (HasFlag(wxBU_BOTTOM
))
125 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget
)),
126 x_alignment
, y_alignment
);
130 if (style
& wxNO_BORDER
)
131 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
133 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
134 GTK_SIGNAL_FUNC(gtk_button_clicked_callback
), (gpointer
*)this );
136 m_parent
->DoAddChild( this );
141 wxSize
best_size( DoGetBestSize() );
142 wxSize
new_size( size
);
143 if (new_size
.x
== -1)
144 new_size
.x
= best_size
.x
;
145 if (new_size
.y
== -1)
146 new_size
.y
= best_size
.y
;
147 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
148 SetSize( new_size
.x
, new_size
.y
);
157 void wxButton::SetDefault()
159 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
160 gtk_widget_grab_default( m_widget
);
162 SetSize( m_x
, m_y
, m_width
, m_height
);
166 wxSize
wxButton::GetDefaultSize()
168 return wxSize(80,26);
171 void wxButton::SetLabel( const wxString
&label
)
173 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
175 wxControl::SetLabel( label
);
178 wxString label2
= PrepareLabelMnemonics( label
);
179 gtk_label_set_text_with_mnemonic( GTK_LABEL( BUTTON_CHILD(m_widget
) ), wxGTK_CONV( label2
) );
181 gtk_label_set( GTK_LABEL( BUTTON_CHILD(m_widget
) ), wxGTK_CONV( GetLabel() ) );
185 bool wxButton::Enable( bool enable
)
187 if ( !wxControl::Enable( enable
) )
190 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget
), enable
);
195 bool wxButton::IsOwnGtkWindow( GdkWindow
*window
)
198 return GTK_BUTTON(m_widget
)->event_window
;
200 return (window
== m_widget
->window
);
204 void wxButton::ApplyWidgetStyle()
207 gtk_widget_set_style( m_widget
, m_widgetStyle
);
208 gtk_widget_set_style( BUTTON_CHILD(m_widget
), m_widgetStyle
);
211 wxSize
wxButton::DoGetBestSize() const
213 wxSize
ret( wxControl::DoGetBestSize() );
215 if (!HasFlag(wxBU_EXACTFIT
))
217 if (ret
.x
< 80) ret
.x
= 80;
223 #endif // wxUSE_BUTTON