The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

FileDialogTab.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/FileDialogTab.vala.
Merge branch '2.10'
1 /* 2 Copyright (C) 2014 2015 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 using Cairo; 15 using Math; 16 17 namespace BirdFont { 18 19 public class FileDialogTab : Table { 20 Gee.ArrayList<Row> rows; 21 22 const int DIRECTORY = -3; 23 const int FILE = -2; 24 25 Gee.ArrayList<string> files; 26 Gee.ArrayList<string> directories; 27 28 string title; 29 FileChooser action; 30 31 File current_dir; 32 33 string selected_filename; 34 TextListener listener; 35 36 private static bool has_drive_letters = false; 37 private static Gee.ArrayList<string> drive_letters; 38 39 bool select_folder = false; 40 41 #if LINUX 42 public static const string path_separator = "/"; 43 #else 44 public static const string path_separator = "\\"; 45 #endif 46 47 public FileDialogTab (string title, FileChooser action, bool folder) { 48 this.title = title; 49 this.action = action; 50 51 select_folder = folder; 52 rows = new Gee.ArrayList<Row> (); 53 files = new Gee.ArrayList<string> (); 54 directories = new Gee.ArrayList<string> (); 55 56 selected_filename = ""; 57 selected_canvas (); 58 } 59 60 public static void add_drive_letter (char c) { 61 if (!has_drive_letters) { 62 drive_letters = new Gee.ArrayList<string> (); 63 has_drive_letters = true; 64 } 65 66 drive_letters.add (@"$((!) c.to_string ()):\\"); 67 } 68 69 public override void update_rows () { 70 Row row; 71 72 rows.clear (); 73 74 // add empty rows under the text area 75 row = new Row.headline (""); 76 rows.add (row); 77 78 if (select_folder) { 79 row = new Row.headline (t_("Select a Folder")); 80 } 81 82 if (directories.size > 0) { 83 row = new Row.headline (t_("Folders")); 84 rows.add (row); 85 } 86 87 foreach (string dir in directories) { 88 row = new Row.columns_1 (dir, DIRECTORY, false); 89 row.set_row_data (new SelectedFile (dir)); 90 rows.add (row); 91 } 92 93 if (files.size > 0) { 94 row = new Row.headline (t_("Files")); 95 rows.add (row); 96 } 97 98 foreach (string f in files) { 99 row = new Row.columns_1 (f, FILE, false); 100 row.set_row_data (new SelectedFile (f)); 101 rows.add (row); 102 } 103 104 GlyphCanvas.redraw (); 105 } 106 107 public override Gee.ArrayList<Row> get_rows () { 108 return rows; 109 } 110 111 public override void selected_row (Row row, int column, bool delete_button) { 112 SelectedFile f; 113 114 if (row.get_index () == FILE) { 115 if (!select_folder) { 116 return_if_fail (row.get_row_data () is SelectedFile); 117 f = (SelectedFile) row.get_row_data (); 118 selected_filename = f.file_name; 119 } 120 } else if (row.get_index () == DIRECTORY) { 121 return_if_fail (row.get_row_data () is SelectedFile); 122 f = (SelectedFile) row.get_row_data (); 123 124 if (select_folder && f.file_name != "..") { 125 selected_filename = f.file_name; 126 } else { 127 if (f.file_name.index_of (":") > -1) { 128 propagate_files (f.file_name); 129 } else { 130 propagate_files (((!) current_dir.get_path ()) + path_separator + f.file_name); 131 } 132 133 selected_filename = ""; 134 } 135 } 136 137 show_text_area (selected_filename); 138 } 139 140 public override void selected_canvas () { 141 string d; 142 show_text_area (""); 143 144 d = Preferences.get ("file_dialog_dir"); 145 146 if (d == "") { 147 d = Environment.get_home_dir (); 148 } 149 150 propagate_files (d); 151 base.selected_canvas (); 152 } 153 154 public void propagate_files (string dir) { 155 FileEnumerator enumerator; 156 FileInfo? file_info; 157 string fn; 158 159 files.clear (); 160 directories.clear (); 161 162 current_dir = File.new_for_path (dir); 163 164 Preferences.set ("file_dialog_dir", dir); 165 166 if (current_dir.get_parent () != null) { 167 directories.add (".."); 168 } 169 170 try { 171 enumerator = current_dir.enumerate_children (FileAttribute.STANDARD_NAME + "," + FileAttribute.STANDARD_TYPE, 0); 172 173 while ((file_info = enumerator.next_file ()) != null) { 174 fn = ((!)file_info).get_name (); 175 if (!fn.has_prefix (".")) { 176 if (((!)file_info).get_file_type () == FileType.DIRECTORY) { 177 directories.add (fn); 178 } else { 179 files.add (fn); 180 } 181 } 182 } 183 } catch (Error e) { 184 warning (e.message); 185 } 186 187 directories.sort (); 188 189 if (has_drive_letters) { 190 for (int i = drive_letters.size - 1; i >= 0; i--) { 191 directories.insert (0, drive_letters.get (i)); 192 } 193 } 194 195 files.sort (); 196 197 layout (); 198 base.selected_canvas (); 199 scroll_to (0); 200 } 201 202 public void show_text_area (string text) { 203 listener = new TextListener ("", text, this.title); 204 205 listener.signal_text_input.connect ((text) => { 206 selected_filename = text; 207 }); 208 209 listener.signal_submit.connect (() => { 210 File f; 211 212 MainWindow.get_tab_bar ().close_display (this); 213 214 if (selected_filename == "") { 215 action.cancel (); 216 } else { 217 f = get_child (current_dir, selected_filename); 218 action.file_selected ((!)f.get_path ()); 219 } 220 }); 221 222 TabContent.show_text_input (listener); 223 } 224 225 public override void double_click (uint button, double ex, double ey) { 226 File f; 227 228 button_release ((int) button, ex, ey); 229 230 if (is_null (selected_filename)) { 231 warning ("No file."); 232 return; 233 } 234 235 if (select_folder) { 236 if (selected_filename.index_of (":") > -1) { 237 propagate_files (selected_filename); 238 } else { 239 propagate_files (((!) current_dir.get_path ()) + path_separator + selected_filename); 240 } 241 } else { 242 if (selected_filename != "") { 243 f = get_child (current_dir, selected_filename); 244 action.file_selected ((!) f.get_path ()); 245 } 246 } 247 } 248 249 public override string get_label () { 250 return title; 251 } 252 253 public override string get_name () { 254 return "FileDialogTab"; 255 } 256 257 public override void button_release (int button, double ex, double ey) { 258 base.button_release (button, ex, ey); 259 show_text_area (selected_filename); 260 } 261 262 class SelectedFile : GLib.Object { 263 public string file_name; 264 265 public SelectedFile (string fn) { 266 file_name = fn; 267 } 268 } 269 } 270 271 } 272