1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
/*
* 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/>.
*/
namespace Gitg
{
public class LabelRenderer
{
private const int margin = 2;
private const int padding = 6;
private static string label_text(Ref r)
{
var escaped = Markup.escape_text(r.parsed_name.shortname);
return "<span size='smaller'>%s</span>".printf(escaped);
}
private static int get_label_width(Pango.Layout layout,
Ref r)
{
var smaller = label_text(r);
int w;
layout.set_markup(smaller, -1);
layout.get_pixel_size(out w, null);
return w + padding * 2;
}
public static int width(Gtk.Widget widget,
Pango.FontDescription *font,
SList<Ref> labels)
{
if (labels == null)
{
return 0;
}
int ret = 0;
var ctx = widget.get_pango_context();
var layout = new Pango.Layout(ctx);
layout.set_font_description(font);
foreach (Ref r in labels)
{
ret += get_label_width(layout, r) + margin;
}
return ret + margin;
}
private static string class_from_ref(RefType type)
{
string style_class;
switch (type)
{
case RefType.BRANCH:
style_class = "branch";
break;
case RefType.REMOTE:
style_class = "remote";
break;
case RefType.TAG:
style_class = "tag";
break;
case RefType.STASH:
style_class = "stash";
break;
default:
style_class = null;
break;
}
return style_class;
}
private static int render_label(Gtk.Widget widget,
Cairo.Context cr,
Pango.Layout layout,
Ref r,
double x,
double y,
int height,
bool use_state)
{
var context = widget.get_style_context();
var smaller = label_text(r);
layout.set_markup(smaller, -1);
int w;
int h;
layout.get_pixel_size(out w, out h);
context.save();
var style_class = class_from_ref(r.parsed_name.rtype);
if (style_class != null)
{
context.add_class(style_class);
}
var rtl = (widget.get_style_context().get_state() & Gtk.StateFlags.DIR_RTL) != 0;
if (rtl)
{
x -= w + padding * 2;
}
context.render_background(cr,
x,
y + margin,
w + padding * 2,
height - margin * 2);
context.render_frame(cr,
x,
y + margin,
w + padding * 2,
height - margin * 2);
context.render_layout(cr,
x + padding,
y + (height - h) / 2.0 - 1,
layout);
context.restore();
return w;
}
public static void draw(Gtk.Widget widget,
Pango.FontDescription font,
Cairo.Context context,
SList<Ref> labels,
Gdk.Rectangle area)
{
double pos;
var rtl = (widget.get_style_context().get_state() & Gtk.StateFlags.DIR_RTL) != 0;
if (!rtl)
{
pos = area.x + margin + 0.5;
}
else
{
pos = area.x + area.width - margin - 0.5;
}
context.save();
context.set_line_width(1.0);
var ctx = widget.get_pango_context();
var layout = new Pango.Layout(ctx);
layout.set_font_description(font);
foreach (Ref r in labels)
{
var w = render_label(widget,
context,
layout,
r,
(int)pos,
area.y,
area.height,
true);
var o = w + padding * 2 + margin;
pos += rtl ? -o : o;
}
context.restore();
}
public static Ref? get_ref_at_pos(Gtk.Widget widget,
Pango.FontDescription font,
SList<Ref> labels,
int x,
out int hot_x)
{
hot_x = 0;
if (labels == null)
{
return null;
}
var ctx = widget.get_pango_context();
var layout = new Pango.Layout(ctx);
layout.set_font_description(font);
int start = margin;
Ref? ret = null;
foreach (Ref r in labels)
{
int width = get_label_width(layout, r);
if (x >= start && x <= start + width)
{
ret = r;
hot_x = x - start;
break;
}
start += width + margin;
}
return ret;
}
private static uchar convert_color_channel(uchar color,
uchar alpha)
{
return (uchar)((alpha != 0) ? (color / (alpha / 255.0)) : 0);
}
private static void convert_bgra_to_rgba(uchar[] src,
uchar[] dst,
int width,
int height)
{
int i = 0;
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
dst[i] = convert_color_channel(src[i + 2], src[i + 3]);
dst[i + 1] = convert_color_channel(src[i + 1], src[i + 3]);
dst[i + 2] = convert_color_channel(src[i], src[i + 3]);
dst[i + 3] = src[i + 3];
i += 4;
}
}
}
public static Gdk.Pixbuf render_ref(Gtk.Widget widget,
Pango.FontDescription font,
Ref r,
int height,
int minwidth)
{
var ctx = widget.get_pango_context();
var layout = new Pango.Layout(ctx);
layout.set_font_description(font);
int width = int.max(get_label_width(layout, r), minwidth);
var surface = new Cairo.ImageSurface(Cairo.Format.ARGB32,
width + 2,
height + 2);
var context = new Cairo.Context(surface);
context.set_line_width(1);
render_label(widget, context, layout, r, 1, 1, height, false);
var data = surface.get_data();
Gdk.Pixbuf ret = new Gdk.Pixbuf(Gdk.Colorspace.RGB,
true,
8,
width + 2,
height + 2);
var pixdata = ret.get_pixels();
convert_bgra_to_rgba(data, pixdata, width + 2, height + 2);
return ret;
}
}
}
// ex:ts=4 noet
|