]>
Commit | Line | Data |
---|---|---|
8e189077 | 1 | ///////////////////////////////////////////////////////////////////////////// |
de6185e2 | 2 | // Name: src/gtk1/spinbutt.cpp |
8e189077 RR |
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 | ||
de6185e2 WS |
14 | #if wxUSE_SPINBTN |
15 | ||
8e189077 | 16 | #include "wx/spinbutt.h" |
dcf924a3 | 17 | |
de6185e2 WS |
18 | #ifndef WX_PRECOMP |
19 | #include "wx/utils.h" | |
20 | #endif | |
dcf924a3 | 21 | |
463c4d71 | 22 | #include "wx/math.h" |
3cbab641 | 23 | #include "wx/gtk1/private.h" |
83624f79 | 24 | |
acfd422a RR |
25 | //----------------------------------------------------------------------------- |
26 | // idle system | |
27 | //----------------------------------------------------------------------------- | |
28 | ||
29 | extern void wxapp_install_idle_handler(); | |
30 | extern bool g_isIdle; | |
31 | ||
8e189077 RR |
32 | //----------------------------------------------------------------------------- |
33 | // data | |
34 | //----------------------------------------------------------------------------- | |
35 | ||
36 | extern bool g_blockEventsOnDrag; | |
37 | ||
738f9e5a | 38 | static const float sensitivity = 0.02; |
6380910c | 39 | |
8e189077 RR |
40 | //----------------------------------------------------------------------------- |
41 | // "value_changed" | |
42 | //----------------------------------------------------------------------------- | |
43 | ||
865bb325 | 44 | extern "C" { |
8e189077 | 45 | static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win ) |
6380910c | 46 | { |
acfd422a RR |
47 | if (g_isIdle) wxapp_install_idle_handler(); |
48 | ||
a2053b27 | 49 | if (!win->m_hasVMT) return; |
83624f79 | 50 | if (g_blockEventsOnDrag) return; |
828f655f | 51 | |
83624f79 | 52 | float diff = win->m_adjust->value - win->m_oldPos; |
6380910c | 53 | if (fabs(diff) < sensitivity) return; |
6380910c | 54 | |
83624f79 | 55 | wxEventType command = wxEVT_NULL; |
6380910c | 56 | |
83624f79 | 57 | float line_step = win->m_adjust->step_increment; |
6380910c | 58 | |
0f42a871 RR |
59 | if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP; |
60 | else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN; | |
83624f79 | 61 | else command = wxEVT_SCROLL_THUMBTRACK; |
8e189077 | 62 | |
66d78146 | 63 | int value = (int)ceil(win->m_adjust->value); |
6380910c | 64 | |
83624f79 RR |
65 | wxSpinEvent event( command, win->GetId()); |
66 | event.SetPosition( value ); | |
83624f79 | 67 | event.SetEventObject( win ); |
0f42a871 RR |
68 | |
69 | if ((win->GetEventHandler()->ProcessEvent( event )) && | |
70 | !event.IsAllowed() ) | |
71 | { | |
72 | /* program has vetoed */ | |
73 | win->m_adjust->value = win->m_oldPos; | |
88d19775 | 74 | |
0f42a871 | 75 | gtk_signal_disconnect_by_func( GTK_OBJECT (win->m_adjust), |
88d19775 MR |
76 | (GtkSignalFunc) gtk_spinbutt_callback, |
77 | (gpointer) win ); | |
78 | ||
0f42a871 RR |
79 | gtk_signal_emit_by_name( GTK_OBJECT(win->m_adjust), "value_changed" ); |
80 | ||
81 | gtk_signal_connect( GTK_OBJECT (win->m_adjust), | |
88d19775 MR |
82 | "value_changed", |
83 | (GtkSignalFunc) gtk_spinbutt_callback, | |
84 | (gpointer) win ); | |
0f42a871 RR |
85 | return; |
86 | } | |
88d19775 | 87 | |
0f42a871 | 88 | win->m_oldPos = win->m_adjust->value; |
88d19775 | 89 | |
e65cc56a RR |
90 | /* always send a thumbtrack event */ |
91 | if (command != wxEVT_SCROLL_THUMBTRACK) | |
92 | { | |
93 | command = wxEVT_SCROLL_THUMBTRACK; | |
94 | wxSpinEvent event2( command, win->GetId()); | |
95 | event2.SetPosition( value ); | |
96 | event2.SetEventObject( win ); | |
97 | win->GetEventHandler()->ProcessEvent( event2 ); | |
98 | } | |
8e189077 | 99 | } |
865bb325 | 100 | } |
8e189077 RR |
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 | { |
de6185e2 | 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") ); |
de6185e2 | 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 | |
de6185e2 | 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" ); |
88d19775 | 207 | |
65045edd RR |
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 | ||
de6185e2 | 242 | #endif // wxUSE_SPINBTN |