ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/xpm.C
(Generate patch)

Comparing rxvt-unicode/src/xpm.C (file contents):
Revision 1.5 by pcg, Sat Jan 31 00:20:21 2004 UTC vs.
Revision 1.28 by root, Wed Jan 18 17:49:47 2006 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*--------------------------------*-C-*---------------------------------*
2 * File: xpm.c 2 * File: xpm.C
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * $Id: xpm.C,v 1.5 2004/01/31 00:20:21 pcg Exp $
5 * 4 *
6 * All portions of code are copyright by their respective author/s. 5 * All portions of code are copyright by their respective author/s.
7 * Copyright (c) 1997 Carsten Haitzler <raster@zip.com.au> 6 * Copyright (c) 1997 Carsten Haitzler <raster@zip.com.au>
8 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de> 7 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de>
9 * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com> 8 * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com>
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *---------------------------------------------------------------------*/ 23 *---------------------------------------------------------------------*/
25 24
26#include "../config.h" /* NECESSARY */ 25#include "../config.h" /* NECESSARY */
27#include "rxvt.h" /* NECESSARY */ 26#include "rxvt.h" /* NECESSARY */
28#include "xpm.intpro" /* PROTOS for internal routines */
29 27
30#ifdef XPM_BACKGROUND 28#ifdef XPM_BACKGROUND
29
30static void rxvt_pixmap_incr (unsigned int *wh, unsigned int *xy, float *incr, float *p, unsigned int widthheight, unsigned int xpmwidthheight);
31 31
32/* 32/*
33 * These GEOM strings indicate absolute size/position: 33 * These GEOM strings indicate absolute size/position:
34 * @ `WxH+X+Y' 34 * @ `WxH+X+Y'
35 * @ `WxH+X' -> Y = X 35 * @ `WxH+X' -> Y = X
50 * @ `?' 50 * @ `?'
51 */ 51 */
52int 52int
53rxvt_term::scale_pixmap (const char *geom) 53rxvt_term::scale_pixmap (const char *geom)
54{ 54{
55 int flags, changed = 0; 55 int flags, changed = 0;
56 int x = 0, y = 0; 56 int x = 0, y = 0;
57 unsigned int w = 0, h = 0; 57 unsigned int w = 0, h = 0;
58 unsigned int n; 58 unsigned int n;
59 char *p, *str; 59 char *p;
60 bgPixmap_t *bgpixmap = &(bgPixmap); 60 bgPixmap_t *bgpixmap = & (bgPixmap);
61 61
62#define MAXLEN_GEOM sizeof("[1000x1000+1000+1000]") 62#define MAXLEN_GEOM sizeof("[10000x10000+10000+10000]")
63 63
64 if (geom == NULL) 64 if (geom == NULL)
65 return 0; 65 return 0;
66 str = (char *)rxvt_malloc (MAXLEN_GEOM + 1); 66
67 if (!STRCMP(geom, "?")) { 67 char str[MAXLEN_GEOM];
68
69 if (!strcmp (geom, "?"))
70 {
68 sprintf(str, "[%dx%d+%d+%d]", /* can't presume snprintf() ! */ 71 sprintf (str, "[%dx%d+%d+%d]", /* can't presume snprintf () ! */
69 min(bgpixmap->w, 9999), min(bgpixmap->h, 9999), 72 min (bgpixmap->w, 32767), min (bgpixmap->h, 32767),
70 min(bgpixmap->x, 9999), min(bgpixmap->y, 9999)); 73 min (bgpixmap->x, 32767), min (bgpixmap->y, 32767));
71 xterm_seq (XTerm_title, str, CHAR_ST); 74 process_xterm_seq (XTerm_title, str, CHAR_ST);
72 free(str); 75 return 0;
73 return 0;
74 } 76 }
75 77
76 if ((p = STRCHR(geom, ';')) == NULL) 78 if ((p = strchr (geom, ';')) == NULL)
77 p = STRCHR(geom, '\0'); 79 p = strchr (geom, '\0');
80
78 n = (p - geom); 81 n = (p - geom);
79 if (n <= MAXLEN_GEOM) { 82 if (n <= MAXLEN_GEOM)
80 STRNCPY(str, geom, n); 83 {
84 strncpy (str, geom, n);
81 str[n] = '\0'; 85 str[n] = '\0';
82 86
83 flags = XParseGeometry(str, &x, &y, &w, &h); 87 flags = XParseGeometry (str, &x, &y, &w, &h);
84 if (!flags) { 88
89 if (!flags)
90 {
85 flags |= WidthValue; 91 flags |= WidthValue;
86 w = 0; 92 w = 0;
87 } /* default is tile */ 93 } /* default is tile */
94
88 if (flags & WidthValue) { 95 if (flags & WidthValue)
96 {
97 if (!(flags & XValue))
98 x = 50;
99
100 if (!(flags & HeightValue))
101 h = w;
102
103 if (w && !h)
104 {
105 w = (bgpixmap->w * w) / 100;
106 h = bgpixmap->h;
107 }
108 else if (h && !w)
109 {
110 w = bgpixmap->w;
111 h = (bgpixmap->h * h) / 100;
112 }
113
114 min_it (w, 32767);
115 min_it (h, 32767);
116
117 if (bgpixmap->w != (short)w)
118 {
119 bgpixmap->w = (short)w;
120 changed++;
121 }
122
123 if (bgpixmap->h != (short)h)
124 {
125 bgpixmap->h = (short)h;
126 changed++;
127 }
128 }
129
89 if (!(flags & XValue)) 130 if (!(flags & YValue))
90 x = 50; 131 {
91 if (!(flags & HeightValue)) 132 if (flags & XNegative)
92 h = w; 133 flags |= YNegative;
93 if (w && !h) { 134
94 w = (bgpixmap->w * w) / 100; 135 y = x;
95 h = bgpixmap->h; 136 }
96 } else if (h && !w) { 137
97 w = bgpixmap->w; 138 if (!(flags & WidthValue) && geom[0] != '=')
98 h = (bgpixmap->h * h) / 100; 139 {
140 x += bgpixmap->x;
141 y += bgpixmap->y;
142 }
143
144 if (xpmAttr.width && xpmAttr.height)
145 {
146 x = MOD(x, xpmAttr.width);
147 y = MOD(y, xpmAttr.height);
148 }
149
150 if (bgpixmap->x != x)
151 {
152 bgpixmap->x = x;
153 changed++;
154 }
155
156 if (bgpixmap->y != y)
157 {
158 bgpixmap->y = y;
159 changed++;
160 }
99 } 161 }
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 162
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; 163 return changed;
143} 164}
144 165
145void 166void
146rxvt_term::resize_pixmap () 167rxvt_term::resize_pixmap ()
147{ 168{
148 XGCValues gcvalue; 169 XGCValues gcvalue;
149 GC gc; 170 GC gc;
150 unsigned int width = TermWin_TotalWidth(); 171 dDisp;
151 unsigned int height = TermWin_TotalHeight();
152 172
153 if (TermWin.pixmap != None) 173 if (pixmap != None)
154 XFreePixmap(Xdisplay, TermWin.pixmap); 174 XFreePixmap (disp, pixmap);
155 175
156 if (bgPixmap.pixmap == None) { /* So be it: I'm not using pixmaps */ 176 if (bgPixmap.pixmap == None)
157 TermWin.pixmap = None; 177 { /* So be it: I'm not using pixmaps */
158 if (!(Options & Opt_transparent) || am_transparent == 0) 178 pixmap = None;
159 XSetWindowBackground(Xdisplay, TermWin.vt,
160 PixColors[Color_bg]);
161 return;
162 }
163 179
180 if (!OPTION (Opt_transparent) || !am_transparent)
181 XSetWindowBackground (disp, vt, pix_colors[Color_bg]);
182
183 return;
184 }
185
164 gcvalue.foreground = PixColors[Color_bg]; 186 gcvalue.foreground = pix_colors[Color_bg];
165 gc = XCreateGC(Xdisplay, TermWin.vt, GCForeground, &gcvalue); 187 gc = XCreateGC (disp, vt, GCForeground, &gcvalue);
166 188
167 if (bgPixmap.pixmap != None) { /* we have a specified pixmap */ 189 if (bgPixmap.pixmap != None)
190 { /* we have a specified pixmap */
168 unsigned int w = bgPixmap.w, h = bgPixmap.h, 191 unsigned int w = bgPixmap.w, h = bgPixmap.h,
169 x = bgPixmap.x, y = bgPixmap.y; 192 x = bgPixmap.x, y = bgPixmap.y;
170 unsigned int xpmh = xpmAttr.height, 193 unsigned int xpmh = xpmAttr.height,
171 xpmw = xpmAttr.width; 194 xpmw = xpmAttr.width;
172 195
173 /* 196 /*
174 * don't zoom pixmap too much nor expand really small pixmaps 197 * don't zoom pixmap too much nor expand really small pixmaps
175 */ 198 */
176 if (w > 1000 || h > 1000) 199 if (w > 32767 || h > 32767)
177 w = 1; 200 w = 1;
178 else if (width > (10 * xpmw) 201 else if (width > (10 * xpmw)
179 || height > (10 * xpmh)) 202 || height > (10 * xpmh))
180 w = 0; /* tile */ 203 w = 0; /* tile */
181 204
182 if (w == 0) { 205 if (!w)
206 {
183 /* basic X tiling - let the X server do it */ 207 /* basic X tiling - let the X server do it */
184 TermWin.pixmap = XCreatePixmap(Xdisplay, TermWin.vt, 208 pixmap = XCreatePixmap (disp, vt, xpmw, xpmh, display->depth);
185 xpmw, xpmh,
186 (unsigned int)XDEPTH);
187 XCopyArea(Xdisplay, bgPixmap.pixmap, TermWin.pixmap, gc,
188 0, 0, xpmw, xpmh, 0, 0);
189 } else {
190 float incr, p;
191 Pixmap tmp;
192 209
193 TermWin.pixmap = XCreatePixmap(Xdisplay, TermWin.vt, 210 XCopyArea (disp, bgPixmap.pixmap, pixmap, gc, x, y, xpmw - x, xpmh - y, 0, 0);
194 width, height, 211 XCopyArea (disp, bgPixmap.pixmap, pixmap, gc, x, 0, xpmw - x, y, 0, xpmh - y);
195 (unsigned int)XDEPTH); 212 XCopyArea (disp, bgPixmap.pixmap, pixmap, gc, 0, y, x, xpmh - y, xpmw - x, 0);
196 /* 213 XCopyArea (disp, bgPixmap.pixmap, pixmap, gc, 0, 0, x, y, xpmw - x, xpmh - y);
214 }
215 else
216 {
217 float incr, p;
218 Pixmap tmp;
219
220 pixmap = XCreatePixmap (disp, vt, width, height,
221 (unsigned int)display->depth);
222 /*
197 * horizontal scaling 223 * horizontal scaling
198 */ 224 */
199 rxvt_pixmap_incr(&w, &x, &incr, &p, width, xpmw); 225 rxvt_pixmap_incr (&w, &x, &incr, &p, width, xpmw);
200 226
201 tmp = XCreatePixmap(Xdisplay, TermWin.vt, 227 tmp = XCreatePixmap (disp, vt, width, xpmh, (unsigned int)display->depth);
202 width, xpmh, (unsigned int)XDEPTH);
203 XFillRectangle(Xdisplay, tmp, gc, 0, 0, width, 228 XFillRectangle (disp, tmp, gc, 0, 0, width, xpmh);
204 xpmh);
205 229
206 for ( /*nil */ ; x < w; x++, p += incr) { 230 for ( /*nil */ ; x < w; x++, p += incr)
207 if (p >= xpmw) 231 {
208 p = 0; 232 if (p >= xpmw)
233 p = 0;
234
209 /* copy one column from the original pixmap to the tmp pixmap */ 235 /* copy one column from the original pixmap to the tmp pixmap */
210 XCopyArea(Xdisplay, bgPixmap.pixmap, tmp, gc, 236 XCopyArea (disp, bgPixmap.pixmap, tmp, gc,
211 (int)p, 0, 1, xpmh, (int)x, 0); 237 (int)p, 0, 1, xpmh, (int)x, 0);
212 } 238 }
213 239
214 /* 240 /*
215 * vertical scaling 241 * vertical scaling
216 */ 242 */
217 rxvt_pixmap_incr(&h, &y, &incr, &p, height, xpmh); 243 rxvt_pixmap_incr (&h, &y, &incr, &p, height, xpmh);
218 244
219 if (y > 0) 245 if (y > 0)
220 XFillRectangle(Xdisplay, TermWin.pixmap, gc, 0, 0, width, 246 XFillRectangle (disp, pixmap, gc, 0, 0, width, y);
221 y); 247
222 if (h < height) 248 if (h < height)
223 XFillRectangle(Xdisplay, TermWin.pixmap, gc, 0, (int)h, 249 XFillRectangle (disp, pixmap, gc, 0, (int)h, width, height - h + 1);
224 width, height - h + 1); 250
225 for ( /*nil */ ; y < h; y++, p += incr) { 251 for ( /*nil */ ; y < h; y++, p += incr)
226 if (p >= xpmh) 252 {
227 p = 0; 253 if (p >= xpmh)
254 p = 0;
255
228 /* copy one row from the tmp pixmap to the main pixmap */ 256 /* copy one row from the tmp pixmap to the main pixmap */
229 XCopyArea(Xdisplay, tmp, TermWin.pixmap, gc, 257 XCopyArea (disp, tmp, pixmap, gc,
230 0, (int)p, width, 1, 0, (int)y); 258 0, (int)p, width, 1, 0, (int)y);
259 }
260
261 XFreePixmap (disp, tmp);
262 }
231 } 263 }
232 XFreePixmap(Xdisplay, tmp); 264
233 }
234 }
235 XSetWindowBackgroundPixmap(Xdisplay, TermWin.vt, TermWin.pixmap); 265 XSetWindowBackgroundPixmap (disp, vt, pixmap);
266
267 if (pixmap != None)
268 {
269 XFreePixmap (disp, pixmap);
270 pixmap = None;
271 }
272
236 XFreeGC(Xdisplay, gc); 273 XFreeGC (disp, gc);
237 am_transparent = 0; 274 am_transparent = 0;
238
239 XClearWindow(Xdisplay, TermWin.vt);
240
241 XSync(Xdisplay, False);
242} 275}
243 276
244/* 277/*
245 * Calculate tiling sizes and increments 278 * Calculate tiling sizes and increments
246 * At start, p == 0, incr == xpmwidthheight 279 * At start, p == 0, incr == xpmwidthheight
247 */ 280 */
248void 281static void
249rxvt_pixmap_incr(unsigned int *wh, unsigned int *xy, float *incr, float *p, unsigned int widthheight, unsigned int xpmwidthheight) 282rxvt_pixmap_incr (unsigned int *wh, unsigned int *xy, float *incr, float *p, unsigned int widthheight, unsigned int xpmwidthheight)
250{ 283{
251 unsigned int cwh, cxy; 284 unsigned int cwh, cxy;
252 float cincr, cp; 285 float cincr, cp;
253 286
254 cp = 0; 287 cp = 0;
255 cincr = (float)xpmwidthheight; 288 cincr = (float)xpmwidthheight;
256 cxy = *xy; 289 cxy = *xy;
257 cwh = *wh; 290 cwh = *wh;
291 if (cwh == 1)
258 if (cwh == 1) { /* display one image, no horizontal/vertical scaling */ 292 { /* display one image, no horizontal/vertical scaling */
259 cincr = (float)widthheight; 293 cincr = (float)widthheight;
260 if (xpmwidthheight <= widthheight) { 294 if (xpmwidthheight <= widthheight)
295 {
261 cwh = xpmwidthheight; 296 cwh = xpmwidthheight;
262 cxy = (cxy * (widthheight - cwh)) / 100; /* beware! order */ 297 cxy = (cxy * (widthheight - cwh)) / 100; /* beware! order */
263 cwh += cxy; 298 cwh += cxy;
264 } else { 299 }
300 else
301 {
302 cxy = 0;
303 cwh = widthheight;
304 }
305 }
306 else if (cwh < 10)
307 { /* fit WH images across/down screen */
308 cincr *= cwh;
265 cxy = 0; 309 cxy = 0;
266 cwh = widthheight; 310 cwh = widthheight;
267 } 311 }
268 } else if (cwh < 10) { /* fit WH images across/down screen */ 312 else
269 cincr *= cwh; 313 {
270 cxy = 0;
271 cwh = widthheight;
272 } else {
273 cincr *= 100.0 / cwh; 314 cincr *= 100.0 / cwh;
274 if (cwh < 100) { /* contract */ 315 if (cwh < 100)
316 { /* contract */
275 float pos; 317 float pos;
276 318
277 cwh = (cwh * widthheight) / 100; 319 cwh = (cwh * widthheight) / 100;
278 pos = (float)cxy / 100 * widthheight - (cwh / 2); 320 pos = (float)cxy / 100 * widthheight - (cwh / 2);
279 321
280 cxy = (widthheight - cwh); 322 cxy = (widthheight - cwh);
281 if (pos <= 0) 323 if (pos <= 0)
282 cxy = 0; 324 cxy = 0;
283 else if (pos < cxy) 325 else if (pos < cxy)
284 cxy = (int) pos; 326 cxy = (int) pos;
285 cwh += cxy; 327 cwh += cxy;
286 } else { /* expand */ 328 }
287 if (cxy > 0) { /* position */ 329 else
288 float pos; 330 { /* expand */
331 if (cxy > 0)
332 { /* position */
333 float pos;
289 334
290 pos = (float)cxy / 100 * xpmwidthheight - (cincr / 2); 335 pos = (float)cxy / 100 * xpmwidthheight - (cincr / 2);
291 cp = xpmwidthheight - cincr; 336 cp = xpmwidthheight - cincr;
292 if (pos <= 0) 337 if (pos <= 0)
293 cp = 0; 338 cp = 0;
294 else if (pos < cp) 339 else if (pos < cp)
295 cp = pos; 340 cp = pos;
341 }
342 cxy = 0;
343 cwh = widthheight;
344 }
296 } 345 }
297 cxy = 0;
298 cwh = widthheight;
299 }
300 }
301 cincr /= widthheight; 346 cincr /= widthheight;
302 *wh = cwh; 347 *wh = cwh;
303 *xy = cxy; 348 *xy = cxy;
304 *incr = cincr; 349 *incr = cincr;
305 *p = cp; 350 *p = cp;
306} 351}
307 352
308Pixmap 353Pixmap
309rxvt_term::set_bgPixmap (const char *file) 354rxvt_term::set_bgPixmap (const char *file)
310{ 355{
311 char *f; 356 char *f;
312 357
313 assert(file != NULL); 358 assert (file != NULL);
314 359
315 if (bgPixmap.pixmap != None) { 360 if (bgPixmap.pixmap != None)
361 {
316 XFreePixmap(Xdisplay, bgPixmap.pixmap); 362 XFreePixmap (display->display, bgPixmap.pixmap);
317 bgPixmap.pixmap = None; 363 bgPixmap.pixmap = None;
318 } 364 }
365
319 XSetWindowBackground(Xdisplay, TermWin.vt, PixColors[Color_bg]); 366 XSetWindowBackground (display->display, vt, pix_colors[Color_bg]);
320 367
321 if (*file != '\0') { 368 if (*file != '\0')
369 {
322/* XWindowAttributes attr; */ 370 /* XWindowAttributes attr; */
323 371
324 /* 372 /*
325 * we already have the required attributes 373 * we already have the required attributes
326 */ 374 */
327/* XGetWindowAttributes(Xdisplay, TermWin.vt, &attr); */ 375 /* XGetWindowAttributes (display->display, vt, &attr); */
328 376
329 xpmAttr.closeness = 30000; 377 xpmAttr.closeness = 30000;
330 xpmAttr.colormap = XCMAP; 378 xpmAttr.colormap = display->cmap;
331 xpmAttr.visual = XVISUAL; 379 xpmAttr.visual = display->visual;
332 xpmAttr.depth = XDEPTH; 380 xpmAttr.depth = display->depth;
333 xpmAttr.valuemask = (XpmCloseness | XpmColormap | XpmVisual | 381 xpmAttr.valuemask = (XpmCloseness | XpmColormap | XpmVisual |
334 XpmDepth | XpmSize | XpmReturnPixels); 382 XpmDepth | XpmSize | XpmReturnPixels);
335 383
336 /* search environment variables here too */ 384 /* search environment variables here too */
337 f = (char *)rxvt_File_find(file, ".xpm", rs[Rs_path]); 385 f = (char *)rxvt_File_find (file, ".xpm", rs[Rs_path]);
338 if (f == NULL 386 if (f == NULL
339 || XpmReadFileToPixmap(Xdisplay, Xroot, f, 387 || XpmReadFileToPixmap (display->display, display->root, f,
340 &bgPixmap.pixmap, NULL, 388 &bgPixmap.pixmap, NULL,
341 &xpmAttr)) { 389 &xpmAttr))
342 char *p; 390 {
391 char *p;
343 392
344 /* semi-colon delimited */ 393 /* semi-colon delimited */
345 if ((p = STRCHR(file, ';')) == NULL) 394 if ((p = strchr (file, ';')) == NULL)
346 p = STRCHR(file, '\0'); 395 p = strchr (file, '\0');
347 396
348 rxvt_print_error("couldn't load XPM file \"%.*s\"", (p - file), 397 rxvt_warn ("couldn't load XPM file \"%.*s\", ignoring.\n", (p - file), file);
349 file); 398 }
350 } 399
351 free(f); 400 free (f);
352 } 401 }
402
353 resize_pixmap (); 403 resize_pixmap ();
354 return bgPixmap.pixmap; 404 return bgPixmap.pixmap;
355} 405}
356 406
357#endif /* XPM_BACKGROUND */ 407#endif /* XPM_BACKGROUND */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines