ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/xpm.C
Revision: 1.32
Committed: Sat Feb 18 15:24:39 2006 UTC (18 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.31: +0 -6 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*--------------------------------*-C-*---------------------------------*
2 pcg 1.12 * File: xpm.C
3 pcg 1.1 *----------------------------------------------------------------------*
4     *
5     * All portions of code are copyright by their respective author/s.
6     * Copyright (c) 1997 Carsten Haitzler <raster@zip.com.au>
7     * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de>
8     * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com>
9     *
10     * This program is free software; you can redistribute it and/or modify
11     * it under the terms of the GNU General Public License as published by
12     * the Free Software Foundation; either version 2 of the License, or
13     * (at your option) any later version.
14     *
15     * This program is distributed in the hope that it will be useful,
16     * but WITHOUT ANY WARRANTY; without even the implied warranty of
17     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18     * GNU General Public License for more details.
19     *
20     * You should have received a copy of the GNU General Public License
21     * along with this program; if not, write to the Free Software
22     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23     *---------------------------------------------------------------------*/
24    
25     #include "../config.h" /* NECESSARY */
26     #include "rxvt.h" /* NECESSARY */
27    
28     #ifdef XPM_BACKGROUND
29    
30 root 1.22 static void rxvt_pixmap_incr (unsigned int *wh, unsigned int *xy, float *incr, float *p, unsigned int widthheight, unsigned int xpmwidthheight);
31    
32 pcg 1.1 /*
33     * These GEOM strings indicate absolute size/position:
34     * @ `WxH+X+Y'
35     * @ `WxH+X' -> Y = X
36     * @ `WxH' -> Y = X = 50
37     * @ `W+X+Y' -> H = W
38     * @ `W+X' -> H = W, Y = X
39     * @ `W' -> H = W, X = Y = 50
40     * @ `0xH' -> H *= H/100, X = Y = 50 (W unchanged)
41     * @ `Wx0' -> W *= W/100, X = Y = 50 (H unchanged)
42     * @ `=+X+Y' -> (H, W unchanged)
43     * @ `=+X' -> Y = X (H, W unchanged)
44     *
45     * These GEOM strings adjust position relative to current position:
46     * @ `+X+Y'
47     * @ `+X' -> Y = X
48     *
49     * And this GEOM string is for querying current scale/position:
50     * @ `?'
51     */
52     int
53 pcg 1.5 rxvt_term::scale_pixmap (const char *geom)
54 pcg 1.1 {
55 root 1.28 int flags, changed = 0;
56     int x = 0, y = 0;
57     unsigned int w = 0, h = 0;
58     unsigned int n;
59     char *p;
60     bgPixmap_t *bgpixmap = & (bgPixmap);
61 pcg 1.1
62 root 1.28 #define MAXLEN_GEOM sizeof("[10000x10000+10000+10000]")
63 pcg 1.1
64 pcg 1.7 if (geom == NULL)
65     return 0;
66 root 1.28
67     char str[MAXLEN_GEOM];
68    
69 root 1.17 if (!strcmp (geom, "?"))
70 pcg 1.7 {
71 pcg 1.9 sprintf (str, "[%dx%d+%d+%d]", /* can't presume snprintf () ! */
72 root 1.28 min (bgpixmap->w, 32767), min (bgpixmap->h, 32767),
73     min (bgpixmap->x, 32767), min (bgpixmap->y, 32767));
74 pcg 1.11 process_xterm_seq (XTerm_title, str, CHAR_ST);
75 pcg 1.7 return 0;
76 pcg 1.1 }
77    
78 root 1.17 if ((p = strchr (geom, ';')) == NULL)
79     p = strchr (geom, '\0');
80 root 1.28
81 pcg 1.7 n = (p - geom);
82     if (n <= MAXLEN_GEOM)
83     {
84 root 1.17 strncpy (str, geom, n);
85 pcg 1.7 str[n] = '\0';
86    
87 pcg 1.9 flags = XParseGeometry (str, &x, &y, &w, &h);
88 root 1.28
89 pcg 1.7 if (!flags)
90     {
91     flags |= WidthValue;
92     w = 0;
93     } /* default is tile */
94 root 1.28
95 pcg 1.7 if (flags & WidthValue)
96     {
97 root 1.28 if (!(flags & XValue))
98 pcg 1.7 x = 50;
99 root 1.28
100     if (!(flags & HeightValue))
101 pcg 1.7 h = w;
102 root 1.28
103 pcg 1.7 if (w && !h)
104     {
105     w = (bgpixmap->w * w) / 100;
106     h = bgpixmap->h;
107     }
108     else if (h && !w)
109     {
110     w = bgpixmap->w;
111     h = (bgpixmap->h * h) / 100;
112     }
113 root 1.28
114     min_it (w, 32767);
115     min_it (h, 32767);
116    
117 pcg 1.7 if (bgpixmap->w != (short)w)
118     {
119     bgpixmap->w = (short)w;
120     changed++;
121     }
122 root 1.28
123 pcg 1.7 if (bgpixmap->h != (short)h)
124     {
125     bgpixmap->h = (short)h;
126     changed++;
127     }
128     }
129 root 1.28
130     if (!(flags & YValue))
131 pcg 1.7 {
132     if (flags & XNegative)
133     flags |= YNegative;
134 root 1.28
135 pcg 1.7 y = x;
136     }
137    
138 root 1.28 if (!(flags & WidthValue) && geom[0] != '=')
139 pcg 1.7 {
140     x += bgpixmap->x;
141     y += bgpixmap->y;
142     }
143 root 1.28
144     if (xpmAttr.width && xpmAttr.height)
145 pcg 1.7 {
146 root 1.28 x = MOD(x, xpmAttr.width);
147     y = MOD(y, xpmAttr.height);
148     }
149    
150 pcg 1.7 if (bgpixmap->x != x)
151     {
152     bgpixmap->x = x;
153     changed++;
154     }
155 root 1.28
156 pcg 1.7 if (bgpixmap->y != y)
157     {
158     bgpixmap->y = y;
159     changed++;
160     }
161 pcg 1.1 }
162 root 1.28
163 pcg 1.7 return changed;
164 pcg 1.1 }
165    
166     void
167 pcg 1.5 rxvt_term::resize_pixmap ()
168 pcg 1.1 {
169 root 1.27 XGCValues gcvalue;
170     GC gc;
171 pcg 1.7
172 root 1.24 if (pixmap != None)
173 root 1.31 XFreePixmap (dpy, pixmap);
174 pcg 1.7
175     if (bgPixmap.pixmap == None)
176     { /* So be it: I'm not using pixmaps */
177 root 1.24 pixmap = None;
178 root 1.20
179 root 1.26 if (!OPTION (Opt_transparent) || !am_transparent)
180 root 1.31 XSetWindowBackground (dpy, vt, pix_colors[Color_bg]);
181 root 1.20
182 pcg 1.7 return;
183 pcg 1.1 }
184    
185 root 1.17 gcvalue.foreground = pix_colors[Color_bg];
186 root 1.31 gc = XCreateGC (dpy, vt, GCForeground, &gcvalue);
187 pcg 1.1
188 pcg 1.7 if (bgPixmap.pixmap != None)
189     { /* we have a specified pixmap */
190 root 1.28 unsigned int w = bgPixmap.w, h = bgPixmap.h,
191     x = bgPixmap.x, y = bgPixmap.y;
192     unsigned int xpmh = xpmAttr.height,
193     xpmw = xpmAttr.width;
194 pcg 1.7
195     /*
196     * don't zoom pixmap too much nor expand really small pixmaps
197     */
198 root 1.28 if (w > 32767 || h > 32767)
199 pcg 1.7 w = 1;
200     else if (width > (10 * xpmw)
201     || height > (10 * xpmh))
202     w = 0; /* tile */
203    
204 root 1.28 if (!w)
205 pcg 1.7 {
206     /* basic X tiling - let the X server do it */
207 root 1.31 pixmap = XCreatePixmap (dpy, vt, xpmw, xpmh, depth);
208 root 1.28
209 root 1.31 XCopyArea (dpy, bgPixmap.pixmap, pixmap, gc, x, y, xpmw - x, xpmh - y, 0, 0);
210     XCopyArea (dpy, bgPixmap.pixmap, pixmap, gc, x, 0, xpmw - x, y, 0, xpmh - y);
211     XCopyArea (dpy, bgPixmap.pixmap, pixmap, gc, 0, y, x, xpmh - y, xpmw - x, 0);
212     XCopyArea (dpy, bgPixmap.pixmap, pixmap, gc, 0, 0, x, y, xpmw - x, xpmh - y);
213 pcg 1.7 }
214     else
215     {
216 root 1.27 float incr, p;
217     Pixmap tmp;
218 pcg 1.7
219 root 1.31 pixmap = XCreatePixmap (dpy, vt, width, height, depth);
220 pcg 1.7 /*
221     * horizontal scaling
222     */
223 pcg 1.9 rxvt_pixmap_incr (&w, &x, &incr, &p, width, xpmw);
224 pcg 1.7
225 root 1.31 tmp = XCreatePixmap (dpy, vt, width, xpmh, depth);
226     XFillRectangle (dpy, tmp, gc, 0, 0, width, xpmh);
227 pcg 1.7
228     for ( /*nil */ ; x < w; x++, p += incr)
229     {
230     if (p >= xpmw)
231     p = 0;
232 root 1.28
233 pcg 1.7 /* copy one column from the original pixmap to the tmp pixmap */
234 root 1.31 XCopyArea (dpy, bgPixmap.pixmap, tmp, gc, (int)p, 0, 1, xpmh, (int)x, 0);
235 pcg 1.7 }
236    
237     /*
238     * vertical scaling
239     */
240 pcg 1.9 rxvt_pixmap_incr (&h, &y, &incr, &p, height, xpmh);
241 pcg 1.7
242     if (y > 0)
243 root 1.31 XFillRectangle (dpy, pixmap, gc, 0, 0, width, y);
244 root 1.20
245 pcg 1.7 if (h < height)
246 root 1.31 XFillRectangle (dpy, pixmap, gc, 0, (int)h, width, height - h + 1);
247 root 1.20
248 pcg 1.7 for ( /*nil */ ; y < h; y++, p += incr)
249     {
250     if (p >= xpmh)
251     p = 0;
252 root 1.20
253 pcg 1.7 /* copy one row from the tmp pixmap to the main pixmap */
254 root 1.31 XCopyArea (dpy, tmp, pixmap, gc, 0, (int)p, width, 1, 0, (int)y);
255 pcg 1.7 }
256 root 1.20
257 root 1.31 XFreePixmap (dpy, tmp);
258 pcg 1.7 }
259 pcg 1.1 }
260 root 1.14
261 root 1.31 XSetWindowBackgroundPixmap (dpy, vt, pixmap);
262 root 1.28
263 root 1.31 XFreeGC (dpy, gc);
264 pcg 1.7 am_transparent = 0;
265 pcg 1.1 }
266    
267     /*
268     * Calculate tiling sizes and increments
269     * At start, p == 0, incr == xpmwidthheight
270     */
271 pcg 1.6 static void
272 pcg 1.9 rxvt_pixmap_incr (unsigned int *wh, unsigned int *xy, float *incr, float *p, unsigned int widthheight, unsigned int xpmwidthheight)
273 pcg 1.1 {
274 pcg 1.7 unsigned int cwh, cxy;
275     float cincr, cp;
276 pcg 1.1
277 pcg 1.7 cp = 0;
278     cincr = (float)xpmwidthheight;
279     cxy = *xy;
280     cwh = *wh;
281     if (cwh == 1)
282     { /* display one image, no horizontal/vertical scaling */
283     cincr = (float)widthheight;
284     if (xpmwidthheight <= widthheight)
285     {
286     cwh = xpmwidthheight;
287     cxy = (cxy * (widthheight - cwh)) / 100; /* beware! order */
288     cwh += cxy;
289     }
290     else
291     {
292     cxy = 0;
293     cwh = widthheight;
294     }
295 pcg 1.1 }
296 pcg 1.7 else if (cwh < 10)
297     { /* fit WH images across/down screen */
298     cincr *= cwh;
299     cxy = 0;
300     cwh = widthheight;
301     }
302     else
303     {
304     cincr *= 100.0 / cwh;
305     if (cwh < 100)
306     { /* contract */
307     float pos;
308    
309     cwh = (cwh * widthheight) / 100;
310     pos = (float)cxy / 100 * widthheight - (cwh / 2);
311    
312     cxy = (widthheight - cwh);
313     if (pos <= 0)
314     cxy = 0;
315     else if (pos < cxy)
316     cxy = (int) pos;
317     cwh += cxy;
318     }
319     else
320     { /* expand */
321     if (cxy > 0)
322     { /* position */
323     float pos;
324    
325     pos = (float)cxy / 100 * xpmwidthheight - (cincr / 2);
326     cp = xpmwidthheight - cincr;
327     if (pos <= 0)
328     cp = 0;
329     else if (pos < cp)
330     cp = pos;
331     }
332     cxy = 0;
333     cwh = widthheight;
334     }
335     }
336     cincr /= widthheight;
337     *wh = cwh;
338     *xy = cxy;
339     *incr = cincr;
340     *p = cp;
341 pcg 1.1 }
342    
343     Pixmap
344 pcg 1.5 rxvt_term::set_bgPixmap (const char *file)
345 pcg 1.1 {
346 root 1.21 char *f;
347 pcg 1.1
348 pcg 1.9 assert (file != NULL);
349 pcg 1.1
350 pcg 1.7 if (bgPixmap.pixmap != None)
351     {
352 root 1.31 XFreePixmap (dpy, bgPixmap.pixmap);
353 pcg 1.7 bgPixmap.pixmap = None;
354 pcg 1.1 }
355 root 1.19
356 root 1.31 XSetWindowBackground (dpy, vt, pix_colors[Color_bg]);
357 pcg 1.1
358 pcg 1.7 if (*file != '\0')
359     {
360     /* XWindowAttributes attr; */
361    
362     /*
363     * we already have the required attributes
364     */
365 root 1.31 /* XGetWindowAttributes (dpy, vt, &attr); */
366 pcg 1.7
367     xpmAttr.closeness = 30000;
368 root 1.29 xpmAttr.colormap = cmap;
369     xpmAttr.visual = visual;
370     xpmAttr.depth = depth;
371     xpmAttr.valuemask = (XpmCloseness | XpmColormap | XpmVisual
372     | XpmDepth | XpmSize | XpmReturnPixels);
373 pcg 1.7
374     /* search environment variables here too */
375 pcg 1.9 f = (char *)rxvt_File_find (file, ".xpm", rs[Rs_path]);
376 pcg 1.7 if (f == NULL
377 root 1.31 || XpmReadFileToPixmap (dpy, display->root, f,
378 root 1.29 &bgPixmap.pixmap, NULL,
379     &xpmAttr))
380 pcg 1.7 {
381 pcg 1.10 char *p;
382 pcg 1.7
383     /* semi-colon delimited */
384 root 1.17 if ((p = strchr (file, ';')) == NULL)
385     p = strchr (file, '\0');
386 pcg 1.7
387 root 1.19 rxvt_warn ("couldn't load XPM file \"%.*s\", ignoring.\n", (p - file), file);
388 pcg 1.7 }
389 pcg 1.10
390 pcg 1.9 free (f);
391 pcg 1.1 }
392 pcg 1.10
393 pcg 1.7 resize_pixmap ();
394     return bgPixmap.pixmap;
395 pcg 1.1 }
396    
397     #endif /* XPM_BACKGROUND */