2012-07-02 18:02:34 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2013-06-23 12:40:04 +00:00
|
|
|
namespace Gitg
|
2012-04-22 13:13:02 +00:00
|
|
|
{
|
|
|
|
class CellRendererLanes : Gtk.CellRendererText
|
|
|
|
{
|
2013-06-23 19:06:29 +00:00
|
|
|
public Commit? commit { get; set; }
|
|
|
|
public Commit? next_commit { get; set; }
|
2012-04-22 13:13:02 +00:00
|
|
|
public uint lane_width { get; set; default = 16; }
|
|
|
|
public uint dot_width { get; set; default = 10; }
|
2013-06-23 19:06:29 +00:00
|
|
|
public unowned SList<Ref> labels { get; set; }
|
2012-04-22 13:13:02 +00:00
|
|
|
|
|
|
|
private int d_last_height;
|
|
|
|
|
2014-01-04 22:14:21 +00:00
|
|
|
private delegate double DirectionFunc(double i);
|
|
|
|
|
2012-04-22 13:13:02 +00:00
|
|
|
private uint num_lanes
|
|
|
|
{
|
2012-04-24 22:01:27 +00:00
|
|
|
get { return commit.get_lanes().length(); }
|
2012-04-22 13:13:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private uint total_width(Gtk.Widget widget)
|
|
|
|
{
|
|
|
|
return num_lanes * lane_width +
|
|
|
|
LabelRenderer.width(widget, font_desc, labels);
|
|
|
|
}
|
|
|
|
|
2014-01-04 22:26:07 +00:00
|
|
|
public override void get_preferred_width(Gtk.Widget widget,
|
|
|
|
out int minimum_width,
|
|
|
|
out int natural_width)
|
2012-04-22 13:13:02 +00:00
|
|
|
{
|
2014-01-04 22:26:07 +00:00
|
|
|
base.get_preferred_width(widget, out minimum_width, out natural_width);
|
2012-04-22 13:13:02 +00:00
|
|
|
|
2014-01-04 22:26:07 +00:00
|
|
|
var w = (int)total_width(widget);
|
|
|
|
|
|
|
|
if (w > minimum_width)
|
|
|
|
{
|
|
|
|
minimum_width = w;
|
|
|
|
}
|
2012-04-22 13:13:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2012-04-24 22:01:27 +00:00
|
|
|
foreach (var lane in commit.get_lanes())
|
2012-04-22 13:13:02 +00:00
|
|
|
{
|
|
|
|
var color = lane.color;
|
|
|
|
context.set_source_rgb(color.r, color.g, color.b);
|
|
|
|
|
2013-06-23 19:06:29 +00:00
|
|
|
if (lane.tag == LaneTag.START)
|
2012-04-22 13:13:02 +00:00
|
|
|
{
|
|
|
|
draw_arrow(context, area, to, true);
|
|
|
|
}
|
2013-06-23 19:06:29 +00:00
|
|
|
else if (lane.tag == LaneTag.END)
|
2012-04-22 13:13:02 +00:00
|
|
|
{
|
|
|
|
draw_arrow(context, area, to, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
++to;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void draw_paths_real(Cairo.Context context,
|
|
|
|
Gdk.Rectangle area,
|
2013-06-23 19:06:29 +00:00
|
|
|
Commit? commit,
|
2014-01-04 22:14:21 +00:00
|
|
|
DirectionFunc f,
|
2012-04-22 13:13:02 +00:00
|
|
|
double yoffset)
|
|
|
|
{
|
|
|
|
if (commit == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int to = 0;
|
|
|
|
double cw = lane_width;
|
|
|
|
double ch = area.height / 2.0;
|
|
|
|
|
2012-04-24 22:01:27 +00:00
|
|
|
foreach (var lane in commit.get_lanes())
|
2012-04-22 13:13:02 +00:00
|
|
|
{
|
|
|
|
var color = lane.color;
|
|
|
|
context.set_source_rgb(color.r, color.g, color.b);
|
|
|
|
|
|
|
|
foreach (var from in lane.from)
|
|
|
|
{
|
2014-01-04 22:14:21 +00:00
|
|
|
double x1 = area.x + f(from * cw + cw / 2.0);
|
|
|
|
double x2 = area.x + f(to * cw + cw / 2.0);
|
2012-04-22 13:13:02 +00:00
|
|
|
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,
|
2014-01-04 22:14:21 +00:00
|
|
|
Gdk.Rectangle area,
|
|
|
|
DirectionFunc f)
|
2012-04-22 13:13:02 +00:00
|
|
|
{
|
2014-01-04 22:14:21 +00:00
|
|
|
draw_paths_real(context, area, commit, f, -1);
|
2012-04-22 13:13:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void draw_bottom_paths(Cairo.Context context,
|
2014-01-04 22:14:21 +00:00
|
|
|
Gdk.Rectangle area,
|
|
|
|
DirectionFunc f)
|
2012-04-22 13:13:02 +00:00
|
|
|
{
|
2014-01-04 22:14:21 +00:00
|
|
|
draw_paths_real(context, area, next_commit, f, 1);
|
2012-04-22 13:13:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void draw_paths(Cairo.Context context,
|
2014-01-04 22:14:21 +00:00
|
|
|
Gdk.Rectangle area,
|
|
|
|
DirectionFunc f)
|
2012-04-22 13:13:02 +00:00
|
|
|
{
|
|
|
|
context.set_line_width(2.0);
|
|
|
|
context.set_line_cap(Cairo.LineCap.ROUND);
|
|
|
|
|
2014-01-04 22:14:21 +00:00
|
|
|
context.save();
|
|
|
|
|
|
|
|
draw_top_paths(context, area, f);
|
|
|
|
draw_bottom_paths(context, area, f);
|
2012-04-22 13:13:02 +00:00
|
|
|
draw_arrows(context, area);
|
2014-01-04 22:14:21 +00:00
|
|
|
|
|
|
|
context.restore();
|
2012-04-22 13:13:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void draw_indicator(Cairo.Context context,
|
2014-01-04 22:14:21 +00:00
|
|
|
Gdk.Rectangle area,
|
|
|
|
DirectionFunc f)
|
2012-04-22 13:13:02 +00:00
|
|
|
{
|
|
|
|
double offset;
|
|
|
|
double radius;
|
|
|
|
|
|
|
|
offset = commit.mylane * lane_width + (lane_width - dot_width) / 2.0;
|
|
|
|
radius = dot_width / 2.0;
|
|
|
|
|
|
|
|
context.set_line_width(2.0);
|
|
|
|
|
2014-01-04 22:14:21 +00:00
|
|
|
context.arc(area.x + f(offset + radius),
|
2012-04-22 13:13:02 +00:00
|
|
|
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,
|
2014-01-04 22:14:21 +00:00
|
|
|
Gtk.Widget widget,
|
|
|
|
Gdk.Rectangle area)
|
2012-04-22 13:13:02 +00:00
|
|
|
{
|
2014-01-04 22:14:21 +00:00
|
|
|
int offset;
|
|
|
|
|
|
|
|
offset = (int)(num_lanes * lane_width);
|
2012-04-22 13:13:02 +00:00
|
|
|
|
2014-01-04 22:14:21 +00:00
|
|
|
var rtl = (widget.get_style_context().get_state() & Gtk.StateFlags.DIR_RTL) != 0;
|
2012-04-22 13:13:02 +00:00
|
|
|
|
2014-01-04 22:14:21 +00:00
|
|
|
if (rtl)
|
|
|
|
{
|
|
|
|
offset = -offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
context.save();
|
2012-04-22 13:13:02 +00:00
|
|
|
context.translate(offset, 0);
|
|
|
|
LabelRenderer.draw(widget, font_desc, context, labels, area);
|
2014-01-04 22:14:21 +00:00
|
|
|
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();
|
2012-04-22 13:13:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void render(Cairo.Context context,
|
|
|
|
Gtk.Widget widget,
|
|
|
|
Gdk.Rectangle area,
|
|
|
|
Gdk.Rectangle cell_area,
|
|
|
|
Gtk.CellRendererState flags)
|
|
|
|
{
|
2012-04-24 22:01:27 +00:00
|
|
|
var ncell_area = cell_area;
|
|
|
|
var narea = area;
|
2012-04-22 13:13:02 +00:00
|
|
|
|
2014-01-04 22:14:21 +00:00
|
|
|
var rtl = (widget.get_style_context().get_state() & Gtk.StateFlags.DIR_RTL) != 0;
|
|
|
|
|
2012-04-24 22:01:27 +00:00
|
|
|
d_last_height = area.height;
|
2012-04-22 13:13:02 +00:00
|
|
|
|
2012-04-24 22:01:27 +00:00
|
|
|
if (commit != null)
|
|
|
|
{
|
|
|
|
context.save();
|
2012-04-22 13:13:02 +00:00
|
|
|
|
2012-04-24 22:01:27 +00:00
|
|
|
Gdk.cairo_rectangle(context, area);
|
|
|
|
context.clip();
|
2012-04-22 13:13:02 +00:00
|
|
|
|
2014-01-04 22:14:21 +00:00
|
|
|
draw_lane(context, widget, area);
|
|
|
|
draw_labels(context, widget, area);
|
2012-04-22 13:13:02 +00:00
|
|
|
|
2012-04-24 22:01:27 +00:00
|
|
|
var tw = total_width(widget);
|
2012-04-22 13:13:02 +00:00
|
|
|
|
2014-01-04 22:14:21 +00:00
|
|
|
if (!rtl)
|
|
|
|
{
|
|
|
|
narea.x += (int)tw;
|
|
|
|
ncell_area.x += (int)tw;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-01-04 23:00:21 +00:00
|
|
|
narea.width -= (int)tw;
|
|
|
|
ncell_area.width -= (int)tw;
|
2014-01-04 22:14:21 +00:00
|
|
|
}
|
2012-04-22 13:13:02 +00:00
|
|
|
|
2012-04-24 22:01:27 +00:00
|
|
|
context.restore();
|
|
|
|
}
|
2012-04-22 13:13:02 +00:00
|
|
|
|
|
|
|
base.render(context, widget, narea, ncell_area, flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 19:06:29 +00:00
|
|
|
// ex:ts=4 noet
|