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.
Merge branch 'master' of github.com:johanmattssonm/birdfont
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 = new Surface.similar (cr.get_target (), Content.COLOR_ALPHA, (int) w, (int) h - 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 gx = ((w / glyph_scale) - glyph_width) / 2; 221 gy = (h / glyph_scale) - 25 / glyph_scale; 222 223 c.save (); 224 c.scale (glyph_scale, glyph_scale); 225 226 g.add_help_lines (); 227 228 c.translate (gx - g.get_lsb () - Glyph.xc (), g.get_baseline () + gy - Glyph.yc ()); 229 230 g.draw_paths (c, color); 231 c.restore (); 232 } else { 233 c.save (); 234 Text fallback = new Text (); 235 Theme.text_color (fallback, "Overview Glyph"); 236 fallback.set_text ((!) character.to_string ()); 237 double font_size = height * 0.8; 238 fallback.set_font_size (font_size); 239 240 gx = (width - fallback.get_extent ()) / 2.0; 241 gy = height - 30; 242 fallback.set_font_size (font_size); 243 fallback.draw_at_baseline (c, gx, gy); 244 c.restore (); 245 } 246 247 cr.save (); 248 cr.set_source_surface (s, x, y - h); 249 cr.paint (); 250 cr.restore (); 251 } 252 253 public bool has_icons () { 254 return width > 50; 255 } 256 257 public void draw_caption (Context cr) { 258 draw_label_background (cr); 259 260 cr.save (); 261 262 if (glyphs != null) { 263 if (selected) { 264 Theme.text_color (label, "Overview Selected Foreground"); 265 } else { 266 Theme.text_color (label, "Overview Foreground"); 267 } 268 269 label.draw_at_baseline (cr, x + 0.08 * width, y + height - 6); 270 } 271 272 draw_menu (cr); 273 cr.restore (); 274 } 275 276 public void create_label_background_cache (Context cr) { 277 Context cc; 278 Cairo.Pattern p; 279 Surface cache; 280 281 // unselected item 282 cache = new Surface.similar (cr.get_target (), Cairo.Content.COLOR_ALPHA, (int) width, 20); 283 cc = new Context (cache); 284 cc.rectangle (0, 0, width - 1, 20 - 1); 285 p = new Cairo.Pattern.linear (0.0, 0, 0.0, 20); 286 Theme.gradient (p, "Overview Item 1", "Overview Item 2"); 287 cc.set_source (p); 288 289 cc.fill (); 290 291 if (has_icons ()) { 292 draw_menu_icon (cc, false); 293 draw_character_info_icon (cc); 294 } 295 296 label_background = (!) cache; 297 298 // selected item 299 cache = new Surface.similar (cr.get_target (), Cairo.Content.COLOR_ALPHA, (int) width, 20); 300 cc = new Context (cache); 301 302 cc.rectangle (0, 0, width - 1, 20 - 1); 303 304 Theme.color (cc, "Selected Overview Item"); 305 306 cc.fill (); 307 308 if (has_icons ()) { 309 draw_menu_icon (cc, true); 310 draw_character_info_icon (cc); 311 } 312 313 selected_label_background = (!) cache; 314 315 // unselected item without menu icon 316 cache = new Surface.similar (cr.get_target (), Cairo.Content.COLOR_ALPHA, (int) width, 20); 317 cc = new Context (cache); 318 319 cc.rectangle (0, 0, width - 1, 20 - 1); 320 p = new Cairo.Pattern.linear (0.0, 0, 0.0, 20); 321 Theme.gradient (p, "Overview Item 1", "Overview Item 2"); 322 cc.set_source (p); 323 cc.fill (); 324 325 if (has_icons ()) { 326 draw_character_info_icon (cc); 327 } 328 329 label_background_no_menu = (!) cache; 330 331 // selected item 332 cache = new Surface.similar (cr.get_target (), Cairo.Content.COLOR_ALPHA, (int) width, 20); 333 cc = new Context (cache); 334 cc.rectangle (0, 0, width - 1, 20 - 1); 335 Theme.color (cc, "Selected Overview Item"); 336 cc.fill (); 337 338 if (has_icons ()) { 339 draw_character_info_icon (cc); 340 } 341 342 selected_label_background_no_menu = (!) cache; 343 } 344 345 bool has_menu () { 346 return glyphs != null; 347 } 348 349 public void draw_label_background (Context cr) { 350 Surface cache; 351 bool icon; 352 353 if (unlikely (label_background == null)) { 354 create_label_background_cache (cr); 355 } 356 357 if (label_background != null 358 && selected_label_background != null 359 && label_background_no_menu != null 360 && selected_label_background_no_menu != null) { 361 362 icon = has_menu (); 363 if (selected && icon) { 364 cache = (!) selected_label_background; 365 } else if (!selected && icon) { 366 cache = (!) label_background; 367 } else if (selected && !icon) { 368 cache = (!) selected_label_background_no_menu; 369 } else { 370 cache = (!) label_background_no_menu; 371 } 372 373 cr.save (); 374 cr.set_antialias (Cairo.Antialias.NONE); 375 cr.set_source_surface (cache, (int) (x + 1), (int) (y + height - 19)); 376 cr.paint (); 377 cr.restore (); 378 } 379 } 380 381 private void draw_character_info_icon (Context cr) { 382 info.draw_icon (cr, selected, width - 17, -2.5); 383 } 384 385 public void hide_menu () { 386 version_menu.menu_visible = false; 387 } 388 389 private void draw_menu_icon (Context cc, bool selected) { 390 Text icon; 391 392 icon = new Text ("dropdown_menu", 17); 393 icon.load_font ("icons.bf"); 394 395 if (selected) { 396 Theme.text_color (icon, "Overview Selected Foreground"); 397 } else { 398 Theme.text_color (icon, "Overview Foreground"); 399 } 400 401 icon.draw_at_top (cc, width - 32, 0); 402 } 403 404 private void draw_menu (Context cr) { 405 if (likely (glyphs == null || !version_menu.menu_visible)) { 406 return; 407 } 408 409 version_menu.draw_menu (cr); 410 } 411 } 412 413 } 414