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.DiffImageSlider : DiffImageComposite
21{
22	private double d_position;
23
24	public double position
25	{
26		get { return d_position; }
27
28		set
29		{
30			var newpos = double.max(0, double.min(value, 1));
31
32			if (newpos != d_position)
33			{
34				d_position = newpos;
35				queue_draw();
36			}
37		}
38	}
39
40	construct
41	{
42		position = 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		int pos = (int)(image_width * position);
64
65		if (old_surface != null)
66		{
67			cr.save();
68			{
69				cr.rectangle(x, y, pos, image_height);
70				cr.clip();
71				cr.set_source_surface(old_surface, x, y);
72				cr.paint();
73			}
74			cr.restore();
75		}
76
77		if (new_surface != null)
78		{
79			cr.save();
80			{
81				cr.rectangle(x + pos, y, image_width - pos, image_height);
82				cr.clip();
83				cr.set_source_surface(new_surface, x, y);
84				cr.paint();
85			}
86			cr.restore();
87		}
88
89		return true;
90	}
91
92	protected override void realize()
93	{
94		base.realize();
95		queue_resize();
96	}
97}
98