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