1/*
2 * This file is part of gitg
3 *
4 * Copyright (C) 2016 - Jesse van den Kieboom
5 *
6 * gitg is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * gitg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with gitg. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20class Gitg.DiffImageOverlay : DiffImageComposite
21{
22	private double d_alpha;
23
24	public double alpha
25	{
26		get { return d_alpha; }
27
28		set
29		{
30			var newalpha = double.max(0, double.min(value, 1));
31
32			if (newalpha != d_alpha)
33			{
34				d_alpha = newalpha;
35				queue_draw();
36			}
37		}
38	}
39
40	construct
41	{
42		alpha = 0.5;
43	}
44
45	protected override bool draw(Cairo.Context cr)
46	{
47		base.draw(cr);
48
49		var window = get_window();
50
51		Gtk.Allocation alloc;
52		get_allocation(out alloc);
53
54		int image_width, image_height;
55		get_sizing(alloc.width, out image_width, out image_height);
56
57		var old_surface = cache.get_old_surface(window);
58		var new_surface = cache.get_new_surface(window);
59
60		int x = (alloc.width - image_width) / 2;
61		int y = 0;
62
63		if (old_surface != null && d_alpha != 1)
64		{
65			cr.set_source_surface(old_surface, x, y);
66			cr.paint_with_alpha(1 - d_alpha);
67		}
68
69		if (new_surface != null && d_alpha != 0)
70		{
71			cr.set_source_surface(new_surface, x, y);
72			cr.paint_with_alpha(d_alpha);
73		}
74
75		return true;
76	}
77
78	protected override void realize()
79	{
80		base.realize();
81		queue_resize();
82	}
83}
84