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 bugs in 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 public FileDialogTab (string title, FileChooser action) { 40 this.title = title; 41 this.action = action; 42 43 rows = new Gee.ArrayList<Row> (); 44 files = new Gee.ArrayList<string> (); 45 directories = new Gee.ArrayList<string> (); 46 47 selected_filename = ""; 48 selected_canvas (); 49 } 50 51 public static void add_drive_letter (char c) { 52 if (!has_drive_letters) { 53 drive_letters = new Gee.ArrayList<string> (); 54 has_drive_letters = true; 55 } 56 57 drive_letters.add (@"$((!) c.to_string ()):\\"); 58 } 59 60 public override void update_rows () { 61 Row row; 62 63 rows.clear (); 64 65 if (directories.size > 0) { 66 row = new Row.headline (t_("Directories")); 67 rows.add (row); 68 } 69 70 foreach (string dir in directories) { 71 row = new Row.columns_1 (dir, DIRECTORY, false); 72 row.set_row_data (new SelectedFile (dir)); 73 rows.add (row); 74 } 75 76 if (files.size > 0) { 77 row = new Row.headline (t_("Files")); 78 rows.add (row); 79 } 80 81 foreach (string f in files) { 82 row = new Row.columns_1 (f, FILE, false); 83 row.set_row_data (new SelectedFile (f)); 84 rows.add (row); 85 } 86 87 GlyphCanvas.redraw (); 88 } 89 90 public override Gee.ArrayList<Row> get_rows () { 91 return rows; 92 } 93 94 public override void selected_row (Row row, int column, bool delete_button) { 95 SelectedFile f; 96 97 if (row.get_index () == FILE) { 98 return_if_fail (row.get_row_data () is SelectedFile); 99 f = (SelectedFile) row.get_row_data (); 100 selected_filename = f.file_name; 101 } else if (row.get_index () == DIRECTORY) { 102 return_if_fail (row.get_row_data () is SelectedFile); 103 f = (SelectedFile) row.get_row_data (); 104 selected_filename = ""; 105 propagate_files (f.file_name); 106 } 107 108 show_text_area (selected_filename); 109 } 110 111 public override void selected_canvas () { 112 string d; 113 show_text_area (""); 114 115 d = Preferences.get ("file_dialog_dir"); 116 117 if (d == "") { 118 d = Environment.get_home_dir (); 119 } 120 121 propagate_files (d); 122 base.selected_canvas (); 123 } 124 125 public void propagate_files (string dir) { 126 FileEnumerator enumerator; 127 FileInfo? file_info; 128 string fn; 129 130 files.clear (); 131 directories.clear (); 132 133 current_dir = File.new_for_path (dir); 134 135 Preferences.set ("file_dialog_dir", dir); 136 137 if (current_dir.get_parent () != null) { 138 directories.add (".."); 139 } 140 141 try { 142 enumerator = current_dir.enumerate_children (FileAttribute.STANDARD_NAME + "," + FileAttribute.STANDARD_TYPE, 0); 143 144 while ((file_info = enumerator.next_file ()) != null) { 145 fn = ((!)file_info).get_name (); 146 if (!fn.has_prefix (".")) { 147 if (((!)file_info).get_file_type () == FileType.DIRECTORY) { 148 directories.add (fn); 149 } else { 150 files.add (fn); 151 } 152 } 153 } 154 } catch (Error e) { 155 warning (e.message); 156 } 157 158 directories.sort (); 159 160 if (has_drive_letters) { 161 for (int i = drive_letters.size - 1; i >= 0; i--) { 162 directories.insert (0, drive_letters.get (i)); 163 } 164 } 165 166 files.sort (); 167 168 selected_canvas (); 169 } 170 171 public void show_text_area (string text) { 172 listener = new TextListener ("", text, this.title); 173 174 listener.signal_text_input.connect ((text) => { 175 selected_filename = text; 176 }); 177 178 listener.signal_submit.connect (() => { 179 File f; 180 181 MainWindow.get_tab_bar ().close_display (this); 182 183 if (selected_filename == "") { 184 action.cancel (); 185 } else { 186 f = get_child (current_dir, selected_filename); 187 action.file_selected ((!)f.get_path ()); 188 } 189 }); 190 191 TabContent.show_text_input (listener); 192 } 193 194 public override void double_click (uint button, double ex, double ey) { 195 File f; 196 197 button_release ((int) button, ex, ey); 198 199 if (is_null (selected_filename)) { 200 warning ("No file."); 201 return; 202 } 203 204 if (selected_filename != "") { 205 f = get_child (current_dir, selected_filename); 206 action.file_selected ((!) f.get_path ()); 207 } 208 } 209 210 public override string get_label () { 211 return title; 212 } 213 214 public override string get_name () { 215 return "FileDialogTab"; 216 } 217 218 class SelectedFile : GLib.Object { 219 public string file_name; 220 221 public SelectedFile (string fn) { 222 file_name = fn; 223 } 224 } 225 } 226 227 } 228