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