Debian Codegraph @

apps/demos/noodle/NoodleSlider.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 | Classes:
39 | NoodleSlider is a component that expands the functionality of the
40 | standard motif slider (they call it 'scale') widget.
41 |
42 | Author(s) : Paul Isaacs
43 |
44 */
45
46
47
48 #include "NoodleSlider.h"
49 #include <X11/StringDefs.h>
50 #include <Xm/Form.h>
51 #include <Xm/Text.h>
52 #include <Xm/Scale.h>
53
54 NoodleSlider::NoodleSlider(
55 Widget parent,
56 const char *name,
57 SbBool buildInsideParent,
58 int startingMin,
59 int startingMax)
60 : SoXtComponent(
61 parent,
62 name,
63 buildInsideParent)
64 {
65 // Build the widget!
66 Widget w = buildWidget( getParentWidget(), startingMin, startingMax);
67 setBaseWidget(w);
68
69 valueChangedCallbacks = new SoCallbackList;
70 }
71
72 NoodleSlider::~NoodleSlider()
73 {
74 delete valueChangedCallbacks;
75 }
76
77 Widget
78 NoodleSlider::buildWidget(
79 Widget parent,
80 int startingMin,
81 int startingMax )
82 {
83 Arg wargs[10];
84 int nargs;
85
86 _form = XtCreateManagedWidget(getWidgetName(),xmFormWidgetClass,
87 parent,NULL, 0);
88
89 _value = XtCreateManagedWidget("value",xmTextWidgetClass,_form,NULL,0);
90
91 nargs = 0;
92 XtSetArg(wargs[nargs],XmNorientation, XmHORIZONTAL ); nargs++;
93 XtSetArg(wargs[nargs],XmNdecimalPoints, 2 ); nargs++;
94 XtSetArg(wargs[nargs],XmNminimum, startingMin ); nargs++;
95 XtSetArg(wargs[nargs],XmNmaximum, startingMax ); nargs++;
96 _slider = XtCreateManagedWidget("slider",xmScaleWidgetClass,
97 _form,wargs,nargs);
98
99 /* set the callbacks for the sub components */
100 XtAddCallback( _value, XmNactivateCallback,
101 (XtCallbackProc) &NoodleSlider::valueWidgetCallback,
102 (XtPointer) this );
103 XtAddCallback( _slider, XmNdragCallback,
104 (XtCallbackProc) &NoodleSlider::sliderWidgetCallback,
105 (XtPointer) this );
106 XtAddCallback( _slider, XmNvalueChangedCallback,
107 (XtCallbackProc) &NoodleSlider::sliderWidgetCallback,
108 (XtPointer) this );
109
110 /* take care of layout within the NoodleSlider */
111 initLayout();
112
113 /* initialize values throughout the NoodleSlider to be those in the scale */
114 /* Right now, this winds up filling the label widgets with text */
115 setValue( getValue());
116 setMin( getMin());
117 setMax( getMax());
118 return _form;
119 }
120
121 void
122 NoodleSlider::initLayout()
123 {
124 if (_form == NULL)
125 return;
126
127 Arg wargs[10];
128 int nargs;
129 nargs = 0;
130 XtSetArg(wargs[nargs],XmNborderWidth, 2 ); nargs++;
131 XtSetValues( _form, wargs, nargs );
132
133 nargs = 0;
134 XtSetArg(wargs[nargs],XmNtopAttachment, XmATTACH_FORM ); nargs++;
135 XtSetArg(wargs[nargs],XmNbottomAttachment, XmATTACH_POSITION ); nargs++;
136 XtSetArg(wargs[nargs],XmNbottomPosition, 100 ); nargs++;
137 XtSetArg(wargs[nargs],XmNleftAttachment, XmATTACH_POSITION ); nargs++;
138 XtSetArg(wargs[nargs],XmNleftPosition, 0 ); nargs++;
139 XtSetArg(wargs[nargs],XmNrightAttachment, XmATTACH_POSITION ); nargs++;
140 XtSetArg(wargs[nargs],XmNrightPosition, 30 ); nargs++;
141 XtSetValues( _value, wargs, nargs );
142
143 nargs = 0;
144 XtSetArg(wargs[nargs],XmNtopAttachment, XmATTACH_POSITION ); nargs++;
145 XtSetArg(wargs[nargs],XmNtopPosition, 0 ); nargs++;
146 XtSetArg(wargs[nargs],XmNbottomAttachment, XmATTACH_FORM ); nargs++;
147 XtSetArg(wargs[nargs],XmNleftAttachment, XmATTACH_POSITION ); nargs++;
148 XtSetArg(wargs[nargs],XmNleftPosition, 30 ); nargs++;
149 XtSetArg(wargs[nargs],XmNrightAttachment, XmATTACH_POSITION ); nargs++;
150 XtSetArg(wargs[nargs],XmNrightPosition, 100 ); nargs++;
151 XtSetArg(wargs[nargs],XmNtitleString, (void *) NULL ); nargs++;
152 XtSetValues( _slider, wargs, nargs );
153 }
154
155 void
156 NoodleSlider::setValue( float newVal )
157 {
158 if ( getMin() > newVal )
159 setMin( newVal );
160 if ( getMax() < newVal )
161 setMax( newVal );
162
163 /* send the value to the slider */
164 if (_slider) {
165 int motifIntValue;
166 motifIntValue = convertFloatToSlider( newVal );
167 XmScaleSetValue( _slider, motifIntValue );
168 }
169
170 /* send the value to the label */
171 char valString[50];
172 sprintf( valString, "%.*f", getNumDecimals(), newVal );
173 XmTextSetString( _value, valString );
174 }
175
176 void
177 NoodleSlider::setMin( float newVal )
178 {
179 float valToUse;
180
181 if ( newVal <= getValue()
182 && newVal < getMax() )
183 valToUse = newVal;
184 else
185 valToUse = getMin();
186
187 /* send the new minimum value to the slider */
188 if ( _slider == NULL )
189 return;
190
191 Arg wargs[10];
192 int motifIntValue;
193
194 motifIntValue = convertFloatToSlider( valToUse );
195 XtSetArg( wargs[0], XmNminimum, motifIntValue );
196 XtSetValues( _slider, wargs, 1 );
197 }
198
199 void
200 NoodleSlider::setMax( float newVal )
201 {
202 float valToUse;
203
204 if ( newVal >= getValue()
205 && newVal > getMin() )
206 valToUse = newVal;
207 else
208 valToUse = getMax();
209
210 /* send the new maximum value to the slider */
211 if ( _slider == NULL )
212 return;
213
214 Arg wargs[10];
215 int motifIntValue;
216
217 motifIntValue = convertFloatToSlider( valToUse );
218 XtSetArg( wargs[0], XmNmaximum, motifIntValue );
219 XtSetValues( _slider, wargs, 1 );
220 }
221
222 float
223 NoodleSlider::getValue()
224 {
225 if ( _slider == NULL )
226 return 0.0;
227
228 Arg wargs[10];
229 int motifIntValue;
230
231 XtSetArg( wargs[0], XmNvalue, &motifIntValue );
232 XtGetValues( _slider, wargs, 1 );
233
234 return( convertSliderToFloat( motifIntValue ));
235 }
236
237 float
238 NoodleSlider::getMin()
239 {
240 if ( _slider == NULL )
241 return 0.0;
242
243 Arg wargs[10];
244 int motifIntValue;
245
246 XtSetArg( wargs[0], XmNminimum, &motifIntValue );
247 XtGetValues( _slider, wargs, 1 );
248
249 return( convertSliderToFloat( motifIntValue ));
250 }
251
252
253 float
254 NoodleSlider::getMax()
255 {
256 if ( _slider == NULL )
257 return 0.0;
258
259 Arg wargs[10];
260 int motifIntValue;
261
262 XtSetArg( wargs[0], XmNmaximum, &motifIntValue );
263 XtGetValues( _slider, wargs, 1 );
264
265 return( convertSliderToFloat( motifIntValue ));
266 }
267
268 short
269 NoodleSlider::getNumDecimals()
270 {
271 if (_slider == NULL)
272 return 0;
273 Arg wargs[10];
274 short decimals;
275
276 XtSetArg( wargs[0], XmNdecimalPoints, &decimals);
277 XtGetValues( _slider, wargs, 1 );
278
279 return decimals;
280 }
281
282 float
283 NoodleSlider::convertSliderToFloat( int sliderValue )
284 {
285 short decimals;
286 float theVal;
287 int count;
288
289 /* shift over the appropriate number of decimal places */
290 decimals = getNumDecimals();
291 if (decimals < 0)
292 decimals = 0;
293 for( count = 0, theVal = sliderValue; count < decimals; count++ )
294 theVal /= 10.0;
295
296 return( theVal );
297 }
298
299 int
300 NoodleSlider::convertFloatToSlider( float floatValue )
301 {
302 short decimals;
303 float convertedFloat;
304 int count, returnValue;
305
306 /* shift over the appropriate number of decimal places */
307 decimals = getNumDecimals();
308
309 if (decimals < 0)
310 decimals = 0;
311 for( count = 0, convertedFloat = floatValue; count < decimals; count++ )
312 convertedFloat *= 10.0;
313 returnValue = (int) convertedFloat;
314 return( returnValue );
315 }
316
317
318 void
319 NoodleSlider::valueWidgetCallback( Widget, void *client_data, void *)
320 {
321 float theVal;
322 NoodleSlider *theNdlSlider = (NoodleSlider *) client_data;
323 Arg wargs[10];
324 char *valString;
325
326 if (theNdlSlider && theNdlSlider->_value ) {
327 // get text value from the label
328 XtSetArg( wargs[0], XmNvalue, &valString );
329 XtGetValues( theNdlSlider->_value, wargs, 1 );
330 if ( sscanf( valString, "%f", &theVal ) ) {
331 // set the value throughout the NoodleSlider
332 theNdlSlider->setValue( theVal );
333 }
334 theNdlSlider->valueChangedCallbacks->invokeCallbacks(theNdlSlider);
335 }
336 }
337 void
338 NoodleSlider::sliderWidgetCallback( Widget, void *client_data, void *)
339 {
340 float theVal;
341 NoodleSlider *theNdlSlider = (NoodleSlider *) client_data;
342
343 // Get the value from the slider
344 theVal = theNdlSlider->getValue();
345
346 theNdlSlider->setValue(theVal);
347
348 theNdlSlider->valueChangedCallbacks->invokeCallbacks(theNdlSlider);
349 }
350
351 void
352 NoodleSlider::addValueChangedCallback(SoCallbackListCB *f, void *userData )
353 { valueChangedCallbacks->addCallback( f, userData ); }
354
355 void
356 NoodleSlider::removeValueChangedCallback(SoCallbackListCB *f, void *userData )
357 { valueChangedCallbacks->removeCallback( f, userData ); }
358
359