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