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 |
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 |
pcg |
1.9 |
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 |
root |
1.17 |
if (!strcmp (geom, "?")) |
68 |
pcg |
1.7 |
{ |
69 |
pcg |
1.9 |
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 |
pcg |
1.11 |
process_xterm_seq (XTerm_title, str, CHAR_ST); |
73 |
pcg |
1.9 |
free (str); |
74 |
pcg |
1.7 |
return 0; |
75 |
pcg |
1.1 |
} |
76 |
|
|
|
77 |
root |
1.17 |
if ((p = strchr (geom, ';')) == NULL) |
78 |
|
|
p = strchr (geom, '\0'); |
79 |
pcg |
1.7 |
n = (p - geom); |
80 |
|
|
if (n <= MAXLEN_GEOM) |
81 |
|
|
{ |
82 |
root |
1.17 |
strncpy (str, geom, n); |
83 |
pcg |
1.7 |
str[n] = '\0'; |
84 |
|
|
|
85 |
pcg |
1.9 |
flags = XParseGeometry (str, &x, &y, &w, &h); |
86 |
pcg |
1.7 |
if (!flags) |
87 |
|
|
{ |
88 |
|
|
flags |= WidthValue; |
89 |
|
|
w = 0; |
90 |
|
|
} /* default is tile */ |
91 |
|
|
if (flags & WidthValue) |
92 |
|
|
{ |
93 |
pcg |
1.9 |
if (! (flags & XValue)) |
94 |
pcg |
1.7 |
x = 50; |
95 |
pcg |
1.9 |
if (! (flags & HeightValue)) |
96 |
pcg |
1.7 |
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 |
pcg |
1.9 |
if (! (flags & YValue)) |
123 |
pcg |
1.7 |
{ |
124 |
|
|
if (flags & XNegative) |
125 |
|
|
flags |= YNegative; |
126 |
|
|
y = x; |
127 |
|
|
} |
128 |
|
|
|
129 |
pcg |
1.9 |
if (! (flags & WidthValue) && geom[0] != '=') |
130 |
pcg |
1.7 |
{ |
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 |
root |
1.25 |
min_it (x, 100); |
142 |
|
|
min_it (y, 100); |
143 |
|
|
max_it (x, 0); |
144 |
|
|
max_it (y, 0); |
145 |
pcg |
1.7 |
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.9 |
free (str); |
157 |
pcg |
1.7 |
return changed; |
158 |
pcg |
1.1 |
} |
159 |
|
|
|
160 |
|
|
void |
161 |
pcg |
1.5 |
rxvt_term::resize_pixmap () |
162 |
pcg |
1.1 |
{ |
163 |
root |
1.27 |
XGCValues gcvalue; |
164 |
|
|
GC gc; |
165 |
root |
1.21 |
dDisp; |
166 |
pcg |
1.7 |
|
167 |
root |
1.24 |
if (pixmap != None) |
168 |
|
|
XFreePixmap (disp, pixmap); |
169 |
pcg |
1.7 |
|
170 |
|
|
if (bgPixmap.pixmap == None) |
171 |
|
|
{ /* So be it: I'm not using pixmaps */ |
172 |
root |
1.24 |
pixmap = None; |
173 |
root |
1.20 |
|
174 |
root |
1.26 |
if (!OPTION (Opt_transparent) || !am_transparent) |
175 |
root |
1.27 |
XSetWindowBackground (disp, vt, pix_colors[Color_bg]); |
176 |
root |
1.20 |
|
177 |
pcg |
1.7 |
return; |
178 |
pcg |
1.1 |
} |
179 |
|
|
|
180 |
root |
1.17 |
gcvalue.foreground = pix_colors[Color_bg]; |
181 |
root |
1.24 |
gc = XCreateGC (disp, 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 |
root |
1.20 |
x = bgPixmap.x, y = bgPixmap.y; |
187 |
pcg |
1.7 |
unsigned int xpmh = xpmAttr.height, |
188 |
root |
1.20 |
xpmw = xpmAttr.width; |
189 |
pcg |
1.7 |
|
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 |
root |
1.27 |
pixmap = XCreatePixmap (disp, vt, xpmw, xpmh, |
203 |
|
|
(unsigned int)display->depth); |
204 |
root |
1.24 |
XCopyArea (disp, bgPixmap.pixmap, pixmap, gc, |
205 |
root |
1.27 |
0, 0, xpmw, xpmh, 0, 0); |
206 |
pcg |
1.7 |
} |
207 |
|
|
else |
208 |
|
|
{ |
209 |
root |
1.27 |
float incr, p; |
210 |
|
|
Pixmap tmp; |
211 |
pcg |
1.7 |
|
212 |
root |
1.27 |
pixmap = XCreatePixmap (disp, vt, width, height, |
213 |
|
|
(unsigned int)display->depth); |
214 |
pcg |
1.7 |
/* |
215 |
|
|
* horizontal scaling |
216 |
|
|
*/ |
217 |
pcg |
1.9 |
rxvt_pixmap_incr (&w, &x, &incr, &p, width, xpmw); |
218 |
pcg |
1.7 |
|
219 |
root |
1.27 |
tmp = XCreatePixmap (disp, vt, width, xpmh, (unsigned int)display->depth); |
220 |
|
|
XFillRectangle (disp, tmp, gc, 0, 0, width, xpmh); |
221 |
pcg |
1.7 |
|
222 |
|
|
for ( /*nil */ ; x < w; x++, p += incr) |
223 |
|
|
{ |
224 |
|
|
if (p >= xpmw) |
225 |
|
|
p = 0; |
226 |
|
|
/* copy one column from the original pixmap to the tmp pixmap */ |
227 |
root |
1.21 |
XCopyArea (disp, bgPixmap.pixmap, tmp, gc, |
228 |
pcg |
1.7 |
(int)p, 0, 1, xpmh, (int)x, 0); |
229 |
|
|
} |
230 |
|
|
|
231 |
|
|
/* |
232 |
|
|
* vertical scaling |
233 |
|
|
*/ |
234 |
pcg |
1.9 |
rxvt_pixmap_incr (&h, &y, &incr, &p, height, xpmh); |
235 |
pcg |
1.7 |
|
236 |
|
|
if (y > 0) |
237 |
root |
1.24 |
XFillRectangle (disp, pixmap, gc, 0, 0, width, y); |
238 |
root |
1.20 |
|
239 |
pcg |
1.7 |
if (h < height) |
240 |
root |
1.24 |
XFillRectangle (disp, pixmap, gc, 0, (int)h, width, height - h + 1); |
241 |
root |
1.20 |
|
242 |
pcg |
1.7 |
for ( /*nil */ ; y < h; y++, p += incr) |
243 |
|
|
{ |
244 |
|
|
if (p >= xpmh) |
245 |
|
|
p = 0; |
246 |
root |
1.20 |
|
247 |
pcg |
1.7 |
/* copy one row from the tmp pixmap to the main pixmap */ |
248 |
root |
1.24 |
XCopyArea (disp, tmp, pixmap, gc, |
249 |
pcg |
1.7 |
0, (int)p, width, 1, 0, (int)y); |
250 |
|
|
} |
251 |
root |
1.20 |
|
252 |
root |
1.21 |
XFreePixmap (disp, tmp); |
253 |
pcg |
1.7 |
} |
254 |
pcg |
1.1 |
} |
255 |
root |
1.14 |
|
256 |
root |
1.24 |
XSetWindowBackgroundPixmap (disp, vt, pixmap); |
257 |
root |
1.21 |
XFreeGC (disp, gc); |
258 |
pcg |
1.7 |
am_transparent = 0; |
259 |
pcg |
1.1 |
} |
260 |
|
|
|
261 |
|
|
/* |
262 |
|
|
* Calculate tiling sizes and increments |
263 |
|
|
* At start, p == 0, incr == xpmwidthheight |
264 |
|
|
*/ |
265 |
pcg |
1.6 |
static void |
266 |
pcg |
1.9 |
rxvt_pixmap_incr (unsigned int *wh, unsigned int *xy, float *incr, float *p, unsigned int widthheight, unsigned int xpmwidthheight) |
267 |
pcg |
1.1 |
{ |
268 |
pcg |
1.7 |
unsigned int cwh, cxy; |
269 |
|
|
float cincr, cp; |
270 |
pcg |
1.1 |
|
271 |
pcg |
1.7 |
cp = 0; |
272 |
|
|
cincr = (float)xpmwidthheight; |
273 |
|
|
cxy = *xy; |
274 |
|
|
cwh = *wh; |
275 |
|
|
if (cwh == 1) |
276 |
|
|
{ /* display one image, no horizontal/vertical scaling */ |
277 |
|
|
cincr = (float)widthheight; |
278 |
|
|
if (xpmwidthheight <= widthheight) |
279 |
|
|
{ |
280 |
|
|
cwh = xpmwidthheight; |
281 |
|
|
cxy = (cxy * (widthheight - cwh)) / 100; /* beware! order */ |
282 |
|
|
cwh += cxy; |
283 |
|
|
} |
284 |
|
|
else |
285 |
|
|
{ |
286 |
|
|
cxy = 0; |
287 |
|
|
cwh = widthheight; |
288 |
|
|
} |
289 |
pcg |
1.1 |
} |
290 |
pcg |
1.7 |
else if (cwh < 10) |
291 |
|
|
{ /* fit WH images across/down screen */ |
292 |
|
|
cincr *= cwh; |
293 |
|
|
cxy = 0; |
294 |
|
|
cwh = widthheight; |
295 |
|
|
} |
296 |
|
|
else |
297 |
|
|
{ |
298 |
|
|
cincr *= 100.0 / cwh; |
299 |
|
|
if (cwh < 100) |
300 |
|
|
{ /* contract */ |
301 |
|
|
float pos; |
302 |
|
|
|
303 |
|
|
cwh = (cwh * widthheight) / 100; |
304 |
|
|
pos = (float)cxy / 100 * widthheight - (cwh / 2); |
305 |
|
|
|
306 |
|
|
cxy = (widthheight - cwh); |
307 |
|
|
if (pos <= 0) |
308 |
|
|
cxy = 0; |
309 |
|
|
else if (pos < cxy) |
310 |
|
|
cxy = (int) pos; |
311 |
|
|
cwh += cxy; |
312 |
|
|
} |
313 |
|
|
else |
314 |
|
|
{ /* expand */ |
315 |
|
|
if (cxy > 0) |
316 |
|
|
{ /* position */ |
317 |
|
|
float pos; |
318 |
|
|
|
319 |
|
|
pos = (float)cxy / 100 * xpmwidthheight - (cincr / 2); |
320 |
|
|
cp = xpmwidthheight - cincr; |
321 |
|
|
if (pos <= 0) |
322 |
|
|
cp = 0; |
323 |
|
|
else if (pos < cp) |
324 |
|
|
cp = pos; |
325 |
|
|
} |
326 |
|
|
cxy = 0; |
327 |
|
|
cwh = widthheight; |
328 |
|
|
} |
329 |
|
|
} |
330 |
|
|
cincr /= widthheight; |
331 |
|
|
*wh = cwh; |
332 |
|
|
*xy = cxy; |
333 |
|
|
*incr = cincr; |
334 |
|
|
*p = cp; |
335 |
pcg |
1.1 |
} |
336 |
|
|
|
337 |
|
|
Pixmap |
338 |
pcg |
1.5 |
rxvt_term::set_bgPixmap (const char *file) |
339 |
pcg |
1.1 |
{ |
340 |
root |
1.21 |
char *f; |
341 |
pcg |
1.1 |
|
342 |
pcg |
1.9 |
assert (file != NULL); |
343 |
pcg |
1.1 |
|
344 |
pcg |
1.7 |
if (bgPixmap.pixmap != None) |
345 |
|
|
{ |
346 |
pcg |
1.9 |
XFreePixmap (display->display, bgPixmap.pixmap); |
347 |
pcg |
1.7 |
bgPixmap.pixmap = None; |
348 |
pcg |
1.1 |
} |
349 |
root |
1.19 |
|
350 |
root |
1.24 |
XSetWindowBackground (display->display, vt, pix_colors[Color_bg]); |
351 |
pcg |
1.1 |
|
352 |
pcg |
1.7 |
if (*file != '\0') |
353 |
|
|
{ |
354 |
|
|
/* XWindowAttributes attr; */ |
355 |
|
|
|
356 |
|
|
/* |
357 |
|
|
* we already have the required attributes |
358 |
|
|
*/ |
359 |
root |
1.24 |
/* XGetWindowAttributes (display->display, vt, &attr); */ |
360 |
pcg |
1.7 |
|
361 |
|
|
xpmAttr.closeness = 30000; |
362 |
root |
1.14 |
xpmAttr.colormap = display->cmap; |
363 |
|
|
xpmAttr.visual = display->visual; |
364 |
|
|
xpmAttr.depth = display->depth; |
365 |
pcg |
1.7 |
xpmAttr.valuemask = (XpmCloseness | XpmColormap | XpmVisual | |
366 |
|
|
XpmDepth | XpmSize | XpmReturnPixels); |
367 |
|
|
|
368 |
|
|
/* search environment variables here too */ |
369 |
pcg |
1.9 |
f = (char *)rxvt_File_find (file, ".xpm", rs[Rs_path]); |
370 |
pcg |
1.7 |
if (f == NULL |
371 |
pcg |
1.9 |
|| XpmReadFileToPixmap (display->display, display->root, f, |
372 |
pcg |
1.7 |
&bgPixmap.pixmap, NULL, |
373 |
|
|
&xpmAttr)) |
374 |
|
|
{ |
375 |
pcg |
1.10 |
char *p; |
376 |
pcg |
1.7 |
|
377 |
|
|
/* semi-colon delimited */ |
378 |
root |
1.17 |
if ((p = strchr (file, ';')) == NULL) |
379 |
|
|
p = strchr (file, '\0'); |
380 |
pcg |
1.7 |
|
381 |
root |
1.19 |
rxvt_warn ("couldn't load XPM file \"%.*s\", ignoring.\n", (p - file), file); |
382 |
pcg |
1.7 |
} |
383 |
pcg |
1.10 |
|
384 |
pcg |
1.9 |
free (f); |
385 |
pcg |
1.1 |
} |
386 |
pcg |
1.10 |
|
387 |
pcg |
1.7 |
resize_pixmap (); |
388 |
|
|
return bgPixmap.pixmap; |
389 |
pcg |
1.1 |
} |
390 |
|
|
|
391 |
|
|
#endif /* XPM_BACKGROUND */ |