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