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