| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "tkInt.h" |
| 18 |
#include "tkPort.h" |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
typedef struct VisualDictionary { |
| 26 |
char *name; |
| 27 |
int minLength; |
| 28 |
|
| 29 |
int class; |
| 30 |
} VisualDictionary; |
| 31 |
static VisualDictionary visualNames[] = { |
| 32 |
{"best", 1, 0}, |
| 33 |
{"directcolor", 2, DirectColor}, |
| 34 |
{"grayscale", 1, GrayScale}, |
| 35 |
{"greyscale", 1, GrayScale}, |
| 36 |
{"pseudocolor", 1, PseudoColor}, |
| 37 |
{"staticcolor", 7, StaticColor}, |
| 38 |
{"staticgray", 7, StaticGray}, |
| 39 |
{"staticgrey", 7, StaticGray}, |
| 40 |
{"truecolor", 1, TrueColor}, |
| 41 |
{NULL, 0, 0}, |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
struct TkColormap { |
| 50 |
Colormap colormap; |
| 51 |
Visual *visual; |
| 52 |
|
| 53 |
int refCount; |
| 54 |
|
| 55 |
|
| 56 |
int shareable; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
struct TkColormap *nextPtr; |
| 63 |
|
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
Visual * |
| 90 |
Tk_GetVisual(interp, tkwin, arg, depthPtr, colormapPtr) |
| 91 |
Tcl_Interp *interp; |
| 92 |
|
| 93 |
Tk_Window tkwin; |
| 94 |
|
| 95 |
Tcl_Obj *arg; |
| 96 |
|
| 97 |
int *depthPtr; |
| 98 |
|
| 99 |
Colormap *colormapPtr; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
{ |
| 104 |
Tk_Window tkwin2; |
| 105 |
XVisualInfo template, *visInfoList, *bestPtr; |
| 106 |
long mask; |
| 107 |
Visual *visual; |
| 108 |
int length, c, numVisuals, prio, bestPrio, i; |
| 109 |
CONST char *p; |
| 110 |
VisualDictionary *dictPtr; |
| 111 |
TkColormap *cmapPtr; |
| 112 |
TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr; |
| 113 |
char *string = Tcl_GetString(arg); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
c = string[0]; |
| 121 |
if (c == '.') { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
tkwin2 = Tk_NameToWindow(interp, string, tkwin); |
| 130 |
if (tkwin2 == NULL) { |
| 131 |
return NULL; |
| 132 |
} |
| 133 |
visual = Tk_Visual(tkwin2); |
| 134 |
if (Tk_Screen(tkwin) == Tk_Screen(tkwin2)) { |
| 135 |
*depthPtr = Tk_Depth(tkwin2); |
| 136 |
if (colormapPtr != NULL) { |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
*colormapPtr = Tk_Colormap(tkwin2); |
| 144 |
for (cmapPtr = dispPtr->cmapPtr; cmapPtr != NULL; |
| 145 |
cmapPtr = cmapPtr->nextPtr) { |
| 146 |
if (cmapPtr->colormap == *colormapPtr) { |
| 147 |
cmapPtr->refCount += 1; |
| 148 |
break; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
return visual; |
| 153 |
} |
| 154 |
template.depth = Tk_Depth(tkwin2); |
| 155 |
template.class = visual->class; |
| 156 |
template.red_mask = visual->red_mask; |
| 157 |
template.green_mask = visual->green_mask; |
| 158 |
template.blue_mask = visual->blue_mask; |
| 159 |
template.colormap_size = visual->map_entries; |
| 160 |
template.bits_per_rgb = visual->bits_per_rgb; |
| 161 |
mask = VisualDepthMask|VisualClassMask|VisualRedMaskMask |
| 162 |
|VisualGreenMaskMask|VisualBlueMaskMask|VisualColormapSizeMask |
| 163 |
|VisualBitsPerRGBMask; |
| 164 |
} else if ((c == 0) || ((c == 'd') && (string[1] != 0) |
| 165 |
&& (strncmp(string, "default", strlen(string)) == 0))) { |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
if (colormapPtr != NULL) { |
| 171 |
*colormapPtr = DefaultColormapOfScreen(Tk_Screen(tkwin)); |
| 172 |
} |
| 173 |
*depthPtr = DefaultDepthOfScreen(Tk_Screen(tkwin)); |
| 174 |
return DefaultVisualOfScreen(Tk_Screen(tkwin)); |
| 175 |
} else if (isdigit(UCHAR(c))) { |
| 176 |
int visualId; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
if (Tcl_GetIntFromObj(interp, arg, &visualId) == TCL_ERROR) { |
| 183 |
Tcl_ResetResult(interp); |
| 184 |
Tcl_AppendResult(interp, "bad X identifier for visual: ", |
| 185 |
string, "\"", (char *) NULL); |
| 186 |
return NULL; |
| 187 |
} |
| 188 |
template.visualid = visualId; |
| 189 |
mask = VisualIDMask; |
| 190 |
} else { |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
for (p = string; *p != 0; p++) { |
| 197 |
if (isspace(UCHAR(*p)) || isdigit(UCHAR(*p))) { |
| 198 |
break; |
| 199 |
} |
| 200 |
} |
| 201 |
length = p - string; |
| 202 |
template.class = -1; |
| 203 |
for (dictPtr = visualNames; dictPtr->name != NULL; dictPtr++) { |
| 204 |
if ((dictPtr->name[0] == c) && (length >= dictPtr->minLength) |
| 205 |
&& (strncmp(string, dictPtr->name, |
| 206 |
(size_t) length) == 0)) { |
| 207 |
template.class = dictPtr->class; |
| 208 |
break; |
| 209 |
} |
| 210 |
} |
| 211 |
if (template.class == -1) { |
| 212 |
Tcl_AppendResult(interp, "unknown or ambiguous visual name \"", |
| 213 |
string, "\": class must be ", (char *) NULL); |
| 214 |
for (dictPtr = visualNames; dictPtr->name != NULL; dictPtr++) { |
| 215 |
Tcl_AppendResult(interp, dictPtr->name, ", ", (char *) NULL); |
| 216 |
} |
| 217 |
Tcl_AppendResult(interp, "or default", (char *) NULL); |
| 218 |
return NULL; |
| 219 |
} |
| 220 |
while (isspace(UCHAR(*p))) { |
| 221 |
p++; |
| 222 |
} |
| 223 |
if (*p == 0) { |
| 224 |
template.depth = 10000; |
| 225 |
} else { |
| 226 |
Tcl_Obj * temp = NULL; |
| 227 |
LangSetString(&temp,p); |
| 228 |
if (Tcl_GetIntFromObj(interp, temp, &template.depth) != TCL_OK) { |
| 229 |
LangFreeArg(temp,TCL_STATIC); |
| 230 |
return NULL; |
| 231 |
} |
| 232 |
LangFreeArg(temp,TCL_STATIC); |
| 233 |
} |
| 234 |
if (c == 'b') { |
| 235 |
mask = 0; |
| 236 |
} else { |
| 237 |
mask = VisualClassMask; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
template.screen = Tk_ScreenNumber(tkwin); |
| 247 |
mask |= VisualScreenMask; |
| 248 |
visInfoList = XGetVisualInfo(Tk_Display(tkwin), mask, &template, |
| 249 |
&numVisuals); |
| 250 |
if (visInfoList == NULL) { |
| 251 |
Tcl_SetResult(interp, "couldn't find an appropriate visual", |
| 252 |
TCL_STATIC); |
| 253 |
return NULL; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
bestPrio = 0; |
| 272 |
bestPtr = NULL; |
| 273 |
for (i = 0; i < numVisuals; i++) { |
| 274 |
switch (visInfoList[i].class) { |
| 275 |
case DirectColor: prio = 5; break; |
| 276 |
case GrayScale: prio = 1; break; |
| 277 |
case PseudoColor: prio = 7; break; |
| 278 |
case StaticColor: prio = 3; break; |
| 279 |
case StaticGray: prio = 1; break; |
| 280 |
case TrueColor: prio = 5; break; |
| 281 |
default: prio = 0; break; |
| 282 |
} |
| 283 |
if (visInfoList[i].visual |
| 284 |
== DefaultVisualOfScreen(Tk_Screen(tkwin))) { |
| 285 |
prio++; |
| 286 |
} |
| 287 |
if (bestPtr == NULL) { |
| 288 |
goto newBest; |
| 289 |
} |
| 290 |
if (visInfoList[i].depth < bestPtr->depth) { |
| 291 |
if (visInfoList[i].depth >= template.depth) { |
| 292 |
goto newBest; |
| 293 |
} |
| 294 |
} else if (visInfoList[i].depth > bestPtr->depth) { |
| 295 |
if (bestPtr->depth < template.depth) { |
| 296 |
goto newBest; |
| 297 |
} |
| 298 |
} else { |
| 299 |
if (prio > bestPrio) { |
| 300 |
goto newBest; |
| 301 |
} |
| 302 |
} |
| 303 |
continue; |
| 304 |
|
| 305 |
newBest: |
| 306 |
bestPtr = &visInfoList[i]; |
| 307 |
bestPrio = prio; |
| 308 |
} |
| 309 |
*depthPtr = bestPtr->depth; |
| 310 |
visual = bestPtr->visual; |
| 311 |
XFree((char *) visInfoList); |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
if (colormapPtr != NULL) { |
| 322 |
if (visual == DefaultVisualOfScreen(Tk_Screen(tkwin))) { |
| 323 |
*colormapPtr = DefaultColormapOfScreen(Tk_Screen(tkwin)); |
| 324 |
} else { |
| 325 |
for (cmapPtr = dispPtr->cmapPtr; cmapPtr != NULL; |
| 326 |
cmapPtr = cmapPtr->nextPtr) { |
| 327 |
if (cmapPtr->shareable && (cmapPtr->visual == visual)) { |
| 328 |
*colormapPtr = cmapPtr->colormap; |
| 329 |
cmapPtr->refCount += 1; |
| 330 |
goto done; |
| 331 |
} |
| 332 |
} |
| 333 |
cmapPtr = (TkColormap *) ckalloc(sizeof(TkColormap)); |
| 334 |
cmapPtr->colormap = XCreateColormap(Tk_Display(tkwin), |
| 335 |
RootWindowOfScreen(Tk_Screen(tkwin)), visual, |
| 336 |
AllocNone); |
| 337 |
cmapPtr->visual = visual; |
| 338 |
cmapPtr->refCount = 1; |
| 339 |
cmapPtr->shareable = 1; |
| 340 |
cmapPtr->nextPtr = dispPtr->cmapPtr; |
| 341 |
dispPtr->cmapPtr = cmapPtr; |
| 342 |
*colormapPtr = cmapPtr->colormap; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
done: |
| 347 |
return visual; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
Colormap |
| 372 |
Tk_GetColormap(interp, tkwin, string) |
| 373 |
Tcl_Interp *interp; |
| 374 |
|
| 375 |
Tk_Window tkwin; |
| 376 |
|
| 377 |
CONST char *string; |
| 378 |
|
| 379 |
|
| 380 |
{ |
| 381 |
Colormap colormap; |
| 382 |
TkColormap *cmapPtr; |
| 383 |
TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr; |
| 384 |
Tk_Window other; |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
if (strcmp(string, "new") == 0) { |
| 391 |
cmapPtr = (TkColormap *) ckalloc(sizeof(TkColormap)); |
| 392 |
cmapPtr->colormap = XCreateColormap(Tk_Display(tkwin), |
| 393 |
RootWindowOfScreen(Tk_Screen(tkwin)), Tk_Visual(tkwin), |
| 394 |
AllocNone); |
| 395 |
cmapPtr->visual = Tk_Visual(tkwin); |
| 396 |
cmapPtr->refCount = 1; |
| 397 |
cmapPtr->shareable = 0; |
| 398 |
cmapPtr->nextPtr = dispPtr->cmapPtr; |
| 399 |
dispPtr->cmapPtr = cmapPtr; |
| 400 |
return cmapPtr->colormap; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
other = Tk_NameToWindow(interp, string, tkwin); |
| 410 |
if (other == NULL) { |
| 411 |
return None; |
| 412 |
} |
| 413 |
if (Tk_Screen(other) != Tk_Screen(tkwin)) { |
| 414 |
Tcl_AppendResult(interp, "can't use colormap for ", string, |
| 415 |
": not on same screen", (char *) NULL); |
| 416 |
return None; |
| 417 |
} |
| 418 |
if (Tk_Visual(other) != Tk_Visual(tkwin)) { |
| 419 |
Tcl_AppendResult(interp, "can't use colormap for ", string, |
| 420 |
": incompatible visuals", (char *) NULL); |
| 421 |
return None; |
| 422 |
} |
| 423 |
colormap = Tk_Colormap(other); |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
for (cmapPtr = dispPtr->cmapPtr; cmapPtr != NULL; |
| 431 |
cmapPtr = cmapPtr->nextPtr) { |
| 432 |
if (cmapPtr->colormap == colormap) { |
| 433 |
cmapPtr->refCount += 1; |
| 434 |
} |
| 435 |
} |
| 436 |
return colormap; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
void |
| 458 |
Tk_FreeColormap(display, colormap) |
| 459 |
Display *display; |
| 460 |
|
| 461 |
Colormap colormap; |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
{ |
| 467 |
TkDisplay *dispPtr; |
| 468 |
TkColormap *cmapPtr, *prevPtr; |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
dispPtr = TkGetDisplay(display); |
| 477 |
if (dispPtr == NULL) { |
| 478 |
panic("unknown display passed to Tk_FreeColormap"); |
| 479 |
} |
| 480 |
for (prevPtr = NULL, cmapPtr = dispPtr->cmapPtr; cmapPtr != NULL; |
| 481 |
prevPtr = cmapPtr, cmapPtr = cmapPtr->nextPtr) { |
| 482 |
if (cmapPtr->colormap == colormap) { |
| 483 |
cmapPtr->refCount -= 1; |
| 484 |
if (cmapPtr->refCount == 0) { |
| 485 |
XFreeColormap(display, colormap); |
| 486 |
if (prevPtr == NULL) { |
| 487 |
dispPtr->cmapPtr = cmapPtr->nextPtr; |
| 488 |
} else { |
| 489 |
prevPtr->nextPtr = cmapPtr->nextPtr; |
| 490 |
} |
| 491 |
ckfree((char *) cmapPtr); |
| 492 |
} |
| 493 |
return; |
| 494 |
} |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
void |
| 520 |
Tk_PreserveColormap(display, colormap) |
| 521 |
Display *display; |
| 522 |
|
| 523 |
Colormap colormap; |
| 524 |
|
| 525 |
{ |
| 526 |
TkDisplay *dispPtr; |
| 527 |
TkColormap *cmapPtr; |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
dispPtr = TkGetDisplay(display); |
| 536 |
if (dispPtr == NULL) { |
| 537 |
panic("unknown display passed to Tk_PreserveColormap"); |
| 538 |
} |
| 539 |
for (cmapPtr = dispPtr->cmapPtr; cmapPtr != NULL; |
| 540 |
cmapPtr = cmapPtr->nextPtr) { |
| 541 |
if (cmapPtr->colormap == colormap) { |
| 542 |
cmapPtr->refCount += 1; |
| 543 |
return; |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|