]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/button.cpp
testing header replace
[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 // data
25 //-----------------------------------------------------------------------------
26
27 extern bool g_blockEventsOnDrag;
28
29 //-----------------------------------------------------------------------------
30 // "clicked"
31 //-----------------------------------------------------------------------------
32
33 extern "C" {
34 static 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
49 extern "C" {
50 static void
51 gtk_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
76 IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
77
78 wxButton::wxButton()
79 {
80 }
81
82 wxButton::~wxButton()
83 {
84 }
85
86 bool 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
140 wxWindow *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 */
154 wxSize 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
188 void 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 ApplyWidgetStyle( false );
215 }
216
217 bool wxButton::Enable( bool enable )
218 {
219 if ( !wxControl::Enable( enable ) )
220 return false;
221
222 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
223
224 return true;
225 }
226
227 GdkWindow *wxButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
228 {
229 return GTK_BUTTON(m_widget)->event_window;
230 }
231
232 void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
233 {
234 gtk_widget_modify_style(m_widget, style);
235 GtkWidget *child = GTK_BIN(m_widget)->child;
236 gtk_widget_modify_style(child, style);
237
238 // for buttons with images, the path to the label is (at least in 2.12)
239 // GtkButton -> GtkAlignment -> GtkHBox -> GtkLabel
240 if ( GTK_IS_ALIGNMENT(child) )
241 {
242 GtkWidget *box = GTK_BIN(child)->child;
243 if ( GTK_IS_BOX(box) )
244 {
245 for (GList* item = GTK_BOX(box)->children; item; item = item->next)
246 {
247 GtkBoxChild* boxChild = static_cast<GtkBoxChild*>(item->data);
248 gtk_widget_modify_style(boxChild->widget, style);
249 }
250 }
251 }
252 }
253
254 wxSize wxButton::DoGetBestSize() const
255 {
256 // the default button in wxGTK is bigger than the other ones because of an
257 // extra border around it, but we don't want to take it into account in
258 // our size calculations (otherwsie the result is visually ugly), so
259 // always return the size of non default button from here
260 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
261 if ( isDefault )
262 {
263 // temporarily unset default flag
264 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
265 }
266
267 wxSize ret( wxControl::DoGetBestSize() );
268
269 if ( isDefault )
270 {
271 // set it back again
272 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
273 }
274
275 if (!HasFlag(wxBU_EXACTFIT))
276 {
277 wxSize defaultSize = GetDefaultSize();
278 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
279 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
280 }
281
282 CacheBestSize(ret);
283 return ret;
284 }
285
286 // static
287 wxVisualAttributes
288 wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
289 {
290 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
291 }
292
293 #endif // wxUSE_BUTTON