]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: spinbutt.cpp | |
3 | // Purpose: wxSpinCtrl | |
4 | // Author: Robert | |
5 | // Modified by: | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) Robert Roebling | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "spinctrl.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/spinctrl.h" | |
16 | ||
17 | #if wxUSE_SPINCTRL | |
18 | ||
19 | #include "wx/utils.h" | |
20 | #include "wx/spinbutt.h" | |
21 | ||
22 | #include <math.h> | |
23 | ||
24 | #include <gdk/gdk.h> | |
25 | #include <gtk/gtk.h> | |
26 | ||
27 | //----------------------------------------------------------------------------- | |
28 | // idle system | |
29 | //----------------------------------------------------------------------------- | |
30 | ||
31 | extern void wxapp_install_idle_handler(); | |
32 | extern bool g_isIdle; | |
33 | ||
34 | //----------------------------------------------------------------------------- | |
35 | // data | |
36 | //----------------------------------------------------------------------------- | |
37 | ||
38 | extern bool g_blockEventsOnDrag; | |
39 | ||
40 | static const float sensitivity = 0.02; | |
41 | ||
42 | //----------------------------------------------------------------------------- | |
43 | // "value_changed" | |
44 | //----------------------------------------------------------------------------- | |
45 | ||
46 | static void gtk_spinctrl_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win ) | |
47 | { | |
48 | if (g_isIdle) wxapp_install_idle_handler(); | |
49 | ||
50 | if (!win->m_hasVMT) return; | |
51 | if (g_blockEventsOnDrag) return; | |
52 | ||
53 | float diff = win->m_adjust->value - win->m_oldPos; | |
54 | if (fabs(diff) < sensitivity) return; | |
55 | win->m_oldPos = win->m_adjust->value; | |
56 | ||
57 | wxEventType command = wxEVT_NULL; | |
58 | ||
59 | float line_step = win->m_adjust->step_increment; | |
60 | ||
61 | if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN; | |
62 | else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP; | |
63 | else command = wxEVT_SCROLL_THUMBTRACK; | |
64 | ||
65 | int value = (int)ceil(win->m_adjust->value); | |
66 | ||
67 | wxSpinEvent event( command, win->GetId()); | |
68 | event.SetPosition( value ); | |
69 | event.SetEventObject( win ); | |
70 | win->GetEventHandler()->ProcessEvent( event ); | |
71 | ||
72 | /* always send a thumbtrack event */ | |
73 | if (command != wxEVT_SCROLL_THUMBTRACK) | |
74 | { | |
75 | command = wxEVT_SCROLL_THUMBTRACK; | |
76 | wxSpinEvent event2( command, win->GetId()); | |
77 | event2.SetPosition( value ); | |
78 | event2.SetEventObject( win ); | |
79 | win->GetEventHandler()->ProcessEvent( event2 ); | |
80 | } | |
81 | } | |
82 | ||
83 | //----------------------------------------------------------------------------- | |
84 | // wxSpinCtrl | |
85 | //----------------------------------------------------------------------------- | |
86 | ||
87 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl) | |
88 | ||
89 | BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl) | |
90 | EVT_CHAR(wxSpinCtrl::OnChar) | |
91 | END_EVENT_TABLE() | |
92 | ||
93 | bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id, | |
94 | const wxString& value, | |
95 | const wxPoint& pos, const wxSize& size, | |
96 | long style, | |
97 | int min, int max, int initial, | |
98 | const wxString& name) | |
99 | { | |
100 | m_needParent = TRUE; | |
101 | m_acceptsFocus = TRUE; | |
102 | ||
103 | wxSize new_size = size; | |
104 | if (new_size.y == -1) | |
105 | new_size.y = 26; | |
106 | ||
107 | if (!PreCreation( parent, pos, new_size ) || | |
108 | !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name )) | |
109 | { | |
110 | wxFAIL_MSG( wxT("wxSpinCtrl creation failed") ); | |
111 | return FALSE; | |
112 | } | |
113 | ||
114 | m_oldPos = initial; | |
115 | ||
116 | m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0); | |
117 | ||
118 | m_widget = gtk_spin_button_new( m_adjust, 1, 0 ); | |
119 | ||
120 | gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), | |
121 | (int)(m_windowStyle & wxSP_WRAP) ); | |
122 | ||
123 | gtk_signal_connect( GTK_OBJECT (m_adjust), | |
124 | "value_changed", | |
125 | (GtkSignalFunc) gtk_spinctrl_callback, | |
126 | (gpointer) this ); | |
127 | ||
128 | m_parent->DoAddChild( this ); | |
129 | ||
130 | PostCreation(); | |
131 | ||
132 | SetBackgroundColour( parent->GetBackgroundColour() ); | |
133 | ||
134 | SetValue( value ); | |
135 | ||
136 | Show( TRUE ); | |
137 | ||
138 | return TRUE; | |
139 | } | |
140 | ||
141 | int wxSpinCtrl::GetMin() const | |
142 | { | |
143 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
144 | ||
145 | return (int)ceil(m_adjust->lower); | |
146 | } | |
147 | ||
148 | int wxSpinCtrl::GetMax() const | |
149 | { | |
150 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
151 | ||
152 | return (int)ceil(m_adjust->upper); | |
153 | } | |
154 | ||
155 | int wxSpinCtrl::GetValue() const | |
156 | { | |
157 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
158 | ||
159 | return (int)ceil(m_adjust->value); | |
160 | } | |
161 | ||
162 | void wxSpinCtrl::SetValue( const wxString& value ) | |
163 | { | |
164 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
165 | ||
166 | int n; | |
167 | if ( (wxSscanf(value, wxT("%d"), &n) == 1) ) | |
168 | { | |
169 | // a number - set it | |
170 | SetValue(n); | |
171 | } | |
172 | else | |
173 | { | |
174 | // invalid number - set text as is (wxMSW compatible) | |
175 | gtk_entry_set_text( GTK_ENTRY(m_widget), value.mbc_str() ); | |
176 | } | |
177 | } | |
178 | ||
179 | void wxSpinCtrl::SetValue( int value ) | |
180 | { | |
181 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
182 | ||
183 | float fpos = (float)value; | |
184 | m_oldPos = fpos; | |
185 | if (fabs(fpos-m_adjust->value) < sensitivity) return; | |
186 | ||
187 | m_adjust->value = fpos; | |
188 | ||
189 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); | |
190 | } | |
191 | ||
192 | void wxSpinCtrl::SetRange(int minVal, int maxVal) | |
193 | { | |
194 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
195 | ||
196 | float fmin = (float)minVal; | |
197 | float fmax = (float)maxVal; | |
198 | ||
199 | if ((fabs(fmin-m_adjust->lower) < sensitivity) && | |
200 | (fabs(fmax-m_adjust->upper) < sensitivity)) | |
201 | { | |
202 | return; | |
203 | } | |
204 | ||
205 | m_adjust->lower = fmin; | |
206 | m_adjust->upper = fmax; | |
207 | ||
208 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); | |
209 | ||
210 | // these two calls are required due to some bug in GTK | |
211 | Refresh(); | |
212 | SetFocus(); | |
213 | } | |
214 | ||
215 | void wxSpinCtrl::OnChar( wxKeyEvent &event ) | |
216 | { | |
217 | wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") ); | |
218 | ||
219 | if (event.KeyCode() == WXK_RETURN) | |
220 | { | |
221 | wxWindow *top_frame = m_parent; | |
222 | while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame)) | |
223 | top_frame = top_frame->GetParent(); | |
224 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); | |
225 | ||
226 | if (window->default_widget) | |
227 | { | |
228 | gtk_widget_activate (window->default_widget); | |
229 | return; | |
230 | } | |
231 | } | |
232 | ||
233 | event.Skip(); | |
234 | } | |
235 | ||
236 | bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window ) | |
237 | { | |
238 | return GTK_SPIN_BUTTON(m_widget)->panel == window; | |
239 | } | |
240 | ||
241 | void wxSpinCtrl::ApplyWidgetStyle() | |
242 | { | |
243 | SetWidgetStyle(); | |
244 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
245 | } | |
246 | ||
247 | #endif | |
248 | // wxUSE_SPINCTRL |