The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

OverViewItem.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/OverViewItem.vala.
Fix Clang compile error
1 /* 2 Copyright (C) 2012 2014 2015 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 using Math; 17 18 namespace BirdFont { 19 20 public class OverViewItem : GLib.Object { 21 public unichar character = '\0'; 22 public GlyphCollection? glyphs; 23 public double x; 24 public double y; 25 public bool selected = false; 26 public CharacterInfo info; 27 28 public static double DEFAULT_WIDTH = 100; 29 public static double DEFAULT_HEIGHT = 130; 30 public static double DEFAULT_MARGIN = 20; 31 32 public static double width = 100; 33 public static double height = 130; 34 public static double margin = 20; 35 36 public static double glyph_scale = 1.0; 37 38 public VersionList version_menu; 39 Text label; 40 41 public static Surface? label_background = null; 42 public static Surface? selected_label_background = null; 43 public static Surface? label_background_no_menu = null; 44 public static Surface? selected_label_background_no_menu = null; 45 46 public OverViewItem (GlyphCollection? glyphs, unichar character, double x, double y) { 47 this.x = x; 48 this.y = y; 49 this.character = character; 50 this.glyphs = glyphs; 51 this.info = new CharacterInfo (character, glyphs); 52 53 label = new Text ((!) character.to_string (), 17); 54 truncate_label (); 55 56 if (glyphs != null) { 57 version_menu = new VersionList ((!) glyphs); 58 version_menu.add_glyph_item.connect ((glyph) => { 59 ((!) glyphs).insert_glyph (glyph, true); 60 }); 61 62 version_menu.signal_delete_item.connect ((glyph_index) => { 63 OverView v = MainWindow.get_overview (); 64 version_menu = new VersionList ((!) glyphs); 65 v.update_item_list (); 66 GlyphCanvas.redraw (); 67 }); 68 } else { 69 version_menu = new VersionList (new GlyphCollection (character, (!) character.to_string ())); 70 } 71 } 72 73 public static void reset_label () { 74 label_background = null; 75 selected_label_background = null; 76 } 77 78 void truncate_label () { 79 double w = has_icons () ? width - 43 : width; 80 label.truncate (w); 81 } 82 83 public string get_name () { 84 StringBuilder s; 85 86 if (glyphs != null) { 87 return ((!) glyphs).get_name (); 88 } 89 90 s = new StringBuilder (); 91 s.append_unichar (character); 92 93 return s.str; 94 } 95 96 public void set_selected (bool s) { 97 selected = s; 98 } 99 100 public static double full_width () { 101 return width + margin; 102 } 103 104 public static double full_height () { 105 return height + margin; 106 } 107 108 public bool click (uint button, double px, double py) { 109 bool a; 110 GlyphCollection g; 111 bool s = (x <= px <= x + width) && (y <= py <= y + height); 112 113 if (has_icons () && glyphs != null) { 114 g = (!) glyphs; 115 version_menu.set_position (x + width - 21, y + height - 18); 116 a = version_menu.menu_item_action (px, py); // select one item on the menu 117 if (a) { 118 return s; 119 } 120 121 version_menu.menu_icon_action (px, py); // click in the open menu 122 } 123 124 info.set_position (x + width - 17, y + height - 22.5); 125 if (has_icons () && info.is_over_icon (px, py)) { 126 MainWindow.get_overview ().set_character_info (info); 127 } 128 129 return s; 130 } 131 132 public bool double_click (uint button, double px, double py) { 133 selected = (x <= px <= x + width) && (y <= py <= y + height); 134 return selected; 135 } 136 137 public bool is_on_screen (WidgetAllocation allocation) { 138 return y + height > 0 && y < allocation.height; 139 } 140 141 public void draw (WidgetAllocation allocation, Context cr) { 142 if (!is_on_screen (allocation)) { 143 return; 144 } 145 146 cr.save (); 147 Theme.color (cr, "Background 1"); 148 cr.rectangle (x, y, width, height); 149 cr.fill (); 150 cr.restore (); 151 152 cr.save (); 153 Theme.color (cr, "Overview Item Border"); 154 cr.rectangle (x, y, width, height); 155 cr.set_line_width (1); 156 cr.stroke (); 157 cr.restore (); 158 159 draw_thumbnail (cr, glyphs, x, y + height); 160 draw_caption (cr); 161 } 162 163 public void adjust_scale () { 164 double x1, x2, y1, y2, glyph_width, glyph_height, scale, gx; 165 Glyph g; 166 Font font; 167 168 if (glyphs != null) { 169 font = BirdFont.get_current_font (); 170 g = ((!) glyphs).get_current (); 171 g.boundaries (out x1, out y1, out x2, out y2); 172 173 glyph_width = x2 - x1; 174 glyph_height = y2 - y1; 175 176 if (glyph_scale == 1) { 177 // caption height is 20 178 glyph_scale = (height - 20) / (font.top_limit - font.bottom_limit); 179 } 180 181 scale = glyph_scale; 182 gx = ((width / scale) - glyph_width) / 2; 183 184 if (gx < 0) { 185 glyph_scale = 1 + 2 * gx / width; 186 } 187 } 188 } 189 190 private void draw_thumbnail (Context cr, GlyphCollection? gl, double x, double y) { 191 Glyph g; 192 Font font; 193 double gx, gy; 194 double x1, x2, y1, y2; 195 double scale_box; 196 double w, h; 197 double glyph_width, glyph_height; 198 Surface s; 199 Context c; 200 Color color = Color.black (); 201 202 w = width; 203 h = height; 204 205 scale_box = width / DEFAULT_WIDTH; 206 207 s = Screen.create_background_surface ((int) width, (int) height - 20); 208 c = new Context (s); 209 210 if (gl != null) { 211 font = BirdFont.get_current_font (); 212 g = ((!) gl).get_current (); 213 214 c.save (); 215 g.boundaries (out x1, out y1, out x2, out y2); 216 217 glyph_width = x2 - x1; 218 glyph_height = y2 - y1; 219 220 c.save (); 221 c.scale (glyph_scale * Screen.get_scale (), glyph_scale * Screen.get_scale ()); 222 223 g.add_help_lines (); 224 225 gx = ((w / glyph_scale) - glyph_width) / 2 - g.get_left_side_bearing (); 226 gy = (h / glyph_scale) - 25 / glyph_scale; 227 228 c.translate (gx - Glyph.xc () - g.get_lsb (), g.get_baseline () + gy - Glyph.yc ()); 229 230 g.draw_paths (c, color); 231 c.restore (); 232 } else { 233 c.scale (Screen.get_scale (), Screen.get_scale ()); 234 235 c.save (); 236 Text fallback = new Text (); 237 Theme.text_color (fallback, "Overview Glyph"); 238 fallback.set_text ((!) character.to_string ()); 239 double font_size = height * 0.8; 240 fallback.set_font_size (font_size); 241 242 gx = (width - fallback.get_extent ()) / 2.0; 243 gy = height - 30; 244 fallback.set_font_size (font_size); 245 fallback.draw_at_baseline (c, gx, gy); 246 c.restore (); 247 } 248 249 cr.save (); 250 cr.set_antialias (Cairo.Antialias.NONE); 251 cr.scale (1 / Screen.get_scale (), 1 / Screen.get_scale ()); 252 cr.set_source_surface (s, (int) (x * Screen.get_scale ()), (int) ((y - h)) * Screen.get_scale ()); 253 cr.paint (); 254 cr.restore (); 255 } 256 257 public bool has_icons () { 258 return width > 50; 259 } 260 261 public void draw_caption (Context cr) { 262 draw_label_background (cr); 263 264 cr.save (); 265 266 if (glyphs != null) { 267 if (selected) { 268 Theme.text_color (label, "Overview Selected Foreground"); 269 } else { 270 Theme.text_color (label, "Overview Foreground"); 271 } 272 273 label.draw_at_baseline (cr, x + 0.08 * width, y + height - 6); 274 } 275 276 draw_menu (cr); 277 cr.restore (); 278 } 279 280 public void create_label_background_cache (Context cr) { 281 Context cc; 282 Cairo.Pattern p; 283 Surface cache; 284 285 // unselected item 286 cache = Screen.create_background_surface ((int) width + 1, 20); 287 cc = new Context (cache); 288 cc.scale(Screen.get_scale(), Screen.get_scale()); 289 290 cc.rectangle (0, 0, width, 20 - 1); 291 p = new Cairo.Pattern.linear (0.0, 0, 0.0, 20); 292 Theme.gradient (p, "Overview Item 1", "Overview Item 2"); 293 cc.set_source (p); 294 295 cc.fill (); 296 297 if (has_icons ()) { 298 draw_menu_icon (cc, false); 299 draw_character_info_icon (cc); 300 } 301 302 label_background = (!) cache; 303 304 // selected item 305 cache = Screen.create_background_surface ((int) width + 1, 20); 306 cc = new Context (cache); 307 cc.scale(Screen.get_scale(), Screen.get_scale()); 308 309 cc.rectangle (0, 0, width, 20 - 1); 310 311 Theme.color (cc, "Selected Overview Item"); 312 313 cc.fill (); 314 315 if (has_icons ()) { 316 draw_menu_icon (cc, true); 317 draw_character_info_icon (cc); 318 } 319 320 selected_label_background = (!) cache; 321 322 // unselected item without menu icon 323 cache = Screen.create_background_surface ((int) width, 20); 324 cc = new Context (cache); 325 cc.scale(Screen.get_scale(), Screen.get_scale()); 326 cc.rectangle (0, 0, width - 1, 20 - 1); 327 p = new Cairo.Pattern.linear (0.0, 0, 0.0, 20); 328 Theme.gradient (p, "Overview Item 1", "Overview Item 2"); 329 cc.set_source (p); 330 cc.fill (); 331 332 if (has_icons ()) { 333 draw_character_info_icon (cc); 334 } 335 336 label_background_no_menu = (!) cache; 337 338 // selected item 339 cache = Screen.create_background_surface ((int) width + 1, 20); 340 cc = new Context (cache); 341 cc.scale(Screen.get_scale(), Screen.get_scale()); 342 cc.rectangle (0, 0, width, 20 - 1); 343 Theme.color (cc, "Selected Overview Item"); 344 cc.fill (); 345 346 if (has_icons ()) { 347 draw_character_info_icon (cc); 348 } 349 350 selected_label_background_no_menu = (!) cache; 351 } 352 353 bool has_menu () { 354 return glyphs != null; 355 } 356 357 public void draw_label_background (Context cr) { 358 Surface cache; 359 bool icon; 360 361 if (unlikely (label_background == null)) { 362 create_label_background_cache (cr); 363 } 364 365 if (label_background != null 366 && selected_label_background != null 367 && label_background_no_menu != null 368 && selected_label_background_no_menu != null) { 369 370 icon = has_menu (); 371 if (selected && icon) { 372 cache = (!) selected_label_background; 373 } else if (!selected && icon) { 374 cache = (!) label_background; 375 } else if (selected && !icon) { 376 cache = (!) selected_label_background_no_menu; 377 } else { 378 cache = (!) label_background_no_menu; 379 } 380 381 Screen.paint_background_surface (cr, cache, (int) x, (int) (y + height - 19)); 382 } 383 } 384 385 private void draw_character_info_icon (Context cr) { 386 info.draw_icon (cr, selected, width - 17, -1.5); 387 } 388 389 public void hide_menu () { 390 version_menu.menu_visible = false; 391 } 392 393 private void draw_menu_icon (Context cc, bool selected) { 394 Text icon; 395 396 icon = new Text ("dropdown_menu", 17); 397 icon.load_font ("icons.bf"); 398 399 if (selected) { 400 Theme.text_color (icon, "Overview Selected Foreground"); 401 } else { 402 Theme.text_color (icon, "Overview Foreground"); 403 } 404 405 icon.draw_at_top (cc, width - 32, 0); 406 } 407 408 private void draw_menu (Context cr) { 409 if (likely (glyphs == null || !version_menu.menu_visible)) { 410 return; 411 } 412 413 version_menu.draw_menu (cr); 414 } 415 } 416 417 } 418