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