]>
Commit | Line | Data |
---|---|---|
4bb6408c | 1 | ///////////////////////////////////////////////////////////////////////////// |
355b4d3d | 2 | // Name: src/motif/spinbutt.cpp |
4bb6408c JS |
3 | // Purpose: wxSpinButton |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
4bb6408c JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
1248b41f MB |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
312ebad4 WS |
15 | #if wxUSE_SPINBTN |
16 | ||
4bb6408c | 17 | #include "wx/spinbutt.h" |
6f18aba5 | 18 | #include "wx/spinctrl.h" |
dba00620 | 19 | #include "wx/timer.h" |
4bb6408c | 20 | |
dba00620 MB |
21 | #ifdef __VMS__ |
22 | #pragma message disable nosimpint | |
23 | #endif | |
24 | #include <Xm/ArrowBG.h> | |
25 | #include <Xm/ArrowB.h> | |
26 | #ifdef __VMS__ | |
27 | #pragma message enable nosimpint | |
28 | #endif | |
29 | ||
30 | #include "wx/motif/private.h" | |
4bb6408c | 31 | |
dba00620 MB |
32 | // helper class |
33 | enum ArrowDirection | |
4bb6408c | 34 | { |
dba00620 MB |
35 | wxARROW_UP, |
36 | wxARROW_DOWN, | |
37 | wxARROW_LEFT, | |
38 | wxARROW_RIGHT | |
39 | }; | |
40 | ||
41 | class wxArrowButtonTimer; | |
42 | class wxArrowButton; | |
43 | ||
44 | // ---------------------------------------------------------------------------- | |
45 | // wxArrowButtonTimer | |
46 | // ---------------------------------------------------------------------------- | |
47 | ||
48 | static const unsigned int TICK_BEFORE_START = 10; | |
49 | static const unsigned int TICK_BEFORE_EXPONENTIAL = 40; | |
50 | static const unsigned int MAX_INCREMENT = 150; | |
51 | static const unsigned int TICK_INTERVAL = 113; | |
52 | ||
53 | class wxArrowButtonTimer : public wxTimer | |
54 | { | |
55 | public: | |
56 | wxArrowButtonTimer( wxArrowButton* btn, int sign ) | |
57 | : m_sign( sign ), | |
58 | m_button( btn ) | |
59 | { Reset(); }; | |
60 | ||
61 | void Notify(); | |
62 | void Reset() { m_ticks = 0; m_increment = 1; } | |
63 | private: | |
64 | unsigned int m_ticks; | |
65 | unsigned int m_increment; | |
66 | int m_sign; | |
67 | wxArrowButton* m_button; | |
68 | }; | |
69 | ||
70 | // ---------------------------------------------------------------------------- | |
71 | // wxArrowButton | |
72 | // ---------------------------------------------------------------------------- | |
73 | ||
74 | class wxArrowButton : public wxControl | |
75 | { | |
76 | friend class wxArrowButtonTimer; | |
77 | public: | |
312ebad4 | 78 | wxArrowButton( int increment ) |
dba00620 MB |
79 | : m_increment( increment ), |
80 | m_timer( 0 ) {} | |
81 | ||
82 | wxArrowButton( wxSpinButton* parent, wxWindowID id, ArrowDirection d, | |
83 | const wxPoint& pos = wxDefaultPosition, | |
84 | const wxSize& size = wxDefaultSize, int increment = 1 ) | |
85 | : wxControl(), | |
86 | m_increment( increment ), | |
87 | m_timer( 0 ) | |
88 | { | |
89 | Create( parent, id, d, pos, size ); | |
90 | } | |
91 | ||
92 | ~wxArrowButton() | |
93 | { delete m_timer; } | |
94 | ||
95 | bool Create( wxSpinButton* parent, wxWindowID id, ArrowDirection d, | |
96 | const wxPoint& pos = wxDefaultPosition, | |
97 | const wxSize& size = wxDefaultSize ); | |
98 | private: | |
99 | // creates a new timer object, or stops the currently running one | |
100 | wxTimer* GetFreshTimer(); | |
101 | wxSpinButton* GetSpinButton() { return (wxSpinButton*)GetParent(); } | |
102 | static void SpinButtonCallback( Widget w, XtPointer clientData, | |
103 | XtPointer WXUNUSED(ptr) ); | |
104 | static void StartTimerCallback( Widget w, XtPointer clientData, | |
105 | XtPointer WXUNUSED(ptr) ); | |
106 | ||
107 | static void StopTimerCallback( Widget w, XtPointer clientData, | |
108 | XtPointer WXUNUSED(ptr) ); | |
109 | ||
110 | int m_increment; | |
111 | wxArrowButtonTimer* m_timer; | |
112 | }; | |
113 | ||
114 | // ---------------------------------------------------------------------------- | |
115 | // wxArrowButtonTimer implementation | |
116 | // ---------------------------------------------------------------------------- | |
117 | ||
118 | void wxArrowButtonTimer::Notify() | |
119 | { | |
120 | ++m_ticks; | |
121 | if( m_ticks < TICK_BEFORE_START ) return; | |
122 | // increment every other tick | |
123 | if( m_ticks <= TICK_BEFORE_EXPONENTIAL && m_ticks & 1 ) | |
124 | return; | |
125 | if( m_ticks > TICK_BEFORE_EXPONENTIAL ) | |
126 | m_increment = 2 * m_increment; | |
127 | if( m_increment >= MAX_INCREMENT ) m_increment = MAX_INCREMENT; | |
128 | m_button->GetSpinButton()->Increment( m_sign * m_increment ); | |
129 | } | |
130 | ||
131 | // ---------------------------------------------------------------------------- | |
132 | // wxArrowButton implementation | |
133 | // ---------------------------------------------------------------------------- | |
134 | ||
135 | wxTimer* wxArrowButton::GetFreshTimer() | |
136 | { | |
137 | if( m_timer ) | |
138 | { | |
139 | m_timer->Stop(); | |
140 | m_timer->Reset(); | |
141 | } | |
142 | else | |
143 | m_timer = new wxArrowButtonTimer( this, m_increment ); | |
144 | ||
145 | return m_timer; | |
146 | } | |
147 | ||
148 | void wxArrowButton::SpinButtonCallback( Widget w, XtPointer clientData, | |
149 | XtPointer WXUNUSED(ptr) ) | |
150 | { | |
151 | if( !wxGetWindowFromTable( w ) ) | |
152 | // Widget has been deleted! | |
153 | return; | |
154 | ||
155 | wxArrowButton* btn = (wxArrowButton*)clientData; | |
156 | ||
157 | btn->GetSpinButton()->Increment( btn->m_increment ); | |
158 | } | |
159 | ||
160 | void wxArrowButton::StartTimerCallback( Widget w, XtPointer clientData, | |
161 | XtPointer WXUNUSED(ptr) ) | |
162 | { | |
163 | if( !wxGetWindowFromTable( w ) ) | |
164 | // Widget has been deleted! | |
165 | return; | |
166 | ||
167 | wxArrowButton* btn = (wxArrowButton*)clientData; | |
168 | btn->GetFreshTimer()->Start( TICK_INTERVAL ); | |
169 | } | |
170 | ||
171 | void wxArrowButton::StopTimerCallback( Widget w, XtPointer clientData, | |
172 | XtPointer WXUNUSED(ptr) ) | |
173 | { | |
174 | if( !wxGetWindowFromTable( w ) ) | |
175 | // Widget has been deleted! | |
176 | return; | |
177 | ||
178 | wxArrowButton* btn = (wxArrowButton*)clientData; | |
179 | delete btn->m_timer; | |
180 | btn->m_timer = 0; | |
181 | } | |
182 | ||
355b4d3d WS |
183 | bool wxArrowButton::Create( wxSpinButton* parent, |
184 | wxWindowID WXUNUSED(id), | |
dba00620 MB |
185 | ArrowDirection d, |
186 | const wxPoint& pos, const wxSize& size ) | |
187 | { | |
02a48e3b | 188 | int arrow_dir = XmARROW_UP; |
dba00620 MB |
189 | |
190 | switch( d ) | |
191 | { | |
192 | case wxARROW_UP: | |
193 | arrow_dir = XmARROW_UP; | |
194 | break; | |
195 | case wxARROW_DOWN: | |
196 | arrow_dir = XmARROW_DOWN; | |
197 | break; | |
198 | case wxARROW_LEFT: | |
199 | arrow_dir = XmARROW_LEFT; | |
200 | break; | |
201 | case wxARROW_RIGHT: | |
202 | arrow_dir = XmARROW_RIGHT; | |
203 | break; | |
204 | } | |
205 | ||
206 | if( parent ) parent->AddChild( this ); | |
4bb6408c | 207 | |
dba00620 MB |
208 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
209 | m_mainWidget = (WXWidget) XtVaCreateManagedWidget( "XmArrowButton", | |
210 | xmArrowButtonWidgetClass, | |
211 | parentWidget, | |
212 | XmNarrowDirection, arrow_dir, | |
de505a9e | 213 | XmNborderWidth, 0, |
d8d18184 | 214 | XmNshadowThickness, 0, |
dba00620 MB |
215 | NULL ); |
216 | ||
217 | XtAddCallback( (Widget) m_mainWidget, | |
218 | XmNactivateCallback, (XtCallbackProc) SpinButtonCallback, | |
219 | (XtPointer) this ); | |
220 | XtAddCallback( (Widget) m_mainWidget, | |
221 | XmNarmCallback, (XtCallbackProc) StartTimerCallback, | |
222 | (XtPointer) this ); | |
223 | XtAddCallback( (Widget) m_mainWidget, | |
224 | XmNactivateCallback, (XtCallbackProc) StopTimerCallback, | |
225 | (XtPointer) this ); | |
226 | ||
227 | AttachWidget( parent, m_mainWidget, (WXWidget) NULL, | |
228 | pos.x, pos.y, size.x, size.y ); | |
229 | ||
79fb2641 MB |
230 | SetForegroundColour( parent->GetBackgroundColour() ); |
231 | ||
312ebad4 | 232 | return true; |
dba00620 MB |
233 | } |
234 | ||
235 | // ---------------------------------------------------------------------------- | |
236 | // wxSpinButton | |
237 | // ---------------------------------------------------------------------------- | |
238 | ||
541c319a VZ |
239 | IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl) |
240 | IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent) | |
dba00620 | 241 | |
fbfb8bcc | 242 | static void CalcSizes( const wxPoint& pt, const wxSize& sz, |
dba00620 MB |
243 | wxPoint& pt1, wxSize& sz1, |
244 | wxPoint& pt2, wxSize& sz2, | |
245 | bool isVertical ) | |
246 | { | |
247 | typedef int wxSize::* CDPTR1; | |
248 | typedef int wxPoint::* CDPTR2; | |
249 | ||
250 | sz1 = sz2 = sz; | |
251 | pt2 = pt1 = pt; | |
252 | ||
253 | CDPTR1 szm = isVertical ? &wxSize::y : &wxSize::x; | |
254 | CDPTR2 ptm = isVertical ? &wxPoint::y : &wxPoint::x; | |
255 | int dim = sz.*szm, half = dim/2; | |
256 | ||
257 | sz1.*szm = half; | |
258 | sz2.*szm = dim - half; | |
259 | pt2.*ptm += half + 1; | |
260 | } | |
261 | ||
262 | bool wxSpinButton::Create( wxWindow *parent, wxWindowID id, | |
263 | const wxPoint& pos, const wxSize& size, | |
264 | long style, const wxString& name ) | |
265 | { | |
4bb6408c JS |
266 | m_windowStyle = style; |
267 | ||
dba00620 MB |
268 | wxSize newSize = GetBestSize(); |
269 | if( size.x != -1 ) newSize.x = size.x; | |
270 | if( size.y != -1 ) newSize.y = size.y; | |
271 | ||
272 | if( !wxControl::Create( parent, id, pos, newSize, style ) ) | |
273 | { | |
312ebad4 | 274 | return false; |
dba00620 MB |
275 | } |
276 | ||
277 | SetName(name); | |
31528cd3 | 278 | |
312ebad4 | 279 | m_windowId = ( id == wxID_ANY ) ? NewControlId() : id; |
4bb6408c | 280 | |
dba00620 MB |
281 | bool isVert = IsVertical(); |
282 | wxPoint pt1, pt2; | |
283 | wxSize sz1, sz2; | |
284 | CalcSizes( wxPoint(0,0), newSize, pt1, sz1, pt2, sz2, isVert ); | |
038305d0 | 285 | m_up = new wxArrowButton( this, -1, isVert ? wxARROW_UP : wxARROW_RIGHT, |
dba00620 MB |
286 | pt1, sz1, 1 ); |
287 | m_down = new wxArrowButton( this, -1, | |
038305d0 | 288 | isVert ? wxARROW_DOWN : wxARROW_LEFT, |
dba00620 MB |
289 | pt2, sz2, -1 ); |
290 | ||
312ebad4 | 291 | return true; |
4bb6408c JS |
292 | } |
293 | ||
294 | wxSpinButton::~wxSpinButton() | |
295 | { | |
296 | } | |
297 | ||
dba00620 MB |
298 | void wxSpinButton::DoMoveWindow(int x, int y, int width, int height) |
299 | { | |
300 | wxControl::DoMoveWindow( x, y, width, height ); | |
301 | ||
302 | wxPoint pt1, pt2; | |
303 | wxSize sz1, sz2; | |
304 | ||
305 | CalcSizes( wxPoint(0,0), wxSize(width,height), pt1, | |
306 | sz1, pt2, sz2, IsVertical() ); | |
307 | m_up->SetSize( pt1.x, pt1.y, sz1.x, sz1.y ); | |
308 | m_down->SetSize( pt2.x, pt2.y, sz2.x, sz2.y ); | |
309 | } | |
310 | ||
541c319a | 311 | void wxSpinButton::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
dba00620 | 312 | { |
541c319a | 313 | if ( (sizeFlags & wxSIZE_ALLOW_MINUS_ONE) && width == -1 ) |
dba00620 | 314 | width = GetSize().x; |
541c319a | 315 | if ( (sizeFlags & wxSIZE_ALLOW_MINUS_ONE) && height == -1 ) |
dba00620 MB |
316 | height = GetSize().y; |
317 | ||
318 | wxControl::DoSetSize(x, y, width, height, 0); | |
319 | } | |
320 | ||
321 | void wxSpinButton::Increment( int delta ) | |
322 | { | |
323 | if( m_pos < m_min ) m_pos = m_min; | |
324 | if( m_pos > m_max ) m_pos = m_max; | |
325 | ||
326 | int npos = m_pos + delta; | |
327 | ||
de505a9e MB |
328 | if( npos < m_min ) |
329 | { | |
038305d0 | 330 | if( GetWindowStyle() & wxSP_WRAP ) |
de505a9e MB |
331 | npos = m_max; |
332 | else | |
333 | npos = m_min; | |
334 | } | |
335 | if( npos > m_max ) | |
336 | { | |
038305d0 | 337 | if( GetWindowStyle() & wxSP_WRAP ) |
de505a9e MB |
338 | npos = m_min; |
339 | else | |
340 | npos = m_max; | |
341 | } | |
dba00620 MB |
342 | if( npos == m_pos ) return; |
343 | ||
344 | wxSpinEvent event( delta > 0 ? wxEVT_SCROLL_LINEUP : wxEVT_SCROLL_LINEDOWN, | |
345 | m_windowId ); | |
346 | event.SetPosition( npos ); | |
347 | event.SetEventObject( this ); | |
348 | ||
349 | GetEventHandler()->ProcessEvent( event ); | |
350 | ||
351 | if( event.IsAllowed() ) | |
352 | { | |
353 | m_pos = npos; | |
354 | event.SetEventType( wxEVT_SCROLL_THUMBTRACK ); | |
355 | event.SetPosition( m_pos ); | |
356 | ||
357 | GetEventHandler()->ProcessEvent( event ); | |
358 | } | |
359 | } | |
360 | ||
361 | wxSize wxSpinButton::DoGetBestSize() const | |
362 | { | |
d8d18184 | 363 | return IsVertical() ? wxSize( 20, 30 ) : wxSize( 30, 20 ); |
dba00620 MB |
364 | } |
365 | ||
4bb6408c JS |
366 | // Attributes |
367 | //////////////////////////////////////////////////////////////////////////// | |
368 | ||
369 | int wxSpinButton::GetValue() const | |
370 | { | |
dba00620 | 371 | return m_pos; |
4bb6408c JS |
372 | } |
373 | ||
dba00620 | 374 | void wxSpinButton::SetValue(int val) |
4bb6408c | 375 | { |
dba00620 | 376 | m_pos = val; |
4bb6408c JS |
377 | } |
378 | ||
379 | void wxSpinButton::SetRange(int minVal, int maxVal) | |
380 | { | |
31528cd3 | 381 | wxSpinButtonBase::SetRange(minVal, maxVal); |
4bb6408c JS |
382 | } |
383 | ||
af111fc3 | 384 | void wxSpinButton::ChangeFont(bool WXUNUSED(keepOriginalSize)) |
0d57be45 JS |
385 | { |
386 | // TODO | |
387 | } | |
388 | ||
389 | void wxSpinButton::ChangeBackgroundColour() | |
390 | { | |
dba00620 | 391 | wxControl::ChangeBackgroundColour(); |
0d57be45 JS |
392 | } |
393 | ||
394 | void wxSpinButton::ChangeForegroundColour() | |
395 | { | |
396 | // TODO | |
397 | } | |
312ebad4 WS |
398 | |
399 | #endif // wxUSE_SPINBTN |