|
| 1 | +/* |
| 2 | + * Copyright © 2013–2014 Philipp Emanuel Weidmann <[email protected]> |
| 3 | + * Copyright © 2015-2016 RedHatter <[email protected]> |
| 4 | + * |
| 5 | + * Nemo vir est qui mundum non reddat meliorem. |
| 6 | + * |
| 7 | + * |
| 8 | + * This file is part of Final Term. |
| 9 | + * |
| 10 | + * Final Term is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU General Public License as published by |
| 12 | + * the Free Software Foundation, either version 3 of the License, or |
| 13 | + * (at your option) any later version. |
| 14 | + * |
| 15 | + * Final Term is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License |
| 21 | + * along with Final Term. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | + |
| 24 | +using Gtk; |
| 25 | +using Pango; |
| 26 | + |
| 27 | +/** |
| 28 | + * Custom TextView to draw double-wide and double-high text. |
| 29 | + */ |
| 30 | +public class TextView : Gtk.TextView { |
| 31 | + public TextView.with_buffer (TextBuffer buffer) { |
| 32 | + set_buffer(buffer); |
| 33 | + editable = false; |
| 34 | + cursor_visible = false; |
| 35 | + wrap_mode = Gtk.WrapMode.CHAR; |
| 36 | + } |
| 37 | + |
| 38 | + public override void draw_layer (TextViewLayer layer, Cairo.Context cr) { |
| 39 | + // Draw after text has been rendered |
| 40 | + if (layer != TextViewLayer.BELOW) |
| 41 | + return; |
| 42 | + |
| 43 | + //TODO: Cache layout |
| 44 | + var line = create_pango_layout(null); |
| 45 | + TextIter iter; |
| 46 | + |
| 47 | + // For each of the three types obtain the text using |
| 48 | + // forward_to_tag_toggle, apply line styles, set clip, and draw with |
| 49 | + // Cairo transforms |
| 50 | + |
| 51 | + // Double-wide: scale x by a factor of 2 |
| 52 | + buffer.get_iter_at_line(out iter, 0); |
| 53 | + var tag = buffer.tag_table.lookup("double-wide"); |
| 54 | + if (tag != null) { |
| 55 | + while (next_segment(cr, tag, ref iter, line)) { |
| 56 | + cr.scale(2.0, 1.0); |
| 57 | + Pango.cairo_show_layout(cr, line); |
| 58 | + cr.scale(0.5, 1.0); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Double-high top half: scale both x and y by a factor of 2 |
| 63 | + buffer.get_iter_at_line(out iter, 0); |
| 64 | + tag = buffer.tag_table.lookup("double-top"); |
| 65 | + if (tag != null) { |
| 66 | + while (next_segment(cr, tag, ref iter, line)) { |
| 67 | + cr.scale(2.0, 2.0); |
| 68 | + Pango.cairo_show_layout(cr, line); |
| 69 | + cr.scale(0.5, 0.5); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Double-high bottom half: scale both x and y by a factor of 2 and |
| 74 | + // shift y up one line |
| 75 | + buffer.get_iter_at_line(out iter, 0); |
| 76 | + tag = buffer.tag_table.lookup("double-bottom"); |
| 77 | + if (tag != null) { |
| 78 | + while (next_segment(cr, tag, ref iter, line)) { |
| 79 | + int line_height; |
| 80 | + get_line_yrange (iter, null, out line_height); |
| 81 | + cr.rel_move_to(0, -line_height); |
| 82 | + cr.scale(2.0, 2.0); |
| 83 | + Pango.cairo_show_layout(cr, line); |
| 84 | + cr.scale(0.5, 0.5); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private bool next_segment (Cairo.Context cr, TextTag tag, ref TextIter iter, Pango.Layout line) { |
| 90 | + if (!iter.forward_to_tag_toggle(tag)) |
| 91 | + return false; // No more segments |
| 92 | + |
| 93 | + var start = iter; |
| 94 | + iter.forward_to_tag_toggle(tag); |
| 95 | + |
| 96 | + // Extract text |
| 97 | + var text = buffer.get_text(start, iter, true); |
| 98 | + line.set_text(text, -1); |
| 99 | + |
| 100 | + // TODO: Set all attribues from tags |
| 101 | + |
| 102 | + // Set color attribute |
| 103 | + var color = get_default_attributes().appearance.fg_color; |
| 104 | + var attr = Pango.attr_foreground_new(color.red, color.green, color.blue); |
| 105 | + attr.start_index = 0; |
| 106 | + attr.end_index = text.length; |
| 107 | + var attr_list = new AttrList(); |
| 108 | + attr_list.change((owned) attr); |
| 109 | + line.set_attributes(attr_list); |
| 110 | + |
| 111 | + // Get position shifted by scroll |
| 112 | + Gdk.Rectangle pos_rect; |
| 113 | + get_cursor_locations(iter, out pos_rect, null); |
| 114 | + pos_rect.y -= (int) get_vadjustment().value; |
| 115 | + pos_rect.x -= (int) get_hadjustment().value; |
| 116 | + |
| 117 | + // clip and move Cairo |
| 118 | + int height; |
| 119 | + line.get_pixel_size(null, out height); |
| 120 | + cr.reset_clip(); |
| 121 | + cr.rectangle(pos_rect.x, pos_rect.y, get_allocated_width(), height); |
| 122 | + cr.clip(); |
| 123 | + cr.move_to(pos_rect.x, pos_rect.y); |
| 124 | + |
| 125 | + return true; |
| 126 | + } |
| 127 | +} |
0 commit comments