Committing in .
[wxWidgets.git] / src / motif / slider.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: slider.cpp
3 // Purpose: wxSlider
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 "slider.h"
14 #endif
15
16 #include "wx/slider.h"
17 #include "wx/utils.h"
18
19 #ifdef __VMS__
20 #pragma message disable nosimpint
21 #endif
22 #include <Xm/Xm.h>
23 #include <Xm/Label.h>
24 #include <Xm/LabelG.h>
25 #include <Xm/RowColumn.h>
26 #include <Xm/Scale.h>
27 #ifdef __VMS__
28 #pragma message enable nosimpint
29 #endif
30
31 #include "wx/motif/private.h"
32
33 void wxSliderCallback (Widget widget, XtPointer clientData, XmScaleCallbackStruct * cbs);
34
35 #if !USE_SHARED_LIBRARY
36 IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
37
38 BEGIN_EVENT_TABLE(wxSlider, wxControl)
39 END_EVENT_TABLE()
40 #endif
41
42
43
44 // Slider
45 wxSlider::wxSlider()
46 {
47 m_pageSize = 1;
48 m_lineSize = 1;
49 m_rangeMax = 0;
50 m_rangeMin = 0;
51 m_tickFreq = 0;
52 }
53
54 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
55 int value, int minValue, int maxValue,
56 const wxPoint& pos,
57 const wxSize& size, long style,
58 const wxValidator& validator,
59 const wxString& name)
60 {
61 if ( !((style & wxSL_HORIZONTAL) || (style & wxSL_VERTICAL)) )
62 style |= wxSL_HORIZONTAL;
63
64 SetName(name);
65 SetValidator(validator);
66 m_backgroundColour = parent->GetBackgroundColour();
67 m_foregroundColour = parent->GetForegroundColour();
68
69 if (parent) parent->AddChild(this);
70
71 m_lineSize = 1;
72 m_windowStyle = style;
73 m_tickFreq = 0;
74
75 if ( id == -1 )
76 m_windowId = (int)NewControlId();
77 else
78 m_windowId = id;
79
80 m_rangeMax = maxValue;
81 m_rangeMin = minValue;
82
83 // Not used in Motif, I think
84 m_pageSize = (int)((maxValue-minValue)/10);
85
86 Widget parentWidget = (Widget) parent->GetClientWidget();
87
88 Widget sliderWidget = XtVaCreateManagedWidget ("sliderWidget",
89 xmScaleWidgetClass, parentWidget,
90 XmNorientation,
91 (((m_windowStyle & wxSL_VERTICAL) == wxSL_VERTICAL) ? XmVERTICAL : XmHORIZONTAL),
92 XmNprocessingDirection,
93 (((m_windowStyle & wxSL_VERTICAL) == wxSL_VERTICAL) ? XmMAX_ON_TOP : XmMAX_ON_RIGHT),
94 XmNmaximum, maxValue,
95 XmNminimum, minValue,
96 XmNvalue, value,
97 XmNshowValue, True,
98 NULL);
99
100 m_mainWidget = (WXWidget) sliderWidget;
101
102 #ifdef __VMS__
103 #pragma message disable codcauunr
104 // VMS gives here the compiler warning :
105 // statement either is unreachable or causes unreachable code
106 #endif
107 if(style & wxSL_NOTIFY_DRAG)
108 XtAddCallback (sliderWidget, XmNdragCallback,
109 (XtCallbackProc) wxSliderCallback, (XtPointer) this);
110 else
111 XtAddCallback (sliderWidget, XmNvalueChangedCallback,
112 (XtCallbackProc) wxSliderCallback, (XtPointer) this);
113 #ifdef __VMS__
114 #pragma message enable codcauunr
115 #endif
116
117 XtAddCallback (sliderWidget, XmNdragCallback, (XtCallbackProc) wxSliderCallback, (XtPointer) this);
118
119 m_font = parent->GetFont();
120
121 ChangeFont(FALSE);
122 SetCanAddEventHandler(TRUE);
123 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
124
125 ChangeBackgroundColour();
126
127 return TRUE;
128 }
129
130 wxSlider::~wxSlider()
131 {
132 }
133
134 int wxSlider::GetValue() const
135 {
136 int val;
137 XtVaGetValues ((Widget) m_mainWidget, XmNvalue, &val, NULL);
138 return val;
139 }
140
141 void wxSlider::SetValue(int value)
142 {
143 XtVaSetValues ((Widget) m_mainWidget, XmNvalue, value, NULL);
144 }
145
146 void wxSlider::GetSize(int *width, int *height) const
147 {
148 wxControl::GetSize(width, height);
149 }
150
151 void wxSlider::DoSetSize(int x, int y, int width, int height, int sizeFlags)
152 {
153 Widget widget = (Widget) m_mainWidget;
154
155 bool managed = XtIsManaged(widget);
156
157 if (managed)
158 XtUnmanageChild (widget);
159
160 if (((m_windowStyle & wxHORIZONTAL) == wxHORIZONTAL) && (width > -1))
161 {
162 XtVaSetValues (widget, XmNscaleWidth, wxMax (width, 10), NULL);
163 }
164
165 if (((m_windowStyle & wxVERTICAL) == wxVERTICAL) && (height > -1))
166 {
167 XtVaSetValues (widget, XmNscaleHeight, wxMax (height, 10), NULL);
168 }
169
170 int xx = x; int yy = y;
171 AdjustForParentClientOrigin(xx, yy, sizeFlags);
172
173 if (x > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
174 XtVaSetValues (widget, XmNx, xx, NULL);
175 if (y > -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
176 XtVaSetValues (widget, XmNy, yy, NULL);
177
178 if (managed)
179 XtManageChild (widget);
180 }
181
182 void wxSlider::SetRange(int minValue, int maxValue)
183 {
184 m_rangeMin = minValue;
185 m_rangeMax = maxValue;
186
187 XtVaSetValues ((Widget) m_mainWidget, XmNminimum, minValue, XmNmaximum, maxValue, NULL);
188 }
189
190 // For trackbars only
191 void wxSlider::SetTickFreq(int n, int WXUNUSED(pos))
192 {
193 // Not implemented in Motif
194 m_tickFreq = n;
195 }
196
197 void wxSlider::SetPageSize(int pageSize)
198 {
199 // Not implemented in Motif
200 m_pageSize = pageSize;
201 }
202
203 int wxSlider::GetPageSize() const
204 {
205 return m_pageSize;
206 }
207
208 void wxSlider::ClearSel()
209 {
210 // Not implemented in Motif
211 }
212
213 void wxSlider::ClearTicks()
214 {
215 // Not implemented in Motif
216 }
217
218 void wxSlider::SetLineSize(int lineSize)
219 {
220 // Not implemented in Motif
221 m_lineSize = lineSize;
222 }
223
224 int wxSlider::GetLineSize() const
225 {
226 // Not implemented in Motif
227 return m_lineSize;
228 }
229
230 int wxSlider::GetSelEnd() const
231 {
232 // Not implemented in Motif
233 return 0;
234 }
235
236 int wxSlider::GetSelStart() const
237 {
238 // Not implemented in Motif
239 return 0;
240 }
241
242 void wxSlider::SetSelection(int WXUNUSED(minPos), int WXUNUSED(maxPos))
243 {
244 // Not implemented in Motif
245 }
246
247 void wxSlider::SetThumbLength(int WXUNUSED(len))
248 {
249 // Not implemented in Motif (?)
250 }
251
252 int wxSlider::GetThumbLength() const
253 {
254 // Not implemented in Motif (?)
255 return 0;
256 }
257
258 void wxSlider::SetTick(int WXUNUSED(tickPos))
259 {
260 // Not implemented in Motif
261 }
262
263 void wxSlider::Command (wxCommandEvent & event)
264 {
265 SetValue (event.GetInt());
266 ProcessCommand (event);
267 }
268
269 void wxSlider::ChangeFont(bool keepOriginalSize)
270 {
271 wxWindow::ChangeFont(keepOriginalSize);
272 }
273
274 void wxSlider::ChangeBackgroundColour()
275 {
276 wxWindow::ChangeBackgroundColour();
277 }
278
279 void wxSlider::ChangeForegroundColour()
280 {
281 wxWindow::ChangeForegroundColour();
282 }
283
284 void wxSliderCallback (Widget widget, XtPointer clientData, XmScaleCallbackStruct * cbs)
285 {
286 wxSlider *slider = (wxSlider *) clientData;
287 switch (cbs->reason)
288 {
289 case XmCR_VALUE_CHANGED:
290 case XmCR_DRAG:
291 default:
292 {
293 // TODO: the XmCR_VALUE_CHANGED case should be handled
294 // differently (it's not sent continually as the slider moves).
295 // In which case we need a similar behaviour for other platforms.
296
297 wxScrollEvent event(wxEVT_SCROLL_THUMBTRACK, slider->GetId());
298 XtVaGetValues (widget, XmNvalue, &event.m_commandInt, NULL);
299 event.SetEventObject(slider);
300 slider->ProcessCommand(event);
301
302 // Also send a wxCommandEvent for compatibility.
303 wxCommandEvent event2(wxEVT_COMMAND_SLIDER_UPDATED, slider->GetId());
304 event2.SetEventObject(slider);
305 slider->ProcessCommand(event2);
306 break;
307 }
308 }
309 }
310