1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/motif/slider.cpp
3 // Purpose:     wxSlider
4 // Author:      Julian Smart
5 // Modified by:
6 // Created:     17/09/98
7 // Copyright:   (c) Julian Smart
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10 
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13 
14 #if wxUSE_SLIDER
15 
16 #include "wx/slider.h"
17 
18 #ifndef WX_PRECOMP
19     #include "wx/utils.h"
20 #endif
21 
22 #ifdef __VMS__
23 #pragma message disable nosimpint
24 #endif
25 #include <Xm/Xm.h>
26 #include <Xm/Label.h>
27 #include <Xm/LabelG.h>
28 #include <Xm/RowColumn.h>
29 #include <Xm/Scale.h>
30 #ifdef __VMS__
31 #pragma message enable nosimpint
32 #endif
33 
34 #include "wx/motif/private.h"
35 
36 static void wxSliderCallback (Widget widget, XtPointer clientData, XmScaleCallbackStruct * cbs);
37 
BEGIN_EVENT_TABLE(wxSlider,wxControl)38 BEGIN_EVENT_TABLE(wxSlider, wxControl)
39 END_EVENT_TABLE()
40 
41 
42 
43 // Slider
44 wxSlider::wxSlider()
45 {
46     m_pageSize = 1;
47     m_lineSize = 1;
48     m_rangeMax = 0;
49     m_rangeMin = 0;
50 }
51 
Create(wxWindow * parent,wxWindowID id,int value,int minValue,int maxValue,const wxPoint & pos,const wxSize & size,long style,const wxValidator & validator,const wxString & name)52 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
53                       int value, int minValue, int maxValue,
54                       const wxPoint& pos,
55                       const wxSize& size, long style,
56                       const wxValidator& validator,
57                       const wxString& name)
58 {
59     if ( !((style & wxSL_HORIZONTAL) || (style & wxSL_VERTICAL)) )
60          style |= wxSL_HORIZONTAL;
61 
62     if( !CreateControl( parent, id, pos, size, style, validator, name ) )
63         return false;
64     PreCreation();
65 
66     m_lineSize = 1;
67     m_windowStyle = style;
68 
69     m_rangeMax = maxValue;
70     m_rangeMin = minValue;
71 
72     // Not used in Motif, I think
73     m_pageSize = (int)((maxValue-minValue)/10);
74 
75     Widget parentWidget = (Widget) parent->GetClientWidget();
76 
77     Widget sliderWidget = XtVaCreateManagedWidget ("sliderWidget",
78         xmScaleWidgetClass, parentWidget,
79         XmNorientation,
80         (((m_windowStyle & wxSL_VERTICAL) == wxSL_VERTICAL) ? XmVERTICAL : XmHORIZONTAL),
81         XmNprocessingDirection,
82         (((m_windowStyle & wxSL_VERTICAL) == wxSL_VERTICAL) ? XmMAX_ON_TOP : XmMAX_ON_RIGHT),
83         XmNmaximum, maxValue,
84         XmNminimum, minValue,
85         XmNvalue, value,
86         XmNshowValue, True,
87         NULL);
88 
89     m_mainWidget = (WXWidget) sliderWidget;
90 
91     XtAddCallback (sliderWidget, XmNvalueChangedCallback, (XtCallbackProc) wxSliderCallback, (XtPointer) this);
92     XtAddCallback (sliderWidget, XmNdragCallback, (XtCallbackProc) wxSliderCallback, (XtPointer) this);
93 
94     PostCreation();
95     AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
96 
97     return true;
98 }
99 
~wxSlider()100 wxSlider::~wxSlider()
101 {
102 }
103 
GetValue() const104 int wxSlider::GetValue() const
105 {
106     int val;
107     XtVaGetValues ((Widget) m_mainWidget, XmNvalue, &val, NULL);
108     return val;
109 }
110 
SetValue(int value)111 void wxSlider::SetValue(int value)
112 {
113     XtVaSetValues ((Widget) m_mainWidget, XmNvalue, value, NULL);
114 }
115 
DoSetSize(int x,int y,int width,int height,int sizeFlags)116 void wxSlider::DoSetSize(int x, int y, int width, int height, int sizeFlags)
117 {
118     Widget widget = (Widget) m_mainWidget;
119 
120     bool managed = XtIsManaged(widget);
121 
122     if (managed)
123         XtUnmanageChild (widget);
124 
125     if (((m_windowStyle & wxHORIZONTAL) == wxHORIZONTAL) && (width > -1))
126     {
127         XtVaSetValues (widget, XmNscaleWidth, wxMax (width, 10), NULL);
128     }
129 
130     if (((m_windowStyle & wxVERTICAL) == wxVERTICAL) && (height > -1))
131     {
132         XtVaSetValues (widget, XmNscaleHeight, wxMax (height, 10), NULL);
133     }
134 
135     int xx = x; int yy = y;
136     AdjustForParentClientOrigin(xx, yy, sizeFlags);
137 
138     if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
139         XtVaSetValues (widget, XmNx, xx, NULL);
140     if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
141         XtVaSetValues (widget, XmNy, yy, NULL);
142 
143     if (managed)
144         XtManageChild (widget);
145 }
146 
SetRange(int minValue,int maxValue)147 void wxSlider::SetRange(int minValue, int maxValue)
148 {
149     m_rangeMin = minValue;
150     m_rangeMax = maxValue;
151 
152     XtVaSetValues ((Widget) m_mainWidget, XmNminimum, minValue, XmNmaximum, maxValue, NULL);
153 }
154 
155 // For trackbars only
SetPageSize(int pageSize)156 void wxSlider::SetPageSize(int pageSize)
157 {
158     // Not implemented in Motif
159     m_pageSize = pageSize;
160 }
161 
GetPageSize() const162 int wxSlider::GetPageSize() const
163 {
164     return m_pageSize;
165 }
166 
SetLineSize(int lineSize)167 void wxSlider::SetLineSize(int lineSize)
168 {
169     // Not implemented in Motif
170     m_lineSize = lineSize;
171 }
172 
GetLineSize() const173 int wxSlider::GetLineSize() const
174 {
175     // Not implemented in Motif
176     return m_lineSize;
177 }
178 
SetThumbLength(int WXUNUSED (len))179 void wxSlider::SetThumbLength(int WXUNUSED(len))
180 {
181     // Not implemented in Motif (?)
182 }
183 
GetThumbLength() const184 int wxSlider::GetThumbLength() const
185 {
186     // Not implemented in Motif (?)
187     return 0;
188 }
189 
Command(wxCommandEvent & event)190 void wxSlider::Command (wxCommandEvent & event)
191 {
192     SetValue (event.GetInt());
193     ProcessCommand (event);
194 }
195 
wxSliderCallback(Widget widget,XtPointer clientData,XmScaleCallbackStruct * cbs)196 void wxSliderCallback (Widget widget, XtPointer clientData,
197                        XmScaleCallbackStruct * cbs)
198 {
199     wxSlider *slider = (wxSlider *) clientData;
200     wxEventType scrollEvent;
201 
202     switch (cbs->reason)
203     {
204     case XmCR_VALUE_CHANGED:
205         scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
206         break;
207 
208     case XmCR_DRAG:
209         scrollEvent = wxEVT_SCROLL_THUMBTRACK;
210         break;
211 
212     default:
213         return;
214     }
215 
216     wxScrollEvent event(scrollEvent, slider->GetId());
217     int commandInt = event.GetInt();
218     XtVaGetValues (widget, XmNvalue, &commandInt, NULL);
219     event.SetInt(commandInt);
220     event.SetEventObject(slider);
221     slider->HandleWindowEvent(event);
222 
223     // Also send a wxCommandEvent for compatibility.
224     wxCommandEvent event2(wxEVT_SLIDER, slider->GetId());
225     event2.SetEventObject(slider);
226     event2.SetInt( event.GetInt() );
227     slider->HandleWindowEvent(event2);
228 }
229 
230 #endif // wxUSE_SLIDER
231