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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tk/] [mac/] [tkMacColor.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/*
2
 * tkMacColor.c --
3
 *
4
 *      This file maintains a database of color values for the Tk
5
 *      toolkit, in order to avoid round-trips to the server to
6
 *      map color names to pixel values.
7
 *
8
 * Copyright (c) 1990-1994 The Regents of the University of California.
9
 * Copyright (c) 1994-1996 Sun Microsystems, Inc.
10
 *
11
 * See the file "license.terms" for information on usage and redistribution
12
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13
 *
14
 * RCS: @(#) $Id: tkMacColor.c,v 1.1.1.1 2002-01-16 10:25:55 markom Exp $
15
 */
16
 
17
#include <tkColor.h>
18
#include "tkMacInt.h"
19
 
20
#include <LowMem.h>
21
#include <Palettes.h>
22
#include <Quickdraw.h>
23
 
24
/*
25
 * Default Auxillary Control Record for all controls.  This is cached once
26
 * and is updated by the system.  We use this to get the default system
27
 * colors used by controls.
28
 */
29
static AuxCtlHandle defaultAuxCtlHandle = NULL;
30
 
31
/*
32
 * Forward declarations for procedures defined later in this file:
33
 */
34
 
35
static int      GetControlPartColor _ANSI_ARGS_((short part, RGBColor *macColor));
36
static int      GetMenuPartColor _ANSI_ARGS_((int part, RGBColor *macColor));
37
static int      GetWindowPartColor _ANSI_ARGS_((short part, RGBColor *macColor));
38
 
39
/*
40
 *----------------------------------------------------------------------
41
 *
42
 * TkSetMacColor --
43
 *
44
 *      Populates a Macintosh RGBColor structure from a X style
45
 *      pixel value.
46
 *
47
 * Results:
48
 *      Returns false if not a real pixel, true otherwise.
49
 *
50
 * Side effects:
51
 *      The variable macColor is updated to the pixels value.
52
 *
53
 *----------------------------------------------------------------------
54
 */
55
 
56
int
57
TkSetMacColor(
58
    unsigned long pixel,        /* Pixel value to convert. */
59
    RGBColor *macColor)         /* Mac color struct to modify. */
60
{
61
    switch (pixel >> 24) {
62
        case HIGHLIGHT_PIXEL:
63
            LMGetHiliteRGB(macColor);
64
            return true;
65
        case HIGHLIGHT_TEXT_PIXEL:
66
            LMGetHiliteRGB(macColor);
67
            if ((macColor->red == 0) && (macColor->green == 0)
68
                    && (macColor->blue == 0)) {
69
                macColor->red = macColor->green = macColor->blue = 0xFFFFFFFF;
70
            } else {
71
                macColor->red = macColor->green = macColor->blue = 0;
72
            }
73
            return true;
74
        case CONTROL_TEXT_PIXEL:
75
            GetControlPartColor(cTextColor, macColor);
76
            return true;
77
        case CONTROL_BODY_PIXEL:
78
            GetControlPartColor(cBodyColor, macColor);
79
            return true;
80
        case CONTROL_FRAME_PIXEL:
81
            GetControlPartColor(cFrameColor, macColor);
82
            return true;
83
        case WINDOW_BODY_PIXEL:
84
            GetWindowPartColor(wContentColor, macColor);
85
            return true;
86
        case MENU_ACTIVE_PIXEL:
87
        case MENU_ACTIVE_TEXT_PIXEL:
88
        case MENU_BACKGROUND_PIXEL:
89
        case MENU_DISABLED_PIXEL:
90
        case MENU_TEXT_PIXEL:
91
            GetMenuPartColor((pixel >> 24), macColor);
92
            return true;
93
        case APPEARANCE_PIXEL:
94
            return false;
95
        case PIXEL_MAGIC:
96
        default:
97
            macColor->blue = (unsigned short) ((pixel & 0xFF) << 8);
98
            macColor->green = (unsigned short) (((pixel >> 8) & 0xFF) << 8);
99
            macColor->red = (unsigned short) (((pixel >> 16) & 0xFF) << 8);
100
            return true;
101
    }
102
}
103
 
104
/*
105
 *----------------------------------------------------------------------
106
 *
107
 * Stub functions --
108
 *
109
 *      These functions are just stubs for functions that either
110
 *      don't make sense on the Mac or have yet to be implemented.
111
 *
112
 * Results:
113
 *      None.
114
 *
115
 * Side effects:
116
 *      These calls do nothing - which may not be expected.
117
 *
118
 *----------------------------------------------------------------------
119
 */
120
 
121
Status
122
XAllocColor(
123
    Display *display,           /* Display. */
124
    Colormap map,               /* Not used. */
125
    XColor *colorPtr)           /* XColor struct to modify. */
126
{
127
    display->request++;
128
    colorPtr->pixel = TkpGetPixel(colorPtr);
129
    return 1;
130
}
131
 
132
Colormap
133
XCreateColormap(
134
    Display *display,           /* Display. */
135
    Window window,              /* X window. */
136
    Visual *visual,             /* Not used. */
137
    int alloc)                  /* Not used. */
138
{
139
    static Colormap index = 1;
140
 
141
    /*
142
     * Just return a new value each time.
143
     */
144
    return index++;
145
}
146
 
147
void
148
XFreeColormap(
149
    Display* display,           /* Display. */
150
    Colormap colormap)          /* Colormap. */
151
{
152
}
153
 
154
void
155
XFreeColors(
156
    Display* display,           /* Display. */
157
    Colormap colormap,          /* Colormap. */
158
    unsigned long* pixels,      /* Array of pixels. */
159
    int npixels,                /* Number of pixels. */
160
    unsigned long planes)       /* Number of pixel planes. */
161
{
162
    /*
163
     * The Macintosh version of Tk uses TrueColor.  Nothing
164
     * needs to be done to release colors as there really is
165
     * no colormap in the Tk sense.
166
     */
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(
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
    Display *display = Tk_Display(tkwin);
194
    Colormap colormap = Tk_Colormap(tkwin);
195
    TkColor *tkColPtr;
196
    XColor color;
197
 
198
    /*
199
     * Check to see if this is a system color.  Otherwise, XParseColor
200
     * will do all the work.
201
     */
202
    if (strncasecmp(name, "system", 6) == 0) {
203
        int foundSystemColor = false;
204
        RGBColor rgbValue;
205
        char pixelCode;
206
 
207
        if (!strcasecmp(name+6, "Highlight")) {
208
            LMGetHiliteRGB(&rgbValue);
209
            pixelCode = HIGHLIGHT_PIXEL;
210
            foundSystemColor = true;
211
        } else if (!strcasecmp(name+6, "HighlightText")) {
212
            LMGetHiliteRGB(&rgbValue);
213
            if ((rgbValue.red == 0) && (rgbValue.green == 0)
214
                    && (rgbValue.blue == 0)) {
215
                rgbValue.red = rgbValue.green = rgbValue.blue = 0xFFFFFFFF;
216
            } else {
217
                rgbValue.red = rgbValue.green = rgbValue.blue = 0;
218
            }
219
            pixelCode = HIGHLIGHT_TEXT_PIXEL;
220
            foundSystemColor = true;
221
        } else if (!strcasecmp(name+6, "ButtonText")) {
222
            GetControlPartColor(cTextColor, &rgbValue);
223
            pixelCode = CONTROL_TEXT_PIXEL;
224
            foundSystemColor = true;
225
        } else if (!strcasecmp(name+6, "ButtonFace")) {
226
            GetControlPartColor(cBodyColor, &rgbValue);
227
            pixelCode = CONTROL_BODY_PIXEL;
228
            foundSystemColor = true;
229
        } else if (!strcasecmp(name+6, "ButtonFrame")) {
230
            GetControlPartColor(cFrameColor, &rgbValue);
231
            pixelCode = CONTROL_FRAME_PIXEL;
232
            foundSystemColor = true;
233
        } else if (!strcasecmp(name+6, "WindowBody")) {
234
            GetWindowPartColor(wContentColor, &rgbValue);
235
            pixelCode = WINDOW_BODY_PIXEL;
236
            foundSystemColor = true;
237
        } else if (!strcasecmp(name+6, "MenuActive")) {
238
            GetMenuPartColor(MENU_ACTIVE_PIXEL, &rgbValue);
239
            pixelCode = MENU_ACTIVE_PIXEL;
240
            foundSystemColor = true;
241
        } else if (!strcasecmp(name+6, "MenuActiveText")) {
242
            GetMenuPartColor(MENU_ACTIVE_TEXT_PIXEL, &rgbValue);
243
            pixelCode = MENU_ACTIVE_TEXT_PIXEL;
244
            foundSystemColor = true;
245
        } else if (!strcasecmp(name+6, "Menu")) {
246
            GetMenuPartColor(MENU_BACKGROUND_PIXEL, &rgbValue);
247
            pixelCode = MENU_BACKGROUND_PIXEL;
248
            foundSystemColor = true;
249
        } else if (!strcasecmp(name+6, "MenuDisabled")) {
250
            GetMenuPartColor(MENU_DISABLED_PIXEL, &rgbValue);
251
            pixelCode = MENU_DISABLED_PIXEL;
252
            foundSystemColor = true;
253
        } else if (!strcasecmp(name+6, "MenuText")) {
254
            GetMenuPartColor(MENU_TEXT_PIXEL, &rgbValue);
255
            pixelCode = MENU_TEXT_PIXEL;
256
            foundSystemColor = true;
257
        } else if (!strcasecmp(name+6, "AppearanceColor")) {
258
            color.red = 0;
259
            color.green = 0;
260
            color.blue = 0;
261
            pixelCode = APPEARANCE_PIXEL;
262
            foundSystemColor = true;
263
        }
264
 
265
        if (foundSystemColor) {
266
            color.red = rgbValue.red;
267
            color.green = rgbValue.green;
268
            color.blue = rgbValue.blue;
269
            color.pixel = ((((((pixelCode << 8)
270
                | ((color.red >> 8) & 0xff)) << 8)
271
                | ((color.green >> 8) & 0xff)) << 8)
272
                | ((color.blue >> 8) & 0xff));
273
 
274
            tkColPtr = (TkColor *) ckalloc(sizeof(TkColor));
275
            tkColPtr->color = color;
276
            return tkColPtr;
277
        }
278
    }
279
 
280
    if (XParseColor(display, colormap, name, &color) == 0) {
281
        return (TkColor *) NULL;
282
    }
283
 
284
    tkColPtr = (TkColor *) ckalloc(sizeof(TkColor));
285
    tkColPtr->color = color;
286
 
287
    return tkColPtr;
288
}
289
 
290
/*
291
 *----------------------------------------------------------------------
292
 *
293
 * TkpGetColorByValue --
294
 *
295
 *      Given a desired set of red-green-blue intensities for a color,
296
 *      locate a pixel value to use to draw that color in a given
297
 *      window.
298
 *
299
 * Results:
300
 *      The return value is a pointer to an TkColor structure that
301
 *      indicates the closest red, blue, and green intensities available
302
 *      to those specified in colorPtr, and also specifies a pixel
303
 *      value to use to draw in that color.
304
 *
305
 * Side effects:
306
 *      May invalidate the colormap cache for the specified window.
307
 *      Allocates a new TkColor structure.
308
 *
309
 *----------------------------------------------------------------------
310
 */
311
 
312
TkColor *
313
TkpGetColorByValue(
314
    Tk_Window tkwin,            /* Window in which color will be used. */
315
    XColor *colorPtr)           /* Red, green, and blue fields indicate
316
                                 * desired color. */
317
{
318
    TkColor *tkColPtr = (TkColor *) ckalloc(sizeof(TkColor));
319
 
320
    tkColPtr->color.red = colorPtr->red;
321
    tkColPtr->color.green = colorPtr->green;
322
    tkColPtr->color.blue = colorPtr->blue;
323
    tkColPtr->color.pixel = TkpGetPixel(&tkColPtr->color);
324
    return tkColPtr;
325
}
326
 
327
/*
328
 *----------------------------------------------------------------------
329
 *
330
 * GetControlPartColor --
331
 *
332
 *      Given a part number this function will return the standard
333
 *      system default color for that part.  It does this by looking
334
 *      in the system's 'cctb' resource.
335
 *
336
 * Results:
337
 *      True if a color is found, false otherwise.
338
 *
339
 * Side effects:
340
 *      If a color is found then the RGB variable will be changed to
341
 *      the parts color.
342
 *
343
 *----------------------------------------------------------------------
344
 */
345
 
346
static int
347
GetControlPartColor(
348
    short part,                 /* Part code. */
349
    RGBColor *macColor)         /* Pointer to Mac color. */
350
{
351
    short index;
352
    CCTabHandle ccTab;
353
 
354
    if (defaultAuxCtlHandle == NULL) {
355
        GetAuxiliaryControlRecord(NULL, &defaultAuxCtlHandle);
356
    }
357
    ccTab = (**defaultAuxCtlHandle).acCTable;
358
    if(ccTab && (ResError() == noErr)) {
359
        for(index = 0; index <= (**ccTab).ctSize; index++) {
360
            if((**ccTab).ctTable[index].value == part) {
361
                *macColor = (**ccTab).ctTable[index].rgb;
362
                return true;
363
            }
364
        }
365
    }
366
    return false;
367
}
368
 
369
/*
370
 *----------------------------------------------------------------------
371
 *
372
 * GetWindowPartColor --
373
 *
374
 *      Given a part number this function will return the standard
375
 *      system default color for that part.  It does this by looking
376
 *      in the system's 'wctb' resource.
377
 *
378
 * Results:
379
 *      True if a color is found, false otherwise.
380
 *
381
 * Side effects:
382
 *      If a color is found then the RGB variable will be changed to
383
 *      the parts color.
384
 *
385
 *----------------------------------------------------------------------
386
 */
387
 
388
static int
389
GetWindowPartColor(
390
    short part,                 /* Part code. */
391
    RGBColor *macColor)         /* Pointer to Mac color. */
392
{
393
    short index;
394
    WCTabHandle wcTab;
395
 
396
    wcTab = (WCTabHandle) GetResource('wctb', 0);
397
    if(wcTab && (ResError() == noErr)) {
398
        for(index = 0; index <= (**wcTab).ctSize; index++) {
399
            if((**wcTab).ctTable[index].value == part) {
400
                *macColor = (**wcTab).ctTable[index].rgb;
401
                return true;
402
            }
403
        }
404
    }
405
    return false;
406
}
407
 
408
/*
409
 *----------------------------------------------------------------------
410
 *
411
 * GetMenuPartColor --
412
 *
413
 *      Given a magic pixel value, returns the RGB color associated
414
 *      with it by looking the value up in the system's 'mctb' resource.
415
 *
416
 * Results:
417
 *      True if a color is found, false otherwise.
418
 *
419
 * Side effects:
420
 *      If a color is found then the RGB variable will be changed to
421
 *      the parts color.
422
 *
423
 *----------------------------------------------------------------------
424
 */
425
 
426
static int
427
GetMenuPartColor(
428
    int pixel,                  /* The magic pixel value */
429
    RGBColor *macColor)         /* Pointer to Mac color */
430
{
431
    RGBColor backColor, foreColor;
432
    GDHandle maxDevice;
433
    Rect globalRect;
434
    MCEntryPtr mcEntryPtr = GetMCEntry(0, 0);
435
 
436
    switch (pixel) {
437
        case MENU_ACTIVE_PIXEL:
438
            if (mcEntryPtr == NULL) {
439
                macColor->red = macColor->blue = macColor->green = 0;
440
            } else {
441
                *macColor = mcEntryPtr->mctRGB3;
442
            }
443
            return 1;
444
        case MENU_ACTIVE_TEXT_PIXEL:
445
            if (mcEntryPtr == NULL) {
446
                macColor->red = macColor->blue = macColor->green = 0xFFFF;
447
            } else {
448
                *macColor = mcEntryPtr->mctRGB2;
449
            }
450
            return 1;
451
        case MENU_BACKGROUND_PIXEL:
452
            if (mcEntryPtr == NULL) {
453
                macColor->red = macColor->blue = macColor->green = 0xFFFF;
454
            } else {
455
                *macColor = mcEntryPtr->mctRGB2;
456
            }
457
            return 1;
458
        case MENU_DISABLED_PIXEL:
459
            if (mcEntryPtr == NULL) {
460
                backColor.red = backColor.blue = backColor.green = 0xFFFF;
461
                foreColor.red = foreColor.blue = foreColor.green = 0x0000;
462
            } else {
463
                backColor = mcEntryPtr->mctRGB2;
464
                foreColor = mcEntryPtr->mctRGB3;
465
            }
466
            SetRect(&globalRect, SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX);
467
            maxDevice = GetMaxDevice(&globalRect);
468
            if (GetGray(maxDevice, &backColor, &foreColor)) {
469
                *macColor = foreColor;
470
            } else {
471
 
472
                /*
473
                 * Pointer may have been moved by GetMaxDevice or GetGray.
474
                 */
475
 
476
                mcEntryPtr = GetMCEntry(0,0);
477
                if (mcEntryPtr == NULL) {
478
                    macColor->red = macColor->green = macColor->blue = 0x7777;
479
                } else {
480
                    *macColor = mcEntryPtr->mctRGB2;
481
                }
482
            }
483
            return 1;
484
        case MENU_TEXT_PIXEL:
485
            if (mcEntryPtr == NULL) {
486
                macColor->red = macColor->green = macColor->blue = 0;
487
            } else {
488
                *macColor = mcEntryPtr->mctRGB3;
489
            }
490
            return 1;
491
    }
492
    return 0;
493
}

powered by: WebSVN 2.1.0

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