| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include "tkWinInt.h" |
| 16 |
#include "tkColor.h" |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
typedef struct WinColor { |
| 24 |
TkColor info; |
| 25 |
int index; |
| 26 |
|
| 27 |
} WinColor; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
typedef struct { |
| 37 |
char *name; |
| 38 |
int index; |
| 39 |
} SystemColorEntry; |
| 40 |
|
| 41 |
|
| 42 |
static SystemColorEntry sysColors[] = { |
| 43 |
"3dDarkShadow", COLOR_3DDKSHADOW, |
| 44 |
"3dLight", COLOR_3DLIGHT, |
| 45 |
"ActiveBorder", COLOR_ACTIVEBORDER, |
| 46 |
"ActiveCaption", COLOR_ACTIVECAPTION, |
| 47 |
"AppWorkspace", COLOR_APPWORKSPACE, |
| 48 |
"Background", COLOR_BACKGROUND, |
| 49 |
"ButtonFace", COLOR_BTNFACE, |
| 50 |
"ButtonHighlight", COLOR_BTNHIGHLIGHT, |
| 51 |
"ButtonShadow", COLOR_BTNSHADOW, |
| 52 |
"ButtonText", COLOR_BTNTEXT, |
| 53 |
"CaptionText", COLOR_CAPTIONTEXT, |
| 54 |
"DisabledText", COLOR_GRAYTEXT, |
| 55 |
"GrayText", COLOR_GRAYTEXT, |
| 56 |
"Highlight", COLOR_HIGHLIGHT, |
| 57 |
"HighlightText", COLOR_HIGHLIGHTTEXT, |
| 58 |
"InactiveBorder", COLOR_INACTIVEBORDER, |
| 59 |
"InactiveCaption", COLOR_INACTIVECAPTION, |
| 60 |
"InactiveCaptionText", COLOR_INACTIVECAPTIONTEXT, |
| 61 |
"InfoBackground", COLOR_INFOBK, |
| 62 |
"InfoText", COLOR_INFOTEXT, |
| 63 |
"Menu", COLOR_MENU, |
| 64 |
"MenuText", COLOR_MENUTEXT, |
| 65 |
"Scrollbar", COLOR_SCROLLBAR, |
| 66 |
"Window", COLOR_WINDOW, |
| 67 |
"WindowFrame", COLOR_WINDOWFRAME, |
| 68 |
"WindowText", COLOR_WINDOWTEXT, |
| 69 |
NULL, 0 |
| 70 |
}; |
| 71 |
|
| 72 |
typedef struct ThreadSpecificData { |
| 73 |
int ncolors; |
| 74 |
} ThreadSpecificData; |
| 75 |
static Tcl_ThreadDataKey dataKey; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
static int FindSystemColor _ANSI_ARGS_((const char *name, |
| 82 |
XColor *colorPtr, int *indexPtr)); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
static int |
| 103 |
FindSystemColor(name, colorPtr, indexPtr) |
| 104 |
const char *name; |
| 105 |
XColor *colorPtr; |
| 106 |
int *indexPtr; |
| 107 |
{ |
| 108 |
int l, u, r, i; |
| 109 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 110 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
if (tsdPtr->ncolors == 0) { |
| 118 |
SystemColorEntry *ePtr; |
| 119 |
int version; |
| 120 |
|
| 121 |
version = LOBYTE(LOWORD(GetVersion())); |
| 122 |
for (ePtr = sysColors; ePtr->name != NULL; ePtr++) { |
| 123 |
if (version < 4) { |
| 124 |
if (ePtr->index == COLOR_3DDKSHADOW) { |
| 125 |
ePtr->index = COLOR_BTNSHADOW; |
| 126 |
} else if (ePtr->index == COLOR_3DLIGHT) { |
| 127 |
ePtr->index = COLOR_BTNHIGHLIGHT; |
| 128 |
} |
| 129 |
} |
| 130 |
tsdPtr->ncolors++; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
l = 0; |
| 139 |
u = tsdPtr->ncolors - 1; |
| 140 |
while (l <= u) { |
| 141 |
i = (l + u) / 2; |
| 142 |
r = strcasecmp(name, sysColors[i].name); |
| 143 |
if (r == 0) { |
| 144 |
break; |
| 145 |
} else if (r < 0) { |
| 146 |
u = i-1; |
| 147 |
} else { |
| 148 |
l = i+1; |
| 149 |
} |
| 150 |
} |
| 151 |
if (l > u) { |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
*indexPtr = sysColors[i].index; |
| 156 |
colorPtr->pixel = GetSysColor(sysColors[i].index); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
colorPtr->red = GetRValue(colorPtr->pixel) * 257; |
| 162 |
colorPtr->green = GetGValue(colorPtr->pixel) * 257; |
| 163 |
colorPtr->blue = GetBValue(colorPtr->pixel) * 257; |
| 164 |
colorPtr->flags = DoRed|DoGreen|DoBlue; |
| 165 |
colorPtr->pad = 0; |
| 166 |
return 1; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
TkColor * |
| 188 |
TkpGetColor(tkwin, name) |
| 189 |
Tk_Window tkwin; |
| 190 |
Tk_Uid name; |
| 191 |
|
| 192 |
{ |
| 193 |
WinColor *winColPtr; |
| 194 |
XColor color; |
| 195 |
int index = -1; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
if (((strncasecmp(name, "system", 6) == 0) |
| 204 |
&& FindSystemColor(name+6, &color, &index)) |
| 205 |
|| XParseColor(Tk_Display(tkwin), Tk_Colormap(tkwin), name, |
| 206 |
&color)) { |
| 207 |
winColPtr = (WinColor *) ckalloc(sizeof(WinColor)); |
| 208 |
winColPtr->info.color = color; |
| 209 |
winColPtr->index = index; |
| 210 |
|
| 211 |
XAllocColor(Tk_Display(tkwin), Tk_Colormap(tkwin), |
| 212 |
&winColPtr->info.color); |
| 213 |
return (TkColor *) winColPtr; |
| 214 |
} |
| 215 |
return (TkColor *) NULL; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
TkColor * |
| 241 |
TkpGetColorByValue(tkwin, colorPtr) |
| 242 |
Tk_Window tkwin; |
| 243 |
XColor *colorPtr; |
| 244 |
|
| 245 |
{ |
| 246 |
WinColor *tkColPtr = (WinColor *) ckalloc(sizeof(WinColor)); |
| 247 |
|
| 248 |
tkColPtr->info.color.red = colorPtr->red; |
| 249 |
tkColPtr->info.color.green = colorPtr->green; |
| 250 |
tkColPtr->info.color.blue = colorPtr->blue; |
| 251 |
tkColPtr->info.color.pixel = 0; |
| 252 |
tkColPtr->index = -1; |
| 253 |
XAllocColor(Tk_Display(tkwin), Tk_Colormap(tkwin), &tkColPtr->info.color); |
| 254 |
return (TkColor *) tkColPtr; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
void |
| 275 |
TkpFreeColor(tkColPtr) |
| 276 |
TkColor *tkColPtr; |
| 277 |
|
| 278 |
|
| 279 |
{ |
| 280 |
Screen *screen = tkColPtr->screen; |
| 281 |
|
| 282 |
XFreeColors(DisplayOfScreen(screen), tkColPtr->colormap, |
| 283 |
&tkColPtr->color.pixel, 1, 0L); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
int |
| 306 |
TkWinIndexOfColor(colorPtr) |
| 307 |
XColor *colorPtr; |
| 308 |
{ |
| 309 |
register WinColor *winColPtr = (WinColor *) colorPtr; |
| 310 |
if (winColPtr->info.magic == COLOR_MAGIC) { |
| 311 |
return winColPtr->index; |
| 312 |
} |
| 313 |
return -1; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
int |
| 334 |
XAllocColor(display, colormap, color) |
| 335 |
Display* display; |
| 336 |
Colormap colormap; |
| 337 |
XColor* color; |
| 338 |
{ |
| 339 |
TkWinColormap *cmap = (TkWinColormap *) colormap; |
| 340 |
PALETTEENTRY entry, closeEntry; |
| 341 |
HDC dc = GetDC(NULL); |
| 342 |
|
| 343 |
entry.peRed = (color->red) >> 8; |
| 344 |
entry.peGreen = (color->green) >> 8; |
| 345 |
entry.peBlue = (color->blue) >> 8; |
| 346 |
entry.peFlags = 0; |
| 347 |
|
| 348 |
if (GetDeviceCaps(dc, RASTERCAPS) & RC_PALETTE) { |
| 349 |
unsigned long sizePalette = GetDeviceCaps(dc, SIZEPALETTE); |
| 350 |
UINT newPixel, closePixel; |
| 351 |
int new, refCount; |
| 352 |
Tcl_HashEntry *entryPtr; |
| 353 |
UINT index; |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
newPixel = RGB(entry.peRed, entry.peGreen, entry.peBlue); |
| 360 |
index = GetNearestPaletteIndex(cmap->palette, newPixel); |
| 361 |
GetPaletteEntries(cmap->palette, index, 1, &closeEntry); |
| 362 |
closePixel = RGB(closeEntry.peRed, closeEntry.peGreen, |
| 363 |
closeEntry.peBlue); |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
if ((index >= cmap->size) || (newPixel != closePixel)) { |
| 376 |
if (cmap->size == sizePalette) { |
| 377 |
color->red = closeEntry.peRed * 257; |
| 378 |
color->green = closeEntry.peGreen * 257; |
| 379 |
color->blue = closeEntry.peBlue * 257; |
| 380 |
entry = closeEntry; |
| 381 |
if (index >= cmap->size) { |
| 382 |
OutputDebugString("XAllocColor: Colormap is bigger than we thought"); |
| 383 |
} |
| 384 |
} else { |
| 385 |
cmap->size++; |
| 386 |
ResizePalette(cmap->palette, cmap->size); |
| 387 |
SetPaletteEntries(cmap->palette, cmap->size - 1, 1, &entry); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
color->pixel = PALETTERGB(entry.peRed, entry.peGreen, entry.peBlue); |
| 392 |
entryPtr = Tcl_CreateHashEntry(&cmap->refCounts, |
| 393 |
(char *) color->pixel, &new); |
| 394 |
if (new) { |
| 395 |
refCount = 1; |
| 396 |
} else { |
| 397 |
refCount = ((int) Tcl_GetHashValue(entryPtr)) + 1; |
| 398 |
} |
| 399 |
Tcl_SetHashValue(entryPtr, (ClientData)refCount); |
| 400 |
} else { |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
color->pixel = GetNearestColor(dc, |
| 407 |
RGB(entry.peRed, entry.peGreen, entry.peBlue)); |
| 408 |
color->red = GetRValue(color->pixel) * 257; |
| 409 |
color->green = GetGValue(color->pixel) * 257; |
| 410 |
color->blue = GetBValue(color->pixel) * 257; |
| 411 |
} |
| 412 |
|
| 413 |
ReleaseDC(NULL, dc); |
| 414 |
return 1; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
void |
| 419 |
XQueryColors(display, colormap, color, ncolors) |
| 420 |
Display* display; |
| 421 |
Colormap colormap; |
| 422 |
XColor* color; |
| 423 |
int ncolors; |
| 424 |
{ |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
while (ncolors-- > 0) |
| 430 |
{ |
| 431 |
color->red = (GetRValue(color->pixel) << 8); |
| 432 |
color->green = (GetGValue(color->pixel) << 8); |
| 433 |
color->blue = (GetBValue(color->pixel) << 8); |
| 434 |
color++; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
void |
| 457 |
XFreeColors(display, colormap, pixels, npixels, planes) |
| 458 |
Display* display; |
| 459 |
Colormap colormap; |
| 460 |
unsigned long* pixels; |
| 461 |
int npixels; |
| 462 |
unsigned long planes; |
| 463 |
{ |
| 464 |
TkWinColormap *cmap = (TkWinColormap *) colormap; |
| 465 |
COLORREF cref; |
| 466 |
UINT count, index, refCount; |
| 467 |
int i; |
| 468 |
PALETTEENTRY entry, *entries; |
| 469 |
Tcl_HashEntry *entryPtr; |
| 470 |
HDC dc = GetDC(NULL); |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
if (GetDeviceCaps(dc, RASTERCAPS) & RC_PALETTE) { |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
for (i = 0; i < npixels; i++) { |
| 483 |
entryPtr = Tcl_FindHashEntry(&cmap->refCounts, |
| 484 |
(char *) pixels[i]); |
| 485 |
if (!entryPtr) { |
| 486 |
panic("Tried to free a color that isn't allocated."); |
| 487 |
} |
| 488 |
refCount = (int) Tcl_GetHashValue(entryPtr) - 1; |
| 489 |
if (refCount == 0) { |
| 490 |
cref = pixels[i] & 0x00ffffff; |
| 491 |
index = GetNearestPaletteIndex(cmap->palette, cref); |
| 492 |
GetPaletteEntries(cmap->palette, index, 1, &entry); |
| 493 |
if (cref == RGB(entry.peRed, entry.peGreen, entry.peBlue)) { |
| 494 |
count = cmap->size - index; |
| 495 |
entries = (PALETTEENTRY *) ckalloc(sizeof(PALETTEENTRY) |
| 496 |
* count); |
| 497 |
GetPaletteEntries(cmap->palette, index+1, count, entries); |
| 498 |
SetPaletteEntries(cmap->palette, index, count, entries); |
| 499 |
ckfree((char *) entries); |
| 500 |
cmap->size--; |
| 501 |
} else { |
| 502 |
panic("Tried to free a color that isn't allocated."); |
| 503 |
} |
| 504 |
Tcl_DeleteHashEntry(entryPtr); |
| 505 |
} else { |
| 506 |
Tcl_SetHashValue(entryPtr, (ClientData)refCount); |
| 507 |
} |
| 508 |
} |
| 509 |
} |
| 510 |
ReleaseDC(NULL, dc); |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
Colormap |
| 530 |
XCreateColormap(display, w, visual, alloc) |
| 531 |
Display* display; |
| 532 |
Window w; |
| 533 |
Visual* visual; |
| 534 |
int alloc; |
| 535 |
{ |
| 536 |
char logPalBuf[sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY)]; |
| 537 |
LOGPALETTE *logPalettePtr; |
| 538 |
PALETTEENTRY *entryPtr; |
| 539 |
TkWinColormap *cmap; |
| 540 |
Tcl_HashEntry *hashPtr; |
| 541 |
int new; |
| 542 |
UINT i; |
| 543 |
HPALETTE sysPal; |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
logPalettePtr = (LOGPALETTE *) logPalBuf; |
| 550 |
logPalettePtr->palVersion = 0x300; |
| 551 |
sysPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE); |
| 552 |
logPalettePtr->palNumEntries = GetPaletteEntries(sysPal, 0, 256, |
| 553 |
logPalettePtr->palPalEntry); |
| 554 |
|
| 555 |
cmap = (TkWinColormap *) ckalloc(sizeof(TkWinColormap)); |
| 556 |
cmap->size = logPalettePtr->palNumEntries; |
| 557 |
cmap->stale = 0; |
| 558 |
cmap->palette = CreatePalette(logPalettePtr); |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
Tcl_InitHashTable(&cmap->refCounts, TCL_ONE_WORD_KEYS); |
| 565 |
for (i = 0; i < logPalettePtr->palNumEntries; i++) { |
| 566 |
entryPtr = logPalettePtr->palPalEntry + i; |
| 567 |
hashPtr = Tcl_CreateHashEntry(&cmap->refCounts, (char*) PALETTERGB( |
| 568 |
entryPtr->peRed, entryPtr->peGreen, entryPtr->peBlue), &new); |
| 569 |
Tcl_SetHashValue(hashPtr, (ClientData)1); |
| 570 |
} |
| 571 |
|
| 572 |
return (Colormap)cmap; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
void |
| 594 |
XFreeColormap(display, colormap) |
| 595 |
Display* display; |
| 596 |
Colormap colormap; |
| 597 |
{ |
| 598 |
TkWinColormap *cmap = (TkWinColormap *) colormap; |
| 599 |
if (!DeleteObject(cmap->palette)) { |
| 600 |
panic("Unable to free colormap, palette is still selected."); |
| 601 |
} |
| 602 |
Tcl_DeleteHashTable(&cmap->refCounts); |
| 603 |
ckfree((char *) cmap); |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
HPALETTE |
| 626 |
TkWinSelectPalette(dc, colormap) |
| 627 |
HDC dc; |
| 628 |
Colormap colormap; |
| 629 |
{ |
| 630 |
TkWinColormap *cmap = (TkWinColormap *) colormap; |
| 631 |
HPALETTE oldPalette; |
| 632 |
|
| 633 |
oldPalette = SelectPalette(dc, cmap->palette, |
| 634 |
(cmap->palette == TkWinGetSystemPalette()) ? FALSE : TRUE); |
| 635 |
RealizePalette(dc); |
| 636 |
return oldPalette; |
| 637 |
} |