]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/button.cpp
Commit pickers-fixes.patch added to 1472329 (Francesco Montorsi)
[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
c801d85f 15#include "wx/button.h"
5f7bcb48 16#include "wx/stockitem.h"
c801d85f 17
9e691f46 18#include "wx/gtk/private.h"
f893066b 19#include "wx/gtk/win_gtk.h"
83624f79 20
c801d85f
KB
21//-----------------------------------------------------------------------------
22// classes
23//-----------------------------------------------------------------------------
24
25class wxButton;
26
66bd6b93
RR
27//-----------------------------------------------------------------------------
28// data
29//-----------------------------------------------------------------------------
30
31extern bool g_blockEventsOnDrag;
32
c801d85f 33//-----------------------------------------------------------------------------
e1e955e1 34// "clicked"
c801d85f
KB
35//-----------------------------------------------------------------------------
36
865bb325 37extern "C" {
66bd6b93 38static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button )
c801d85f 39{
9e691f46 40 if (g_isIdle)
32ac755d 41 wxapp_install_idle_handler();
acfd422a 42
a2053b27 43 if (!button->m_hasVMT) return;
acfd422a 44 if (g_blockEventsOnDrag) return;
9e691f46 45
acfd422a
RR
46 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
47 event.SetEventObject(button);
48 button->GetEventHandler()->ProcessEvent(event);
6de97a3b 49}
865bb325 50}
c801d85f 51
a90c0600
RR
52//-----------------------------------------------------------------------------
53// "style_set" from m_widget
54//-----------------------------------------------------------------------------
55
56static gint
57gtk_button_style_set_callback( GtkWidget *m_widget, GtkStyle *WXUNUSED(style), wxButton *win )
58{
59 if (g_isIdle)
60 wxapp_install_idle_handler();
b2ff89d6 61
f893066b
RR
62 int left_border = 0;
63 int right_border = 0;
64 int top_border = 0;
65 int bottom_border = 0;
b2ff89d6 66
f893066b
RR
67 /* the default button has a border around it */
68 if (GTK_WIDGET_CAN_DEFAULT(m_widget))
69 {
f893066b
RR
70 GtkBorder *default_border = NULL;
71 gtk_widget_style_get( m_widget, "default_border", &default_border, NULL );
72 if (default_border)
73 {
74 left_border += default_border->left;
75 right_border += default_border->right;
76 top_border += default_border->top;
77 bottom_border += default_border->bottom;
78 g_free( default_border );
79 }
6f02a879
VZ
80 win->MoveWindow(
81 win->m_x - top_border,
82 win->m_y - left_border,
83 win->m_width + left_border + right_border,
84 win->m_height + top_border + bottom_border);
b2ff89d6 85 }
a90c0600
RR
86
87 return FALSE;
88}
89
c801d85f 90//-----------------------------------------------------------------------------
e1e955e1
RR
91// wxButton
92//-----------------------------------------------------------------------------
93
94IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
c801d85f 95
fd0eed64 96wxButton::wxButton()
c801d85f 97{
6de97a3b 98}
c801d85f 99
fd0eed64
RR
100wxButton::~wxButton()
101{
fd0eed64
RR
102}
103
c801d85f 104bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label,
32c77a71 105 const wxPoint &pos, const wxSize &size,
6de97a3b 106 long style, const wxValidator& validator, const wxString &name )
c801d85f 107{
93763ad5
WS
108 m_needParent = true;
109 m_acceptsFocus = true;
32c77a71 110
4dcaf11a
RR
111 if (!PreCreation( parent, pos, size ) ||
112 !CreateBase( parent, id, pos, size, style, validator, name ))
113 {
223d09f6 114 wxFAIL_MSG( wxT("wxButton creation failed") );
93763ad5 115 return false;
4dcaf11a 116 }
c801d85f 117
5f7bcb48 118 m_widget = gtk_button_new_with_mnemonic("");
354aa1e3 119
2e8613b7
RR
120 float x_alignment = 0.5;
121 if (HasFlag(wxBU_LEFT))
122 x_alignment = 0.0;
123 else if (HasFlag(wxBU_RIGHT))
124 x_alignment = 1.0;
125
126 float y_alignment = 0.5;
127 if (HasFlag(wxBU_TOP))
128 y_alignment = 0.0;
129 else if (HasFlag(wxBU_BOTTOM))
130 y_alignment = 1.0;
131
3dc786e0 132#ifdef __WXGTK24__
77f70672
RR
133 if (!gtk_check_version(2,4,0))
134 {
135 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
136 }
137 else
4fa87bd9 138#endif
77f70672 139 {
afa7bd1e
MR
140 if (GTK_IS_MISC(GTK_BIN(m_widget)->child))
141 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(m_widget)->child),
77f70672
RR
142 x_alignment, y_alignment);
143 }
a696db45 144
5f7bcb48 145 SetLabel(label);
354aa1e3 146
de1c750f
RR
147 if (style & wxNO_BORDER)
148 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 149
9fa72bd2
MR
150 g_signal_connect_after (m_widget, "clicked",
151 G_CALLBACK (gtk_button_clicked_callback),
152 this);
c801d85f 153
9fa72bd2
MR
154 g_signal_connect_after (m_widget, "style_set",
155 G_CALLBACK (gtk_button_style_set_callback),
156 this);
b2ff89d6 157
f03fc89f 158 m_parent->DoAddChild( this );
9e691f46 159
abdeb9e7 160 PostCreation(size);
db434467 161
4fa87bd9
VS
162 return true;
163}
b2ff89d6 164
32c77a71 165
da048e3d 166void wxButton::SetDefault()
c801d85f 167{
97149f18
RD
168 wxWindow *parent = GetParent();
169 wxCHECK_RET( parent, _T("button without parent?") );
170
a00b2d0a 171 parent->SetDefaultItem(this);
b2ff89d6 172
3502e687
RR
173 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
174 gtk_widget_grab_default( m_widget );
b2ff89d6 175
f893066b
RR
176 // resize for default border
177 gtk_button_style_set_callback( m_widget, NULL, this );
6de97a3b 178}
c801d85f 179
ebea0891 180/* static */
4fa87bd9 181wxSize wxButtonBase::GetDefaultSize()
8dbf4589 182{
4fa87bd9
VS
183 static wxSize size = wxDefaultSize;
184 if (size == wxDefaultSize)
185 {
186 // NB: Default size of buttons should be same as size of stock
187 // buttons as used in most GTK+ apps. Unfortunately it's a little
188 // tricky to obtain this size: stock button's size may be smaller
189 // than size of button in GtkButtonBox and vice versa,
190 // GtkButtonBox's minimal button size may be smaller than stock
191 // button's size. We have to retrieve both values and combine them.
192
193 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
194 GtkWidget *box = gtk_hbutton_box_new();
195 GtkWidget *btn = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
196 gtk_container_add(GTK_CONTAINER(box), btn);
197 gtk_container_add(GTK_CONTAINER(wnd), box);
198 GtkRequisition req;
199 gtk_widget_size_request(btn, &req);
200
201 gint minwidth, minheight;
202 gtk_widget_style_get(box,
203 "child-min-width", &minwidth,
204 "child-min-height", &minheight,
205 NULL);
206
207 size.x = wxMax(minwidth, req.width);
208 size.y = wxMax(minheight, req.height);
b2ff89d6 209
4fa87bd9
VS
210 gtk_widget_destroy(wnd);
211 }
212 return size;
8dbf4589
RR
213}
214
5f7bcb48 215void wxButton::SetLabel( const wxString &lbl )
c801d85f 216{
223d09f6 217 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
9e691f46 218
5f7bcb48
VS
219 wxString label(lbl);
220
5f7bcb48
VS
221 if (label.empty() && wxIsStockID(m_windowId))
222 label = wxGetStockLabel(m_windowId);
5f7bcb48
VS
223
224 wxControl::SetLabel(label);
9e691f46 225
b2ff89d6
VZ
226 const wxString labelGTK = GTKConvertMnemonics(label);
227
5f7bcb48
VS
228 if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label))
229 {
230 const char *stock = wxGetStockGtkID(m_windowId);
231 if (stock)
232 {
233 gtk_button_set_label(GTK_BUTTON(m_widget), stock);
234 gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE);
b04683b1 235 return;
5f7bcb48 236 }
5f7bcb48
VS
237 }
238
b2ff89d6 239 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
5f7bcb48 240 gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE);
b2ff89d6 241
19ac2f44 242 ApplyWidgetStyle( false );
6de97a3b 243}
c801d85f 244
f03fc89f 245bool wxButton::Enable( bool enable )
a9c96bcc 246{
f03fc89f 247 if ( !wxControl::Enable( enable ) )
93763ad5 248 return false;
9e691f46 249
afa7bd1e 250 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
f03fc89f 251
93763ad5 252 return true;
a9c96bcc
RR
253}
254
2b5f62a0
VZ
255bool wxButton::IsOwnGtkWindow( GdkWindow *window )
256{
2b5f62a0 257 return GTK_BUTTON(m_widget)->event_window;
2b5f62a0
VZ
258}
259
f40fdaa3 260void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 261{
f40fdaa3 262 gtk_widget_modify_style(m_widget, style);
afa7bd1e 263 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
a81258be 264}
db434467
RR
265
266wxSize wxButton::DoGetBestSize() const
267{
4f819fe4
VZ
268 // the default button in wxGTK is bigger than the other ones because of an
269 // extra border around it, but we don't want to take it into account in
270 // our size calculations (otherwsie the result is visually ugly), so
271 // always return the size of non default button from here
272 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
273 if ( isDefault )
274 {
275 // temporarily unset default flag
276 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
277 }
278
db434467 279 wxSize ret( wxControl::DoGetBestSize() );
9e691f46 280
4f819fe4
VZ
281 if ( isDefault )
282 {
283 // set it back again
284 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
285 }
286
8ab696e0
RR
287 if (!HasFlag(wxBU_EXACTFIT))
288 {
4fa87bd9
VS
289 wxSize defaultSize = GetDefaultSize();
290 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
291 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
8ab696e0 292 }
9e691f46 293
9f884528 294 CacheBestSize(ret);
db434467
RR
295 return ret;
296}
297
9d522606
RD
298// static
299wxVisualAttributes
300wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
301{
302 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
303}
304
1e6feb95 305#endif // wxUSE_BUTTON