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