Debian Codegraph @

pTk/mTk/win/tkWinColor.c

raw
1 /*
2 * tkWinColor.c --
3 *
4 * Functions to map color names to system color values.
5 *
6 * Copyright (c) 1995 Sun Microsystems, Inc.
7 * Copyright (c) 1994 Software Research Associates, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: tkWinColor.c,v 1.6 2000/07/06 03:17:44 mo Exp $
13 */
14
15 #include "tkWinInt.h"
16 #include "tkColor.h"
17
18 /*
19 * The following structure is used to keep track of each color that is
20 * allocated by this module.
21 */
22
23 typedef struct WinColor {
24 TkColor info; /* Generic color information. */
25 int index; /* Index for GetSysColor(), -1 if color
26 * is not a "live" system color. */
27 } WinColor;
28
29 /*
30 * The sysColors array contains the names and index values for the
31 * Windows indirect system color names. In use, all of the names
32 * will have the string "System" prepended, but we omit it in the table
33 * to save space.
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 * Forward declarations for functions defined later in this file.
79 */
80
81 static int FindSystemColor _ANSI_ARGS_((const char *name,
82 XColor *colorPtr, int *indexPtr));
83
84 /*
85 *----------------------------------------------------------------------
86 *
87 * FindSystemColor --
88 *
89 * This routine finds the color entry that corresponds to the
90 * specified color.
91 *
92 * Results:
93 * Returns non-zero on success. The RGB values of the XColor
94 * will be initialized to the proper values on success.
95 *
96 * Side effects:
97 * None.
98 *
99 *----------------------------------------------------------------------
100 */
101
102 static int
103 FindSystemColor(name, colorPtr, indexPtr)
104 const char *name; /* Color name. */
105 XColor *colorPtr; /* Where to store results. */
106 int *indexPtr; /* Out parameter to store color index. */
107 {
108 int l, u, r, i;
109 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
110 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
111
112 /*
113 * Count the number of elements in the color array if we haven't
114 * done so yet.
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 * Perform a binary search on the sorted array of colors.
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 * x257 is (value<<8 + value) to get the properly bit shifted
159 * and padded value. [Bug: 4919]
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 * TkpGetColor --
173 *
174 * Allocate a new TkColor for the color with the given name.
175 *
176 * Results:
177 * Returns a newly allocated TkColor, or NULL on failure.
178 *
179 * Side effects:
180 * May invalidate the colormap cache associated with tkwin upon
181 * allocating a new colormap entry. Allocates a new TkColor
182 * structure.
183 *
184 *----------------------------------------------------------------------
185 */
186
187 TkColor *
188 TkpGetColor(tkwin, name)
189 Tk_Window tkwin; /* Window in which color will be used. */
190 Tk_Uid name; /* Name of color to allocated (in form
191 * suitable for passing to XParseColor). */
192 {
193 WinColor *winColPtr;
194 XColor color;
195 int index = -1; /* -1 indicates that this is not an indirect
196 * sytem color. */
197 /*
198 * Check to see if it is a system color or an X color string. If the
199 * color is found, allocate a new WinColor and store the XColor and the
200 * system color index.
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 * TkpGetColorByValue --
222 *
223 * Given a desired set of red-green-blue intensities for a color,
224 * locate a pixel value to use to draw that color in a given
225 * window.
226 *
227 * Results:
228 * The return value is a pointer to an TkColor structure that
229 * indicates the closest red, blue, and green intensities available
230 * to those specified in colorPtr, and also specifies a pixel
231 * value to use to draw in that color.
232 *
233 * Side effects:
234 * May invalidate the colormap cache for the specified window.
235 * Allocates a new TkColor structure.
236 *
237 *----------------------------------------------------------------------
238 */
239
240 TkColor *
241 TkpGetColorByValue(tkwin, colorPtr)
242 Tk_Window tkwin; /* Window in which color will be used. */
243 XColor *colorPtr; /* Red, green, and blue fields indicate
244 * desired color. */
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 * TkpFreeColor --
261 *
262 * Release the specified color back to the system.
263 *
264 * Results:
265 * None
266 *
267 * Side effects:
268 * Invalidates the colormap cache for the colormap associated with
269 * the given color.
270 *
271 *----------------------------------------------------------------------
272 */
273
274 void
275 TkpFreeColor(tkColPtr)
276 TkColor *tkColPtr; /* Color to be released. Must have been
277 * allocated by TkpGetColor or
278 * TkpGetColorByValue. */
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 * TkWinIndexOfColor --
290 *
291 * Given a color, return the system color index that was used
292 * to create the color.
293 *
294 * Results:
295 * If the color was allocated using a system indirect color name,
296 * then the corresponding GetSysColor() index is returned.
297 * Otherwise, -1 is returned.
298 *
299 * Side effects:
300 * None.
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 * XAllocColor --
320 *
321 * Find the closest available color to the specified XColor.
322 *
323 * Results:
324 * Updates the color argument and returns 1 on success. Otherwise
325 * returns 0.
326 *
327 * Side effects:
328 * Allocates a new color in the palette.
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 * Find the nearest existing palette entry.
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 * If this is not a duplicate, allocate a new entry. Note that
367 * we may get values for index that are above the current size
368 * of the palette. This happens because we don't shrink the size of
369 * the palette object when we deallocate colors so there may be
370 * stale values that match in the upper slots. We should ignore
371 * those values and just put the new color in as if the colors
372 * had not matched.
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 * Determine what color will actually be used on non-colormap systems.
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 /* If I understand what happens above and in tkWinImage.c
426 then ->pixel value is always an RGB value regardless of whether
427 system uses a palette - so just unpack it
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 * XFreeColors --
443 *
444 * Deallocate a block of colors.
445 *
446 * Results:
447 * None.
448 *
449 * Side effects:
450 * Removes entries for the current palette and compacts the
451 * remaining set.
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 * We don't have to do anything for non-palette devices.
474 */
475
476 if (GetDeviceCaps(dc, RASTERCAPS) & RC_PALETTE) {
477
478 /*
479 * This is really slow for large values of npixels.
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 * XCreateColormap --
517 *
518 * Allocate a new colormap.
519 *
520 * Results:
521 * Returns a newly allocated colormap.
522 *
523 * Side effects:
524 * Allocates an empty palette and color list.
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 * Allocate a starting palette with all of the reserved colors.
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 * Add hash entries for each of the static colors.
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 * XFreeColormap --
579 *
580 * Frees the resources associated with the given colormap.
581 *
582 * Results:
583 * None.
584 *
585 * Side effects:
586 * Deletes the palette associated with the colormap. Note that
587 * the palette must not be selected into a device context when
588 * this occurs.
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 * TkWinSelectPalette --
610 *
611 * This function sets up the specified device context with a
612 * given palette. If the palette is stale, it realizes it in
613 * the background unless the palette is the current global
614 * palette.
615 *
616 * Results:
617 * Returns the previous palette selected into the device context.
618 *
619 * Side effects:
620 * May change the system palette.
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 }