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