ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/xpm.C
Revision: 1.7
Committed: Sun Feb 1 01:34:41 2004 UTC (20 years, 3 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: after_astyle
Changes since 1.6: +300 -263 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*--------------------------------*-C-*---------------------------------*
2     * File: xpm.c
3     *----------------------------------------------------------------------*
4 pcg 1.7 * $Id: xpm.C,v 1.6 2004/01/31 02:15:02 pcg Exp $
5 pcg 1.1 *
6     * All portions of code are copyright by their respective author/s.
7     * Copyright (c) 1997 Carsten Haitzler <raster@zip.com.au>
8     * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de>
9     * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com>
10     *
11     * This program is free software; you can redistribute it and/or modify
12     * it under the terms of the GNU General Public License as published by
13     * the Free Software Foundation; either version 2 of the License, or
14     * (at your option) any later version.
15     *
16     * This program is distributed in the hope that it will be useful,
17     * but WITHOUT ANY WARRANTY; without even the implied warranty of
18     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19     * GNU General Public License for more details.
20     *
21     * You should have received a copy of the GNU General Public License
22     * along with this program; if not, write to the Free Software
23     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24     *---------------------------------------------------------------------*/
25    
26     #include "../config.h" /* NECESSARY */
27     #include "rxvt.h" /* NECESSARY */
28     #include "xpm.intpro" /* PROTOS for internal routines */
29    
30     #ifdef XPM_BACKGROUND
31    
32     /*
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 pcg 1.7 int flags, changed = 0;
56     int x = 0, y = 0;
57     unsigned int w = 0, h = 0;
58     unsigned int n;
59     char *p, *str;
60     bgPixmap_t *bgpixmap = &(bgPixmap);
61 pcg 1.1
62     #define MAXLEN_GEOM sizeof("[1000x1000+1000+1000]")
63    
64 pcg 1.7 if (geom == NULL)
65     return 0;
66     str = (char *)rxvt_malloc (MAXLEN_GEOM + 1);
67     if (!STRCMP(geom, "?"))
68     {
69     sprintf(str, "[%dx%d+%d+%d]", /* can't presume snprintf() ! */
70     min(bgpixmap->w, 9999), min(bgpixmap->h, 9999),
71     min(bgpixmap->x, 9999), min(bgpixmap->y, 9999));
72     xterm_seq (XTerm_title, str, CHAR_ST);
73     free(str);
74     return 0;
75 pcg 1.1 }
76    
77 pcg 1.7 if ((p = STRCHR(geom, ';')) == NULL)
78     p = STRCHR(geom, '\0');
79     n = (p - geom);
80     if (n <= MAXLEN_GEOM)
81     {
82     STRNCPY(str, geom, n);
83     str[n] = '\0';
84    
85     flags = XParseGeometry(str, &x, &y, &w, &h);
86     if (!flags)
87     {
88     flags |= WidthValue;
89     w = 0;
90     } /* default is tile */
91     if (flags & WidthValue)
92     {
93     if (!(flags & XValue))
94     x = 50;
95     if (!(flags & HeightValue))
96     h = w;
97     if (w && !h)
98     {
99     w = (bgpixmap->w * w) / 100;
100     h = bgpixmap->h;
101     }
102     else if (h && !w)
103     {
104     w = bgpixmap->w;
105     h = (bgpixmap->h * h) / 100;
106     }
107     if (w > 1000)
108     w = 1000;
109     if (h > 1000)
110     h = 1000;
111     if (bgpixmap->w != (short)w)
112     {
113     bgpixmap->w = (short)w;
114     changed++;
115     }
116     if (bgpixmap->h != (short)h)
117     {
118     bgpixmap->h = (short)h;
119     changed++;
120     }
121     }
122     if (!(flags & YValue))
123     {
124     if (flags & XNegative)
125     flags |= YNegative;
126     y = x;
127     }
128    
129     if (!(flags & WidthValue) && geom[0] != '=')
130     {
131     x += bgpixmap->x;
132     y += bgpixmap->y;
133     }
134     else
135     {
136     if (flags & XNegative)
137     x += 100;
138     if (flags & YNegative)
139     y += 100;
140     }
141     MIN_IT(x, 100);
142     MIN_IT(y, 100);
143     MAX_IT(x, 0);
144     MAX_IT(y, 0);
145     if (bgpixmap->x != x)
146     {
147     bgpixmap->x = x;
148     changed++;
149     }
150     if (bgpixmap->y != y)
151     {
152     bgpixmap->y = y;
153     changed++;
154     }
155 pcg 1.1 }
156 pcg 1.7 free(str);
157     return changed;
158 pcg 1.1 }
159    
160     void
161 pcg 1.5 rxvt_term::resize_pixmap ()
162 pcg 1.1 {
163 pcg 1.7 XGCValues gcvalue;
164     GC gc;
165     unsigned int width = TermWin_TotalWidth();
166     unsigned int height = TermWin_TotalHeight();
167    
168     if (TermWin.pixmap != None)
169     XFreePixmap(Xdisplay, TermWin.pixmap);
170    
171     if (bgPixmap.pixmap == None)
172     { /* So be it: I'm not using pixmaps */
173     TermWin.pixmap = None;
174     if (!(Options & Opt_transparent) || am_transparent == 0)
175     XSetWindowBackground(Xdisplay, TermWin.vt,
176     PixColors[Color_bg]);
177     return;
178 pcg 1.1 }
179    
180 pcg 1.7 gcvalue.foreground = PixColors[Color_bg];
181     gc = XCreateGC(Xdisplay, TermWin.vt, GCForeground, &gcvalue);
182 pcg 1.1
183 pcg 1.7 if (bgPixmap.pixmap != None)
184     { /* we have a specified pixmap */
185     unsigned int w = bgPixmap.w, h = bgPixmap.h,
186     x = bgPixmap.x, y = bgPixmap.y;
187     unsigned int xpmh = xpmAttr.height,
188     xpmw = xpmAttr.width;
189    
190     /*
191     * don't zoom pixmap too much nor expand really small pixmaps
192     */
193     if (w > 1000 || h > 1000)
194     w = 1;
195     else if (width > (10 * xpmw)
196     || height > (10 * xpmh))
197     w = 0; /* tile */
198    
199     if (w == 0)
200     {
201     /* basic X tiling - let the X server do it */
202     TermWin.pixmap = XCreatePixmap(Xdisplay, TermWin.vt,
203     xpmw, xpmh,
204     (unsigned int)XDEPTH);
205     XCopyArea(Xdisplay, bgPixmap.pixmap, TermWin.pixmap, gc,
206     0, 0, xpmw, xpmh, 0, 0);
207     }
208     else
209     {
210     float incr, p;
211     Pixmap tmp;
212    
213     TermWin.pixmap = XCreatePixmap(Xdisplay, TermWin.vt,
214     width, height,
215     (unsigned int)XDEPTH);
216     /*
217     * horizontal scaling
218     */
219     rxvt_pixmap_incr(&w, &x, &incr, &p, width, xpmw);
220    
221     tmp = XCreatePixmap(Xdisplay, TermWin.vt,
222     width, xpmh, (unsigned int)XDEPTH);
223     XFillRectangle(Xdisplay, tmp, gc, 0, 0, width,
224     xpmh);
225    
226     for ( /*nil */ ; x < w; x++, p += incr)
227     {
228     if (p >= xpmw)
229     p = 0;
230     /* copy one column from the original pixmap to the tmp pixmap */
231     XCopyArea(Xdisplay, bgPixmap.pixmap, tmp, gc,
232     (int)p, 0, 1, xpmh, (int)x, 0);
233     }
234    
235     /*
236     * vertical scaling
237     */
238     rxvt_pixmap_incr(&h, &y, &incr, &p, height, xpmh);
239    
240     if (y > 0)
241     XFillRectangle(Xdisplay, TermWin.pixmap, gc, 0, 0, width,
242     y);
243     if (h < height)
244     XFillRectangle(Xdisplay, TermWin.pixmap, gc, 0, (int)h,
245     width, height - h + 1);
246     for ( /*nil */ ; y < h; y++, p += incr)
247     {
248     if (p >= xpmh)
249     p = 0;
250     /* copy one row from the tmp pixmap to the main pixmap */
251     XCopyArea(Xdisplay, tmp, TermWin.pixmap, gc,
252     0, (int)p, width, 1, 0, (int)y);
253     }
254     XFreePixmap(Xdisplay, tmp);
255     }
256 pcg 1.1 }
257 pcg 1.7 XSetWindowBackgroundPixmap(Xdisplay, TermWin.vt, TermWin.pixmap);
258     XFreeGC(Xdisplay, gc);
259     am_transparent = 0;
260 pcg 1.1
261 pcg 1.7 XClearWindow(Xdisplay, TermWin.vt);
262 pcg 1.1
263 pcg 1.7 XSync(Xdisplay, False);
264 pcg 1.1 }
265    
266     /*
267     * Calculate tiling sizes and increments
268     * At start, p == 0, incr == xpmwidthheight
269     */
270 pcg 1.6 /* INTPROTO */
271     static void
272 pcg 1.1 rxvt_pixmap_incr(unsigned int *wh, unsigned int *xy, float *incr, float *p, unsigned int widthheight, unsigned int xpmwidthheight)
273     {
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 pcg 1.7 char *f;
347 pcg 1.1
348 pcg 1.7 assert(file != NULL);
349 pcg 1.1
350 pcg 1.7 if (bgPixmap.pixmap != None)
351     {
352     XFreePixmap(Xdisplay, bgPixmap.pixmap);
353     bgPixmap.pixmap = None;
354 pcg 1.1 }
355 pcg 1.7 XSetWindowBackground(Xdisplay, TermWin.vt, PixColors[Color_bg]);
356 pcg 1.1
357 pcg 1.7 if (*file != '\0')
358     {
359     /* XWindowAttributes attr; */
360    
361     /*
362     * we already have the required attributes
363     */
364     /* XGetWindowAttributes(Xdisplay, TermWin.vt, &attr); */
365    
366     xpmAttr.closeness = 30000;
367     xpmAttr.colormap = XCMAP;
368     xpmAttr.visual = XVISUAL;
369     xpmAttr.depth = XDEPTH;
370     xpmAttr.valuemask = (XpmCloseness | XpmColormap | XpmVisual |
371     XpmDepth | XpmSize | XpmReturnPixels);
372    
373     /* search environment variables here too */
374     f = (char *)rxvt_File_find(file, ".xpm", rs[Rs_path]);
375     if (f == NULL
376     || XpmReadFileToPixmap(Xdisplay, Xroot, f,
377     &bgPixmap.pixmap, NULL,
378     &xpmAttr))
379     {
380     char *p;
381    
382     /* semi-colon delimited */
383     if ((p = STRCHR(file, ';')) == NULL)
384     p = STRCHR(file, '\0');
385    
386     rxvt_print_error("couldn't load XPM file \"%.*s\"", (p - file),
387     file);
388     }
389     free(f);
390 pcg 1.1 }
391 pcg 1.7 resize_pixmap ();
392     return bgPixmap.pixmap;
393 pcg 1.1 }
394    
395     #endif /* XPM_BACKGROUND */