]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/spinbutt.cpp
Simplify wxWinCE #ifdefs.
[wxWidgets.git] / src / gtk1 / spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #include "wx/spinbutt.h"
15
16 #if wxUSE_SPINBTN
17
18 #include "wx/utils.h"
19 #include "wx/math.h"
20 #include "wx/gtk1/private.h"
21
22 //-----------------------------------------------------------------------------
23 // idle system
24 //-----------------------------------------------------------------------------
25
26 extern void wxapp_install_idle_handler();
27 extern bool g_isIdle;
28
29 //-----------------------------------------------------------------------------
30 // data
31 //-----------------------------------------------------------------------------
32
33 extern bool g_blockEventsOnDrag;
34
35 static const float sensitivity = 0.02;
36
37 //-----------------------------------------------------------------------------
38 // "value_changed"
39 //-----------------------------------------------------------------------------
40
41 extern "C" {
42 static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win )
43 {
44 if (g_isIdle) wxapp_install_idle_handler();
45
46 if (!win->m_hasVMT) return;
47 if (g_blockEventsOnDrag) return;
48
49 float diff = win->m_adjust->value - win->m_oldPos;
50 if (fabs(diff) < sensitivity) return;
51
52 wxEventType command = wxEVT_NULL;
53
54 float line_step = win->m_adjust->step_increment;
55
56 if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP;
57 else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN;
58 else command = wxEVT_SCROLL_THUMBTRACK;
59
60 int value = (int)ceil(win->m_adjust->value);
61
62 wxSpinEvent event( command, win->GetId());
63 event.SetPosition( value );
64 event.SetEventObject( win );
65
66 if ((win->GetEventHandler()->ProcessEvent( event )) &&
67 !event.IsAllowed() )
68 {
69 /* program has vetoed */
70 win->m_adjust->value = win->m_oldPos;
71
72 gtk_signal_disconnect_by_func( GTK_OBJECT (win->m_adjust),
73 (GtkSignalFunc) gtk_spinbutt_callback,
74 (gpointer) win );
75
76 gtk_signal_emit_by_name( GTK_OBJECT(win->m_adjust), "value_changed" );
77
78 gtk_signal_connect( GTK_OBJECT (win->m_adjust),
79 "value_changed",
80 (GtkSignalFunc) gtk_spinbutt_callback,
81 (gpointer) win );
82 return;
83 }
84
85 win->m_oldPos = win->m_adjust->value;
86
87 /* always send a thumbtrack event */
88 if (command != wxEVT_SCROLL_THUMBTRACK)
89 {
90 command = wxEVT_SCROLL_THUMBTRACK;
91 wxSpinEvent event2( command, win->GetId());
92 event2.SetPosition( value );
93 event2.SetEventObject( win );
94 win->GetEventHandler()->ProcessEvent( event2 );
95 }
96 }
97 }
98
99 //-----------------------------------------------------------------------------
100 // wxSpinButton
101 //-----------------------------------------------------------------------------
102
103 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
104 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
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