OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tk/] [win/] [tkWinColor.c] - Blame information for rev 578

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/*
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.1.1.1 2002-01-16 10:26:02 markom Exp $
13
 */
14
 
15
#include <tkColor.h>
16
#include <tkWinInt.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
 * colorTable is a hash table used to look up X colors by name.
31
 */
32
 
33
static Tcl_HashTable colorTable;
34
 
35
/*
36
 * The sysColors array contains the names and index values for the
37
 * Windows indirect system color names.  In use, all of the names
38
 * will have the string "System" prepended, but we omit it in the table
39
 * to save space.
40
 */
41
 
42
typedef struct {
43
    char *name;
44
    int index;
45
} SystemColorEntry;
46
 
47
 
48
static SystemColorEntry sysColors[] = {
49
    "3dDarkShadow",             COLOR_3DDKSHADOW,
50
    "3dLight",                  COLOR_3DLIGHT,
51
    "ActiveBorder",             COLOR_ACTIVEBORDER,
52
    "ActiveCaption",            COLOR_ACTIVECAPTION,
53
    "AppWorkspace",             COLOR_APPWORKSPACE,
54
    "Background",               COLOR_BACKGROUND,
55
    "ButtonFace",               COLOR_BTNFACE,
56
    "ButtonHighlight",          COLOR_BTNHIGHLIGHT,
57
    "ButtonShadow",             COLOR_BTNSHADOW,
58
    "ButtonText",               COLOR_BTNTEXT,
59
    "CaptionText",              COLOR_CAPTIONTEXT,
60
    "DisabledText",             COLOR_GRAYTEXT,
61
    "GrayText",                 COLOR_GRAYTEXT,
62
    "Highlight",                COLOR_HIGHLIGHT,
63
    "HighlightText",            COLOR_HIGHLIGHTTEXT,
64
    "InactiveBorder",           COLOR_INACTIVEBORDER,
65
    "InactiveCaption",          COLOR_INACTIVECAPTION,
66
    "InactiveCaptionText",      COLOR_INACTIVECAPTIONTEXT,
67
    "InfoBackground",           COLOR_INFOBK,
68
    "InfoText",                 COLOR_INFOTEXT,
69
    "Menu",                     COLOR_MENU,
70
    "MenuText",                 COLOR_MENUTEXT,
71
    "Scrollbar",                COLOR_SCROLLBAR,
72
    "Window",                   COLOR_WINDOW,
73
    "WindowFrame",              COLOR_WINDOWFRAME,
74
    "WindowText",               COLOR_WINDOWTEXT,
75
    NULL,                       0
76
};
77
 
78
static int ncolors = 0;
79
 
80
/*
81
 * Forward declarations for functions defined later in this file.
82
 */
83
 
84
static int      FindSystemColor _ANSI_ARGS_((const char *name,
85
                    XColor *colorPtr, int *indexPtr));
86
static int      GetColorByName _ANSI_ARGS_((char *name, XColor *color));
87
static int      GetColorByValue _ANSI_ARGS_((char *value, XColor *color));
88
 
89
/*
90
 *----------------------------------------------------------------------
91
 *
92
 * FindSystemColor --
93
 *
94
 *      This routine finds the color entry that corresponds to the
95
 *      specified color.
96
 *
97
 * Results:
98
 *      Returns non-zero on success.  The RGB values of the XColor
99
 *      will be initialized to the proper values on success.
100
 *
101
 * Side effects:
102
 *      None.
103
 *
104
 *----------------------------------------------------------------------
105
 */
106
 
107
static int
108
FindSystemColor(name, colorPtr, indexPtr)
109
    const char *name;           /* Color name. */
110
    XColor *colorPtr;           /* Where to store results. */
111
    int *indexPtr;              /* Out parameter to store color index. */
112
{
113
    int l, u, r, i;
114
 
115
    /*
116
     * Count the number of elements in the color array if we haven't
117
     * done so yet.
118
     */
119
 
120
    if (ncolors == 0) {
121
        SystemColorEntry *ePtr;
122
        int version;
123
 
124
        version = LOBYTE(LOWORD(GetVersion()));
125
        for (ePtr = sysColors; ePtr->name != NULL; ePtr++) {
126
            if (version < 4) {
127
                if (ePtr->index == COLOR_3DDKSHADOW) {
128
                    ePtr->index = COLOR_BTNSHADOW;
129
                } else if (ePtr->index == COLOR_3DLIGHT) {
130
                    ePtr->index = COLOR_BTNHIGHLIGHT;
131
                }
132
            }
133
            ncolors++;
134
        }
135
    }
136
 
137
    /*
138
     * Perform a binary search on the sorted array of colors.
139
     */
140
 
141
    l = 0;
142
    u = ncolors - 1;
143
    while (l <= u) {
144
        i = (l + u) / 2;
145
        r = strcasecmp(name, sysColors[i].name);
146
        if (r == 0) {
147
            break;
148
        } else if (r < 0) {
149
            u = i-1;
150
        } else {
151
            l = i+1;
152
        }
153
    }
154
    if (l > u) {
155
        return 0;
156
    }
157
 
158
    *indexPtr = sysColors[i].index;
159
    colorPtr->pixel = GetSysColor(sysColors[i].index);
160
    colorPtr->red = GetRValue(colorPtr->pixel) << 8;
161
    colorPtr->green = GetGValue(colorPtr->pixel) << 8;
162
    colorPtr->blue = GetBValue(colorPtr->pixel) << 8;
163
    colorPtr->flags = DoRed|DoGreen|DoBlue;
164
    colorPtr->pad = 0;
165
    return 1;
166
}
167
 
168
/*
169
 *----------------------------------------------------------------------
170
 *
171
 * TkpGetColor --
172
 *
173
 *      Allocate a new TkColor for the color with the given name.
174
 *
175
 * Results:
176
 *      Returns a newly allocated TkColor, or NULL on failure.
177
 *
178
 * Side effects:
179
 *      May invalidate the colormap cache associated with tkwin upon
180
 *      allocating a new colormap entry.  Allocates a new TkColor
181
 *      structure.
182
 *
183
 *----------------------------------------------------------------------
184
 */
185
 
186
TkColor *
187
TkpGetColor(tkwin, name)
188
    Tk_Window tkwin;            /* Window in which color will be used. */
189
    Tk_Uid name;                /* Name of color to allocated (in form
190
                                 * suitable for passing to XParseColor). */
191
{
192
    WinColor *winColPtr;
193
    XColor color;
194
    int index = -1;             /* -1 indicates that this is not an indirect
195
                                 * sytem color. */
196
 
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 << 8;
378
                color->green = closeEntry.peGreen << 8;
379
                color->blue = closeEntry.peBlue << 8;
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) << 8);
409
        color->green = (GetGValue(color->pixel) << 8);
410
        color->blue = (GetBValue(color->pixel) << 8);
411
    }
412
 
413
    ReleaseDC(NULL, dc);
414
    return 1;
415
}
416
 
417
/*
418
 *----------------------------------------------------------------------
419
 *
420
 * XFreeColors --
421
 *
422
 *      Deallocate a block of colors.
423
 *
424
 * Results:
425
 *      None.
426
 *
427
 * Side effects:
428
 *      Removes entries for the current palette and compacts the
429
 *      remaining set.
430
 *
431
 *----------------------------------------------------------------------
432
 */
433
 
434
void
435
XFreeColors(display, colormap, pixels, npixels, planes)
436
    Display* display;
437
    Colormap colormap;
438
    unsigned long* pixels;
439
    int npixels;
440
    unsigned long planes;
441
{
442
    TkWinColormap *cmap = (TkWinColormap *) colormap;
443
    COLORREF cref;
444
    UINT count, index, refCount;
445
    int i;
446
    PALETTEENTRY entry, *entries;
447
    Tcl_HashEntry *entryPtr;
448
    HDC dc = GetDC(NULL);
449
 
450
    /*
451
     * We don't have to do anything for non-palette devices.
452
     */
453
 
454
    if (GetDeviceCaps(dc, RASTERCAPS) & RC_PALETTE) {
455
 
456
        /*
457
         * This is really slow for large values of npixels.
458
         */
459
 
460
        for (i = 0; i < npixels; i++) {
461
            entryPtr = Tcl_FindHashEntry(&cmap->refCounts,
462
                    (char *) pixels[i]);
463
            if (!entryPtr) {
464
                panic("Tried to free a color that isn't allocated.");
465
            }
466
            refCount = (int) Tcl_GetHashValue(entryPtr) - 1;
467
            if (refCount == 0) {
468
                cref = pixels[i] & 0x00ffffff;
469
                index = GetNearestPaletteIndex(cmap->palette, cref);
470
                GetPaletteEntries(cmap->palette, index, 1, &entry);
471
                if (cref == RGB(entry.peRed, entry.peGreen, entry.peBlue)) {
472
                    count = cmap->size - index;
473
                    entries = (PALETTEENTRY *) ckalloc(sizeof(PALETTEENTRY)
474
                            * count);
475
                    GetPaletteEntries(cmap->palette, index+1, count, entries);
476
                    SetPaletteEntries(cmap->palette, index, count, entries);
477
                    ckfree((char *) entries);
478
                    cmap->size--;
479
                } else {
480
                    panic("Tried to free a color that isn't allocated.");
481
                }
482
                Tcl_DeleteHashEntry(entryPtr);
483
            } else {
484
                Tcl_SetHashValue(entryPtr, (ClientData)refCount);
485
            }
486
        }
487
    }
488
    ReleaseDC(NULL, dc);
489
}
490
 
491
/*
492
 *----------------------------------------------------------------------
493
 *
494
 * XCreateColormap --
495
 *
496
 *      Allocate a new colormap.
497
 *
498
 * Results:
499
 *      Returns a newly allocated colormap.
500
 *
501
 * Side effects:
502
 *      Allocates an empty palette and color list.
503
 *
504
 *----------------------------------------------------------------------
505
 */
506
 
507
Colormap
508
XCreateColormap(display, w, visual, alloc)
509
    Display* display;
510
    Window w;
511
    Visual* visual;
512
    int alloc;
513
{
514
    char logPalBuf[sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY)];
515
    LOGPALETTE *logPalettePtr;
516
    PALETTEENTRY *entryPtr;
517
    TkWinColormap *cmap;
518
    Tcl_HashEntry *hashPtr;
519
    int new;
520
    UINT i;
521
    HPALETTE sysPal;
522
 
523
    /*
524
     * Allocate a starting palette with all of the reserved colors.
525
     */
526
 
527
    logPalettePtr = (LOGPALETTE *) logPalBuf;
528
    logPalettePtr->palVersion = 0x300;
529
    sysPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
530
    logPalettePtr->palNumEntries = GetPaletteEntries(sysPal, 0, 256,
531
            logPalettePtr->palPalEntry);
532
 
533
    cmap = (TkWinColormap *) ckalloc(sizeof(TkWinColormap));
534
    cmap->size = logPalettePtr->palNumEntries;
535
    cmap->stale = 0;
536
    cmap->palette = CreatePalette(logPalettePtr);
537
 
538
    /*
539
     * Add hash entries for each of the static colors.
540
     */
541
 
542
    Tcl_InitHashTable(&cmap->refCounts, TCL_ONE_WORD_KEYS);
543
    for (i = 0; i < logPalettePtr->palNumEntries; i++) {
544
        entryPtr = logPalettePtr->palPalEntry + i;
545
        hashPtr = Tcl_CreateHashEntry(&cmap->refCounts, (char*) PALETTERGB(
546
            entryPtr->peRed, entryPtr->peGreen, entryPtr->peBlue), &new);
547
        Tcl_SetHashValue(hashPtr, (ClientData)1);
548
    }
549
 
550
    return (Colormap)cmap;
551
}
552
 
553
/*
554
 *----------------------------------------------------------------------
555
 *
556
 * XFreeColormap --
557
 *
558
 *      Frees the resources associated with the given colormap.
559
 *
560
 * Results:
561
 *      None.
562
 *
563
 * Side effects:
564
 *      Deletes the palette associated with the colormap.  Note that
565
 *      the palette must not be selected into a device context when
566
 *      this occurs.
567
 *
568
 *----------------------------------------------------------------------
569
 */
570
 
571
void
572
XFreeColormap(display, colormap)
573
    Display* display;
574
    Colormap colormap;
575
{
576
    TkWinColormap *cmap = (TkWinColormap *) colormap;
577
    if (!DeleteObject(cmap->palette)) {
578
        panic("Unable to free colormap, palette is still selected.");
579
    }
580
    Tcl_DeleteHashTable(&cmap->refCounts);
581
    ckfree((char *) cmap);
582
}
583
 
584
/*
585
 *----------------------------------------------------------------------
586
 *
587
 * TkWinSelectPalette --
588
 *
589
 *      This function sets up the specified device context with a
590
 *      given palette.  If the palette is stale, it realizes it in
591
 *      the background unless the palette is the current global
592
 *      palette.
593
 *
594
 * Results:
595
 *      Returns the previous palette selected into the device context.
596
 *
597
 * Side effects:
598
 *      May change the system palette.
599
 *
600
 *----------------------------------------------------------------------
601
 */
602
 
603
HPALETTE
604
TkWinSelectPalette(dc, colormap)
605
    HDC dc;
606
    Colormap colormap;
607
{
608
    TkWinColormap *cmap = (TkWinColormap *) colormap;
609
    HPALETTE oldPalette;
610
 
611
    oldPalette = SelectPalette(dc, cmap->palette,
612
            (cmap->palette == TkWinGetSystemPalette()) ? FALSE : TRUE);
613
    RealizePalette(dc);
614
    return oldPalette;
615
}
616
 
617
/* CYGNUS LOCAL: The system colors have changed.  Update them.  */
618
 
619
static void
620
ChangeColor(tkColPtr)
621
    TkColor *tkColPtr;
622
{
623
    WinColor *winColPtr = (WinColor *) tkColPtr;
624
 
625
    if (winColPtr->index != -1) {
626
        unsigned long pixel;
627
 
628
        pixel = GetSysColor(winColPtr->index);
629
        if (pixel != winColPtr->info.color.pixel) {
630
            winColPtr->info.color.pixel = pixel;
631
            winColPtr->info.color.red = GetRValue(pixel) << 8;
632
            winColPtr->info.color.green = GetGValue(pixel) << 8;
633
            winColPtr->info.color.blue = GetBValue(pixel) << 8;
634
            TkColorChanged((TkColor *) winColPtr);
635
        }
636
    }
637
}
638
 
639
void
640
TkWinSysColorChange()
641
{
642
    TkMapOverColors(ChangeColor);
643
}

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.