1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "button.h"
18 #include "wx/button.h"
20 #include "wx/gtk/private.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern void wxapp_install_idle_handler();
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 extern bool g_blockEventsOnDrag
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 static void gtk_button_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
48 wxapp_install_idle_handler();
50 if (!button
->m_hasVMT
) return;
51 if (g_blockEventsOnDrag
) return;
53 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
54 event
.SetEventObject(button
);
55 button
->GetEventHandler()->ProcessEvent(event
);
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 IMPLEMENT_DYNAMIC_CLASS(wxButton
,wxControl
)
72 bool wxButton::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&label
,
73 const wxPoint
&pos
, const wxSize
&size
,
74 long style
, const wxValidator
& validator
, const wxString
&name
)
77 m_acceptsFocus
= TRUE
;
79 if (!PreCreation( parent
, pos
, size
) ||
80 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
82 wxFAIL_MSG( wxT("wxButton creation failed") );
87 wxString label2( label );
88 for (size_t i = 0; i < label2.Len(); i++)
90 if (label2.GetChar(i) == wxT('&'))
91 label2.SetChar(i,wxT('_'));
94 GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() );
95 gtk_widget_show( accel_label );
97 m_widget = gtk_button_new();
98 gtk_container_add( GTK_CONTAINER(m_widget), accel_label );
100 gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget );
102 guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() );
103 gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) );
105 wxControl::SetLabel( label );
108 m_widget
= gtk_button_new_with_label("");
110 float x_alignment
= 0.5;
111 if (HasFlag(wxBU_LEFT
))
113 else if (HasFlag(wxBU_RIGHT
))
116 float y_alignment
= 0.5;
117 if (HasFlag(wxBU_TOP
))
119 else if (HasFlag(wxBU_BOTTOM
))
122 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget
)),
123 x_alignment
, y_alignment
);
127 if (style
& wxNO_BORDER
)
128 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
130 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
131 GTK_SIGNAL_FUNC(gtk_button_clicked_callback
), (gpointer
*)this );
133 m_parent
->DoAddChild( this );
137 SetFont( parent
->GetFont() );
139 wxSize
best_size( DoGetBestSize() );
140 wxSize
new_size( size
);
141 if (new_size
.x
== -1)
142 new_size
.x
= best_size
.x
;
143 if (new_size
.y
== -1)
144 new_size
.y
= best_size
.y
;
145 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
146 SetSize( new_size
.x
, new_size
.y
);
150 SetBackgroundColour( parent
->GetBackgroundColour() );
151 SetForegroundColour( parent
->GetForegroundColour() );
158 void wxButton::SetDefault()
160 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
161 gtk_widget_grab_default( m_widget
);
163 SetSize( m_x
, m_y
, m_width
, m_height
);
167 wxSize
wxButton::GetDefaultSize()
169 return wxSize(80,26);
172 void wxButton::SetLabel( const wxString
&label
)
174 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
176 wxControl::SetLabel( label
);
178 gtk_label_set( GTK_LABEL( BUTTON_CHILD(m_widget
) ), wxGTK_CONV( GetLabel() ) );
181 bool wxButton::Enable( bool enable
)
183 if ( !wxControl::Enable( enable
) )
186 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget
), enable
);
191 bool wxButton::IsOwnGtkWindow( GdkWindow
*window
)
194 return GTK_BUTTON(m_widget
)->event_window
;
196 return (window
== m_widget
->window
);
200 void wxButton::ApplyWidgetStyle()
203 gtk_widget_set_style( m_widget
, m_widgetStyle
);
204 gtk_widget_set_style( BUTTON_CHILD(m_widget
), m_widgetStyle
);
207 wxSize
wxButton::DoGetBestSize() const
209 wxSize
ret( wxControl::DoGetBestSize() );
211 if (!HasFlag(wxBU_EXACTFIT
))
213 if (ret
.x
< 80) ret
.x
= 80;
219 #endif // wxUSE_BUTTON