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