The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

MainWindow.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/MainWindow.vala.
Merge ../birdfont-2.x
1 /* 2 Copyright (C) 2012 2014 2016 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 MainWindow : GLib.Object { 18 19 public static Toolbox tools; 20 public static GlyphCanvas glyph_canvas; 21 public static MainWindow singleton; 22 public static MenuTab menu_tab; 23 public static RecentFiles file_tab; 24 public static OverView overview; 25 public static TabBar tabs; 26 public static NativeWindow native_window; 27 public static KerningDisplay kerning_display; 28 public static CharDatabase character_database; 29 public static LigatureList ligature_display; 30 public static SpacingClassTab spacing_class_tab; 31 public static AbstractMenu menu; 32 public static Dialog dialog; 33 public static SpacingTab spacing_tab; 34 public static Task blocking_background_task; 35 public static Scrollbar scrollbar; 36 37 /** Number of pixels per mm */ 38 public static double units = 1; 39 40 private static double scrollbar_size = -1; 41 42 public MainWindow () { 43 singleton = this; 44 45 glyph_canvas = new GlyphCanvas (); 46 tabs = new TabBar (); 47 tools = new Toolbox (glyph_canvas, tabs); 48 menu_tab = new MenuTab (); 49 file_tab = new RecentFiles (); 50 overview = new OverView(); 51 kerning_display = new KerningDisplay (); 52 character_database = new CharDatabase (); 53 ligature_display = new LigatureList (); 54 spacing_class_tab = new SpacingClassTab (); 55 menu = new Menu (); 56 dialog = new Dialog (); 57 spacing_tab = new SpacingTab (); 58 blocking_background_task = new Task (null); 59 scrollbar = new Scrollbar (); 60 61 tools.select_tool (DrawingTools.bezier_tool); 62 } 63 64 public static bool has_scrollbar () { 65 return scrollbar.is_visible (); 66 } 67 68 public static void abort_task () { 69 blocking_background_task.cancel (); 70 } 71 72 public static void run_blocking_task (Task task) { 73 blocking_background_task = task; 74 native_window.run_background_thread (task); 75 } 76 77 public static SpacingTab get_spacing_tab () { 78 return spacing_tab; 79 } 80 81 public static Dialog get_dialog () { 82 return dialog; 83 } 84 85 public static void show_dialog (Dialog d) 86 requires (!is_null(MainWindow.get_tab_bar ())) { 87 Tab t = MainWindow.get_tab_bar ().get_selected_tab (); 88 string tab_name = t.get_display ().get_name (); 89 90 if (tab_name == "Preview") { 91 MenuTab.select_overview (); 92 } 93 94 dialog = d; 95 dialog.visible = true; 96 GlyphCanvas.redraw (); 97 98 set_cursor (NativeWindow.VISIBLE); 99 } 100 101 public static void show_license_dialog () { 102 show_dialog (new LicenseDialog ()); 103 } 104 105 public static void set_cursor (int flags) { 106 if (BirdFont.has_argument ("--test")) { 107 if (dialog.visible) { 108 native_window.set_cursor (NativeWindow.VISIBLE); 109 } else { 110 native_window.set_cursor (flags); 111 } 112 } 113 } 114 115 public static void show_message (string text) { 116 MessageDialog md = new MessageDialog (text); 117 show_dialog (md); 118 } 119 120 public static void hide_dialog () { 121 dialog = new Dialog (); 122 dialog.visible = false; 123 GlyphCanvas.redraw (); 124 } 125 126 public static void set_menu (AbstractMenu m) { 127 menu = m; 128 } 129 130 public static AbstractMenu get_menu () { 131 return menu; 132 } 133 134 /** Set the number of pixels per millimeter for the current screen. */ 135 public static void set_units_per_pixel (double u) { 136 MainWindow.units = u; 137 } 138 139 public static void init () { 140 singleton = new MainWindow (); 141 } 142 143 public static RecentFiles get_recent_files_tab () { 144 return file_tab; 145 } 146 147 public static void open_recent_files_tab () { 148 // FIXME: do not idle 149 IdleSource idle = new IdleSource (); 150 tabs.add_unique_tab (file_tab); 151 idle.set_callback (() => { 152 tabs.select_tab_name ("Files"); 153 return false; 154 }); 155 idle.attach (null); 156 } 157 158 public static void select_all_paths () { 159 Tool t = tools.get_current_tool (); 160 161 if (! (t is MoveTool || t is ResizeTool)) { 162 get_toolbox ().select_tool (DrawingTools.move_tool); 163 } 164 165 IdleSource idle = new IdleSource (); 166 idle.set_callback (() => { 167 DrawingTools.move_tool.select_all_paths (); 168 return false; 169 }); 170 idle.attach (null); 171 } 172 173 public static DrawingTools get_drawing_tools () { 174 return Toolbox.drawing_tools; 175 } 176 177 public void set_native (NativeWindow nw) { 178 native_window = nw; 179 } 180 181 public static FontDisplay get_current_display () { 182 return get_glyph_canvas ().get_current_display (); 183 } 184 185 public static GlyphCanvas get_glyph_canvas () { 186 return glyph_canvas; 187 } 188 189 public static GlyphCollection get_current_glyph_collection () { 190 if (unlikely (is_null (BirdFont.current_glyph_collection))) { 191 GlyphCollection gc; 192 193 warning ("No default glyph have been set yet.\n"); 194 gc = new GlyphCollection.with_glyph ('\0', ""); 195 196 return gc; 197 } 198 199 return BirdFont.current_glyph_collection; 200 } 201 202 public static Glyph get_current_glyph () { 203 return get_current_glyph_collection ().get_current (); 204 } 205 206 public static Toolbox get_toolbox () { 207 return tools; 208 } 209 210 public static Tool get_tool (string n) { 211 return tools.get_tool (n); 212 } 213 214 public static TabBar get_tab_bar () { 215 return tabs; 216 } 217 218 public static Tab get_current_tab () { 219 return tabs.get_selected_tab (); 220 } 221 222 public static bool select_tab (Tab t) { 223 return tabs.selected_open_tab (t); 224 } 225 226 public static SpacingClassTab get_spacing_class_tab () { 227 return spacing_class_tab; 228 } 229 230 public static void update_glyph_sequence () { 231 TextListener listener = new TextListener (t_("Glyph sequence"), Preferences.get ("glyph_sequence"), t_("Close")); 232 listener.signal_text_input.connect ((text) => { 233 Preferences.set ("glyph_sequence", text); 234 GlyphCanvas.redraw (); 235 }); 236 listener.signal_submit.connect (() => { 237 TabContent.hide_text_input (); 238 }); 239 TabContent.show_text_input (listener); 240 } 241 242 public static MainWindow get_singleton () { 243 return singleton; 244 } 245 246 public static void file_chooser (string title, FileChooser fc, uint flags) { 247 MainWindow.native_window.file_chooser (title, fc, flags); 248 } 249 250 public static void hide_scrollbar () { 251 if (!is_null (MainWindow.native_window)) { 252 MainWindow.scrollbar.set_size (-1); 253 } 254 } 255 256 public static void show_scrollbar () { 257 if (!is_null (MainWindow.native_window)) { 258 MainWindow.scrollbar.set_size (scrollbar_size); 259 } 260 } 261 262 public static void set_scrollbar_size (double size) { 263 if (!is_null (MainWindow.native_window)) { 264 scrollbar_size = size; 265 MainWindow.scrollbar.set_size (size); 266 } 267 } 268 269 public static void set_scrollbar_position (double position) { 270 if (!is_null (MainWindow.native_window)) { 271 MainWindow.scrollbar.set_position (position); 272 } 273 } 274 275 public static void close_all_tabs () { 276 get_tab_bar ().close_all_tabs (); 277 } 278 279 public static string translate (string s) { 280 return t_(s); 281 } 282 283 public static KerningDisplay get_kerning_display () { 284 return kerning_display; 285 } 286 287 public static LigatureList get_ligature_display () { 288 return ligature_display; 289 } 290 291 public static void next_tab () { 292 TabBar tb = MainWindow.get_tab_bar (); 293 int n = tb.get_selected () + 1; 294 295 if (!(0 <= n < tb.get_length ())) { 296 return; 297 } 298 299 tb.select_tab (n); 300 } 301 302 public static void previous_tab () { 303 TabBar tb = MainWindow.get_tab_bar (); 304 int n = tb.get_selected () - 1; 305 306 if (!(0 <= n < tb.get_length ())) { 307 return; 308 } 309 310 tb.select_tab (n); 311 } 312 313 public static void close_tab () { 314 TabBar tb = MainWindow.get_tab_bar (); 315 int n = tb.get_selected (); 316 317 if (!(0 <= n < tb.get_length ())) { 318 return; 319 } 320 321 tb.close_tab (n); 322 } 323 324 public static void set_toolbox (Toolbox tb) { 325 tools = tb; 326 } 327 328 public static OverView get_overview () { 329 foreach (Tab t in tabs.tabs) { 330 if (t.get_display () is OverView) { 331 return (OverView) t.get_display (); 332 } 333 } 334 335 return overview; 336 } 337 } 338 339 } 340