fix warnings about conversion to bool, closes #14381
[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 #include "wx/gtk/private/list.h"
23
24 // ----------------------------------------------------------------------------
25 // GTK callbacks
26 // ----------------------------------------------------------------------------
27
28 extern "C"
29 {
30
31 static void
32 wxgtk_button_clicked_callback(GtkWidget *WXUNUSED(widget), wxButton *button)
33 {
34 if ( button->GTKShouldIgnoreEvent() )
35 return;
36
37 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
38 event.SetEventObject(button);
39 button->HandleWindowEvent(event);
40 }
41
42 //-----------------------------------------------------------------------------
43 // "style_set" from m_widget
44 //-----------------------------------------------------------------------------
45
46 static void
47 wxgtk_button_style_set_callback(GtkWidget* widget, GtkStyle*, wxButton* win)
48 {
49 /* the default button has a border around it */
50 wxWindow* parent = win->GetParent();
51 if (parent && parent->m_wxwindow && gtk_widget_get_can_default(widget))
52 {
53 GtkBorder* border = NULL;
54 gtk_widget_style_get(widget, "default_border", &border, NULL);
55 if (border)
56 {
57 win->MoveWindow(
58 win->m_x - border->left,
59 win->m_y - border->top,
60 win->m_width + border->left + border->right,
61 win->m_height + border->top + border->bottom);
62 gtk_border_free(border);
63 }
64 }
65 }
66
67 } // extern "C"
68
69 //-----------------------------------------------------------------------------
70 // wxButton
71 //-----------------------------------------------------------------------------
72
73 bool wxButton::Create(wxWindow *parent,
74 wxWindowID id,
75 const wxString &label,
76 const wxPoint& pos,
77 const wxSize& size,
78 long style,
79 const wxValidator& validator,
80 const wxString& name)
81 {
82 if (!PreCreation( parent, pos, size ) ||
83 !CreateBase( parent, id, pos, size, style, validator, name ))
84 {
85 wxFAIL_MSG( wxT("wxButton creation failed") );
86 return false;
87 }
88
89 // create either a standard button with text label (which may still contain
90 // an image under GTK+ 2.6+) or a bitmap-only button if we don't have any
91 // label
92 const bool
93 useLabel = !(style & wxBU_NOTEXT) && (!label.empty() || wxIsStockID(id));
94 if ( useLabel )
95 {
96 m_widget = gtk_button_new_with_mnemonic("");
97 }
98 else // no label, suppose we will have a bitmap
99 {
100 m_widget = gtk_button_new();
101
102 GtkWidget *image = gtk_image_new();
103 gtk_widget_show(image);
104 gtk_container_add(GTK_CONTAINER(m_widget), image);
105 }
106
107 g_object_ref(m_widget);
108
109 float x_alignment = 0.5;
110 if (HasFlag(wxBU_LEFT))
111 x_alignment = 0.0;
112 else if (HasFlag(wxBU_RIGHT))
113 x_alignment = 1.0;
114
115 float y_alignment = 0.5;
116 if (HasFlag(wxBU_TOP))
117 y_alignment = 0.0;
118 else if (HasFlag(wxBU_BOTTOM))
119 y_alignment = 1.0;
120
121 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
122
123 if ( useLabel )
124 SetLabel(label);
125
126 if (style & wxNO_BORDER)
127 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
128
129 g_signal_connect_after (m_widget, "clicked",
130 G_CALLBACK (wxgtk_button_clicked_callback),
131 this);
132
133 g_signal_connect_after (m_widget, "style_set",
134 G_CALLBACK (wxgtk_button_style_set_callback),
135 this);
136
137 m_parent->DoAddChild( this );
138
139 PostCreation(size);
140
141 return true;
142 }
143
144
145 wxWindow *wxButton::SetDefault()
146 {
147 wxWindow *oldDefault = wxButtonBase::SetDefault();
148
149 gtk_widget_set_can_default(m_widget, TRUE);
150 gtk_widget_grab_default( m_widget );
151
152 // resize for default border
153 wxgtk_button_style_set_callback( m_widget, NULL, this );
154
155 return oldDefault;
156 }
157
158 /* static */
159 wxSize wxButtonBase::GetDefaultSize()
160 {
161 static wxSize size = wxDefaultSize;
162 if (size == wxDefaultSize)
163 {
164 // NB: Default size of buttons should be same as size of stock
165 // buttons as used in most GTK+ apps. Unfortunately it's a little
166 // tricky to obtain this size: stock button's size may be smaller
167 // than size of button in GtkButtonBox and vice versa,
168 // GtkButtonBox's minimal button size may be smaller than stock
169 // button's size. We have to retrieve both values and combine them.
170
171 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
172 GtkWidget *box = gtk_hbutton_box_new();
173 GtkWidget *btn = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
174 gtk_container_add(GTK_CONTAINER(box), btn);
175 gtk_container_add(GTK_CONTAINER(wnd), box);
176 GtkRequisition req;
177 gtk_widget_size_request(btn, &req);
178
179 gint minwidth, minheight;
180 gtk_widget_style_get(box,
181 "child-min-width", &minwidth,
182 "child-min-height", &minheight,
183 NULL);
184
185 size.x = wxMax(minwidth, req.width);
186 size.y = wxMax(minheight, req.height);
187
188 gtk_widget_destroy(wnd);
189 }
190 return size;
191 }
192
193 void wxButton::SetLabel( const wxString &lbl )
194 {
195 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
196
197 wxString label(lbl);
198
199 if (label.empty() && wxIsStockID(m_windowId))
200 label = wxGetStockLabel(m_windowId);
201
202 wxAnyButton::SetLabel(label);
203
204 // don't use label if it was explicitly disabled
205 if ( HasFlag(wxBU_NOTEXT) )
206 return;
207
208 if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label))
209 {
210 const char *stock = wxGetStockGtkID(m_windowId);
211 if (stock)
212 {
213 gtk_button_set_label(GTK_BUTTON(m_widget), stock);
214 gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE);
215 return;
216 }
217 }
218
219 // this call is necessary if the button had been initially created without
220 // a (text) label -- then we didn't use gtk_button_new_with_mnemonic() and
221 // so "use-underline" GtkButton property remained unset
222 gtk_button_set_use_underline(GTK_BUTTON(m_widget), TRUE);
223 const wxString labelGTK = GTKConvertMnemonics(label);
224 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
225 gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE);
226
227 GTKApplyWidgetStyle( false );
228 }
229
230 #if wxUSE_MARKUP
231 bool wxButton::DoSetLabelMarkup(const wxString& markup)
232 {
233 wxCHECK_MSG( m_widget != NULL, false, "invalid button" );
234
235 const wxString stripped = RemoveMarkup(markup);
236 if ( stripped.empty() && !markup.empty() )
237 return false;
238
239 wxControl::SetLabel(stripped);
240
241 GtkLabel * const label = GTKGetLabel();
242 wxCHECK_MSG( label, false, "no label in this button?" );
243
244 GTKSetLabelWithMarkupForLabel(label, markup);
245
246 return true;
247 }
248
249 GtkLabel *wxButton::GTKGetLabel() const
250 {
251 GtkWidget* child = gtk_bin_get_child(GTK_BIN(m_widget));
252 if ( GTK_IS_ALIGNMENT(child) )
253 {
254 GtkWidget* box = gtk_bin_get_child(GTK_BIN(child));
255 GtkLabel* label = NULL;
256 wxGtkList list(gtk_container_get_children(GTK_CONTAINER(box)));
257 for (GList* item = list; item; item = item->next)
258 {
259 if (GTK_IS_LABEL(item->data))
260 label = GTK_LABEL(item->data);
261 }
262
263 return label;
264 }
265
266 return GTK_LABEL(child);
267 }
268 #endif // wxUSE_MARKUP
269
270 void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
271 {
272 gtk_widget_modify_style(m_widget, style);
273 GtkWidget* child = gtk_bin_get_child(GTK_BIN(m_widget));
274 gtk_widget_modify_style(child, style);
275
276 // for buttons with images, the path to the label is (at least in 2.12)
277 // GtkButton -> GtkAlignment -> GtkHBox -> GtkLabel
278 if ( GTK_IS_ALIGNMENT(child) )
279 {
280 GtkWidget* box = gtk_bin_get_child(GTK_BIN(child));
281 if ( GTK_IS_BOX(box) )
282 {
283 wxGtkList list(gtk_container_get_children(GTK_CONTAINER(box)));
284 for (GList* item = list; item; item = item->next)
285 {
286 gtk_widget_modify_style(GTK_WIDGET(item->data), style);
287 }
288 }
289 }
290 }
291
292 wxSize wxButton::DoGetBestSize() const
293 {
294 // the default button in wxGTK is bigger than the other ones because of an
295 // extra border around it, but we don't want to take it into account in
296 // our size calculations (otherwise the result is visually ugly), so
297 // always return the size of non default button from here
298 const bool isDefault = gtk_widget_has_default(m_widget) != 0;
299 if ( isDefault )
300 {
301 // temporarily unset default flag
302 gtk_widget_set_can_default(m_widget, FALSE);
303 }
304
305 wxSize ret( wxAnyButton::DoGetBestSize() );
306
307 if ( isDefault )
308 {
309 // set it back again
310 gtk_widget_set_can_default(m_widget, TRUE);
311 }
312
313 if (!HasFlag(wxBU_EXACTFIT))
314 {
315 wxSize defaultSize = GetDefaultSize();
316 if (ret.x < defaultSize.x)
317 ret.x = defaultSize.x;
318 if (ret.y < defaultSize.y)
319 ret.y = defaultSize.y;
320 }
321
322 CacheBestSize(ret);
323 return ret;
324 }
325
326 // static
327 wxVisualAttributes
328 wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
329 {
330 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
331 }
332
333 #endif // wxUSE_BUTTON