1 /*
2 Copyright (C) 2015 2019 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 using Cairo;
16 using Math;
17
18 namespace BirdFont {
19
20 /** A list of recently edited fonts. */
21 public class RecentFiles : Table {
22 Gee.ArrayList<Row> rows = new Gee.ArrayList<Row> ();
23
24 const int NEW_FONT = -5;
25 const int CURRENT_FONT = -4;
26 const int RECENT_FONT = -3;
27
28 public RecentFiles () {
29 }
30
31 public override Gee.ArrayList<Row> get_rows () {
32 return rows;
33 }
34
35 public override void selected_row (Row row, int column, bool delete_button) {
36 if (row.get_index () == NEW_FONT) {
37 MenuTab.new_file ();
38 MenuTab.select_overview ();
39 } else if (row.get_index () == RECENT_FONT) {
40 return_if_fail (row.get_row_data () is Font);
41 Font f = (Font) row.get_row_data ();
42 MainWindow.scrollbar.set_size (0);
43 GlyphCanvas.redraw ();
44 load_font (f.get_path ());
45 }
46
47 GlyphCanvas.redraw ();
48 }
49
50 public override void update_rows () {
51 Row row;
52 Gee.ArrayList<Font> recent_fonts = get_recent_font_files ();
53 Font current_font = BirdFont.get_current_font ();
54
55 rows.clear ();
56
57 if (recent_fonts.size == 0) {
58 row = new Row.headline (t_("No fonts created yet"));
59 rows.add (row);
60
61 row = new Row.columns_1 (t_("Create a New Font"), NEW_FONT, false);
62 rows.add (row);
63 }
64
65 if (current_font.font_file != null) {
66 row = new Row.headline (current_font.get_file_name ());
67 rows.add (row);
68
69 row = new Row.columns_1 (t_("Folder") + ": " + (!) current_font.get_folder ().get_path (), CURRENT_FONT, false);
70 rows.add (row);
71
72 row = new Row.columns_1 (t_("Glyphs") + @": $(current_font.length ())", CURRENT_FONT, false);
73 rows.add (row);
74 }
75
76 if (recent_fonts.size > 0) {
77 row = new Row.headline (t_("Recent Files"));
78 rows.add (row);
79 }
80
81 foreach (Font font in recent_fonts) {
82 row = new Row.columns_1 (font.get_file_name (), RECENT_FONT, false);
83 row.set_row_data (font);
84 rows.add (row);
85 }
86
87 GlyphCanvas.redraw ();
88 }
89
90 public override string get_label () {
91 return t_("Files");
92 }
93
94 public override string get_name () {
95 return "Files";
96 }
97
98 public Gee.ArrayList<Font> get_recent_font_files () {
99 File file;
100 Font font;
101 bool unique;
102 Gee.ArrayList<Font> fonts = new Gee.ArrayList<Font> ();
103
104 foreach (string f in Preferences.get_recent_files ()) {
105 if (f == "") {
106 continue;
107 }
108
109 file = File.new_for_path (f);
110
111 font = new Font ();
112 font.set_font_file (f);
113
114 unique = true;
115 foreach (Font recent_font in fonts) {
116 if (recent_font.get_path () == f) {
117 unique = false;
118 }
119 }
120
121 if (unique && file.query_exists ()) {
122 fonts.insert (0, font);
123 }
124 }
125
126 return fonts;
127 }
128
129 public override void draw (WidgetAllocation allocation, Context cr) {
130 MainWindow.get_overview ().allocation = allocation;
131 base.draw (allocation, cr);
132 }
133
134 public static void load_font (string fn) {
135 Font font;
136 SaveDialogListener dialog = new SaveDialogListener ();
137
138 if (MenuTab.has_suppress_event ()) {
139 return;
140 }
141
142 font = BirdFont.get_current_font ();
143
144 MenuTab.load_callback = new LoadCallback ();
145 MenuTab.load_callback.file_loaded.connect (() => {
146 Font f;
147
148 if (MenuTab.has_suppress_event ()) {
149 warning ("Load font event suppressed.");
150 return;
151 }
152
153 f = BirdFont.get_current_font ();
154
155 MainWindow.get_drawing_tools ().remove_all_grid_buttons ();
156 foreach (string v in f.grid_width) {
157 MainWindow.get_drawing_tools ().parse_grid (v);
158 }
159
160 DrawingTools.background_scale.set_value (f.background_scale);
161 KerningTools.update_kerning_classes ();
162 MenuTab.apply_font_setting (f);
163
164 MenuTab.show_default_characters ();
165 MainWindow.get_overview ().selected_canvas ();
166 });
167
168 dialog.signal_discard.connect (() => {
169 Font f;
170
171 if (MenuTab.has_suppress_event ()) {
172 return;
173 }
174
175 f = BirdFont.new_font ();
176
177 MainWindow.close_all_tabs ();
178 MenuTab.clear_font_settings ();
179
180 f.set_file (fn);
181
182 if (!fn.has_suffix (".bf_backup")) {
183 Preferences.add_recent_files (fn);
184 }
185
186 MainWindow.native_window.load (); // background thread
187 });
188
189 dialog.signal_save.connect (() => {
190 if (MenuTab.has_suppress_event ()) {
191 warn_if_test ("Event suppressed.");
192 return;
193 }
194
195 MenuTab.set_save_callback (new SaveCallback ());
196 MenuTab.save_callback.file_saved.connect (() => {
197 dialog.signal_discard ();
198 });
199 MenuTab.save_callback.save (); // background thread
200 });
201
202 dialog.signal_cancel.connect (() => {
203 MainWindow.hide_dialog ();
204 });
205
206 if (!font.is_modified ()) {
207 dialog.signal_discard ();
208 } else {
209 MainWindow.show_dialog (new SaveDialog (dialog));
210 }
211 }
212 }
213
214 }
215