The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

LabelTool.vala in libbirdfont

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/LabelTool.vala.
Merge ../birdfont-2.x
1 /* 2 Copyright (C) 2014 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using Cairo; 16 17 namespace BirdFont { 18 19 public class LabelTool : Tool { 20 21 public string label { 22 get { 23 return label_text.text; 24 } 25 26 set { 27 clear_cache (); 28 label_text.set_text (value); 29 } 30 } 31 32 public string number { 33 get { 34 return counter_number; 35 } 36 37 set { 38 clear_cache (); 39 counter_number = value; 40 } 41 } 42 43 string counter_number = ""; 44 45 public bool has_counter { get; set; } 46 public bool has_delete_button { get; set; } 47 public signal void delete_action (LabelTool self); 48 public string data = ""; 49 50 double counter_box_width = 24 * Toolbox.get_scale (); 51 double counter_box_height = 11 * Toolbox.get_scale (); 52 53 Text label_text; 54 55 Surface? selected_cache = null; 56 Surface? deselected_cache = null; 57 58 public LabelTool (string label) { 59 double text_height; 60 61 base (); 62 63 label_text = new Text (); 64 label_text.set_text (label); 65 66 this.label = label; 67 this.number = "-"; 68 69 text_height = 17 * Toolbox.get_scale (); 70 label_text.set_font_size (text_height); 71 72 has_delete_button = false; 73 has_counter = false; 74 75 panel_press_action.connect ((selected, button, tx, ty) => { 76 if (has_delete_button && y <= ty <= y + h && tx >= w - 30 * Toolbox.get_scale ()) { 77 delete_action (this); 78 } 79 }); 80 } 81 82 void clear_cache () { 83 selected_cache = null; 84 deselected_cache = null; 85 } 86 87 public override void draw_tool (Context cr, double px, double py) { 88 double x = this.x - px; 89 double y = this.y - py; 90 91 if (is_selected ()) { 92 93 if (selected_cache == null) { 94 selected_cache = Screen.create_background_surface ((int) w, (int) h + 2); 95 Context c = new Context ((!) selected_cache); 96 c.scale (1 / Screen.get_scale (), 1 / Screen.get_scale ()); 97 draw_tool_surface (c, x, 2, true); 98 } 99 100 cr.save (); 101 cr.set_antialias (Cairo.Antialias.NONE); 102 cr.set_source_surface ((!) selected_cache, 0, (int) y - 2); 103 cr.paint (); 104 cr.restore (); 105 } else { 106 107 if (deselected_cache == null) { 108 deselected_cache = Screen.create_background_surface ((int) w, (int) h + 2); 109 Context c = new Context ((!) deselected_cache); 110 c.scale (1 / Screen.get_scale (), 1 / Screen.get_scale ()); 111 draw_tool_surface (c, x, 2, false); 112 } 113 114 cr.save (); 115 cr.set_antialias (Cairo.Antialias.NONE); 116 cr.set_source_surface ((!) deselected_cache, 0, (int) y - 2); 117 cr.paint (); 118 cr.restore (); 119 } 120 } 121 122 public void draw_tool_surface (Context cr, double px, double py, bool selected) { 123 Text glyph_count; 124 double bgx, bgy; 125 double center_x, center_y; 126 double text_height; 127 double text_width; 128 double x = px; 129 double y = py; 130 131 // background 132 if (selected) { 133 cr.save (); 134 Theme.color (cr, "Menu Background"); 135 cr.rectangle (0, y - 2 * Toolbox.get_scale (), w, h); // labels overlap with 2 pixels 136 cr.fill (); 137 cr.restore (); 138 } 139 140 // tab label 141 cr.save (); 142 143 Theme.text_color (label_text, "Text Tool Box"); 144 145 text_width = Toolbox.allocation_width; 146 147 if (has_counter) { 148 text_width -= counter_box_width - 15; 149 } 150 151 if (has_delete_button) { 152 text_width -= 30; 153 } 154 155 label_text.truncate (text_width); 156 label_text.draw_at_top (cr, x, y); 157 cr.restore (); 158 159 // glyph count 160 if (has_counter) { 161 cr.save (); 162 bgx = Toolbox.allocation_width - counter_box_width - 9; 163 bgy = y + 2; 164 165 if (is_selected ()) { 166 Theme.color (cr, "Glyph Count Background 1"); 167 } else { 168 Theme.color (cr, "Glyph Count Background 2"); 169 } 170 171 draw_rounded_rectangle (cr, bgx, bgy, counter_box_width, counter_box_height, 3); 172 cr.fill (); 173 cr.restore (); 174 175 glyph_count = new Text (); 176 glyph_count.set_text (@"$(this.number)"); 177 text_height = 12; 178 179 glyph_count.set_font_size (text_height); 180 center_x = bgx + (counter_box_width / 2.0 - glyph_count.get_extent () / 2.0); 181 center_y = bgy + (counter_box_height / 2.0 + 5); 182 183 if (is_selected ()) { 184 Theme.text_color (glyph_count, "Text Foreground"); 185 } else { 186 Theme.text_color (glyph_count, "Text Foreground"); 187 } 188 189 glyph_count.set_font_size (text_height); 190 glyph_count.draw_at_baseline (cr, center_x, center_y); 191 } 192 193 if (has_delete_button) { 194 cr.save (); 195 Theme.color (cr, "Text Foreground"); 196 cr.set_line_width (1); 197 cr.move_to (w - 20, y + h / 2 - 2.5 - 2); 198 cr.line_to (w - 25, y + h / 2 + 2.5 - 2); 199 cr.move_to (w - 20, y + h / 2 + 2.5 - 2); 200 cr.line_to (w - 25, y + h / 2 - 2.5 - 2); 201 cr.stroke (); 202 cr.restore (); 203 } 204 } 205 } 206 207 } 208