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
Circle boundaries heads/master
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 Tab t = MainWindow.get_tab_bar ().get_selected_tab (); 117 string tab_name = t.get_display ().get_name (); 118 119 if (tab_name == "Preview") { 120 MenuTab.select_overview (); 121 } 122 123 MessageDialog md = new MessageDialog (text); 124 show_dialog (md); 125 } 126 127 public static void hide_dialog () { 128 dialog = new Dialog (); 129 dialog.visible = false; 130 GlyphCanvas.redraw (); 131 } 132 133 public static void set_menu (AbstractMenu m) { 134 menu = m; 135 } 136 137 public static AbstractMenu get_menu () { 138 return menu; 139 } 140 141 /** Set the number of pixels per millimeter for the current screen. */ 142 public static void set_units_per_pixel (double u) { 143 MainWindow.units = u; 144 } 145 146 public static void init () { 147 singleton = new MainWindow (); 148 } 149 150 public static RecentFiles get_recent_files_tab () { 151 return file_tab; 152 } 153 154 public static void open_recent_files_tab () { 155 // FIXME: do not idle 156 IdleSource idle = new IdleSource (); 157 tabs.add_unique_tab (file_tab); 158 idle.set_callback (() => { 159 tabs.select_tab_name ("Files"); 160 return false; 161 }); 162 idle.attach (null); 163 } 164 165 public static void select_all_paths () { 166 Tool t = tools.get_current_tool (); 167 168 if (! (t is MoveTool || t is ResizeTool)) { 169 get_toolbox ().select_tool (DrawingTools.move_tool); 170 } 171 172 IdleSource idle = new IdleSource (); 173 idle.set_callback (() => { 174 DrawingTools.move_tool.select_all_paths (); 175 return false; 176 }); 177 idle.attach (null); 178 } 179 180 public static DrawingTools get_drawing_tools () { 181 return Toolbox.drawing_tools; 182 } 183 184 public void set_native (NativeWindow nw) { 185 native_window = nw; 186 } 187 188 public static FontDisplay get_current_display () { 189 return get_glyph_canvas ().get_current_display (); 190 } 191 192 public static GlyphCanvas get_glyph_canvas () { 193 return glyph_canvas; 194 } 195 196 public static GlyphCollection get_current_glyph_collection () { 197 if (unlikely (is_null (BirdFont.current_glyph_collection))) { 198 GlyphCollection gc; 199 200 warning ("No default glyph have been set yet.\n"); 201 gc = new GlyphCollection.with_glyph ('\0', ""); 202 203 return gc; 204 } 205 206 return BirdFont.current_glyph_collection; 207 } 208 209 public static Glyph get_current_glyph () { 210 return get_current_glyph_collection ().get_current (); 211 } 212 213 public static Toolbox get_toolbox () { 214 return tools; 215 } 216 217 public static Tool get_tool (string n) { 218 return tools.get_tool (n); 219 } 220 221 public static TabBar get_tab_bar () { 222 return tabs; 223 } 224 225 public static Tab get_current_tab () { 226 return tabs.get_selected_tab (); 227 } 228 229 public static bool select_tab (Tab t) { 230 return tabs.selected_open_tab (t); 231 } 232 233 public static SpacingClassTab get_spacing_class_tab () { 234 return spacing_class_tab; 235 } 236 237 public static void update_glyph_sequence () { 238 TextListener listener = new TextListener (t_("Glyph sequence"), Preferences.get ("glyph_sequence"), t_("Close")); 239 listener.signal_text_input.connect ((text) => { 240 Preferences.set ("glyph_sequence", text); 241 GlyphCanvas.redraw (); 242 }); 243 listener.signal_submit.connect (() => { 244 TabContent.hide_text_input (); 245 }); 246 TabContent.show_text_input (listener); 247 } 248 249 public static MainWindow get_singleton () { 250 return singleton; 251 } 252 253 public static void file_chooser (string title, FileChooser fc, uint flags) { 254 MainWindow.native_window.file_chooser (title, fc, flags); 255 } 256 257 public static void hide_scrollbar () { 258 if (!is_null (MainWindow.native_window)) { 259 MainWindow.scrollbar.set_size (-1); 260 } 261 } 262 263 public static void show_scrollbar () { 264 if (!is_null (MainWindow.native_window)) { 265 MainWindow.scrollbar.set_size (scrollbar_size); 266 } 267 } 268 269 public static void set_scrollbar_size (double size) { 270 if (!is_null (MainWindow.native_window)) { 271 scrollbar_size = size; 272 MainWindow.scrollbar.set_size (size); 273 } 274 } 275 276 public static void set_scrollbar_position (double position) { 277 if (!is_null (MainWindow.native_window)) { 278 MainWindow.scrollbar.set_position (position); 279 } 280 } 281 282 public static void close_all_tabs () { 283 get_tab_bar ().close_all_tabs (); 284 } 285 286 public static string translate (string s) { 287 return t_(s); 288 } 289 290 public static KerningDisplay get_kerning_display () { 291 return kerning_display; 292 } 293 294 public static LigatureList get_ligature_display () { 295 return ligature_display; 296 } 297 298 public static void next_tab () { 299 TabBar tb = MainWindow.get_tab_bar (); 300 int n = tb.get_selected () + 1; 301 302 if (!(0 <= n < tb.get_length ())) { 303 return; 304 } 305 306 tb.select_tab (n); 307 } 308 309 public static void previous_tab () { 310 TabBar tb = MainWindow.get_tab_bar (); 311 int n = tb.get_selected () - 1; 312 313 if (!(0 <= n < tb.get_length ())) { 314 return; 315 } 316 317 tb.select_tab (n); 318 } 319 320 public static void close_tab () { 321 TabBar tb = MainWindow.get_tab_bar (); 322 int n = tb.get_selected (); 323 324 if (!(0 <= n < tb.get_length ())) { 325 return; 326 } 327 328 tb.close_tab (n); 329 } 330 331 public static void set_toolbox (Toolbox tb) { 332 tools = tb; 333 } 334 335 public static OverView get_overview () { 336 foreach (Tab t in tabs.tabs) { 337 if (t.get_display () is OverView) { 338 return (OverView) t.get_display (); 339 } 340 } 341 342 return overview; 343 } 344 } 345 346 } 347