[175] | 1 |
|
---|
| 2 | /*
|
---|
| 3 | * bltInit.c --
|
---|
| 4 | *
|
---|
| 5 | * This module initials the BLT toolkit, registering its commands
|
---|
| 6 | * with the Tcl/Tk interpreter.
|
---|
| 7 | *
|
---|
| 8 | * Copyright 1991-1998 Lucent Technologies, Inc.
|
---|
| 9 | *
|
---|
| 10 | * Permission to use, copy, modify, and distribute this software and
|
---|
| 11 | * its documentation for any purpose and without fee is hereby
|
---|
| 12 | * granted, provided that the above copyright notice appear in all
|
---|
| 13 | * copies and that both that the copyright notice and warranty
|
---|
| 14 | * disclaimer appear in supporting documentation, and that the names
|
---|
| 15 | * of Lucent Technologies any of their entities not be used in
|
---|
| 16 | * advertising or publicity pertaining to distribution of the software
|
---|
| 17 | * without specific, written prior permission.
|
---|
| 18 | *
|
---|
| 19 | * Lucent Technologies disclaims all warranties with regard to this
|
---|
| 20 | * software, including all implied warranties of merchantability and
|
---|
| 21 | * fitness. In no event shall Lucent Technologies be liable for any
|
---|
| 22 | * special, indirect or consequential damages or any damages
|
---|
| 23 | * whatsoever resulting from loss of use, data or profits, whether in
|
---|
| 24 | * an action of contract, negligence or other tortuous action, arising
|
---|
| 25 | * out of or in connection with the use or performance of this
|
---|
| 26 | * software.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include "bltInt.h"
|
---|
| 30 |
|
---|
| 31 | #define EXACT 0
|
---|
| 32 |
|
---|
| 33 | double bltNaN;
|
---|
| 34 | Tcl_Obj *bltEmptyStringObjPtr;
|
---|
| 35 |
|
---|
| 36 | static Tcl_MathProc MinMathProc, MaxMathProc;
|
---|
| 37 |
|
---|
| 38 | static Tcl_AppInitProc *bltCmds[] =
|
---|
| 39 | {
|
---|
| 40 | Blt_BusyInit,
|
---|
| 41 | Blt_VectorInit,
|
---|
| 42 | Blt_SplineInit,
|
---|
| 43 | Blt_Crc32Init,
|
---|
| 44 | Blt_GraphInit,
|
---|
| 45 | Blt_TableInit,
|
---|
| 46 | Blt_TabnotebookInit,
|
---|
| 47 | Blt_BitmapInit,
|
---|
| 48 | Blt_TreeInit,
|
---|
| 49 | Blt_TreeViewInit,
|
---|
| 50 | #ifndef NO_PRINTER
|
---|
| 51 | Blt_PrinterInit,
|
---|
| 52 | #endif
|
---|
| 53 | (Tcl_AppInitProc *) NULL
|
---|
| 54 | };
|
---|
| 55 |
|
---|
| 56 | #ifdef __BORLANDC__
|
---|
| 57 | static double
|
---|
| 58 | MakeNaN(void)
|
---|
| 59 | {
|
---|
| 60 | union Real {
|
---|
| 61 | struct DoubleWord {
|
---|
| 62 | int lo, hi;
|
---|
| 63 | } doubleWord;
|
---|
| 64 | double number;
|
---|
| 65 | } real;
|
---|
| 66 |
|
---|
| 67 | real.doubleWord.lo = real.doubleWord.hi = 0x7FFFFFFF;
|
---|
| 68 | return real.number;
|
---|
| 69 | }
|
---|
| 70 | #endif /* __BORLANDC__ */
|
---|
| 71 |
|
---|
| 72 | #ifdef _MSC_VER
|
---|
| 73 | static double
|
---|
| 74 | MakeNaN(void)
|
---|
| 75 | {
|
---|
| 76 | return sqrt(-1.0); /* Generate IEEE 754 Quiet Not-A-Number. */
|
---|
| 77 | }
|
---|
| 78 | #endif /* _MSC_VER */
|
---|
| 79 |
|
---|
| 80 | #if !defined(__BORLANDC__) && !defined(_MSC_VER)
|
---|
| 81 | static double
|
---|
| 82 | MakeNaN(void)
|
---|
| 83 | {
|
---|
| 84 | return 0.0 / 0.0; /* Generate IEEE 754 Not-A-Number. */
|
---|
| 85 | }
|
---|
| 86 | #endif /* !__BORLANDC__ && !_MSC_VER */
|
---|
| 87 |
|
---|
| 88 | /* ARGSUSED */
|
---|
| 89 | static int
|
---|
| 90 | MinMathProc(clientData, interp, argsPtr, resultPtr)
|
---|
| 91 | ClientData clientData; /* Not used. */
|
---|
| 92 | Tcl_Interp *interp;
|
---|
| 93 | Tcl_Value *argsPtr;
|
---|
| 94 | Tcl_Value *resultPtr;
|
---|
| 95 | {
|
---|
| 96 | Tcl_Value *op1Ptr, *op2Ptr;
|
---|
| 97 |
|
---|
| 98 | op1Ptr = argsPtr, op2Ptr = argsPtr + 1;
|
---|
| 99 | if ((op1Ptr->type == TCL_INT) && (op2Ptr->type == TCL_INT)) {
|
---|
| 100 | resultPtr->intValue = MIN(op1Ptr->intValue, op2Ptr->intValue);
|
---|
| 101 | resultPtr->type = TCL_INT;
|
---|
| 102 | } else {
|
---|
| 103 | double a, b;
|
---|
| 104 |
|
---|
| 105 | a = (op1Ptr->type == TCL_INT)
|
---|
| 106 | ? (double)op1Ptr->intValue : op1Ptr->doubleValue;
|
---|
| 107 | b = (op2Ptr->type == TCL_INT)
|
---|
| 108 | ? (double)op2Ptr->intValue : op2Ptr->doubleValue;
|
---|
| 109 | resultPtr->doubleValue = MIN(a, b);
|
---|
| 110 | resultPtr->type = TCL_DOUBLE;
|
---|
| 111 | }
|
---|
| 112 | return TCL_OK;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /*ARGSUSED*/
|
---|
| 116 | static int
|
---|
| 117 | MaxMathProc(clientData, interp, argsPtr, resultPtr)
|
---|
| 118 | ClientData clientData; /* Not Used. */
|
---|
| 119 | Tcl_Interp *interp;
|
---|
| 120 | Tcl_Value *argsPtr;
|
---|
| 121 | Tcl_Value *resultPtr;
|
---|
| 122 | {
|
---|
| 123 | Tcl_Value *op1Ptr, *op2Ptr;
|
---|
| 124 |
|
---|
| 125 | op1Ptr = argsPtr, op2Ptr = argsPtr + 1;
|
---|
| 126 | if ((op1Ptr->type == TCL_INT) && (op2Ptr->type == TCL_INT)) {
|
---|
| 127 | resultPtr->intValue = MAX(op1Ptr->intValue, op2Ptr->intValue);
|
---|
| 128 | resultPtr->type = TCL_INT;
|
---|
| 129 | } else {
|
---|
| 130 | double a, b;
|
---|
| 131 |
|
---|
| 132 | a = (op1Ptr->type == TCL_INT)
|
---|
| 133 | ? (double)op1Ptr->intValue : op1Ptr->doubleValue;
|
---|
| 134 | b = (op2Ptr->type == TCL_INT)
|
---|
| 135 | ? (double)op2Ptr->intValue : op2Ptr->doubleValue;
|
---|
| 136 | resultPtr->doubleValue = MAX(a, b);
|
---|
| 137 | resultPtr->type = TCL_DOUBLE;
|
---|
| 138 | }
|
---|
| 139 | return TCL_OK;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /*LINTLIBRARY*/
|
---|
| 143 | EXPORT int
|
---|
| 144 | Blt_Init(interp)
|
---|
| 145 | Tcl_Interp *interp; /* Interpreter to add extra commands */
|
---|
| 146 | {
|
---|
| 147 | Tcl_AppInitProc **p;
|
---|
| 148 | Tcl_Namespace *nsPtr;
|
---|
| 149 | Tcl_ValueType args[2];
|
---|
| 150 |
|
---|
| 151 | /*
|
---|
| 152 | * Check that the versions of Tcl that have been loaded are
|
---|
| 153 | * the same ones that BLT was compiled against.
|
---|
| 154 | */
|
---|
| 155 |
|
---|
| 156 | if (Tcl_PkgRequire(interp, "Tcl", TCL_VERSION, EXACT) == NULL) {
|
---|
| 157 | return TCL_ERROR;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | /* Set the "blt_version", "blt_patchLevel", and "blt_libPath" Tcl
|
---|
| 161 | * variables. We'll use them in the following script. */
|
---|
| 162 | if ((Tcl_SetVar(interp, "blt_version", BLT_VERSION,
|
---|
| 163 | TCL_GLOBAL_ONLY) == NULL) ||
|
---|
| 164 | (Tcl_SetVar(interp, "blt_patchLevel", BLT_PATCH_LEVEL,
|
---|
| 165 | TCL_GLOBAL_ONLY) == NULL)) {
|
---|
| 166 | return TCL_ERROR;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | nsPtr = Tcl_CreateNamespace(interp, "blt", NULL,
|
---|
| 170 | (Tcl_NamespaceDeleteProc *) NULL);
|
---|
| 171 | if (nsPtr == NULL) {
|
---|
| 172 | return TCL_ERROR;
|
---|
| 173 | }
|
---|
| 174 | /* Initialize the BLT commands that only require Tcl. */
|
---|
| 175 | for (p = bltCmds; *p != NULL; p++) {
|
---|
| 176 | if ((**p) (interp) != TCL_OK) {
|
---|
| 177 | Tcl_DeleteNamespace(nsPtr);
|
---|
| 178 | return TCL_ERROR;
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 | args[0] = args[1] = TCL_EITHER;
|
---|
| 182 | Tcl_CreateMathFunc(interp, "min", 2, args, MinMathProc, (ClientData)0);
|
---|
| 183 | Tcl_CreateMathFunc(interp, "max", 2, args, MaxMathProc, (ClientData)0);
|
---|
| 184 | Blt_RegisterArrayObj(interp);
|
---|
| 185 | bltEmptyStringObjPtr = Tcl_NewStringObj("", -1);
|
---|
| 186 | bltNaN = MakeNaN();
|
---|
| 187 |
|
---|
| 188 | return Tcl_PkgProvide(interp, "BLT", BLT_VERSION);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 | /*LINTLIBRARY*/
|
---|
| 193 | EXPORT int
|
---|
| 194 | Blt_SafeInit(interp)
|
---|
| 195 | Tcl_Interp *interp; /* Interpreter to add extra commands */
|
---|
| 196 | {
|
---|
| 197 | return Blt_Init(interp);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | /*
|
---|
| 201 | *----------------------------------------------------------------------
|
---|
| 202 | *
|
---|
| 203 | * Blt_InitCmd --
|
---|
| 204 | *
|
---|
| 205 | * Given the name of a command, return a pointer to the
|
---|
| 206 | * clientData field of the command.
|
---|
| 207 | *
|
---|
| 208 | * Results:
|
---|
| 209 | * A standard TCL result. If the command is found, TCL_OK
|
---|
| 210 | * is returned and clientDataPtr points to the clientData
|
---|
| 211 | * field of the command (if the clientDataPtr in not NULL).
|
---|
| 212 | *
|
---|
| 213 | * Side effects:
|
---|
| 214 | * If the command is found, clientDataPtr is set to the address
|
---|
| 215 | * of the clientData of the command. If not found, an error
|
---|
| 216 | * message is left in interp->result.
|
---|
| 217 | *
|
---|
| 218 | *----------------------------------------------------------------------
|
---|
| 219 | */
|
---|
| 220 |
|
---|
| 221 | /*ARGSUSED*/
|
---|
| 222 | Tcl_Command
|
---|
| 223 | Blt_InitCmd(interp, nsName, specPtr)
|
---|
| 224 | Tcl_Interp *interp;
|
---|
| 225 | char *nsName;
|
---|
| 226 | Blt_CmdSpec *specPtr;
|
---|
| 227 | {
|
---|
| 228 | char *cmdPath;
|
---|
| 229 | Tcl_DString dString;
|
---|
| 230 | Tcl_Command cmdToken;
|
---|
| 231 |
|
---|
| 232 | Tcl_DStringInit(&dString);
|
---|
| 233 |
|
---|
| 234 | if (nsName != NULL) {
|
---|
| 235 | Tcl_DStringAppend(&dString, nsName, -1);
|
---|
| 236 | }
|
---|
| 237 | Tcl_DStringAppend(&dString, "::", -1);
|
---|
| 238 |
|
---|
| 239 | Tcl_DStringAppend(&dString, specPtr->name, -1);
|
---|
| 240 |
|
---|
| 241 | cmdPath = Tcl_DStringValue(&dString);
|
---|
| 242 | cmdToken = Tcl_FindCommand(interp, cmdPath, (Tcl_Namespace *)NULL, 0);
|
---|
| 243 | if (cmdToken != NULL) {
|
---|
| 244 | Tcl_DStringFree(&dString);
|
---|
| 245 | return cmdToken; /* Assume command was already initialized */
|
---|
| 246 | }
|
---|
| 247 | cmdToken = Tcl_CreateCommand(interp, cmdPath, specPtr->cmdProc,
|
---|
| 248 | specPtr->clientData, specPtr->cmdDeleteProc);
|
---|
| 249 | Tcl_DStringFree(&dString);
|
---|
| 250 |
|
---|
| 251 | {
|
---|
| 252 | Tcl_Namespace *nsPtr;
|
---|
| 253 | int dontResetList = 0;
|
---|
| 254 |
|
---|
| 255 | nsPtr = Tcl_FindNamespace(interp, nsName, (Tcl_Namespace *)NULL,
|
---|
| 256 | TCL_LEAVE_ERR_MSG);
|
---|
| 257 | if (nsPtr == NULL) {
|
---|
| 258 | return NULL;
|
---|
| 259 | }
|
---|
| 260 | if (Tcl_Export(interp, nsPtr, specPtr->name, dontResetList) != TCL_OK) {
|
---|
| 261 | return NULL;
|
---|
| 262 | }
|
---|
| 263 | }
|
---|
| 264 | return cmdToken;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | /*
|
---|
| 268 | *----------------------------------------------------------------------
|
---|
| 269 | *
|
---|
| 270 | * Blt_InitObjCmd --
|
---|
| 271 | *
|
---|
| 272 | * Given the name of a command, return a pointer to the
|
---|
| 273 | * clientData field of the command.
|
---|
| 274 | *
|
---|
| 275 | * Results:
|
---|
| 276 | * A standard TCL result. If the command is found, TCL_OK
|
---|
| 277 | * is returned and clientDataPtr points to the clientData
|
---|
| 278 | * field of the command (if the clientDataPtr in not NULL).
|
---|
| 279 | *
|
---|
| 280 | * Side effects:
|
---|
| 281 | * If the command is found, clientDataPtr is set to the address
|
---|
| 282 | * of the clientData of the command. If not found, an error
|
---|
| 283 | * message is left in interp->result.
|
---|
| 284 | *
|
---|
| 285 | *----------------------------------------------------------------------
|
---|
| 286 | */
|
---|
| 287 | /*ARGSUSED*/
|
---|
| 288 | Tcl_Command
|
---|
| 289 | Blt_InitObjCmd(interp, nsName, specPtr)
|
---|
| 290 | Tcl_Interp *interp;
|
---|
| 291 | char *nsName;
|
---|
| 292 | Blt_ObjCmdSpec *specPtr;
|
---|
| 293 | {
|
---|
| 294 | char *cmdPath;
|
---|
| 295 | Tcl_DString dString;
|
---|
| 296 | Tcl_Command cmdToken;
|
---|
| 297 | Tcl_Namespace *nsPtr;
|
---|
| 298 |
|
---|
| 299 | Tcl_DStringInit(&dString);
|
---|
| 300 | if (nsName != NULL) {
|
---|
| 301 | Tcl_DStringAppend(&dString, nsName, -1);
|
---|
| 302 | }
|
---|
| 303 | Tcl_DStringAppend(&dString, "::", -1);
|
---|
| 304 | Tcl_DStringAppend(&dString, specPtr->name, -1);
|
---|
| 305 |
|
---|
| 306 | cmdPath = Tcl_DStringValue(&dString);
|
---|
| 307 | cmdToken = Tcl_FindCommand(interp, cmdPath, (Tcl_Namespace *)NULL, 0);
|
---|
| 308 | if (cmdToken != NULL) {
|
---|
| 309 | Tcl_DStringFree(&dString);
|
---|
| 310 | return cmdToken; /* Assume command was already initialized */
|
---|
| 311 | }
|
---|
| 312 | cmdToken = Tcl_CreateObjCommand(interp, cmdPath,
|
---|
| 313 | (Tcl_ObjCmdProc *)specPtr->cmdProc,
|
---|
| 314 | specPtr->clientData,
|
---|
| 315 | specPtr->cmdDeleteProc);
|
---|
| 316 | Tcl_DStringFree(&dString);
|
---|
| 317 |
|
---|
| 318 | nsPtr = Tcl_FindNamespace(interp, nsName, (Tcl_Namespace *)NULL,
|
---|
| 319 | TCL_LEAVE_ERR_MSG);
|
---|
| 320 | if (nsPtr == NULL) {
|
---|
| 321 | return NULL;
|
---|
| 322 | }
|
---|
| 323 | if (Tcl_Export(interp, nsPtr, specPtr->name, FALSE) != TCL_OK) {
|
---|
| 324 | return NULL;
|
---|
| 325 | }
|
---|
| 326 | return cmdToken;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | /*
|
---|
| 330 | *----------------------------------------------------------------------
|
---|
| 331 | *
|
---|
| 332 | * Blt_InitCmds --
|
---|
| 333 | *
|
---|
| 334 | * Given the name of a command, return a pointer to the
|
---|
| 335 | * clientData field of the command.
|
---|
| 336 | *
|
---|
| 337 | * Results:
|
---|
| 338 | * A standard TCL result. If the command is found, TCL_OK
|
---|
| 339 | * is returned and clientDataPtr points to the clientData
|
---|
| 340 | * field of the command (if the clientDataPtr in not NULL).
|
---|
| 341 | *
|
---|
| 342 | * Side effects:
|
---|
| 343 | * If the command is found, clientDataPtr is set to the address
|
---|
| 344 | * of the clientData of the command. If not found, an error
|
---|
| 345 | * message is left in interp->result.
|
---|
| 346 | *
|
---|
| 347 | *----------------------------------------------------------------------
|
---|
| 348 | */
|
---|
| 349 | int
|
---|
| 350 | Blt_InitCmds(interp, nsName, specPtr, nCmds)
|
---|
| 351 | Tcl_Interp *interp;
|
---|
| 352 | char *nsName;
|
---|
| 353 | Blt_CmdSpec *specPtr;
|
---|
| 354 | int nCmds;
|
---|
| 355 | {
|
---|
| 356 | Blt_CmdSpec *endPtr;
|
---|
| 357 |
|
---|
| 358 | for (endPtr = specPtr + nCmds; specPtr < endPtr; specPtr++) {
|
---|
| 359 | if (Blt_InitCmd(interp, nsName, specPtr) == NULL) {
|
---|
| 360 | return TCL_ERROR;
|
---|
| 361 | }
|
---|
| 362 | }
|
---|
| 363 | return TCL_OK;
|
---|
| 364 | }
|
---|