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