]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/button.cpp
Call Dismiss rather than Show(false) on the infobar to correct the layout.
[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
9e691f46 21#include "wx/gtk/private.h"
83624f79 22
b4a4eafb
VZ
23// ----------------------------------------------------------------------------
24// GTK callbacks
25// ----------------------------------------------------------------------------
66bd6b93 26
b4a4eafb
VZ
27extern "C"
28{
c801d85f 29
b4a4eafb
VZ
30static void
31wxgtk_button_clicked_callback(GtkWidget *WXUNUSED(widget), wxButton *button)
c801d85f 32{
b4a4eafb
VZ
33 if ( button->GTKShouldIgnoreEvent() )
34 return;
9e691f46 35
acfd422a
RR
36 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
37 event.SetEventObject(button);
937013e0 38 button->HandleWindowEvent(event);
6de97a3b 39}
b4a4eafb 40
a90c0600
RR
41//-----------------------------------------------------------------------------
42// "style_set" from m_widget
43//-----------------------------------------------------------------------------
44
4cdf71be 45static void
b4a4eafb 46wxgtk_button_style_set_callback(GtkWidget* widget, GtkStyle*, wxButton* win)
a90c0600 47{
f893066b 48 /* the default button has a border around it */
4cdf71be 49 wxWindow* parent = win->GetParent();
fc9ab22a 50 if (parent && parent->m_wxwindow && gtk_widget_get_can_default(widget))
f893066b 51 {
4cdf71be
PC
52 GtkBorder* border = NULL;
53 gtk_widget_style_get(widget, "default_border", &border, NULL);
54 if (border)
f893066b 55 {
4cdf71be
PC
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);
f893066b 62 }
b2ff89d6 63 }
4cdf71be 64}
b4a4eafb
VZ
65
66} // extern "C"
a90c0600 67
c801d85f 68//-----------------------------------------------------------------------------
e1e955e1
RR
69// wxButton
70//-----------------------------------------------------------------------------
71
e8375af8
VZ
72bool 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)
c801d85f 80{
4dcaf11a
RR
81 if (!PreCreation( parent, pos, size ) ||
82 !CreateBase( parent, id, pos, size, style, validator, name ))
83 {
223d09f6 84 wxFAIL_MSG( wxT("wxButton creation failed") );
93763ad5 85 return false;
4dcaf11a 86 }
c801d85f 87
c37dd6da
VZ
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
a2117591
VZ
91 const bool
92 useLabel = !(style & wxBU_NOTEXT) && (!label.empty() || wxIsStockID(id));
c37dd6da
VZ
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
9ff9d30c 106 g_object_ref(m_widget);
354aa1e3 107
2e8613b7
RR
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
593ac8df 120 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
a696db45 121
c37dd6da
VZ
122 if ( useLabel )
123 SetLabel(label);
354aa1e3 124
de1c750f
RR
125 if (style & wxNO_BORDER)
126 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 127
9fa72bd2 128 g_signal_connect_after (m_widget, "clicked",
b4a4eafb 129 G_CALLBACK (wxgtk_button_clicked_callback),
9fa72bd2 130 this);
c801d85f 131
9fa72bd2 132 g_signal_connect_after (m_widget, "style_set",
b4a4eafb 133 G_CALLBACK (wxgtk_button_style_set_callback),
9fa72bd2 134 this);
b2ff89d6 135
f03fc89f 136 m_parent->DoAddChild( this );
9e691f46 137
abdeb9e7 138 PostCreation(size);
db434467 139
4fa87bd9
VS
140 return true;
141}
b2ff89d6 142
32c77a71 143
94aff5ff 144wxWindow *wxButton::SetDefault()
c801d85f 145{
94aff5ff 146 wxWindow *oldDefault = wxButtonBase::SetDefault();
b2ff89d6 147
fc9ab22a 148 gtk_widget_set_can_default(m_widget, TRUE);
3502e687 149 gtk_widget_grab_default( m_widget );
b2ff89d6 150
f893066b 151 // resize for default border
b4a4eafb 152 wxgtk_button_style_set_callback( m_widget, NULL, this );
94aff5ff
VZ
153
154 return oldDefault;
6de97a3b 155}
c801d85f 156
ebea0891 157/* static */
4fa87bd9 158wxSize wxButtonBase::GetDefaultSize()
8dbf4589 159{
4fa87bd9
VS
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);
b2ff89d6 186
4fa87bd9
VS
187 gtk_widget_destroy(wnd);
188 }
189 return size;
8dbf4589
RR
190}
191
5f7bcb48 192void wxButton::SetLabel( const wxString &lbl )
c801d85f 193{
223d09f6 194 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
9e691f46 195
5f7bcb48
VS
196 wxString label(lbl);
197
5f7bcb48
VS
198 if (label.empty() && wxIsStockID(m_windowId))
199 label = wxGetStockLabel(m_windowId);
5f7bcb48 200
b4354db1 201 wxAnyButton::SetLabel(label);
9e691f46 202
a2117591
VZ
203 // don't use label if it was explicitly disabled
204 if ( HasFlag(wxBU_NOTEXT) )
205 return;
206
5f7bcb48
VS
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);
b04683b1 214 return;
5f7bcb48 215 }
5f7bcb48
VS
216 }
217
5366ff46
VZ
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);
4cdf71be 222 const wxString labelGTK = GTKConvertMnemonics(label);
b2ff89d6 223 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
5f7bcb48 224 gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE);
b2ff89d6 225
496e7ec6 226 GTKApplyWidgetStyle( false );
6de97a3b 227}
c801d85f 228
f382836f 229#if wxUSE_MARKUP
de1cc378
VZ
230bool 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
de1cc378
VZ
248GtkLabel *wxButton::GTKGetLabel() const
249{
385e8575 250 GtkWidget* child = gtk_bin_get_child(GTK_BIN(m_widget));
de1cc378
VZ
251 if ( GTK_IS_ALIGNMENT(child) )
252 {
385e8575
PC
253 GtkWidget* box = gtk_bin_get_child(GTK_BIN(child));
254 GtkLabel* label = NULL;
255 GList* list = gtk_container_get_children(GTK_CONTAINER(box));
256 for (GList* item = list; item; item = item->next)
de1cc378 257 {
af806b13
PC
258 if (GTK_IS_LABEL(item->data))
259 label = GTK_LABEL(item->data);
de1cc378 260 }
385e8575 261 g_list_free(list);
de1cc378 262
385e8575 263 return label;
de1cc378
VZ
264 }
265
266 return GTK_LABEL(child);
267}
af806b13 268#endif // wxUSE_MARKUP
de1cc378 269
f40fdaa3 270void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 271{
f40fdaa3 272 gtk_widget_modify_style(m_widget, style);
385e8575 273 GtkWidget* child = gtk_bin_get_child(GTK_BIN(m_widget));
dfc22083
VZ
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 {
385e8575 280 GtkWidget* box = gtk_bin_get_child(GTK_BIN(child));
dfc22083
VZ
281 if ( GTK_IS_BOX(box) )
282 {
385e8575
PC
283 GList* list = gtk_container_get_children(GTK_CONTAINER(box));
284 for (GList* item = list; item; item = item->next)
f4b0832d 285 {
af806b13 286 gtk_widget_modify_style(GTK_WIDGET(item->data), style);
f4b0832d 287 }
385e8575 288 g_list_free(list);
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
fc9ab22a 299 const bool isDefault = gtk_widget_has_default(m_widget);
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{
331 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
332}
333
1e6feb95 334#endif // wxUSE_BUTTON