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