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