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