]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/button.cpp
testing header replace
[wxWidgets.git] / src / gtk1 / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/button.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_BUTTON
14
15 #ifndef WX_PRECOMP
16 #include "wx/button.h"
17 #endif
18
19 #include "wx/stockitem.h"
20
21 #include "wx/gtk1/private.h"
22 #include "wx/gtk1/win_gtk.h"
23
24 //-----------------------------------------------------------------------------
25 // classes
26 //-----------------------------------------------------------------------------
27
28 class wxButton;
29
30 //-----------------------------------------------------------------------------
31 // idle system
32 //-----------------------------------------------------------------------------
33
34 extern void wxapp_install_idle_handler();
35 extern bool g_isIdle;
36
37 //-----------------------------------------------------------------------------
38 // data
39 //-----------------------------------------------------------------------------
40
41 extern bool g_blockEventsOnDrag;
42
43 //-----------------------------------------------------------------------------
44 // "clicked"
45 //-----------------------------------------------------------------------------
46
47 extern "C" {
48 static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button )
49 {
50 if (g_isIdle)
51 wxapp_install_idle_handler();
52
53 if (!button->m_hasVMT) return;
54 if (g_blockEventsOnDrag) return;
55
56 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
57 event.SetEventObject(button);
58 button->HandleWindowEvent(event);
59 }
60 }
61
62 //-----------------------------------------------------------------------------
63 // "style_set" from m_widget
64 //-----------------------------------------------------------------------------
65
66 static gint
67 gtk_button_style_set_callback( GtkWidget *m_widget, GtkStyle *WXUNUSED(style), wxButton *win )
68 {
69 if (g_isIdle)
70 wxapp_install_idle_handler();
71
72 int left_border = 0;
73 int right_border = 0;
74 int top_border = 0;
75 int bottom_border = 0;
76
77 /* the default button has a border around it */
78 if (GTK_WIDGET_CAN_DEFAULT(m_widget))
79 {
80 left_border = 6;
81 right_border = 6;
82 top_border = 6;
83 bottom_border = 5;
84 win->DoMoveWindow( win->m_x-top_border,
85 win->m_y-left_border,
86 win->m_width+left_border+right_border,
87 win->m_height+top_border+bottom_border );
88 }
89
90 return FALSE;
91 }
92
93 //-----------------------------------------------------------------------------
94 // wxButton
95 //-----------------------------------------------------------------------------
96
97 IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
98
99 wxButton::wxButton()
100 {
101 }
102
103 wxButton::~wxButton()
104 {
105 }
106
107 bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label,
108 const wxPoint &pos, const wxSize &size,
109 long style, const wxValidator& validator, const wxString &name )
110 {
111 m_needParent = true;
112 m_acceptsFocus = true;
113
114 if (!PreCreation( parent, pos, size ) ||
115 !CreateBase( parent, id, pos, size, style, validator, name ))
116 {
117 wxFAIL_MSG( wxT("wxButton creation failed") );
118 return false;
119 }
120
121 m_widget = gtk_button_new_with_label("");
122
123 float x_alignment = 0.5;
124 if (HasFlag(wxBU_LEFT))
125 x_alignment = 0.0;
126 else if (HasFlag(wxBU_RIGHT))
127 x_alignment = 1.0;
128
129 float y_alignment = 0.5;
130 if (HasFlag(wxBU_TOP))
131 y_alignment = 0.0;
132 else if (HasFlag(wxBU_BOTTOM))
133 y_alignment = 1.0;
134
135 if (GTK_IS_MISC(BUTTON_CHILD(m_widget)))
136 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget)),
137 x_alignment, y_alignment);
138
139 SetLabel(label);
140
141 if (style & wxNO_BORDER)
142 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
143
144 gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked",
145 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
146
147 gtk_signal_connect_after( GTK_OBJECT(m_widget), "style_set",
148 GTK_SIGNAL_FUNC(gtk_button_style_set_callback), (gpointer*) this );
149
150 m_parent->DoAddChild( this );
151
152 PostCreation(size);
153
154 return true;
155 }
156
157
158 wxWindow *wxButton::SetDefault()
159 {
160 wxWindow *oldDefault = wxButtonBase::SetDefault();
161
162 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
163 gtk_widget_grab_default( m_widget );
164
165 // resize for default border
166 gtk_button_style_set_callback( m_widget, NULL, this );
167
168 return oldDefault;
169 }
170
171 /* static */
172 wxSize wxButtonBase::GetDefaultSize()
173 {
174 return wxSize(80,26);
175 }
176
177 void wxButton::SetLabel( const wxString &lbl )
178 {
179 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
180
181 wxString label(lbl);
182
183 if (label.empty() && wxIsStockID(m_windowId))
184 label = wxGetStockLabel(m_windowId);
185
186 wxControl::SetLabel(label);
187
188 const wxString labelGTK = GTKRemoveMnemonics(label);
189
190 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV(labelGTK));
191 }
192
193 bool wxButton::Enable( bool enable )
194 {
195 if ( !wxControl::Enable( enable ) )
196 return false;
197
198 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
199
200 return true;
201 }
202
203 bool wxButton::IsOwnGtkWindow( GdkWindow *window )
204 {
205 return (window == m_widget->window);
206 }
207
208 void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
209 {
210 gtk_widget_modify_style(m_widget, style);
211 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
212 }
213
214 wxSize wxButton::DoGetBestSize() const
215 {
216 // the default button in wxGTK is bigger than the other ones because of an
217 // extra border around it, but we don't want to take it into account in
218 // our size calculations (otherwsie the result is visually ugly), so
219 // always return the size of non default button from here
220 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
221 if ( isDefault )
222 {
223 // temporarily unset default flag
224 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
225 }
226
227 wxSize ret( wxControl::DoGetBestSize() );
228
229 if ( isDefault )
230 {
231 // set it back again
232 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
233 }
234
235 ret.x += 10; // add a few pixels for sloppy (but common) themes
236
237 if (!HasFlag(wxBU_EXACTFIT))
238 {
239 wxSize defaultSize = GetDefaultSize();
240 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
241 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
242 }
243
244 CacheBestSize(ret);
245 return ret;
246 }
247
248 // static
249 wxVisualAttributes
250 wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
251 {
252 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
253 }
254
255 #endif // wxUSE_BUTTON