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

Comparing rxvt-unicode/src/background.C (file contents):
Revision 1.54 by sf-exg, Wed Sep 1 22:49:35 2010 UTC vs.
Revision 1.62 by sf-exg, Sun Oct 3 21:44:13 2010 UTC

96 // this is basically redundant as bgPixmap_t is only used in 96 // this is basically redundant as bgPixmap_t is only used in
97 // zero_initialised-derived structs 97 // zero_initialised-derived structs
98#ifdef HAVE_AFTERIMAGE 98#ifdef HAVE_AFTERIMAGE
99 original_asim = NULL; 99 original_asim = NULL;
100#endif 100#endif
101#ifdef HAVE_PIXBUF
102 pixbuf = NULL;
103#endif
101#ifdef BG_IMAGE_FROM_FILE 104#ifdef BG_IMAGE_FROM_FILE
102 have_image = false; 105 have_image = false;
103 h_scale = v_scale = 0; 106 h_scale = v_scale = 0;
104 h_align = v_align = 0; 107 h_align = v_align = 0;
105#endif 108#endif
115#ifdef HAVE_AFTERIMAGE 118#ifdef HAVE_AFTERIMAGE
116 if (original_asim) 119 if (original_asim)
117 safe_asimage_destroy (original_asim); 120 safe_asimage_destroy (original_asim);
118#endif 121#endif
119 122
123#ifdef HAVE_PIXBUF
124 if (pixbuf)
125 g_object_unref (pixbuf);
126#endif
127
120 if (pixmap && target) 128 if (pixmap && target)
121 XFreePixmap (target->dpy, pixmap); 129 XFreePixmap (target->dpy, pixmap);
122} 130}
123 131
124bool 132bool
130# endif 138# endif
131 139
132# ifdef BG_IMAGE_FROM_FILE 140# ifdef BG_IMAGE_FROM_FILE
133 if (have_image) 141 if (have_image)
134 { 142 {
135 if (h_scale != 0 || v_scale != 0 143 if (flags & sizeSensitive)
136 || h_align != 0 || v_align != 0)
137 return true; 144 return true;
138 } 145 }
139# endif 146# endif
140 147
141 return false; 148 return false;
220 227
221static inline int 228static inline int
222make_align_position (int align, int window_size, int image_size) 229make_align_position (int align, int window_size, int image_size)
223{ 230{
224 int diff = window_size - image_size; 231 int diff = window_size - image_size;
225 int smaller = MIN (image_size,window_size); 232 int smaller = min (image_size, window_size);
226 233
227 if (align >= 0 && align <= 50) 234 if (align >= 0 && align <= 50)
228 return diff * align / 100; 235 return diff * align / 100;
229 else if (align > 50 && align <= 100) 236 else if (align > 50 && align <= 100)
230 return window_size - image_size - diff * (100 - align) / 100; 237 return window_size - image_size - diff * (100 - align) / 100;
239make_clip_rectangle (int pos, int size, int target_size, int &dst_pos, int &dst_size) 246make_clip_rectangle (int pos, int size, int target_size, int &dst_pos, int &dst_size)
240{ 247{
241 int src_pos = 0; 248 int src_pos = 0;
242 dst_pos = 0; 249 dst_pos = 0;
243 dst_size = size; 250 dst_size = size;
244 if (pos < 0 && size > target_size) 251 if (pos < 0)
245 { 252 {
246 src_pos = -pos; 253 src_pos = -pos;
247 dst_size += pos; 254 dst_size += pos;
248 } 255 }
249 else if (pos > 0) 256 else if (pos > 0)
380 w = h = noScale; 387 w = h = noScale;
381 geom_flags |= WidthValue|HeightValue; 388 geom_flags |= WidthValue|HeightValue;
382 } 389 }
383 else if (CHECK_GEOM_OPS ("propscale")) 390 else if (CHECK_GEOM_OPS ("propscale"))
384 { 391 {
385 if (w == 0 && h == 0)
386 {
387 w = windowScale;
388 geom_flags |= WidthValue;
389 }
390 new_flags |= propScale; 392 new_flags |= propScale;
391 } 393 }
392 else if (CHECK_GEOM_OPS ("hscale")) 394 else if (CHECK_GEOM_OPS ("hscale"))
393 { 395 {
394 if (w == 0) w = windowScale; 396 if (w == 0) w = windowScale;
443 //fprintf (stderr, "flags = %lX, scale = %ux%u, align=%+d%+d\n", 445 //fprintf (stderr, "flags = %lX, scale = %ux%u, align=%+d%+d\n",
444 // flags, h_scale, v_scale, h_align, v_align); 446 // flags, h_scale, v_scale, h_align, v_align);
445 return (changed > 0); 447 return (changed > 0);
446} 448}
447 449
450void
451bgPixmap_t::get_image_geometry (int image_width, int image_height, int &w, int &h, int &x, int &y)
452{
453 int target_width = target->szHint.width;
454 int target_height = target->szHint.height;
455
456 if (flags & propScale)
457 {
458 float scale = (float)target_width / image_width;
459 min_it (scale, (float)target_height / image_height);
460 w = image_width * scale + 0.5;
461 h = image_height * scale + 0.5;
462 }
463 else
464 {
465 w = h_scale * target_width / 100;
466 h = v_scale * target_height / 100;
467 }
468
469 if (h_align == rootAlign || v_align == rootAlign)
470 {
471 target->get_window_origin (x, y);
472 x = -x;
473 y = -y;
474 }
475
476 if (h_align != rootAlign)
477 x = make_align_position (h_align, target_width, w > 0 ? w : image_width);
478
479 if (v_align != rootAlign)
480 y = make_align_position (v_align, target_height, h > 0 ? h : image_height);
481
482 flags &= ~sizeSensitive;
483 if (h_scale != 0 || v_scale != 0
484 || h_align != 0 || v_align != 0
485 || image_width > target_width || image_height > target_height)
486 flags |= sizeSensitive;
487}
488
448# ifdef HAVE_AFTERIMAGE 489# ifdef HAVE_AFTERIMAGE
449bool 490bool
450bgPixmap_t::render_image (unsigned long background_flags) 491bgPixmap_t::render_image (unsigned long background_flags)
451{ 492{
452 if (target == NULL) 493 if (target == NULL)
496 int new_pmap_width = target_width; 537 int new_pmap_width = target_width;
497 int new_pmap_height = target_height; 538 int new_pmap_height = target_height;
498 539
499 int x = 0; 540 int x = 0;
500 int y = 0; 541 int y = 0;
501 int w = h_scale * target_width / 100; 542 int w = 0;
502 int h = v_scale * target_height / 100; 543 int h = 0;
503 544
504 TIMING_TEST_START (asim); 545 TIMING_TEST_START (asim);
505 546
506 if (original_asim) 547 if (original_asim)
507 { 548 get_image_geometry (original_asim->width, original_asim->height, w, h, x, y);
508 if (h_align == rootAlign || v_align == rootAlign)
509 {
510 target->get_window_origin (x, y);
511 x = -x;
512 y = -y;
513 }
514
515 if (h_align != rootAlign)
516 x = make_align_position (h_align, target_width, w > 0 ? w : (int)original_asim->width);
517
518 if (v_align != rootAlign)
519 y = make_align_position (v_align, target_height, h > 0 ? h : (int)original_asim->height);
520 }
521 549
522 if (!original_asim 550 if (!original_asim
523 || x >= target_width 551 || x >= target_width
524 || y >= target_height 552 || y >= target_height
525 || (w > 0 && x + w <= 0) 553 || (w > 0 && x + w <= 0)
557 100, ASIMAGE_QUALITY_DEFAULT); 585 100, ASIMAGE_QUALITY_DEFAULT);
558 } 586 }
559 587
560 if (background == NULL) 588 if (background == NULL)
561 { 589 {
562 /* if tiling - pixmap has to be sized exactly as the image,
563 but there is no need to make it bigger than the window! */
564 if (h_scale == 0)
565 new_pmap_width = min (result->width, target_width);
566 if (v_scale == 0)
567 new_pmap_height = min (result->height, target_height);
568 /* we also need to tile our image in one or both directions */
569 if (h_scale == 0 || v_scale == 0) 590 if (h_scale == 0 || v_scale == 0)
570 { 591 {
592 /* if tiling - pixmap has to be sized exactly as the image,
593 but there is no need to make it bigger than the window! */
594 new_pmap_width = min (result->width, target_width);
595 new_pmap_height = min (result->height, target_height);
596
597 /* we also need to tile our image in both directions */
571 ASImage *tmp = tile_asimage (target->asv, result, 598 ASImage *tmp = tile_asimage (target->asv, result,
572 (h_scale > 0) ? 0 : (int)result->width - x, 599 (int)result->width - x,
573 (v_scale > 0) ? 0 : (int)result->height - y, 600 (int)result->height - y,
574 new_pmap_width, 601 new_pmap_width,
575 new_pmap_height, 602 new_pmap_height,
576 TINT_LEAVE_SAME, ASA_XImage, 603 TINT_LEAVE_SAME, ASA_XImage,
577 100, ASIMAGE_QUALITY_DEFAULT); 604 100, ASIMAGE_QUALITY_DEFAULT);
578 if (tmp) 605 if (tmp)
676 703
677 int src_x = 0, src_y = 0, dst_x = 0, dst_y = 0; 704 int src_x = 0, src_y = 0, dst_x = 0, dst_y = 0;
678 int dst_width = result->width, dst_height = result->height; 705 int dst_width = result->width, dst_height = result->height;
679 if (background == NULL) 706 if (background == NULL)
680 { 707 {
708 if (!(h_scale == 0 || v_scale == 0))
709 {
681 if (h_scale > 0) src_x = make_clip_rectangle (x, result->width , new_pmap_width , dst_x, dst_width ); 710 src_x = make_clip_rectangle (x, result->width , new_pmap_width , dst_x, dst_width );
682 if (v_scale > 0) src_y = make_clip_rectangle (y, result->height, new_pmap_height, dst_y, dst_height); 711 src_y = make_clip_rectangle (y, result->height, new_pmap_height, dst_y, dst_height);
712 }
683 713
684 if (dst_x > 0 || dst_y > 0 714 if (dst_x > 0 || dst_y > 0
685 || dst_x + dst_width < new_pmap_width 715 || dst_x + dst_width < new_pmap_width
686 || dst_y + dst_height < new_pmap_height) 716 || dst_y + dst_height < new_pmap_height)
687 XFillRectangle (target->dpy, pixmap, gc, 0, 0, new_pmap_width, new_pmap_height); 717 XFillRectangle (target->dpy, pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
704 destroy_asimage (&background); 734 destroy_asimage (&background);
705 735
706 return ret; 736 return ret;
707} 737}
708# endif /* HAVE_AFTERIMAGE */ 738# endif /* HAVE_AFTERIMAGE */
739
740# ifdef HAVE_PIXBUF
741bool
742bgPixmap_t::render_image (unsigned long background_flags)
743{
744 if (target == NULL)
745 return false;
746
747 if (!pixbuf)
748 return false;
749
750 // TODO: add alpha blending
751 if (background_flags)
752 return false;
753
754 GdkPixbuf *result;
755
756 int image_width = gdk_pixbuf_get_width (pixbuf);
757 int image_height = gdk_pixbuf_get_height (pixbuf);
758
759 int target_width = target->szHint.width;
760 int target_height = target->szHint.height;
761 int new_pmap_width = target_width;
762 int new_pmap_height = target_height;
763
764 int x = 0;
765 int y = 0;
766 int w = 0;
767 int h = 0;
768
769 get_image_geometry (image_width, image_height, w, h, x, y);
770
771 if (x >= target_width
772 || y >= target_height
773 || (w > 0 && x + w <= 0)
774 || (h > 0 && y + h <= 0))
775 return false;
776
777 result = pixbuf;
778
779 if ((w > 0 && w != image_width)
780 || (h > 0 && h != image_height))
781 {
782 result = gdk_pixbuf_scale_simple (pixbuf,
783 w > 0 ? w : image_width,
784 h > 0 ? h : image_height,
785 GDK_INTERP_BILINEAR);
786 }
787
788 bool ret = false;
789
790 if (result)
791 {
792 XGCValues gcv;
793 GC gc;
794
795 image_width = gdk_pixbuf_get_width (result);
796 image_height = gdk_pixbuf_get_height (result);
797
798 if (h_scale == 0 || v_scale == 0)
799 {
800 new_pmap_width = min (image_width, target_width);
801 new_pmap_height = min (image_height, target_height);
802 }
803
804 if (pixmap)
805 {
806 if (pmap_width != new_pmap_width
807 || pmap_height != new_pmap_height
808 || pmap_depth != target->depth)
809 {
810 XFreePixmap (target->dpy, pixmap);
811 pixmap = None;
812 }
813 }
814
815 if (pixmap == None)
816 {
817 pixmap = XCreatePixmap (target->dpy, target->vt, new_pmap_width, new_pmap_height, target->depth);
818 pmap_width = new_pmap_width;
819 pmap_height = new_pmap_height;
820 pmap_depth = target->depth;
821 }
822
823 gcv.foreground = target->pix_colors[Color_bg];
824 gc = XCreateGC (target->dpy, target->vt, GCForeground, &gcv);
825
826 if (h_scale == 0 || v_scale == 0)
827 {
828 Pixmap tile = XCreatePixmap (target->dpy, target->vt, image_width, image_height, target->depth);
829 gdk_pixbuf_xlib_render_to_drawable (result, tile, gc,
830 0, 0,
831 0, 0,
832 image_width, image_height,
833 XLIB_RGB_DITHER_NONE,
834 0, 0);
835
836 gcv.tile = tile;
837 gcv.fill_style = FillTiled;
838 gcv.ts_x_origin = x;
839 gcv.ts_y_origin = y;
840 XChangeGC (target->dpy, gc, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
841
842 XFillRectangle (target->dpy, pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
843 XFreePixmap (target->dpy, tile);
844 }
845 else
846 {
847 int src_x, src_y, dst_x, dst_y;
848 int dst_width, dst_height;
849
850 src_x = make_clip_rectangle (x, image_width , new_pmap_width , dst_x, dst_width );
851 src_y = make_clip_rectangle (y, image_height, new_pmap_height, dst_y, dst_height);
852
853 if (dst_x > 0 || dst_y > 0
854 || dst_x + dst_width < new_pmap_width
855 || dst_y + dst_height < new_pmap_height)
856 XFillRectangle (target->dpy, pixmap, gc, 0, 0, new_pmap_width, new_pmap_height);
857
858 if (dst_x < new_pmap_width && dst_y < new_pmap_height)
859 gdk_pixbuf_xlib_render_to_drawable (result, pixmap, gc,
860 src_x, src_y,
861 dst_x, dst_y,
862 dst_width, dst_height,
863 XLIB_RGB_DITHER_NONE,
864 0, 0);
865 }
866
867 if (result != pixbuf)
868 g_object_unref (result);
869
870 XFreeGC (target->dpy, gc);
871
872 ret = true;
873 }
874
875 return ret;
876}
877# endif /* HAVE_PIXBUF */
709 878
710bool 879bool
711bgPixmap_t::set_file (const char *file) 880bgPixmap_t::set_file (const char *file)
712{ 881{
713 assert (file); 882 assert (file);
726# ifdef HAVE_AFTERIMAGE 895# ifdef HAVE_AFTERIMAGE
727 if (!target->asimman) 896 if (!target->asimman)
728 target->asimman = create_generic_imageman (target->rs[Rs_path]); 897 target->asimman = create_generic_imageman (target->rs[Rs_path]);
729 original_asim = get_asimage (target->asimman, file, 0xFFFFFFFF, 100); 898 original_asim = get_asimage (target->asimman, file, 0xFFFFFFFF, 100);
730 if (original_asim) 899 if (original_asim)
900 have_image = true;
901 return have_image;
902# endif
903
904# ifdef HAVE_PIXBUF
905 pixbuf = gdk_pixbuf_new_from_file (file, NULL);
906 if (pixbuf)
731 have_image = true; 907 have_image = true;
732 return have_image; 908 return have_image;
733# endif 909# endif
734 } 910 }
735 911

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines