]>
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 | ||
865bb325 | 46 | extern "C" { |
8e189077 | 47 | static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win ) |
6380910c | 48 | { |
acfd422a RR |
49 | if (g_isIdle) wxapp_install_idle_handler(); |
50 | ||
a2053b27 | 51 | if (!win->m_hasVMT) return; |
83624f79 | 52 | if (g_blockEventsOnDrag) return; |
828f655f | 53 | |
83624f79 | 54 | float diff = win->m_adjust->value - win->m_oldPos; |
6380910c | 55 | if (fabs(diff) < sensitivity) return; |
6380910c | 56 | |
83624f79 | 57 | wxEventType command = wxEVT_NULL; |
6380910c | 58 | |
83624f79 | 59 | float line_step = win->m_adjust->step_increment; |
6380910c | 60 | |
0f42a871 RR |
61 | if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP; |
62 | else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN; | |
83624f79 | 63 | else command = wxEVT_SCROLL_THUMBTRACK; |
8e189077 | 64 | |
66d78146 | 65 | int value = (int)ceil(win->m_adjust->value); |
6380910c | 66 | |
83624f79 RR |
67 | wxSpinEvent event( command, win->GetId()); |
68 | event.SetPosition( value ); | |
83624f79 | 69 | event.SetEventObject( win ); |
0f42a871 RR |
70 | |
71 | if ((win->GetEventHandler()->ProcessEvent( event )) && | |
72 | !event.IsAllowed() ) | |
73 | { | |
74 | /* program has vetoed */ | |
75 | win->m_adjust->value = win->m_oldPos; | |
76 | ||
77 | gtk_signal_disconnect_by_func( GTK_OBJECT (win->m_adjust), | |
78 | (GtkSignalFunc) gtk_spinbutt_callback, | |
79 | (gpointer) win ); | |
80 | ||
81 | gtk_signal_emit_by_name( GTK_OBJECT(win->m_adjust), "value_changed" ); | |
82 | ||
83 | gtk_signal_connect( GTK_OBJECT (win->m_adjust), | |
84 | "value_changed", | |
85 | (GtkSignalFunc) gtk_spinbutt_callback, | |
86 | (gpointer) win ); | |
87 | return; | |
88 | } | |
89 | ||
90 | win->m_oldPos = win->m_adjust->value; | |
e65cc56a RR |
91 | |
92 | /* always send a thumbtrack event */ | |
93 | if (command != wxEVT_SCROLL_THUMBTRACK) | |
94 | { | |
95 | command = wxEVT_SCROLL_THUMBTRACK; | |
96 | wxSpinEvent event2( command, win->GetId()); | |
97 | event2.SetPosition( value ); | |
98 | event2.SetEventObject( win ); | |
99 | win->GetEventHandler()->ProcessEvent( event2 ); | |
100 | } | |
8e189077 | 101 | } |
865bb325 | 102 | } |
8e189077 RR |
103 | |
104 | //----------------------------------------------------------------------------- | |
105 | // wxSpinButton | |
106 | //----------------------------------------------------------------------------- | |
107 | ||
108 | IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl) | |
2fa7c206 | 109 | IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent) |
8e189077 RR |
110 | |
111 | BEGIN_EVENT_TABLE(wxSpinButton, wxControl) | |
112 | EVT_SIZE(wxSpinButton::OnSize) | |
113 | END_EVENT_TABLE() | |
114 | ||
31528cd3 VZ |
115 | bool wxSpinButton::Create(wxWindow *parent, |
116 | wxWindowID id, | |
117 | const wxPoint& pos, | |
118 | const wxSize& size, | |
119 | long style, | |
120 | const wxString& name) | |
8e189077 | 121 | { |
83624f79 | 122 | m_needParent = TRUE; |
6380910c | 123 | |
9d9b7755 VZ |
124 | wxSize new_size = size, |
125 | sizeBest = DoGetBestSize(); | |
126 | new_size.x = sizeBest.x; // override width always | |
6380910c | 127 | if (new_size.y == -1) |
9d9b7755 | 128 | new_size.y = sizeBest.y; |
6380910c | 129 | |
2259e007 | 130 | if (!PreCreation( parent, pos, new_size ) || |
4dcaf11a RR |
131 | !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name )) |
132 | { | |
223d09f6 | 133 | wxFAIL_MSG( wxT("wxXX creation failed") ); |
0f42a871 | 134 | return FALSE; |
4dcaf11a | 135 | } |
8e189077 | 136 | |
83624f79 | 137 | m_oldPos = 0.0; |
8e189077 | 138 | |
83624f79 | 139 | m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0); |
6380910c | 140 | |
83624f79 | 141 | m_widget = gtk_spin_button_new( m_adjust, 0, 0 ); |
6380910c | 142 | |
b02da6b1 VZ |
143 | gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), |
144 | (int)(m_windowStyle & wxSP_WRAP) ); | |
6380910c VZ |
145 | |
146 | gtk_signal_connect( GTK_OBJECT (m_adjust), | |
83624f79 | 147 | "value_changed", |
6380910c VZ |
148 | (GtkSignalFunc) gtk_spinbutt_callback, |
149 | (gpointer) this ); | |
150 | ||
f03fc89f | 151 | m_parent->DoAddChild( this ); |
6380910c | 152 | |
abdeb9e7 | 153 | PostCreation(new_size); |
6380910c | 154 | |
83624f79 | 155 | return TRUE; |
8e189077 RR |
156 | } |
157 | ||
8e189077 RR |
158 | int wxSpinButton::GetMin() const |
159 | { | |
223d09f6 | 160 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); |
6380910c | 161 | |
66d78146 | 162 | return (int)ceil(m_adjust->lower); |
8e189077 RR |
163 | } |
164 | ||
165 | int wxSpinButton::GetMax() const | |
166 | { | |
223d09f6 | 167 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); |
6380910c | 168 | |
66d78146 | 169 | return (int)ceil(m_adjust->upper); |
8e189077 RR |
170 | } |
171 | ||
172 | int wxSpinButton::GetValue() const | |
173 | { | |
223d09f6 | 174 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); |
6380910c | 175 | |
66d78146 | 176 | return (int)ceil(m_adjust->value); |
8e189077 RR |
177 | } |
178 | ||
179 | void wxSpinButton::SetValue( int value ) | |
180 | { | |
223d09f6 | 181 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); |
6380910c | 182 | |
83624f79 RR |
183 | float fpos = (float)value; |
184 | m_oldPos = fpos; | |
6380910c VZ |
185 | if (fabs(fpos-m_adjust->value) < sensitivity) return; |
186 | ||
83624f79 | 187 | m_adjust->value = fpos; |
6380910c | 188 | |
83624f79 | 189 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); |
8e189077 RR |
190 | } |
191 | ||
192 | void wxSpinButton::SetRange(int minVal, int maxVal) | |
193 | { | |
223d09f6 | 194 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); |
6380910c | 195 | |
83624f79 RR |
196 | float fmin = (float)minVal; |
197 | float fmax = (float)maxVal; | |
6380910c VZ |
198 | |
199 | if ((fabs(fmin-m_adjust->lower) < sensitivity) && | |
200 | (fabs(fmax-m_adjust->upper) < sensitivity)) | |
83624f79 RR |
201 | { |
202 | return; | |
203 | } | |
6380910c | 204 | |
83624f79 RR |
205 | m_adjust->lower = fmin; |
206 | m_adjust->upper = fmax; | |
8e189077 | 207 | |
83624f79 | 208 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
65045edd RR |
209 | |
210 | // these two calls are required due to some bug in GTK | |
211 | Refresh(); | |
212 | SetFocus(); | |
8e189077 RR |
213 | } |
214 | ||
215 | void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) ) | |
216 | { | |
223d09f6 | 217 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); |
6380910c | 218 | |
9d9b7755 | 219 | m_width = DoGetBestSize().x; |
83624f79 | 220 | gtk_widget_set_usize( m_widget, m_width, m_height ); |
8e189077 RR |
221 | } |
222 | ||
223 | bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window ) | |
224 | { | |
83624f79 | 225 | return GTK_SPIN_BUTTON(m_widget)->panel == window; |
8e189077 RR |
226 | } |
227 | ||
9d9b7755 VZ |
228 | wxSize wxSpinButton::DoGetBestSize() const |
229 | { | |
9f884528 RD |
230 | wxSize best(15, 26); // FIXME |
231 | CacheBestSize(best); | |
232 | return best; | |
9d9b7755 VZ |
233 | } |
234 | ||
9d522606 RD |
235 | // static |
236 | wxVisualAttributes | |
237 | wxSpinButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
238 | { | |
239 | // TODO: overload to accept functions like gtk_spin_button_new? | |
240 | // Until then use a similar type | |
241 | return GetDefaultAttributesFromGTKWidget(gtk_button_new); | |
242 | } | |
243 | ||
31528cd3 | 244 | #endif |