]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/button.cpp
Applied #15375 to stop event-sending in generic wxSpinCtrl ctor (eco)
[wxWidgets.git] / src / gtk1 / button.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/gtk1/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
2cdcee61 14#ifndef WX_PRECOMP
94aff5ff 15 #include "wx/button.h"
2cdcee61
WS
16#endif
17
5f7bcb48 18#include "wx/stockitem.h"
c801d85f 19
3cbab641
MR
20#include "wx/gtk1/private.h"
21#include "wx/gtk1/win_gtk.h"
83624f79 22
c801d85f
KB
23//-----------------------------------------------------------------------------
24// classes
25//-----------------------------------------------------------------------------
26
27class wxButton;
28
acfd422a
RR
29//-----------------------------------------------------------------------------
30// idle system
31//-----------------------------------------------------------------------------
32
33extern void wxapp_install_idle_handler();
34extern bool g_isIdle;
35
66bd6b93
RR
36//-----------------------------------------------------------------------------
37// data
38//-----------------------------------------------------------------------------
39
40extern bool g_blockEventsOnDrag;
41
c801d85f 42//-----------------------------------------------------------------------------
e1e955e1 43// "clicked"
c801d85f
KB
44//-----------------------------------------------------------------------------
45
865bb325 46extern "C" {
66bd6b93 47static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button )
c801d85f 48{
9e691f46 49 if (g_isIdle)
32ac755d 50 wxapp_install_idle_handler();
acfd422a 51
a2053b27 52 if (!button->m_hasVMT) return;
acfd422a 53 if (g_blockEventsOnDrag) return;
9e691f46 54
ce7fe42e 55 wxCommandEvent event(wxEVT_BUTTON, button->GetId());
acfd422a 56 event.SetEventObject(button);
937013e0 57 button->HandleWindowEvent(event);
6de97a3b 58}
865bb325 59}
c801d85f 60
a90c0600
RR
61//-----------------------------------------------------------------------------
62// "style_set" from m_widget
63//-----------------------------------------------------------------------------
64
65static gint
66gtk_button_style_set_callback( GtkWidget *m_widget, GtkStyle *WXUNUSED(style), wxButton *win )
67{
68 if (g_isIdle)
69 wxapp_install_idle_handler();
b2ff89d6 70
f893066b
RR
71 int left_border = 0;
72 int right_border = 0;
73 int top_border = 0;
74 int bottom_border = 0;
b2ff89d6 75
f893066b
RR
76 /* the default button has a border around it */
77 if (GTK_WIDGET_CAN_DEFAULT(m_widget))
78 {
f893066b
RR
79 left_border = 6;
80 right_border = 6;
81 top_border = 6;
82 bottom_border = 5;
f893066b
RR
83 win->DoMoveWindow( win->m_x-top_border,
84 win->m_y-left_border,
85 win->m_width+left_border+right_border,
86 win->m_height+top_border+bottom_border );
b2ff89d6 87 }
a90c0600
RR
88
89 return FALSE;
90}
91
c801d85f 92//-----------------------------------------------------------------------------
e1e955e1
RR
93// wxButton
94//-----------------------------------------------------------------------------
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
354aa1e3
RR
118 m_widget = gtk_button_new_with_label("");
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
3cbab641
MR
132 if (GTK_IS_MISC(BUTTON_CHILD(m_widget)))
133 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget)),
77f70672 134 x_alignment, y_alignment);
a696db45 135
5f7bcb48 136 SetLabel(label);
354aa1e3 137
de1c750f
RR
138 if (style & wxNO_BORDER)
139 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 140
58b907f6 141 gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked",
b292e2f5 142 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
c801d85f 143
a90c0600
RR
144 gtk_signal_connect_after( GTK_OBJECT(m_widget), "style_set",
145 GTK_SIGNAL_FUNC(gtk_button_style_set_callback), (gpointer*) this );
b2ff89d6 146
f03fc89f 147 m_parent->DoAddChild( this );
9e691f46 148
abdeb9e7 149 PostCreation(size);
db434467 150
4fa87bd9
VS
151 return true;
152}
b2ff89d6 153
32c77a71 154
94aff5ff 155wxWindow *wxButton::SetDefault()
c801d85f 156{
94aff5ff 157 wxWindow *oldDefault = wxButtonBase::SetDefault();
b2ff89d6 158
3502e687
RR
159 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
160 gtk_widget_grab_default( m_widget );
b2ff89d6 161
f893066b
RR
162 // resize for default border
163 gtk_button_style_set_callback( m_widget, NULL, this );
94aff5ff
VZ
164
165 return oldDefault;
6de97a3b 166}
c801d85f 167
ebea0891 168/* static */
4fa87bd9 169wxSize wxButtonBase::GetDefaultSize()
8dbf4589
RR
170{
171 return wxSize(80,26);
172}
173
5f7bcb48 174void wxButton::SetLabel( const wxString &lbl )
c801d85f 175{
223d09f6 176 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
9e691f46 177
5f7bcb48
VS
178 wxString label(lbl);
179
5f7bcb48
VS
180 if (label.empty() && wxIsStockID(m_windowId))
181 label = wxGetStockLabel(m_windowId);
5f7bcb48
VS
182
183 wxControl::SetLabel(label);
9e691f46 184
ee6dd41a 185 const wxString labelGTK = GTKRemoveMnemonics(label);
b2ff89d6 186
b2ff89d6 187 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV(labelGTK));
6de97a3b 188}
c801d85f 189
f03fc89f 190bool wxButton::Enable( bool enable )
a9c96bcc 191{
f03fc89f 192 if ( !wxControl::Enable( enable ) )
93763ad5 193 return false;
9e691f46
VZ
194
195 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
f03fc89f 196
93763ad5 197 return true;
a9c96bcc
RR
198}
199
2b5f62a0
VZ
200bool wxButton::IsOwnGtkWindow( GdkWindow *window )
201{
2b5f62a0 202 return (window == m_widget->window);
2b5f62a0
VZ
203}
204
f40fdaa3 205void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 206{
f40fdaa3
VS
207 gtk_widget_modify_style(m_widget, style);
208 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
a81258be 209}
db434467
RR
210
211wxSize wxButton::DoGetBestSize() const
212{
4f819fe4
VZ
213 // the default button in wxGTK is bigger than the other ones because of an
214 // extra border around it, but we don't want to take it into account in
215 // our size calculations (otherwsie the result is visually ugly), so
216 // always return the size of non default button from here
217 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
218 if ( isDefault )
219 {
220 // temporarily unset default flag
221 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
222 }
223
db434467 224 wxSize ret( wxControl::DoGetBestSize() );
9e691f46 225
4f819fe4
VZ
226 if ( isDefault )
227 {
228 // set it back again
229 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
230 }
231
391ddf40 232 ret.x += 10; // add a few pixels for sloppy (but common) themes
b2ff89d6 233
8ab696e0
RR
234 if (!HasFlag(wxBU_EXACTFIT))
235 {
4fa87bd9
VS
236 wxSize defaultSize = GetDefaultSize();
237 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
238 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
8ab696e0 239 }
9e691f46 240
9f884528 241 CacheBestSize(ret);
db434467
RR
242 return ret;
243}
244
9d522606
RD
245// static
246wxVisualAttributes
247wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
248{
249 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
250}
251
1e6feb95 252#endif // wxUSE_BUTTON