The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

OverView.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/OverView.vala.
Merge branch '2.14' of github.com:johanmattssonm/birdfont into 2.14
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 17 namespace BirdFont { 18 19 /** A display with all glyphs present in this font. */ 20 public class OverView : FontDisplay { 21 public WidgetAllocation allocation = new WidgetAllocation (); 22 23 public OverViewItem selected_item = new OverViewItem (null, '\0', 0, 0); 24 25 public Gee.ArrayList<GlyphCollection> copied_glyphs = new Gee.ArrayList<GlyphCollection> (); 26 public Gee.ArrayList<GlyphCollection> selected_items = new Gee.ArrayList<GlyphCollection> (); 27 28 int selected = 0; 29 int first_visible = 0; 30 int rows = 0; 31 int items_per_row = 0; 32 33 double view_offset_y = 0; 34 double view_offset_x = 0; 35 36 public signal void open_new_glyph_signal (unichar c); 37 public signal void open_glyph_signal (GlyphCollection c); 38 39 public GlyphRange glyph_range { 40 get { 41 return _glyph_range; 42 } 43 44 set { 45 _glyph_range = value; 46 } 47 } 48 49 GlyphRange _glyph_range; 50 51 string search_query = ""; 52 53 Gee.ArrayList<OverViewItem> visible_items = new Gee.ArrayList<OverViewItem> (); 54 55 /** List of undo commands. */ 56 public Gee.ArrayList<OverViewUndoItem> undo_items = new Gee.ArrayList<OverViewUndoItem> (); 57 public Gee.ArrayList<OverViewUndoItem> redo_items = new Gee.ArrayList<OverViewUndoItem> (); 58 59 /** Show all characters that has been drawn. */ 60 public bool all_available { 61 set { 62 _all_available = value; 63 update_item_list (); 64 } 65 66 get { 67 return _all_available; 68 } 69 } 70 71 bool _all_available = false; 72 73 /** Show unicode database info. */ 74 CharacterInfo? character_info = null; 75 76 double scroll_size = 1; 77 const double UCD_LINE_HEIGHT = 17 * 1.3; 78 79 public OverView (GlyphRange? range = null, 80 bool open_selected = true, bool default_character_set = true) { 81 82 GlyphRange gr; 83 84 if (range == null) { 85 gr = new GlyphRange (); 86 set_current_glyph_range (gr); 87 } 88 89 if (open_selected) { 90 this.open_glyph_signal.connect ((glyph_collection) => { 91 TabBar tabs = MainWindow.get_tab_bar (); 92 string n = glyph_collection.get_current ().name; 93 bool selected = tabs.select_char (n); 94 GlyphCanvas canvas; 95 Glyph g = glyph_collection.get_current (); 96 97 if (!selected) { 98 canvas = MainWindow.get_glyph_canvas (); 99 tabs.add_tab (g, true, glyph_collection); 100 canvas.set_current_glyph_collection (glyph_collection); 101 set_initial_zoom (); 102 PenTool.update_orientation (); 103 } 104 }); 105 106 this.open_new_glyph_signal.connect ((character) => { 107 // ignore control characters 108 if (character <= 0x1F) { 109 return; 110 } 111 112 create_new_glyph (character); 113 }); 114 } 115 116 if (default_character_set) { 117 IdleSource idle = new IdleSource (); 118 119 idle.set_callback (() => { 120 use_default_character_set (); 121 selected_canvas (); 122 return false; 123 }); 124 125 idle.attach (null); 126 } 127 128 update_item_list (); 129 update_scrollbar (); 130 reset_zoom (); 131 } 132 133 public void select_all_glyphs () { 134 Font f; 135 GlyphCollection? glyphs; 136 137 f = BirdFont.get_current_font (); 138 139 for (int index = 0; index < f.length (); index++) { 140 glyphs = f.get_glyph_collection_indice ((uint32) index); 141 return_if_fail (glyphs != null); 142 143 selected_items.add ((!) glyphs); 144 } 145 146 foreach (OverViewItem item in visible_items) { 147 item.selected = item.glyphs != null; 148 } 149 150 GlyphCanvas.redraw (); 151 } 152 153 public void use_default_character_set () { 154 GlyphRange gr = new GlyphRange (); 155 all_available = false; 156 DefaultCharacterSet.use_default_range (gr); 157 set_current_glyph_range (gr); 158 OverviewTools.update_overview_characterset (); 159 FontDisplay.dirty_scrollbar = true; 160 } 161 162 public GlyphCollection create_new_glyph (unichar character) { 163 StringBuilder name = new StringBuilder (); 164 TabBar tabs = MainWindow.get_tab_bar (); 165 bool selected; 166 Glyph glyph; 167 GlyphCollection glyph_collection = MainWindow.get_current_glyph_collection (); 168 GlyphCanvas canvas; 169 170 name.append_unichar (character); 171 selected = tabs.select_char (name.str); 172 173 if (!selected) { 174 glyph_collection = add_character_to_font (character); 175 176 glyph = glyph_collection.get_current (); 177 glyph.layers.add_layer (new Layer ()); 178 tabs.add_tab (glyph, true, glyph_collection); 179 180 selected_items.add (glyph_collection); 181 182 canvas = MainWindow.get_glyph_canvas (); 183 canvas.set_current_glyph_collection (glyph_collection); 184 185 set_initial_zoom (); 186 } else { 187 warning ("Glyph is already open"); 188 } 189 190 OverviewTools.update_overview_characterset (); 191 return glyph_collection; 192 } 193 194 public GlyphCollection add_empty_character_to_font (unichar character, bool unassigned, string name) { 195 return add_character_to_font (character, true, unassigned); 196 } 197 198 public GlyphCollection add_character_to_font (unichar character, bool empty = false, 199 bool unassigned = false, string glyph_name = "") { 200 StringBuilder name = new StringBuilder (); 201 Font font = BirdFont.get_current_font (); 202 GlyphCollection? fg; 203 Glyph glyph; 204 GlyphCollection glyph_collection; 205 206 if (glyph_name == "") { 207 name.append_unichar (character); 208 } else { 209 name.append (glyph_name); 210 } 211 212 if (all_available) { 213 fg = font.get_glyph_collection_by_name (name.str); 214 } else { 215 fg = font.get_glyph_collection (name.str); 216 } 217 218 if (fg != null) { 219 glyph_collection = (!) fg; 220 } else { 221 glyph_collection = new GlyphCollection (character, name.str); 222 223 if (!empty) { 224 glyph = new Glyph (name.str, (!unassigned) ? character : '\0'); 225 glyph_collection.insert_glyph (glyph, true); 226 } 227 228 font.add_glyph_collection (glyph_collection); 229 } 230 231 glyph_collection.set_unassigned (unassigned); 232 233 return glyph_collection; 234 } 235 236 public static void search () { 237 OverView ow = MainWindow.get_overview (); 238 TextListener listener = new TextListener (t_("Search"), ow.search_query, t_("Filter")); 239 240 listener.signal_text_input.connect ((text) => { 241 OverView o = MainWindow.get_overview (); 242 o.search_query = text; 243 }); 244 245 listener.signal_submit.connect (() => { 246 OverView o = MainWindow.get_overview (); 247 string q = o.search_query; 248 249 if (q.char_count () > 1) { 250 q = q.down (); 251 } 252 253 GlyphRange r = CharDatabase.search (q); 254 o.set_current_glyph_range (r); 255 MainWindow.get_tab_bar ().select_tab_name ("Overview"); 256 257 TextListener tl = new TextListener (t_("Search"), o.search_query, t_("Filter")); 258 TabContent.show_text_input (tl); 259 }); 260 261 TabContent.show_text_input (listener); 262 } 263 264 public Glyph? get_current_glyph () { 265 OverViewItem oi = selected_item; 266 if (oi.glyphs != null) { 267 return ((!) oi.glyphs).get_current (); 268 } 269 return null; 270 } 271 272 public void set_initial_zoom () { 273 Toolbox tools = MainWindow.get_toolbox (); 274 ZoomTool z = (ZoomTool) tools.get_tool ("zoom_tool"); 275 z.store_current_view (); 276 MainWindow.get_current_glyph ().default_zoom (); 277 z.store_current_view (); 278 OverViewItem.reset_label (); 279 } 280 281 public double get_height () { 282 double l; 283 Font f; 284 285 if (rows == 0) { 286 return 0; 287 } 288 289 if (all_available) { 290 f = BirdFont.get_current_font (); 291 l = f.length (); 292 } else { 293 l = glyph_range.length (); 294 } 295 296 return 2.0 * OverViewItem.height * (l / rows); 297 } 298 299 public bool selected_char_is_visible () { 300 return first_visible <= selected <= first_visible + items_per_row * rows; 301 } 302 303 public override bool has_scrollbar () { 304 return true; 305 } 306 307 public bool all_characters_in_view () { 308 double length; 309 Font f; 310 311 if (all_available) { 312 f = BirdFont.get_current_font (); 313 length = f.length (); 314 } else { 315 length = glyph_range.length (); 316 } 317 318 return length < items_per_row * rows; 319 } 320 321 public void move_up () { 322 first_visible -= items_per_row; 323 selected += items_per_row; 324 325 if (first_visible < 0) { 326 selected -= items_per_row; 327 first_visible = 0; 328 view_offset_y = 0; 329 } 330 } 331 332 public void move_down () { 333 if (!at_bottom ()) { 334 first_visible += items_per_row; 335 selected -= items_per_row; 336 } 337 } 338 339 public override void scroll_wheel (double x, double y, double dx, double dy) { 340 double pixel_delta = 3 * dy; 341 342 if (dy > 0) { 343 view_offset_y += pixel_delta; 344 345 while (view_offset_y > 0) { 346 view_offset_y -= OverViewItem.height; 347 move_up (); 348 } 349 } else { 350 if (at_bottom ()) { 351 if (view_offset_y > -2 * OverViewItem.height && !all_characters_in_view ()) { 352 view_offset_y += pixel_delta; 353 } 354 } else { 355 view_offset_y += pixel_delta; 356 while (view_offset_y < -OverViewItem.height) { 357 view_offset_y += OverViewItem.height; 358 move_down (); 359 } 360 } 361 } 362 363 if (view_offset_y < -2 * OverViewItem.height) { 364 view_offset_y = -2 * OverViewItem.height; 365 } 366 367 update_item_list (); 368 update_scrollbar (); 369 hide_menu (); 370 GlyphCanvas.redraw (); 371 } 372 373 public override void selected_canvas () { 374 OverviewTools.update_overview_characterset (); 375 KeyBindings.set_require_modifier (true); 376 update_scrollbar (); 377 update_zoom_bar (); 378 OverViewItem.glyph_scale = 1; 379 update_item_list (); 380 selected_item = get_selected_item (); 381 GlyphCanvas.redraw (); 382 } 383 384 public void update_zoom_bar () { 385 double z = OverViewItem.width / OverViewItem.DEFAULT_WIDTH - 0.5; 386 Toolbox.overview_tools.zoom_bar.set_zoom (z); 387 Toolbox.redraw_tool_box (); 388 update_item_list (); 389 } 390 391 public void set_zoom (double zoom) { 392 double z = zoom + 0.5; 393 OverViewItem.glyph_scale = 1; 394 OverViewItem.width = OverViewItem.DEFAULT_WIDTH * z; 395 OverViewItem.height = OverViewItem.DEFAULT_HEIGHT * z; 396 OverViewItem.margin = OverViewItem.DEFAULT_MARGIN * z; 397 update_item_list (); 398 OverViewItem.reset_label (); 399 GlyphCanvas.redraw (); 400 } 401 402 public override void zoom_min () { 403 OverViewItem.width = OverViewItem.DEFAULT_WIDTH * 0.5; 404 OverViewItem.height = OverViewItem.DEFAULT_HEIGHT * 0.5; 405 OverViewItem.margin = OverViewItem.DEFAULT_MARGIN * 0.5; 406 update_item_list (); 407 OverViewItem.reset_label (); 408 GlyphCanvas.redraw (); 409 update_zoom_bar (); 410 } 411 412 public override void reset_zoom () { 413 OverViewItem.width = OverViewItem.DEFAULT_WIDTH; 414 OverViewItem.height = OverViewItem.DEFAULT_HEIGHT; 415 OverViewItem.margin = OverViewItem.DEFAULT_MARGIN; 416 update_item_list (); 417 OverViewItem.reset_label (); 418 GlyphCanvas.redraw (); 419 update_zoom_bar (); 420 } 421 422 public override void zoom_max () { 423 OverViewItem.width = allocation.width; 424 OverViewItem.height = allocation.height; 425 update_item_list (); 426 OverViewItem.reset_label (); 427 GlyphCanvas.redraw (); 428 } 429 430 public override void zoom_in () { 431 OverViewItem.width *= 1.1; 432 OverViewItem.height *= 1.1; 433 OverViewItem.margin *= 1.1; 434 update_item_list (); 435 OverViewItem.reset_label (); 436 GlyphCanvas.redraw (); 437 update_zoom_bar (); 438 } 439 440 public override void zoom_out () { 441 OverViewItem.width *= 0.9; 442 OverViewItem.height *= 0.9; 443 OverViewItem.margin *= 0.9; 444 update_item_list (); 445 OverViewItem.reset_label (); 446 GlyphCanvas.redraw (); 447 update_zoom_bar (); 448 } 449 450 public override void store_current_view () { 451 } 452 453 public override void restore_last_view () { 454 } 455 456 public override void next_view () { 457 } 458 459 public override string get_label () { 460 return t_("Overview"); 461 } 462 463 public override string get_name () { 464 return "Overview"; 465 } 466 467 public void display_all_available_glyphs () { 468 all_available = true; 469 470 view_offset_y = 0; 471 first_visible = 0; 472 selected = 0; 473 474 update_item_list (); 475 selected_item = get_selected_item (); 476 GlyphCanvas.redraw (); 477 } 478 479 OverViewItem get_selected_item () { 480 if (visible_items.size == 0) { 481 return new OverViewItem (null, '\0', 0, 0); 482 } 483 484 if (!(0 <= selected < visible_items.size)) { 485 return selected_item; 486 } 487 488 return visible_items.get (selected); 489 } 490 491 int get_items_per_row () { 492 int i = 1; 493 double tab_with = allocation.width - 30; // 30 px for the scroll bar 494 OverViewItem.margin = OverViewItem.width * 0.1; 495 double l = OverViewItem.margin + OverViewItem.full_width (); 496 497 while (l <= tab_with) { 498 l += OverViewItem.full_width (); 499 i++; 500 } 501 502 return i - 1; 503 } 504 505 public void update_item_list (int item_list_length = -1) { 506 string character_string; 507 Font f = BirdFont.get_current_font (); 508 GlyphCollection? glyphs = null; 509 uint32 index; 510 OverViewItem item; 511 double x, y; 512 unichar character; 513 Glyph glyph; 514 double tab_with; 515 516 tab_with = allocation.width - 30; // scrollbar 517 518 items_per_row = get_items_per_row (); 519 rows = (int) (allocation.height / OverViewItem.full_height ()) + 2; 520 521 if (item_list_length == -1) { 522 item_list_length = items_per_row * rows; 523 } 524 525 visible_items.clear (); 526 visible_items = new Gee.ArrayList<OverViewItem> (); 527 528 // update item list 529 index = (uint32) first_visible; 530 x = OverViewItem.margin; 531 y = OverViewItem.margin; 532 for (int i = 0; i < item_list_length; i++) { 533 if (all_available) { 534 if (! (0 <= index < f.length ())) { 535 break; 536 } 537 538 glyphs = f.get_glyph_collection_indice ((uint32) index); 539 return_if_fail (glyphs != null); 540 541 glyph = ((!) glyphs).get_current (); 542 character_string = glyph.name; 543 character = glyph.unichar_code; 544 } else { 545 if (!(0 <= index < glyph_range.get_length ())) { 546 break; 547 } 548 549 character_string = glyph_range.get_char ((uint32) index); 550 glyphs = f.get_glyph_collection_by_name (character_string); 551 character = character_string.get_char (0); 552 } 553 554 item = new OverViewItem (glyphs, character, x, y); 555 item.adjust_scale (); 556 557 x += OverViewItem.full_width (); 558 559 if (x + OverViewItem.full_width () >= tab_with) { 560 x = OverViewItem.margin; 561 y += OverViewItem.full_height (); 562 } 563 564 item.selected = (i == selected); 565 566 if (glyphs != null) { 567 item.selected |= selected_items.index_of ((!) glyphs) != -1; 568 } 569 570 visible_items.add (item); 571 index++; 572 } 573 574 foreach (OverViewItem i in visible_items) { 575 i.y += view_offset_y; 576 i.x += view_offset_x; 577 } 578 } 579 580 public override void draw (WidgetAllocation allocation, Context cr) { 581 582 if (this.allocation.width != allocation.width 583 || this.allocation.height != allocation.height 584 || this.allocation.width == 0) { 585 this.allocation = allocation; 586 update_item_list (); 587 } 588 589 this.allocation = allocation; 590 591 // clear canvas 592 cr.save (); 593 Theme.color (cr, "Background 1"); 594 cr.rectangle (0, 0, allocation.width, allocation.height); 595 cr.fill (); 596 cr.restore (); 597 598 foreach (OverViewItem i in visible_items) { 599 i.draw (allocation, cr); 600 } 601 602 if (unlikely (visible_items.size == 0)) { 603 draw_empty_canvas (allocation, cr); 604 } 605 606 if (unlikely (character_info != null)) { 607 draw_character_info (cr); 608 } 609 } 610 611 void draw_empty_canvas (WidgetAllocation allocation, Context cr) { 612 Text t; 613 614 cr.save (); 615 t = new Text (t_("No glyphs in this view."), 24); 616 Theme.text_color (t, "Text Foreground"); 617 t.widget_x = 40; 618 t.widget_y = 30; 619 t.draw (cr); 620 cr.restore (); 621 } 622 623 public void scroll_rows (int row_adjustment) { 624 for (int i = 0; i < row_adjustment; i++) { 625 scroll (-OverViewItem.height); 626 } 627 628 for (int i = 0; i > row_adjustment; i--) { 629 scroll (OverViewItem.height); 630 } 631 } 632 633 public void scroll_adjustment (double pixel_adjustment) { 634 double l; 635 Font f; 636 637 if (all_available) { 638 f = BirdFont.get_current_font (); 639 l = f.length (); 640 } else { 641 l = glyph_range.length (); 642 } 643 644 if (first_visible <= 0) { 645 return; 646 } 647 648 if (first_visible + rows * items_per_row >= l) { 649 return; 650 } 651 652 scroll ((int64) pixel_adjustment); 653 } 654 655 void scroll_to_position (int64 r) { 656 if (r < 0) { 657 scroll_top (); 658 return; 659 } 660 661 selected -= (int) (r - first_visible); 662 first_visible = (int) r; 663 update_item_list (); 664 } 665 666 public override void scroll_to (double position) requires (items_per_row > 0) { 667 double r; 668 int nrows; 669 Font f; 670 671 if (all_available) { 672 f = BirdFont.get_current_font (); 673 nrows = (int) (f.length () / items_per_row); 674 } else { 675 nrows = (int) (glyph_range.length () / items_per_row); 676 } 677 678 view_offset_y = 0; 679 r = (int64) (position * (nrows - rows + 3)); // 3 invisible rows 680 r *= items_per_row; 681 682 scroll_to_position ((int64) r); 683 update_item_list (); 684 GlyphCanvas.redraw (); 685 } 686 687 private void scroll (double pixel_adjustment) { 688 if (first_visible < 0 && pixel_adjustment < 0) { 689 scroll_top (); 690 return; 691 } 692 693 view_offset_y += pixel_adjustment; 694 695 if (view_offset_y >= 0) { 696 while (view_offset_y > OverViewItem.height) { 697 view_offset_y -= OverViewItem.height; 698 first_visible -= items_per_row; 699 } 700 701 first_visible -= items_per_row; 702 view_offset_y -= OverViewItem.height; 703 } else if (view_offset_y < -OverViewItem.height) { 704 view_offset_y = 0; 705 first_visible += items_per_row; 706 } 707 708 update_item_list (); 709 } 710 711 public void scroll_top () { 712 first_visible = 0; 713 view_offset_y = 0; 714 715 update_item_list (); 716 717 if (visible_items.size != 0) { 718 selected_item = get_selected_item (); 719 } 720 } 721 722 /** Returns true if the selected glyph is at the last row. */ 723 private bool last_row () { 724 return visible_items.size - selected <= items_per_row; 725 } 726 727 public void key_down () { 728 Font f = BirdFont.get_current_font (); 729 int64 len = (all_available) ? f.length () : glyph_range.length (); 730 731 if (at_bottom () && last_row ()) { 732 return; 733 } 734 735 selected += items_per_row; 736 737 if (selected >= items_per_row * rows) { 738 first_visible += items_per_row; 739 selected -= items_per_row; 740 } 741 742 if (first_visible + selected >= len) { 743 selected = (int) (len - first_visible - 1); 744 745 if (selected < items_per_row * (rows - 1)) { 746 first_visible -= items_per_row; 747 selected += items_per_row; 748 } 749 } 750 751 if (selected >= visible_items.size) { 752 selected = (int) (visible_items.size - 1); 753 } 754 755 selected_item = get_selected_item (); 756 update_item_list (); 757 } 758 759 public void key_right () { 760 Font f = BirdFont.get_current_font (); 761 int64 len = (all_available) ? f.length () : glyph_range.length (); 762 763 if (at_bottom () && first_visible + selected + 1 >= len) { 764 selected = (int) (visible_items.size - 1); 765 selected_item = get_selected_item (); 766 return; 767 } 768 769 selected += 1; 770 771 if (selected >= items_per_row * rows) { 772 first_visible += items_per_row; 773 selected -= items_per_row; 774 selected -= 1; 775 } 776 777 if (first_visible + selected > len) { 778 first_visible -= items_per_row; 779 selected = (int) (len - first_visible - 1); 780 selected_item = get_selected_item (); 781 } 782 update_item_list (); 783 } 784 785 public void key_up () { 786 selected -= items_per_row; 787 788 if (selected < 0) { 789 first_visible -= items_per_row; 790 selected += items_per_row; 791 } 792 793 if (first_visible < 0) { 794 first_visible = 0; 795 } 796 update_item_list (); 797 } 798 799 public void key_left () { 800 selected -= 1; 801 802 if (selected < 0) { 803 first_visible -= items_per_row; 804 selected += items_per_row; 805 selected += 1; 806 } 807 808 if (first_visible < 0) { 809 scroll_top (); 810 } 811 update_item_list (); 812 } 813 814 public string get_selected_char () { 815 Font f; 816 Glyph? g; 817 818 if (all_available) { 819 f = BirdFont.get_current_font (); 820 g = f.get_glyph_indice (selected); 821 return_val_if_fail (g != null, "".dup ()); 822 return ((!) g).get_name (); 823 } 824 825 return glyph_range.get_char (selected); 826 } 827 828 public override void key_press (uint keyval) { 829 hide_menu (); 830 update_item_list (); 831 GlyphCanvas.redraw (); 832 833 if (KeyBindings.has_ctrl () || KeyBindings.has_logo ()) { 834 return; 835 } 836 837 switch (keyval) { 838 case Key.ENTER: 839 open_current_glyph (); 840 return; 841 842 case Key.UP: 843 get_selected_item ().selected = false; 844 845 key_up (); 846 selected_item = get_selected_item (); 847 848 selected_items.clear (); 849 if (selected_item.glyphs != null) { 850 selected_items.add ((!) selected_item.glyphs); 851 } 852 853 update_scrollbar (); 854 return; 855 856 case Key.RIGHT: 857 get_selected_item ().selected = false; 858 859 key_right (); 860 selected_item = get_selected_item (); 861 862 selected_items.clear (); 863 if (selected_item.glyphs != null) { 864 selected_items.add ((!) selected_item.glyphs); 865 } 866 867 update_scrollbar (); 868 return; 869 870 case Key.LEFT: 871 get_selected_item ().selected = false; 872 873 key_left (); 874 selected_item = get_selected_item (); 875 876 selected_items.clear (); 877 if (selected_item.glyphs != null) { 878 selected_items.add ((!) selected_item.glyphs); 879 } 880 881 update_scrollbar(); 882 return; 883 884 case Key.DOWN: 885 get_selected_item ().selected = false; 886 887 key_down (); 888 selected_item = get_selected_item (); 889 890 selected_items.clear (); 891 if (selected_item.glyphs != null) { 892 selected_items.add ((!) selected_item.glyphs); 893 } 894 895 update_scrollbar (); 896 return; 897 898 case Key.PG_UP: 899 get_selected_item ().selected = false; 900 901 for (int i = 0; i < rows; i++) { 902 key_up (); 903 } 904 selected_item = get_selected_item (); 905 906 selected_items.clear (); 907 if (selected_item.glyphs != null) { 908 selected_items.add ((!) selected_item.glyphs); 909 } 910 911 update_scrollbar (); 912 return; 913 914 case Key.PG_DOWN: 915 get_selected_item ().selected = false; 916 917 for (int i = 0; i < rows; i++) { 918 key_down (); 919 } 920 selected_item = get_selected_item (); 921 922 selected_items.clear (); 923 if (selected_item.glyphs != null) { 924 selected_items.add ((!) selected_item.glyphs); 925 } 926 927 update_scrollbar (); 928 return; 929 930 case Key.DEL: 931 delete_selected_glyph (); 932 selected_item = get_selected_item (); 933 return; 934 935 case Key.BACK_SPACE: 936 delete_selected_glyph (); 937 selected_item = get_selected_item (); 938 return; 939 } 940 941 if (!KeyBindings.has_ctrl () && !KeyBindings.has_logo ()) { 942 scroll_to_char (keyval); 943 } 944 945 selected_item = get_selected_item (); 946 947 selected_items.clear (); 948 if (selected_item.glyphs != null) { 949 selected_items.add ((!) selected_item.glyphs); 950 } 951 952 update_item_list (); 953 GlyphCanvas.redraw (); 954 } 955 956 public void delete_selected_glyph () { 957 Font font = BirdFont.get_current_font (); 958 OverViewUndoItem undo_item = new OverViewUndoItem (); 959 960 undo_item.alternate_sets = font.alternates.copy (); 961 962 foreach (GlyphCollection g in selected_items) { 963 undo_item.glyphs.add (g.copy ()); 964 } 965 store_undo_items (undo_item); 966 967 foreach (GlyphCollection gc in selected_items) { 968 font.delete_glyph (gc); 969 } 970 971 update_item_list (); 972 GlyphCanvas.redraw (); 973 } 974 975 public override void undo () { 976 Font font = BirdFont.get_current_font (); 977 OverViewUndoItem previous_collection; 978 979 if (undo_items.size == 0) { 980 return; 981 } 982 983 previous_collection = undo_items.get (undo_items.size - 1); 984 redo_items.add (get_current_state (previous_collection)); 985 986 // remove the old glyph and add the new one 987 foreach (GlyphCollection g in previous_collection.glyphs) { 988 font.delete_glyph (g); 989 990 if (g.length () > 0) { 991 font.add_glyph_collection (g); 992 } 993 } 994 995 Font f = BirdFont.get_current_font (); 996 f.alternates = previous_collection.alternate_sets.copy (); 997 998 undo_items.remove_at (undo_items.size - 1); 999 GlyphCanvas.redraw (); 1000 } 1001 1002 public override void redo () { 1003 Font font = BirdFont.get_current_font (); 1004 OverViewUndoItem previous_collection; 1005 1006 if (redo_items.size == 0) { 1007 return; 1008 } 1009 1010 previous_collection = redo_items.get (redo_items.size - 1); 1011 undo_items.add (get_current_state (previous_collection)); 1012 1013 // remove the old glyph and add the new one 1014 foreach (GlyphCollection g in previous_collection.glyphs) { 1015 font.delete_glyph (g); 1016 font.add_glyph_collection (g); 1017 } 1018 1019 font.alternates = previous_collection.alternate_sets.copy (); 1020 1021 redo_items.remove_at (redo_items.size - 1); 1022 GlyphCanvas.redraw (); 1023 } 1024 1025 public OverViewUndoItem get_current_state (OverViewUndoItem previous_collection) { 1026 GlyphCollection? gc; 1027 OverViewUndoItem ui = new OverViewUndoItem (); 1028 Font font = BirdFont.get_current_font (); 1029 1030 ui.alternate_sets = font.alternates.copy (); 1031 1032 foreach (GlyphCollection g in previous_collection.glyphs) { 1033 gc = font.get_glyph_collection (g.get_name ()); 1034 1035 if (gc != null) { 1036 ui.glyphs.add (((!) gc).copy ()); 1037 } else { 1038 ui.glyphs.add (new GlyphCollection (g.get_unicode_character (), g.get_name ())); 1039 } 1040 } 1041 1042 return ui; 1043 } 1044 1045 public void store_undo_state (GlyphCollection gc) { 1046 OverViewUndoItem i = new OverViewUndoItem (); 1047 Font f = BirdFont.get_current_font (); 1048 i.alternate_sets = f.alternates.copy (); 1049 i.glyphs.add (gc); 1050 store_undo_items (i); 1051 } 1052 1053 public void store_undo_items (OverViewUndoItem i) { 1054 undo_items.add (i); 1055 redo_items.clear (); 1056 } 1057 1058 bool select_visible_glyph (string name) { 1059 int i = 0; 1060 1061 foreach (OverViewItem o in visible_items) { 1062 if (o.get_name () == name) { 1063 selected = i; 1064 selected_item = get_selected_item (); 1065 return true; 1066 } 1067 1068 if (i > 1000) { 1069 warning ("selected character not found"); 1070 return true; 1071 } 1072 1073 i++; 1074 } 1075 1076 return false; 1077 } 1078 1079 public void scroll_to_char (unichar c) { 1080 StringBuilder s = new StringBuilder (); 1081 1082 if (is_modifier_key (c)) { 1083 return; 1084 } 1085 1086 s.append_unichar (c); 1087 scroll_to_glyph (s.str); 1088 } 1089 1090 public void scroll_to_glyph (string name) { 1091 GlyphRange gr = glyph_range; 1092 int i, r, index; 1093 string ch; 1094 Font font = BirdFont.get_current_font (); 1095 GlyphCollection? glyphs = null; 1096 Glyph glyph; 1097 1098 index = -1; 1099 1100 if (items_per_row <= 0) { 1101 return; 1102 } 1103 1104 ch = name; 1105 1106 // selected char is visible 1107 if (select_visible_glyph (ch)) { 1108 return; 1109 } 1110 1111 // scroll to char 1112 if (all_available) { 1113 1114 // don't search for glyphs in huge CJK fonts 1115 if (font.length () > 300) { 1116 r = 0; 1117 } else { 1118 // FIXME: too slow 1119 for (r = 0; r < font.length (); r += items_per_row) { 1120 for (i = 0; i < items_per_row; i++) { 1121 glyphs = font.get_glyph_collection_indice ((uint32) r + i); 1122 return_if_fail (glyphs != null); 1123 glyph = ((!) glyphs).get_current (); 1124 1125 if (glyph.name == ch) { 1126 index = i; 1127 } 1128 } 1129 1130 if (index > -1) { 1131 break; 1132 } 1133 } 1134 } 1135 } else { 1136 1137 if (ch.char_count () > 1) { 1138 warning ("Can't scroll to ligature in this view"); 1139 return; 1140 } 1141 1142 for (r = 0; r < gr.length (); r += items_per_row) { 1143 for (i = 0; i < items_per_row; i++) { 1144 if (gr.get_char (r + i) == ch) { 1145 index = i; 1146 } 1147 } 1148 1149 if (index > -1) { 1150 break; 1151 } 1152 } 1153 } 1154 1155 if (index > -1) { 1156 first_visible = r; 1157 update_item_list (); 1158 select_visible_glyph (ch); 1159 } 1160 } 1161 1162 public override void double_click (uint button, double ex, double ey) 1163 requires (!is_null (visible_items) && !is_null (allocation)) { 1164 1165 return_if_fail (!is_null (this)); 1166 1167 foreach (OverViewItem i in visible_items) { 1168 if (i.double_click (button, ex, ey)) { 1169 open_overview_item (i); 1170 } 1171 } 1172 1173 GlyphCanvas.redraw (); 1174 } 1175 1176 public void open_overview_item (OverViewItem i) { 1177 if (i.glyphs != null) { 1178 open_glyph_signal ((!) i.glyphs); 1179 ((!) i.glyphs).get_current ().close_path (); 1180 } else { 1181 open_new_glyph_signal (i.character); 1182 } 1183 } 1184 1185 public void set_character_info (CharacterInfo i) { 1186 character_info = i; 1187 } 1188 1189 public int get_selected_index () { 1190 GlyphCollection gc; 1191 int index = 0; 1192 1193 if (selected_items.size == 0) { 1194 return 0; 1195 } 1196 1197 gc = selected_items.get (0); 1198 1199 foreach (OverViewItem i in visible_items) { 1200 if (i.glyphs != null && gc == ((!) i.glyphs)) { 1201 break; 1202 } 1203 1204 index++; 1205 } 1206 1207 return index; 1208 } 1209 1210 public void hide_menu () { 1211 foreach (OverViewItem i in visible_items) { 1212 i.hide_menu (); 1213 } 1214 } 1215 1216 public override void button_press (uint button, double x, double y) { 1217 OverViewItem i; 1218 int index = 0; 1219 int selected_index = -1; 1220 bool update = false; 1221 1222 if (character_info != null) { 1223 character_info = null; 1224 GlyphCanvas.redraw (); 1225 return; 1226 } 1227 1228 for (int j = 0; j < visible_items.size; j++) { 1229 i = visible_items.get (j); 1230 1231 if (i.click (button, x, y)) { 1232 selected = index; 1233 selected_item = get_selected_item (); 1234 1235 if (KeyBindings.has_shift ()) { 1236 if (selected_item.glyphs != null) { 1237 1238 selected_index = selected_items.index_of ((!) selected_item.glyphs); 1239 if (selected_index == -1) { 1240 selected_items.add ((!) selected_item.glyphs); 1241 } else { 1242 return_if_fail (0 <= selected_index < selected_items.size); 1243 selected_items.remove_at (selected_index); 1244 selected = get_selected_index (); 1245 selected_item = get_selected_item (); 1246 } 1247 } 1248 } else { 1249 selected_items.clear (); 1250 if (selected_item.glyphs != null) { 1251 selected_items.add ((!) selected_item.glyphs); 1252 } 1253 } 1254 1255 update = !i.version_menu.menu_visible; 1256 } 1257 index++; 1258 } 1259 1260 if (update) { 1261 update_item_list (); 1262 } 1263 1264 GlyphCanvas.redraw (); 1265 } 1266 1267 /** Returns true if overview shows the last character. */ 1268 private bool at_bottom () { 1269 Font f; 1270 double t = rows * items_per_row + first_visible; 1271 1272 if (all_available) { 1273 f = BirdFont.get_current_font (); 1274 return t >= f.length (); 1275 } 1276 1277 return t >= glyph_range.length (); 1278 } 1279 1280 public void set_current_glyph_range (GlyphRange range) { 1281 GlyphRange? current = glyph_range; 1282 string c; 1283 1284 if (current != null) { 1285 c = glyph_range.get_char (selected); 1286 } 1287 1288 all_available = false; 1289 1290 glyph_range = range; 1291 scroll_top (); 1292 1293 // TODO: scroll down to c 1294 update_item_list (); 1295 selected_item = get_selected_item (); 1296 1297 GlyphCanvas.redraw (); 1298 } 1299 1300 public void select_next_glyph () { 1301 key_right (); 1302 } 1303 1304 public void open_current_glyph () { 1305 open_overview_item (selected_item); 1306 } 1307 1308 public override void update_scrollbar () { 1309 Font f; 1310 double nrows = 0; 1311 double pos = 0; 1312 double size; 1313 double visible_rows; 1314 1315 if (rows == 0) { 1316 MainWindow.set_scrollbar_size (0); 1317 MainWindow.set_scrollbar_position (0); 1318 } else { 1319 if (all_available) { 1320 f = BirdFont.get_current_font (); 1321 nrows = Math.floor ((f.length ()) / rows); 1322 size = f.length (); 1323 } else { 1324 nrows = Math.floor ((glyph_range.length ()) / rows); 1325 size = glyph_range.length (); 1326 } 1327 1328 if (nrows <= 0) { 1329 nrows = 1; 1330 } 1331 1332 visible_rows = allocation.height / OverViewItem.height; 1333 scroll_size = visible_rows / nrows; 1334 MainWindow.set_scrollbar_size (scroll_size); 1335 pos = first_visible / (nrows * items_per_row - visible_rows * items_per_row); 1336 MainWindow.set_scrollbar_position (pos); 1337 } 1338 } 1339 1340 /** Display one entry from the Unicode Character Database. */ 1341 void draw_character_info (Context cr) 1342 requires (character_info != null) { 1343 double x, y, w, h; 1344 int i; 1345 string unicode_value, unicode_description; 1346 string[] column; 1347 string entry; 1348 int len = 0; 1349 int length = 0; 1350 bool see_also = false; 1351 WidgetAllocation allocation = MainWindow.get_overview ().allocation; 1352 string name; 1353 string[] lines; 1354 double character_start; 1355 double character_height; 1356 1357 entry = ((!)character_info).get_entry (); 1358 lines = entry.split ("\n"); 1359 1360 foreach (string line in entry.split ("\n")) { 1361 len = line.char_count (); 1362 if (len > length) { 1363 length = len; 1364 } 1365 } 1366 1367 x = allocation.width * 0.1; 1368 y = allocation.height * 0.1; 1369 w = allocation.width * 0.9 - x; 1370 h = allocation.height * 0.9 - y; 1371 1372 if (w < 8 * length) { 1373 w = 8 * length; 1374 x = (allocation.width - w) / 2.0; 1375 } 1376 1377 if (x < 0) { 1378 x = 2; 1379 } 1380 1381 // background 1382 cr.save (); 1383 Theme.color_opacity (cr, "Background 1", 0.98); 1384 cr.rectangle (x, y, w, h); 1385 cr.fill (); 1386 cr.restore (); 1387 1388 cr.save (); 1389 Theme.color_opacity (cr, "Foreground 1", 0.98); 1390 cr.set_line_width (2); 1391 cr.rectangle (x, y, w, h); 1392 cr.stroke (); 1393 cr.restore (); 1394 1395 // database entry 1396 1397 if (((!)character_info).is_ligature ()) { 1398 name = ((!)character_info).get_name (); 1399 draw_info_line (name, cr, x, y, 0); 1400 } else { 1401 i = 0; 1402 foreach (string line in lines) { 1403 if (i == 0) { 1404 column = line.split ("\t"); 1405 return_if_fail (column.length == 2); 1406 unicode_value = "U+" + column[0]; 1407 unicode_description = column[1]; 1408 1409 draw_info_line (unicode_description, cr, x, y, i); 1410 i++; 1411 1412 draw_info_line (unicode_value, cr, x, y, i); 1413 i++; 1414 } else { 1415 1416 if (line.has_prefix ("\t*")) { 1417 draw_info_line (line.replace ("\t*", "•"), cr, x, y, i); 1418 i++; 1419 } else if (line.has_prefix ("\tx (")) { 1420 if (!see_also) { 1421 i++; 1422 draw_info_line (t_("See also:"), cr, x, y, i); 1423 i++; 1424 see_also = true; 1425 } 1426 1427 draw_info_line (line.replace ("\tx (", "•").replace (")", ""), cr, x, y, i); 1428 i++; 1429 } else { 1430 i++; 1431 } 1432 } 1433 } 1434 1435 character_start = y + 10 + i * UCD_LINE_HEIGHT; 1436 character_height = h - character_start; 1437 draw_fallback_character (cr, x, character_start, character_height); 1438 } 1439 } 1440 1441 /** Fallback character in UCD info. */ 1442 void draw_fallback_character (Context cr, double x, double y, double height) 1443 requires (character_info != null) { 1444 unichar c = ((!)character_info).unicode; 1445 1446 cr.save (); 1447 Text character = new Text (); 1448 Theme.text_color (character, "Foreground 1"); 1449 character.set_text ((!) c.to_string ()); 1450 character.set_font_size (height); 1451 character.draw_at_top (cr, x + 10, y); 1452 cr.restore (); 1453 } 1454 1455 void draw_info_line (string line, Context cr, double x, double y, int row) { 1456 Text ucd_entry = new Text (line); 1457 cr.save (); 1458 Theme.text_color (ucd_entry, "Foreground 1"); 1459 ucd_entry.widget_x = 10 + x; 1460 ucd_entry.widget_y = 10 + y + row * UCD_LINE_HEIGHT; 1461 ucd_entry.draw (cr); 1462 cr.restore (); 1463 } 1464 1465 public void paste () { 1466 GlyphCollection gc; 1467 GlyphCollection? c; 1468 Glyph glyph; 1469 uint32 index; 1470 int i; 1471 int skip = 0; 1472 int s; 1473 string character_string; 1474 Gee.ArrayList<GlyphCollection> glyps; 1475 Font f; 1476 OverViewUndoItem undo_item; 1477 1478 f = BirdFont.get_current_font (); 1479 gc = new GlyphCollection ('\0', ""); 1480 glyps = new Gee.ArrayList<GlyphCollection> (); 1481 1482 copied_glyphs.sort ((a, b) => { 1483 return (int) ((GlyphCollection) a).get_unicode_character () 1484 - (int) ((GlyphCollection) b).get_unicode_character (); 1485 }); 1486 1487 index = (uint32) first_visible + selected; 1488 for (i = 0; i < copied_glyphs.size; i++) { 1489 if (all_available) { 1490 if (f.length () == 0) { 1491 c = add_empty_character_to_font (copied_glyphs.get (i).get_unicode_character (), 1492 copied_glyphs.get (i).is_unassigned (), 1493 copied_glyphs.get (i).get_name ()); 1494 } else if (index >= f.length ()) { 1495 // FIXME: duplicated unicodes? 1496 c = add_empty_character_to_font (copied_glyphs.get (i).get_unicode_character (), 1497 copied_glyphs.get (i).is_unassigned (), 1498 copied_glyphs.get (i).get_name ()); 1499 } else { 1500 c = f.get_glyph_collection_indice ((uint32) index); 1501 } 1502 1503 if (c == null) { 1504 c = add_empty_character_to_font (copied_glyphs.get (i).get_unicode_character (), 1505 copied_glyphs.get (i).is_unassigned (), 1506 copied_glyphs.get (i).get_name ()); 1507 } 1508 1509 return_if_fail (c != null); 1510 gc = (!) c; 1511 } else { 1512 if (i != 0) { 1513 s = (int) copied_glyphs.get (i).get_unicode_character (); 1514 s -= (int) copied_glyphs.get (i - 1).get_unicode_character (); 1515 s -= 1; 1516 skip += s; 1517 } 1518 1519 character_string = glyph_range.get_char ((uint32) (index + skip)); 1520 c = f.get_glyph_collection_by_name (character_string); 1521 1522 if (c == null) { 1523 gc = add_empty_character_to_font (character_string.get_char (), 1524 copied_glyphs.get (i).is_unassigned (), 1525 copied_glyphs.get (i).get_name ()); 1526 } else { 1527 gc = (!) c; 1528 } 1529 } 1530 1531 glyps.add (gc); 1532 index++; 1533 } 1534 1535 undo_item = new OverViewUndoItem (); 1536 undo_item.alternate_sets = f.alternates.copy (); 1537 foreach (GlyphCollection g in glyps) { 1538 undo_item.glyphs.add (g.copy ()); 1539 } 1540 store_undo_items (undo_item); 1541 1542 if (glyps.size != copied_glyphs.size) { 1543 warning ("glyps.size != copied_glyphs.size"); 1544 return; 1545 } 1546 1547 if (copied_glyphs.size < i) { 1548 warning ("Array index out of bounds."); 1549 return; 1550 } 1551 1552 i = 0; 1553 foreach (GlyphCollection g in glyps) { 1554 glyph = copied_glyphs.get (i).get_current ().copy (); 1555 glyph.version_id = (glyph.version_id == -1 || g.length () == 0) ? 1 : g.get_last_id () + 1; 1556 glyph.unichar_code = g.get_unicode_character (); 1557 1558 if (!g.is_unassigned ()) { 1559 glyph.name = (!) glyph.unichar_code.to_string (); 1560 } else { 1561 glyph.name = g.get_name (); 1562 } 1563 1564 g.insert_glyph (glyph, true); 1565 i++; 1566 } 1567 1568 f.touch (); 1569 } 1570 1571 public class OverViewUndoItem { 1572 public AlternateSets alternate_sets = new AlternateSets (); 1573 public Gee.ArrayList<GlyphCollection> glyphs = new Gee.ArrayList<GlyphCollection> (); 1574 } 1575 } 1576 1577 } 1578