| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#include <imgInt.h> |
| 14 |
#include <ctype.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <string.h> |
| 17 |
#include <imgPmap.h> |
| 18 |
|
| 19 |
#if defined(__WIN32__) && !defined (__GNUC__) |
| 20 |
#define strncasecmp strnicmp |
| 21 |
#endif |
| 22 |
|
| 23 |
#ifndef MAC_TCL |
| 24 |
#include <sys/types.h> |
| 25 |
#include <sys/stat.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#ifndef TCL_STUB_MAGIC |
| 29 |
EXTERN void panic _ANSI_ARGS_(TCL_VARARGS(char *,format)); |
| 30 |
#endif |
| 31 |
|
| 32 |
#define UCHAR(c) ((unsigned char) (c)) |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
static int ImgXpmCreate _ANSI_ARGS_((Tcl_Interp *interp, |
| 39 |
char *name, int argc, Tcl_Obj *objv[], |
| 40 |
Tk_ImageType *typePtr, Tk_ImageMaster master, |
| 41 |
ClientData *clientDataPtr)); |
| 42 |
static ClientData ImgXpmGet _ANSI_ARGS_((Tk_Window tkwin, |
| 43 |
ClientData clientData)); |
| 44 |
static void ImgXpmDisplay _ANSI_ARGS_((ClientData clientData, |
| 45 |
Display *display, Drawable drawable, |
| 46 |
int imageX, int imageY, int width, int height, |
| 47 |
int drawableX, int drawableY)); |
| 48 |
static void ImgXpmFree _ANSI_ARGS_((ClientData clientData, |
| 49 |
Display *display)); |
| 50 |
static void ImgXpmDelete _ANSI_ARGS_((ClientData clientData)); |
| 51 |
static int ImgXpmCmd _ANSI_ARGS_((ClientData clientData, |
| 52 |
Tcl_Interp *interp, int argc, char **argv)); |
| 53 |
static void ImgXpmCmdDeletedProc _ANSI_ARGS_(( |
| 54 |
ClientData clientData)); |
| 55 |
static void ImgXpmConfigureInstance _ANSI_ARGS_(( |
| 56 |
PixmapInstance *instancePtr)); |
| 57 |
static int ImgXpmConfigureMaster _ANSI_ARGS_(( |
| 58 |
PixmapMaster *masterPtr, int argc, char **argv, |
| 59 |
int flags)); |
| 60 |
static int ImgXpmGetData _ANSI_ARGS_((Tcl_Interp *interp, |
| 61 |
PixmapMaster *masterPtr)); |
| 62 |
static char ** ImgXpmGetDataFromFile _ANSI_ARGS_((Tcl_Interp * interp, |
| 63 |
char * string, int * numLines_return)); |
| 64 |
static char ** ImgXpmGetDataFromString _ANSI_ARGS_((Tcl_Interp*interp, |
| 65 |
char * string, int * numLines_return)); |
| 66 |
static void ImgXpmGetPixmapFromData _ANSI_ARGS_(( |
| 67 |
Tcl_Interp * interp, |
| 68 |
PixmapMaster *masterPtr, |
| 69 |
PixmapInstance *instancePtr)); |
| 70 |
static char * GetType _ANSI_ARGS_((char * colorDefn, |
| 71 |
int * type_ret)); |
| 72 |
static char * GetColor _ANSI_ARGS_((char * colorDefn, |
| 73 |
char * colorName, int * type_ret)); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
static Tk_ConfigSpec configSpecs[] = { |
| 80 |
{TK_CONFIG_STRING, "-data", (char *) NULL, (char *) NULL, |
| 81 |
(char *) NULL, Tk_Offset(PixmapMaster, dataString), TK_CONFIG_NULL_OK}, |
| 82 |
{TK_CONFIG_STRING, "-file", (char *) NULL, (char *) NULL, |
| 83 |
(char *) NULL, Tk_Offset(PixmapMaster, fileString), TK_CONFIG_NULL_OK}, |
| 84 |
{TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL, |
| 85 |
(char *) NULL, 0, 0} |
| 86 |
}; |
| 87 |
|
| 88 |
Tk_ImageType imgPixmapImageType = { |
| 89 |
"pixmap", |
| 90 |
(Tk_ImageCreateProc *) ImgXpmCreate, |
| 91 |
ImgXpmGet, |
| 92 |
ImgXpmDisplay, |
| 93 |
ImgXpmFree, |
| 94 |
ImgXpmDelete, |
| 95 |
#ifdef TK_CONFIG_OBJS |
| 96 |
(Tk_ImagePostscriptProc *) NULL, |
| 97 |
#endif |
| 98 |
(Tk_ImageType *) NULL |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
static int |
| 118 |
ImgXpmCreate(interp, name, argc, objv, typePtr, master, clientDataPtr) |
| 119 |
Tcl_Interp *interp; |
| 120 |
|
| 121 |
char *name; |
| 122 |
int argc; |
| 123 |
Tcl_Obj *objv[]; |
| 124 |
|
| 125 |
Tk_ImageType *typePtr; |
| 126 |
Tk_ImageMaster master; |
| 127 |
|
| 128 |
ClientData *clientDataPtr; |
| 129 |
|
| 130 |
{ |
| 131 |
PixmapMaster *masterPtr; |
| 132 |
int i; |
| 133 |
#if 0 |
| 134 |
char *argvbuf[10]; |
| 135 |
char **args = argvbuf; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
if (argc > 10) { |
| 141 |
args = (char **) ckalloc(argc * sizeof(char *)); |
| 142 |
} |
| 143 |
for (i = 0; i < argc; i++) { |
| 144 |
args[i] = ImgGetStringFromObj(objv[i], NULL); |
| 145 |
} |
| 146 |
#endif |
| 147 |
|
| 148 |
masterPtr = (PixmapMaster *) ckalloc(sizeof(PixmapMaster)); |
| 149 |
masterPtr->tkMaster = master; |
| 150 |
masterPtr->interp = interp; |
| 151 |
masterPtr->imageCmd = Tcl_CreateCommand(interp, name, ImgXpmCmd, |
| 152 |
(ClientData) masterPtr, ImgXpmCmdDeletedProc); |
| 153 |
|
| 154 |
masterPtr->fileString = NULL; |
| 155 |
masterPtr->dataString = NULL; |
| 156 |
masterPtr->data = NULL; |
| 157 |
masterPtr->isDataAlloced = 0; |
| 158 |
masterPtr->instancePtr = NULL; |
| 159 |
|
| 160 |
if (ImgXpmConfigureMaster(masterPtr, argc, objv, 0) != TCL_OK) { |
| 161 |
ImgXpmDelete((ClientData) masterPtr); |
| 162 |
#if 0 |
| 163 |
if (args != argvbuf) { |
| 164 |
ckfree((char *) args); |
| 165 |
} |
| 166 |
#endif |
| 167 |
return TCL_ERROR; |
| 168 |
} |
| 169 |
*clientDataPtr = (ClientData) masterPtr; |
| 170 |
#if 0 |
| 171 |
if (args != argvbuf) { |
| 172 |
ckfree((char *) args); |
| 173 |
} |
| 174 |
#endif |
| 175 |
return TCL_OK; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
static int |
| 201 |
ImgXpmConfigureMaster(masterPtr, argc, argv, flags) |
| 202 |
PixmapMaster *masterPtr; |
| 203 |
|
| 204 |
int argc; |
| 205 |
char **argv; |
| 206 |
int flags; |
| 207 |
|
| 208 |
{ |
| 209 |
PixmapInstance *instancePtr; |
| 210 |
char * oldData, * oldFile; |
| 211 |
|
| 212 |
oldData = masterPtr->dataString; |
| 213 |
oldFile = masterPtr->fileString; |
| 214 |
|
| 215 |
if (Tk_ConfigureWidget(masterPtr->interp, Tk_MainWindow(masterPtr->interp), |
| 216 |
configSpecs, argc, argv, (char *) masterPtr, flags) |
| 217 |
!= TCL_OK) { |
| 218 |
return TCL_ERROR; |
| 219 |
} |
| 220 |
|
| 221 |
if (masterPtr->dataString != NULL || |
| 222 |
masterPtr->fileString != NULL) { |
| 223 |
if (ImgXpmGetData(masterPtr->interp, masterPtr) != TCL_OK) { |
| 224 |
goto error; |
| 225 |
} |
| 226 |
} else { |
| 227 |
Tcl_AppendResult(masterPtr->interp, |
| 228 |
"must specify one of -data or -file", NULL); |
| 229 |
goto error; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
for (instancePtr = masterPtr->instancePtr; instancePtr != NULL; |
| 238 |
instancePtr = instancePtr->nextPtr) { |
| 239 |
ImgXpmConfigureInstance(instancePtr); |
| 240 |
} |
| 241 |
|
| 242 |
if (masterPtr->data) { |
| 243 |
Tk_ImageChanged(masterPtr->tkMaster, 0, 0, |
| 244 |
masterPtr->size[0], masterPtr->size[1], |
| 245 |
masterPtr->size[0], masterPtr->size[1]); |
| 246 |
} else { |
| 247 |
Tk_ImageChanged(masterPtr->tkMaster, 0, 0, 0, 0, 0, 0); |
| 248 |
} |
| 249 |
|
| 250 |
return TCL_OK; |
| 251 |
|
| 252 |
error: |
| 253 |
|
| 254 |
if (masterPtr->dataString && masterPtr->dataString != oldData) { |
| 255 |
ckfree(masterPtr->dataString); |
| 256 |
} |
| 257 |
if (masterPtr->fileString && masterPtr->fileString != oldFile) { |
| 258 |
ckfree(masterPtr->fileString); |
| 259 |
} |
| 260 |
masterPtr->dataString = oldData; |
| 261 |
masterPtr->fileString = oldFile; |
| 262 |
return TCL_ERROR; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
static int |
| 284 |
ImgXpmGetData(interp, masterPtr) |
| 285 |
Tcl_Interp *interp; |
| 286 |
PixmapMaster *masterPtr; |
| 287 |
{ |
| 288 |
char ** data = NULL; |
| 289 |
int isAllocated = 0; |
| 290 |
int listArgc; |
| 291 |
char ** listArgv = NULL; |
| 292 |
int numLines; |
| 293 |
int size[2]; |
| 294 |
int cpp; |
| 295 |
int ncolors; |
| 296 |
int code = TCL_OK; |
| 297 |
|
| 298 |
if (masterPtr->fileString != NULL) { |
| 299 |
if (Tcl_IsSafe(interp)) { |
| 300 |
Tcl_AppendResult(interp, "can't get image from a file in a", |
| 301 |
" safe interpreter", (char *) NULL); |
| 302 |
return TCL_ERROR; |
| 303 |
} |
| 304 |
data = ImgXpmGetDataFromFile(interp, masterPtr->fileString, &numLines); |
| 305 |
isAllocated = 1; |
| 306 |
} |
| 307 |
else if (masterPtr->dataString != NULL) { |
| 308 |
data = ImgXpmGetDataFromString(interp,masterPtr->dataString,&numLines); |
| 309 |
isAllocated = 1; |
| 310 |
} |
| 311 |
else { |
| 312 |
|
| 313 |
panic("ImgXpmGetData(): -data and -file are all NULL"); |
| 314 |
} |
| 315 |
|
| 316 |
if (data == NULL) { |
| 317 |
|
| 318 |
return TCL_ERROR; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
if (Tcl_SplitList(interp, data[0], &listArgc, &listArgv) != TCL_OK) { |
| 323 |
code = TCL_ERROR; goto done; |
| 324 |
} |
| 325 |
|
| 326 |
if (listArgc < 4) { |
| 327 |
code = TCL_ERROR; goto done; |
| 328 |
} |
| 329 |
|
| 330 |
if (Tcl_GetInt(interp, listArgv[0], &size[0]) != TCL_OK) { |
| 331 |
code = TCL_ERROR; goto done; |
| 332 |
} |
| 333 |
if (Tcl_GetInt(interp, listArgv[1], &size[1]) != TCL_OK) { |
| 334 |
code = TCL_ERROR; goto done; |
| 335 |
} |
| 336 |
if (Tcl_GetInt(interp, listArgv[2], &ncolors) != TCL_OK) { |
| 337 |
code = TCL_ERROR; goto done; |
| 338 |
} |
| 339 |
if (Tcl_GetInt(interp, listArgv[3], &cpp) != TCL_OK) { |
| 340 |
code = TCL_ERROR; goto done; |
| 341 |
} |
| 342 |
|
| 343 |
if (isAllocated) { |
| 344 |
if (numLines != size[1] + ncolors + 1) { |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
code = TCL_ERROR; goto done; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
done: |
| 353 |
if (code == TCL_OK) { |
| 354 |
if (masterPtr->isDataAlloced && masterPtr->data) { |
| 355 |
ckfree((char*)masterPtr->data); |
| 356 |
} |
| 357 |
masterPtr->isDataAlloced = isAllocated; |
| 358 |
masterPtr->data = data; |
| 359 |
masterPtr->size[0] = size[0]; |
| 360 |
masterPtr->size[1] = size[1]; |
| 361 |
masterPtr->cpp = cpp; |
| 362 |
masterPtr->ncolors = ncolors; |
| 363 |
} else { |
| 364 |
if (isAllocated && data) { |
| 365 |
ckfree((char*)data); |
| 366 |
} |
| 367 |
|
| 368 |
Tcl_ResetResult(interp); |
| 369 |
Tcl_AppendResult(interp, "File format error", NULL); |
| 370 |
} |
| 371 |
|
| 372 |
if (listArgv) { |
| 373 |
ckfree((char*)listArgv); |
| 374 |
} |
| 375 |
|
| 376 |
return code; |
| 377 |
} |
| 378 |
|
| 379 |
static char ** ImgXpmGetDataFromString(interp, string, numLines_return) |
| 380 |
Tcl_Interp * interp; |
| 381 |
char * string; |
| 382 |
int * numLines_return; |
| 383 |
{ |
| 384 |
int quoted; |
| 385 |
char * p, * list; |
| 386 |
int numLines; |
| 387 |
char ** data; |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
while (isspace(UCHAR(*string))) { |
| 395 |
++ string; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
if (strncmp("/* XPM", string, 6) != 0) { |
| 400 |
goto error; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
for (quoted = 0, p=string; *p;) { |
| 405 |
if (!quoted) { |
| 406 |
if (*p == '"') { |
| 407 |
quoted = 1; |
| 408 |
++ p; |
| 409 |
continue; |
| 410 |
} |
| 411 |
|
| 412 |
if (*p == '/' && *(p+1) == '*') { |
| 413 |
*p++ = ' '; |
| 414 |
*p++ = ' '; |
| 415 |
while (1) { |
| 416 |
if (*p == 0) { |
| 417 |
break; |
| 418 |
} |
| 419 |
if (*p == '*' && *(p+1) == '/') { |
| 420 |
*p++ = ' '; |
| 421 |
*p++ = ' '; |
| 422 |
break; |
| 423 |
} |
| 424 |
*p++ = ' '; |
| 425 |
} |
| 426 |
continue; |
| 427 |
} |
| 428 |
++ p; |
| 429 |
} else { |
| 430 |
if (*p == '"') { |
| 431 |
quoted = 0; |
| 432 |
} |
| 433 |
++ p; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
for (p=string; *p;) { |
| 439 |
if (*p != '{') { |
| 440 |
++ p; |
| 441 |
} else { |
| 442 |
++p; |
| 443 |
break; |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
quoted = 0; |
| 449 |
list = p; |
| 450 |
|
| 451 |
while (*p) { |
| 452 |
if (!quoted) { |
| 453 |
if (*p == '"') { |
| 454 |
quoted = 1; |
| 455 |
++ p; |
| 456 |
continue; |
| 457 |
} |
| 458 |
|
| 459 |
if (isspace(UCHAR(*p))) { |
| 460 |
*p = ' '; |
| 461 |
} |
| 462 |
else if (*p == ',') { |
| 463 |
*p = ' '; |
| 464 |
} |
| 465 |
else if (*p == '}') { |
| 466 |
*p = 0; |
| 467 |
break; |
| 468 |
} |
| 469 |
++p; |
| 470 |
} |
| 471 |
else { |
| 472 |
if (*p == '"') { |
| 473 |
quoted = 0; |
| 474 |
} |
| 475 |
++ p; |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
if (Tcl_SplitList(interp, list, &numLines, &data) != TCL_OK) { |
| 490 |
goto error; |
| 491 |
} else { |
| 492 |
if (numLines == 0) { |
| 493 |
|
| 494 |
if (data != NULL) { |
| 495 |
ckfree((char*)data); |
| 496 |
goto error; |
| 497 |
} |
| 498 |
} |
| 499 |
* numLines_return = numLines; |
| 500 |
return data; |
| 501 |
} |
| 502 |
|
| 503 |
error: |
| 504 |
Tcl_AppendResult(interp, "File format error", NULL); |
| 505 |
return (char**) NULL; |
| 506 |
} |
| 507 |
|
| 508 |
static char ** ImgXpmGetDataFromFile(interp, fileName, numLines_return) |
| 509 |
Tcl_Interp * interp; |
| 510 |
char * fileName; |
| 511 |
int * numLines_return; |
| 512 |
{ |
| 513 |
Tcl_Channel chan; |
| 514 |
int size; |
| 515 |
char ** data = (char **) NULL; |
| 516 |
char *cmdBuffer = NULL; |
| 517 |
|
| 518 |
chan = ImgOpenFileChannel(interp, fileName, 0); |
| 519 |
if (!chan) { |
| 520 |
return (char **) NULL; |
| 521 |
} |
| 522 |
|
| 523 |
size = Tcl_Seek(chan, 0, SEEK_END); |
| 524 |
if (size > 0) { |
| 525 |
Tcl_Seek(chan, 0, SEEK_SET); |
| 526 |
cmdBuffer = (char *) ckalloc(size+1); |
| 527 |
size = Tcl_Read(chan, cmdBuffer, size); |
| 528 |
} |
| 529 |
if (Tcl_Close(interp, chan) != TCL_OK) { |
| 530 |
goto error; |
| 531 |
} |
| 532 |
if (size < 0) { |
| 533 |
Tcl_AppendResult(interp, fileName, ": ", |
| 534 |
Tcl_PosixError(interp), (char *)NULL); |
| 535 |
goto error; |
| 536 |
} |
| 537 |
cmdBuffer[size] = 0; |
| 538 |
|
| 539 |
data = ImgXpmGetDataFromString(interp, cmdBuffer, numLines_return); |
| 540 |
error: |
| 541 |
if (cmdBuffer) { |
| 542 |
ckfree(cmdBuffer); |
| 543 |
} |
| 544 |
return data; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
static char * |
| 549 |
GetType(colorDefn, type_ret) |
| 550 |
char * colorDefn; |
| 551 |
int * type_ret; |
| 552 |
{ |
| 553 |
char * p = colorDefn; |
| 554 |
|
| 555 |
|
| 556 |
while (*p && isspace(UCHAR(*p))) { |
| 557 |
p ++; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
if (p[0] != '\0' && p[0] == 'm' && |
| 562 |
p[1] != '\0' && isspace(UCHAR(p[1]))) { |
| 563 |
*type_ret = XPM_MONO; |
| 564 |
p += 2; |
| 565 |
} |
| 566 |
else if (p[0] != '\0' && p[0] == 'g' && |
| 567 |
p[1] != '\0' && p[1] == '4' && |
| 568 |
p[2] != '\0' && isspace(UCHAR(p[2]))) { |
| 569 |
*type_ret = XPM_GRAY_4; |
| 570 |
p += 3; |
| 571 |
} |
| 572 |
else if (p[0] != '\0' && p[0] == 'g' && |
| 573 |
p[1] != '\0' && isspace(UCHAR(p[1]))) { |
| 574 |
*type_ret = XPM_GRAY; |
| 575 |
p += 2; |
| 576 |
} |
| 577 |
else if (p[0] != '\0' && p[0] == 'c' && |
| 578 |
p[1] != '\0' && isspace(UCHAR(p[1]))) { |
| 579 |
*type_ret = XPM_COLOR; |
| 580 |
p += 2; |
| 581 |
} |
| 582 |
else if (p[0] != '\0' && p[0] == 's' && |
| 583 |
p[1] != '\0' && isspace(UCHAR(p[1]))) { |
| 584 |
*type_ret = XPM_SYMBOLIC; |
| 585 |
p += 2; |
| 586 |
} |
| 587 |
else { |
| 588 |
*type_ret = XPM_UNKNOWN; |
| 589 |
return NULL; |
| 590 |
} |
| 591 |
|
| 592 |
return p; |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
static char * |
| 599 |
GetColor(colorDefn, colorName, type_ret) |
| 600 |
char * colorDefn; |
| 601 |
char * colorName; |
| 602 |
int * type_ret; |
| 603 |
{ |
| 604 |
int type; |
| 605 |
char * p; |
| 606 |
|
| 607 |
if (!colorDefn) { |
| 608 |
return NULL; |
| 609 |
} |
| 610 |
|
| 611 |
if ((colorDefn = GetType(colorDefn, &type)) == NULL) { |
| 612 |
|
| 613 |
return NULL; |
| 614 |
} |
| 615 |
else { |
| 616 |
*type_ret = type; |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
while (*colorDefn && isspace(UCHAR(*colorDefn))) { |
| 621 |
colorDefn ++; |
| 622 |
} |
| 623 |
|
| 624 |
p = colorName; |
| 625 |
|
| 626 |
while (1) { |
| 627 |
int dummy; |
| 628 |
|
| 629 |
while (*colorDefn && !isspace(UCHAR(*colorDefn))) { |
| 630 |
*p++ = *colorDefn++; |
| 631 |
} |
| 632 |
|
| 633 |
if (!*colorDefn) { |
| 634 |
break; |
| 635 |
} |
| 636 |
|
| 637 |
if (GetType(colorDefn, &dummy) == NULL) { |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
while (*colorDefn && isspace(UCHAR(*colorDefn))) { |
| 642 |
*p++ = *colorDefn++; |
| 643 |
} |
| 644 |
} else { |
| 645 |
break; |
| 646 |
} |
| 647 |
if (!*colorDefn) { |
| 648 |
break; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
*p = '\0'; |
| 654 |
|
| 655 |
return colorDefn; |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
static void |
| 665 |
ImgXpmGetPixmapFromData(interp, masterPtr, instancePtr) |
| 666 |
Tcl_Interp * interp; |
| 667 |
PixmapMaster *masterPtr; |
| 668 |
PixmapInstance *instancePtr; |
| 669 |
{ |
| 670 |
XImage * image = NULL, * mask = NULL; |
| 671 |
int depth, i, j, k, lOffset, isTransp = 0, isMono; |
| 672 |
ColorStruct * colors; |
| 673 |
|
| 674 |
depth = Tk_Depth(instancePtr->tkwin); |
| 675 |
|
| 676 |
switch ((Tk_Visual(instancePtr->tkwin))->class) { |
| 677 |
case StaticGray: |
| 678 |
case GrayScale: |
| 679 |
isMono = 1; |
| 680 |
break; |
| 681 |
default: |
| 682 |
isMono = 0; |
| 683 |
} |
| 684 |
|
| 685 |
ImgXpmAllocTmpBuffer(masterPtr, instancePtr, &image, &mask); |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
lOffset = 1; |
| 691 |
colors = (ColorStruct*)ckalloc(sizeof(ColorStruct)*masterPtr->ncolors); |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
for (i=0; i<masterPtr->ncolors; i++) { |
| 697 |
colors[i].colorPtr = NULL; |
| 698 |
if (masterPtr->cpp == 1) { |
| 699 |
colors[i].c = 0; |
| 700 |
} else { |
| 701 |
colors[i].cstring = (char*)ckalloc(masterPtr->cpp); |
| 702 |
colors[i].cstring[0] = 0; |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
for (i=0; i<masterPtr->ncolors; i++) { |
| 707 |
char * colorDefn; |
| 708 |
char * colorName; |
| 709 |
|
| 710 |
char * useName; |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
int found; |
| 716 |
|
| 717 |
colorDefn = masterPtr->data[i+lOffset]+masterPtr->cpp; |
| 718 |
colorName = (char*)ckalloc(strlen(colorDefn)); |
| 719 |
useName = (char*)ckalloc(strlen(colorDefn)); |
| 720 |
found = 0; |
| 721 |
|
| 722 |
while (colorDefn && *colorDefn) { |
| 723 |
int type; |
| 724 |
|
| 725 |
if ((colorDefn=GetColor(colorDefn, colorName, &type)) == NULL) { |
| 726 |
break; |
| 727 |
} |
| 728 |
if (colorName[0] == '\0') { |
| 729 |
continue; |
| 730 |
} |
| 731 |
|
| 732 |
switch (type) { |
| 733 |
case XPM_MONO: |
| 734 |
if (isMono && depth == 1) { |
| 735 |
strcpy(useName, colorName); |
| 736 |
found = 1; goto gotcolor; |
| 737 |
} |
| 738 |
break; |
| 739 |
case XPM_GRAY_4: |
| 740 |
if (isMono && depth == 4) { |
| 741 |
strcpy(useName, colorName); |
| 742 |
found = 1; goto gotcolor; |
| 743 |
} |
| 744 |
break; |
| 745 |
case XPM_GRAY: |
| 746 |
if (isMono && depth > 4) { |
| 747 |
strcpy(useName, colorName); |
| 748 |
found = 1; goto gotcolor; |
| 749 |
} |
| 750 |
break; |
| 751 |
case XPM_COLOR: |
| 752 |
if (!isMono) { |
| 753 |
strcpy(useName, colorName); |
| 754 |
found = 1; goto gotcolor; |
| 755 |
} |
| 756 |
break; |
| 757 |
} |
| 758 |
if (type != XPM_SYMBOLIC && type != XPM_UNKNOWN) { |
| 759 |
if (!found) { |
| 760 |
strcpy(useName, colorName); |
| 761 |
found = 1; |
| 762 |
} |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
gotcolor: |
| 767 |
if (masterPtr->cpp == 1) { |
| 768 |
colors[i].c = masterPtr->data[i+lOffset][0]; |
| 769 |
} else { |
| 770 |
strncpy(colors[i].cstring, masterPtr->data[i+lOffset], |
| 771 |
(size_t)masterPtr->cpp); |
| 772 |
} |
| 773 |
|
| 774 |
if (found) { |
| 775 |
if (strncasecmp(useName, "none", 5) != 0) { |
| 776 |
colors[i].colorPtr = Tk_GetColor(interp, |
| 777 |
instancePtr->tkwin, Tk_GetUid(useName)); |
| 778 |
if (colors[i].colorPtr == NULL) { |
| 779 |
colors[i].colorPtr = Tk_GetColor(interp, |
| 780 |
instancePtr->tkwin, Tk_GetUid("black")); |
| 781 |
} |
| 782 |
} |
| 783 |
} else { |
| 784 |
colors[i].colorPtr = Tk_GetColor(interp, |
| 785 |
instancePtr->tkwin, Tk_GetUid("black")); |
| 786 |
} |
| 787 |
|
| 788 |
ckfree(colorName); |
| 789 |
ckfree(useName); |
| 790 |
} |
| 791 |
|
| 792 |
lOffset += masterPtr->ncolors; |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
for (i=0; i<masterPtr->size[1]; i++) { |
| 798 |
char * p = masterPtr->data[i+lOffset]; |
| 799 |
|
| 800 |
for (j=0; j<masterPtr->size[0]; j++) { |
| 801 |
if (masterPtr->cpp == 1) { |
| 802 |
for (k=0; k<masterPtr->ncolors; k++) { |
| 803 |
if (*p == colors[k].c) { |
| 804 |
ImgXpmSetPixel(instancePtr, image, mask, j, i, |
| 805 |
colors[k].colorPtr, &isTransp); |
| 806 |
break; |
| 807 |
} |
| 808 |
} |
| 809 |
if (*p) { |
| 810 |
p++; |
| 811 |
} |
| 812 |
} else { |
| 813 |
for (k=0; k<masterPtr->ncolors; k++) { |
| 814 |
if (strncmp(p, colors[k].cstring, |
| 815 |
(size_t)masterPtr->cpp) == 0) { |
| 816 |
ImgXpmSetPixel(instancePtr, image, mask, j, i, |
| 817 |
colors[k].colorPtr, &isTransp); |
| 818 |
break; |
| 819 |
} |
| 820 |
} |
| 821 |
for (k=0; *p && k<masterPtr->cpp; k++) { |
| 822 |
p++; |
| 823 |
} |
| 824 |
} |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
instancePtr->colors = colors; |
| 829 |
|
| 830 |
ImgXpmRealizePixmap(masterPtr, instancePtr, image, mask, isTransp); |
| 831 |
ImgXpmFreeTmpBuffer(masterPtr, instancePtr, image, mask); |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
static void |
| 854 |
ImgXpmConfigureInstance(instancePtr) |
| 855 |
PixmapInstance *instancePtr; |
| 856 |
{ |
| 857 |
PixmapMaster *masterPtr = instancePtr->masterPtr; |
| 858 |
|
| 859 |
if (instancePtr->pixmap != None) { |
| 860 |
Tk_FreePixmap(Tk_Display(instancePtr->tkwin), instancePtr->pixmap); |
| 861 |
} |
| 862 |
ImgXpmFreeInstanceData(instancePtr, 0); |
| 863 |
|
| 864 |
if (instancePtr->colors != NULL) { |
| 865 |
int i; |
| 866 |
for (i=0; i<masterPtr->ncolors; i++) { |
| 867 |
if (instancePtr->colors[i].colorPtr != NULL) { |
| 868 |
Tk_FreeColor(instancePtr->colors[i].colorPtr); |
| 869 |
} |
| 870 |
if (masterPtr->cpp != 1) { |
| 871 |
ckfree(instancePtr->colors[i].cstring); |
| 872 |
} |
| 873 |
} |
| 874 |
ckfree((char*)instancePtr->colors); |
| 875 |
} |
| 876 |
|
| 877 |
if (Tk_WindowId(instancePtr->tkwin) == None) { |
| 878 |
Tk_MakeWindowExist(instancePtr->tkwin); |
| 879 |
} |
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
ImgXpmGetPixmapFromData(masterPtr->interp, masterPtr, instancePtr); |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
static int |
| 909 |
ImgXpmCmd(clientData, interp, argc, argv) |
| 910 |
ClientData clientData; |
| 911 |
Tcl_Interp *interp; |
| 912 |
int argc; |
| 913 |
char **argv; |
| 914 |
{ |
| 915 |
PixmapMaster *masterPtr = (PixmapMaster *) clientData; |
| 916 |
int c, code; |
| 917 |
size_t length; |
| 918 |
|
| 919 |
if (argc < 2) { |
| 920 |
Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 921 |
argv[0], " option ?arg arg ...?\"", |
| 922 |
(char *) NULL); |
| 923 |
return TCL_ERROR; |
| 924 |
} |
| 925 |
c = argv[1][0]; |
| 926 |
length = strlen(argv[1]); |
| 927 |
|
| 928 |
if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0) |
| 929 |
&& (length >= 2)) { |
| 930 |
if (argc != 3) { |
| 931 |
Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 932 |
argv[0], " cget option\"", |
| 933 |
(char *) NULL); |
| 934 |
return TCL_ERROR; |
| 935 |
} |
| 936 |
return Tk_ConfigureValue(interp, Tk_MainWindow(interp), configSpecs, |
| 937 |
(char *) masterPtr, argv[2], 0); |
| 938 |
} else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0) |
| 939 |
&& (length >= 2)) { |
| 940 |
if (argc == 2) { |
| 941 |
code = Tk_ConfigureInfo(interp, Tk_MainWindow(interp), |
| 942 |
configSpecs, (char *) masterPtr, (char *) NULL, 0); |
| 943 |
} else if (argc == 3) { |
| 944 |
code = Tk_ConfigureInfo(interp, Tk_MainWindow(interp), |
| 945 |
configSpecs, (char *) masterPtr, argv[2], 0); |
| 946 |
} else { |
| 947 |
code = ImgXpmConfigureMaster(masterPtr, argc-2, argv+2, |
| 948 |
TK_CONFIG_ARGV_ONLY); |
| 949 |
} |
| 950 |
return code; |
| 951 |
} else if ((c == 'r') && (strncmp(argv[1], "refcount", length) == 0)) { |
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
PixmapInstance *instancePtr; |
| 956 |
int count = 0; |
| 957 |
char buff[30]; |
| 958 |
|
| 959 |
if (argc != 1) { |
| 960 |
Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 961 |
argv[0], "\"", (char *) NULL); |
| 962 |
return TCL_ERROR; |
| 963 |
} |
| 964 |
for (instancePtr=masterPtr->instancePtr; instancePtr; |
| 965 |
instancePtr = instancePtr->nextPtr) { |
| 966 |
count += instancePtr->refCount; |
| 967 |
} |
| 968 |
sprintf(buff, "%d", count); |
| 969 |
Tcl_AppendResult(interp, buff, (char *) NULL); |
| 970 |
return TCL_OK; |
| 971 |
} else { |
| 972 |
Tcl_AppendResult(interp, "bad option \"", argv[1], |
| 973 |
"\": must be cget, configure or refcount", (char *) NULL); |
| 974 |
return TCL_ERROR; |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
static ClientData |
| 998 |
ImgXpmGet(tkwin, masterData) |
| 999 |
Tk_Window tkwin; |
| 1000 |
|
| 1001 |
ClientData masterData; |
| 1002 |
|
| 1003 |
{ |
| 1004 |
PixmapMaster *masterPtr = (PixmapMaster *) masterData; |
| 1005 |
PixmapInstance *instancePtr; |
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
for (instancePtr = masterPtr->instancePtr; instancePtr != NULL; |
| 1013 |
instancePtr = instancePtr->nextPtr) { |
| 1014 |
if (instancePtr->tkwin == tkwin) { |
| 1015 |
instancePtr->refCount++; |
| 1016 |
return (ClientData) instancePtr; |
| 1017 |
} |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
instancePtr = (PixmapInstance *) ckalloc(sizeof(PixmapInstance)); |
| 1025 |
instancePtr->refCount = 1; |
| 1026 |
instancePtr->masterPtr = masterPtr; |
| 1027 |
instancePtr->tkwin = tkwin; |
| 1028 |
instancePtr->pixmap = None; |
| 1029 |
instancePtr->nextPtr = masterPtr->instancePtr; |
| 1030 |
instancePtr->colors = NULL; |
| 1031 |
masterPtr->instancePtr = instancePtr; |
| 1032 |
|
| 1033 |
ImgInitPixmapInstance(masterPtr, instancePtr); |
| 1034 |
ImgXpmConfigureInstance(instancePtr); |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
if (instancePtr->nextPtr == NULL) { |
| 1040 |
if (masterPtr->data) { |
| 1041 |
Tk_ImageChanged(masterPtr->tkMaster, 0, 0, |
| 1042 |
masterPtr->size[0], masterPtr->size[1], |
| 1043 |
masterPtr->size[0], masterPtr->size[1]); |
| 1044 |
} else { |
| 1045 |
Tk_ImageChanged(masterPtr->tkMaster, 0, 0, 0, 0, 0, 0); |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
return (ClientData) instancePtr; |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
static void |
| 1069 |
ImgXpmDisplay(clientData, display, drawable, imageX, imageY, width, |
| 1070 |
height, drawableX, drawableY) |
| 1071 |
ClientData clientData; |
| 1072 |
|
| 1073 |
Display *display; |
| 1074 |
Drawable drawable; |
| 1075 |
int imageX, imageY; |
| 1076 |
|
| 1077 |
int width, height; |
| 1078 |
int drawableX, drawableY; |
| 1079 |
|
| 1080 |
{ |
| 1081 |
ImgpXpmDisplay(clientData, display, drawable, imageX, imageY, width, |
| 1082 |
height, drawableX, drawableY); |
| 1083 |
} |
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
static void |
| 1103 |
ImgXpmFree(clientData, display) |
| 1104 |
ClientData clientData; |
| 1105 |
|
| 1106 |
Display *display; |
| 1107 |
{ |
| 1108 |
PixmapInstance *instancePtr = (PixmapInstance *) clientData; |
| 1109 |
PixmapInstance *prevPtr; |
| 1110 |
|
| 1111 |
instancePtr->refCount--; |
| 1112 |
if (instancePtr->refCount > 0) { |
| 1113 |
return; |
| 1114 |
} |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
if (instancePtr->pixmap != None) { |
| 1121 |
Tk_FreePixmap(display, instancePtr->pixmap); |
| 1122 |
} |
| 1123 |
ImgXpmFreeInstanceData(instancePtr, 1); |
| 1124 |
|
| 1125 |
if (instancePtr->colors != NULL) { |
| 1126 |
int i; |
| 1127 |
for (i=0; i<instancePtr->masterPtr->ncolors; i++) { |
| 1128 |
if (instancePtr->colors[i].colorPtr != NULL) { |
| 1129 |
Tk_FreeColor(instancePtr->colors[i].colorPtr); |
| 1130 |
} |
| 1131 |
if (instancePtr->masterPtr->cpp != 1) { |
| 1132 |
ckfree(instancePtr->colors[i].cstring); |
| 1133 |
} |
| 1134 |
} |
| 1135 |
ckfree((char*)instancePtr->colors); |
| 1136 |
} |
| 1137 |
|
| 1138 |
if (instancePtr->masterPtr->instancePtr == instancePtr) { |
| 1139 |
instancePtr->masterPtr->instancePtr = instancePtr->nextPtr; |
| 1140 |
} else { |
| 1141 |
for (prevPtr = instancePtr->masterPtr->instancePtr; |
| 1142 |
prevPtr->nextPtr != instancePtr; prevPtr = prevPtr->nextPtr) { |
| 1143 |
|
| 1144 |
} |
| 1145 |
prevPtr->nextPtr = instancePtr->nextPtr; |
| 1146 |
} |
| 1147 |
ckfree((char *) instancePtr); |
| 1148 |
} |
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
static void |
| 1168 |
ImgXpmDelete(masterData) |
| 1169 |
ClientData masterData; |
| 1170 |
|
| 1171 |
{ |
| 1172 |
PixmapMaster *masterPtr = (PixmapMaster *) masterData; |
| 1173 |
|
| 1174 |
if (masterPtr->instancePtr != NULL) { |
| 1175 |
panic("tried to delete pixmap image when instances still exist"); |
| 1176 |
} |
| 1177 |
masterPtr->tkMaster = NULL; |
| 1178 |
if (masterPtr->imageCmd != NULL) { |
| 1179 |
Tcl_DeleteCommand(masterPtr->interp, |
| 1180 |
Tcl_GetCommandName(masterPtr->interp, masterPtr->imageCmd)); |
| 1181 |
} |
| 1182 |
if (masterPtr->isDataAlloced && masterPtr->data != NULL) { |
| 1183 |
ckfree((char*)masterPtr->data); |
| 1184 |
masterPtr->data = NULL; |
| 1185 |
} |
| 1186 |
|
| 1187 |
Tk_FreeOptions(configSpecs, (char *) masterPtr, (Display *) NULL, 0); |
| 1188 |
ckfree((char *) masterPtr); |
| 1189 |
} |
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
static void |
| 1208 |
ImgXpmCmdDeletedProc(clientData) |
| 1209 |
ClientData clientData; |
| 1210 |
|
| 1211 |
{ |
| 1212 |
PixmapMaster *masterPtr = (PixmapMaster *) clientData; |
| 1213 |
|
| 1214 |
masterPtr->imageCmd = NULL; |
| 1215 |
if (masterPtr->tkMaster != NULL) { |
| 1216 |
Tk_DeleteImage(masterPtr->interp, Tk_NameOfImage(masterPtr->tkMaster)); |
| 1217 |
} |
| 1218 |
} |