Propagate Layer changes via Style command on-the-fly.
[fvwm.git] / libs / PictureBase.c
blob14034ec9174dd4d0e038e9b03e8f298a9add7cd5
1 /* -*-c-*- */
2 /* This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * This module is all original code
19 * by Rob Nation
20 * Copyright 1993, Robert Nation
21 * You may use this code for any purpose, as long as the original
22 * copyright remains in the source code and all documentation
25 Changed 02/12/97 by Dan Espen:
26 - added routines to determine color closeness, for color use reduction.
27 Some of the logic comes from pixy2, so the copyright is below.
30 * Copyright 1996, Romano Giannetti. No guarantees or warantees or anything
31 * are provided or implied in any way whatsoever. Use this program at your
32 * own risk. Permission to use this program for any purpose is given,
33 * as long as the copyright is kept intact.
35 * Romano Giannetti - Dipartimento di Ingegneria dell'Informazione
36 * via Diotisalvi, 2 PISA
37 * mailto:romano@iet.unipi.it
38 * http://www.iet.unipi.it/~romano
44 * Routines to handle initialization, loading, and removing of xpm's or mono-
45 * icon images.
49 #include "config.h"
51 #include <stdio.h>
53 #include <X11/Xlib.h>
55 #include "fvwmlib.h"
56 #include "Graphics.h"
57 #include "PictureBase.h"
58 #include "PictureUtils.h"
59 #include "Fsvg.h"
60 #include "Strings.h"
62 Bool Pdefault;
63 Visual *Pvisual;
64 static Visual *FvwmVisual;
65 Colormap Pcmap;
66 static Colormap FvwmCmap;
67 unsigned int Pdepth;
68 static unsigned int FvwmDepth;
69 Display *Pdpy; /* Save area for display pointer */
70 Bool PUseDynamicColors;
72 Pixel PWhitePixel;
73 Pixel PBlackPixel;
74 Pixel FvwmWhitePixel;
75 Pixel FvwmBlackPixel;
77 void PictureSetupWhiteAndBlack(void);
80 void PictureInitCMap(Display *dpy) {
81 char *envp;
83 Pdpy = dpy;
84 /* if fvwm has not set this env-var it is using the default visual */
85 envp = getenv("FVWM_VISUALID");
86 if (envp != NULL && *envp > 0) {
87 /* convert the env-vars to a visual and colormap */
88 int viscount;
89 XVisualInfo vizinfo, *xvi;
91 sscanf(envp, "%lx", &vizinfo.visualid);
92 xvi = XGetVisualInfo(dpy, VisualIDMask, &vizinfo, &viscount);
93 Pvisual = xvi->visual;
94 Pdepth = xvi->depth;
95 /* Note: if FVWM_VISUALID is set, FVWM_COLORMAP is set too */
96 sscanf(getenv("FVWM_COLORMAP"), "%lx", &Pcmap);
97 Pdefault = False;
98 } else {
99 int screen = DefaultScreen(dpy);
101 Pvisual = DefaultVisual(dpy, screen);
102 Pdepth = DefaultDepth(dpy, screen);
103 Pcmap = DefaultColormap(dpy, screen);
104 Pdefault = True;
107 PictureSetupWhiteAndBlack();
108 PictureSaveFvwmVisual();
110 /* initialise color limit */
111 PUseDynamicColors = 0;
112 PictureInitColors(PICTURE_CALLED_BY_MODULE, True, NULL, False, True);
113 return;
116 void PictureInitCMapRoot(
117 Display *dpy, Bool init_color_limit, PictureColorLimitOption *opt,
118 Bool use_my_color_limit, Bool init_dither)
120 int screen = DefaultScreen(dpy);
122 Pdpy = dpy;
124 Pvisual = DefaultVisual(dpy, screen);
125 Pdepth = DefaultDepth(dpy, screen);
126 Pcmap = DefaultColormap(dpy, screen);
127 Pdefault = True;
129 PictureSetupWhiteAndBlack();
130 PictureSaveFvwmVisual();
132 /* initialise color limit */
133 PictureInitColors(
134 PICTURE_CALLED_BY_MODULE, init_color_limit, opt,
135 use_my_color_limit, init_dither);
136 return;
139 void PictureSetupWhiteAndBlack(void)
141 XColor c;
143 if (!Pdefault)
145 c.flags = DoRed|DoGreen|DoBlue;
146 c.red = c.green = c.blue = 65535;
147 XAllocColor(Pdpy, Pcmap, &c);
148 PWhitePixel = c.pixel;
149 c.red = c.green = c.blue = 0;
150 XAllocColor(Pdpy, Pcmap, &c);
151 PBlackPixel = c.pixel;
153 else
155 PWhitePixel = WhitePixel(Pdpy, DefaultScreen(Pdpy));
156 PBlackPixel = BlackPixel(Pdpy, DefaultScreen(Pdpy));
159 return;
162 void PictureUseDefaultVisual(void)
164 int screen = DefaultScreen(Pdpy);
166 Pvisual = DefaultVisual(Pdpy, screen);
167 Pdepth = DefaultDepth(Pdpy, screen);
168 Pcmap = DefaultColormap(Pdpy, screen);
169 PWhitePixel = WhitePixel(Pdpy, DefaultScreen(Pdpy));
170 PBlackPixel = BlackPixel(Pdpy, DefaultScreen(Pdpy));
171 return;
174 void PictureUseFvwmVisual(void)
176 Pvisual = FvwmVisual;
177 Pdepth = FvwmDepth;
178 Pcmap = FvwmCmap;
179 PWhitePixel = FvwmWhitePixel;
180 PBlackPixel = FvwmBlackPixel;
181 return;
184 void PictureSaveFvwmVisual(void)
186 FvwmVisual = Pvisual;
187 FvwmDepth = Pdepth;
188 FvwmCmap = Pcmap;
189 FvwmWhitePixel = PWhitePixel;
190 FvwmBlackPixel = PBlackPixel;
191 return;
194 Pixel PictureWhitePixel(void)
196 return PWhitePixel;
199 Pixel PictureBlackPixel(void)
201 return PBlackPixel;
204 GC PictureDefaultGC(Display *dpy, Window win)
206 static GC gc = None;
208 if (Pdepth == DefaultDepth(dpy, DefaultScreen(dpy)))
210 return DefaultGC(dpy, DefaultScreen(dpy));
212 if (gc == None)
214 XGCValues xgcv;
216 xgcv.foreground = PictureBlackPixel();
217 xgcv.background = PictureWhitePixel();
218 gc = fvwmlib_XCreateGC(dpy, win, 0, NULL);
221 return gc;
224 static char* imagePath = FVWM_IMAGEPATH;
226 void PictureSetImagePath( const char* newpath )
228 static int need_to_free = 0;
229 setPath( &imagePath, newpath, need_to_free );
230 need_to_free = 1;
232 return;
235 char* PictureGetImagePath(void)
237 return imagePath;
242 * Find the specified image file somewhere along the given path.
244 * There is a possible race condition here: We check the file and later
245 * do something with it. By then, the file might not be accessible.
246 * Oh well.
249 char* PictureFindImageFile(const char* icon, const char* pathlist, int type)
251 int length;
252 char *tmpbuf;
253 char *full_filename;
254 const char *render_opts;
256 if (pathlist == NULL)
258 pathlist = imagePath;
260 if (icon == NULL)
262 return NULL;
265 full_filename = searchPath(pathlist, icon, ".gz", type);
267 /* With USE_SVG, rendering options may be appended to the
268 original filename, hence seachPath() won't find the file.
269 So we hide any such appended options and try once more. */
270 if (USE_SVG && !full_filename &&
271 (render_opts = strrchr(icon, ':')))
273 length = render_opts - icon;
274 tmpbuf = (char *)safemalloc(length + 1);
275 strncpy(tmpbuf, icon, length);
276 tmpbuf[length] = 0;
278 full_filename = searchPath(pathlist, tmpbuf, ".gz", type);
279 free(tmpbuf);
280 if (full_filename)
282 /* Prepending (the previously appended) options
283 will leave any file suffix exposed. Callers
284 who want to access the file on disk will have
285 to remove these prepended options themselves.
286 The format is ":svg_opts:/path/to/file.svg". */
287 tmpbuf = CatString3(render_opts, ":", full_filename);
288 free(full_filename);
289 full_filename = safestrdup(tmpbuf);
293 return full_filename;