summaryrefslogtreecommitdiff
path: root/tcl/macosx/tkMacOSXColor.c
blob: f5ab4abe0440ad346bf97a6b737e3d9c013ded51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* 
 * tkMacOSXColor.c --
 *
 *        This file maintains a database of color values for the Tk
 *        toolkit, in order to avoid round-trips to the server to
 *        map color names to pixel values.
 *
 * Copyright (c) 1990-1994 The Regents of the University of California.
 * Copyright (c) 1994-1996 Sun Microsystems, Inc.
 * Copyright 2001, Apple Computer, Inc.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id$
 */

#include <tkColor.h>
#include "tkMacOSXInt.h"
#include <Carbon/Carbon.h>

/*
 * Default Auxillary Control Record for all controls.  This is cached once
 * and is updated by the system.  We use this to get the default system
 * colors used by controls.
 */
/*
 * Stubbed out for OS X
static AuxCtlHandle defaultAuxCtlHandle = NULL;
*/

/*
 * Forward declarations for procedures defined later in this file:
 */

static int        GetControlPartColor _ANSI_ARGS_((short part, RGBColor *macColor));
static int        GetMenuPartColor _ANSI_ARGS_((int part, RGBColor *macColor));
static int        GetWindowPartColor _ANSI_ARGS_((short part, RGBColor *macColor));

/*
 *----------------------------------------------------------------------
 *
 * TkSetMacColor --
 *
 *        Populates a Macintosh RGBColor structure from a X style
 *        pixel value.
 *
 * Results:
 *        Returns false if not a real pixel, true otherwise.
 *
 * Side effects:
 *        The variable macColor is updated to the pixels value.
 *
 *----------------------------------------------------------------------
 */

int
TkSetMacColor(
    unsigned long pixel,        /* Pixel value to convert. */
    RGBColor *macColor)                /* Mac color struct to modify. */
{
    switch (pixel >> 24) {
        case HIGHLIGHT_PIXEL:
            LMGetHiliteRGB(macColor);
            return true;
        case HIGHLIGHT_TEXT_PIXEL:
            LMGetHiliteRGB(macColor);
            if ((macColor->red == 0) && (macColor->green == 0)
                    && (macColor->blue == 0)) {
                macColor->red = macColor->green = macColor->blue = 0xFFFF;
            } else {
                macColor->red = macColor->green = macColor->blue = 0;
            }
            return true;
        case CONTROL_TEXT_PIXEL:
            GetControlPartColor(cTextColor, macColor);
            return true;
        case CONTROL_BODY_PIXEL:
            GetControlPartColor(cBodyColor, macColor);
            return true;
        case CONTROL_FRAME_PIXEL:
            GetControlPartColor(cFrameColor, macColor);
            return true;
        case WINDOW_BODY_PIXEL:
            GetWindowPartColor(wContentColor, macColor);
            return true;
        case MENU_ACTIVE_PIXEL:
        case MENU_ACTIVE_TEXT_PIXEL:
        case MENU_BACKGROUND_PIXEL:
        case MENU_DISABLED_PIXEL:
        case MENU_TEXT_PIXEL:
            return GetMenuPartColor((pixel >> 24), macColor);
        case APPEARANCE_PIXEL:
            return false;
        case PIXEL_MAGIC:
        default:
            macColor->blue = (unsigned short) ((pixel & 0xFF) << 8);
            macColor->green = (unsigned short) (((pixel >> 8) & 0xFF) << 8);
            macColor->red = (unsigned short) (((pixel >> 16) & 0xFF) << 8);
            return true;
    }
}

/*
 *----------------------------------------------------------------------
 *
 * Stub functions --
 *
 *        These functions are just stubs for functions that either
 *        don't make sense on the Mac or have yet to be implemented.
 *
 * Results:
 *        None.
 *
 * Side effects:
 *        These calls do nothing - which may not be expected.
 *
 *----------------------------------------------------------------------
 */

Status
XAllocColor(
    Display *display,                /* Display. */
    Colormap map,                /* Not used. */
    XColor *colorPtr)                /* XColor struct to modify. */
{
    display->request++;
    colorPtr->pixel = TkpGetPixel(colorPtr);
    return 1;
}

Colormap
XCreateColormap(
    Display *display,                /* Display. */
    Window window,                /* X window. */
    Visual *visual,                /* Not used. */
    int alloc)                        /* Not used. */
{
    static Colormap index = 1;
    
    /*
     * Just return a new value each time.
     */
    return index++;
}

void
XFreeColormap(
    Display* display,                /* Display. */
    Colormap colormap)                /* Colormap. */
{
}

void
XFreeColors(
    Display* display,                /* Display. */
    Colormap colormap,                /* Colormap. */
    unsigned long* pixels,        /* Array of pixels. */
    int npixels,                /* Number of pixels. */
    unsigned long planes)        /* Number of pixel planes. */
{
    /*
     * The Macintosh version of Tk uses TrueColor.  Nothing
     * needs to be done to release colors as there really is
     * no colormap in the Tk sense.
     */
}

/*
 *----------------------------------------------------------------------
 *
 * TkpGetColor --
 *
 *        Allocate a new TkColor for the color with the given name.
 *
 * Results:
 *        Returns a newly allocated TkColor, or NULL on failure.
 *
 * Side effects:
 *        May invalidate the colormap cache associated with tkwin upon
 *        allocating a new colormap entry.  Allocates a new TkColor
 *        structure.
 *
 *----------------------------------------------------------------------
 */

TkColor *
TkpGetColor(
    Tk_Window tkwin,                /* Window in which color will be used. */
    Tk_Uid name)                /* Name of color to allocated (in form
                                 * suitable for passing to XParseColor). */
{
    Display *display = Tk_Display(tkwin);
    Colormap colormap = Tk_Colormap(tkwin);
    TkColor *tkColPtr;
    XColor color;

    /*
     * Check to see if this is a system color.  Otherwise, XParseColor
     * will do all the work.
     */
    if (strncasecmp(name, "system", 6) == 0) {
        int foundSystemColor = false;
        RGBColor rgbValue;
        char pixelCode = 0;
        
        if (!strcasecmp(name+6, "Highlight")) {
            LMGetHiliteRGB(&rgbValue);
            pixelCode = HIGHLIGHT_PIXEL;
            foundSystemColor = true;
        } else if (!strcasecmp(name+6, "HighlightText")) {
            LMGetHiliteRGB(&rgbValue);
            if ((rgbValue.red == 0) && (rgbValue.green == 0)
                    && (rgbValue.blue == 0)) {
                rgbValue.red = rgbValue.green = rgbValue.blue = 0xFFFF;
            } else {
                rgbValue.red = rgbValue.green = rgbValue.blue = 0;
            }
            pixelCode = HIGHLIGHT_TEXT_PIXEL;
            foundSystemColor = true;
        } else if (!strcasecmp(name+6, "ButtonText")) {
            GetControlPartColor(cTextColor, &rgbValue);
            pixelCode = CONTROL_TEXT_PIXEL;
            foundSystemColor = true;
        } else if (!strcasecmp(name+6, "ButtonFace")) {
            GetControlPartColor(cBodyColor, &rgbValue);
            pixelCode = CONTROL_BODY_PIXEL;
            foundSystemColor = true;
        } else if (!strcasecmp(name+6, "ButtonFrame")) {
            GetControlPartColor(cFrameColor, &rgbValue);
            pixelCode = CONTROL_FRAME_PIXEL;
            foundSystemColor = true;
        } else if (!strcasecmp(name+6, "WindowBody")) {
            GetWindowPartColor(wContentColor, &rgbValue);
            pixelCode = WINDOW_BODY_PIXEL;
            foundSystemColor = true;
        } else if (!strcasecmp(name+6, "MenuActive")) {
            GetMenuPartColor(MENU_ACTIVE_PIXEL, &rgbValue);
            pixelCode = MENU_ACTIVE_PIXEL;
            foundSystemColor = true;
        } else if (!strcasecmp(name+6, "MenuActiveText")) {
            GetMenuPartColor(MENU_ACTIVE_TEXT_PIXEL, &rgbValue);
            pixelCode = MENU_ACTIVE_TEXT_PIXEL;
            foundSystemColor = true;
        } else if (!strcasecmp(name+6, "Menu")) {
            GetMenuPartColor(MENU_BACKGROUND_PIXEL, &rgbValue);
            pixelCode = MENU_BACKGROUND_PIXEL;
            foundSystemColor = true;
        } else if (!strcasecmp(name+6, "MenuDisabled")) {
            GetMenuPartColor(MENU_DISABLED_PIXEL, &rgbValue);
            pixelCode = MENU_DISABLED_PIXEL;
            foundSystemColor = true;
        } else if (!strcasecmp(name+6, "MenuText")) {
            GetMenuPartColor(MENU_TEXT_PIXEL, &rgbValue);
            pixelCode = MENU_TEXT_PIXEL;
            foundSystemColor = true;
        } else if (!strcasecmp(name+6, "AppearanceColor")) {
            color.red = 0;
            color.green = 0;
            color.blue = 0;
            pixelCode = APPEARANCE_PIXEL;
            foundSystemColor = true;
        }
        
        if (foundSystemColor) {
            color.red = rgbValue.red;
            color.green = rgbValue.green;
            color.blue = rgbValue.blue;
            color.pixel = ((((((pixelCode << 8)
                | ((color.red >> 8) & 0xff)) << 8)
                | ((color.green >> 8) & 0xff)) << 8)
                | ((color.blue >> 8) & 0xff));
            
            tkColPtr = (TkColor *) ckalloc(sizeof(TkColor));
            tkColPtr->color = color;
            return tkColPtr;
        }
    }
    
    if (XParseColor(display, colormap, name, &color) == 0) {
        return (TkColor *) NULL;
    }
    
    tkColPtr = (TkColor *) ckalloc(sizeof(TkColor));
    tkColPtr->color = color;

    return tkColPtr;
}

/*
 *----------------------------------------------------------------------
 *
 * TkpGetColorByValue --
 *
 *        Given a desired set of red-green-blue intensities for a color,
 *        locate a pixel value to use to draw that color in a given
 *        window.
 *
 * Results:
 *        The return value is a pointer to an TkColor structure that
 *        indicates the closest red, blue, and green intensities available
 *        to those specified in colorPtr, and also specifies a pixel
 *        value to use to draw in that color.
 *
 * Side effects:
 *        May invalidate the colormap cache for the specified window.
 *        Allocates a new TkColor structure.
 *
 *----------------------------------------------------------------------
 */

TkColor *
TkpGetColorByValue(
    Tk_Window tkwin,                /* Window in which color will be used. */
    XColor *colorPtr)                /* Red, green, and blue fields indicate
                                 * desired color. */
{
    TkColor *tkColPtr = (TkColor *) ckalloc(sizeof(TkColor));

    tkColPtr->color.red = colorPtr->red;
    tkColPtr->color.green = colorPtr->green;
    tkColPtr->color.blue = colorPtr->blue;
    tkColPtr->color.pixel = TkpGetPixel(&tkColPtr->color);
    return tkColPtr;
}

/*
 *----------------------------------------------------------------------
 *
 * GetControlPartColor --
 *
 *        Given a part number this function will return the standard
 *        system default color for that part.  It does this by looking
 *        in the system's 'cctb' resource.
 *
 * Results:
 *        True if a color is found, false otherwise.
 *
 * Side effects:
 *        If a color is found then the RGB variable will be changed to
 *        the parts color.
 *
 *----------------------------------------------------------------------
 */

static int 
GetControlPartColor(
    short part,                 /* Part code. */
    RGBColor *macColor)                /* Pointer to Mac color. */
{
/* Stubbed out for OS X
    short index;
    CCTabHandle ccTab;

    if (defaultAuxCtlHandle == NULL) {
        GetAuxiliaryControlRecord(NULL, &defaultAuxCtlHandle);
    }
    ccTab = (**defaultAuxCtlHandle).acCTable;
    if(ccTab && (ResError() == noErr)) {
        for(index = 0; index <= (**ccTab).ctSize; index++) {
            if((**ccTab).ctTable[index].value == part) {
                *macColor = (**ccTab).ctTable[index].rgb;
                return true;
            }
        }
    }
*/
    return false;
}

/*
 *----------------------------------------------------------------------
 *
 * GetWindowPartColor --
 *
 *      Given a part number this function will return the standard
 *      system default color for that part.  It does this by looking
 *      in the system's 'wctb' resource.
 *
 * Results:
 *      True if a color is found, false otherwise.
 *
 * Side effects:
 *      If a color is found then the RGB variable will be changed to
 *      the parts color.
 *
 *----------------------------------------------------------------------
 */

static int
GetWindowPartColor(
    short part,                 /* Part code. */
    RGBColor *macColor)         /* Pointer to Mac color. */
{
    short index;
    WCTabHandle wcTab;

    if (part == wContentColor) {
        GetThemeBrushAsColor(kThemeBrushDocumentWindowBackground,
                0xFFFF, true, macColor);
        return true;
    } else {
        wcTab = (WCTabHandle) GetResource('wctb', 0);
        if(wcTab && (ResError() == noErr)) {
            for(index = 0; index <= (**wcTab).ctSize; index++) {
                if((**wcTab).ctTable[index].value == part) {
                    *macColor = (**wcTab).ctTable[index].rgb;
                    return true;
                }
            }
        }
        return false;
    }
}

/*
 *----------------------------------------------------------------------
 *
 * GetMenuPartColor --
 *
 *        Given a magic pixel value, returns the RGB color associated
 *        with it by looking the value up in the system's 'mctb' resource.
 *
 * Results:
 *        True if a color is found, false otherwise.
 *
 * Side effects:
 *        If a color is found then the RGB variable will be changed to
 *        the parts color.
 *
 *----------------------------------------------------------------------
 */

static int
GetMenuPartColor(
    int pixel,                        /* The magic pixel value */
    RGBColor *macColor)                /* Pointer to Mac color */
{
    
    /* Under Appearance, we don't want to set any menu colors when we
       are asked for the standard menu colors.  So we return false (which
       means don't use this color... */
       
        macColor->red = 0xFFFF;
        macColor->green = 0;
        macColor->blue = 0;
        return false;
}