]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/spinbutt.cpp
Applied colspan corrections, #15274 and #15275 (dghart)
[wxWidgets.git] / src / gtk1 / spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/spinbutt.cpp
3 // Purpose: wxSpinButton
4 // Author: Robert
5 // Modified by:
6 // RCS-ID: $Id$
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #if wxUSE_SPINBTN
15
16 #include "wx/spinbutt.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/utils.h"
20 #include "wx/math.h"
21 #endif
22
23 #include "wx/gtk1/private.h"
24
25 //-----------------------------------------------------------------------------
26 // idle system
27 //-----------------------------------------------------------------------------
28
29 extern void wxapp_install_idle_handler();
30 extern bool g_isIdle;
31
32 //-----------------------------------------------------------------------------
33 // data
34 //-----------------------------------------------------------------------------
35
36 extern bool g_blockEventsOnDrag;
37
38 static const float sensitivity = 0.02;
39
40 //-----------------------------------------------------------------------------
41 // "value_changed"
42 //-----------------------------------------------------------------------------
43
44 extern "C" {
45 static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win )
46 {
47 if (g_isIdle) wxapp_install_idle_handler();
48
49 if (!win->m_hasVMT) return;
50 if (g_blockEventsOnDrag) return;
51
52 float diff = win->m_adjust->value - win->m_oldPos;
53 if (fabs(diff) < sensitivity) return;
54
55 wxEventType command = wxEVT_NULL;
56
57 float line_step = win->m_adjust->step_increment;
58
59 if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP;
60 else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN;
61 else command = wxEVT_SCROLL_THUMBTRACK;
62
63 int value = (int)ceil(win->m_adjust->value);
64
65 wxSpinEvent event( command, win->GetId());
66 event.SetPosition( value );
67 event.SetEventObject( win );
68
69 if ((win->HandleWindowEvent( event )) &&
70 !event.IsAllowed() )
71 {
72 /* program has vetoed */
73 win->m_adjust->value = win->m_oldPos;
74
75 gtk_signal_disconnect_by_func( GTK_OBJECT (win->m_adjust),
76 (GtkSignalFunc) gtk_spinbutt_callback,
77 (gpointer) win );
78
79 gtk_signal_emit_by_name( GTK_OBJECT(win->m_adjust), "value_changed" );
80
81 gtk_signal_connect( GTK_OBJECT (win->m_adjust),
82 "value_changed",
83 (GtkSignalFunc) gtk_spinbutt_callback,
84 (gpointer) win );
85 return;
86 }
87
88 win->m_oldPos = win->m_adjust->value;
89
90 /* always send a thumbtrack event */
91 if (command != wxEVT_SCROLL_THUMBTRACK)
92 {
93 command = wxEVT_SCROLL_THUMBTRACK;
94 wxSpinEvent event2( command, win->GetId());
95 event2.SetPosition( value );
96 event2.SetEventObject( win );
97 win->HandleWindowEvent( event2 );
98 }
99 }
100 }
101
102 //-----------------------------------------------------------------------------
103 // wxSpinButton
104 //-----------------------------------------------------------------------------
105
106 BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
107 EVT_SIZE(wxSpinButton::OnSize)
108 END_EVENT_TABLE()
109
110 bool wxSpinButton::Create(wxWindow *parent,
111 wxWindowID id,
112 const wxPoint& pos,
113 const wxSize& size,
114 long style,
115 const wxString& name)
116 {
117 m_needParent = true;
118
119 wxSize new_size = size,
120 sizeBest = DoGetBestSize();
121 new_size.x = sizeBest.x; // override width always
122 if (new_size.y == -1)
123 new_size.y = sizeBest.y;
124
125 if (!PreCreation( parent, pos, new_size ) ||
126 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
127 {
128 wxFAIL_MSG( wxT("wxXX creation failed") );
129 return false;
130 }
131
132 m_oldPos = 0.0;
133
134 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
135
136 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
137
138 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
139 (int)(m_windowStyle & wxSP_WRAP) );
140
141 gtk_signal_connect( GTK_OBJECT (m_adjust),
142 "value_changed",
143 (GtkSignalFunc) gtk_spinbutt_callback,
144 (gpointer) this );
145
146 m_parent->DoAddChild( this );
147
148 PostCreation(new_size);
149
150 return true;
151 }
152
153 int wxSpinButton::GetMin() const
154 {
155 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
156
157 return (int)ceil(m_adjust->lower);
158 }
159
160 int wxSpinButton::GetMax() const
161 {
162 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
163
164 return (int)ceil(m_adjust->upper);
165 }
166
167 int wxSpinButton::GetValue() const
168 {
169 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
170
171 return (int)ceil(m_adjust->value);
172 }
173
174 void wxSpinButton::SetValue( int value )
175 {
176 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
177
178 float fpos = (float)value;
179 m_oldPos = fpos;
180 if (fabs(fpos-m_adjust->value) < sensitivity) return;
181
182 m_adjust->value = fpos;
183
184 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
185 }
186
187 void wxSpinButton::SetRange(int minVal, int maxVal)
188 {
189 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
190
191 float fmin = (float)minVal;
192 float fmax = (float)maxVal;
193
194 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
195 (fabs(fmax-m_adjust->upper) < sensitivity))
196 {
197 return;
198 }
199
200 m_adjust->lower = fmin;
201 m_adjust->upper = fmax;
202
203 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
204
205 // these two calls are required due to some bug in GTK
206 Refresh();
207 SetFocus();
208 }
209
210 void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
211 {
212 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
213
214 m_width = DoGetBestSize().x;
215 gtk_widget_set_usize( m_widget, m_width, m_height );
216 }
217
218 bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
219 {
220 return GTK_SPIN_BUTTON(m_widget)->panel == window;
221 }
222
223 wxSize wxSpinButton::DoGetBestSize() const
224 {
225 wxSize best(15, 26); // FIXME
226 CacheBestSize(best);
227 return best;
228 }
229
230 // static
231 wxVisualAttributes
232 wxSpinButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
233 {
234 // TODO: overload to accept functions like gtk_spin_button_new?
235 // Until then use a similar type
236 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
237 }
238
239 #endif // wxUSE_SPINBTN