1 |
/*--------------------------------*-C-*---------------------------------* |
2 |
* File: xpm.C |
3 |
*----------------------------------------------------------------------* |
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 |
#include "xpm.intpro" /* PROTOS for internal routines */ |
28 |
|
29 |
#ifdef XPM_BACKGROUND |
30 |
|
31 |
/* |
32 |
* These GEOM strings indicate absolute size/position: |
33 |
* @ `WxH+X+Y' |
34 |
* @ `WxH+X' -> Y = X |
35 |
* @ `WxH' -> Y = X = 50 |
36 |
* @ `W+X+Y' -> H = W |
37 |
* @ `W+X' -> H = W, Y = X |
38 |
* @ `W' -> H = W, X = Y = 50 |
39 |
* @ `0xH' -> H *= H/100, X = Y = 50 (W unchanged) |
40 |
* @ `Wx0' -> W *= W/100, X = Y = 50 (H unchanged) |
41 |
* @ `=+X+Y' -> (H, W unchanged) |
42 |
* @ `=+X' -> Y = X (H, W unchanged) |
43 |
* |
44 |
* These GEOM strings adjust position relative to current position: |
45 |
* @ `+X+Y' |
46 |
* @ `+X' -> Y = X |
47 |
* |
48 |
* And this GEOM string is for querying current scale/position: |
49 |
* @ `?' |
50 |
*/ |
51 |
int |
52 |
rxvt_term::scale_pixmap (const char *geom) |
53 |
{ |
54 |
int flags, changed = 0; |
55 |
int x = 0, y = 0; |
56 |
unsigned int w = 0, h = 0; |
57 |
unsigned int n; |
58 |
char *p, *str; |
59 |
bgPixmap_t *bgpixmap = & (bgPixmap); |
60 |
|
61 |
#define MAXLEN_GEOM sizeof("[1000x1000+1000+1000]") |
62 |
|
63 |
if (geom == NULL) |
64 |
return 0; |
65 |
str = (char *)rxvt_malloc (MAXLEN_GEOM + 1); |
66 |
if (!STRCMP (geom, "?")) |
67 |
{ |
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 |
process_xterm_seq (XTerm_title, str, CHAR_ST); |
72 |
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 |
{ |
81 |
STRNCPY (str, geom, n); |
82 |
str[n] = '\0'; |
83 |
|
84 |
flags = XParseGeometry (str, &x, &y, &w, &h); |
85 |
if (!flags) |
86 |
{ |
87 |
flags |= WidthValue; |
88 |
w = 0; |
89 |
} /* default is tile */ |
90 |
if (flags & WidthValue) |
91 |
{ |
92 |
if (! (flags & XValue)) |
93 |
x = 50; |
94 |
if (! (flags & HeightValue)) |
95 |
h = w; |
96 |
if (w && !h) |
97 |
{ |
98 |
w = (bgpixmap->w * w) / 100; |
99 |
h = bgpixmap->h; |
100 |
} |
101 |
else if (h && !w) |
102 |
{ |
103 |
w = bgpixmap->w; |
104 |
h = (bgpixmap->h * h) / 100; |
105 |
} |
106 |
if (w > 1000) |
107 |
w = 1000; |
108 |
if (h > 1000) |
109 |
h = 1000; |
110 |
if (bgpixmap->w != (short)w) |
111 |
{ |
112 |
bgpixmap->w = (short)w; |
113 |
changed++; |
114 |
} |
115 |
if (bgpixmap->h != (short)h) |
116 |
{ |
117 |
bgpixmap->h = (short)h; |
118 |
changed++; |
119 |
} |
120 |
} |
121 |
if (! (flags & YValue)) |
122 |
{ |
123 |
if (flags & XNegative) |
124 |
flags |= YNegative; |
125 |
y = x; |
126 |
} |
127 |
|
128 |
if (! (flags & WidthValue) && geom[0] != '=') |
129 |
{ |
130 |
x += bgpixmap->x; |
131 |
y += bgpixmap->y; |
132 |
} |
133 |
else |
134 |
{ |
135 |
if (flags & XNegative) |
136 |
x += 100; |
137 |
if (flags & YNegative) |
138 |
y += 100; |
139 |
} |
140 |
MIN_IT (x, 100); |
141 |
MIN_IT (y, 100); |
142 |
MAX_IT (x, 0); |
143 |
MAX_IT (y, 0); |
144 |
if (bgpixmap->x != x) |
145 |
{ |
146 |
bgpixmap->x = x; |
147 |
changed++; |
148 |
} |
149 |
if (bgpixmap->y != y) |
150 |
{ |
151 |
bgpixmap->y = y; |
152 |
changed++; |
153 |
} |
154 |
} |
155 |
free (str); |
156 |
return changed; |
157 |
} |
158 |
|
159 |
void |
160 |
rxvt_term::resize_pixmap () |
161 |
{ |
162 |
XGCValues gcvalue; |
163 |
GC gc; |
164 |
unsigned int width = TermWin_TotalWidth (); |
165 |
unsigned int height = TermWin_TotalHeight (); |
166 |
|
167 |
if (TermWin.pixmap != None) |
168 |
XFreePixmap (display->display, TermWin.pixmap); |
169 |
|
170 |
if (bgPixmap.pixmap == None) |
171 |
{ /* So be it: I'm not using pixmaps */ |
172 |
TermWin.pixmap = None; |
173 |
if (! (Options & Opt_transparent) || am_transparent == 0) |
174 |
XSetWindowBackground (display->display, TermWin.vt, |
175 |
PixColors[Color_bg]); |
176 |
return; |
177 |
} |
178 |
|
179 |
gcvalue.foreground = PixColors[Color_bg]; |
180 |
gc = XCreateGC (display->display, TermWin.vt, GCForeground, &gcvalue); |
181 |
|
182 |
if (bgPixmap.pixmap != None) |
183 |
{ /* we have a specified pixmap */ |
184 |
unsigned int w = bgPixmap.w, h = bgPixmap.h, |
185 |
x = bgPixmap.x, y = bgPixmap.y; |
186 |
unsigned int xpmh = xpmAttr.height, |
187 |
xpmw = xpmAttr.width; |
188 |
|
189 |
/* |
190 |
* don't zoom pixmap too much nor expand really small pixmaps |
191 |
*/ |
192 |
if (w > 1000 || h > 1000) |
193 |
w = 1; |
194 |
else if (width > (10 * xpmw) |
195 |
|| height > (10 * xpmh)) |
196 |
w = 0; /* tile */ |
197 |
|
198 |
if (w == 0) |
199 |
{ |
200 |
/* basic X tiling - let the X server do it */ |
201 |
TermWin.pixmap = XCreatePixmap (display->display, TermWin.vt, |
202 |
xpmw, xpmh, |
203 |
(unsigned int)display->depth); |
204 |
XCopyArea (display->display, bgPixmap.pixmap, TermWin.pixmap, gc, |
205 |
0, 0, xpmw, xpmh, 0, 0); |
206 |
} |
207 |
else |
208 |
{ |
209 |
float incr, p; |
210 |
Pixmap tmp; |
211 |
|
212 |
TermWin.pixmap = XCreatePixmap (display->display, TermWin.vt, |
213 |
width, height, |
214 |
(unsigned int)display->depth); |
215 |
/* |
216 |
* horizontal scaling |
217 |
*/ |
218 |
rxvt_pixmap_incr (&w, &x, &incr, &p, width, xpmw); |
219 |
|
220 |
tmp = XCreatePixmap (display->display, TermWin.vt, |
221 |
width, xpmh, (unsigned int)display->depth); |
222 |
XFillRectangle (display->display, tmp, gc, 0, 0, width, |
223 |
xpmh); |
224 |
|
225 |
for ( /*nil */ ; x < w; x++, p += incr) |
226 |
{ |
227 |
if (p >= xpmw) |
228 |
p = 0; |
229 |
/* copy one column from the original pixmap to the tmp pixmap */ |
230 |
XCopyArea (display->display, bgPixmap.pixmap, tmp, gc, |
231 |
(int)p, 0, 1, xpmh, (int)x, 0); |
232 |
} |
233 |
|
234 |
/* |
235 |
* vertical scaling |
236 |
*/ |
237 |
rxvt_pixmap_incr (&h, &y, &incr, &p, height, xpmh); |
238 |
|
239 |
if (y > 0) |
240 |
XFillRectangle (display->display, TermWin.pixmap, gc, 0, 0, width, |
241 |
y); |
242 |
if (h < height) |
243 |
XFillRectangle (display->display, TermWin.pixmap, gc, 0, (int)h, |
244 |
width, height - h + 1); |
245 |
for ( /*nil */ ; y < h; y++, p += incr) |
246 |
{ |
247 |
if (p >= xpmh) |
248 |
p = 0; |
249 |
/* copy one row from the tmp pixmap to the main pixmap */ |
250 |
XCopyArea (display->display, tmp, TermWin.pixmap, gc, |
251 |
0, (int)p, width, 1, 0, (int)y); |
252 |
} |
253 |
XFreePixmap (display->display, tmp); |
254 |
} |
255 |
} |
256 |
|
257 |
XSetWindowBackgroundPixmap (display->display, TermWin.vt, TermWin.pixmap); |
258 |
XFreeGC (display->display, gc); |
259 |
am_transparent = 0; |
260 |
|
261 |
XClearWindow (display->display, TermWin.vt); |
262 |
|
263 |
XSync (display->display, False); |
264 |
} |
265 |
|
266 |
/* |
267 |
* Calculate tiling sizes and increments |
268 |
* At start, p == 0, incr == xpmwidthheight |
269 |
*/ |
270 |
/* INTPROTO */ |
271 |
static void |
272 |
rxvt_pixmap_incr (unsigned int *wh, unsigned int *xy, float *incr, float *p, unsigned int widthheight, unsigned int xpmwidthheight) |
273 |
{ |
274 |
unsigned int cwh, cxy; |
275 |
float cincr, cp; |
276 |
|
277 |
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 |
} |
296 |
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 |
} |
342 |
|
343 |
Pixmap |
344 |
rxvt_term::set_bgPixmap (const char *file) |
345 |
{ |
346 |
char *f; |
347 |
|
348 |
assert (file != NULL); |
349 |
|
350 |
if (bgPixmap.pixmap != None) |
351 |
{ |
352 |
XFreePixmap (display->display, bgPixmap.pixmap); |
353 |
bgPixmap.pixmap = None; |
354 |
} |
355 |
XSetWindowBackground (display->display, TermWin.vt, PixColors[Color_bg]); |
356 |
|
357 |
if (*file != '\0') |
358 |
{ |
359 |
/* XWindowAttributes attr; */ |
360 |
|
361 |
/* |
362 |
* we already have the required attributes |
363 |
*/ |
364 |
/* XGetWindowAttributes (display->display, TermWin.vt, &attr); */ |
365 |
|
366 |
xpmAttr.closeness = 30000; |
367 |
xpmAttr.colormap = display->cmap; |
368 |
xpmAttr.visual = display->visual; |
369 |
xpmAttr.depth = display->depth; |
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 (display->display, display->root, 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_warn ("couldn't load XPM file \"%.*s\", ignoring.", (p - file), file); |
387 |
} |
388 |
|
389 |
free (f); |
390 |
} |
391 |
|
392 |
resize_pixmap (); |
393 |
return bgPixmap.pixmap; |
394 |
} |
395 |
|
396 |
#endif /* XPM_BACKGROUND */ |