The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

VersionList.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/VersionList.vala.
Remove debug info
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 using Cairo; 16 using Math; 17 18 namespace BirdFont { 19 20 public class VersionList : DropMenu { 21 public int current_version_id = -1; 22 unowned GlyphCollection glyph_collection; 23 24 public Gee.ArrayList<Glyph> glyphs; 25 26 public VersionList (Glyph? g = null, GlyphCollection glyph_collection) { 27 base (); 28 29 print (@"VersionList: glyph_collection: $(glyph_collection.ref_count)\n"); 30 31 this.glyph_collection = glyph_collection; 32 glyphs = new Gee.ArrayList<Glyph> (); 33 set_direction (MenuDirection.POP_UP); 34 35 MenuAction ma = add_item (t_("New version")); 36 ma.has_delete_button = false; 37 ma.action = (self) => { 38 return_if_fail (self.parent != null); 39 return_if_fail (glyphs.size > 0); 40 41 BirdFont.get_current_font ().touch (); 42 43 add_new_version (); 44 current_version_id = glyphs.get (glyphs.size - 1).version_id; 45 }; 46 47 // delete one version 48 signal_delete_item.connect ((index) => { 49 delete_item (index); 50 }); 51 52 if (g != null) { 53 add_glyph ((!) g); 54 } 55 56 } 57 58 private void delete_item (int index) { 59 int current_version; 60 Font font = BirdFont.get_current_font (); 61 OverView over_view = MainWindow.get_overview (); 62 63 font.touch (); 64 65 index--; // first item is the add new action 66 67 // delete the entire glyph if the last remaining version is removed 68 if (glyphs.size == 1) { 69 over_view.store_undo_state (glyph_collection.copy ()); 70 font.delete_glyph (glyph_collection); 71 return; 72 } 73 74 return_if_fail (0 <= index < glyphs.size); 75 76 font.deleted_glyphs.add (glyph_collection.get_current ()); 77 78 over_view.store_undo_state (glyph_collection.copy ()); 79 80 glyphs.remove_at (index); 81 82 recreate_index (); 83 84 current_version = get_current_version_index (); 85 if (index == current_version) { 86 set_selected_item (get_action_no2 ()); // select the first glyph if the current glyph is deleted 87 } else if (index < current_version) { 88 return_if_fail (0 <= current_version - 1 < glyphs.size); 89 current_version_id = glyphs.get (current_version - 1).version_id; 90 } 91 } 92 93 private int get_current_version_index () { 94 int i = 0; 95 foreach (Glyph g in glyphs) { 96 if (g.version_id == current_version_id) { 97 return i; 98 } 99 i++; 100 } 101 return i; 102 } 103 104 public void set_selected_version (int version_id) { 105 current_version_id = version_id; 106 update_selection (); 107 } 108 109 public Glyph get_current () { 110 Glyph? gl = null; 111 112 foreach (Glyph g in glyphs) { 113 if (g.version_id == current_version_id) { 114 return g; 115 } 116 } 117 118 if (unlikely (glyphs.size > 0)) { 119 warning (@"Can not find current glyph for id $current_version_id"); 120 gl = glyphs.get (glyphs.size - 1); 121 set_selected_version (((!) gl).version_id); 122 return (!) gl; 123 } 124 125 if (unlikely (glyphs.size == 0 && current_version_id == -1)) { 126 warning (@"No glyphs added to collection"); 127 gl = new Glyph.no_lines ("", '\0'); 128 } 129 130 return (!) gl; 131 } 132 133 public void add_new_version () { 134 Glyph g = get_current (); 135 Glyph new_version = g.copy (); 136 new_version.version_id = get_last_id () + 1; 137 add_glyph (new_version); 138 } 139 140 public int get_last_id () { 141 return_val_if_fail (glyphs.size > 0, 1); 142 return glyphs.get (glyphs.size - 1).version_id; 143 } 144 145 private void set_selected_item (MenuAction ma) { 146 int i = ma.index; 147 Glyph current_glyph; 148 Glyph g; 149 150 return_if_fail (0 <= i < glyphs.size); 151 g = glyphs.get (i); 152 153 current_version_id = g.version_id; 154 155 return_if_fail (ma.parent != null); 156 157 ((!)ma.parent).deselect_all (); 158 ma.set_selected (true); 159 160 reload_all_open_glyphs (); 161 162 if (!is_null (BirdFont.current_glyph_collection)) { 163 current_glyph = MainWindow.get_current_glyph (); 164 g.set_allocation (current_glyph.allocation); 165 g.set_default_zoom (); 166 } 167 } 168 169 /** Reload a glyph when a new version is selected. Updates the path 170 * in glyph view, not from disk but from the glyph table. 171 */ 172 void reload_all_open_glyphs () { 173 TabBar b; 174 Tab tab; 175 Tab? tn; 176 Glyph glyph; 177 Glyph updated_glyph; 178 Glyph? ug; 179 Font font = BirdFont.get_current_font (); 180 StringBuilder uni = new StringBuilder (); 181 182 if (is_null (MainWindow.get_tab_bar ())) { 183 return; 184 } 185 186 b = MainWindow.get_tab_bar (); 187 188 for (int i = 0; i < b.get_length (); i++) { 189 tn = b.get_nth (i); 190 191 if (tn == null) { 192 warning ("tab is null"); 193 return; 194 } 195 196 tab = (!) tn; 197 198 if (! (tab.get_display () is Glyph)) { 199 continue; 200 } 201 202 glyph = (Glyph) tab.get_display (); 203 uni.truncate (0); 204 uni.append_unichar (glyph.unichar_code); 205 ug = font.get_glyph (uni.str); 206 207 if (ug == null) { 208 return; 209 } 210 211 updated_glyph = (!) ug; 212 tab.set_display (updated_glyph); 213 updated_glyph.view_zoom = glyph.view_zoom; 214 updated_glyph.view_offset_x = glyph.view_offset_x; 215 updated_glyph.view_offset_y = glyph.view_offset_y; 216 } 217 } 218 219 public void add_glyph (Glyph new_version, bool selected = true) { 220 MenuAction ma; 221 int v; 222 223 v = new_version.version_id; 224 glyphs.add (new_version); 225 226 ma = add_item (t_("Version") + @" $v"); 227 ma.index = (int) glyphs.size - 1; 228 229 ma.action = (self) => { 230 Font font = BirdFont.get_current_font (); 231 set_selected_item (self); 232 font.touch (); 233 }; 234 235 if (selected) { 236 set_selected_item (ma); 237 } 238 239 update_selection (); 240 } 241 242 bool has_version (int id) { 243 foreach (Glyph g in glyphs) { 244 if (g.version_id == id) { 245 return true; 246 } 247 } 248 return false; 249 } 250 251 void update_selection () { 252 if (has_version (current_version_id)) { 253 set_selected_item (get_action_index (get_current_version_index () + 1)); // the first item is the "new version" 254 } 255 } 256 257 } 258 259 } 260