]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/button.cpp
fix compilation breaking typo in recent commit
[wxWidgets.git] / src / gtk / button.cpp
... / ...
CommitLineData
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// data
25//-----------------------------------------------------------------------------
26
27extern bool g_blockEventsOnDrag;
28
29//-----------------------------------------------------------------------------
30// "clicked"
31//-----------------------------------------------------------------------------
32
33extern "C" {
34static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button )
35{
36 if (!button->m_hasVMT) return;
37 if (g_blockEventsOnDrag) return;
38
39 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
40 event.SetEventObject(button);
41 button->HandleWindowEvent(event);
42}
43}
44
45//-----------------------------------------------------------------------------
46// "style_set" from m_widget
47//-----------------------------------------------------------------------------
48
49extern "C" {
50static void
51gtk_button_style_set_callback(GtkWidget* widget, GtkStyle*, wxButton* win)
52{
53 /* the default button has a border around it */
54 wxWindow* parent = win->GetParent();
55 if (parent && parent->m_wxwindow && GTK_WIDGET_CAN_DEFAULT(widget))
56 {
57 GtkBorder* border = NULL;
58 gtk_widget_style_get(widget, "default_border", &border, NULL);
59 if (border)
60 {
61 win->MoveWindow(
62 win->m_x - border->left,
63 win->m_y - border->top,
64 win->m_width + border->left + border->right,
65 win->m_height + border->top + border->bottom);
66 gtk_border_free(border);
67 }
68 }
69}
70}
71
72//-----------------------------------------------------------------------------
73// wxButton
74//-----------------------------------------------------------------------------
75
76IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
77
78wxButton::wxButton()
79{
80}
81
82wxButton::~wxButton()
83{
84}
85
86bool wxButton::Create(wxWindow *parent,
87 wxWindowID id,
88 const wxString &label,
89 const wxPoint& pos,
90 const wxSize& size,
91 long style,
92 const wxValidator& validator,
93 const wxString& name)
94{
95 if (!PreCreation( parent, pos, size ) ||
96 !CreateBase( parent, id, pos, size, style, validator, name ))
97 {
98 wxFAIL_MSG( wxT("wxButton creation failed") );
99 return false;
100 }
101
102 m_widget = gtk_button_new_with_mnemonic("");
103 g_object_ref(m_widget);
104
105 float x_alignment = 0.5;
106 if (HasFlag(wxBU_LEFT))
107 x_alignment = 0.0;
108 else if (HasFlag(wxBU_RIGHT))
109 x_alignment = 1.0;
110
111 float y_alignment = 0.5;
112 if (HasFlag(wxBU_TOP))
113 y_alignment = 0.0;
114 else if (HasFlag(wxBU_BOTTOM))
115 y_alignment = 1.0;
116
117 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
118
119 SetLabel(label);
120
121 if (style & wxNO_BORDER)
122 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
123
124 g_signal_connect_after (m_widget, "clicked",
125 G_CALLBACK (gtk_button_clicked_callback),
126 this);
127
128 g_signal_connect_after (m_widget, "style_set",
129 G_CALLBACK (gtk_button_style_set_callback),
130 this);
131
132 m_parent->DoAddChild( this );
133
134 PostCreation(size);
135
136 return true;
137}
138
139
140wxWindow *wxButton::SetDefault()
141{
142 wxWindow *oldDefault = wxButtonBase::SetDefault();
143
144 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
145 gtk_widget_grab_default( m_widget );
146
147 // resize for default border
148 gtk_button_style_set_callback( m_widget, NULL, this );
149
150 return oldDefault;
151}
152
153/* static */
154wxSize wxButtonBase::GetDefaultSize()
155{
156 static wxSize size = wxDefaultSize;
157 if (size == wxDefaultSize)
158 {
159 // NB: Default size of buttons should be same as size of stock
160 // buttons as used in most GTK+ apps. Unfortunately it's a little
161 // tricky to obtain this size: stock button's size may be smaller
162 // than size of button in GtkButtonBox and vice versa,
163 // GtkButtonBox's minimal button size may be smaller than stock
164 // button's size. We have to retrieve both values and combine them.
165
166 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
167 GtkWidget *box = gtk_hbutton_box_new();
168 GtkWidget *btn = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
169 gtk_container_add(GTK_CONTAINER(box), btn);
170 gtk_container_add(GTK_CONTAINER(wnd), box);
171 GtkRequisition req;
172 gtk_widget_size_request(btn, &req);
173
174 gint minwidth, minheight;
175 gtk_widget_style_get(box,
176 "child-min-width", &minwidth,
177 "child-min-height", &minheight,
178 NULL);
179
180 size.x = wxMax(minwidth, req.width);
181 size.y = wxMax(minheight, req.height);
182
183 gtk_widget_destroy(wnd);
184 }
185 return size;
186}
187
188void wxButton::SetLabel( const wxString &lbl )
189{
190 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
191
192 wxString label(lbl);
193
194 if (label.empty() && wxIsStockID(m_windowId))
195 label = wxGetStockLabel(m_windowId);
196
197 wxControl::SetLabel(label);
198
199 if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label))
200 {
201 const char *stock = wxGetStockGtkID(m_windowId);
202 if (stock)
203 {
204 gtk_button_set_label(GTK_BUTTON(m_widget), stock);
205 gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE);
206 return;
207 }
208 }
209
210 const wxString labelGTK = GTKConvertMnemonics(label);
211 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
212 gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE);
213
214 GTKApplyWidgetStyle( false );
215}
216
217bool wxButton::Enable( bool enable )
218{
219 bool isEnabled = IsEnabled();
220
221 if ( !wxControl::Enable( enable ) )
222 return false;
223
224 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
225
226 if (!isEnabled && enable)
227 {
228 GTKFixSensitivity();
229 }
230
231 return true;
232}
233
234GdkWindow *wxButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
235{
236 return GTK_BUTTON(m_widget)->event_window;
237}
238
239void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
240{
241 gtk_widget_modify_style(m_widget, style);
242 GtkWidget *child = GTK_BIN(m_widget)->child;
243 gtk_widget_modify_style(child, style);
244
245 // for buttons with images, the path to the label is (at least in 2.12)
246 // GtkButton -> GtkAlignment -> GtkHBox -> GtkLabel
247 if ( GTK_IS_ALIGNMENT(child) )
248 {
249 GtkWidget *box = GTK_BIN(child)->child;
250 if ( GTK_IS_BOX(box) )
251 {
252 for (GList* item = GTK_BOX(box)->children; item; item = item->next)
253 {
254 GtkBoxChild* boxChild = static_cast<GtkBoxChild*>(item->data);
255 gtk_widget_modify_style(boxChild->widget, style);
256 }
257 }
258 }
259}
260
261wxSize wxButton::DoGetBestSize() const
262{
263 // the default button in wxGTK is bigger than the other ones because of an
264 // extra border around it, but we don't want to take it into account in
265 // our size calculations (otherwsie the result is visually ugly), so
266 // always return the size of non default button from here
267 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
268 if ( isDefault )
269 {
270 // temporarily unset default flag
271 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
272 }
273
274 wxSize ret( wxControl::DoGetBestSize() );
275
276 if ( isDefault )
277 {
278 // set it back again
279 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
280 }
281
282 if (!HasFlag(wxBU_EXACTFIT))
283 {
284 wxSize defaultSize = GetDefaultSize();
285 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
286 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
287 }
288
289 CacheBestSize(ret);
290 return ret;
291}
292
293// static
294wxVisualAttributes
295wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
296{
297 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
298}
299
300#endif // wxUSE_BUTTON