1 /*  Part of XPCE --- The SWI-Prolog GUI toolkit
2 
3     Author:        Jan Wielemaker and Anjo Anjewierden
4     E-mail:        jan@swi.psy.uva.nl
5     WWW:           http://www.swi.psy.uva.nl/projects/xpce/
6     Copyright (c)  1996-2011, University of Amsterdam
7     All rights reserved.
8 
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions
11     are met:
12 
13     1. Redistributions of source code must retain the above copyright
14        notice, this list of conditions and the following disclaimer.
15 
16     2. Redistributions in binary form must reproduce the above copyright
17        notice, this list of conditions and the following disclaimer in
18        the documentation and/or other materials provided with the
19        distribution.
20 
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25     COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32     POSSIBILITY OF SUCH DAMAGE.
33 */
34 
35 #include <h/kernel.h>
36 #include <h/graphics.h>
37 
38 static HashTable ColourMaps;		/* name --> maps */
39 
40 static status
initialiseColourMap(ColourMap cm,Name name,Vector colours)41 initialiseColourMap(ColourMap cm, Name name, Vector colours)
42 { if ( isDefault(name) )
43     name = NAME_system;
44   if ( isDefault(colours) )
45     colours = NIL;
46 
47   assign(cm, name,      name);
48   assign(cm, colours,   colours);
49   assign(cm, read_only, OFF);
50 
51   succeed;
52 }
53 
54 
55 static status
unlinkColourMap(ColourMap cm)56 unlinkColourMap(ColourMap cm)
57 { ws_unlink_colour_map(cm);
58 
59   succeed;
60 }
61 
62 
63 static ColourMap
getConvertColourMap(Class class,Name name)64 getConvertColourMap(Class class, Name name)
65 { ColourMap cm;
66   int size;
67 
68   if ( ColourMaps && (cm = getMemberHashTable(ColourMaps, name)) )
69     answer(cm);
70 
71   if ( isstrA(&name->data) &&
72        sscanf(strName(name), "colour_cube_%d", &size) == 1 )
73   { cm = newObject(ClassColourMap, name, NIL, EAV);
74     lockObject(cm, ON);
75 
76     ws_colour_cube(cm, size);
77     assign(cm, read_only, ON);
78     answer(cm);
79   }
80 
81   fail;
82 }
83 
84 
85 static ColourMap
getLookupColourMap(Class class,Name name)86 getLookupColourMap(Class class, Name name)
87 { return getConvertColourMap(class, name);
88 }
89 
90 
91 static Vector
getColoursColourMap(ColourMap cm)92 getColoursColourMap(ColourMap cm)
93 { if ( isNil(cm->colours) )
94     ws_colour_map_colours(cm);
95 
96   if ( notNil(cm->colours) )
97     answer(cm->colours);
98 
99   fail;
100 }
101 
102 
103 /* Type declaractions */
104 
105 static char *T_initialise[] = { "name=[name]*", "colours=[vector]*" };
106 
107 /* Instance Variables */
108 
109 static vardecl var_colour_map[] =
110 { IV(NAME_name, "name*", IV_GET,
111      NAME_name, "Name (for lookup) of the colourmap"),
112   IV(NAME_colours, "vector*", IV_NONE,
113      NAME_storage, "Vector of colours defining the map"),
114   IV(NAME_readOnly, "bool", IV_NONE,
115      NAME_storage, "If @on, map cannot be changed"),
116   IV(NAME_wsRef, "alien:WsRef", IV_NONE,
117      NAME_storage, "Window system handle")
118 };
119 
120 /* Send Methods */
121 
122 static senddecl send_colour_map[] =
123 { SM(NAME_initialise, 2, T_initialise, initialiseColourMap,
124      DEFAULT, "Create from name and colours"),
125   SM(NAME_unlink, 0, NULL, unlinkColourMap,
126      DEFAULT, "Destroy system resources")
127 };
128 
129 /* Get Methods */
130 
131 static getdecl get_colour_map[] =
132 { GM(NAME_lookup, 1, "colour_map", "name", getLookupColourMap,
133      NAME_oms, "Reuse existing named colour_map"),
134   GM(NAME_convert, 1, "colour_map", "name", getConvertColourMap,
135      DEFAULT, "Allow using name to specify a map"),
136   GM(NAME_colours, 0, "vector*", NULL, getColoursColourMap,
137      DEFAULT, "Get the colours of the map")
138 };
139 
140 /* Resources */
141 
142 #define rc_colour_map NULL
143 /*
144 static classvardecl rc_colour_map[] =
145 {
146 };
147 */
148 
149 /* Class Declaration */
150 
151 static Name colour_map_termnames[] = { NAME_name, NAME_colours };
152 
153 ClassDecl(colour_map_decls,
154           var_colour_map, send_colour_map, get_colour_map, rc_colour_map,
155           2, colour_map_termnames,
156           "$Rev$");
157 
158 status
makeClassColourMap(Class class)159 makeClassColourMap(Class class)
160 { declareClass(class, &colour_map_decls);
161 
162   ColourMaps = globalObject(NAME_colourMaps, ClassHashTable, toInt(8), EAV);
163 
164   succeed;
165 }
166