.
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 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 over_view;
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 Menu menu;
32 public static Dialog dialog;
33 public static SpacingTab spacing_tab;
34
35 /** Number of pixels per mm */
36 public static double units = 1;
37
38 public MainWindow () {
39 singleton = this;
40
41 glyph_canvas = new GlyphCanvas ();
42 tabs = new TabBar ();
43 tools = new Toolbox (glyph_canvas, tabs);
44 menu_tab = new MenuTab ();
45 file_tab = new RecentFiles ();
46 over_view = new OverView();
47 kerning_display = new KerningDisplay ();
48 character_database = new CharDatabase ();
49 ligature_display = new LigatureList ();
50 spacing_class_tab = new SpacingClassTab ();
51 menu = new Menu ();
52 dialog = new Dialog ();
53 spacing_tab = new SpacingTab ();
54
55 tools.select_tool (DrawingTools.foresight_tool);
56 }
57
58 public static SpacingTab get_spacing_tab () {
59 return spacing_tab;
60 }
61
62 public static Dialog get_dialog () {
63 return dialog;
64 }
65
66 public static void show_dialog (Dialog d)
67 requires (!is_null(MainWindow.get_tab_bar ())) {
68 Tab t = MainWindow.get_tab_bar ().get_selected_tab ();
69 string tab_name = t.get_display ().get_name ();
70
71 if (tab_name == "Preview") {
72 MenuTab.select_overview ();
73 }
74
75 dialog = d;
76 dialog.visible = true;
77 GlyphCanvas.redraw ();
78 }
79
80 public static void show_message (string text) {
81 show_dialog (new MessageDialog (text));
82 }
83
84 public static void hide_dialog () {
85 dialog = new Dialog ();
86 dialog.visible = false;
87 GlyphCanvas.redraw ();
88 }
89
90 public static Menu get_menu () {
91 return menu;
92 }
93
94 /** Set the number of picels per millimeter for the current screen. */
95 public static void set_units_per_pixel (double u) {
96 MainWindow.units = u;
97 }
98
99 public static void init () {
100 singleton = new MainWindow ();
101 }
102
103 public static RecentFiles get_recent_files_tab () {
104 return file_tab;
105 }
106
107 public static void open_recent_files_tab () {
108 // FIXME: do not idle
109 IdleSource idle = new IdleSource ();
110 tabs.add_unique_tab (file_tab);
111 idle.set_callback (() => {
112 tabs.select_tab_name ("Files");
113 return false;
114 });
115 idle.attach (null);
116 }
117
118 public static void select_all_paths () {
119 Tool t = tools.get_current_tool ();
120
121 if (! (t is MoveTool || t is ResizeTool)) {
122 Toolbox.select_tool_by_name ("move");
123 }
124
125 DrawingTools.move_tool.select_all_paths ();
126 }
127
128 public static DrawingTools get_drawing_tools () {
129 return Toolbox.drawing_tools;
130 }
131
132 public void set_native (NativeWindow nw) {
133 native_window = nw;
134 }
135
136 public static FontDisplay get_current_display () {
137 return get_glyph_canvas ().get_current_display ();
138 }
139
140 public static GlyphCanvas get_glyph_canvas () {
141 return glyph_canvas;
142 }
143
144 public static GlyphCollection get_current_glyph_collection () {
145 if (unlikely (is_null (BirdFont.current_glyph_collection))) {
146 GlyphCollection gc;
147
148 warning ("No default glyph have been set yet.\n");
149 gc = new GlyphCollection ('\0', "");
150 gc.add_glyph (new Glyph ("", '\0'));
151
152 return gc;
153 }
154
155 return BirdFont.current_glyph_collection;
156 }
157
158 public static Glyph get_current_glyph () {
159 return get_current_glyph_collection ().get_current ();
160 }
161
162 public static Toolbox get_toolbox () {
163 return tools;
164 }
165
166 public static Tool get_tool (string n) {
167 return tools.get_tool (n);
168 }
169
170 public static TabBar get_tab_bar () {
171 return tabs;
172 }
173
174 public static Tab get_current_tab () {
175 return tabs.get_selected_tab ();
176 }
177
178 public static bool select_tab (Tab t) {
179 return tabs.selected_open_tab (t);
180 }
181
182 public static OverView get_overview () {
183 OverView over_view;
184
185 foreach (var t in tabs.tabs) {
186 if (t.get_display () is OverView) {
187 return (OverView) t.get_display ();
188 }
189 }
190
191 over_view = new OverView();
192 tabs.add_unique_tab (over_view);
193
194 return over_view;
195 }
196
197 public static SpacingClassTab get_spacing_class_tab () {
198 return spacing_class_tab;
199 }
200
201 public static void update_glyph_sequence () {
202 TextListener listener = new TextListener (t_("Glyph sequence"), Preferences.get ("glyph_sequence"), t_("Close"));
203 listener.signal_text_input.connect ((text) => {
204 Preferences.set ("glyph_sequence", text);
205 GlyphCanvas.redraw ();
206 });
207 listener.signal_submit.connect (() => {
208 TabContent.hide_text_input ();
209 });
210 TabContent.show_text_input (listener);
211 }
212
213 public static MainWindow get_singleton () {
214 return singleton;
215 }
216
217 public static void file_chooser (string title, FileChooser fc, uint flags) {
218 MainWindow.native_window.file_chooser (title, fc, flags);
219 }
220
221 public static void set_scrollbar_size (double size) {
222 if (!is_null (MainWindow.native_window)) {
223 MainWindow.native_window.set_scrollbar_size (size);
224 }
225 }
226
227 public static void set_scrollbar_position (double position) {
228 if (!is_null (MainWindow.native_window)) {
229 MainWindow.native_window.set_scrollbar_position (position);
230 }
231 }
232
233 /** Reaload all paths and help lines from disk. */
234 public static void clear_glyph_cache () {
235 Glyph g;
236 foreach (Tab t in get_tab_bar ().tabs) {
237 if (t.get_display () is Glyph) {
238 g = (Glyph) t.get_display ();
239 g.add_help_lines ();
240 }
241 }
242
243 GlyphCanvas.redraw ();
244 }
245
246 public static void close_all_tabs () {
247 get_tab_bar ().close_all_tabs ();
248 }
249
250 public static string translate (string s) {
251 return t_(s);
252 }
253
254 public static KerningDisplay get_kerning_display () {
255 return kerning_display;
256 }
257
258 public static LigatureList get_ligature_display () {
259 return ligature_display;
260 }
261
262 public static void next_tab () {
263 TabBar tb = MainWindow.get_tab_bar ();
264 int n = tb.get_selected () + 1;
265
266 if (!(0 <= n < tb.get_length ())) {
267 return;
268 }
269
270 tb.select_tab (n);
271 }
272
273 public static void previous_tab () {
274 TabBar tb = MainWindow.get_tab_bar ();
275 int n = tb.get_selected () - 1;
276
277 if (!(0 <= n < tb.get_length ())) {
278 return;
279 }
280
281 tb.select_tab (n);
282 }
283
284 public static void close_tab () {
285 TabBar tb = MainWindow.get_tab_bar ();
286 int n = tb.get_selected ();
287
288 if (!(0 <= n < tb.get_length ())) {
289 return;
290 }
291
292 tb.close_tab (n);
293 }
294 }
295
296 }
297