_conncect_after() should be used for controls emitting
[wxWidgets.git] / src / gtk1 / 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "button.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #include "wx/defs.h"
18
19 #if wxUSE_BUTTON
20
21 #include "wx/button.h"
22 #include "wx/stockitem.h"
23
24 #include "wx/gtk/private.h"
25
26 //-----------------------------------------------------------------------------
27 // classes
28 //-----------------------------------------------------------------------------
29
30 class wxButton;
31
32 //-----------------------------------------------------------------------------
33 // idle system
34 //-----------------------------------------------------------------------------
35
36 extern void wxapp_install_idle_handler();
37 extern bool g_isIdle;
38
39 //-----------------------------------------------------------------------------
40 // data
41 //-----------------------------------------------------------------------------
42
43 extern bool g_blockEventsOnDrag;
44
45 //-----------------------------------------------------------------------------
46 // "clicked"
47 //-----------------------------------------------------------------------------
48
49 static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button )
50 {
51 if (g_isIdle)
52 wxapp_install_idle_handler();
53
54 if (!button->m_hasVMT) return;
55 if (g_blockEventsOnDrag) return;
56
57 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
58 event.SetEventObject(button);
59 button->GetEventHandler()->ProcessEvent(event);
60 }
61
62 //-----------------------------------------------------------------------------
63 // wxButton
64 //-----------------------------------------------------------------------------
65
66 IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
67
68 wxButton::wxButton()
69 {
70 }
71
72 wxButton::~wxButton()
73 {
74 }
75
76 bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label,
77 const wxPoint &pos, const wxSize &size,
78 long style, const wxValidator& validator, const wxString &name )
79 {
80 m_needParent = TRUE;
81 m_acceptsFocus = TRUE;
82
83 if (!PreCreation( parent, pos, size ) ||
84 !CreateBase( parent, id, pos, size, style, validator, name ))
85 {
86 wxFAIL_MSG( wxT("wxButton creation failed") );
87 return FALSE;
88 }
89
90 /*
91 wxString label2( label );
92 for (size_t i = 0; i < label2.Len(); i++)
93 {
94 if (label2.GetChar(i) == wxT('&'))
95 label2.SetChar(i,wxT('_'));
96 }
97
98 GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() );
99 gtk_widget_show( accel_label );
100
101 m_widget = gtk_button_new();
102 gtk_container_add( GTK_CONTAINER(m_widget), accel_label );
103
104 gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget );
105
106 guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() );
107 gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) );
108
109 wxControl::SetLabel( label );
110 */
111
112 #ifdef __WXGTK20__
113 m_widget = gtk_button_new_with_mnemonic("");
114 #else
115 m_widget = gtk_button_new_with_label("");
116 #endif
117
118 float x_alignment = 0.5;
119 if (HasFlag(wxBU_LEFT))
120 x_alignment = 0.0;
121 else if (HasFlag(wxBU_RIGHT))
122 x_alignment = 1.0;
123
124 float y_alignment = 0.5;
125 if (HasFlag(wxBU_TOP))
126 y_alignment = 0.0;
127 else if (HasFlag(wxBU_BOTTOM))
128 y_alignment = 1.0;
129
130 #if __WXGTK24__
131 if (!gtk_check_version(2,4,0))
132 {
133 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
134 }
135 else
136 #endif
137 {
138 if (GTK_IS_MISC(BUTTON_CHILD(m_widget)))
139 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget)),
140 x_alignment, y_alignment);
141 }
142
143 SetLabel(label);
144
145 if (style & wxNO_BORDER)
146 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
147
148 gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked",
149 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
150
151 m_parent->DoAddChild( this );
152
153 PostCreation(size);
154
155 return true;
156 }
157
158
159 void wxButton::SetDefault()
160 {
161 wxWindow *parent = GetParent();
162 wxCHECK_RET( parent, _T("button without parent?") );
163
164 parent->SetDefaultItem(this);
165
166 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
167 gtk_widget_grab_default( m_widget );
168
169 SetSize( m_x, m_y, m_width, m_height );
170 }
171
172 /* static */
173 wxSize wxButtonBase::GetDefaultSize()
174 {
175 #ifdef __WXGTK20__
176 static wxSize size = wxDefaultSize;
177 if (size == wxDefaultSize)
178 {
179 // NB: Default size of buttons should be same as size of stock
180 // buttons as used in most GTK+ apps. Unfortunately it's a little
181 // tricky to obtain this size: stock button's size may be smaller
182 // than size of button in GtkButtonBox and vice versa,
183 // GtkButtonBox's minimal button size may be smaller than stock
184 // button's size. We have to retrieve both values and combine them.
185
186 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
187 GtkWidget *box = gtk_hbutton_box_new();
188 GtkWidget *btn = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
189 gtk_container_add(GTK_CONTAINER(box), btn);
190 gtk_container_add(GTK_CONTAINER(wnd), box);
191 GtkRequisition req;
192 gtk_widget_size_request(btn, &req);
193
194 gint minwidth, minheight;
195 gtk_widget_style_get(box,
196 "child-min-width", &minwidth,
197 "child-min-height", &minheight,
198 NULL);
199
200 size.x = wxMax(minwidth, req.width);
201 size.y = wxMax(minheight, req.height);
202
203 gtk_widget_destroy(wnd);
204 }
205 return size;
206 #else
207 return wxSize(80,26);
208 #endif
209 }
210
211 void wxButton::SetLabel( const wxString &lbl )
212 {
213 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
214
215 wxString label(lbl);
216
217 if (label.empty() && wxIsStockID(m_windowId))
218 label = wxGetStockLabel(m_windowId);
219
220 wxControl::SetLabel(label);
221
222 #ifdef __WXGTK20__
223 if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label))
224 {
225 const char *stock = wxGetStockGtkID(m_windowId);
226 if (stock)
227 {
228 gtk_button_set_label(GTK_BUTTON(m_widget), stock);
229 gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE);
230 return;
231 }
232 }
233
234 wxString label2 = PrepareLabelMnemonics(label);
235 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(label2));
236 gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE);
237 #else
238 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV(GetLabel()));
239 #endif
240 }
241
242 bool wxButton::Enable( bool enable )
243 {
244 if ( !wxControl::Enable( enable ) )
245 return FALSE;
246
247 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
248
249 return TRUE;
250 }
251
252 bool wxButton::IsOwnGtkWindow( GdkWindow *window )
253 {
254 #ifdef __WXGTK20__
255 return GTK_BUTTON(m_widget)->event_window;
256 #else
257 return (window == m_widget->window);
258 #endif
259 }
260
261 void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
262 {
263 gtk_widget_modify_style(m_widget, style);
264 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
265 }
266
267 wxSize wxButton::DoGetBestSize() const
268 {
269 // the default button in wxGTK is bigger than the other ones because of an
270 // extra border around it, but we don't want to take it into account in
271 // our size calculations (otherwsie the result is visually ugly), so
272 // always return the size of non default button from here
273 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
274 if ( isDefault )
275 {
276 // temporarily unset default flag
277 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
278 }
279
280 wxSize ret( wxControl::DoGetBestSize() );
281
282 if ( isDefault )
283 {
284 // set it back again
285 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
286 }
287
288 #ifndef __WXGTK20__
289 ret.x += 10; // add a few pixels for sloppy (but common) themes
290 #endif
291
292 if (!HasFlag(wxBU_EXACTFIT))
293 {
294 wxSize defaultSize = GetDefaultSize();
295 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
296 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
297 }
298
299 CacheBestSize(ret);
300 return ret;
301 }
302
303 // static
304 wxVisualAttributes
305 wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
306 {
307 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
308 }
309
310 #endif // wxUSE_BUTTON
311