]>
Commit | Line | Data |
---|---|---|
738f9e5a RR |
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 | #ifdef wxUSE_SPINBTN | |
18 | ||
19 | #include "wx/utils.h" | |
20 | #include "wx/spinbutt.h" | |
21 | #include <math.h> | |
22 | ||
23 | #include "gdk/gdk.h" | |
24 | #include "gtk/gtk.h" | |
25 | ||
26 | //----------------------------------------------------------------------------- | |
27 | // idle system | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | extern void wxapp_install_idle_handler(); | |
31 | extern bool g_isIdle; | |
32 | ||
33 | //----------------------------------------------------------------------------- | |
34 | // data | |
35 | //----------------------------------------------------------------------------- | |
36 | ||
37 | extern bool g_blockEventsOnDrag; | |
38 | ||
39 | static const float sensitivity = 0.02; | |
40 | ||
41 | //----------------------------------------------------------------------------- | |
42 | // "value_changed" | |
43 | //----------------------------------------------------------------------------- | |
44 | ||
45 | static void gtk_spinctrl_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *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 | win->m_oldPos = win->m_adjust->value; | |
55 | ||
56 | wxEventType command = wxEVT_NULL; | |
57 | ||
58 | float line_step = win->m_adjust->step_increment; | |
59 | ||
60 | if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN; | |
61 | else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP; | |
62 | else command = wxEVT_SCROLL_THUMBTRACK; | |
63 | ||
64 | int value = (int)ceil(win->m_adjust->value); | |
65 | ||
66 | wxSpinEvent event( command, win->GetId()); | |
67 | event.SetPosition( value ); | |
68 | event.SetEventObject( win ); | |
69 | win->GetEventHandler()->ProcessEvent( event ); | |
70 | ||
71 | /* always send a thumbtrack event */ | |
72 | if (command != wxEVT_SCROLL_THUMBTRACK) | |
73 | { | |
74 | command = wxEVT_SCROLL_THUMBTRACK; | |
75 | wxSpinEvent event2( command, win->GetId()); | |
76 | event2.SetPosition( value ); | |
77 | event2.SetEventObject( win ); | |
78 | win->GetEventHandler()->ProcessEvent( event2 ); | |
79 | } | |
80 | } | |
81 | ||
82 | //----------------------------------------------------------------------------- | |
83 | // wxSpinCtrl | |
84 | //----------------------------------------------------------------------------- | |
85 | ||
86 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl) | |
87 | ||
da048e3d RR |
88 | BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl) |
89 | EVT_CHAR(wxSpinCtrl::OnChar) | |
90 | END_EVENT_TABLE() | |
91 | ||
738f9e5a | 92 | bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id, |
ce89fdd2 VZ |
93 | const wxString& value, |
94 | const wxPoint& pos, const wxSize& size, | |
95 | long style, | |
96 | int min, int max, int initial, | |
97 | const wxString& name) | |
738f9e5a RR |
98 | { |
99 | m_needParent = TRUE; | |
354aa1e3 | 100 | m_acceptsFocus = TRUE; |
738f9e5a RR |
101 | |
102 | wxSize new_size = size; | |
103 | if (new_size.y == -1) | |
104 | new_size.y = 26; | |
105 | ||
106 | if (!PreCreation( parent, pos, new_size ) || | |
107 | !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name )) | |
108 | { | |
109 | wxFAIL_MSG( wxT("wxSpinCtrl creation failed") ); | |
110 | return FALSE; | |
111 | } | |
112 | ||
113 | m_oldPos = initial; | |
114 | ||
115 | m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0); | |
116 | ||
117 | m_widget = gtk_spin_button_new( m_adjust, 1, 0 ); | |
118 | ||
119 | gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), (m_windowStyle & wxSP_WRAP) ); | |
120 | ||
121 | gtk_signal_connect( GTK_OBJECT (m_adjust), | |
122 | "value_changed", | |
123 | (GtkSignalFunc) gtk_spinctrl_callback, | |
124 | (gpointer) this ); | |
125 | ||
126 | m_parent->DoAddChild( this ); | |
127 | ||
128 | PostCreation(); | |
129 | ||
130 | SetBackgroundColour( parent->GetBackgroundColour() ); | |
131 | ||
ce89fdd2 VZ |
132 | SetValue( value ); |
133 | ||
738f9e5a RR |
134 | Show( TRUE ); |
135 | ||
136 | return TRUE; | |
137 | } | |
138 | ||
139 | int wxSpinCtrl::GetMin() const | |
140 | { | |
141 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
142 | ||
143 | return (int)ceil(m_adjust->lower); | |
144 | } | |
145 | ||
146 | int wxSpinCtrl::GetMax() const | |
147 | { | |
148 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
149 | ||
150 | return (int)ceil(m_adjust->upper); | |
151 | } | |
152 | ||
153 | int wxSpinCtrl::GetValue() const | |
154 | { | |
155 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
156 | ||
157 | return (int)ceil(m_adjust->value); | |
158 | } | |
159 | ||
ce89fdd2 VZ |
160 | void wxSpinCtrl::SetValue( const wxString& value ) |
161 | { | |
162 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
163 | ||
164 | int n; | |
165 | if ( (wxSscanf(value, wxT("%d"), &n) == 1) ) | |
166 | { | |
167 | // a number - set it | |
168 | SetValue(n); | |
169 | } | |
170 | else | |
171 | { | |
172 | // invalid number - set text as is (wxMSW compatible) | |
7dd62924 | 173 | gtk_entry_set_text( GTK_ENTRY(m_widget), value.mbc_str() ); |
ce89fdd2 VZ |
174 | } |
175 | } | |
176 | ||
738f9e5a RR |
177 | void wxSpinCtrl::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 wxSpinCtrl::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 | ||
da048e3d RR |
213 | void wxSpinCtrl::OnChar( wxKeyEvent &event ) |
214 | { | |
215 | wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") ); | |
216 | ||
217 | if (event.KeyCode() == WXK_RETURN) | |
218 | { | |
219 | wxWindow *top_frame = m_parent; | |
220 | while (top_frame->GetParent() && !(top_frame->GetParent()->m_isFrame)) | |
221 | top_frame = top_frame->GetParent(); | |
222 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); | |
223 | ||
224 | if (window->default_widget) | |
225 | { | |
226 | gtk_widget_activate (window->default_widget); | |
227 | return; | |
228 | } | |
229 | } | |
230 | ||
231 | event.Skip(); | |
232 | } | |
233 | ||
738f9e5a RR |
234 | bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window ) |
235 | { | |
236 | return GTK_SPIN_BUTTON(m_widget)->panel == window; | |
237 | } | |
238 | ||
239 | void wxSpinCtrl::ApplyWidgetStyle() | |
240 | { | |
241 | SetWidgetStyle(); | |
242 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
243 | } | |
244 | ||
245 | #endif |