| 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 |
* Copyright (C) 1990-93 Silicon Graphics, Inc. |
| 39 |
* |
| 40 |
_______________________________________________________________________ |
| 41 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 |
| |
| 43 |
| $Revision: 1.4 $ |
| 44 |
| |
| 45 |
| Classes: |
| 46 |
| SoXtMinMaxSlider is a component that expands the functionality of the |
| 47 |
| standard motif slider (they call it 'scale') widget. |
| 48 |
| |
| 49 |
| Author(s) : Paul Isaacs |
| 50 |
| |
| 51 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 52 |
_______________________________________________________________________ |
| 53 |
*/ |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
#include <inttypes.h> |
| 58 |
#include "SoXtMinMaxSlider.h" |
| 59 |
#include "SoXtSliderTool.h" |
| 60 |
#include "SoXtMultiSlider.h" |
| 61 |
#include <X11/StringDefs.h> |
| 62 |
#include <Xm/Scale.h> |
| 63 |
|
| 64 |
/***************************************************************************/ |
| 65 |
/* static variables used by XtGetApplicationResources to get values of */ |
| 66 |
/* user-settable parameters. The defaults values are used if the user */ |
| 67 |
/* does not override them. */ |
| 68 |
/***************************************************************************/ |
| 69 |
typedef struct { |
| 70 |
unsigned char orientation; |
| 71 |
short decimalPoints; |
| 72 |
int maximum; |
| 73 |
int minimum; |
| 74 |
} ScaleStyleData, *ScaleStyleDataPtr; |
| 75 |
|
| 76 |
static XtResource _scaleStyleResources[] = { |
| 77 |
{ XmNorientation, XmCOrientation, XmROrientation, sizeof( unsigned char), |
| 78 |
XtOffset( ScaleStyleDataPtr, orientation ), |
| 79 |
XtRString, (XtPointer) "horizontal" }, |
| 80 |
{ XmNdecimalPoints, XmCDecimalPoints, XmRShort, sizeof( short), |
| 81 |
XtOffset( ScaleStyleDataPtr, decimalPoints ), |
| 82 |
XtRImmediate, (XtPointer) DEFAULT_SLIDER_DECIMAL_POINTS }, |
| 83 |
{ XmNmaximum, XmCMaximum, XmRInt, sizeof( int), |
| 84 |
XtOffset( ScaleStyleDataPtr, maximum ), |
| 85 |
XtRImmediate, (XtPointer) DEFAULT_SLIDER_SCALE_MAXIMUM }, |
| 86 |
{ XmNminimum, XmCMinimum, XmRInt, sizeof( int), |
| 87 |
XtOffset( ScaleStyleDataPtr, minimum ), |
| 88 |
XtRImmediate, (XtPointer) DEFAULT_SLIDER_SCALE_MINIMUM } |
| 89 |
}; |
| 90 |
|
| 91 |
SoXtMinMaxSlider::SoXtMinMaxSlider( |
| 92 |
Widget parent, |
| 93 |
const char *name, |
| 94 |
SbBool buildInsideParent, |
| 95 |
int startingMin, |
| 96 |
int startingMax) |
| 97 |
: SoXtSliderSetBase( |
| 98 |
parent, |
| 99 |
name, |
| 100 |
buildInsideParent, |
| 101 |
NULL) |
| 102 |
{ |
| 103 |
_startingMin = startingMin; |
| 104 |
_startingMax = startingMax; |
| 105 |
|
| 106 |
_sliderTool = NULL; |
| 107 |
_multiSlider = NULL; |
| 108 |
|
| 109 |
_layoutWidth = 160; |
| 110 |
_layoutHeight = 40; |
| 111 |
|
| 112 |
// Build the widget! |
| 113 |
Widget w = buildWidget(getParentWidget()); |
| 114 |
setBaseWidget(w); |
| 115 |
} |
| 116 |
|
| 117 |
SoXtMinMaxSlider::~SoXtMinMaxSlider() {} |
| 118 |
|
| 119 |
Widget |
| 120 |
SoXtMinMaxSlider::buildWidget( Widget parent ) |
| 121 |
{ |
| 122 |
Arg wargs[10]; |
| 123 |
int nargs; |
| 124 |
ScaleStyleData styleData; |
| 125 |
|
| 126 |
nargs = 0; |
| 127 |
XtSetArg(wargs[nargs],XmNorientation, XmHORIZONTAL ); nargs++; |
| 128 |
XtSetArg(wargs[nargs],XmNminimum, _startingMin ); nargs++; |
| 129 |
XtSetArg(wargs[nargs],XmNmaximum, _startingMax ); nargs++; |
| 130 |
widget = XtCreateManagedWidget(getWidgetName(),xmScaleWidgetClass, |
| 131 |
parent,wargs, nargs); |
| 132 |
|
| 133 |
|
| 134 |
/* see if user decided to override any of these values */ |
| 135 |
/* get startingMin and startingMax as the defaults in the query */ |
| 136 |
_scaleStyleResources[2].default_addr = (XtPointer) (unsigned long) _startingMax; |
| 137 |
_scaleStyleResources[3].default_addr = (XtPointer) (unsigned long) _startingMin; |
| 138 |
/* ask the question */ |
| 139 |
XtGetApplicationResources( widget, (XtPointer) &styleData, |
| 140 |
_scaleStyleResources, XtNumber( _scaleStyleResources), |
| 141 |
NULL, 0 ); |
| 142 |
nargs = 0; |
| 143 |
XtSetArg(wargs[nargs],XmNorientation, styleData.orientation ); nargs++; |
| 144 |
XtSetArg(wargs[nargs],XmNdecimalPoints, styleData.decimalPoints ); nargs++; |
| 145 |
XtSetArg(wargs[nargs],XmNminimum, styleData.minimum ); nargs++; |
| 146 |
XtSetArg(wargs[nargs],XmNmaximum, styleData.maximum ); nargs++; |
| 147 |
XtSetValues( widget, wargs, nargs ); |
| 148 |
|
| 149 |
/* establish all the callbacks */ |
| 150 |
|
| 151 |
XtAddCallback( widget, XmNdragCallback, |
| 152 |
(XtCallbackProc) &SoXtMinMaxSlider::sliderCallback, |
| 153 |
(XtPointer) this ); |
| 154 |
XtAddCallback( widget, XmNvalueChangedCallback, |
| 155 |
(XtCallbackProc) &SoXtMinMaxSlider::sliderCallback, |
| 156 |
(XtPointer) this ); |
| 157 |
return widget; |
| 158 |
} |
| 159 |
|
| 160 |
short |
| 161 |
SoXtMinMaxSlider::getNumDecimals() |
| 162 |
{ |
| 163 |
if ( widget == NULL) |
| 164 |
return 0; |
| 165 |
|
| 166 |
Arg wargs[10]; |
| 167 |
short decimals; |
| 168 |
|
| 169 |
XtSetArg( wargs[0], XmNdecimalPoints, &decimals ); |
| 170 |
XtGetValues( widget, wargs, 1 ); |
| 171 |
|
| 172 |
return decimals; |
| 173 |
} |
| 174 |
|
| 175 |
float |
| 176 |
SoXtMinMaxSlider::convertSliderToFloat( int sliderValue ) |
| 177 |
{ |
| 178 |
short decimals; |
| 179 |
float theVal; |
| 180 |
int count; |
| 181 |
|
| 182 |
/* shift over the appropriate number of decimal places */ |
| 183 |
decimals = getNumDecimals(); |
| 184 |
if (decimals < 0) |
| 185 |
decimals = 0; |
| 186 |
for( count = 0, theVal = sliderValue; count < decimals; count++ ) |
| 187 |
theVal /= 10.0; |
| 188 |
return( theVal ); |
| 189 |
} |
| 190 |
|
| 191 |
int |
| 192 |
SoXtMinMaxSlider::convertFloatToSlider( float floatValue ) |
| 193 |
{ |
| 194 |
short decimals; |
| 195 |
float convertedFloat; |
| 196 |
int count, returnValue; |
| 197 |
|
| 198 |
/* shift over the appropriate number of decimal places */ |
| 199 |
decimals = getNumDecimals(); |
| 200 |
if (decimals < 0) |
| 201 |
decimals = 0; |
| 202 |
for( count = 0, convertedFloat = floatValue; count < decimals; count++ ) |
| 203 |
convertedFloat *= 10.0; |
| 204 |
returnValue = (int) convertedFloat; |
| 205 |
return( returnValue ); |
| 206 |
} |
| 207 |
|
| 208 |
float |
| 209 |
SoXtMinMaxSlider::getSliderValue() |
| 210 |
{ |
| 211 |
if (widget == NULL) |
| 212 |
return 0.0; |
| 213 |
|
| 214 |
Arg wargs[10]; |
| 215 |
int motifIntValue; |
| 216 |
|
| 217 |
/* get motifIntValue */ |
| 218 |
XtSetArg( wargs[0], XmNvalue, &motifIntValue ); |
| 219 |
XtGetValues( widget, wargs, 1 ); |
| 220 |
|
| 221 |
return ( convertSliderToFloat( motifIntValue ) ); |
| 222 |
} |
| 223 |
|
| 224 |
float |
| 225 |
SoXtMinMaxSlider::getSliderMin() |
| 226 |
{ |
| 227 |
if (widget == NULL) |
| 228 |
return 0.0; |
| 229 |
|
| 230 |
Arg wargs[10]; |
| 231 |
int motifIntValue; |
| 232 |
|
| 233 |
/* get motifIntValue */ |
| 234 |
XtSetArg( wargs[0], XmNminimum, &motifIntValue ); |
| 235 |
XtGetValues( widget, wargs, 1 ); |
| 236 |
|
| 237 |
return ( convertSliderToFloat( motifIntValue ) ); |
| 238 |
} |
| 239 |
|
| 240 |
float |
| 241 |
SoXtMinMaxSlider::getSliderMax() |
| 242 |
{ |
| 243 |
if (widget == NULL) |
| 244 |
return 0.0; |
| 245 |
|
| 246 |
Arg wargs[10]; |
| 247 |
int motifIntValue; |
| 248 |
|
| 249 |
/* get motifIntValue */ |
| 250 |
XtSetArg( wargs[0], XmNmaximum, &motifIntValue ); |
| 251 |
XtGetValues( widget, wargs, 1 ); |
| 252 |
|
| 253 |
return ( convertSliderToFloat( motifIntValue ) ); |
| 254 |
} |
| 255 |
|
| 256 |
void |
| 257 |
SoXtMinMaxSlider::setSliderValue( float theVal ) |
| 258 |
{ |
| 259 |
if (widget == NULL) |
| 260 |
return; |
| 261 |
|
| 262 |
int motifIntValue; |
| 263 |
|
| 264 |
motifIntValue = convertFloatToSlider( theVal ); |
| 265 |
XmScaleSetValue( widget, motifIntValue ); |
| 266 |
} |
| 267 |
|
| 268 |
void |
| 269 |
SoXtMinMaxSlider::setSliderMin( float theMin ) |
| 270 |
{ |
| 271 |
if (widget == NULL) |
| 272 |
return; |
| 273 |
|
| 274 |
Arg wargs[10]; |
| 275 |
int motifIntValue; |
| 276 |
|
| 277 |
motifIntValue = convertFloatToSlider( theMin ); |
| 278 |
XtSetArg( wargs[0], XmNminimum, motifIntValue ); |
| 279 |
XtSetValues( widget, wargs, 1 ); |
| 280 |
} |
| 281 |
|
| 282 |
void |
| 283 |
SoXtMinMaxSlider::setSliderMax( float theMax ) |
| 284 |
{ |
| 285 |
if (widget == NULL) |
| 286 |
return; |
| 287 |
|
| 288 |
Arg wargs[10]; |
| 289 |
int motifIntValue; |
| 290 |
|
| 291 |
motifIntValue = convertFloatToSlider( theMax ); |
| 292 |
XtSetArg( wargs[0], XmNmaximum, motifIntValue ); |
| 293 |
XtSetValues( widget, wargs, 1 ); |
| 294 |
} |
| 295 |
|
| 296 |
void |
| 297 |
SoXtMinMaxSlider::sliderCallback( Widget, void *client_data, void * ) |
| 298 |
{ |
| 299 |
float theVal; |
| 300 |
SoXtMinMaxSlider *obj = (SoXtMinMaxSlider *) client_data; |
| 301 |
|
| 302 |
/* Get the value from the slider */ |
| 303 |
theVal = obj->getSliderValue(); |
| 304 |
|
| 305 |
// If you can find a SliderTool in the ancestry... |
| 306 |
if ( obj->_sliderTool ) { |
| 307 |
/* Set the value throughout the SliderTool */ |
| 308 |
obj->_sliderTool->toolSetValue( theVal ); |
| 309 |
} |
| 310 |
|
| 311 |
// If you can find a SoMultiSlider in the ancestry... |
| 312 |
if ( obj->_multiSlider ) |
| 313 |
obj->_multiSlider->exportValuesToInventor(); // send the values out |
| 314 |
} |