| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include "tkPort.h" |
| 25 |
#include "default.h" |
| 26 |
#include "tkInt.h" |
| 27 |
#include <math.h> |
| 28 |
#include "tkVMacro.h" |
| 29 |
#include "tkScale.h" |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
static char *orientStrings[] = { |
| 37 |
"horizontal", "vertical", (char *) NULL |
| 38 |
}; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
static char *stateStrings[] = { |
| 46 |
"active", "disabled", "normal", (char *) NULL |
| 47 |
}; |
| 48 |
|
| 49 |
static Tk_OptionSpec optionSpecs[] = { |
| 50 |
{TK_OPTION_BORDER, "-activebackground", "activeBackground", "Foreground", |
| 51 |
DEF_SCALE_ACTIVE_BG_COLOR, -1, Tk_Offset(TkScale, activeBorder), |
| 52 |
0, (ClientData) DEF_SCALE_ACTIVE_BG_MONO, 0}, |
| 53 |
{TK_OPTION_BORDER, "-background", "background", "Background", |
| 54 |
DEF_SCALE_BG_COLOR, -1, Tk_Offset(TkScale, bgBorder), |
| 55 |
0, (ClientData) DEF_SCALE_BG_MONO, 0}, |
| 56 |
{TK_OPTION_DOUBLE, "-bigincrement", "bigIncrement", "BigIncrement", |
| 57 |
DEF_SCALE_BIG_INCREMENT, -1, Tk_Offset(TkScale, bigIncrement), |
| 58 |
0, 0, 0}, |
| 59 |
{TK_OPTION_SYNONYM, "-bd", (char *) NULL, (char *) NULL, |
| 60 |
(char *) NULL, 0, -1, 0, (ClientData) "-borderwidth", 0}, |
| 61 |
{TK_OPTION_SYNONYM, "-bg", (char *) NULL, (char *) NULL, |
| 62 |
(char *) NULL, 0, -1, 0, (ClientData) "-background", 0}, |
| 63 |
{TK_OPTION_PIXELS, "-borderwidth", "borderWidth", "BorderWidth", |
| 64 |
DEF_SCALE_BORDER_WIDTH, -1, Tk_Offset(TkScale, borderWidth), |
| 65 |
0, 0, 0}, |
| 66 |
{TK_OPTION_CALLBACK, "-command", "command", "Command", |
| 67 |
DEF_SCALE_COMMAND, -1, Tk_Offset(TkScale, command), |
| 68 |
TK_OPTION_NULL_OK, 0, 0}, |
| 69 |
{TK_OPTION_CURSOR, "-cursor", "cursor", "Cursor", |
| 70 |
DEF_SCALE_CURSOR, -1, Tk_Offset(TkScale, cursor), |
| 71 |
TK_OPTION_NULL_OK, 0, 0}, |
| 72 |
{TK_OPTION_INT, "-digits", "digits", "Digits", |
| 73 |
DEF_SCALE_DIGITS, -1, Tk_Offset(TkScale, digits), |
| 74 |
0, 0, 0}, |
| 75 |
{TK_OPTION_SYNONYM, "-fg", "foreground", (char *) NULL, |
| 76 |
(char *) NULL, 0, -1, 0, (ClientData) "-foreground", 0}, |
| 77 |
{TK_OPTION_FONT, "-font", "font", "Font", |
| 78 |
DEF_SCALE_FONT, -1, Tk_Offset(TkScale, tkfont), 0, 0, 0}, |
| 79 |
{TK_OPTION_COLOR, "-foreground", "foreground", "Foreground", |
| 80 |
DEF_SCALE_FG_COLOR, -1, Tk_Offset(TkScale, textColorPtr), 0, |
| 81 |
(ClientData) DEF_SCALE_FG_MONO, 0}, |
| 82 |
{TK_OPTION_DOUBLE, "-from", "from", "From", DEF_SCALE_FROM, -1, |
| 83 |
Tk_Offset(TkScale, fromValue), 0, 0, 0}, |
| 84 |
{TK_OPTION_BORDER, "-highlightbackground", "highlightBackground", |
| 85 |
"HighlightBackground", DEF_SCALE_HIGHLIGHT_BG_COLOR, |
| 86 |
-1, Tk_Offset(TkScale, highlightBorder), |
| 87 |
0, (ClientData) DEF_SCALE_HIGHLIGHT_BG_MONO, 0}, |
| 88 |
{TK_OPTION_COLOR, "-highlightcolor", "highlightColor", "HighlightColor", |
| 89 |
DEF_SCALE_HIGHLIGHT, -1, Tk_Offset(TkScale, highlightColorPtr), |
| 90 |
0, 0, 0}, |
| 91 |
{TK_OPTION_PIXELS, "-highlightthickness", "highlightThickness", |
| 92 |
"HighlightThickness", DEF_SCALE_HIGHLIGHT_WIDTH, -1, |
| 93 |
Tk_Offset(TkScale, highlightWidth), 0, 0, 0}, |
| 94 |
{TK_OPTION_STRING, "-label", "label", "Label", |
| 95 |
DEF_SCALE_LABEL, -1, Tk_Offset(TkScale, label), |
| 96 |
TK_OPTION_NULL_OK, 0, 0}, |
| 97 |
{TK_OPTION_PIXELS, "-length", "length", "Length", |
| 98 |
DEF_SCALE_LENGTH, -1, Tk_Offset(TkScale, length), 0, 0, 0}, |
| 99 |
{TK_OPTION_STRING_TABLE, "-orient", "orient", "Orient", |
| 100 |
DEF_SCALE_ORIENT, -1, Tk_Offset(TkScale, orient), |
| 101 |
0, (ClientData) orientStrings, 0}, |
| 102 |
{TK_OPTION_RELIEF, "-relief", "relief", "Relief", |
| 103 |
DEF_SCALE_RELIEF, -1, Tk_Offset(TkScale, relief), 0, 0, 0}, |
| 104 |
{TK_OPTION_INT, "-repeatdelay", "repeatDelay", "RepeatDelay", |
| 105 |
DEF_SCALE_REPEAT_DELAY, -1, Tk_Offset(TkScale, repeatDelay), |
| 106 |
0, 0, 0}, |
| 107 |
{TK_OPTION_INT, "-repeatinterval", "repeatInterval", "RepeatInterval", |
| 108 |
DEF_SCALE_REPEAT_INTERVAL, -1, Tk_Offset(TkScale, repeatInterval), |
| 109 |
0, 0, 0}, |
| 110 |
{TK_OPTION_DOUBLE, "-resolution", "resolution", "Resolution", |
| 111 |
DEF_SCALE_RESOLUTION, -1, Tk_Offset(TkScale, resolution), |
| 112 |
0, 0, 0}, |
| 113 |
{TK_OPTION_BOOLEAN, "-showvalue", "showValue", "ShowValue", |
| 114 |
DEF_SCALE_SHOW_VALUE, -1, Tk_Offset(TkScale, showValue), |
| 115 |
0, 0, 0}, |
| 116 |
{TK_OPTION_PIXELS, "-sliderlength", "sliderLength", "SliderLength", |
| 117 |
DEF_SCALE_SLIDER_LENGTH, -1, Tk_Offset(TkScale, sliderLength), |
| 118 |
0, 0, 0}, |
| 119 |
{TK_OPTION_RELIEF, "-sliderrelief", "sliderRelief", "SliderRelief", |
| 120 |
DEF_SCALE_SLIDER_RELIEF, -1, Tk_Offset(TkScale, sliderRelief), |
| 121 |
0, 0, 0}, |
| 122 |
{TK_OPTION_STRING_TABLE, "-state", "state", "State", |
| 123 |
DEF_SCALE_STATE, -1, Tk_Offset(TkScale, state), |
| 124 |
0, (ClientData) stateStrings, 0}, |
| 125 |
{TK_OPTION_STRING, "-takefocus", "takeFocus", "TakeFocus", |
| 126 |
DEF_SCALE_TAKE_FOCUS, Tk_Offset(TkScale, takeFocusPtr), -1, |
| 127 |
TK_OPTION_NULL_OK, 0, 0}, |
| 128 |
{TK_OPTION_DOUBLE, "-tickinterval", "tickInterval", "TickInterval", |
| 129 |
DEF_SCALE_TICK_INTERVAL, -1, Tk_Offset(TkScale, tickInterval), |
| 130 |
0, 0, 0}, |
| 131 |
{TK_OPTION_DOUBLE, "-to", "to", "To", |
| 132 |
DEF_SCALE_TO, -1, Tk_Offset(TkScale, toValue), 0, 0, 0}, |
| 133 |
{TK_OPTION_COLOR, "-troughcolor", "troughColor", "Background", |
| 134 |
DEF_SCALE_TROUGH_COLOR, -1, Tk_Offset(TkScale, troughColorPtr), |
| 135 |
0, (ClientData) DEF_SCALE_TROUGH_MONO, 0}, |
| 136 |
{TK_OPTION_STRING, "-variable", "variable", "Variable", |
| 137 |
DEF_SCALE_VARIABLE, Tk_Offset(TkScale, varNamePtr), -1, |
| 138 |
TK_OPTION_NULL_OK, 0, 0}, |
| 139 |
{TK_OPTION_PIXELS, "-width", "width", "Width", |
| 140 |
DEF_SCALE_WIDTH, -1, Tk_Offset(TkScale, width), 0, 0, 0}, |
| 141 |
{TK_OPTION_END, (char *) NULL, (char *) NULL, (char *) NULL, |
| 142 |
(char *) NULL, 0, -1, 0, 0, 0} |
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
static CONST char *commandNames[] = { |
| 152 |
"cget", "configure", "coords", "get", "identify", "set", (char *) NULL |
| 153 |
}; |
| 154 |
|
| 155 |
enum command { |
| 156 |
COMMAND_CGET, COMMAND_CONFIGURE, COMMAND_COORDS, COMMAND_GET, |
| 157 |
COMMAND_IDENTIFY, COMMAND_SET |
| 158 |
}; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
static void ComputeFormat _ANSI_ARGS_((TkScale *scalePtr)); |
| 165 |
static void ComputeScaleGeometry _ANSI_ARGS_((TkScale *scalePtr)); |
| 166 |
static int ConfigureScale _ANSI_ARGS_((Tcl_Interp *interp, |
| 167 |
TkScale *scalePtr, int objc, |
| 168 |
Tcl_Obj *CONST objv[])); |
| 169 |
static void DestroyScale _ANSI_ARGS_((char *memPtr)); |
| 170 |
static void ScaleCmdDeletedProc _ANSI_ARGS_(( |
| 171 |
ClientData clientData)); |
| 172 |
static void ScaleEventProc _ANSI_ARGS_((ClientData clientData, |
| 173 |
XEvent *eventPtr)); |
| 174 |
static char * ScaleVarProc _ANSI_ARGS_((ClientData clientData, |
| 175 |
Tcl_Interp *interp, Var name1, |
| 176 |
CONST char *name2, int flags)); |
| 177 |
static int ScaleWidgetObjCmd _ANSI_ARGS_((ClientData clientData, |
| 178 |
Tcl_Interp *interp, int objc, |
| 179 |
Tcl_Obj *CONST objv[])); |
| 180 |
static void ScaleWorldChanged _ANSI_ARGS_(( |
| 181 |
ClientData instanceData)); |
| 182 |
static void ScaleSetVariable _ANSI_ARGS_((TkScale *scalePtr)); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
static Tk_ClassProcs scaleClass = { |
| 190 |
sizeof(Tk_ClassProcs), |
| 191 |
ScaleWorldChanged, |
| 192 |
}; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
int |
| 214 |
Tk_ScaleObjCmd(clientData, interp, objc, objv) |
| 215 |
ClientData clientData; |
| 216 |
Tcl_Interp *interp; |
| 217 |
int objc; |
| 218 |
Tcl_Obj *CONST objv[]; |
| 219 |
{ |
| 220 |
register TkScale *scalePtr; |
| 221 |
Tk_OptionTable optionTable; |
| 222 |
Tk_Window tkwin; |
| 223 |
|
| 224 |
if (objc < 2) { |
| 225 |
Tcl_WrongNumArgs(interp, 1, objv, "pathName ?options?"); |
| 226 |
return TCL_ERROR; |
| 227 |
} |
| 228 |
|
| 229 |
tkwin = Tk_CreateWindowFromPath(interp, Tk_MainWindow(interp), |
| 230 |
Tcl_GetString(objv[1]), (char *) NULL); |
| 231 |
if (tkwin == NULL) { |
| 232 |
return TCL_ERROR; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
optionTable = Tk_CreateOptionTable(interp, optionSpecs); |
| 241 |
|
| 242 |
Tk_SetClass(tkwin, "Scale"); |
| 243 |
scalePtr = TkpCreateScale(tkwin); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
scalePtr->tkwin = tkwin; |
| 252 |
scalePtr->display = Tk_Display(tkwin); |
| 253 |
scalePtr->interp = interp; |
| 254 |
scalePtr->widgetCmd = Tcl_CreateObjCommand(interp, |
| 255 |
Tk_PathName(scalePtr->tkwin), ScaleWidgetObjCmd, |
| 256 |
(ClientData) scalePtr, ScaleCmdDeletedProc); |
| 257 |
scalePtr->optionTable = optionTable; |
| 258 |
scalePtr->orient = ORIENT_VERTICAL; |
| 259 |
scalePtr->width = 0; |
| 260 |
scalePtr->length = 0; |
| 261 |
scalePtr->value = 0.0; |
| 262 |
scalePtr->varNamePtr = NULL; |
| 263 |
scalePtr->fromValue = 0.0; |
| 264 |
scalePtr->toValue = 0.0; |
| 265 |
scalePtr->tickInterval = 0.0; |
| 266 |
scalePtr->resolution = 1.0; |
| 267 |
scalePtr->digits = 0; |
| 268 |
scalePtr->bigIncrement = 0.0; |
| 269 |
scalePtr->command = NULL; |
| 270 |
scalePtr->repeatDelay = 0; |
| 271 |
scalePtr->repeatInterval = 0; |
| 272 |
scalePtr->label = NULL; |
| 273 |
scalePtr->labelLength = 0; |
| 274 |
scalePtr->state = STATE_NORMAL; |
| 275 |
scalePtr->borderWidth = 0; |
| 276 |
scalePtr->bgBorder = NULL; |
| 277 |
scalePtr->activeBorder = NULL; |
| 278 |
scalePtr->sliderRelief = TK_RELIEF_RAISED; |
| 279 |
scalePtr->troughColorPtr = NULL; |
| 280 |
scalePtr->troughGC = None; |
| 281 |
scalePtr->copyGC = None; |
| 282 |
scalePtr->tkfont = NULL; |
| 283 |
scalePtr->textColorPtr = NULL; |
| 284 |
scalePtr->textGC = None; |
| 285 |
scalePtr->relief = TK_RELIEF_FLAT; |
| 286 |
scalePtr->highlightWidth = 0; |
| 287 |
scalePtr->highlightBorder = NULL; |
| 288 |
scalePtr->highlightColorPtr = NULL; |
| 289 |
scalePtr->inset = 0; |
| 290 |
scalePtr->sliderLength = 0; |
| 291 |
scalePtr->showValue = 0; |
| 292 |
scalePtr->horizLabelY = 0; |
| 293 |
scalePtr->horizValueY = 0; |
| 294 |
scalePtr->horizTroughY = 0; |
| 295 |
scalePtr->horizTickY = 0; |
| 296 |
scalePtr->vertTickRightX = 0; |
| 297 |
scalePtr->vertValueRightX = 0; |
| 298 |
scalePtr->vertTroughX = 0; |
| 299 |
scalePtr->vertLabelX = 0; |
| 300 |
scalePtr->fontHeight = 0; |
| 301 |
scalePtr->cursor = None; |
| 302 |
scalePtr->takeFocusPtr = NULL; |
| 303 |
scalePtr->flags = NEVER_SET; |
| 304 |
|
| 305 |
Tk_SetClassProcs(scalePtr->tkwin, &scaleClass, (ClientData) scalePtr); |
| 306 |
Tk_CreateEventHandler(scalePtr->tkwin, |
| 307 |
ExposureMask|StructureNotifyMask|FocusChangeMask, |
| 308 |
ScaleEventProc, (ClientData) scalePtr); |
| 309 |
|
| 310 |
if ((Tk_InitOptions(interp, (char *) scalePtr, optionTable, tkwin) |
| 311 |
!= TCL_OK) || |
| 312 |
(ConfigureScale(interp, scalePtr, objc - 2, objv + 2) != TCL_OK)) { |
| 313 |
Tk_DestroyWindow(scalePtr->tkwin); |
| 314 |
return TCL_ERROR; |
| 315 |
} |
| 316 |
|
| 317 |
Tcl_SetResult(interp, Tk_PathName(scalePtr->tkwin), TCL_STATIC); |
| 318 |
return TCL_OK; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
static int |
| 340 |
ScaleWidgetObjCmd(clientData, interp, objc, objv) |
| 341 |
ClientData clientData; |
| 342 |
|
| 343 |
Tcl_Interp *interp; |
| 344 |
int objc; |
| 345 |
Tcl_Obj *CONST objv[]; |
| 346 |
{ |
| 347 |
TkScale *scalePtr = (TkScale *) clientData; |
| 348 |
Tcl_Obj *objPtr; |
| 349 |
int index, result; |
| 350 |
|
| 351 |
if (objc < 2) { |
| 352 |
Tcl_WrongNumArgs(interp, 1, objv, "option ?arg arg ...?"); |
| 353 |
return TCL_ERROR; |
| 354 |
} |
| 355 |
result = Tcl_GetIndexFromObj(interp, objv[1], commandNames, |
| 356 |
"option", 0, &index); |
| 357 |
if (result != TCL_OK) { |
| 358 |
return result; |
| 359 |
} |
| 360 |
Tcl_Preserve((ClientData) scalePtr); |
| 361 |
|
| 362 |
switch (index) { |
| 363 |
case COMMAND_CGET: { |
| 364 |
if (objc != 3) { |
| 365 |
Tcl_WrongNumArgs(interp, 1, objv, "cget option"); |
| 366 |
goto error; |
| 367 |
} |
| 368 |
objPtr = Tk_GetOptionValue(interp, (char *) scalePtr, |
| 369 |
scalePtr->optionTable, objv[2], scalePtr->tkwin); |
| 370 |
if (objPtr == NULL) { |
| 371 |
goto error; |
| 372 |
} else { |
| 373 |
Tcl_SetObjResult(interp, objPtr); |
| 374 |
} |
| 375 |
break; |
| 376 |
} |
| 377 |
case COMMAND_CONFIGURE: { |
| 378 |
if (objc <= 3) { |
| 379 |
objPtr = Tk_GetOptionInfo(interp, (char *) scalePtr, |
| 380 |
scalePtr->optionTable, |
| 381 |
(objc == 3) ? objv[2] : (Tcl_Obj *) NULL, |
| 382 |
scalePtr->tkwin); |
| 383 |
if (objPtr == NULL) { |
| 384 |
goto error; |
| 385 |
} else { |
| 386 |
Tcl_SetObjResult(interp, objPtr); |
| 387 |
} |
| 388 |
} else { |
| 389 |
result = ConfigureScale(interp, scalePtr, objc-2, objv+2); |
| 390 |
} |
| 391 |
break; |
| 392 |
} |
| 393 |
case COMMAND_COORDS: { |
| 394 |
int x, y ; |
| 395 |
double value; |
| 396 |
char buf[TCL_INTEGER_SPACE * 2]; |
| 397 |
|
| 398 |
if ((objc != 2) && (objc != 3)) { |
| 399 |
Tcl_WrongNumArgs(interp, 1, objv, "coords ?value?"); |
| 400 |
goto error; |
| 401 |
} |
| 402 |
if (objc == 3) { |
| 403 |
if (Tcl_GetDoubleFromObj(interp, objv[2], &value) |
| 404 |
!= TCL_OK) { |
| 405 |
goto error; |
| 406 |
} |
| 407 |
} else { |
| 408 |
value = scalePtr->value; |
| 409 |
} |
| 410 |
if (scalePtr->orient == ORIENT_VERTICAL) { |
| 411 |
x = scalePtr->vertTroughX + scalePtr->width/2 |
| 412 |
+ scalePtr->borderWidth; |
| 413 |
y = TkScaleValueToPixel(scalePtr, value); |
| 414 |
} else { |
| 415 |
x = TkScaleValueToPixel(scalePtr, value); |
| 416 |
y = scalePtr->horizTroughY + scalePtr->width/2 |
| 417 |
+ scalePtr->borderWidth; |
| 418 |
} |
| 419 |
Tcl_IntResults(interp,2,0, x, y); |
| 420 |
break; |
| 421 |
} |
| 422 |
case COMMAND_GET: { |
| 423 |
double value; |
| 424 |
int x, y; |
| 425 |
char buf[TCL_DOUBLE_SPACE]; |
| 426 |
|
| 427 |
if ((objc != 2) && (objc != 4)) { |
| 428 |
Tcl_WrongNumArgs(interp, 1, objv, "get ?x y?"); |
| 429 |
goto error; |
| 430 |
} |
| 431 |
if (objc == 2) { |
| 432 |
value = scalePtr->value; |
| 433 |
} else { |
| 434 |
if ((Tcl_GetIntFromObj(interp, objv[2], &x) != TCL_OK) |
| 435 |
|| (Tcl_GetIntFromObj(interp, objv[3], &y) |
| 436 |
!= TCL_OK)) { |
| 437 |
goto error; |
| 438 |
} |
| 439 |
value = TkScalePixelToValue(scalePtr, x, y); |
| 440 |
} |
| 441 |
sprintf(buf, scalePtr->format, value); |
| 442 |
Tcl_SetResult(interp, buf, TCL_VOLATILE); |
| 443 |
break; |
| 444 |
} |
| 445 |
case COMMAND_IDENTIFY: { |
| 446 |
int x, y, thing; |
| 447 |
|
| 448 |
if (objc != 4) { |
| 449 |
Tcl_WrongNumArgs(interp, 1, objv, "identify x y"); |
| 450 |
goto error; |
| 451 |
} |
| 452 |
if ((Tcl_GetIntFromObj(interp, objv[2], &x) != TCL_OK) |
| 453 |
|| (Tcl_GetIntFromObj(interp, objv[3], &y) != TCL_OK)) { |
| 454 |
goto error; |
| 455 |
} |
| 456 |
thing = TkpScaleElement(scalePtr, x,y); |
| 457 |
switch (thing) { |
| 458 |
case TROUGH1: |
| 459 |
Tcl_SetResult(interp, "trough1", TCL_STATIC); |
| 460 |
break; |
| 461 |
case SLIDER: |
| 462 |
Tcl_SetResult(interp, "slider", TCL_STATIC); |
| 463 |
break; |
| 464 |
case TROUGH2: |
| 465 |
Tcl_SetResult(interp, "trough2", TCL_STATIC); |
| 466 |
break; |
| 467 |
} |
| 468 |
break; |
| 469 |
} |
| 470 |
case COMMAND_SET: { |
| 471 |
double value; |
| 472 |
|
| 473 |
if (objc != 3) { |
| 474 |
Tcl_WrongNumArgs(interp, 1, objv, "set value"); |
| 475 |
goto error; |
| 476 |
} |
| 477 |
if (Tcl_GetDoubleFromObj(interp, objv[2], &value) != TCL_OK) { |
| 478 |
goto error; |
| 479 |
} |
| 480 |
if (scalePtr->state != STATE_DISABLED) { |
| 481 |
TkScaleSetValue(scalePtr, value, 1, 1); |
| 482 |
} |
| 483 |
break; |
| 484 |
} |
| 485 |
} |
| 486 |
Tcl_Release((ClientData) scalePtr); |
| 487 |
return result; |
| 488 |
|
| 489 |
error: |
| 490 |
Tcl_Release((ClientData) scalePtr); |
| 491 |
return TCL_ERROR; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
static void |
| 513 |
DestroyScale(memPtr) |
| 514 |
char *memPtr; |
| 515 |
{ |
| 516 |
register TkScale *scalePtr = (TkScale *) memPtr; |
| 517 |
|
| 518 |
scalePtr->flags |= SCALE_DELETED; |
| 519 |
|
| 520 |
Tcl_DeleteCommandFromToken(scalePtr->interp, scalePtr->widgetCmd); |
| 521 |
if (scalePtr->flags & REDRAW_PENDING) { |
| 522 |
Tcl_CancelIdleCall(TkpDisplayScale, (ClientData) scalePtr); |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
if (scalePtr->varNamePtr != NULL) { |
| 532 |
Lang_UntraceVar(scalePtr->interp, scalePtr->varNamePtr, |
| 533 |
TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS, |
| 534 |
ScaleVarProc, (ClientData) scalePtr); |
| 535 |
} |
| 536 |
if (scalePtr->troughGC != None) { |
| 537 |
Tk_FreeGC(scalePtr->display, scalePtr->troughGC); |
| 538 |
} |
| 539 |
if (scalePtr->copyGC != None) { |
| 540 |
Tk_FreeGC(scalePtr->display, scalePtr->copyGC); |
| 541 |
} |
| 542 |
if (scalePtr->textGC != None) { |
| 543 |
Tk_FreeGC(scalePtr->display, scalePtr->textGC); |
| 544 |
} |
| 545 |
Tk_FreeConfigOptions((char *) scalePtr, scalePtr->optionTable, |
| 546 |
scalePtr->tkwin); |
| 547 |
scalePtr->tkwin = NULL; |
| 548 |
TkpDestroyScale(scalePtr); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
static int |
| 573 |
ConfigureScale(interp, scalePtr, objc, objv) |
| 574 |
Tcl_Interp *interp; |
| 575 |
register TkScale *scalePtr; |
| 576 |
|
| 577 |
int objc; |
| 578 |
Tcl_Obj *CONST objv[]; |
| 579 |
{ |
| 580 |
Tk_SavedOptions savedOptions; |
| 581 |
Tcl_Obj *errorResult = NULL; |
| 582 |
int error; |
| 583 |
double oldValue = scalePtr->value; |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
if (scalePtr->varNamePtr != NULL) { |
| 590 |
Lang_UntraceVar(interp, scalePtr->varNamePtr, |
| 591 |
TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS, |
| 592 |
ScaleVarProc, (ClientData) scalePtr); |
| 593 |
} |
| 594 |
|
| 595 |
for (error = 0; error <= 1; error++) { |
| 596 |
if (!error) { |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
if (Tk_SetOptions(interp, (char *) scalePtr, |
| 602 |
scalePtr->optionTable, objc, objv, |
| 603 |
scalePtr->tkwin, &savedOptions, (int *) NULL) != TCL_OK) { |
| 604 |
continue; |
| 605 |
} |
| 606 |
} else { |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
errorResult = Tcl_GetObjResult(interp); |
| 612 |
Tcl_IncrRefCount(errorResult); |
| 613 |
Tk_RestoreSavedOptions(&savedOptions); |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
if (scalePtr->varNamePtr != NULL) { |
| 623 |
double value; |
| 624 |
Tcl_Obj *valuePtr; |
| 625 |
|
| 626 |
valuePtr = Tcl_ObjGetVar2(interp, scalePtr->varNamePtr, NULL, |
| 627 |
TCL_GLOBAL_ONLY); |
| 628 |
if ((valuePtr != NULL) && |
| 629 |
(Tcl_GetDoubleFromObj(NULL, valuePtr, &value) == TCL_OK)) { |
| 630 |
scalePtr->value = TkRoundToResolution(scalePtr, value); |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
scalePtr->fromValue = TkRoundToResolution(scalePtr, |
| 640 |
scalePtr->fromValue); |
| 641 |
scalePtr->toValue = TkRoundToResolution(scalePtr, scalePtr->toValue); |
| 642 |
scalePtr->tickInterval = TkRoundToResolution(scalePtr, |
| 643 |
scalePtr->tickInterval); |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
if ((scalePtr->tickInterval < 0) |
| 651 |
^ ((scalePtr->toValue - scalePtr->fromValue) < 0)) { |
| 652 |
scalePtr->tickInterval = -scalePtr->tickInterval; |
| 653 |
} |
| 654 |
|
| 655 |
ComputeFormat(scalePtr); |
| 656 |
|
| 657 |
scalePtr->labelLength = scalePtr->label ? strlen(scalePtr->label) : 0; |
| 658 |
|
| 659 |
Tk_SetBackgroundFromBorder(scalePtr->tkwin, scalePtr->bgBorder); |
| 660 |
|
| 661 |
if (scalePtr->highlightWidth < 0) { |
| 662 |
scalePtr->highlightWidth = 0; |
| 663 |
} |
| 664 |
scalePtr->inset = scalePtr->highlightWidth + scalePtr->borderWidth; |
| 665 |
break; |
| 666 |
} |
| 667 |
if (!error) { |
| 668 |
Tk_FreeSavedOptions(&savedOptions); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
TkScaleSetValue(scalePtr, scalePtr->value, 0, 1); |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
if (scalePtr->varNamePtr != NULL) { |
| 685 |
Tcl_Obj *valuePtr; |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
valuePtr = Tcl_ObjGetVar2(interp, scalePtr->varNamePtr, NULL, |
| 692 |
TCL_GLOBAL_ONLY); |
| 693 |
if ((valuePtr == NULL) || (scalePtr->value != oldValue) |
| 694 |
|| (Tcl_GetDoubleFromObj(NULL, valuePtr, &oldValue) != TCL_OK) |
| 695 |
|| (scalePtr->value != oldValue)) { |
| 696 |
ScaleSetVariable(scalePtr); |
| 697 |
} |
| 698 |
Lang_TraceVar(interp, scalePtr->varNamePtr, |
| 699 |
TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS, |
| 700 |
ScaleVarProc, (ClientData) scalePtr); |
| 701 |
} |
| 702 |
|
| 703 |
ScaleWorldChanged((ClientData) scalePtr); |
| 704 |
if (error) { |
| 705 |
Tcl_SetObjResult(interp, errorResult); |
| 706 |
Tcl_DecrRefCount(errorResult); |
| 707 |
return TCL_ERROR; |
| 708 |
} else { |
| 709 |
return TCL_OK; |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
static void |
| 732 |
ScaleWorldChanged(instanceData) |
| 733 |
ClientData instanceData; |
| 734 |
{ |
| 735 |
XGCValues gcValues; |
| 736 |
GC gc; |
| 737 |
TkScale *scalePtr; |
| 738 |
|
| 739 |
scalePtr = (TkScale *) instanceData; |
| 740 |
|
| 741 |
gcValues.foreground = scalePtr->troughColorPtr->pixel; |
| 742 |
gc = Tk_GetGC(scalePtr->tkwin, GCForeground, &gcValues); |
| 743 |
if (scalePtr->troughGC != None) { |
| 744 |
Tk_FreeGC(scalePtr->display, scalePtr->troughGC); |
| 745 |
} |
| 746 |
scalePtr->troughGC = gc; |
| 747 |
|
| 748 |
gcValues.font = Tk_FontId(scalePtr->tkfont); |
| 749 |
gcValues.foreground = scalePtr->textColorPtr->pixel; |
| 750 |
gc = Tk_GetGC(scalePtr->tkwin, GCForeground | GCFont, &gcValues); |
| 751 |
if (scalePtr->textGC != None) { |
| 752 |
Tk_FreeGC(scalePtr->display, scalePtr->textGC); |
| 753 |
} |
| 754 |
scalePtr->textGC = gc; |
| 755 |
|
| 756 |
if (scalePtr->copyGC == None) { |
| 757 |
gcValues.graphics_exposures = False; |
| 758 |
scalePtr->copyGC = Tk_GetGC(scalePtr->tkwin, GCGraphicsExposures, |
| 759 |
&gcValues); |
| 760 |
} |
| 761 |
scalePtr->inset = scalePtr->highlightWidth + scalePtr->borderWidth; |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
ComputeScaleGeometry(scalePtr); |
| 769 |
|
| 770 |
TkEventuallyRedrawScale(scalePtr, REDRAW_ALL); |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
static void |
| 792 |
ComputeFormat(scalePtr) |
| 793 |
TkScale *scalePtr; |
| 794 |
{ |
| 795 |
double maxValue, x; |
| 796 |
int mostSigDigit, numDigits, leastSigDigit, afterDecimal; |
| 797 |
int eDigits, fDigits; |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
maxValue = fabs(scalePtr->fromValue); |
| 805 |
x = fabs(scalePtr->toValue); |
| 806 |
if (x > maxValue) { |
| 807 |
maxValue = x; |
| 808 |
} |
| 809 |
if (maxValue == 0) { |
| 810 |
maxValue = 1; |
| 811 |
} |
| 812 |
mostSigDigit = (int) floor(log10(maxValue)); |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
numDigits = scalePtr->digits; |
| 825 |
if (numDigits <= 0) { |
| 826 |
if (scalePtr->resolution > 0) { |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
leastSigDigit = (int) floor(log10(scalePtr->resolution)); |
| 832 |
} else { |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
x = fabs(scalePtr->fromValue - scalePtr->toValue); |
| 840 |
if (scalePtr->length > 0) { |
| 841 |
x /= scalePtr->length; |
| 842 |
} |
| 843 |
if (x > 0){ |
| 844 |
leastSigDigit = (int) floor(log10(x)); |
| 845 |
} else { |
| 846 |
leastSigDigit = 0; |
| 847 |
} |
| 848 |
} |
| 849 |
numDigits = mostSigDigit - leastSigDigit + 1; |
| 850 |
if (numDigits < 1) { |
| 851 |
numDigits = 1; |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
eDigits = numDigits + 4; |
| 861 |
if (numDigits > 1) { |
| 862 |
eDigits++; |
| 863 |
} |
| 864 |
afterDecimal = numDigits - mostSigDigit - 1; |
| 865 |
if (afterDecimal < 0) { |
| 866 |
afterDecimal = 0; |
| 867 |
} |
| 868 |
fDigits = (mostSigDigit >= 0) ? mostSigDigit + afterDecimal : afterDecimal; |
| 869 |
if (afterDecimal > 0) { |
| 870 |
fDigits++; |
| 871 |
} |
| 872 |
if (mostSigDigit < 0) { |
| 873 |
fDigits++; |
| 874 |
} |
| 875 |
if (fDigits <= eDigits) { |
| 876 |
sprintf(scalePtr->format, "%%.%df", afterDecimal); |
| 877 |
} else { |
| 878 |
sprintf(scalePtr->format, "%%.%de", numDigits-1); |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
static void |
| 902 |
ComputeScaleGeometry(scalePtr) |
| 903 |
register TkScale *scalePtr; |
| 904 |
{ |
| 905 |
char valueString[PRINT_CHARS]; |
| 906 |
int tmp, valuePixels, x, y, extraSpace; |
| 907 |
Tk_FontMetrics fm; |
| 908 |
|
| 909 |
Tk_GetFontMetrics(scalePtr->tkfont, &fm); |
| 910 |
scalePtr->fontHeight = fm.linespace + SPACING; |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
if (scalePtr->orient == ORIENT_HORIZONTAL) { |
| 919 |
y = scalePtr->inset; |
| 920 |
extraSpace = 0; |
| 921 |
if (scalePtr->labelLength != 0) { |
| 922 |
scalePtr->horizLabelY = y + SPACING; |
| 923 |
y += scalePtr->fontHeight; |
| 924 |
extraSpace = SPACING; |
| 925 |
} |
| 926 |
if (scalePtr->showValue) { |
| 927 |
scalePtr->horizValueY = y + SPACING; |
| 928 |
y += scalePtr->fontHeight; |
| 929 |
extraSpace = SPACING; |
| 930 |
} else { |
| 931 |
scalePtr->horizValueY = y; |
| 932 |
} |
| 933 |
y += extraSpace; |
| 934 |
scalePtr->horizTroughY = y; |
| 935 |
y += scalePtr->width + 2*scalePtr->borderWidth; |
| 936 |
if (scalePtr->tickInterval != 0) { |
| 937 |
scalePtr->horizTickY = y + SPACING; |
| 938 |
y += scalePtr->fontHeight + SPACING; |
| 939 |
} |
| 940 |
Tk_GeometryRequest(scalePtr->tkwin, |
| 941 |
scalePtr->length + 2*scalePtr->inset, y + scalePtr->inset); |
| 942 |
Tk_SetInternalBorder(scalePtr->tkwin, scalePtr->inset); |
| 943 |
return; |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
sprintf(valueString, scalePtr->format, scalePtr->fromValue); |
| 953 |
valuePixels = Tk_TextWidth(scalePtr->tkfont, valueString, -1); |
| 954 |
|
| 955 |
sprintf(valueString, scalePtr->format, scalePtr->toValue); |
| 956 |
tmp = Tk_TextWidth(scalePtr->tkfont, valueString, -1); |
| 957 |
if (valuePixels < tmp) { |
| 958 |
valuePixels = tmp; |
| 959 |
} |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
x = scalePtr->inset; |
| 967 |
if ((scalePtr->tickInterval != 0) && (scalePtr->showValue)) { |
| 968 |
scalePtr->vertTickRightX = x + SPACING + valuePixels; |
| 969 |
scalePtr->vertValueRightX = scalePtr->vertTickRightX + valuePixels |
| 970 |
+ fm.ascent/2; |
| 971 |
x = scalePtr->vertValueRightX + SPACING; |
| 972 |
} else if (scalePtr->tickInterval != 0) { |
| 973 |
scalePtr->vertTickRightX = x + SPACING + valuePixels; |
| 974 |
scalePtr->vertValueRightX = scalePtr->vertTickRightX; |
| 975 |
x = scalePtr->vertTickRightX + SPACING; |
| 976 |
} else if (scalePtr->showValue) { |
| 977 |
scalePtr->vertTickRightX = x; |
| 978 |
scalePtr->vertValueRightX = x + SPACING + valuePixels; |
| 979 |
x = scalePtr->vertValueRightX + SPACING; |
| 980 |
} else { |
| 981 |
scalePtr->vertTickRightX = x; |
| 982 |
scalePtr->vertValueRightX = x; |
| 983 |
} |
| 984 |
scalePtr->vertTroughX = x; |
| 985 |
x += 2*scalePtr->borderWidth + scalePtr->width; |
| 986 |
if (scalePtr->labelLength == 0) { |
| 987 |
scalePtr->vertLabelX = 0; |
| 988 |
} else { |
| 989 |
scalePtr->vertLabelX = x + fm.ascent/2; |
| 990 |
x = scalePtr->vertLabelX + fm.ascent/2 |
| 991 |
+ Tk_TextWidth(scalePtr->tkfont, scalePtr->label, |
| 992 |
scalePtr->labelLength); |
| 993 |
} |
| 994 |
Tk_GeometryRequest(scalePtr->tkwin, x + scalePtr->inset, |
| 995 |
scalePtr->length + 2*scalePtr->inset); |
| 996 |
Tk_SetInternalBorder(scalePtr->tkwin, scalePtr->inset); |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
static void |
| 1018 |
ScaleEventProc(clientData, eventPtr) |
| 1019 |
ClientData clientData; |
| 1020 |
XEvent *eventPtr; |
| 1021 |
{ |
| 1022 |
TkScale *scalePtr = (TkScale *) clientData; |
| 1023 |
|
| 1024 |
if ((eventPtr->type == Expose) && (eventPtr->xexpose.count == 0)) { |
| 1025 |
TkEventuallyRedrawScale(scalePtr, REDRAW_ALL); |
| 1026 |
} else if (eventPtr->type == DestroyNotify) { |
| 1027 |
DestroyScale((char *) clientData); |
| 1028 |
} else if (eventPtr->type == ConfigureNotify) { |
| 1029 |
ComputeScaleGeometry(scalePtr); |
| 1030 |
TkEventuallyRedrawScale(scalePtr, REDRAW_ALL); |
| 1031 |
} else if (eventPtr->type == FocusIn) { |
| 1032 |
if (eventPtr->xfocus.detail != NotifyInferior) { |
| 1033 |
scalePtr->flags |= GOT_FOCUS; |
| 1034 |
if (scalePtr->highlightWidth > 0) { |
| 1035 |
TkEventuallyRedrawScale(scalePtr, REDRAW_ALL); |
| 1036 |
} |
| 1037 |
} |
| 1038 |
} else if (eventPtr->type == FocusOut) { |
| 1039 |
if (eventPtr->xfocus.detail != NotifyInferior) { |
| 1040 |
scalePtr->flags &= ~GOT_FOCUS; |
| 1041 |
if (scalePtr->highlightWidth > 0) { |
| 1042 |
TkEventuallyRedrawScale(scalePtr, REDRAW_ALL); |
| 1043 |
} |
| 1044 |
} |
| 1045 |
} |
| 1046 |
} |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
static void |
| 1067 |
ScaleCmdDeletedProc(clientData) |
| 1068 |
ClientData clientData; |
| 1069 |
{ |
| 1070 |
TkScale *scalePtr = (TkScale *) clientData; |
| 1071 |
Tk_Window tkwin = scalePtr->tkwin; |
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
if (!(scalePtr->flags & SCALE_DELETED)) { |
| 1081 |
scalePtr->flags |= SCALE_DELETED; |
| 1082 |
Tk_DestroyWindow(tkwin); |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
void |
| 1106 |
TkEventuallyRedrawScale(scalePtr, what) |
| 1107 |
register TkScale *scalePtr; |
| 1108 |
int what; |
| 1109 |
|
| 1110 |
{ |
| 1111 |
if ((what == 0) || (scalePtr->tkwin == NULL) |
| 1112 |
|| !Tk_IsMapped(scalePtr->tkwin)) { |
| 1113 |
return; |
| 1114 |
} |
| 1115 |
if (!(scalePtr->flags & REDRAW_PENDING)) { |
| 1116 |
scalePtr->flags |= REDRAW_PENDING; |
| 1117 |
Tcl_DoWhenIdle(TkpDisplayScale, (ClientData) scalePtr); |
| 1118 |
} |
| 1119 |
scalePtr->flags |= what; |
| 1120 |
} |
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
double |
| 1140 |
TkRoundToResolution(scalePtr, value) |
| 1141 |
TkScale *scalePtr; |
| 1142 |
double value; |
| 1143 |
{ |
| 1144 |
double rem, new, tick; |
| 1145 |
|
| 1146 |
if (scalePtr->resolution <= 0) { |
| 1147 |
return value; |
| 1148 |
} |
| 1149 |
tick = floor(value/scalePtr->resolution); |
| 1150 |
new = scalePtr->resolution * tick; |
| 1151 |
rem = value - new; |
| 1152 |
if (rem < 0) { |
| 1153 |
if (rem <= -scalePtr->resolution/2) { |
| 1154 |
new = (tick - 1.0) * scalePtr->resolution; |
| 1155 |
} |
| 1156 |
} else { |
| 1157 |
if (rem >= scalePtr->resolution/2) { |
| 1158 |
new = (tick + 1.0) * scalePtr->resolution; |
| 1159 |
} |
| 1160 |
} |
| 1161 |
return new; |
| 1162 |
} |
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
static char * |
| 1185 |
ScaleVarProc(clientData, interp, name1, name2, flags) |
| 1186 |
ClientData clientData; |
| 1187 |
Tcl_Interp *interp; |
| 1188 |
Var name1; |
| 1189 |
CONST char *name2; |
| 1190 |
int flags; |
| 1191 |
{ |
| 1192 |
register TkScale *scalePtr = (TkScale *) clientData; |
| 1193 |
char *resultStr; |
| 1194 |
double value; |
| 1195 |
Tcl_Obj *valuePtr; |
| 1196 |
int result; |
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
if (flags & TCL_TRACE_UNSETS) { |
| 1204 |
if ((flags & TCL_TRACE_DESTROYED) && !(flags & TCL_INTERP_DESTROYED)) { |
| 1205 |
Lang_TraceVar(interp, scalePtr->varNamePtr, |
| 1206 |
TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS, |
| 1207 |
ScaleVarProc, clientData); |
| 1208 |
scalePtr->flags |= NEVER_SET; |
| 1209 |
TkScaleSetValue(scalePtr, scalePtr->value, 1, 0); |
| 1210 |
} |
| 1211 |
return (char *) NULL; |
| 1212 |
} |
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
if (scalePtr->flags & SETTING_VAR) { |
| 1221 |
return (char *) NULL; |
| 1222 |
} |
| 1223 |
resultStr = NULL; |
| 1224 |
valuePtr = Tcl_ObjGetVar2(interp, scalePtr->varNamePtr, NULL, |
| 1225 |
TCL_GLOBAL_ONLY); |
| 1226 |
result = Tcl_GetDoubleFromObj(interp, valuePtr, &value); |
| 1227 |
if (result != TCL_OK) { |
| 1228 |
resultStr = "can't assign non-numeric value to scale variable"; |
| 1229 |
ScaleSetVariable(scalePtr); |
| 1230 |
} else { |
| 1231 |
scalePtr->value = TkRoundToResolution(scalePtr, value); |
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
TkScaleSetValue(scalePtr, scalePtr->value, 1, 0); |
| 1242 |
} |
| 1243 |
TkEventuallyRedrawScale(scalePtr, REDRAW_SLIDER); |
| 1244 |
|
| 1245 |
return resultStr; |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
void |
| 1267 |
TkScaleSetValue(scalePtr, value, setVar, invokeCommand) |
| 1268 |
register TkScale *scalePtr; |
| 1269 |
double value; |
| 1270 |
|
| 1271 |
int setVar; |
| 1272 |
|
| 1273 |
int invokeCommand; |
| 1274 |
|
| 1275 |
{ |
| 1276 |
value = TkRoundToResolution(scalePtr, value); |
| 1277 |
if ((value < scalePtr->fromValue) |
| 1278 |
^ (scalePtr->toValue < scalePtr->fromValue)) { |
| 1279 |
value = scalePtr->fromValue; |
| 1280 |
} |
| 1281 |
if ((value > scalePtr->toValue) |
| 1282 |
^ (scalePtr->toValue < scalePtr->fromValue)) { |
| 1283 |
value = scalePtr->toValue; |
| 1284 |
} |
| 1285 |
if (scalePtr->flags & NEVER_SET) { |
| 1286 |
scalePtr->flags &= ~NEVER_SET; |
| 1287 |
} else if (scalePtr->value == value) { |
| 1288 |
return; |
| 1289 |
} |
| 1290 |
scalePtr->value = value; |
| 1291 |
if (invokeCommand) { |
| 1292 |
scalePtr->flags |= INVOKE_COMMAND; |
| 1293 |
} |
| 1294 |
TkEventuallyRedrawScale(scalePtr, REDRAW_SLIDER); |
| 1295 |
|
| 1296 |
if (setVar && scalePtr->varNamePtr) { |
| 1297 |
ScaleSetVariable(scalePtr); |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
static void |
| 1318 |
ScaleSetVariable(scalePtr) |
| 1319 |
register TkScale *scalePtr; |
| 1320 |
{ |
| 1321 |
if (scalePtr->varNamePtr != NULL) { |
| 1322 |
char string[PRINT_CHARS]; |
| 1323 |
sprintf(string, scalePtr->format, scalePtr->value); |
| 1324 |
scalePtr->flags |= SETTING_VAR; |
| 1325 |
Tcl_ObjSetVar2(scalePtr->interp, scalePtr->varNamePtr, NULL, |
| 1326 |
Tcl_NewStringObj(string, -1), TCL_GLOBAL_ONLY); |
| 1327 |
scalePtr->flags &= ~SETTING_VAR; |
| 1328 |
} |
| 1329 |
} |
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
double |
| 1351 |
TkScalePixelToValue(scalePtr, x, y) |
| 1352 |
register TkScale *scalePtr; |
| 1353 |
int x, y; |
| 1354 |
|
| 1355 |
{ |
| 1356 |
double value, pixelRange; |
| 1357 |
|
| 1358 |
if (scalePtr->orient == ORIENT_VERTICAL) { |
| 1359 |
pixelRange = Tk_Height(scalePtr->tkwin) - scalePtr->sliderLength |
| 1360 |
- 2*scalePtr->inset - 2*scalePtr->borderWidth; |
| 1361 |
value = y; |
| 1362 |
} else { |
| 1363 |
pixelRange = Tk_Width(scalePtr->tkwin) - scalePtr->sliderLength |
| 1364 |
- 2*scalePtr->inset - 2*scalePtr->borderWidth; |
| 1365 |
value = x; |
| 1366 |
} |
| 1367 |
|
| 1368 |
if (pixelRange <= 0) { |
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
return scalePtr->value; |
| 1375 |
} |
| 1376 |
value -= scalePtr->sliderLength/2 + scalePtr->inset |
| 1377 |
+ scalePtr->borderWidth; |
| 1378 |
value /= pixelRange; |
| 1379 |
if (value < 0) { |
| 1380 |
value = 0; |
| 1381 |
} |
| 1382 |
if (value > 1) { |
| 1383 |
value = 1; |
| 1384 |
} |
| 1385 |
value = scalePtr->fromValue + |
| 1386 |
value * (scalePtr->toValue - scalePtr->fromValue); |
| 1387 |
return TkRoundToResolution(scalePtr, value); |
| 1388 |
} |
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
int |
| 1411 |
TkScaleValueToPixel(scalePtr, value) |
| 1412 |
register TkScale *scalePtr; |
| 1413 |
double value; |
| 1414 |
{ |
| 1415 |
int y, pixelRange; |
| 1416 |
double valueRange; |
| 1417 |
|
| 1418 |
valueRange = scalePtr->toValue - scalePtr->fromValue; |
| 1419 |
pixelRange = ((scalePtr->orient == ORIENT_VERTICAL) |
| 1420 |
? Tk_Height(scalePtr->tkwin) : Tk_Width(scalePtr->tkwin)) |
| 1421 |
- scalePtr->sliderLength - 2*scalePtr->inset - 2*scalePtr->borderWidth; |
| 1422 |
if (valueRange == 0) { |
| 1423 |
y = 0; |
| 1424 |
} else { |
| 1425 |
y = (int) ((value - scalePtr->fromValue) * pixelRange |
| 1426 |
/ valueRange + 0.5); |
| 1427 |
if (y < 0) { |
| 1428 |
y = 0; |
| 1429 |
} else if (y > pixelRange) { |
| 1430 |
y = pixelRange; |
| 1431 |
} |
| 1432 |
} |
| 1433 |
y += scalePtr->sliderLength/2 + scalePtr->inset + scalePtr->borderWidth; |
| 1434 |
return y; |
| 1435 |
} |
| 1436 |
|
| 1437 |
|