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.
Fix file dialog on Windows
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 string d = (!) current_dir.get_path (); 131 printd (@"Subdir: $(f.file_name) in $d path_separator: $path_separator\n"); 132 string p = d + path_separator + f.file_name; 133 p = p.replace ("\\\\", "\\"); 134 propagate_files (p); 135 } 136 137 selected_filename = ""; 138 } 139 } 140 141 show_text_area (selected_filename); 142 } 143 144 public override void selected_canvas () { 145 string d; 146 show_text_area (""); 147 148 d = Preferences.get ("file_dialog_dir"); 149 150 if (d == "") { 151 d = Environment.get_home_dir (); 152 } 153 154 propagate_files (d); 155 base.selected_canvas (); 156 } 157 158 public void propagate_files (string dir) { 159 FileEnumerator enumerator; 160 FileInfo? file_info; 161 string fn; 162 163 printd (@"Propagate_files in directory: $dir\n"); 164 165 files.clear (); 166 directories.clear (); 167 168 current_dir = File.new_for_path (dir); 169 170 Preferences.set ("file_dialog_dir", dir); 171 172 if (current_dir.get_parent () != null) { 173 directories.add (".."); 174 } 175 176 try { 177 enumerator = current_dir.enumerate_children (FileAttribute.STANDARD_NAME + "," + FileAttribute.STANDARD_TYPE, 0); 178 179 while ((file_info = enumerator.next_file ()) != null) { 180 fn = ((!)file_info).get_name (); 181 if (!fn.has_prefix (".")) { 182 if (((!)file_info).get_file_type () == FileType.DIRECTORY) { 183 directories.add (fn); 184 } else { 185 files.add (fn); 186 } 187 } 188 } 189 } catch (Error e) { 190 warning (e.message); 191 } 192 193 directories.sort (); 194 195 if (has_drive_letters) { 196 for (int i = drive_letters.size - 1; i >= 0; i--) { 197 directories.insert (0, drive_letters.get (i)); 198 } 199 } 200 201 files.sort (); 202 203 layout (); 204 base.selected_canvas (); 205 scroll_to (0); 206 } 207 208 public void show_text_area (string text) { 209 listener = new TextListener ("", text, this.title); 210 211 listener.signal_text_input.connect ((text) => { 212 selected_filename = text; 213 }); 214 215 listener.signal_submit.connect (() => { 216 File f; 217 218 MainWindow.get_tab_bar ().close_display (this); 219 220 if (selected_filename == "") { 221 action.cancel (); 222 } else { 223 f = get_child (current_dir, selected_filename); 224 action.file_selected ((!)f.get_path ()); 225 } 226 }); 227 228 TabContent.show_text_input (listener); 229 } 230 231 public override void double_click (uint button, double ex, double ey) { 232 File f; 233 234 button_release ((int) button, ex, ey); 235 236 if (is_null (selected_filename)) { 237 warning ("No file."); 238 return; 239 } 240 241 if (select_folder) { 242 if (selected_filename.index_of (":") > -1) { 243 propagate_files (selected_filename); 244 } else { 245 propagate_files (((!) current_dir.get_path ()) + path_separator + selected_filename); 246 } 247 } else { 248 if (selected_filename != "") { 249 f = get_child (current_dir, selected_filename); 250 action.file_selected ((!) f.get_path ()); 251 } 252 } 253 } 254 255 public override string get_label () { 256 return title; 257 } 258 259 public override string get_name () { 260 return "FileDialogTab"; 261 } 262 263 public override void button_release (int button, double ex, double ey) { 264 base.button_release (button, ex, ey); 265 show_text_area (selected_filename); 266 } 267 268 class SelectedFile : GLib.Object { 269 public string file_name; 270 271 public SelectedFile (string fn) { 272 file_name = fn; 273 } 274 } 275 } 276 277 } 278