gitg-cell-renderer-lanes.vala 8.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * This file is part of gitg
 *
 * Copyright (C) 2012 - Jesse van den Kieboom
 *
 * gitg is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * gitg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gitg. If not, see <http://www.gnu.org/licenses/>.
 */

20
namespace Gitg
21
{
22
	public class CellRendererLanes : Gtk.CellRendererText
23
	{
Paolo Borelli's avatar
Paolo Borelli committed
24 25
		public Commit? commit { get; set; }
		public Commit? next_commit { get; set; }
26 27
		public uint lane_width { get; set; default = 16; }
		public uint dot_width { get; set; default = 10; }
Paolo Borelli's avatar
Paolo Borelli committed
28
		public unowned SList<Ref> labels { get; set; }
29 30 31

		private int d_last_height;

Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
32 33
		private delegate double DirectionFunc(double i);

34
		private uint num_visible_lanes
35
		{
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
			get
			{
				int ret = 0;
				int trailing_hidden = 0;

				foreach (var lane in commit.get_lanes())
				{
					++ret;

					if ((lane.tag & LaneTag.HIDDEN) != 0)
					{
						trailing_hidden++;
					}
					else
					{
						trailing_hidden = 0;
					}
				}

				return ret - trailing_hidden;
			}
57 58 59 60
		}

		private uint total_width(Gtk.Widget widget)
		{
61
			return num_visible_lanes * lane_width +
62 63 64
			       LabelRenderer.width(widget, font_desc, labels);
		}

65 66 67
		public override void get_preferred_width(Gtk.Widget widget,
		                                         out int    minimum_width,
		                                         out int    natural_width)
68
		{
69
			base.get_preferred_width(widget, out minimum_width, out natural_width);
70

71 72 73 74 75 76
			var w = (int)total_width(widget);

			if (w > minimum_width)
			{
				minimum_width = w;
			}
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
		}

		private void draw_arrow(Cairo.Context context,
		                        Gdk.Rectangle area,
		                        uint          laneidx,
		                        bool          top)
		{
			double cw = lane_width;
			double xpos = area.x + laneidx * cw + cw / 2.0;
			double df = (top ? -1 : 1) * 0.25 * area.height;
			double ypos = area.y + area.height / 2.0 + df;
			double q = cw / 4.0;

			context.move_to(xpos - q, ypos + (top ? q : -q));
			context.line_to(xpos, ypos);
			context.line_to(xpos + q, ypos + (top ? q : -q));
			context.stroke();

			context.move_to(xpos, ypos);
			context.line_to(xpos, ypos - df);
			context.stroke();
		}

		private void draw_arrows(Cairo.Context context,
		                         Gdk.Rectangle area)
		{
			uint to = 0;

105
			foreach (var lane in commit.get_lanes())
106 107 108 109
			{
				var color = lane.color;
				context.set_source_rgb(color.r, color.g, color.b);

Paolo Borelli's avatar
Paolo Borelli committed
110
				if (lane.tag == LaneTag.START)
111 112 113
				{
					draw_arrow(context, area, to, true);
				}
Paolo Borelli's avatar
Paolo Borelli committed
114
				else if (lane.tag == LaneTag.END)
115 116 117 118 119 120 121 122 123 124
				{
					draw_arrow(context, area, to, false);
				}

				++to;
			}
		}

		private void draw_paths_real(Cairo.Context context,
		                             Gdk.Rectangle area,
Paolo Borelli's avatar
Paolo Borelli committed
125
		                             Commit?       commit,
Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
126
		                             DirectionFunc f,
127 128 129 130 131 132 133 134 135 136 137
		                             double        yoffset)
		{
			if (commit == null)
			{
				return;
			}

			int to = 0;
			double cw = lane_width;
			double ch = area.height / 2.0;

138
			foreach (var lane in commit.get_lanes())
139
			{
140 141 142 143 144 145
				if ((lane.tag & LaneTag.HIDDEN) != 0)
				{
					++to;
					continue;
				}

146 147 148 149 150
				var color = lane.color;
				context.set_source_rgb(color.r, color.g, color.b);

				foreach (var from in lane.from)
				{
Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
151 152
					double x1 = area.x + f(from * cw + cw / 2.0);
					double x2 = area.x + f(to * cw + cw / 2.0);
153 154 155 156 157 158 159 160 161 162 163 164 165 166
					double y1 = area.y + yoffset * ch;
					double y2 = area.y + (yoffset + 1) * ch;
					double y3 = area.y + (yoffset + 2) * ch;

					context.move_to(x1, y1);
					context.curve_to(x1, y2, x2, y2, x2, y3);
					context.stroke();
				}

				++to;
			}
		}

		private void draw_top_paths(Cairo.Context context,
Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
167 168
		                            Gdk.Rectangle area,
		                            DirectionFunc f)
169
		{
Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
170
			draw_paths_real(context, area, commit, f, -1);
171 172 173
		}

		private void draw_bottom_paths(Cairo.Context context,
Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
174 175
		                               Gdk.Rectangle area,
		                               DirectionFunc f)
176
		{
Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
177
			draw_paths_real(context, area, next_commit, f, 1);
178 179 180
		}

		private void draw_paths(Cairo.Context context,
Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
181 182
		                        Gdk.Rectangle area,
		                        DirectionFunc f)
183 184 185 186
		{
			context.set_line_width(2.0);
			context.set_line_cap(Cairo.LineCap.ROUND);

Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
187 188 189 190
			context.save();

			draw_top_paths(context, area, f);
			draw_bottom_paths(context, area, f);
191
			draw_arrows(context, area);
Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
192 193

			context.restore();
194 195 196
		}

		private void draw_indicator(Cairo.Context context,
Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
197 198
		                            Gdk.Rectangle area,
		                            DirectionFunc f)
199 200 201 202 203 204 205
		{
			double offset;
			double radius;

			offset = commit.mylane * lane_width + (lane_width - dot_width) / 2.0;
			radius = dot_width / 2.0;

206
			context.set_line_width(0.0);
207

Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
208
			context.arc(area.x + f(offset + radius),
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
			            area.y + area.height / 2.0,
			            radius,
			            0,
			            2 * Math.PI);

			context.set_source_rgb(0, 0, 0);
			context.stroke_preserve();

			if (commit.lane != null)
			{
				var color = commit.lane.color;
				context.set_source_rgb(color.r, color.g, color.b);
			}

			context.fill();
		}

		private void draw_labels(Cairo.Context context,
Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
227 228
		                         Gtk.Widget    widget,
		                         Gdk.Rectangle area)
229
		{
Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
230 231
			int offset;

232
			offset = (int)(num_visible_lanes * lane_width);
233

Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
234
			var rtl = (widget.get_style_context().get_state() & Gtk.StateFlags.DIR_RTL) != 0;
235

Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
236 237 238 239 240 241
			if (rtl)
			{
				offset = -offset;
			}

			context.save();
242 243
			context.translate(offset, 0);
			LabelRenderer.draw(widget, font_desc, context, labels, area);
Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
			context.restore();
		}

		private void draw_lane(Cairo.Context context,
		                       Gtk.Widget    widget,
		                       Gdk.Rectangle area)
		{
			DirectionFunc f;

			var rtl = (widget.get_style_context().get_state() & Gtk.StateFlags.DIR_RTL) != 0;

			context.save();

			if (rtl)
			{
				context.translate(area.width, 0);
				f = (a) => -a;
			}
			else
			{
				f = (a) => a;
			}

			draw_paths(context, area, f);
			draw_indicator(context, area, f);

			context.restore();
271 272 273 274 275 276 277 278
		}

		public override void render(Cairo.Context         context,
		                            Gtk.Widget            widget,
		                            Gdk.Rectangle         area,
		                            Gdk.Rectangle         cell_area,
		                            Gtk.CellRendererState flags)
		{
279 280
			var ncell_area = cell_area;
			var narea = area;
281

Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
282 283
			var rtl = (widget.get_style_context().get_state() & Gtk.StateFlags.DIR_RTL) != 0;

284
			d_last_height = area.height;
285

286 287 288
			if (commit != null)
			{
				context.save();
289

290 291
				Gdk.cairo_rectangle(context, area);
				context.clip();
292

Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
293 294
				draw_lane(context, widget, area);
				draw_labels(context, widget, area);
295

296
				var tw = total_width(widget);
297

Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
298 299 300 301 302 303 304
				if (!rtl)
				{
					narea.x += (int)tw;
					ncell_area.x += (int)tw;
				}
				else
				{
305 306
					narea.width -= (int)tw;
					ncell_area.width -= (int)tw;
Jesse van den Kieboom's avatar
Jesse van den Kieboom committed
307
				}
308

309 310
				context.restore();
			}
311

312 313 314 315 316
			if (rtl == (Pango.find_base_dir(text, -1) != Pango.Direction.RTL))
			{
				xalign = 1.0f;
			}

317 318
			base.render(context, widget, narea, ncell_area, flags);
		}
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338

		public Ref? get_ref_at_pos(Gtk.Widget widget,
		                           int        x,
		                           int        cell_w,
		                           out int    hot_x)
		{
			var rtl = (widget.get_style_context().get_state() & Gtk.StateFlags.DIR_RTL) != 0;
			var offset = (int)(labels.length() * lane_width);

			if (rtl)
			{
				x = cell_w - x;
			}

			return LabelRenderer.get_ref_at_pos(widget,
			                                    font_desc,
			                                    labels,
			                                    x - offset,
			                                    out hot_x);
		}
339 340 341
	}
}

Paolo Borelli's avatar
Paolo Borelli committed
342
// ex:ts=4 noet