1 /*--------------------------------*-C-*---------------------------------*
3 *----------------------------------------------------------------------*
4 * $Id: xpm.C,v 1.2 2003-11-24 17:31:28 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:
54 rxvt_scale_pixmap(pR_ const char *geom)
56 int flags, changed = 0;
58 unsigned int w = 0, h = 0;
61 bgPixmap_t *bgpixmap = &(R->h->bgPixmap);
63 #define MAXLEN_GEOM sizeof("[1000x1000+1000+1000]")
67 str = rxvt_malloc(MAXLEN_GEOM + 1);
68 if (!STRCMP(geom, "?")) {
69 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 rxvt_xterm_seq(aR_ XTerm_title, str, CHAR_ST);
77 if ((p = STRCHR(geom, ';')) == NULL)
78 p = STRCHR(geom, '\0');
80 if (n <= MAXLEN_GEOM) {
81 STRNCPY(str, geom, n);
84 flags = XParseGeometry(str, &x, &y, &w, &h);
88 } /* default is tile */
89 if (flags & WidthValue) {
90 if (!(flags & XValue))
92 if (!(flags & HeightValue))
95 w = (bgpixmap->w * w) / 100;
99 h = (bgpixmap->h * h) / 100;
105 if (bgpixmap->w != (short)w) {
106 bgpixmap->w = (short)w;
109 if (bgpixmap->h != (short)h) {
110 bgpixmap->h = (short)h;
114 if (!(flags & YValue)) {
115 if (flags & XNegative)
120 if (!(flags & WidthValue) && geom[0] != '=') {
124 if (flags & XNegative)
126 if (flags & YNegative)
133 if (bgpixmap->x != x) {
137 if (bgpixmap->y != y) {
148 rxvt_resize_pixmap(pR)
152 unsigned int width = TermWin_TotalWidth();
153 unsigned int height = TermWin_TotalHeight();
155 if (R->TermWin.pixmap != None)
156 XFreePixmap(R->Xdisplay, R->TermWin.pixmap);
158 if (R->h->bgPixmap.pixmap == None) { /* So be it: I'm not using pixmaps */
159 R->TermWin.pixmap = None;
160 if (!(R->Options & Opt_transparent) || R->h->am_transparent == 0)
161 XSetWindowBackground(R->Xdisplay, R->TermWin.vt,
162 R->PixColors[Color_bg]);
166 gcvalue.foreground = R->PixColors[Color_bg];
167 gc = XCreateGC(R->Xdisplay, R->TermWin.vt, GCForeground, &gcvalue);
169 if (R->h->bgPixmap.pixmap != None) { /* we have a specified pixmap */
170 unsigned int w = R->h->bgPixmap.w, h = R->h->bgPixmap.h,
171 x = R->h->bgPixmap.x, y = R->h->bgPixmap.y;
172 unsigned int xpmh = R->h->xpmAttr.height,
173 xpmw = R->h->xpmAttr.width;
176 * don't zoom pixmap too much nor expand really small pixmaps
178 if (w > 1000 || h > 1000)
180 else if (width > (10 * xpmw)
181 || height > (10 * xpmh))
185 /* basic X tiling - let the X server do it */
186 R->TermWin.pixmap = XCreatePixmap(R->Xdisplay, R->TermWin.vt,
188 (unsigned int)XDEPTH);
189 XCopyArea(R->Xdisplay, R->h->bgPixmap.pixmap, R->TermWin.pixmap, gc,
190 0, 0, xpmw, xpmh, 0, 0);
195 R->TermWin.pixmap = XCreatePixmap(R->Xdisplay, R->TermWin.vt,
197 (unsigned int)XDEPTH);
201 rxvt_pixmap_incr(&w, &x, &incr, &p, width, xpmw);
203 tmp = XCreatePixmap(R->Xdisplay, R->TermWin.vt,
204 width, xpmh, (unsigned int)XDEPTH);
205 XFillRectangle(R->Xdisplay, tmp, gc, 0, 0, width,
208 for ( /*nil */ ; x < w; x++, p += incr) {
211 /* copy one column from the original pixmap to the tmp pixmap */
212 XCopyArea(R->Xdisplay, R->h->bgPixmap.pixmap, tmp, gc,
213 (int)p, 0, 1, xpmh, (int)x, 0);
219 rxvt_pixmap_incr(&h, &y, &incr, &p, height, xpmh);
222 XFillRectangle(R->Xdisplay, R->TermWin.pixmap, gc, 0, 0, width,
225 XFillRectangle(R->Xdisplay, R->TermWin.pixmap, gc, 0, (int)h,
226 width, height - h + 1);
227 for ( /*nil */ ; y < h; y++, p += incr) {
230 /* copy one row from the tmp pixmap to the main pixmap */
231 XCopyArea(R->Xdisplay, tmp, R->TermWin.pixmap, gc,
232 0, (int)p, width, 1, 0, (int)y);
234 XFreePixmap(R->Xdisplay, tmp);
237 XSetWindowBackgroundPixmap(R->Xdisplay, R->TermWin.vt, R->TermWin.pixmap);
238 XFreeGC(R->Xdisplay, gc);
239 R->h->am_transparent = 0;
241 XClearWindow(R->Xdisplay, R->TermWin.vt);
243 XSync(R->Xdisplay, False);
247 * Calculate tiling sizes and increments
248 * At start, p == 0, incr == xpmwidthheight
252 rxvt_pixmap_incr(unsigned int *wh, unsigned int *xy, float *incr, float *p, unsigned int widthheight, unsigned int xpmwidthheight)
254 unsigned int cwh, cxy;
258 cincr = (float)xpmwidthheight;
261 if (cwh == 1) { /* display one image, no horizontal/vertical scaling */
262 cincr = (float)widthheight;
263 if (xpmwidthheight <= widthheight) {
264 cwh = xpmwidthheight;
265 cxy = (cxy * (widthheight - cwh)) / 100; /* beware! order */
271 } else if (cwh < 10) { /* fit WH images across/down screen */
276 cincr *= 100.0 / cwh;
277 if (cwh < 100) { /* contract */
280 cwh = (cwh * widthheight) / 100;
281 pos = (float)cxy / 100 * widthheight - (cwh / 2);
283 cxy = (widthheight - cwh);
289 } else { /* expand */
290 if (cxy > 0) { /* position */
293 pos = (float)cxy / 100 * xpmwidthheight - (cincr / 2);
294 cp = xpmwidthheight - cincr;
304 cincr /= widthheight;
313 rxvt_set_bgPixmap(pR_ const char *file)
317 assert(file != NULL);
319 if (R->h->bgPixmap.pixmap != None) {
320 XFreePixmap(R->Xdisplay, R->h->bgPixmap.pixmap);
321 R->h->bgPixmap.pixmap = None;
323 XSetWindowBackground(R->Xdisplay, R->TermWin.vt, R->PixColors[Color_bg]);
326 /* XWindowAttributes attr; */
329 * we already have the required attributes
331 /* XGetWindowAttributes(R->Xdisplay, R->TermWin.vt, &attr); */
333 R->h->xpmAttr.closeness = 30000;
334 R->h->xpmAttr.colormap = XCMAP;
335 R->h->xpmAttr.visual = XVISUAL;
336 R->h->xpmAttr.depth = XDEPTH;
337 R->h->xpmAttr.valuemask = (XpmCloseness | XpmColormap | XpmVisual |
338 XpmDepth | XpmSize | XpmReturnPixels);
340 /* search environment variables here too */
341 f = (char *)rxvt_File_find(file, ".xpm", R->h->rs[Rs_path]);
343 || XpmReadFileToPixmap(R->Xdisplay, Xroot, f,
344 &R->h->bgPixmap.pixmap, NULL,
348 /* semi-colon delimited */
349 if ((p = STRCHR(file, ';')) == NULL)
350 p = STRCHR(file, '\0');
352 rxvt_print_error("couldn't load XPM file \"%.*s\"", (p - file),
357 rxvt_resize_pixmap(aR);
358 return R->h->bgPixmap.pixmap;
361 #endif /* XPM_BACKGROUND */