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