ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/xpm.C
Revision: 1.5
Committed: Sat Jan 31 00:20:21 2004 UTC (20 years, 4 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.4: +51 -55 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*--------------------------------*-C-*---------------------------------*
2     * File: xpm.c
3     *----------------------------------------------------------------------*
4 pcg 1.5 * $Id: xpm.C,v 1.4 2003/12/18 02:07:12 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     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 pcg 1.5 bgPixmap_t *bgpixmap = &(bgPixmap);
61 pcg 1.1
62     #define MAXLEN_GEOM sizeof("[1000x1000+1000+1000]")
63    
64     if (geom == NULL)
65     return 0;
66 pcg 1.4 str = (char *)rxvt_malloc (MAXLEN_GEOM + 1);
67 pcg 1.1 if (!STRCMP(geom, "?")) {
68     sprintf(str, "[%dx%d+%d+%d]", /* can't presume snprintf() ! */
69     min(bgpixmap->w, 9999), min(bgpixmap->h, 9999),
70     min(bgpixmap->x, 9999), min(bgpixmap->y, 9999));
71 pcg 1.5 xterm_seq (XTerm_title, str, CHAR_ST);
72 pcg 1.1 free(str);
73     return 0;
74     }
75    
76     if ((p = STRCHR(geom, ';')) == NULL)
77     p = STRCHR(geom, '\0');
78     n = (p - geom);
79     if (n <= MAXLEN_GEOM) {
80     STRNCPY(str, geom, n);
81     str[n] = '\0';
82    
83     flags = XParseGeometry(str, &x, &y, &w, &h);
84     if (!flags) {
85     flags |= WidthValue;
86     w = 0;
87     } /* default is tile */
88     if (flags & WidthValue) {
89     if (!(flags & XValue))
90     x = 50;
91     if (!(flags & HeightValue))
92     h = w;
93     if (w && !h) {
94     w = (bgpixmap->w * w) / 100;
95     h = bgpixmap->h;
96     } else if (h && !w) {
97     w = bgpixmap->w;
98     h = (bgpixmap->h * h) / 100;
99     }
100     if (w > 1000)
101     w = 1000;
102     if (h > 1000)
103     h = 1000;
104     if (bgpixmap->w != (short)w) {
105     bgpixmap->w = (short)w;
106     changed++;
107     }
108     if (bgpixmap->h != (short)h) {
109     bgpixmap->h = (short)h;
110     changed++;
111     }
112     }
113     if (!(flags & YValue)) {
114     if (flags & XNegative)
115     flags |= YNegative;
116     y = x;
117     }
118    
119     if (!(flags & WidthValue) && geom[0] != '=') {
120     x += bgpixmap->x;
121     y += bgpixmap->y;
122     } else {
123     if (flags & XNegative)
124     x += 100;
125     if (flags & YNegative)
126     y += 100;
127     }
128     MIN_IT(x, 100);
129     MIN_IT(y, 100);
130     MAX_IT(x, 0);
131     MAX_IT(y, 0);
132     if (bgpixmap->x != x) {
133     bgpixmap->x = x;
134     changed++;
135     }
136     if (bgpixmap->y != y) {
137     bgpixmap->y = y;
138     changed++;
139     }
140     }
141     free(str);
142     return changed;
143     }
144    
145     void
146 pcg 1.5 rxvt_term::resize_pixmap ()
147 pcg 1.1 {
148     XGCValues gcvalue;
149     GC gc;
150     unsigned int width = TermWin_TotalWidth();
151     unsigned int height = TermWin_TotalHeight();
152    
153 pcg 1.5 if (TermWin.pixmap != None)
154     XFreePixmap(Xdisplay, TermWin.pixmap);
155 pcg 1.1
156 pcg 1.5 if (bgPixmap.pixmap == None) { /* So be it: I'm not using pixmaps */
157     TermWin.pixmap = None;
158     if (!(Options & Opt_transparent) || am_transparent == 0)
159     XSetWindowBackground(Xdisplay, TermWin.vt,
160     PixColors[Color_bg]);
161 pcg 1.1 return;
162     }
163    
164 pcg 1.5 gcvalue.foreground = PixColors[Color_bg];
165     gc = XCreateGC(Xdisplay, TermWin.vt, GCForeground, &gcvalue);
166 pcg 1.1
167 pcg 1.5 if (bgPixmap.pixmap != None) { /* we have a specified pixmap */
168     unsigned int w = bgPixmap.w, h = bgPixmap.h,
169     x = bgPixmap.x, y = bgPixmap.y;
170     unsigned int xpmh = xpmAttr.height,
171     xpmw = xpmAttr.width;
172 pcg 1.1
173     /*
174     * don't zoom pixmap too much nor expand really small pixmaps
175     */
176     if (w > 1000 || h > 1000)
177     w = 1;
178     else if (width > (10 * xpmw)
179     || height > (10 * xpmh))
180     w = 0; /* tile */
181    
182     if (w == 0) {
183     /* basic X tiling - let the X server do it */
184 pcg 1.5 TermWin.pixmap = XCreatePixmap(Xdisplay, TermWin.vt,
185 pcg 1.1 xpmw, xpmh,
186     (unsigned int)XDEPTH);
187 pcg 1.5 XCopyArea(Xdisplay, bgPixmap.pixmap, TermWin.pixmap, gc,
188 pcg 1.1 0, 0, xpmw, xpmh, 0, 0);
189     } else {
190     float incr, p;
191     Pixmap tmp;
192    
193 pcg 1.5 TermWin.pixmap = XCreatePixmap(Xdisplay, TermWin.vt,
194 pcg 1.1 width, height,
195     (unsigned int)XDEPTH);
196     /*
197     * horizontal scaling
198     */
199     rxvt_pixmap_incr(&w, &x, &incr, &p, width, xpmw);
200    
201 pcg 1.5 tmp = XCreatePixmap(Xdisplay, TermWin.vt,
202 pcg 1.1 width, xpmh, (unsigned int)XDEPTH);
203 pcg 1.5 XFillRectangle(Xdisplay, tmp, gc, 0, 0, width,
204 pcg 1.1 xpmh);
205    
206     for ( /*nil */ ; x < w; x++, p += incr) {
207     if (p >= xpmw)
208     p = 0;
209     /* copy one column from the original pixmap to the tmp pixmap */
210 pcg 1.5 XCopyArea(Xdisplay, bgPixmap.pixmap, tmp, gc,
211 pcg 1.1 (int)p, 0, 1, xpmh, (int)x, 0);
212     }
213    
214     /*
215     * vertical scaling
216     */
217     rxvt_pixmap_incr(&h, &y, &incr, &p, height, xpmh);
218    
219     if (y > 0)
220 pcg 1.5 XFillRectangle(Xdisplay, TermWin.pixmap, gc, 0, 0, width,
221 pcg 1.1 y);
222     if (h < height)
223 pcg 1.5 XFillRectangle(Xdisplay, TermWin.pixmap, gc, 0, (int)h,
224 pcg 1.1 width, height - h + 1);
225     for ( /*nil */ ; y < h; y++, p += incr) {
226     if (p >= xpmh)
227     p = 0;
228     /* copy one row from the tmp pixmap to the main pixmap */
229 pcg 1.5 XCopyArea(Xdisplay, tmp, TermWin.pixmap, gc,
230 pcg 1.1 0, (int)p, width, 1, 0, (int)y);
231     }
232 pcg 1.5 XFreePixmap(Xdisplay, tmp);
233 pcg 1.1 }
234     }
235 pcg 1.5 XSetWindowBackgroundPixmap(Xdisplay, TermWin.vt, TermWin.pixmap);
236     XFreeGC(Xdisplay, gc);
237     am_transparent = 0;
238 pcg 1.1
239 pcg 1.5 XClearWindow(Xdisplay, TermWin.vt);
240 pcg 1.1
241 pcg 1.5 XSync(Xdisplay, False);
242 pcg 1.1 }
243    
244     /*
245     * Calculate tiling sizes and increments
246     * At start, p == 0, incr == xpmwidthheight
247     */
248     void
249     rxvt_pixmap_incr(unsigned int *wh, unsigned int *xy, float *incr, float *p, unsigned int widthheight, unsigned int xpmwidthheight)
250     {
251     unsigned int cwh, cxy;
252     float cincr, cp;
253    
254     cp = 0;
255     cincr = (float)xpmwidthheight;
256     cxy = *xy;
257     cwh = *wh;
258     if (cwh == 1) { /* display one image, no horizontal/vertical scaling */
259     cincr = (float)widthheight;
260     if (xpmwidthheight <= widthheight) {
261     cwh = xpmwidthheight;
262     cxy = (cxy * (widthheight - cwh)) / 100; /* beware! order */
263     cwh += cxy;
264     } else {
265     cxy = 0;
266     cwh = widthheight;
267     }
268     } else if (cwh < 10) { /* fit WH images across/down screen */
269     cincr *= cwh;
270     cxy = 0;
271     cwh = widthheight;
272     } else {
273     cincr *= 100.0 / cwh;
274     if (cwh < 100) { /* contract */
275     float pos;
276    
277     cwh = (cwh * widthheight) / 100;
278     pos = (float)cxy / 100 * widthheight - (cwh / 2);
279    
280     cxy = (widthheight - cwh);
281     if (pos <= 0)
282     cxy = 0;
283     else if (pos < cxy)
284 pcg 1.4 cxy = (int) pos;
285 pcg 1.1 cwh += cxy;
286     } else { /* expand */
287     if (cxy > 0) { /* position */
288     float pos;
289    
290     pos = (float)cxy / 100 * xpmwidthheight - (cincr / 2);
291     cp = xpmwidthheight - cincr;
292     if (pos <= 0)
293     cp = 0;
294     else if (pos < cp)
295     cp = pos;
296     }
297     cxy = 0;
298     cwh = widthheight;
299     }
300     }
301     cincr /= widthheight;
302     *wh = cwh;
303     *xy = cxy;
304     *incr = cincr;
305     *p = cp;
306     }
307    
308     Pixmap
309 pcg 1.5 rxvt_term::set_bgPixmap (const char *file)
310 pcg 1.1 {
311     char *f;
312    
313     assert(file != NULL);
314    
315 pcg 1.5 if (bgPixmap.pixmap != None) {
316     XFreePixmap(Xdisplay, bgPixmap.pixmap);
317     bgPixmap.pixmap = None;
318 pcg 1.1 }
319 pcg 1.5 XSetWindowBackground(Xdisplay, TermWin.vt, PixColors[Color_bg]);
320 pcg 1.1
321     if (*file != '\0') {
322     /* XWindowAttributes attr; */
323    
324     /*
325     * we already have the required attributes
326     */
327 pcg 1.5 /* XGetWindowAttributes(Xdisplay, TermWin.vt, &attr); */
328 pcg 1.1
329 pcg 1.5 xpmAttr.closeness = 30000;
330     xpmAttr.colormap = XCMAP;
331     xpmAttr.visual = XVISUAL;
332     xpmAttr.depth = XDEPTH;
333     xpmAttr.valuemask = (XpmCloseness | XpmColormap | XpmVisual |
334 pcg 1.1 XpmDepth | XpmSize | XpmReturnPixels);
335    
336     /* search environment variables here too */
337 pcg 1.5 f = (char *)rxvt_File_find(file, ".xpm", rs[Rs_path]);
338 pcg 1.1 if (f == NULL
339 pcg 1.5 || XpmReadFileToPixmap(Xdisplay, Xroot, f,
340     &bgPixmap.pixmap, NULL,
341     &xpmAttr)) {
342 pcg 1.1 char *p;
343    
344     /* semi-colon delimited */
345     if ((p = STRCHR(file, ';')) == NULL)
346     p = STRCHR(file, '\0');
347    
348     rxvt_print_error("couldn't load XPM file \"%.*s\"", (p - file),
349     file);
350     }
351     free(f);
352     }
353 pcg 1.5 resize_pixmap ();
354     return bgPixmap.pixmap;
355 pcg 1.1 }
356    
357     #endif /* XPM_BACKGROUND */