]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/button.cpp
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / src / gtk / button.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/gtk/button.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
01111366 5// Copyright: (c) 1998 Robert Roebling
65571936 6// Licence: wxWindows licence
c801d85f
KB
7/////////////////////////////////////////////////////////////////////////////
8
14f355c2
VS
9// For compilers that support precompilation, includes "wx.h".
10#include "wx/wxprec.h"
11
1e6feb95
VZ
12#if wxUSE_BUTTON
13
b84aec03 14#ifndef WX_PRECOMP
94aff5ff 15 #include "wx/button.h"
b84aec03
WS
16#endif
17
5f7bcb48 18#include "wx/stockitem.h"
c801d85f 19
9dc44eff 20#include <gtk/gtk.h>
9e691f46 21#include "wx/gtk/private.h"
9dc44eff 22#include "wx/gtk/private/gtk2-compat.h"
0eec47e4 23#include "wx/gtk/private/list.h"
83624f79 24
b4a4eafb
VZ
25// ----------------------------------------------------------------------------
26// GTK callbacks
27// ----------------------------------------------------------------------------
66bd6b93 28
b4a4eafb
VZ
29extern "C"
30{
c801d85f 31
b4a4eafb
VZ
32static void
33wxgtk_button_clicked_callback(GtkWidget *WXUNUSED(widget), wxButton *button)
c801d85f 34{
b4a4eafb
VZ
35 if ( button->GTKShouldIgnoreEvent() )
36 return;
9e691f46 37
ce7fe42e 38 wxCommandEvent event(wxEVT_BUTTON, button->GetId());
acfd422a 39 event.SetEventObject(button);
937013e0 40 button->HandleWindowEvent(event);
6de97a3b 41}
b4a4eafb 42
a90c0600
RR
43//-----------------------------------------------------------------------------
44// "style_set" from m_widget
45//-----------------------------------------------------------------------------
46
4cdf71be 47static void
b4a4eafb 48wxgtk_button_style_set_callback(GtkWidget* widget, GtkStyle*, wxButton* win)
a90c0600 49{
f893066b 50 /* the default button has a border around it */
4cdf71be 51 wxWindow* parent = win->GetParent();
fc9ab22a 52 if (parent && parent->m_wxwindow && gtk_widget_get_can_default(widget))
f893066b 53 {
4cdf71be
PC
54 GtkBorder* border = NULL;
55 gtk_widget_style_get(widget, "default_border", &border, NULL);
56 if (border)
f893066b 57 {
4cdf71be
PC
58 win->MoveWindow(
59 win->m_x - border->left,
60 win->m_y - border->top,
61 win->m_width + border->left + border->right,
62 win->m_height + border->top + border->bottom);
63 gtk_border_free(border);
f893066b 64 }
b2ff89d6 65 }
4cdf71be 66}
b4a4eafb
VZ
67
68} // extern "C"
a90c0600 69
c801d85f 70//-----------------------------------------------------------------------------
e1e955e1
RR
71// wxButton
72//-----------------------------------------------------------------------------
73
e8375af8
VZ
74bool wxButton::Create(wxWindow *parent,
75 wxWindowID id,
76 const wxString &label,
77 const wxPoint& pos,
78 const wxSize& size,
79 long style,
80 const wxValidator& validator,
81 const wxString& name)
c801d85f 82{
4dcaf11a
RR
83 if (!PreCreation( parent, pos, size ) ||
84 !CreateBase( parent, id, pos, size, style, validator, name ))
85 {
223d09f6 86 wxFAIL_MSG( wxT("wxButton creation failed") );
93763ad5 87 return false;
4dcaf11a 88 }
c801d85f 89
c37dd6da
VZ
90 // create either a standard button with text label (which may still contain
91 // an image under GTK+ 2.6+) or a bitmap-only button if we don't have any
92 // label
a2117591
VZ
93 const bool
94 useLabel = !(style & wxBU_NOTEXT) && (!label.empty() || wxIsStockID(id));
c37dd6da
VZ
95 if ( useLabel )
96 {
97 m_widget = gtk_button_new_with_mnemonic("");
98 }
99 else // no label, suppose we will have a bitmap
100 {
101 m_widget = gtk_button_new();
102
103 GtkWidget *image = gtk_image_new();
104 gtk_widget_show(image);
105 gtk_container_add(GTK_CONTAINER(m_widget), image);
106 }
107
9ff9d30c 108 g_object_ref(m_widget);
354aa1e3 109
2e8613b7
RR
110 float x_alignment = 0.5;
111 if (HasFlag(wxBU_LEFT))
112 x_alignment = 0.0;
113 else if (HasFlag(wxBU_RIGHT))
114 x_alignment = 1.0;
115
116 float y_alignment = 0.5;
117 if (HasFlag(wxBU_TOP))
118 y_alignment = 0.0;
119 else if (HasFlag(wxBU_BOTTOM))
120 y_alignment = 1.0;
121
593ac8df 122 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
a696db45 123
c37dd6da
VZ
124 if ( useLabel )
125 SetLabel(label);
354aa1e3 126
de1c750f
RR
127 if (style & wxNO_BORDER)
128 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 129
9fa72bd2 130 g_signal_connect_after (m_widget, "clicked",
b4a4eafb 131 G_CALLBACK (wxgtk_button_clicked_callback),
9fa72bd2 132 this);
c801d85f 133
9fa72bd2 134 g_signal_connect_after (m_widget, "style_set",
b4a4eafb 135 G_CALLBACK (wxgtk_button_style_set_callback),
9fa72bd2 136 this);
b2ff89d6 137
f03fc89f 138 m_parent->DoAddChild( this );
9e691f46 139
abdeb9e7 140 PostCreation(size);
db434467 141
4fa87bd9
VS
142 return true;
143}
b2ff89d6 144
32c77a71 145
94aff5ff 146wxWindow *wxButton::SetDefault()
c801d85f 147{
94aff5ff 148 wxWindow *oldDefault = wxButtonBase::SetDefault();
b2ff89d6 149
fc9ab22a 150 gtk_widget_set_can_default(m_widget, TRUE);
3502e687 151 gtk_widget_grab_default( m_widget );
b2ff89d6 152
f893066b 153 // resize for default border
b4a4eafb 154 wxgtk_button_style_set_callback( m_widget, NULL, this );
94aff5ff
VZ
155
156 return oldDefault;
6de97a3b 157}
c801d85f 158
ebea0891 159/* static */
4fa87bd9 160wxSize wxButtonBase::GetDefaultSize()
8dbf4589 161{
4fa87bd9
VS
162 static wxSize size = wxDefaultSize;
163 if (size == wxDefaultSize)
164 {
165 // NB: Default size of buttons should be same as size of stock
166 // buttons as used in most GTK+ apps. Unfortunately it's a little
167 // tricky to obtain this size: stock button's size may be smaller
168 // than size of button in GtkButtonBox and vice versa,
169 // GtkButtonBox's minimal button size may be smaller than stock
170 // button's size. We have to retrieve both values and combine them.
171
172 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1897abe1 173 GtkWidget *box = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
4fa87bd9
VS
174 GtkWidget *btn = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
175 gtk_container_add(GTK_CONTAINER(box), btn);
176 gtk_container_add(GTK_CONTAINER(wnd), box);
177 GtkRequisition req;
1897abe1 178 gtk_widget_get_preferred_size(btn, NULL, &req);
4fa87bd9
VS
179
180 gint minwidth, minheight;
181 gtk_widget_style_get(box,
182 "child-min-width", &minwidth,
183 "child-min-height", &minheight,
184 NULL);
185
186 size.x = wxMax(minwidth, req.width);
187 size.y = wxMax(minheight, req.height);
b2ff89d6 188
4fa87bd9
VS
189 gtk_widget_destroy(wnd);
190 }
191 return size;
8dbf4589
RR
192}
193
5f7bcb48 194void wxButton::SetLabel( const wxString &lbl )
c801d85f 195{
223d09f6 196 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
9e691f46 197
5f7bcb48
VS
198 wxString label(lbl);
199
5f7bcb48
VS
200 if (label.empty() && wxIsStockID(m_windowId))
201 label = wxGetStockLabel(m_windowId);
5f7bcb48 202
b4354db1 203 wxAnyButton::SetLabel(label);
9e691f46 204
a2117591
VZ
205 // don't use label if it was explicitly disabled
206 if ( HasFlag(wxBU_NOTEXT) )
207 return;
208
5f7bcb48
VS
209 if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label))
210 {
211 const char *stock = wxGetStockGtkID(m_windowId);
212 if (stock)
213 {
214 gtk_button_set_label(GTK_BUTTON(m_widget), stock);
215 gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE);
b04683b1 216 return;
5f7bcb48 217 }
5f7bcb48
VS
218 }
219
5366ff46
VZ
220 // this call is necessary if the button had been initially created without
221 // a (text) label -- then we didn't use gtk_button_new_with_mnemonic() and
222 // so "use-underline" GtkButton property remained unset
223 gtk_button_set_use_underline(GTK_BUTTON(m_widget), TRUE);
4cdf71be 224 const wxString labelGTK = GTKConvertMnemonics(label);
b2ff89d6 225 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
5f7bcb48 226 gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE);
b2ff89d6 227
496e7ec6 228 GTKApplyWidgetStyle( false );
6de97a3b 229}
c801d85f 230
f382836f 231#if wxUSE_MARKUP
de1cc378
VZ
232bool wxButton::DoSetLabelMarkup(const wxString& markup)
233{
234 wxCHECK_MSG( m_widget != NULL, false, "invalid button" );
235
236 const wxString stripped = RemoveMarkup(markup);
237 if ( stripped.empty() && !markup.empty() )
238 return false;
239
240 wxControl::SetLabel(stripped);
241
242 GtkLabel * const label = GTKGetLabel();
243 wxCHECK_MSG( label, false, "no label in this button?" );
244
245 GTKSetLabelWithMarkupForLabel(label, markup);
246
247 return true;
248}
249
de1cc378
VZ
250GtkLabel *wxButton::GTKGetLabel() const
251{
385e8575 252 GtkWidget* child = gtk_bin_get_child(GTK_BIN(m_widget));
de1cc378
VZ
253 if ( GTK_IS_ALIGNMENT(child) )
254 {
385e8575
PC
255 GtkWidget* box = gtk_bin_get_child(GTK_BIN(child));
256 GtkLabel* label = NULL;
0eec47e4 257 wxGtkList list(gtk_container_get_children(GTK_CONTAINER(box)));
385e8575 258 for (GList* item = list; item; item = item->next)
de1cc378 259 {
af806b13
PC
260 if (GTK_IS_LABEL(item->data))
261 label = GTK_LABEL(item->data);
de1cc378
VZ
262 }
263
385e8575 264 return label;
de1cc378
VZ
265 }
266
267 return GTK_LABEL(child);
268}
af806b13 269#endif // wxUSE_MARKUP
de1cc378 270
f40fdaa3 271void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 272{
9dc44eff 273 GTKApplyStyle(m_widget, style);
385e8575 274 GtkWidget* child = gtk_bin_get_child(GTK_BIN(m_widget));
9dc44eff 275 GTKApplyStyle(child, style);
dfc22083
VZ
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 {
385e8575 281 GtkWidget* box = gtk_bin_get_child(GTK_BIN(child));
dfc22083
VZ
282 if ( GTK_IS_BOX(box) )
283 {
0eec47e4 284 wxGtkList list(gtk_container_get_children(GTK_CONTAINER(box)));
385e8575 285 for (GList* item = list; item; item = item->next)
f4b0832d 286 {
9dc44eff 287 GTKApplyStyle(GTK_WIDGET(item->data), style);
f4b0832d 288 }
dfc22083
VZ
289 }
290 }
a81258be 291}
db434467
RR
292
293wxSize wxButton::DoGetBestSize() const
294{
4f819fe4
VZ
295 // the default button in wxGTK is bigger than the other ones because of an
296 // extra border around it, but we don't want to take it into account in
7be740a3 297 // our size calculations (otherwise the result is visually ugly), so
4f819fe4 298 // always return the size of non default button from here
d5027818 299 const bool isDefault = gtk_widget_has_default(m_widget) != 0;
4f819fe4
VZ
300 if ( isDefault )
301 {
302 // temporarily unset default flag
fc9ab22a 303 gtk_widget_set_can_default(m_widget, FALSE);
4f819fe4
VZ
304 }
305
b4354db1 306 wxSize ret( wxAnyButton::DoGetBestSize() );
9e691f46 307
4f819fe4
VZ
308 if ( isDefault )
309 {
310 // set it back again
fc9ab22a 311 gtk_widget_set_can_default(m_widget, TRUE);
4f819fe4
VZ
312 }
313
8ab696e0
RR
314 if (!HasFlag(wxBU_EXACTFIT))
315 {
4fa87bd9 316 wxSize defaultSize = GetDefaultSize();
7be740a3
VZ
317 if (ret.x < defaultSize.x)
318 ret.x = defaultSize.x;
319 if (ret.y < defaultSize.y)
320 ret.y = defaultSize.y;
8ab696e0 321 }
9e691f46 322
9f884528 323 CacheBestSize(ret);
db434467
RR
324 return ret;
325}
326
9d522606
RD
327// static
328wxVisualAttributes
329wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
330{
7fff16b8 331 return GetDefaultAttributesFromGTKWidget(gtk_button_new());
9d522606
RD
332}
333
1e6feb95 334#endif // wxUSE_BUTTON