1 /*--------------------------------*-C-*---------------------------------*
3 *----------------------------------------------------------------------*
4 * $Id: xpm.C,v 1.6 2004-01-31 02:15:02 pcg Exp $
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>
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.
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.
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 *---------------------------------------------------------------------*/
26 #include "../config.h" /* NECESSARY */
27 #include "rxvt.h" /* NECESSARY */
28 #include "xpm.intpro" /* PROTOS for internal routines */
33 * These GEOM strings indicate absolute size/position:
36 * @ `WxH' -> Y = X = 50
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)
45 * These GEOM strings adjust position relative to current position:
49 * And this GEOM string is for querying current scale/position:
53 rxvt_term::scale_pixmap (const char *geom)
55 int flags, changed = 0;
57 unsigned int w = 0, h = 0;
60 bgPixmap_t *bgpixmap = &(bgPixmap);
62 #define MAXLEN_GEOM sizeof("[1000x1000+1000+1000]")
66 str = (char *)rxvt_malloc (MAXLEN_GEOM + 1);
67 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 xterm_seq (XTerm_title, str, CHAR_ST);
76 if ((p = STRCHR(geom, ';')) == NULL)
77 p = STRCHR(geom, '\0');
79 if (n <= MAXLEN_GEOM) {
80 STRNCPY(str, geom, n);
83 flags = XParseGeometry(str, &x, &y, &w, &h);
87 } /* default is tile */
88 if (flags & WidthValue) {
89 if (!(flags & XValue))
91 if (!(flags & HeightValue))
94 w = (bgpixmap->w * w) / 100;
98 h = (bgpixmap->h * h) / 100;
104 if (bgpixmap->w != (short)w) {
105 bgpixmap->w = (short)w;
108 if (bgpixmap->h != (short)h) {
109 bgpixmap->h = (short)h;
113 if (!(flags & YValue)) {
114 if (flags & XNegative)
119 if (!(flags & WidthValue) && geom[0] != '=') {
123 if (flags & XNegative)
125 if (flags & YNegative)
132 if (bgpixmap->x != x) {
136 if (bgpixmap->y != y) {
146 rxvt_term::resize_pixmap ()
150 unsigned int width = TermWin_TotalWidth();
151 unsigned int height = TermWin_TotalHeight();
153 if (TermWin.pixmap != None)
154 XFreePixmap(Xdisplay, TermWin.pixmap);
156 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]);
164 gcvalue.foreground = PixColors[Color_bg];
165 gc = XCreateGC(Xdisplay, TermWin.vt, GCForeground, &gcvalue);
167 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;
174 * don't zoom pixmap too much nor expand really small pixmaps
176 if (w > 1000 || h > 1000)
178 else if (width > (10 * xpmw)
179 || height > (10 * xpmh))
183 /* basic X tiling - let the X server do it */
184 TermWin.pixmap = XCreatePixmap(Xdisplay, TermWin.vt,
186 (unsigned int)XDEPTH);
187 XCopyArea(Xdisplay, bgPixmap.pixmap, TermWin.pixmap, gc,
188 0, 0, xpmw, xpmh, 0, 0);
193 TermWin.pixmap = XCreatePixmap(Xdisplay, TermWin.vt,
195 (unsigned int)XDEPTH);
199 rxvt_pixmap_incr(&w, &x, &incr, &p, width, xpmw);
201 tmp = XCreatePixmap(Xdisplay, TermWin.vt,
202 width, xpmh, (unsigned int)XDEPTH);
203 XFillRectangle(Xdisplay, tmp, gc, 0, 0, width,
206 for ( /*nil */ ; x < w; x++, p += incr) {
209 /* copy one column from the original pixmap to the tmp pixmap */
210 XCopyArea(Xdisplay, bgPixmap.pixmap, tmp, gc,
211 (int)p, 0, 1, xpmh, (int)x, 0);
217 rxvt_pixmap_incr(&h, &y, &incr, &p, height, xpmh);
220 XFillRectangle(Xdisplay, TermWin.pixmap, gc, 0, 0, width,
223 XFillRectangle(Xdisplay, TermWin.pixmap, gc, 0, (int)h,
224 width, height - h + 1);
225 for ( /*nil */ ; y < h; y++, p += incr) {
228 /* copy one row from the tmp pixmap to the main pixmap */
229 XCopyArea(Xdisplay, tmp, TermWin.pixmap, gc,
230 0, (int)p, width, 1, 0, (int)y);
232 XFreePixmap(Xdisplay, tmp);
235 XSetWindowBackgroundPixmap(Xdisplay, TermWin.vt, TermWin.pixmap);
236 XFreeGC(Xdisplay, gc);
239 XClearWindow(Xdisplay, TermWin.vt);
241 XSync(Xdisplay, False);
245 * Calculate tiling sizes and increments
246 * At start, p == 0, incr == xpmwidthheight
250 rxvt_pixmap_incr(unsigned int *wh, unsigned int *xy, float *incr, float *p, unsigned int widthheight, unsigned int xpmwidthheight)
252 unsigned int cwh, cxy;
256 cincr = (float)xpmwidthheight;
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 */
269 } else if (cwh < 10) { /* fit WH images across/down screen */
274 cincr *= 100.0 / cwh;
275 if (cwh < 100) { /* contract */
278 cwh = (cwh * widthheight) / 100;
279 pos = (float)cxy / 100 * widthheight - (cwh / 2);
281 cxy = (widthheight - cwh);
287 } else { /* expand */
288 if (cxy > 0) { /* position */
291 pos = (float)cxy / 100 * xpmwidthheight - (cincr / 2);
292 cp = xpmwidthheight - cincr;
302 cincr /= widthheight;
310 rxvt_term::set_bgPixmap (const char *file)
314 assert(file != NULL);
316 if (bgPixmap.pixmap != None) {
317 XFreePixmap(Xdisplay, bgPixmap.pixmap);
318 bgPixmap.pixmap = None;
320 XSetWindowBackground(Xdisplay, TermWin.vt, PixColors[Color_bg]);
323 /* XWindowAttributes attr; */
326 * we already have the required attributes
328 /* XGetWindowAttributes(Xdisplay, TermWin.vt, &attr); */
330 xpmAttr.closeness = 30000;
331 xpmAttr.colormap = XCMAP;
332 xpmAttr.visual = XVISUAL;
333 xpmAttr.depth = XDEPTH;
334 xpmAttr.valuemask = (XpmCloseness | XpmColormap | XpmVisual |
335 XpmDepth | XpmSize | XpmReturnPixels);
337 /* search environment variables here too */
338 f = (char *)rxvt_File_find(file, ".xpm", rs[Rs_path]);
340 || XpmReadFileToPixmap(Xdisplay, Xroot, f,
341 &bgPixmap.pixmap, NULL,
345 /* semi-colon delimited */
346 if ((p = STRCHR(file, ';')) == NULL)
347 p = STRCHR(file, '\0');
349 rxvt_print_error("couldn't load XPM file \"%.*s\"", (p - file),
355 return bgPixmap.pixmap;
358 #endif /* XPM_BACKGROUND */