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