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.DiffImageComposite : Gtk.DrawingArea
21{
22	public Gitg.DiffImageSurfaceCache cache { get; set; }
23
24	private void get_natural_size(out int image_width, out int image_height)
25	{
26		var pixbuf = cache.old_pixbuf;
27
28		if (pixbuf == null)
29		{
30			image_width = 0;
31			image_height = 0;
32
33			return;
34		}
35
36		var window = get_window();
37
38		double xscale = 1, yscale = 1;
39
40		if (window != null)
41		{
42			cache.get_old_surface(get_window()).get_device_scale(out xscale, out yscale);
43		}
44
45		image_width = (int)(pixbuf.get_width() / xscale);
46		image_height = (int)(pixbuf.get_height() / yscale);
47	}
48
49	protected void get_sizing(int width, out int image_width, out int image_height)
50	{
51		get_natural_size(out image_width, out image_height);
52
53		// Scale down to fit in width
54		if (image_width > width)
55		{
56			image_height *= width / image_width;
57			image_width = width;
58		}
59	}
60
61	protected override void get_preferred_width(out int minimum_width, out int natural_width)
62	{
63		int natural_height;
64
65		get_natural_size(out natural_width, out natural_height);
66		minimum_width = 0;
67	}
68
69	protected override void get_preferred_height_for_width(int width, out int minimum_height, out int natural_height)
70	{
71		int image_width, image_height;
72
73		get_sizing(width, out image_width, out image_height);
74
75		minimum_height = image_height;
76		natural_height = image_height;
77	}
78
79	protected override Gtk.SizeRequestMode get_request_mode()
80	{
81		return Gtk.SizeRequestMode.HEIGHT_FOR_WIDTH;
82	}
83
84	protected override bool draw(Cairo.Context cr)
85	{
86		Gtk.Allocation alloc;
87		get_allocation(out alloc);
88
89		var ctx = get_style_context();
90
91		ctx.render_background(cr, alloc.x, alloc.y, alloc.width, alloc.height);
92		return true;
93	}
94
95	protected override void realize()
96	{
97		base.realize();
98		queue_resize();
99	}
100}
101