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