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