]> git.saurik.com Git - wxWidgets.git/blob - src/motif/slider.cpp
set string length to 0 in AllocBeforeWrite() just to be tidy (replaces patch 1123226)
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "slider.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #include "wx/defs.h"
20
21 #if wxUSE_SLIDER
22
23 #include "wx/slider.h"
24 #include "wx/utils.h"
25
26 #ifdef __VMS__
27 #pragma message disable nosimpint
28 #endif
29 #include <Xm/Xm.h>
30 #include <Xm/Label.h>
31 #include <Xm/LabelG.h>
32 #include <Xm/RowColumn.h>
33 #include <Xm/Scale.h>
34 #ifdef __VMS__
35 #pragma message enable nosimpint
36 #endif
37
38 #include "wx/motif/private.h"
39
40 void wxSliderCallback (Widget widget, XtPointer clientData, XmScaleCallbackStruct * cbs);
41
42 IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
43
44 BEGIN_EVENT_TABLE(wxSlider, wxControl)
45 END_EVENT_TABLE()
46
47
48
49 // Slider
50 wxSlider::wxSlider()
51 {
52 m_pageSize = 1;
53 m_lineSize = 1;
54 m_rangeMax = 0;
55 m_rangeMin = 0;
56 }
57
58 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
59 int value, int minValue, int maxValue,
60 const wxPoint& pos,
61 const wxSize& size, long style,
62 const wxValidator& validator,
63 const wxString& name)
64 {
65 if ( !((style & wxSL_HORIZONTAL) || (style & wxSL_VERTICAL)) )
66 style |= wxSL_HORIZONTAL;
67
68 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
69 return false;
70
71 m_lineSize = 1;
72 m_windowStyle = style;
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 #ifdef __VMS__
97 #pragma message disable codcauunr
98 // VMS gives here the compiler warning :
99 // statement either is unreachable or causes unreachable code
100 #endif
101 if(style & wxSL_NOTIFY_DRAG)
102 XtAddCallback (sliderWidget, XmNdragCallback,
103 (XtCallbackProc) wxSliderCallback, (XtPointer) this);
104 else
105 XtAddCallback (sliderWidget, XmNvalueChangedCallback,
106 (XtCallbackProc) wxSliderCallback, (XtPointer) this);
107 #ifdef __VMS__
108 #pragma message enable codcauunr
109 #endif
110
111 XtAddCallback (sliderWidget, XmNdragCallback, (XtCallbackProc) wxSliderCallback, (XtPointer) this);
112
113 ChangeFont(false);
114 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
115
116 ChangeBackgroundColour();
117
118 return true;
119 }
120
121 wxSlider::~wxSlider()
122 {
123 }
124
125 int wxSlider::GetValue() const
126 {
127 int val;
128 XtVaGetValues ((Widget) m_mainWidget, XmNvalue, &val, NULL);
129 return val;
130 }
131
132 void wxSlider::SetValue(int value)
133 {
134 XtVaSetValues ((Widget) m_mainWidget, XmNvalue, value, NULL);
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::SetPageSize(int pageSize)
178 {
179 // Not implemented in Motif
180 m_pageSize = pageSize;
181 }
182
183 int wxSlider::GetPageSize() const
184 {
185 return m_pageSize;
186 }
187
188 void wxSlider::SetLineSize(int lineSize)
189 {
190 // Not implemented in Motif
191 m_lineSize = lineSize;
192 }
193
194 int wxSlider::GetLineSize() const
195 {
196 // Not implemented in Motif
197 return m_lineSize;
198 }
199
200 void wxSlider::SetThumbLength(int WXUNUSED(len))
201 {
202 // Not implemented in Motif (?)
203 }
204
205 int wxSlider::GetThumbLength() const
206 {
207 // Not implemented in Motif (?)
208 return 0;
209 }
210
211 void wxSlider::Command (wxCommandEvent & event)
212 {
213 SetValue (event.GetInt());
214 ProcessCommand (event);
215 }
216
217 void wxSliderCallback (Widget widget, XtPointer clientData,
218 XmScaleCallbackStruct * cbs)
219 {
220 wxSlider *slider = (wxSlider *) clientData;
221 wxEventType scrollEvent;
222
223 switch (cbs->reason)
224 {
225 case XmCR_VALUE_CHANGED:
226 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
227 break;
228
229 case XmCR_DRAG:
230 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
231 break;
232
233 default:
234 return;
235 }
236
237 wxScrollEvent event(scrollEvent, slider->GetId());
238 int commandInt = event.GetInt();
239 XtVaGetValues (widget, XmNvalue, &commandInt, NULL);
240 event.SetInt(commandInt);
241 event.SetEventObject(slider);
242 slider->GetEventHandler()->ProcessEvent(event);
243
244 // Also send a wxCommandEvent for compatibility.
245 wxCommandEvent event2(wxEVT_COMMAND_SLIDER_UPDATED, slider->GetId());
246 event2.SetEventObject(slider);
247 event2.SetInt( event.GetInt() );
248 slider->GetEventHandler()->ProcessEvent(event2);
249 }
250
251 #endif // wxUSE_SLIDER