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