Reorganize idle system code.
[wxWidgets.git] / src / gtk / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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/gtk/private.h"
22
23 //-----------------------------------------------------------------------------
24 // classes
25 //-----------------------------------------------------------------------------
26
27 class wxButton;
28
29 //-----------------------------------------------------------------------------
30 // data
31 //-----------------------------------------------------------------------------
32
33 extern bool g_blockEventsOnDrag;
34
35 //-----------------------------------------------------------------------------
36 // "clicked"
37 //-----------------------------------------------------------------------------
38
39 extern "C" {
40 static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button )
41 {
42 if (!button->m_hasVMT) return;
43 if (g_blockEventsOnDrag) return;
44
45 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
46 event.SetEventObject(button);
47 button->GetEventHandler()->ProcessEvent(event);
48 }
49 }
50
51 //-----------------------------------------------------------------------------
52 // "style_set" from m_widget
53 //-----------------------------------------------------------------------------
54
55 static gint
56 gtk_button_style_set_callback( GtkWidget *m_widget, GtkStyle *WXUNUSED(style), wxButton *win )
57 {
58 int left_border = 0;
59 int right_border = 0;
60 int top_border = 0;
61 int bottom_border = 0;
62
63 /* the default button has a border around it */
64 if (GTK_WIDGET_CAN_DEFAULT(m_widget))
65 {
66 GtkBorder *default_border = NULL;
67 gtk_widget_style_get( m_widget, "default_border", &default_border, NULL );
68 if (default_border)
69 {
70 left_border += default_border->left;
71 right_border += default_border->right;
72 top_border += default_border->top;
73 bottom_border += default_border->bottom;
74 g_free( default_border );
75 }
76 win->MoveWindow(
77 win->m_x - top_border,
78 win->m_y - left_border,
79 win->m_width + left_border + right_border,
80 win->m_height + top_border + bottom_border);
81 }
82
83 return FALSE;
84 }
85
86 //-----------------------------------------------------------------------------
87 // wxButton
88 //-----------------------------------------------------------------------------
89
90 IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
91
92 wxButton::wxButton()
93 {
94 }
95
96 wxButton::~wxButton()
97 {
98 }
99
100 bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label,
101 const wxPoint &pos, const wxSize &size,
102 long style, const wxValidator& validator, const wxString &name )
103 {
104 m_needParent = true;
105
106 if (!PreCreation( parent, pos, size ) ||
107 !CreateBase( parent, id, pos, size, style, validator, name ))
108 {
109 wxFAIL_MSG( wxT("wxButton creation failed") );
110 return false;
111 }
112
113 m_widget = gtk_button_new_with_mnemonic("");
114
115 float x_alignment = 0.5;
116 if (HasFlag(wxBU_LEFT))
117 x_alignment = 0.0;
118 else if (HasFlag(wxBU_RIGHT))
119 x_alignment = 1.0;
120
121 float y_alignment = 0.5;
122 if (HasFlag(wxBU_TOP))
123 y_alignment = 0.0;
124 else if (HasFlag(wxBU_BOTTOM))
125 y_alignment = 1.0;
126
127 #ifdef __WXGTK24__
128 if (!gtk_check_version(2,4,0))
129 {
130 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
131 }
132 else
133 #endif
134 {
135 if (GTK_IS_MISC(GTK_BIN(m_widget)->child))
136 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(m_widget)->child),
137 x_alignment, y_alignment);
138 }
139
140 SetLabel(label);
141
142 if (style & wxNO_BORDER)
143 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
144
145 g_signal_connect_after (m_widget, "clicked",
146 G_CALLBACK (gtk_button_clicked_callback),
147 this);
148
149 g_signal_connect_after (m_widget, "style_set",
150 G_CALLBACK (gtk_button_style_set_callback),
151 this);
152
153 m_parent->DoAddChild( this );
154
155 PostCreation(size);
156
157 return true;
158 }
159
160
161 wxWindow *wxButton::SetDefault()
162 {
163 wxWindow *oldDefault = wxButtonBase::SetDefault();
164
165 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
166 gtk_widget_grab_default( m_widget );
167
168 // resize for default border
169 gtk_button_style_set_callback( m_widget, NULL, this );
170
171 return oldDefault;
172 }
173
174 /* static */
175 wxSize wxButtonBase::GetDefaultSize()
176 {
177 static wxSize size = wxDefaultSize;
178 if (size == wxDefaultSize)
179 {
180 // NB: Default size of buttons should be same as size of stock
181 // buttons as used in most GTK+ apps. Unfortunately it's a little
182 // tricky to obtain this size: stock button's size may be smaller
183 // than size of button in GtkButtonBox and vice versa,
184 // GtkButtonBox's minimal button size may be smaller than stock
185 // button's size. We have to retrieve both values and combine them.
186
187 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
188 GtkWidget *box = gtk_hbutton_box_new();
189 GtkWidget *btn = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
190 gtk_container_add(GTK_CONTAINER(box), btn);
191 gtk_container_add(GTK_CONTAINER(wnd), box);
192 GtkRequisition req;
193 gtk_widget_size_request(btn, &req);
194
195 gint minwidth, minheight;
196 gtk_widget_style_get(box,
197 "child-min-width", &minwidth,
198 "child-min-height", &minheight,
199 NULL);
200
201 size.x = wxMax(minwidth, req.width);
202 size.y = wxMax(minheight, req.height);
203
204 gtk_widget_destroy(wnd);
205 }
206 return size;
207 }
208
209 void wxButton::SetLabel( const wxString &lbl )
210 {
211 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
212
213 wxString label(lbl);
214
215 if (label.empty() && wxIsStockID(m_windowId))
216 label = wxGetStockLabel(m_windowId);
217
218 wxControl::SetLabel(label);
219
220 const wxString labelGTK = GTKConvertMnemonics(label);
221
222 if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label))
223 {
224 const char *stock = wxGetStockGtkID(m_windowId);
225 if (stock)
226 {
227 gtk_button_set_label(GTK_BUTTON(m_widget), stock);
228 gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE);
229 return;
230 }
231 }
232
233 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
234 gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE);
235
236 ApplyWidgetStyle( false );
237 }
238
239 bool wxButton::Enable( bool enable )
240 {
241 if ( !wxControl::Enable( enable ) )
242 return false;
243
244 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
245
246 return true;
247 }
248
249 GdkWindow *wxButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
250 {
251 return GTK_BUTTON(m_widget)->event_window;
252 }
253
254 void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
255 {
256 gtk_widget_modify_style(m_widget, style);
257 gtk_widget_modify_style(GTK_BIN(m_widget)->child, 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 if (!HasFlag(wxBU_EXACTFIT))
282 {
283 wxSize defaultSize = GetDefaultSize();
284 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
285 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
286 }
287
288 CacheBestSize(ret);
289 return ret;
290 }
291
292 // static
293 wxVisualAttributes
294 wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
295 {
296 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
297 }
298
299 #endif // wxUSE_BUTTON