FileBrowser: Editable Bookmarks.
[blender-staging.git] / source / blender / editors / space_file / filesel.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_file/filesel.c
28  *  \ingroup spfile
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34 #include <math.h>
35
36 #include <sys/stat.h>
37 #include <sys/types.h>
38
39 /* path/file handeling stuff */
40 #ifdef WIN32
41 #  include <io.h>
42 #  include <direct.h>
43 #  include "BLI_winstuff.h"
44 #else
45 #  include <unistd.h>
46 #  include <sys/times.h>
47 #  include <dirent.h>
48 #endif
49
50 #include "DNA_space_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_userdef_types.h"
53
54 #include "MEM_guardedalloc.h"
55
56 #include "BLI_blenlib.h"
57 #include "BLI_utildefines.h"
58 #include "BLI_fileops_types.h"
59 #include "BLI_fnmatch.h"
60
61 #include "BKE_appdir.h"
62 #include "BKE_context.h"
63 #include "BKE_global.h"
64 #include "BKE_main.h"
65
66 #include "BLF_api.h"
67
68
69 #include "ED_fileselect.h"
70
71 #include "WM_api.h"
72 #include "WM_types.h"
73
74
75 #include "RNA_access.h"
76
77 #include "UI_interface.h"
78 #include "UI_interface_icons.h"
79
80 #include "file_intern.h"
81 #include "filelist.h"
82
83 FileSelectParams *ED_fileselect_get_params(struct SpaceFile *sfile)
84 {
85         if (!sfile->params) {
86                 ED_fileselect_set_params(sfile);
87         }
88         return sfile->params;
89 }
90
91 /**
92  * \note RNA_struct_property_is_set_ex is used here because we want
93  *       the previously used settings to be used here rather then overriding them */
94 short ED_fileselect_set_params(SpaceFile *sfile)
95 {
96         FileSelectParams *params;
97         wmOperator *op = sfile->op;
98
99         /* create new parameters if necessary */
100         if (!sfile->params) {
101                 sfile->params = MEM_callocN(sizeof(FileSelectParams), "fileselparams");
102                 /* set path to most recently opened .blend */
103                 BLI_split_dirfile(G.main->name, sfile->params->dir, sfile->params->file, sizeof(sfile->params->dir), sizeof(sfile->params->file));
104                 sfile->params->filter_glob[0] = '\0';
105         }
106
107         params = sfile->params;
108
109         /* set the parameters from the operator, if it exists */
110         if (op) {
111                 PropertyRNA *prop;
112                 const bool is_files = (RNA_struct_find_property(op->ptr, "files") != NULL);
113                 const bool is_filepath = (RNA_struct_find_property(op->ptr, "filepath") != NULL);
114                 const bool is_filename = (RNA_struct_find_property(op->ptr, "filename") != NULL);
115                 const bool is_directory = (RNA_struct_find_property(op->ptr, "directory") != NULL);
116                 const bool is_relative_path = (RNA_struct_find_property(op->ptr, "relative_path") != NULL);
117
118                 BLI_strncpy_utf8(params->title, RNA_struct_ui_name(op->type->srna), sizeof(params->title));
119
120                 if (RNA_struct_find_property(op->ptr, "filemode"))
121                         params->type = RNA_int_get(op->ptr, "filemode");
122                 else
123                         params->type = FILE_SPECIAL;
124
125                 if (is_filepath && RNA_struct_property_is_set_ex(op->ptr, "filepath", false)) {
126                         char name[FILE_MAX];
127                         RNA_string_get(op->ptr, "filepath", name);
128                         if (params->type == FILE_LOADLIB) {
129                                 BLI_strncpy(params->dir, name, sizeof(params->dir));
130                                 sfile->params->file[0] = '\0';
131                         }
132                         else {
133                                 BLI_split_dirfile(name, sfile->params->dir, sfile->params->file, sizeof(sfile->params->dir), sizeof(sfile->params->file));
134                         }
135                 }
136                 else {
137                         if (is_directory && RNA_struct_property_is_set_ex(op->ptr, "directory", false)) {
138                                 RNA_string_get(op->ptr, "directory", params->dir);
139                                 sfile->params->file[0] = '\0';
140                         }
141
142                         if (is_filename && RNA_struct_property_is_set_ex(op->ptr, "filename", false)) {
143                                 RNA_string_get(op->ptr, "filename", params->file);
144                         }
145                 }
146
147                 if (params->dir[0]) {
148                         BLI_cleanup_dir(G.main->name, params->dir);
149                         BLI_path_abs(params->dir, G.main->name);
150                 }
151
152                 if (is_directory == true && is_filename == false && is_filepath == false && is_files == false) {
153                         params->flag |= FILE_DIRSEL_ONLY;
154                 }
155                 else {
156                         params->flag &= ~FILE_DIRSEL_ONLY;
157                 }
158
159                 params->filter = 0;
160                 if ((prop = RNA_struct_find_property(op->ptr, "filter_blender")))
161                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_BLENDER : 0;
162                 if ((prop = RNA_struct_find_property(op->ptr, "filter_backup")))
163                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_BLENDER_BACKUP : 0;
164                 if ((prop = RNA_struct_find_property(op->ptr, "filter_image")))
165                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_IMAGE : 0;
166                 if ((prop = RNA_struct_find_property(op->ptr, "filter_movie")))
167                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_MOVIE : 0;
168                 if ((prop = RNA_struct_find_property(op->ptr, "filter_python")))
169                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_PYSCRIPT : 0;
170                 if ((prop = RNA_struct_find_property(op->ptr, "filter_font")))
171                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_FTFONT : 0;
172                 if ((prop = RNA_struct_find_property(op->ptr, "filter_sound")))
173                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_SOUND : 0;
174                 if ((prop = RNA_struct_find_property(op->ptr, "filter_text")))
175                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_TEXT : 0;
176                 if ((prop = RNA_struct_find_property(op->ptr, "filter_folder")))
177                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_FOLDER : 0;
178                 if ((prop = RNA_struct_find_property(op->ptr, "filter_btx")))
179                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_BTX : 0;
180                 if ((prop = RNA_struct_find_property(op->ptr, "filter_collada")))
181                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_COLLADA : 0;
182                 if ((prop = RNA_struct_find_property(op->ptr, "filter_glob"))) {
183                         RNA_property_string_get(op->ptr, prop, params->filter_glob);
184                         params->filter |= (FILE_TYPE_OPERATOR | FILE_TYPE_FOLDER);
185                 }
186                 else {
187                         params->filter_glob[0] = '\0';
188                 }
189
190                 if (params->filter != 0) {
191                         if (U.uiflag & USER_FILTERFILEEXTS) {
192                                 params->flag |= FILE_FILTER;
193                         }
194                         else {
195                                 params->flag &= ~FILE_FILTER;
196                         }
197                 }
198
199                 if (U.uiflag & USER_HIDE_DOT) {
200                         params->flag |= FILE_HIDE_DOT;
201                 }
202                 else {
203                         params->flag &= ~FILE_HIDE_DOT;
204                 }
205                 
206
207                 if (params->type == FILE_LOADLIB) {
208                         params->flag |= RNA_boolean_get(op->ptr, "link") ? FILE_LINK : 0;
209                         params->flag |= RNA_boolean_get(op->ptr, "autoselect") ? FILE_AUTOSELECT : 0;
210                         params->flag |= RNA_boolean_get(op->ptr, "active_layer") ? FILE_ACTIVELAY : 0;
211                 }
212
213                 if (RNA_struct_find_property(op->ptr, "display_type"))
214                         params->display = RNA_enum_get(op->ptr, "display_type");
215
216                 if (params->display == FILE_DEFAULTDISPLAY) {
217                         if (U.uiflag & USER_SHOW_THUMBNAILS) {
218                                 if (params->filter & (FILE_TYPE_IMAGE | FILE_TYPE_MOVIE))
219                                         params->display = FILE_IMGDISPLAY;
220                                 else
221                                         params->display = FILE_SHORTDISPLAY;
222                         }
223                         else {
224                                 params->display = FILE_SHORTDISPLAY;
225                         }
226                 }
227
228                 if (is_relative_path) {
229                         if (!RNA_struct_property_is_set_ex(op->ptr, "relative_path", false)) {
230                                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
231                         }
232                 }
233         }
234         else {
235                 /* default values, if no operator */
236                 params->type = FILE_UNIX;
237                 params->flag |= FILE_HIDE_DOT;
238                 params->flag &= ~FILE_DIRSEL_ONLY;
239                 params->display = FILE_SHORTDISPLAY;
240                 params->filter = 0;
241                 params->filter_glob[0] = '\0';
242         }
243
244         /* operator has no setting for this */
245         params->sort = FILE_SORT_ALPHA;
246
247
248         /* initialize the list with previous folders */
249         if (!sfile->folders_prev)
250                 sfile->folders_prev = folderlist_new();
251
252         if (!sfile->params->dir[0]) {
253                 if (G.main->name[0]) {
254                         BLI_split_dir_part(G.main->name, sfile->params->dir, sizeof(sfile->params->dir));
255                 }
256                 else {
257                         const char *doc_path = BKE_appdir_folder_default();
258                         if (doc_path) {
259                                 BLI_strncpy(sfile->params->dir, doc_path, sizeof(sfile->params->dir));
260                         }
261                 }
262         }
263
264         folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
265
266         /* switching thumbnails needs to recalc layout [#28809] */
267         if (sfile->layout) {
268                 sfile->layout->dirty = true;
269         }
270
271         return 1;
272 }
273
274 void ED_fileselect_reset_params(SpaceFile *sfile)
275 {
276         sfile->params->type = FILE_UNIX;
277         sfile->params->flag = 0;
278         sfile->params->title[0] = '\0';
279 }
280
281 int ED_fileselect_layout_numfiles(FileLayout *layout, ARegion *ar)
282 {
283         int numfiles;
284
285         /* Values in pixels.
286          *
287          * - *_item: size of each (row|col), (including padding)
288          * - *_view: (x|y) size of the view.
289          * - *_over: extra pixels, to take into account, when the fit isnt exact
290          *   (needed since you may see the end of the previous column and the beginning of the next).
291          *
292          * Could be more clever and take scrolling into account,
293          * but for now don't bother.
294          */
295         if (layout->flag & FILE_LAYOUT_HOR) {
296                 const int x_item = layout->tile_w + (2 * layout->tile_border_x);
297                 const int x_view = (int)(BLI_rctf_size_x(&ar->v2d.cur));
298                 const int x_over = x_item - (x_view % x_item);
299                 numfiles = (int)((float)(x_view + x_over) / (float)(x_item));
300                 return numfiles * layout->rows;
301         }
302         else {
303                 const int y_item = layout->tile_h + (2 * layout->tile_border_y);
304                 const int y_view = (int)(BLI_rctf_size_y(&ar->v2d.cur));
305                 const int y_over = y_item - (y_view % y_item);
306                 numfiles = (int)((float)(y_view + y_over) / (float)(y_item));
307                 return numfiles * layout->columns;
308         }
309 }
310
311 static bool is_inside(int x, int y, int cols, int rows)
312 {
313         return ((x >= 0) && (x < cols) && (y >= 0) && (y < rows));
314 }
315
316 FileSelection ED_fileselect_layout_offset_rect(FileLayout *layout, const rcti *rect)
317 {
318         int colmin, colmax, rowmin, rowmax;
319         FileSelection sel;
320         sel.first = sel.last = -1;
321
322         if (layout == NULL)
323                 return sel;
324         
325         colmin = (rect->xmin) / (layout->tile_w + 2 * layout->tile_border_x);
326         rowmin = (rect->ymin) / (layout->tile_h + 2 * layout->tile_border_y);
327         colmax = (rect->xmax) / (layout->tile_w + 2 * layout->tile_border_x);
328         rowmax = (rect->ymax) / (layout->tile_h + 2 * layout->tile_border_y);
329         
330         if (is_inside(colmin, rowmin, layout->columns, layout->rows) ||
331             is_inside(colmax, rowmax, layout->columns, layout->rows) )
332         {
333                 CLAMP(colmin, 0, layout->columns - 1);
334                 CLAMP(rowmin, 0, layout->rows - 1);
335                 CLAMP(colmax, 0, layout->columns - 1);
336                 CLAMP(rowmax, 0, layout->rows - 1);
337         }
338         
339         if ((colmin > layout->columns - 1) || (rowmin > layout->rows - 1)) {
340                 sel.first = -1;
341         }
342         else {
343                 if (layout->flag & FILE_LAYOUT_HOR) 
344                         sel.first = layout->rows * colmin + rowmin;
345                 else
346                         sel.first = colmin + layout->columns * rowmin;
347         }
348         if ((colmax > layout->columns - 1) || (rowmax > layout->rows - 1)) {
349                 sel.last = -1;
350         }
351         else {
352                 if (layout->flag & FILE_LAYOUT_HOR) 
353                         sel.last = layout->rows * colmax + rowmax;
354                 else
355                         sel.last = colmax + layout->columns * rowmax;
356         }
357
358         return sel;
359 }
360
361 int ED_fileselect_layout_offset(FileLayout *layout, int x, int y)
362 {
363         int offsetx, offsety;
364         int active_file;
365
366         if (layout == NULL)
367                 return -1;
368         
369         offsetx = (x) / (layout->tile_w + 2 * layout->tile_border_x);
370         offsety = (y) / (layout->tile_h + 2 * layout->tile_border_y);
371         
372         if (offsetx > layout->columns - 1) return -1;
373         if (offsety > layout->rows - 1) return -1;
374         
375         if (layout->flag & FILE_LAYOUT_HOR) 
376                 active_file = layout->rows * offsetx + offsety;
377         else
378                 active_file = offsetx + layout->columns * offsety;
379         return active_file;
380 }
381
382 void ED_fileselect_layout_tilepos(FileLayout *layout, int tile, int *x, int *y)
383 {
384         if (layout->flag == FILE_LAYOUT_HOR) {
385                 *x = layout->tile_border_x + (tile / layout->rows) * (layout->tile_w + 2 * layout->tile_border_x);
386                 *y = layout->tile_border_y + (tile % layout->rows) * (layout->tile_h + 2 * layout->tile_border_y);
387         }
388         else {
389                 *x = layout->tile_border_x + ((tile) % layout->columns) * (layout->tile_w + 2 * layout->tile_border_x);
390                 *y = layout->tile_border_y + ((tile) / layout->columns) * (layout->tile_h + 2 * layout->tile_border_y);
391         }
392 }
393
394 /* Shorten a string to a given width w. 
395  * If front is set, shorten from the front,
396  * otherwise shorten from the end. */
397 float file_shorten_string(char *string, float w, int front)
398 {       
399         char temp[FILE_MAX];
400         short shortened = 0;
401         float sw = 0;
402         float pad = 0;
403
404         if (w <= 0) {
405                 *string = '\0';
406                 return 0.0;
407         }
408
409         sw = file_string_width(string);
410         if (front == 1) {
411                 const char *s = string;
412                 BLI_strncpy(temp, "...", 4);
413                 pad = file_string_width(temp);
414                 while ((*s) && (sw + pad > w)) {
415                         s++;
416                         sw = file_string_width(s);
417                         shortened = 1;
418                 }
419                 if (shortened) {
420                         int slen = strlen(s);
421                         BLI_strncpy(temp + 3, s, slen + 1);
422                         temp[slen + 4] = '\0';
423                         BLI_strncpy(string, temp, slen + 4);
424                 }
425         }
426         else {
427                 const char *s = string;
428                 while (sw > w) {
429                         int slen = strlen(string);
430                         string[slen - 1] = '\0';
431                         sw = file_string_width(s);
432                         shortened = 1;
433                 }
434
435                 if (shortened) {
436                         int slen = strlen(string);
437                         if (slen > 3) {
438                                 BLI_strncpy(string + slen - 3, "...", 4);
439                         }
440                 }
441         }
442         
443         return sw;
444 }
445
446 float file_string_width(const char *str)
447 {
448         uiStyle *style = UI_style_get();
449         UI_fontstyle_set(&style->widget);
450         return BLF_width(style->widget.uifont_id, str, BLF_DRAW_STR_DUMMY_MAX);
451 }
452
453 float file_font_pointsize(void)
454 {
455 #if 0
456         float s;
457         char tmp[2] = "X";
458         uiStyle *style = UI_style_get();
459         UI_fontstyle_set(&style->widget);
460         s = BLF_height(style->widget.uifont_id, tmp);
461         return style->widget.points;
462 #else
463         uiStyle *style = UI_style_get();
464         UI_fontstyle_set(&style->widget);
465         return style->widget.points * UI_DPI_FAC;
466 #endif
467 }
468
469 static void column_widths(struct FileList *files, struct FileLayout *layout)
470 {
471         int i;
472         int numfiles = filelist_numfiles(files);
473
474         for (i = 0; i < MAX_FILE_COLUMN; ++i) {
475                 layout->column_widths[i] = 0;
476         }
477
478         for (i = 0; (i < numfiles); ++i) {
479                 struct direntry *file = filelist_file(files, i);
480                 if (file) {
481                         float len;
482                         len = file_string_width(file->relname);
483                         if (len > layout->column_widths[COLUMN_NAME]) layout->column_widths[COLUMN_NAME] = len;
484                         len = file_string_width(file->date);
485                         if (len > layout->column_widths[COLUMN_DATE]) layout->column_widths[COLUMN_DATE] = len;
486                         len = file_string_width(file->time);
487                         if (len > layout->column_widths[COLUMN_TIME]) layout->column_widths[COLUMN_TIME] = len;
488                         len = file_string_width(file->size);
489                         if (len > layout->column_widths[COLUMN_SIZE]) layout->column_widths[COLUMN_SIZE] = len;
490                         len = file_string_width(file->mode1);
491                         if (len > layout->column_widths[COLUMN_MODE1]) layout->column_widths[COLUMN_MODE1] = len;
492                         len = file_string_width(file->mode2);
493                         if (len > layout->column_widths[COLUMN_MODE2]) layout->column_widths[COLUMN_MODE2] = len;
494                         len = file_string_width(file->mode3);
495                         if (len > layout->column_widths[COLUMN_MODE3]) layout->column_widths[COLUMN_MODE3] = len;
496                         len = file_string_width(file->owner);
497                         if (len > layout->column_widths[COLUMN_OWNER]) layout->column_widths[COLUMN_OWNER] = len;
498                 }
499         }
500 }
501
502 void ED_fileselect_init_layout(struct SpaceFile *sfile, ARegion *ar)
503 {
504         FileSelectParams *params = ED_fileselect_get_params(sfile);
505         FileLayout *layout = NULL;
506         View2D *v2d = &ar->v2d;
507         int maxlen = 0;
508         int numfiles;
509         int textheight;
510
511         if (sfile->layout == NULL) {
512                 sfile->layout = MEM_callocN(sizeof(struct FileLayout), "file_layout");
513                 sfile->layout->dirty = true;
514         }
515         else if (sfile->layout->dirty == false) {
516                 return;
517         }
518
519         numfiles = filelist_numfiles(sfile->files);
520         textheight = (int)file_font_pointsize();
521         layout = sfile->layout;
522         layout->textheight = textheight;
523
524         if (params->display == FILE_IMGDISPLAY) {
525                 layout->prv_w = 4.8f * UI_UNIT_X;
526                 layout->prv_h = 4.8f * UI_UNIT_Y;
527                 layout->tile_border_x = 0.3f * UI_UNIT_X;
528                 layout->tile_border_y = 0.3f * UI_UNIT_X;
529                 layout->prv_border_x = 0.3f * UI_UNIT_X;
530                 layout->prv_border_y = 0.3f * UI_UNIT_Y;
531                 layout->tile_w = layout->prv_w + 2 * layout->prv_border_x;
532                 layout->tile_h = layout->prv_h + 2 * layout->prv_border_y + textheight;
533                 layout->width = (int)(BLI_rctf_size_x(&v2d->cur) - 2 * layout->tile_border_x);
534                 layout->columns = layout->width / (layout->tile_w + 2 * layout->tile_border_x);
535                 if (layout->columns > 0)
536                         layout->rows = numfiles / layout->columns + 1;  // XXX dirty, modulo is zero
537                 else {
538                         layout->columns = 1;
539                         layout->rows = numfiles + 1; // XXX dirty, modulo is zero
540                 }
541                 layout->height = sfile->layout->rows * (layout->tile_h + 2 * layout->tile_border_y) + layout->tile_border_y * 2;
542                 layout->flag = FILE_LAYOUT_VER;
543         }
544         else {
545                 int column_space = 0.6f * UI_UNIT_X;
546                 int column_icon_space = 0.2f * UI_UNIT_X;
547
548                 layout->prv_w = 0;
549                 layout->prv_h = 0;
550                 layout->tile_border_x = 0.4f * UI_UNIT_X;
551                 layout->tile_border_y = 0.1f * UI_UNIT_Y;
552                 layout->prv_border_x = 0;
553                 layout->prv_border_y = 0;
554                 layout->tile_h = textheight * 3 / 2;
555                 layout->height = (int)(BLI_rctf_size_y(&v2d->cur) - 2 * layout->tile_border_y);
556                 layout->rows = layout->height / (layout->tile_h + 2 * layout->tile_border_y);
557
558                 column_widths(sfile->files, layout);
559
560                 if (params->display == FILE_SHORTDISPLAY) {
561                         maxlen = ICON_DEFAULT_WIDTH_SCALE + column_icon_space +
562                                  (int)layout->column_widths[COLUMN_NAME] + column_space +
563                                  (int)layout->column_widths[COLUMN_SIZE] + column_space;
564                 }
565                 else {
566                         maxlen = ICON_DEFAULT_WIDTH_SCALE + column_icon_space +
567                                  (int)layout->column_widths[COLUMN_NAME] + column_space +
568 #ifndef WIN32
569                                  (int)layout->column_widths[COLUMN_MODE1] + column_space +
570                                  (int)layout->column_widths[COLUMN_MODE2] + column_space +
571                                  (int)layout->column_widths[COLUMN_MODE3] + column_space +
572                                  (int)layout->column_widths[COLUMN_OWNER] + column_space +
573 #endif
574                                  (int)layout->column_widths[COLUMN_DATE] + column_space +
575                                  (int)layout->column_widths[COLUMN_TIME] + column_space +
576                                  (int)layout->column_widths[COLUMN_SIZE] + column_space;
577
578                 }
579                 layout->tile_w = maxlen;
580                 if (layout->rows > 0)
581                         layout->columns = numfiles / layout->rows + 1;  // XXX dirty, modulo is zero
582                 else {
583                         layout->rows = 1;
584                         layout->columns = numfiles + 1; // XXX dirty, modulo is zero
585                 }
586                 layout->width = sfile->layout->columns * (layout->tile_w + 2 * layout->tile_border_x) + layout->tile_border_x * 2;
587                 layout->flag = FILE_LAYOUT_HOR;
588         }
589         layout->dirty = false;
590 }
591
592 FileLayout *ED_fileselect_get_layout(struct SpaceFile *sfile, ARegion *ar)
593 {
594         if (!sfile->layout) {
595                 ED_fileselect_init_layout(sfile, ar);
596         }
597         return sfile->layout;
598 }
599
600 void ED_file_change_dir(bContext *C, const bool checkdir)
601 {
602         wmWindowManager *wm = CTX_wm_manager(C);
603         SpaceFile *sfile = CTX_wm_space_file(C);
604
605         if (sfile->params) {
606                 ED_fileselect_clear(wm, sfile);
607
608                 /* Clear search string, it is very rare to want to keep that filter while changing dir,
609                  * and usually very annoying to keep it actually! */
610                 sfile->params->filter_search[0] = '\0';
611
612                 if (checkdir && !BLI_is_dir(sfile->params->dir)) {
613                         BLI_strncpy(sfile->params->dir, filelist_dir(sfile->files), sizeof(sfile->params->dir));
614                         /* could return but just refresh the current dir */
615                 }
616                 filelist_setdir(sfile->files, sfile->params->dir);
617                 
618                 if (folderlist_clear_next(sfile))
619                         folderlist_free(sfile->folders_next);
620
621                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
622
623                 file_draw_check_cb(C, NULL, NULL);
624         }
625 }
626
627 int file_select_match(struct SpaceFile *sfile, const char *pattern, char *matched_file)
628 {
629         int match = 0;
630         
631         int i;
632         struct direntry *file;
633         int n = filelist_numfiles(sfile->files);
634
635         /* select any file that matches the pattern, this includes exact match 
636          * if the user selects a single file by entering the filename
637          */
638         for (i = 0; i < n; i++) {
639                 file = filelist_file(sfile->files, i);
640                 if (fnmatch(pattern, file->relname, 0) == 0) {
641                         file->selflag |= FILE_SEL_SELECTED;
642                         if (!match) {
643                                 BLI_strncpy(matched_file, file->relname, FILE_MAX);
644                         }
645                         match++;
646                 }
647         }
648
649         return match;
650 }
651
652 int autocomplete_directory(struct bContext *C, char *str, void *UNUSED(arg_v))
653 {
654         SpaceFile *sfile = CTX_wm_space_file(C);
655         int match = AUTOCOMPLETE_NO_MATCH;
656
657         /* search if str matches the beginning of name */
658         if (str[0] && sfile->files) {
659                 char dirname[FILE_MAX];
660
661                 DIR *dir;
662                 struct dirent *de;
663                 
664                 BLI_split_dir_part(str, dirname, sizeof(dirname));
665
666                 dir = opendir(dirname);
667
668                 if (dir) {
669                         AutoComplete *autocpl = UI_autocomplete_begin(str, FILE_MAX);
670
671                         while ((de = readdir(dir)) != NULL) {
672                                 if (FILENAME_IS_CURRPAR(de->d_name)) {
673                                         /* pass */
674                                 }
675                                 else {
676                                         char path[FILE_MAX];
677                                         BLI_stat_t status;
678                                         
679                                         BLI_join_dirfile(path, sizeof(path), dirname, de->d_name);
680
681                                         if (BLI_stat(path, &status) == 0) {
682                                                 if (S_ISDIR(status.st_mode)) { /* is subdir */
683                                                         UI_autocomplete_update_name(autocpl, path);
684                                                 }
685                                         }
686                                 }
687                         }
688                         closedir(dir);
689
690                         match = UI_autocomplete_end(autocpl, str);
691                         if (match) {
692                                 if (match == AUTOCOMPLETE_FULL_MATCH) {
693                                         BLI_add_slash(str);
694                                 }
695                                 else {
696                                         BLI_strncpy(sfile->params->dir, str, sizeof(sfile->params->dir));
697                                 }
698                         }
699                 }
700         }
701
702         return match;
703 }
704
705 int autocomplete_file(struct bContext *C, char *str, void *UNUSED(arg_v))
706 {
707         SpaceFile *sfile = CTX_wm_space_file(C);
708         int match = AUTOCOMPLETE_NO_MATCH;
709
710         /* search if str matches the beginning of name */
711         if (str[0] && sfile->files) {
712                 AutoComplete *autocpl = UI_autocomplete_begin(str, FILE_MAX);
713                 int nentries = filelist_numfiles(sfile->files);
714                 int i;
715
716                 for (i = 0; i < nentries; ++i) {
717                         struct direntry *file = filelist_file(sfile->files, i);
718                         if (file && (S_ISREG(file->type) || S_ISDIR(file->type))) {
719                                 UI_autocomplete_update_name(autocpl, file->relname);
720                         }
721                 }
722                 match = UI_autocomplete_end(autocpl, str);
723         }
724
725         return match;
726 }
727
728 void ED_fileselect_clear(struct wmWindowManager *wm, struct SpaceFile *sfile)
729 {
730         /* only NULL in rare cases - [#29734] */
731         if (sfile->files) {
732                 thumbnails_stop(wm, sfile->files);
733                 filelist_freelib(sfile->files);
734                 filelist_free(sfile->files);
735         }
736
737         sfile->params->active_file = -1;
738         WM_main_add_notifier(NC_SPACE | ND_SPACE_FILE_LIST, NULL);
739 }
740
741 void ED_fileselect_exit(struct wmWindowManager *wm, struct SpaceFile *sfile)
742 {
743         if (!sfile) return;
744         if (sfile->op) {
745                 WM_event_fileselect_event(wm, sfile->op, EVT_FILESELECT_EXTERNAL_CANCEL);
746                 sfile->op = NULL;
747         }
748
749         folderlist_free(sfile->folders_prev);
750         folderlist_free(sfile->folders_next);
751         
752         if (sfile->files) {
753                 ED_fileselect_clear(wm, sfile);
754                 MEM_freeN(sfile->files);
755                 sfile->files = NULL;
756         }
757
758 }