]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/button.cpp
fix setting the font for a single-line text control (bug 508169)
[wxWidgets.git] / src / gtk / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: button.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "button.h"
12 #endif
13
14 #include "wx/defs.h"
15
16 #if wxUSE_BUTTON
17
18 #include "wx/button.h"
19
20 #include <gdk/gdk.h>
21 #include <gtk/gtk.h>
22
23 //-----------------------------------------------------------------------------
24 // classes
25 //-----------------------------------------------------------------------------
26
27 class wxButton;
28
29 //-----------------------------------------------------------------------------
30 // idle system
31 //-----------------------------------------------------------------------------
32
33 extern void wxapp_install_idle_handler();
34 extern bool g_isIdle;
35
36 //-----------------------------------------------------------------------------
37 // data
38 //-----------------------------------------------------------------------------
39
40 extern bool g_blockEventsOnDrag;
41
42 //-----------------------------------------------------------------------------
43 // "clicked"
44 //-----------------------------------------------------------------------------
45
46 static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button )
47 {
48 if (g_isIdle)
49 wxapp_install_idle_handler();
50
51 if (!button->m_hasVMT) return;
52 if (g_blockEventsOnDrag) return;
53
54 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
55 event.SetEventObject(button);
56 button->GetEventHandler()->ProcessEvent(event);
57 }
58
59 //-----------------------------------------------------------------------------
60 // wxButton
61 //-----------------------------------------------------------------------------
62
63 IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
64
65 wxButton::wxButton()
66 {
67 }
68
69 wxButton::~wxButton()
70 {
71 }
72
73 bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label,
74 const wxPoint &pos, const wxSize &size,
75 long style, const wxValidator& validator, const wxString &name )
76 {
77 m_needParent = TRUE;
78 m_acceptsFocus = TRUE;
79
80 if (!PreCreation( parent, pos, size ) ||
81 !CreateBase( parent, id, pos, size, style, validator, name ))
82 {
83 wxFAIL_MSG( wxT("wxButton creation failed") );
84 return FALSE;
85 }
86
87 /*
88 wxString label2( label );
89 for (size_t i = 0; i < label2.Len(); i++)
90 {
91 if (label2.GetChar(i) == wxT('&'))
92 label2.SetChar(i,wxT('_'));
93 }
94
95 GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() );
96 gtk_widget_show( accel_label );
97
98 m_widget = gtk_button_new();
99 gtk_container_add( GTK_CONTAINER(m_widget), accel_label );
100
101 gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget );
102
103 guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() );
104 gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) );
105
106 wxControl::SetLabel( label );
107 */
108
109 m_widget = gtk_button_new_with_label("");
110
111 SetLabel( label );
112
113 #if (GTK_MINOR_VERSION > 0)
114 if (style & wxNO_BORDER)
115 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
116 #endif
117
118 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
119 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
120
121 m_parent->DoAddChild( this );
122
123 PostCreation();
124
125 SetFont( parent->GetFont() );
126
127 wxSize best_size( DoGetBestSize() );
128 wxSize new_size( size );
129 if (new_size.x == -1)
130 new_size.x = best_size.x;
131 if (new_size.y == -1)
132 new_size.y = best_size.y;
133 if ((new_size.x != size.x) || (new_size.y != size.y))
134 SetSize( new_size.x, new_size.y );
135
136 SetSize( new_size );
137
138 SetBackgroundColour( parent->GetBackgroundColour() );
139 SetForegroundColour( parent->GetForegroundColour() );
140
141 Show( TRUE );
142
143 return TRUE;
144 }
145
146 void wxButton::SetDefault()
147 {
148 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
149 gtk_widget_grab_default( m_widget );
150
151 SetSize( m_x, m_y, m_width, m_height );
152 }
153
154 /* static */
155 wxSize wxButton::GetDefaultSize()
156 {
157 return wxSize(80,26);
158 }
159
160 void wxButton::SetLabel( const wxString &label )
161 {
162 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
163
164 wxControl::SetLabel( label );
165
166 gtk_label_set( GTK_LABEL( GTK_BUTTON(m_widget)->child ), GetLabel().mbc_str() );
167 }
168
169 bool wxButton::Enable( bool enable )
170 {
171 if ( !wxControl::Enable( enable ) )
172 return FALSE;
173
174 gtk_widget_set_sensitive( GTK_BUTTON(m_widget)->child, enable );
175
176 return TRUE;
177 }
178
179 void wxButton::ApplyWidgetStyle()
180 {
181 SetWidgetStyle();
182 gtk_widget_set_style( m_widget, m_widgetStyle );
183 gtk_widget_set_style( GTK_BUTTON(m_widget)->child, m_widgetStyle );
184 }
185
186 wxSize wxButton::DoGetBestSize() const
187 {
188 wxSize ret( wxControl::DoGetBestSize() );
189
190 if (!HasFlag(wxBU_EXACTFIT))
191 {
192 if (ret.x < 80) ret.x = 80;
193 }
194
195 return ret;
196 }
197
198 #endif // wxUSE_BUTTON
199