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