The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

GtkWindow.vala in birdfont

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 birdfont/GtkWindow.vala.
Platform independent overwrite dialog
1 /* 2 Copyright (C) 2012, 2013, 2014 Johan Mattsson 3 4 This program is free software: you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation, either version 3 of the License, or 7 (at your option) any later version. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 using Cairo; 19 using Gtk; 20 using Gdk; 21 using BirdFont; 22 using WebKit; 23 using Gdk; 24 using Notify; 25 26 namespace BirdFont { 27 28 public class GtkWindow : Gtk.Window, NativeWindow { 29 30 Box list_box; 31 Box canvas_box; 32 33 WebView html_canvas; 34 ScrolledWindow html_box; 35 36 Box tab_box; 37 38 GlyphCanvasArea glyph_canvas_area; 39 40 Clipboard clipboard; 41 string clipboard_svg = ""; 42 string inkscape_clipboard = ""; 43 44 Scrollbar scrollbar; 45 bool scrollbar_supress_signal = false; 46 47 DescriptionForm description; 48 49 /** Text input and callbacks. */ 50 public static bool text_input_is_active = false; 51 TextListener text_listener = new TextListener ("", "", ""); 52 Label text_input_label; 53 Entry text_entry; 54 Box text_box; 55 Gtk.Button submit_text_button; 56 57 Gtk.Window tooltip_window = new Gtk.Window (); 58 59 ToolboxCanvas toolbox; 60 61 Task background_task = new Task (); 62 63 public GtkWindow (string title) { 64 scrollbar = new Scrollbar (Orientation.VERTICAL, new Adjustment (0, 0, 1, 1, 0.01, 0.1)); 65 ((Gtk.Window)this).set_title ("BirdFont"); 66 } 67 68 public void init () { 69 Notify.init ("Fonts have been exported."); 70 71 description = new DescriptionForm (); 72 73 clipboard = Clipboard.get_for_display (get_display (), Gdk.SELECTION_CLIPBOARD); 74 75 Signal.connect(this, "notify::is-active", (GLib.Callback) window_focus, null); 76 77 scrollbar.value_changed.connect (() => { 78 double p; 79 80 if (!scrollbar_supress_signal) { 81 p = scrollbar.get_value () / (1 - scrollbar.adjustment.page_size); 82 FontDisplay display = MainWindow.get_current_display (); 83 display.scroll_to (p); 84 } 85 }); 86 87 delete_event.connect (() => { 88 MenuTab.quit (); 89 return true; 90 }); 91 92 set_size_and_position (); 93 94 glyph_canvas_area = new GlyphCanvasArea (MainWindow.glyph_canvas); 95 96 html_canvas = new WebView (); 97 WebKit.set_cache_model (CacheModel.DOCUMENT_VIEWER); 98 html_canvas.get_settings ().enable_default_context_menu = false; 99 100 html_box = new ScrolledWindow (null, null); 101 html_box.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC); 102 html_box.add (html_canvas); 103 html_canvas.set_editable (true); 104 105 MainWindow.get_tab_bar ().signal_tab_selected.connect ((f, tab) => { 106 string uri = ""; 107 string html = ""; 108 FontDisplay fd = tab.get_display (); 109 110 scrollbar.set_visible (fd.has_scrollbar ()); 111 112 if (fd.get_name () == "Preview") { 113 uri = Preview.get_uri (); 114 html = Preview.get_html_with_absolute_paths (); 115 116 try { 117 html_canvas.load_html_string (html, uri); 118 } catch (Error e) { 119 warning (e.message); 120 warning ("Failed to load html into canvas."); 121 } 122 123 // show the webview when loading has finished 124 html_box.set_visible (true); 125 glyph_canvas_area.set_visible (false); 126 description.canvas.set_visible (false); 127 128 } else { 129 html_box.set_visible (false); 130 glyph_canvas_area.set_visible (true); 131 description.canvas.set_visible (false); 132 } 133 }); 134 135 // Hide this canvas when window is realized and flip canvas 136 // visibility in tab selection signal. 137 html_canvas.draw.connect ((t, e) => { 138 glyph_canvas_area.set_visible (false); 139 return false; 140 }); 141 142 canvas_box = new Box (Orientation.HORIZONTAL, 0); 143 canvas_box.pack_start (glyph_canvas_area, true, true, 0); 144 canvas_box.pack_start (html_box, true, true, 0); 145 canvas_box.pack_start (description.canvas, true, true, 0); 146 canvas_box.pack_start (scrollbar, false, true, 0); 147 148 submit_text_button = new Gtk.Button (); 149 submit_text_button.set_label ("Submit"); 150 text_input_label = new Label (" " + "Text"); 151 text_entry = new Entry (); 152 text_box = new Box (Orientation.HORIZONTAL, 6); 153 text_box.pack_start (text_input_label, false, false, 0); 154 text_box.pack_start (text_entry, true, true, 0); 155 text_box.pack_start (submit_text_button, false, false, 0); 156 157 text_entry.changed.connect (() => { 158 text_listener.signal_text_input (text_entry.text); 159 }); 160 161 submit_text_button.clicked.connect (() => { 162 text_listener.signal_submit (text_entry.text); 163 text_input_is_active = false; 164 }); 165 166 tab_box = new Box (Orientation.VERTICAL, 0); 167 168 tab_box.pack_start (new TabbarCanvas (MainWindow.get_tab_bar ()), false, false, 0); 169 tab_box.pack_start (text_box, false, false, 5); 170 tab_box.pack_start (canvas_box, true, true, 0); 171 172 toolbox = new ToolboxCanvas (MainWindow.get_toolbox ()); 173 list_box = new Box (Orientation.HORIZONTAL, 0); 174 list_box.pack_start (toolbox, false, false, 0); 175 list_box.pack_start (tab_box, true, true, 0); 176 177 Box vbox = new Box (Orientation.VERTICAL, 0); 178 vbox.pack_start(list_box, true, true, 0); 179 add (vbox); 180 181 try { 182 set_icon_from_file ((!) SearchPaths.find_file (null, "birdfont_window_icon.png").get_path ()); 183 } catch (GLib.Error e) { 184 warning (e.message); 185 } 186 187 key_press_event.connect ((t, event) => { 188 if (!GtkWindow.text_input_is_active) { 189 GtkWindow.reset_modifier (event.state); 190 TabContent.key_press (event.keyval); 191 } 192 193 return false; 194 }); 195 196 key_release_event.connect ((t, event) => { 197 if (!GtkWindow.text_input_is_active) { 198 TabContent.key_release (event.keyval); 199 } 200 201 return false; 202 }); 203 204 show_all (); 205 206 scrollbar.set_visible (false); 207 description.canvas.set_visible (false); 208 209 hide_text_input (); 210 211 MainWindow.open_recent_files_tab (); 212 } 213 214 public void window_focus (void* data) { 215 TabContent.reset_modifier (); 216 } 217 218 public static void reset_modifier (ModifierType flags) { 219 if ((flags & ModifierType.CONTROL_MASK) == 0) { 220 TabContent.key_release (Key.CTRL_RIGHT); 221 TabContent.key_release (Key.CTRL_LEFT); 222 } 223 224 if ((flags & ModifierType.SHIFT_MASK) == 0) { 225 TabContent.key_release (Key.SHIFT_LEFT); 226 TabContent.key_release (Key.SHIFT_RIGHT); 227 } 228 229 if ((flags & ModifierType.MOD1_MASK) == 0) { 230 TabContent.key_release (Key.ALT_LEFT); 231 TabContent.key_release (Key.ALT_RIGHT); 232 } 233 234 if ((flags & ModifierType.MOD5_MASK) == 0) { 235 TabContent.key_release (Key.LOGO_LEFT); 236 TabContent.key_release (Key.LOGO_RIGHT); 237 } 238 } 239 240 public void font_loaded () { 241 Font f = BirdFont.get_current_font (); 242 set_title (@"$(f.full_name)"); 243 } 244 245 public void set_scrollbar_size (double size) { 246 scrollbar.adjustment.page_size = size; 247 scrollbar.set_visible (size != 0); 248 } 249 250 public void set_scrollbar_position (double position) { 251 scrollbar_supress_signal = true; 252 scrollbar.adjustment.value = position * (1 - scrollbar.adjustment.page_size); 253 scrollbar_supress_signal = false; 254 } 255 256 public void dump_clipboard_content (Clipboard clipboard, SelectionData selection_data) { 257 string d; 258 return_if_fail (!is_null (selection_data)); 259 d = (string) ((!) selection_data); 260 stdout.printf (d); 261 } 262 263 public void dump_clipboard_target (Clipboard clipboard, Atom[] atoms) { 264 foreach (Atom target in atoms) { 265 print ("Target: " + target.name () + "\n"); 266 clipboard.request_contents (target, dump_clipboard_content); 267 } 268 } 269 270 public void dump_clipboard () { 271 clipboard.request_targets (dump_clipboard_target); 272 } 273 274 public string get_clipboard_data () { 275 SelectionData? selection_data; 276 Atom target; 277 string? t; 278 279 target = Atom.intern_static_string ("image/x-inkscape-svg"); 280 selection_data = clipboard.wait_for_contents (target); 281 282 if (!is_null (selection_data)) { 283 return (string) (((!) selection_data).get_data ()); 284 } 285 286 t = clipboard.wait_for_text (); 287 if (t != null) { 288 return (!) t; 289 } 290 291 return ""; 292 } 293 294 public void set_inkscape_clipboard (string inkscape_clipboard_data) { 295 if (BirdFont.mac) { 296 clipboard.set_text (inkscape_clipboard_data, -1); 297 } else { 298 TargetEntry t = { "image/x-inkscape-svg", 0, 0 }; 299 TargetEntry[] targets = { t }; 300 inkscape_clipboard = inkscape_clipboard_data; 301 302 // we can not add data to this closure because the third argument 303 // is owner and not private data. 304 clipboard.set_with_owner (targets, 305 306 // obtain clipboard data 307 (clipboard, selection_data, info, owner) => { 308 Atom type; 309 uchar[] data = (uchar[])(!)((GtkWindow*)owner)->inkscape_clipboard.to_utf8 (); 310 type = Atom.intern_static_string ("image/x-inkscape-svg"); 311 selection_data.set (type, 8, data); 312 }, 313 314 // clear clipboard data 315 (clipboard, user_data) => { 316 }, 317 318 this); 319 } 320 } 321 322 public void set_clipboard_text (string text) { 323 clipboard.set_text (text, -1); 324 } 325 326 public string get_clipboard_text () { 327 string? t; 328 329 t = clipboard.wait_for_text (); 330 if (t != null) { 331 return ((!) t).dup (); 332 } 333 334 return "".dup (); 335 } 336 337 public void set_clipboard (string svg) { 338 TargetEntry t = { "image/svg+xml", 0, 0 }; 339 TargetEntry[] targets = { t }; 340 clipboard_svg = svg; 341 clipboard.set_with_owner (targets, 342 343 // obtain clipboard data 344 (clipboard, selection_data, info, owner) => { 345 Atom type; 346 uchar[] data = (uchar[])(!)((GtkWindow*)owner)->clipboard_svg.to_utf8 (); 347 type = Atom.intern_static_string ("image/svg+xml"); 348 selection_data.set (type, 0, data); 349 }, 350 351 // clear clipboard data 352 (clipboard, user_data) => { 353 }, 354 355 this); 356 } 357 358 public void update_window_size () { 359 int w, h; 360 get_size (out w, out h); 361 362 Preferences.set ("window_width", @"$w"); 363 Preferences.set ("window_height", @"$h"); 364 } 365 366 private void set_size_and_position () { 367 int w = Preferences.get_window_width (); 368 int h = Preferences.get_window_height (); 369 set_default_size (w, h); 370 } 371 372 public void quit () { 373 Gtk.main_quit (); 374 } 375 376 public void file_chooser (string title, FileChooser fc, uint flags) { 377 string? fn = null; 378 bool folder; 379 if (BirdFont.get_arguments () .has_argument ("--windows")) { 380 folder = (flags & FileChooser.DIRECTORY) > 0; 381 MenuTab.show_file_dialog_tab (title, fc, folder); 382 } else { 383 if ((flags & FileChooser.DIRECTORY) > 0) { 384 if ((flags & FileChooser.LOAD) > 0) { 385 fn = show_file_chooser (title, FileChooserAction.SELECT_FOLDER, Stock.OPEN); 386 } else if ((flags & FileChooser.SAVE) > 0) { 387 fn = show_file_chooser (title, FileChooserAction.SELECT_FOLDER, Stock.SAVE); 388 } else { 389 warning ("Open or save is not set."); 390 } 391 } else if ((flags & FileChooser.LOAD) > 0) { 392 fn = show_file_chooser (title, FileChooserAction.OPEN, Stock.OPEN); 393 } else if ((flags & FileChooser.SAVE) > 0) { 394 fn = show_file_chooser (title, FileChooserAction.SAVE, Stock.SAVE); 395 } else { 396 warning ("Unknown type"); 397 } 398 } 399 400 fc.selected (fn); 401 } 402 403 public string? show_file_chooser (string title, FileChooserAction action, string label) { 404 string? fn = null; 405 FileChooserDialog file_chooser = new FileChooserDialog (title, this, action, Stock.CANCEL, ResponseType.CANCEL, label, ResponseType.ACCEPT); 406 Font font = BirdFont.get_current_font (); 407 int i; 408 string last_folder; 409 410 last_folder = Preferences.get ("last_folder"); 411 412 try { 413 if (last_folder == "") { 414 file_chooser.set_current_folder_file (font.get_folder ()); 415 } else { 416 file_chooser.set_current_folder_file (File.new_for_path (last_folder)); 417 } 418 } catch (GLib.Error e) { 419 stderr.printf (e.message); 420 } 421 422 if (file_chooser.run () == ResponseType.ACCEPT) { 423 GlyphCanvas.redraw (); 424 fn = file_chooser.get_filename (); 425 } 426 427 file_chooser.destroy (); 428 429 if (fn != null) { 430 i = ((!) fn).last_index_of ("/"); 431 if (i > -1) { 432 last_folder = ((!) fn).substring (0, i); 433 Preferences.set ("last_folder", @"$last_folder"); 434 } 435 } 436 437 return fn; 438 } 439 440 public void hide_text_input () { 441 text_listener = new TextListener ("", "", ""); 442 text_box.hide (); 443 text_input_is_active = false; 444 } 445 446 public void set_text_listener (TextListener listener) { 447 text_listener = listener; 448 text_input_label.set_text (" " + listener.label); 449 submit_text_button.set_label (listener.button_label); 450 text_box.show (); 451 text_entry.set_text (listener.default_text); 452 text_entry.activate.connect (() => { 453 text_listener.signal_submit (text_entry.text); 454 text_input_is_active = false; 455 }); 456 text_entry.grab_focus (); 457 text_input_is_active = true; 458 } 459 460 public bool convert_to_png (string from, string to) { 461 Pixbuf pixbuf; 462 string folder; 463 int i; 464 465 try { 466 i = to.last_index_of ("/"); 467 if (i != -1) { 468 folder = to.substring (0, i); 469 DirUtils.create (folder, 0xFFFFFF); 470 } 471 472 pixbuf = new Pixbuf.from_file (from); 473 pixbuf.save (to, "png"); 474 } catch (GLib.Error e) { 475 warning (e.message); 476 return false; 477 } 478 479 return true; 480 } 481 482 public void run_background_thread (Task t) { 483 unowned Thread<void*> bg; 484 485 MenuTab.start_background_thread (); 486 background_task = t; 487 488 try { 489 bg = Thread.create<void*> (this.background_thread, true); 490 } catch (GLib.Error e) { 491 warning (e.message); 492 } 493 } 494 495 public void* background_thread () { 496 background_task.run (); 497 MenuTab.stop_background_thread (); 498 return null; 499 } 500 501 /** Run export in a background thread. */ 502 public void export_font () { 503 unowned Thread<void*> export_thread; 504 505 MenuTab.start_background_thread (); 506 507 try { 508 export_thread = Thread.create<void*> (this.export_thread, true); 509 } catch (GLib.Error e) { 510 warning (e.message); 511 } 512 } 513 514 public void* export_thread () { 515 IdleSource idle = new IdleSource (); 516 517 ExportCallback.export_fonts (); 518 MenuTab.stop_background_thread (); 519 MenuTab.signal_file_exported (); 520 521 idle.set_callback (() => { 522 Notify.Notification export_notification; 523 export_notification = new Notify.Notification ("BirdFont", t_("Your fonts have been exported."), null); 524 export_notification.show (); 525 return false; 526 }); 527 idle.attach (null); 528 529 return null; 530 } 531 532 /** Load font in a background thread. */ 533 public void load () { 534 unowned Thread<void*> thread; 535 536 MenuTab.start_background_thread (); 537 538 try { 539 thread = Thread.create<void*> (this.loading_thread, true); 540 } catch (GLib.Error e) { 541 warning (e.message); 542 } 543 } 544 545 public void* loading_thread () { 546 BirdFont.get_current_font ().load (); 547 MenuTab.stop_background_thread (); 548 MenuTab.signal_file_loaded (); 549 return null; 550 } 551 552 /** Save font in a background thread. */ 553 public void save () { 554 unowned Thread<void*> thread; 555 556 MenuTab.start_background_thread (); 557 558 try { 559 thread = Thread.create<void*> (this.saving_thread, true); 560 } catch (GLib.Error e) { 561 warning (e.message); 562 } 563 } 564 565 public void* saving_thread () { 566 BirdFont.get_current_font ().save (); 567 MenuTab.stop_background_thread (); 568 MenuTab.signal_file_saved (); 569 return null; 570 } 571 572 public void load_background_image () { 573 unowned Thread<void*> thread; 574 575 MenuTab.start_background_thread (); 576 577 try { 578 thread = Thread.create<void*> (this.background_image_thread, true); 579 } catch (GLib.Error e) { 580 warning (e.message); 581 } 582 } 583 584 public void* background_image_thread () { 585 BackgroundTool.load_background_image (); 586 MenuTab.stop_background_thread (); 587 return null; 588 } 589 590 public bool can_export () { 591 return true; 592 } 593 594 public void set_cursor (int visible) { 595 if (visible != NativeWindow.VISIBLE) { 596 get_window ().set_cursor (new Cursor (CursorType.BLANK_CURSOR)); 597 } else { 598 get_window ().set_cursor (new Cursor (CursorType.ARROW)); 599 } 600 } 601 } 602 603 class TabbarCanvas : DrawingArea { 604 TabBar tabbar; 605 606 public TabbarCanvas (TabBar tb) { 607 tabbar = tb; 608 609 // FIXME: DELETE set_extension_events (ExtensionMode.CURSOR | EventMask.POINTER_MOTION_MASK); 610 add_events (EventMask.BUTTON_PRESS_MASK | EventMask.POINTER_MOTION_MASK | EventMask.LEAVE_NOTIFY_MASK); 611 612 motion_notify_event.connect ((t, e)=> { 613 Gtk.Allocation alloc; 614 tabbar.motion (e.x, e.y); 615 get_allocation (out alloc); 616 queue_draw_area (0, 0, alloc.width, alloc.height); 617 return true; 618 }); 619 620 button_press_event.connect ((t, e)=> { 621 Gtk.Allocation alloc; 622 get_allocation (out alloc); 623 GtkWindow.reset_modifier (e.state); 624 tabbar.select_tab_click (e.x, e.y, alloc.width, alloc.height); 625 queue_draw_area (0, 0, alloc.width, alloc.height); 626 return true; 627 }); 628 629 draw.connect ((t, e)=> { 630 Gtk.Allocation alloc; 631 Context cr; 632 StyleContext context; 633 Gdk.RGBA color; 634 635 cr = cairo_create (get_window ()); 636 get_allocation (out alloc); 637 638 context = get_style_context (); 639 context.add_class (STYLE_CLASS_BUTTON); 640 color = context.get_background_color (Gtk.StateFlags.NORMAL); 641 642 if (color.alpha > 0) { 643 tabbar.set_background_color (color.red, color.green, color.blue); 644 } 645 646 tabbar.draw (cr, alloc.width, alloc.height); 647 return true; 648 }); 649 650 tabbar.signal_tab_selected.connect ((t) => { 651 Gtk.Allocation alloc; 652 get_allocation (out alloc); 653 queue_draw_area (0, 0, alloc.width, alloc.height); 654 }); 655 656 tabbar.redraw_tab_bar.connect ((x, y, w, h) => { 657 queue_draw_area (x, y, w, h); 658 }); 659 660 set_size_request (20, 38); 661 } 662 663 } 664 665 class ToolboxCanvas : DrawingArea { 666 Toolbox tb; 667 668 public ToolboxCanvas (Toolbox toolbox) { 669 tb = toolbox; 670 671 realize.connect (() => { 672 Gtk.Allocation allocation; 673 get_allocation (out allocation); 674 Toolbox.allocation_width = allocation.width; 675 Toolbox.allocation_height = allocation.height; 676 Toolbox.redraw_tool_box (); 677 }); 678 679 tb.redraw.connect ((x, y, w, h) => { 680 queue_draw_area (x, y, w, h); 681 }); 682 683 button_press_event.connect ((se, e)=> { 684 if (e.type == EventType.2BUTTON_PRESS) { 685 tb.double_click (e.button, e.x, e.y); 686 } else { 687 tb.press (e.button, e.x, e.y); 688 } 689 return true; 690 }); 691 692 button_release_event.connect ((se, e)=> { 693 tb.release (e.button, e.x, e.y); 694 return true; 695 }); 696 697 motion_notify_event.connect ((sen, e)=> { 698 // FIXME: e.y is two pixels off in GTK under Gnome 699 tb.move (e.x, e.y); 700 return true; 701 }); 702 703 draw.connect ((t, e)=> { 704 Gtk.Allocation allocation; 705 get_allocation (out allocation); 706 707 Context cw = cairo_create(get_window()); 708 Toolbox.allocation_width = allocation.width; 709 Toolbox.allocation_height = allocation.height; 710 tb.draw (allocation.width, allocation.height, cw); 711 712 return true; 713 }); 714 715 scroll_event.connect ((t, e)=> { 716 if (e.direction == Gdk.ScrollDirection.UP) { 717 tb.scroll_up (e.x, e.y); 718 } else if (e.direction == Gdk.ScrollDirection.DOWN) { 719 tb.scroll_down (e.x, e.y); 720 } 721 return true; 722 }); 723 724 add_events (EventMask.BUTTON_PRESS_MASK | EventMask.BUTTON_RELEASE_MASK | EventMask.POINTER_MOTION_MASK | EventMask.LEAVE_NOTIFY_MASK | EventMask.SCROLL_MASK); 725 726 set_size_request (212, 100); 727 728 leave_notify_event.connect ((t, e)=> { 729 tb.reset_active_tool (); 730 return true; 731 }); 732 733 } 734 } 735 736 public class GlyphCanvasArea : DrawingArea { 737 GlyphCanvas glyph_canvas; 738 WidgetAllocation alloc = new WidgetAllocation (); 739 uint32 last_release = 0; 740 uint32 last_press = 0; 741 742 public GlyphCanvasArea (GlyphCanvas gc) { 743 int event_flags; 744 745 glyph_canvas = gc; 746 747 event_flags = EventMask.BUTTON_PRESS_MASK; 748 event_flags |= EventMask.BUTTON_RELEASE_MASK; 749 event_flags |= EventMask.POINTER_MOTION_MASK; 750 event_flags |= EventMask.LEAVE_NOTIFY_MASK; 751 event_flags |= EventMask.SCROLL_MASK; 752 753 add_events (event_flags); 754 755 bool button_down = false; 756 757 glyph_canvas.signal_redraw_area.connect ((x, y, w, h) => { 758 queue_draw_area ((int)x, (int)y, (int)w, (int)h); 759 }); 760 761 draw.connect ((t, e)=> { 762 Gtk.Allocation allocation; 763 get_allocation (out allocation); 764 765 alloc = new WidgetAllocation (); 766 767 alloc.width = allocation.width; 768 alloc.height = allocation.height; 769 alloc.x = allocation.x; 770 alloc.y = allocation.y; 771 772 Context cw = cairo_create (get_window()); 773 774 Surface s = new Surface.similar (cw.get_target (), Cairo.Content.COLOR_ALPHA, alloc.width, alloc.height); 775 Context c = new Context (s); 776 777 TabContent.draw (alloc, c); 778 779 cw.save (); 780 cw.set_source_surface (c.get_target (), 0, 0); 781 cw.paint (); 782 cw.restore (); 783 784 return true; 785 }); 786 787 button_press_event.connect ((t, e)=> { 788 GtkWindow.reset_modifier (e.state); 789 790 if (button_down) { 791 warning (@"Button already is down. $(e.button)"); 792 } 793 794 if (e.time < last_press) { 795 warning ("Discarding event."); 796 return true; 797 } 798 799 last_press = e.time; 800 801 if (e.type == EventType.BUTTON_PRESS) { 802 TabContent.button_press (e.button, e.x, e.y); 803 button_down = true; 804 } else if (e.type == EventType.2BUTTON_PRESS) { 805 TabContent.double_click (e.button, e.x, e.y); 806 } 807 808 return true; 809 }); 810 811 button_release_event.connect ((t, e)=> { 812 if (!button_down) { 813 warning (@"Button is not down $(e.button)"); 814 } 815 816 if (e.time < last_release) { 817 warning ("Discarding event."); 818 return true; 819 } 820 821 if (e.type == EventType.BUTTON_RELEASE) { 822 TabContent.button_release ((int) e.button, e.x, e.y); 823 last_release = e.time; 824 button_down = false; 825 } 826 827 return true; 828 }); 829 830 motion_notify_event.connect ((t, e)=> { 831 TabContent.motion_notify (e.x, e.y); 832 return true; 833 }); 834 835 scroll_event.connect ((t, e)=> { 836 if (e.direction == Gdk.ScrollDirection.UP) { 837 TabContent.scroll_wheel_up (e.x, e.y); 838 } else if (e.direction == Gdk.ScrollDirection.DOWN) { 839 TabContent.scroll_wheel_down (e.x, e.y) ; 840 } 841 842 TabContent.button_release (2, e.x, e.y); 843 return true; 844 }); 845 846 can_focus = true; 847 } 848 } 849 850 public class DescriptionForm : GLib.Object { 851 852 public ScrolledWindow canvas; 853 public Box box; 854 855 Entry postscript_name; 856 Entry font_name; 857 Entry style; 858 CheckButton bold; 859 CheckButton italic; 860 Entry weight; 861 Entry full_name; 862 Entry id; 863 Entry version; 864 865 TextView description; 866 TextView copyright; 867 868 public DescriptionForm () { 869 box = new Box (Orientation.VERTICAL, 6); 870 canvas = new ScrolledWindow (null, null); 871 canvas.set_policy (Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC); 872 873 postscript_name = new Entry (); 874 add_entry (postscript_name, t_("PostScript Name")); 875 postscript_name.changed.connect (() => { 876 Font f = BirdFont.get_current_font (); 877 f.postscript_name = postscript_name.text; 878 }); 879 880 font_name = new Entry (); 881 add_entry (font_name, t_("Name")); 882 font_name.changed.connect (() => { 883 Font f = BirdFont.get_current_font (); 884 f.name = font_name.text; 885 }); 886 887 style = new Entry (); 888 add_entry (style, t_("Style")); 889 style.changed.connect (() => { 890 Font f = BirdFont.get_current_font (); 891 f.subfamily = style.text; 892 }); 893 894 bold = new CheckButton.with_label (t_("Bold")); 895 bold.toggled.connect (() => { 896 Font f = BirdFont.get_current_font (); 897 f.bold = bold.active;; 898 }); 899 box.pack_start (bold, false, false, 0); 900 901 italic = new CheckButton.with_label (t_("Italic")); 902 italic.toggled.connect (() => { 903 Font f = BirdFont.get_current_font (); 904 f.italic = italic.active;; 905 }); 906 box.pack_start (italic, false, false, 0); 907 908 weight = new Entry (); 909 add_entry (weight, t_("Weight")); 910 weight.changed.connect (() => { 911 Font f = BirdFont.get_current_font (); 912 f.set_weight (weight.text); 913 }); 914 915 full_name = new Entry (); 916 add_entry (full_name, t_("Full name (name and style)")); 917 full_name.changed.connect (() => { 918 Font f = BirdFont.get_current_font (); 919 f.full_name = full_name.text; 920 }); 921 922 id = new Entry (); 923 add_entry (id, t_("Unique identifier")); 924 id.changed.connect (() => { 925 Font f = BirdFont.get_current_font (); 926 f.unique_identifier = id.text; 927 }); 928 929 version = new Entry (); 930 add_entry (version, t_("Version")); 931 version.changed.connect (() => { 932 Font f = BirdFont.get_current_font (); 933 f.version = version.text; 934 }); 935 936 description = new TextView (); 937 add_textview (description, t_("Description")); 938 description.get_buffer ().changed.connect (() => { 939 Font f = BirdFont.get_current_font (); 940 f.description = description.get_buffer ().text; 941 }); 942 description.set_wrap_mode (Gtk.WrapMode.WORD); 943 944 copyright = new TextView (); 945 add_textview (copyright, t_("Copyright")); 946 copyright.get_buffer ().changed.connect (() => { 947 Font f = BirdFont.get_current_font (); 948 f.copyright = copyright.get_buffer ().text; 949 }); 950 copyright.set_wrap_mode (Gtk.WrapMode.WORD); 951 952 update_fields (); 953 954 canvas.add_with_viewport (box); 955 canvas.show_all (); 956 } 957 958 public void update_fields () { 959 Font font = BirdFont.get_current_font (); 960 961 return_if_fail (font.postscript_name.validate ()); 962 return_if_fail (font.name.validate ()); 963 return_if_fail (font.subfamily.validate ()); 964 return_if_fail (font.full_name.validate ()); 965 return_if_fail (font.unique_identifier.validate ()); 966 return_if_fail (font.version.validate ()); 967 return_if_fail (font.description.validate ()); 968 return_if_fail (font.copyright.validate ()); 969 970 postscript_name.set_text (font.postscript_name); 971 font_name.set_text (font.name); 972 style.set_text (font.subfamily); 973 bold.active = font.bold; 974 italic.active = font.italic; 975 weight.set_text (font.get_weight ()); 976 full_name.set_text (font.full_name); 977 id.set_text (font.unique_identifier); 978 version.set_text (font.version); 979 980 description.get_buffer ().set_text (font.description.dup ()); 981 copyright.get_buffer ().set_text (font.copyright.dup ()); 982 983 } 984 985 void add_entry (Entry e, string label) { 986 Box vb; 987 Box hb; 988 Label l; 989 Box margin; 990 991 margin = new Box (Orientation.HORIZONTAL, 6); 992 l = new Label (label); 993 vb = new Box (Orientation.VERTICAL, 2); 994 hb = new Box (Orientation.HORIZONTAL, 2); 995 hb.pack_start (l, false, false, 0); 996 vb.pack_start (hb, true, true, 5); 997 vb.pack_start (e, true, true, 0); 998 margin.pack_start (vb, true, true, 5); 999 box.pack_start (margin, false, false, 5); 1000 } 1001 1002 void add_textview (TextView t, string label) { 1003 Box vb; 1004 Box hb; 1005 Label l; 1006 Box margin; 1007 1008 margin = new Box (Orientation.HORIZONTAL, 6); 1009 l = new Label (label); 1010 vb = new Box (Orientation.VERTICAL, 2); 1011 hb = new Box (Orientation.HORIZONTAL, 2); 1012 hb.pack_start (l, false, false, 0); 1013 vb.pack_start (hb, true, true, 5); 1014 vb.pack_start (t, true, true, 0); 1015 margin.pack_start (vb, true, true, 5); 1016 box.pack_start (margin, false, false, 5); 1017 } 1018 } 1019 1020 } 1021