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.
Fix translation
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 * Screen.get_scale ()), (int) ((h + 2) * Screen.get_scale ())); 95 Context c = new Context ((!) selected_cache); 96 c.scale (Screen.get_scale (), Screen.get_scale ()); 97 draw_tool_surface (c, x, 2, true); 98 } 99 100 cr.save (); 101 cr.scale (1 / Screen.get_scale (), 1 / Screen.get_scale ()); 102 cr.set_antialias (Cairo.Antialias.NONE); 103 cr.set_source_surface ((!) selected_cache, 0, (int) ((y - 2) * Screen.get_scale ())); 104 cr.paint (); 105 cr.restore (); 106 } else { 107 108 if (deselected_cache == null) { 109 deselected_cache = Screen.create_background_surface ((int) (w * Screen.get_scale ()), (int) ((h + 2) * Screen.get_scale ())); 110 Context c = new Context ((!) deselected_cache); 111 c.scale (Screen.get_scale (), Screen.get_scale ()); 112 draw_tool_surface (c, x, 2, false); 113 } 114 115 cr.save (); 116 cr.scale (1 / Screen.get_scale (), 1 / Screen.get_scale ()); 117 cr.set_antialias (Cairo.Antialias.NONE); 118 cr.set_source_surface ((!) deselected_cache, 0, (int) ((y - 2) * Screen.get_scale ())); 119 cr.paint (); 120 cr.restore (); 121 } 122 } 123 124 public void draw_tool_surface (Context cr, double px, double py, bool selected) { 125 Text glyph_count; 126 double bgx, bgy; 127 double center_x, center_y; 128 double text_height; 129 double text_width; 130 double x = px; 131 double y = py; 132 133 // background 134 if (selected) { 135 cr.save (); 136 Theme.color (cr, "Menu Background"); 137 cr.rectangle (0, y - 2 * Toolbox.get_scale (), w, h); // labels overlap with 2 pixels 138 cr.fill (); 139 cr.restore (); 140 } 141 142 // tab label 143 cr.save (); 144 145 Theme.text_color (label_text, "Text Tool Box"); 146 147 text_width = Toolbox.allocation_width; 148 149 if (has_counter) { 150 text_width -= counter_box_width - 15; 151 } 152 153 if (has_delete_button) { 154 text_width -= 30; 155 } 156 157 label_text.truncate (text_width); 158 label_text.draw_at_top (cr, x, y); 159 cr.restore (); 160 161 // glyph count 162 if (has_counter) { 163 cr.save (); 164 bgx = Toolbox.allocation_width - counter_box_width - 9; 165 bgy = y + 2; 166 167 if (is_selected ()) { 168 Theme.color (cr, "Glyph Count Background 1"); 169 } else { 170 Theme.color (cr, "Glyph Count Background 2"); 171 } 172 173 draw_rounded_rectangle (cr, bgx, bgy, counter_box_width, counter_box_height, 3); 174 cr.fill (); 175 cr.restore (); 176 177 glyph_count = new Text (); 178 glyph_count.set_text (@"$(this.number)"); 179 text_height = 12; 180 181 glyph_count.set_font_size (text_height); 182 center_x = bgx + (counter_box_width / 2.0 - glyph_count.get_extent () / 2.0); 183 center_y = bgy + (counter_box_height / 2.0 + 5); 184 185 if (is_selected ()) { 186 Theme.text_color (glyph_count, "Text Foreground"); 187 } else { 188 Theme.text_color (glyph_count, "Text Foreground"); 189 } 190 191 glyph_count.set_font_size (text_height); 192 glyph_count.draw_at_baseline (cr, center_x, center_y); 193 } 194 195 if (has_delete_button) { 196 cr.save (); 197 Theme.color (cr, "Text Foreground"); 198 cr.set_line_width (1); 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.move_to (w - 20, y + h / 2 + 2.5 - 2); 202 cr.line_to (w - 25, y + h / 2 - 2.5 - 2); 203 cr.stroke (); 204 cr.restore (); 205 } 206 } 207 } 208 209 } 210