ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/xpm.C
Revision: 1.6
Committed: Sat Jan 31 02:15:02 2004 UTC (20 years, 3 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: before_astyle
Changes since 1.5: +3 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*--------------------------------*-C-*---------------------------------*
2     * File: xpm.c
3     *----------------------------------------------------------------------*
4 pcg 1.6 * $Id: xpm.C,v 1.5 2004/01/31 00:20:21 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 pcg 1.6 /* INTPROTO */
249     static void
250 pcg 1.1 rxvt_pixmap_incr(unsigned int *wh, unsigned int *xy, float *incr, float *p, unsigned int widthheight, unsigned int xpmwidthheight)
251     {
252     unsigned int cwh, cxy;
253     float cincr, cp;
254    
255     cp = 0;
256     cincr = (float)xpmwidthheight;
257     cxy = *xy;
258     cwh = *wh;
259     if (cwh == 1) { /* display one image, no horizontal/vertical scaling */
260     cincr = (float)widthheight;
261     if (xpmwidthheight <= widthheight) {
262     cwh = xpmwidthheight;
263     cxy = (cxy * (widthheight - cwh)) / 100; /* beware! order */
264     cwh += cxy;
265     } else {
266     cxy = 0;
267     cwh = widthheight;
268     }
269     } else if (cwh < 10) { /* fit WH images across/down screen */
270     cincr *= cwh;
271     cxy = 0;
272     cwh = widthheight;
273     } else {
274     cincr *= 100.0 / cwh;
275     if (cwh < 100) { /* contract */
276     float pos;
277    
278     cwh = (cwh * widthheight) / 100;
279     pos = (float)cxy / 100 * widthheight - (cwh / 2);
280    
281     cxy = (widthheight - cwh);
282     if (pos <= 0)
283     cxy = 0;
284     else if (pos < cxy)
285 pcg 1.4 cxy = (int) pos;
286 pcg 1.1 cwh += cxy;
287     } else { /* expand */
288     if (cxy > 0) { /* position */
289     float pos;
290    
291     pos = (float)cxy / 100 * xpmwidthheight - (cincr / 2);
292     cp = xpmwidthheight - cincr;
293     if (pos <= 0)
294     cp = 0;
295     else if (pos < cp)
296     cp = pos;
297     }
298     cxy = 0;
299     cwh = widthheight;
300     }
301     }
302     cincr /= widthheight;
303     *wh = cwh;
304     *xy = cxy;
305     *incr = cincr;
306     *p = cp;
307     }
308    
309     Pixmap
310 pcg 1.5 rxvt_term::set_bgPixmap (const char *file)
311 pcg 1.1 {
312     char *f;
313    
314     assert(file != NULL);
315    
316 pcg 1.5 if (bgPixmap.pixmap != None) {
317     XFreePixmap(Xdisplay, bgPixmap.pixmap);
318     bgPixmap.pixmap = None;
319 pcg 1.1 }
320 pcg 1.5 XSetWindowBackground(Xdisplay, TermWin.vt, PixColors[Color_bg]);
321 pcg 1.1
322     if (*file != '\0') {
323     /* XWindowAttributes attr; */
324    
325     /*
326     * we already have the required attributes
327     */
328 pcg 1.5 /* XGetWindowAttributes(Xdisplay, TermWin.vt, &attr); */
329 pcg 1.1
330 pcg 1.5 xpmAttr.closeness = 30000;
331     xpmAttr.colormap = XCMAP;
332     xpmAttr.visual = XVISUAL;
333     xpmAttr.depth = XDEPTH;
334     xpmAttr.valuemask = (XpmCloseness | XpmColormap | XpmVisual |
335 pcg 1.1 XpmDepth | XpmSize | XpmReturnPixels);
336    
337     /* search environment variables here too */
338 pcg 1.5 f = (char *)rxvt_File_find(file, ".xpm", rs[Rs_path]);
339 pcg 1.1 if (f == NULL
340 pcg 1.5 || XpmReadFileToPixmap(Xdisplay, Xroot, f,
341     &bgPixmap.pixmap, NULL,
342     &xpmAttr)) {
343 pcg 1.1 char *p;
344    
345     /* semi-colon delimited */
346     if ((p = STRCHR(file, ';')) == NULL)
347     p = STRCHR(file, '\0');
348    
349     rxvt_print_error("couldn't load XPM file \"%.*s\"", (p - file),
350     file);
351     }
352     free(f);
353     }
354 pcg 1.5 resize_pixmap ();
355     return bgPixmap.pixmap;
356 pcg 1.1 }
357    
358     #endif /* XPM_BACKGROUND */