1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "button.h"
14 #include "wx/button.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 static void gtk_button_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
45 wxapp_install_idle_handler();
47 if (!button
->m_hasVMT
) return;
48 if (g_blockEventsOnDrag
) return;
50 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
51 event
.SetEventObject(button
);
52 button
->GetEventHandler()->ProcessEvent(event
);
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 IMPLEMENT_DYNAMIC_CLASS(wxButton
,wxControl
)
69 bool wxButton::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&label
,
70 const wxPoint
&pos
, const wxSize
&size
,
71 long style
, const wxValidator
& validator
, const wxString
&name
)
74 m_acceptsFocus
= TRUE
;
76 if (!PreCreation( parent
, pos
, size
) ||
77 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
79 wxFAIL_MSG( wxT("wxButton creation failed") );
84 wxString label2( label );
85 for (size_t i = 0; i < label2.Len(); i++)
87 if (label2.GetChar(i) == wxT('&'))
88 label2.SetChar(i,wxT('_'));
91 GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() );
92 gtk_widget_show( accel_label );
94 m_widget = gtk_button_new();
95 gtk_container_add( GTK_CONTAINER(m_widget), accel_label );
97 gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget );
99 guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() );
100 gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) );
102 wxControl::SetLabel( label );
105 m_widget
= gtk_button_new_with_label("");
109 #if (GTK_MINOR_VERSION > 0)
110 if (style
& wxNO_BORDER
)
111 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
114 int x
= 0; int y
= 0;
115 wxFont
new_font( parent
->GetFont() );
116 GetTextExtent( m_label
, &x
, &y
, (int*)NULL
, (int*)NULL
, &new_font
);
118 wxSize newSize
= size
;
122 if (newSize
.x
< 80) newSize
.x
= 80;
127 if (newSize
.y
< 26) newSize
.y
= 26;
130 SetSize( newSize
.x
, newSize
.y
);
132 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
133 GTK_SIGNAL_FUNC(gtk_button_clicked_callback
), (gpointer
*)this );
135 m_parent
->DoAddChild( this );
139 SetBackgroundColour( parent
->GetBackgroundColour() );
140 SetForegroundColour( parent
->GetForegroundColour() );
141 SetFont( parent
->GetFont() );
148 void wxButton::SetDefault()
150 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
151 gtk_widget_grab_default( m_widget
);
153 SetSize( m_x
, m_y
, m_width
, m_height
);
157 wxSize
wxButton::GetDefaultSize()
159 return wxSize(80,26);
162 void wxButton::SetLabel( const wxString
&label
)
164 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
166 wxControl::SetLabel( label
);
168 gtk_label_set( GTK_LABEL( GTK_BUTTON(m_widget
)->child
), GetLabel().mbc_str() );
171 bool wxButton::Enable( bool enable
)
173 if ( !wxControl::Enable( enable
) )
176 gtk_widget_set_sensitive( GTK_BUTTON(m_widget
)->child
, enable
);
181 void wxButton::ApplyWidgetStyle()
184 gtk_widget_set_style( m_widget
, m_widgetStyle
);
185 gtk_widget_set_style( GTK_BUTTON(m_widget
)->child
, m_widgetStyle
);