]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/spinbutt.cpp
don't return wxFONTENCODING_DEFAULT from wxFont::GetEncoding(), this results in asser...
[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->GetEventHandler()->ProcessEvent( 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->GetEventHandler()->ProcessEvent( event2 );
98 }
99 }
100 }
101
102 //-----------------------------------------------------------------------------
103 // wxSpinButton
104 //-----------------------------------------------------------------------------
105
106 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl)
107 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
108
109 BEGIN_EVENT_TABLE(wxSpinButton, wxControl)
110 EVT_SIZE(wxSpinButton::OnSize)
111 END_EVENT_TABLE()
112
113 bool wxSpinButton::Create(wxWindow *parent,
114 wxWindowID id,
115 const wxPoint& pos,
116 const wxSize& size,
117 long style,
118 const wxString& name)
119 {
120 m_needParent = true;
121
122 wxSize new_size = size,
123 sizeBest = DoGetBestSize();
124 new_size.x = sizeBest.x; // override width always
125 if (new_size.y == -1)
126 new_size.y = sizeBest.y;
127
128 if (!PreCreation( parent, pos, new_size ) ||
129 !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name ))
130 {
131 wxFAIL_MSG( wxT("wxXX creation failed") );
132 return false;
133 }
134
135 m_oldPos = 0.0;
136
137 m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
138
139 m_widget = gtk_spin_button_new( m_adjust, 0, 0 );
140
141 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
142 (int)(m_windowStyle & wxSP_WRAP) );
143
144 gtk_signal_connect( GTK_OBJECT (m_adjust),
145 "value_changed",
146 (GtkSignalFunc) gtk_spinbutt_callback,
147 (gpointer) this );
148
149 m_parent->DoAddChild( this );
150
151 PostCreation(new_size);
152
153 return true;
154 }
155
156 int wxSpinButton::GetMin() const
157 {
158 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
159
160 return (int)ceil(m_adjust->lower);
161 }
162
163 int wxSpinButton::GetMax() const
164 {
165 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
166
167 return (int)ceil(m_adjust->upper);
168 }
169
170 int wxSpinButton::GetValue() const
171 {
172 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
173
174 return (int)ceil(m_adjust->value);
175 }
176
177 void wxSpinButton::SetValue( int value )
178 {
179 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
180
181 float fpos = (float)value;
182 m_oldPos = fpos;
183 if (fabs(fpos-m_adjust->value) < sensitivity) return;
184
185 m_adjust->value = fpos;
186
187 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" );
188 }
189
190 void wxSpinButton::SetRange(int minVal, int maxVal)
191 {
192 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
193
194 float fmin = (float)minVal;
195 float fmax = (float)maxVal;
196
197 if ((fabs(fmin-m_adjust->lower) < sensitivity) &&
198 (fabs(fmax-m_adjust->upper) < sensitivity))
199 {
200 return;
201 }
202
203 m_adjust->lower = fmin;
204 m_adjust->upper = fmax;
205
206 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" );
207
208 // these two calls are required due to some bug in GTK
209 Refresh();
210 SetFocus();
211 }
212
213 void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) )
214 {
215 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
216
217 m_width = DoGetBestSize().x;
218 gtk_widget_set_usize( m_widget, m_width, m_height );
219 }
220
221 bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window )
222 {
223 return GTK_SPIN_BUTTON(m_widget)->panel == window;
224 }
225
226 wxSize wxSpinButton::DoGetBestSize() const
227 {
228 wxSize best(15, 26); // FIXME
229 CacheBestSize(best);
230 return best;
231 }
232
233 // static
234 wxVisualAttributes
235 wxSpinButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
236 {
237 // TODO: overload to accept functions like gtk_spin_button_new?
238 // Until then use a similar type
239 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
240 }
241
242 #endif // wxUSE_SPINBTN