The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

MenuTab.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/MenuTab.vala.
Use other types of graphical objects
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 namespace BirdFont { 16 17 public class MenuTab : FontDisplay { 18 19 /** Ignore input events when the background thread is running. 20 * 21 * Do always check the return value of set_suppress_event when this 22 * variable is updated. 23 * 24 * This variable is used only in the gui thread. 25 */ 26 private static bool suppress_event; 27 28 /** True if the background thread is running. */ 29 public static bool background_thread; 30 31 /** A notification sent when the file has been saved. */ 32 public static SaveCallback save_callback; 33 34 /** A notification sent when the file has been loaded. */ 35 public static LoadCallback load_callback; 36 37 /** A notification sent when the file has been loaded. */ 38 public static ExportCallback export_callback; 39 40 public MenuTab () { 41 save_callback = new SaveCallback (); 42 save_callback.is_done = true; 43 44 load_callback = new LoadCallback (); 45 export_callback = new ExportCallback (); 46 47 suppress_event = false; 48 background_thread = false; 49 } 50 51 public static ExportCallback get_export_callback () { 52 return export_callback; 53 } 54 55 public static bool has_suppress_event () { 56 bool suppress; 57 58 lock (suppress_event) { 59 suppress = suppress_event; 60 } 61 62 return suppress; 63 } 64 65 public static void set_save_callback (SaveCallback c) { 66 if (!save_callback.is_done) { 67 warning ("Prevoius save command has not finished"); 68 } 69 70 save_callback = c; 71 } 72 73 public static void start_background_thread () { 74 if (!set_suppress_event (true)) { 75 warning ("suppressed event"); 76 return; 77 } 78 79 TabBar.start_wheel (); 80 } 81 82 public static void stop_background_thread () { 83 IdleSource idle = new IdleSource (); 84 idle.set_callback (() => { 85 set_suppress_event (false); 86 TabBar.stop_wheel (); 87 GlyphCanvas.redraw (); 88 return false; 89 }); 90 idle.attach (null); 91 } 92 93 public static bool validate_metadata () { 94 Font font = BirdFont.get_current_font (); 95 string m = t_("Missing metadata in font:") + "\n"; 96 97 if (font.postscript_name == "") { 98 MainWindow.show_message (m + t_("PostScript Name")); 99 return false; 100 } 101 102 if (font.name == "") { 103 MainWindow.show_message (m + t_("Name")); 104 return false; 105 } 106 107 if (font.subfamily == "") { 108 MainWindow.show_message (m + t_("Style")); 109 return false; 110 } 111 112 if (font.full_name == "") { 113 MainWindow.show_message (m + t_("Full Name (Name and Style)")); 114 return false; 115 } 116 117 if (font.unique_identifier == "") { 118 MainWindow.show_message (m + t_("Unique Identifier")); 119 return false; 120 } 121 122 Font current_font = BirdFont.get_current_font (); 123 string ttf_name = ExportSettings.get_file_name (current_font) + ".ttf"; 124 string ttf_name_mac = ExportSettings.get_file_name_mac (current_font) + ".ttf"; 125 126 print (@"$ttf_name == $ttf_name_mac"); 127 if (ttf_name == ttf_name_mac) { 128 MainWindow.show_message (t_("You need to choose a different name for the TTF file with Mac adjustmets.")); 129 ttf_name_mac = ExportSettings.get_file_name_mac (current_font) + " Mac.ttf"; 130 return false; 131 } 132 133 return true; 134 } 135 136 public static void export_fonts_in_background () { 137 Font f; 138 139 if (suppress_event || !MainWindow.native_window.can_export ()) { 140 return; 141 } 142 143 f = BirdFont.get_current_font (); 144 145 if (f.font_file == null) { 146 MainWindow.show_message (t_("You need to save your font before exporting it.")); 147 return; 148 } 149 150 if (!validate_metadata ()) { 151 return; 152 } 153 154 if (!ExportSettings.has_export_settings (f)) { 155 show_export_settings_tab (); 156 } else { 157 MenuTab.export_callback = new ExportCallback (); 158 MenuTab.export_callback.export_fonts_in_background (); 159 } 160 } 161 162 public static bool set_suppress_event (bool e) { 163 if (suppress_event && e) { 164 warning ("suppress_event is already set"); 165 return false; 166 } 167 168 if (e) { 169 TabContent.create_pause_surface (); 170 } 171 172 background_thread = e; 173 suppress_event = e; 174 175 // key up will be ignored if events are suppressed 176 if (suppress_event) { 177 IdleSource idle = new IdleSource (); 178 idle.set_callback (() => { 179 KeyBindings.reset (); 180 return false; 181 }); 182 idle.attach (null); 183 } 184 185 return true; 186 } 187 188 public override string get_label () { 189 return t_("Menu"); 190 } 191 192 public override string get_name () { 193 return "Menu"; 194 } 195 196 public static void select_overview () { 197 if (suppress_event) { 198 warn_if_test ("Event suppressed"); 199 return; 200 } 201 202 if (BirdFont.get_current_font ().is_empty ()) { 203 show_default_characters (); 204 } else { 205 MainWindow.get_tab_bar ().add_unique_tab (new OverView ()); 206 MainWindow.get_tab_bar ().select_tab_name ("Overview"); 207 } 208 } 209 210 public static void signal_file_exported () { 211 IdleSource idle = new IdleSource (); 212 idle.set_callback (() => { 213 export_callback.file_exported (); 214 return false; 215 }); 216 idle.attach (null); 217 } 218 219 public static void signal_file_saved () { 220 IdleSource idle = new IdleSource (); 221 idle.set_callback (() => { 222 save_callback.file_saved (); 223 return false; 224 }); 225 idle.attach (null); 226 } 227 228 public static void signal_file_loaded () { 229 IdleSource idle = new IdleSource (); 230 idle.set_callback (() => { 231 load_callback.file_loaded (); 232 MainWindow.native_window.font_loaded (); 233 return false; 234 }); 235 idle.attach (null); 236 } 237 238 public static void apply_font_setting (Font f) { 239 DrawingTools.background_scale.set_value (f.background_scale); 240 241 DrawingTools.grid_expander.tool.clear (); 242 243 if (f.grid_width.size == 0) { 244 f.grid_width.add ("1"); 245 f.grid_width.add ("2"); 246 f.grid_width.add ("4"); 247 } 248 249 foreach (string grid in f.grid_width) { 250 DrawingTools.add_new_grid (double.parse (grid), false); 251 } 252 253 string sw = f.settings.get_setting ("stroke_width"); 254 if (sw != ""){ 255 StrokeTool.stroke_width = double.parse (sw); 256 DrawingTools.object_stroke.set_value_round (StrokeTool.stroke_width); 257 } 258 259 string pt = f.settings.get_setting ("point_type"); 260 DrawingTools.set_default_point_type (pt); 261 262 string stroke = f.settings.get_setting ("apply_stroke"); 263 bool s = bool.parse (stroke); 264 DrawingTools.add_stroke.set_selected (s); 265 StrokeTool.add_stroke = s; 266 267 string lc = f.settings.get_setting ("line_cap"); 268 269 if (lc == "butt") { 270 StrokeTool.line_cap = LineCap.BUTT; 271 } else if (lc == "square") { 272 StrokeTool.line_cap = LineCap.SQUARE; 273 } else if (lc == "round") { 274 StrokeTool.line_cap = LineCap.ROUND; 275 } 276 277 DrawingTools.set_stroke_tool_visibility (); 278 279 string lock_grid = f.settings.get_setting ("lock_grid"); 280 bool lg = bool.parse (lock_grid); 281 GridTool.lock_grid = lg; 282 DrawingTools.lock_grid.selected = GridTool.lock_grid; 283 284 string skew_overview = f.settings.get_setting ("skew_overview"); 285 if (skew_overview != "") { 286 double so = double.parse (skew_overview); 287 OverviewTools.skew.set_value_round (so); 288 } 289 290 string autotrace_resolution = f.settings.get_setting ("autotrace_resolution"); 291 if (autotrace_resolution != "") { 292 double ar = double.parse (autotrace_resolution); 293 DrawingTools.background_threshold.set_value_round (ar); 294 } 295 296 string autotrace_threshold = f.settings.get_setting ("autotrace_threshold"); 297 if (autotrace_threshold != "") { 298 double at = double.parse (autotrace_threshold); 299 DrawingTools.auto_trace_resolution.set_value_round (at); 300 } 301 302 string autotrace_simplification = f.settings.get_setting ("autotrace_simplification"); 303 if (autotrace_simplification != "") { 304 double asi = double.parse (autotrace_simplification); 305 DrawingTools.auto_trace_simplify.set_value_round (asi); 306 } 307 308 string kerning_zoom = f.settings.get_setting ("kerning_zoom"); 309 if (kerning_zoom != "") { 310 double k = double.parse (kerning_zoom); 311 if (!is_null (KerningTools.zoom_bar)) { 312 KerningTools.zoom_bar.zoom_level = k; 313 KerningTools.zoom_bar.new_zoom (k); 314 } 315 } 316 317 string spacing_zoom = f.settings.get_setting ("spacing_zoom"); 318 if (spacing_zoom != "") { 319 double sz = double.parse (spacing_zoom); 320 if (!is_null (SpacingTools.zoom_bar)) { 321 SpacingTools.zoom_bar.zoom_level = sz; 322 SpacingTools.zoom_bar.new_zoom (sz); 323 } 324 } 325 326 MainWindow.get_toolbox ().update_expanders (); 327 MainWindow.get_toolbox ().update_all_expanders (); 328 Toolbox.redraw_tool_box (); 329 OverViewItem.glyph_scale = 1; 330 } 331 332 // FIXME: background thread 333 public static void save_as_bfp () { 334 FileChooser fc = new FileChooser (); 335 336 if (suppress_event) { 337 warn_if_test ("Event suppressed"); 338 return; 339 } 340 341 if (!set_suppress_event (true)) { 342 return; 343 } 344 345 fc.file_selected.connect((fn) => { 346 Font f = BirdFont.get_current_font (); 347 348 if (fn != null) { 349 f.init_bfp ((!) fn); 350 } 351 352 set_suppress_event (false); 353 }); 354 355 MainWindow.file_chooser (t_("Save"), fc, FileChooser.SAVE); 356 } 357 358 public static void new_file () { 359 Font font; 360 SaveDialogListener dialog = new SaveDialogListener (); 361 362 if (suppress_event) { 363 warn_if_test ("Event suppressed"); 364 return; 365 } 366 367 font = BirdFont.get_current_font (); 368 369 dialog.signal_discard.connect (() => { 370 MainWindow.close_all_tabs (); 371 372 BirdFont.new_font (); 373 MainWindow.native_window.font_loaded (); 374 375 show_default_characters (); 376 377 GlyphCanvas.redraw (); 378 }); 379 380 dialog.signal_save.connect (() => { 381 MenuTab.save_callback = new SaveCallback (); 382 MenuTab.save_callback.file_saved.connect (() => { 383 dialog.signal_discard (); 384 }); 385 save_callback.save (); 386 }); 387 388 dialog.signal_cancel.connect (() => { 389 MainWindow.hide_dialog (); 390 }); 391 392 if (!font.is_modified ()) { 393 dialog.signal_discard (); 394 } else { 395 MainWindow.show_dialog (new SaveDialog (dialog)); 396 } 397 398 return; 399 } 400 401 public static void quit () { 402 if (suppress_event) { 403 warn_if_test ("Event suppressed"); 404 return; 405 } 406 407 TabContent.hide_text_input (); 408 409 SaveDialogListener dialog = new SaveDialogListener (); 410 Font font = BirdFont.get_current_font (); 411 412 Preferences.save (); 413 414 dialog.signal_discard.connect (() => { 415 ensure_main_loop_is_empty (); 416 MainWindow.native_window.quit (); 417 }); 418 419 dialog.signal_save.connect (() => { 420 MenuTab.set_save_callback (new SaveCallback ()); 421 MenuTab.save_callback.file_saved.connect (() => { 422 ensure_main_loop_is_empty (); 423 MainWindow.native_window.quit (); 424 }); 425 save_callback.save (); 426 }); 427 428 dialog.signal_cancel.connect (() => { 429 MainWindow.hide_dialog (); 430 }); 431 432 if (!font.is_modified ()) { 433 dialog.signal_discard (); 434 } else { 435 MainWindow.show_dialog (new SaveDialog (dialog)); 436 } 437 } 438 439 public static void show_export_settings_tab () { 440 MainWindow.get_tab_bar ().add_unique_tab (new ExportSettings ()); 441 } 442 443 public static void show_description () { 444 MainWindow.get_tab_bar ().add_unique_tab (new DescriptionDisplay ()); 445 } 446 447 public static void show_kerning_context () { 448 if (suppress_event) { 449 warn_if_test ("Event suppressed"); 450 return; 451 } 452 453 KerningDisplay kd = MainWindow.get_kerning_display (); 454 MainWindow.get_tab_bar ().add_unique_tab (kd); 455 } 456 457 public static void show_spacing_tab () { 458 if (suppress_event) { 459 warn_if_test ("Event suppressed"); 460 return; 461 } 462 463 SpacingTab s = MainWindow.get_spacing_tab (); 464 MainWindow.get_tab_bar ().add_unique_tab (s); 465 } 466 467 public static void show_ligature_tab () { 468 if (suppress_event) { 469 warn_if_test ("Event suppressed"); 470 return; 471 } 472 473 LigatureList d = MainWindow.get_ligature_display (); 474 MainWindow.get_tab_bar ().add_unique_tab (d); 475 } 476 477 public static void preview () { 478 Font font = BirdFont.get_current_font (); 479 480 if (suppress_event) { 481 warn_if_test ("Event suppressed"); 482 return; 483 } 484 485 if (font.font_file == null) { 486 save_callback = new SaveCallback (); 487 save_callback.file_saved.connect (() => { 488 show_preview_tab (); 489 }); 490 save_callback.save (); 491 } else { 492 show_preview_tab (); 493 } 494 } 495 496 public static void show_preview_tab () { 497 OverWriteDialogListener listener = new OverWriteDialogListener (); 498 TabBar tab_bar = MainWindow.get_tab_bar (); 499 FontFormat format = BirdFont.get_current_font ().format; 500 501 if (suppress_event) { 502 warn_if_test ("Event suppressed"); 503 return; 504 } 505 506 listener.overwrite_signal.connect (() => { 507 KeyBindings.set_modifier (NONE); 508 tab_bar.add_unique_tab (new Preview (), true); 509 PreviewTools.update_preview (); 510 }); 511 512 if ((format == FontFormat.SVG || format == FontFormat.FREETYPE) && !OverWriteDialogListener.dont_ask_again) { 513 MainWindow.show_dialog (new OverwriteDialog (listener)); 514 } else { 515 listener.overwrite (); 516 } 517 } 518 519 /** Display the language selection tab. */ 520 public static void select_language () { 521 if (suppress_event) { 522 warn_if_test ("Event suppressed"); 523 return; 524 } 525 526 MainWindow.get_tab_bar ().add_unique_tab (new LanguageSelectionTab ()); 527 } 528 529 public static void use_current_glyph_as_background () { 530 if (suppress_event) { 531 warn_if_test ("Event suppressed"); 532 return; 533 } 534 535 Glyph.background_glyph = MainWindow.get_current_glyph (); 536 537 if (MainWindow.get_current_display () is OverView) { 538 Glyph.background_glyph = MainWindow.get_overview ().get_current_glyph (); 539 } 540 } 541 542 public static void reset_glyph_background () { 543 Glyph.background_glyph = null; 544 } 545 546 public static void remove_all_kerning_pairs () { 547 if (suppress_event) { 548 warn_if_test ("Event suppressed"); 549 return; 550 } 551 552 KerningClasses classes = BirdFont.get_current_font ().get_kerning_classes (); 553 classes.remove_all_pairs (); 554 KerningTools.update_kerning_classes (); 555 } 556 557 public static void list_all_kerning_pairs () { 558 if (suppress_event) { 559 warn_if_test ("Event suppressed"); 560 return; 561 } 562 563 MainWindow.get_tab_bar ().add_unique_tab (new KerningList ()); 564 } 565 566 public static void ensure_main_loop_is_empty () { 567 unowned MainContext context; 568 bool acquired; 569 570 context = MainContext.default (); 571 acquired = context.acquire (); 572 573 if (unlikely (!acquired)) { 574 warning ("Failed to acquire main loop.\n"); 575 return; 576 } 577 578 while (context.pending ()) { 579 context.iteration (true); 580 } 581 context.release (); 582 } 583 584 public static void save_as () { 585 if (MenuTab.has_suppress_event () || !save_callback.is_done) { 586 warn_if_test ("Event suppressed"); 587 return; 588 } 589 590 MenuTab.set_save_callback (new SaveCallback ()); 591 MenuTab.save_callback.save_as(); 592 } 593 594 public static void save () { 595 if (MenuTab.has_suppress_event () && !save_callback.is_done) { 596 warn_if_test ("Event suppressed"); 597 return; 598 } 599 600 MenuTab.set_save_callback (new SaveCallback ()); 601 MenuTab.save_callback.save (); 602 } 603 604 public static void load () { 605 if (MenuTab.has_suppress_event ()) { 606 warn_if_test ("Event suppressed"); 607 return; 608 } 609 610 MenuTab.load_callback = new LoadCallback (); 611 MenuTab.load_callback.load (); 612 613 MenuTab.load_callback.file_loaded.connect (() => { 614 Font f = BirdFont.get_current_font (); 615 MenuTab.apply_font_setting (f); 616 }); 617 } 618 619 public static void move_to_baseline () { 620 if (suppress_event) { 621 warn_if_test ("Event suppressed"); 622 return; 623 } 624 625 DrawingTools.move_tool.move_to_baseline (); 626 } 627 628 public static void show_file_dialog_tab (string title, FileChooser action, bool folder) { 629 FileDialogTab ft = new FileDialogTab (title, action, folder); 630 MainWindow.get_tab_bar ().add_tab (ft); 631 } 632 633 public static void simplify_path () { 634 if (suppress_event) { 635 warn_if_test ("Event suppressed"); 636 return; 637 } 638 639 Task t = new Task (simplify); 640 MainWindow.run_blocking_task (t); 641 } 642 643 private static void simplify () { 644 Glyph g = MainWindow.get_current_glyph (); 645 Gee.ArrayList<Path> paths = new Gee.ArrayList<Path> (); 646 647 // selected objects 648 foreach (Path p in g.active_paths) { 649 paths.add (PenTool.simplify (p, false, PenTool.simplification_threshold)); 650 } 651 652 // selected segments 653 if (paths.size == 0) { 654 foreach (Path p in g.get_all_paths ()) { 655 g.add_active_path (null, p); 656 } 657 658 foreach (Path p in g.active_paths) { 659 paths.add (PenTool.simplify (p, true, PenTool.simplification_threshold)); 660 } 661 } 662 663 g.store_undo_state (); 664 665 foreach (Path p in g.active_paths) { 666 g.layers.remove_path (p); 667 } 668 669 foreach (Path p in g.active_paths) { 670 g.layers.remove_path (p); 671 } 672 673 foreach (Path p in paths) { 674 g.add_path (p); 675 g.add_active_path (null, p); 676 } 677 678 g.active_paths.clear (); 679 g.update_view (); 680 } 681 682 public static void show_spacing_class_tab () { 683 SpacingClassTab t = MainWindow.get_spacing_class_tab (); 684 MainWindow.get_tab_bar ().add_unique_tab (t); 685 } 686 687 public static void add_ligature () { 688 TextListener listener; 689 string ligature_name = ""; 690 691 if (suppress_event) { 692 warn_if_test ("Event suppressed"); 693 return; 694 } 695 696 listener = new TextListener (t_("Name"), "", t_("Add ligature")); 697 698 listener.signal_text_input.connect ((text) => { 699 ligature_name = text; 700 }); 701 702 listener.signal_submit.connect (() => { 703 Font font = BirdFont.get_current_font (); 704 GlyphCollection? fg; 705 Glyph glyph; 706 GlyphCollection glyph_collection; 707 OverView o = MainWindow.get_overview (); 708 709 fg = font.get_glyph_collection_by_name (ligature_name); 710 711 if (fg == null) { 712 glyph_collection = new GlyphCollection ('\0', ligature_name); 713 714 glyph = new Glyph (ligature_name, '\0'); 715 glyph_collection.set_unassigned (true); 716 glyph_collection.insert_glyph (glyph, true); 717 718 font.add_glyph_collection (glyph_collection); 719 } 720 721 o.display_all_available_glyphs (); 722 o.scroll_to_glyph (ligature_name); 723 724 TabContent.hide_text_input (); 725 show_all_available_characters (); 726 }); 727 728 TabContent.show_text_input (listener); 729 } 730 731 public static void show_default_characters () { 732 MainWindow.get_tab_bar ().add_unique_tab (new OverView ()); 733 OverView o = MainWindow.get_overview (); 734 GlyphRange gr = new GlyphRange (); 735 736 if (!BirdFont.get_current_font ().initialised) { 737 MenuTab.new_file (); 738 } 739 740 DefaultCharacterSet.use_default_range (gr); 741 o.set_current_glyph_range (gr); 742 743 MainWindow.get_tab_bar ().select_tab_name ("Overview"); 744 } 745 746 public static void show_all_available_characters () { 747 MainWindow.get_tab_bar ().add_unique_tab (new OverView ()); 748 749 if (!BirdFont.get_current_font ().initialised) { 750 MenuTab.new_file (); 751 } 752 753 MainWindow.get_tab_bar ().select_tab_name ("Overview"); 754 OverviewTools.show_all_available_characters (); 755 } 756 757 public static void show_background_tab () { 758 BackgroundTab bt; 759 760 if (suppress_event) { 761 warn_if_test ("Event suppressed"); 762 return; 763 } 764 765 bt = BackgroundTab.get_instance (); 766 MainWindow.get_tab_bar ().add_unique_tab (bt); 767 } 768 769 public static void show_settings_tab () { 770 MainWindow.get_tab_bar ().add_unique_tab (new SettingsTab ()); 771 } 772 773 public static void show_theme_tab () { 774 MainWindow.get_tab_bar ().add_unique_tab (new ThemeTab ()); 775 } 776 777 public static void show_guide_tab () { 778 MainWindow.get_tab_bar ().add_unique_tab (new GuideTab ()); 779 } 780 } 781 782 } 783