Merge branch 'master' into blender2.8
[blender.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                 /* set the default thumbnails size */
106                 sfile->params->thumbnail_size = 128;
107         }
108
109         params = sfile->params;
110
111         /* set the parameters from the operator, if it exists */
112         if (op) {
113                 PropertyRNA *prop;
114                 const bool is_files = (RNA_struct_find_property(op->ptr, "files") != NULL);
115                 const bool is_filepath = (RNA_struct_find_property(op->ptr, "filepath") != NULL);
116                 const bool is_filename = (RNA_struct_find_property(op->ptr, "filename") != NULL);
117                 const bool is_directory = (RNA_struct_find_property(op->ptr, "directory") != NULL);
118                 const bool is_relative_path = (RNA_struct_find_property(op->ptr, "relative_path") != NULL);
119
120                 BLI_strncpy_utf8(params->title, RNA_struct_ui_name(op->type->srna), sizeof(params->title));
121
122                 if ((prop = RNA_struct_find_property(op->ptr, "filemode"))) {
123                         params->type = RNA_property_int_get(op->ptr, prop);
124                 }
125                 else {
126                         params->type = FILE_SPECIAL;
127                 }
128
129                 if (is_filepath && RNA_struct_property_is_set_ex(op->ptr, "filepath", false)) {
130                         char name[FILE_MAX];
131                         RNA_string_get(op->ptr, "filepath", name);
132                         if (params->type == FILE_LOADLIB) {
133                                 BLI_strncpy(params->dir, name, sizeof(params->dir));
134                                 sfile->params->file[0] = '\0';
135                         }
136                         else {
137                                 BLI_split_dirfile(name, sfile->params->dir, sfile->params->file, sizeof(sfile->params->dir), sizeof(sfile->params->file));
138                         }
139                 }
140                 else {
141                         if (is_directory && RNA_struct_property_is_set_ex(op->ptr, "directory", false)) {
142                                 RNA_string_get(op->ptr, "directory", params->dir);
143                                 sfile->params->file[0] = '\0';
144                         }
145
146                         if (is_filename && RNA_struct_property_is_set_ex(op->ptr, "filename", false)) {
147                                 RNA_string_get(op->ptr, "filename", params->file);
148                         }
149                 }
150
151                 if (params->dir[0]) {
152                         BLI_cleanup_dir(G.main->name, params->dir);
153                         BLI_path_abs(params->dir, G.main->name);
154                 }
155
156                 if (is_directory == true && is_filename == false && is_filepath == false && is_files == false) {
157                         params->flag |= FILE_DIRSEL_ONLY;
158                 }
159                 else {
160                         params->flag &= ~FILE_DIRSEL_ONLY;
161                 }
162
163                 params->filter = 0;
164                 if ((prop = RNA_struct_find_property(op->ptr, "filter_blender")))
165                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_BLENDER : 0;
166                 if ((prop = RNA_struct_find_property(op->ptr, "filter_blenlib")))
167                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_BLENDERLIB : 0;
168                 if ((prop = RNA_struct_find_property(op->ptr, "filter_backup")))
169                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_BLENDER_BACKUP : 0;
170                 if ((prop = RNA_struct_find_property(op->ptr, "filter_image")))
171                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_IMAGE : 0;
172                 if ((prop = RNA_struct_find_property(op->ptr, "filter_movie")))
173                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_MOVIE : 0;
174                 if ((prop = RNA_struct_find_property(op->ptr, "filter_python")))
175                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_PYSCRIPT : 0;
176                 if ((prop = RNA_struct_find_property(op->ptr, "filter_font")))
177                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_FTFONT : 0;
178                 if ((prop = RNA_struct_find_property(op->ptr, "filter_sound")))
179                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_SOUND : 0;
180                 if ((prop = RNA_struct_find_property(op->ptr, "filter_text")))
181                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_TEXT : 0;
182                 if ((prop = RNA_struct_find_property(op->ptr, "filter_folder")))
183                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_FOLDER : 0;
184                 if ((prop = RNA_struct_find_property(op->ptr, "filter_btx")))
185                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_BTX : 0;
186                 if ((prop = RNA_struct_find_property(op->ptr, "filter_collada")))
187                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_COLLADA : 0;
188                 if ((prop = RNA_struct_find_property(op->ptr, "filter_alembic")))
189                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_ALEMBIC : 0;
190                 if ((prop = RNA_struct_find_property(op->ptr, "filter_glob"))) {
191                         /* Protection against pyscripts not setting proper size limit... */
192                         char *tmp = RNA_property_string_get_alloc(
193                                         op->ptr, prop, params->filter_glob, sizeof(params->filter_glob), NULL);
194                         if (tmp != params->filter_glob) {
195                                 BLI_strncpy(params->filter_glob, tmp, sizeof(params->filter_glob));
196                                 MEM_freeN(tmp);
197                         }
198                         params->filter |= (FILE_TYPE_OPERATOR | FILE_TYPE_FOLDER);
199                 }
200                 else {
201                         params->filter_glob[0] = '\0';
202                 }
203
204                 if (params->filter != 0) {
205                         if (U.uiflag & USER_FILTERFILEEXTS) {
206                                 params->flag |= FILE_FILTER;
207                         }
208                         else {
209                                 params->flag &= ~FILE_FILTER;
210                         }
211                 }
212
213                 /* For now, always init filterid to 'all true' */
214                 params->filter_id = FILTER_ID_AC | FILTER_ID_AR | FILTER_ID_BR | FILTER_ID_CA | FILTER_ID_CU | FILTER_ID_GD |
215                                     FILTER_ID_GR | FILTER_ID_IM | FILTER_ID_LA | FILTER_ID_LS | FILTER_ID_LT | FILTER_ID_MA |
216                                     FILTER_ID_MB | FILTER_ID_MC | FILTER_ID_ME | FILTER_ID_MSK | FILTER_ID_NT | FILTER_ID_OB |
217                                     FILTER_ID_PA | FILTER_ID_PAL | FILTER_ID_PC | FILTER_ID_SCE | FILTER_ID_SPK | FILTER_ID_SO |
218                                     FILTER_ID_TE | FILTER_ID_TXT | FILTER_ID_VF | FILTER_ID_WO | FILTER_ID_CF | FILTER_ID_WS |
219                                     FILTER_ID_LP;
220
221                 if (U.uiflag & USER_HIDE_DOT) {
222                         params->flag |= FILE_HIDE_DOT;
223                 }
224                 else {
225                         params->flag &= ~FILE_HIDE_DOT;
226                 }
227
228
229                 if (params->type == FILE_LOADLIB) {
230                         params->flag |= RNA_boolean_get(op->ptr, "link") ? FILE_LINK : 0;
231                         params->flag |= RNA_boolean_get(op->ptr, "autoselect") ? FILE_AUTOSELECT : 0;
232                         params->flag |= RNA_boolean_get(op->ptr, "active_collection") ? FILE_ACTIVE_COLLECTION : 0;
233                 }
234
235                 if ((prop = RNA_struct_find_property(op->ptr, "display_type"))) {
236                         params->display = RNA_property_enum_get(op->ptr, prop);
237                 }
238
239                 if ((prop = RNA_struct_find_property(op->ptr, "sort_method"))) {
240                         params->sort = RNA_property_enum_get(op->ptr, prop);
241                 }
242                 else {
243                         params->sort = FILE_SORT_ALPHA;
244                 }
245
246                 if (params->display == FILE_DEFAULTDISPLAY) {
247                         if (U.uiflag & USER_SHOW_THUMBNAILS) {
248                                 if (params->filter & (FILE_TYPE_IMAGE | FILE_TYPE_MOVIE | FILE_TYPE_FTFONT))
249                                         params->display = FILE_IMGDISPLAY;
250                                 else
251                                         params->display = FILE_SHORTDISPLAY;
252                         }
253                         else {
254                                 params->display = FILE_SHORTDISPLAY;
255                         }
256                 }
257
258                 if (is_relative_path) {
259                         if ((prop = RNA_struct_find_property(op->ptr, "relative_path"))) {
260                                 if (!RNA_property_is_set_ex(op->ptr, prop, false)) {
261                                         RNA_property_boolean_set(op->ptr, prop, (U.flag & USER_RELPATHS) != 0);
262                                 }
263                         }
264                 }
265         }
266         else {
267                 /* default values, if no operator */
268                 params->type = FILE_UNIX;
269                 params->flag |= FILE_HIDE_DOT;
270                 params->flag &= ~FILE_DIRSEL_ONLY;
271                 params->display = FILE_SHORTDISPLAY;
272                 params->sort = FILE_SORT_ALPHA;
273                 params->filter = 0;
274                 params->filter_glob[0] = '\0';
275         }
276
277         /* operator has no setting for this */
278         params->active_file = -1;
279
280
281         /* initialize the list with previous folders */
282         if (!sfile->folders_prev)
283                 sfile->folders_prev = folderlist_new();
284
285         if (!sfile->params->dir[0]) {
286                 if (G.main->name[0]) {
287                         BLI_split_dir_part(G.main->name, sfile->params->dir, sizeof(sfile->params->dir));
288                 }
289                 else {
290                         const char *doc_path = BKE_appdir_folder_default();
291                         if (doc_path) {
292                                 BLI_strncpy(sfile->params->dir, doc_path, sizeof(sfile->params->dir));
293                         }
294                 }
295         }
296
297         folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
298
299         /* switching thumbnails needs to recalc layout [#28809] */
300         if (sfile->layout) {
301                 sfile->layout->dirty = true;
302         }
303
304         return 1;
305 }
306
307 void ED_fileselect_reset_params(SpaceFile *sfile)
308 {
309         sfile->params->type = FILE_UNIX;
310         sfile->params->flag = 0;
311         sfile->params->title[0] = '\0';
312         sfile->params->active_file = -1;
313 }
314
315 /**
316  * Sets FileSelectParams->file (name of selected file)
317  */
318 void fileselect_file_set(SpaceFile *sfile, const int index)
319 {
320         const struct FileDirEntry *file = filelist_file(sfile->files, index);
321         if (file && file->relpath && file->relpath[0] && !(file->typeflag & FILE_TYPE_FOLDER)) {
322                 BLI_strncpy(sfile->params->file, file->relpath, FILE_MAXFILE);
323         }
324 }
325
326 int ED_fileselect_layout_numfiles(FileLayout *layout, ARegion *ar)
327 {
328         int numfiles;
329
330         /* Values in pixels.
331          *
332          * - *_item: size of each (row|col), (including padding)
333          * - *_view: (x|y) size of the view.
334          * - *_over: extra pixels, to take into account, when the fit isnt exact
335          *   (needed since you may see the end of the previous column and the beginning of the next).
336          *
337          * Could be more clever and take scrolling into account,
338          * but for now don't bother.
339          */
340         if (layout->flag & FILE_LAYOUT_HOR) {
341                 const int x_item = layout->tile_w + (2 * layout->tile_border_x);
342                 const int x_view = (int)(BLI_rctf_size_x(&ar->v2d.cur));
343                 const int x_over = x_item - (x_view % x_item);
344                 numfiles = (int)((float)(x_view + x_over) / (float)(x_item));
345                 return numfiles * layout->rows;
346         }
347         else {
348                 const int y_item = layout->tile_h + (2 * layout->tile_border_y);
349                 const int y_view = (int)(BLI_rctf_size_y(&ar->v2d.cur));
350                 const int y_over = y_item - (y_view % y_item);
351                 numfiles = (int)((float)(y_view + y_over) / (float)(y_item));
352                 return numfiles * layout->columns;
353         }
354 }
355
356 static bool is_inside(int x, int y, int cols, int rows)
357 {
358         return ((x >= 0) && (x < cols) && (y >= 0) && (y < rows));
359 }
360
361 FileSelection ED_fileselect_layout_offset_rect(FileLayout *layout, const rcti *rect)
362 {
363         int colmin, colmax, rowmin, rowmax;
364         FileSelection sel;
365         sel.first = sel.last = -1;
366
367         if (layout == NULL)
368                 return sel;
369
370         colmin = (rect->xmin) / (layout->tile_w + 2 * layout->tile_border_x);
371         rowmin = (rect->ymin) / (layout->tile_h + 2 * layout->tile_border_y);
372         colmax = (rect->xmax) / (layout->tile_w + 2 * layout->tile_border_x);
373         rowmax = (rect->ymax) / (layout->tile_h + 2 * layout->tile_border_y);
374
375         if (is_inside(colmin, rowmin, layout->columns, layout->rows) ||
376             is_inside(colmax, rowmax, layout->columns, layout->rows) )
377         {
378                 CLAMP(colmin, 0, layout->columns - 1);
379                 CLAMP(rowmin, 0, layout->rows - 1);
380                 CLAMP(colmax, 0, layout->columns - 1);
381                 CLAMP(rowmax, 0, layout->rows - 1);
382         }
383
384         if ((colmin > layout->columns - 1) || (rowmin > layout->rows - 1)) {
385                 sel.first = -1;
386         }
387         else {
388                 if (layout->flag & FILE_LAYOUT_HOR)
389                         sel.first = layout->rows * colmin + rowmin;
390                 else
391                         sel.first = colmin + layout->columns * rowmin;
392         }
393         if ((colmax > layout->columns - 1) || (rowmax > layout->rows - 1)) {
394                 sel.last = -1;
395         }
396         else {
397                 if (layout->flag & FILE_LAYOUT_HOR)
398                         sel.last = layout->rows * colmax + rowmax;
399                 else
400                         sel.last = colmax + layout->columns * rowmax;
401         }
402
403         return sel;
404 }
405
406 int ED_fileselect_layout_offset(FileLayout *layout, int x, int y)
407 {
408         int offsetx, offsety;
409         int active_file;
410
411         if (layout == NULL)
412                 return -1;
413
414         offsetx = (x) / (layout->tile_w + 2 * layout->tile_border_x);
415         offsety = (y) / (layout->tile_h + 2 * layout->tile_border_y);
416
417         if (offsetx > layout->columns - 1) return -1;
418         if (offsety > layout->rows - 1) return -1;
419
420         if (layout->flag & FILE_LAYOUT_HOR)
421                 active_file = layout->rows * offsetx + offsety;
422         else
423                 active_file = offsetx + layout->columns * offsety;
424         return active_file;
425 }
426
427 void ED_fileselect_layout_tilepos(FileLayout *layout, int tile, int *x, int *y)
428 {
429         if (layout->flag == FILE_LAYOUT_HOR) {
430                 *x = layout->tile_border_x + (tile / layout->rows) * (layout->tile_w + 2 * layout->tile_border_x);
431                 *y = layout->tile_border_y + (tile % layout->rows) * (layout->tile_h + 2 * layout->tile_border_y);
432         }
433         else {
434                 *x = layout->tile_border_x + ((tile) % layout->columns) * (layout->tile_w + 2 * layout->tile_border_x);
435                 *y = layout->tile_border_y + ((tile) / layout->columns) * (layout->tile_h + 2 * layout->tile_border_y);
436         }
437 }
438
439 float file_string_width(const char *str)
440 {
441         uiStyle *style = UI_style_get();
442         float width;
443
444         UI_fontstyle_set(&style->widget);
445         if (style->widget.kerning == 1) {  /* for BLF_width */
446                 BLF_enable(style->widget.uifont_id, BLF_KERNING_DEFAULT);
447         }
448
449         width = BLF_width(style->widget.uifont_id, str, BLF_DRAW_STR_DUMMY_MAX);
450
451         if (style->widget.kerning == 1) {
452                 BLF_disable(style->widget.uifont_id, BLF_KERNING_DEFAULT);
453         }
454
455         return width;
456 }
457
458 float file_font_pointsize(void)
459 {
460 #if 0
461         float s;
462         char tmp[2] = "X";
463         uiStyle *style = UI_style_get();
464         UI_fontstyle_set(&style->widget);
465         s = BLF_height(style->widget.uifont_id, tmp);
466         return style->widget.points;
467 #else
468         uiStyle *style = UI_style_get();
469         UI_fontstyle_set(&style->widget);
470         return style->widget.points * UI_DPI_FAC;
471 #endif
472 }
473
474 static void column_widths(FileSelectParams *params, struct FileLayout *layout)
475 {
476         int i;
477         const bool small_size = SMALL_SIZE_CHECK(params->thumbnail_size);
478
479         for (i = 0; i < MAX_FILE_COLUMN; ++i) {
480                 layout->column_widths[i] = 0;
481         }
482
483         layout->column_widths[COLUMN_NAME] = ((float)params->thumbnail_size / 8.0f) * UI_UNIT_X;
484         /* Biggest possible reasonable values... */
485         layout->column_widths[COLUMN_DATE] = file_string_width(small_size ? "23/08/89" : "23-Dec-89");
486         layout->column_widths[COLUMN_TIME] = file_string_width("23:59");
487         layout->column_widths[COLUMN_SIZE] = file_string_width(small_size ? "98.7 M" : "98.7 MiB");
488 }
489
490 void ED_fileselect_init_layout(struct SpaceFile *sfile, ARegion *ar)
491 {
492         FileSelectParams *params = ED_fileselect_get_params(sfile);
493         FileLayout *layout = NULL;
494         View2D *v2d = &ar->v2d;
495         int maxlen = 0;
496         int numfiles;
497         int textheight;
498
499         if (sfile->layout == NULL) {
500                 sfile->layout = MEM_callocN(sizeof(struct FileLayout), "file_layout");
501                 sfile->layout->dirty = true;
502         }
503         else if (sfile->layout->dirty == false) {
504                 return;
505         }
506
507         numfiles = filelist_files_ensure(sfile->files);
508         textheight = (int)file_font_pointsize();
509         layout = sfile->layout;
510         layout->textheight = textheight;
511
512         if (params->display == FILE_IMGDISPLAY) {
513                 layout->prv_w = ((float)params->thumbnail_size / 20.0f) * UI_UNIT_X;
514                 layout->prv_h = ((float)params->thumbnail_size / 20.0f) * UI_UNIT_Y;
515                 layout->tile_border_x = 0.3f * UI_UNIT_X;
516                 layout->tile_border_y = 0.3f * UI_UNIT_X;
517                 layout->prv_border_x = 0.3f * UI_UNIT_X;
518                 layout->prv_border_y = 0.3f * UI_UNIT_Y;
519                 layout->tile_w = layout->prv_w + 2 * layout->prv_border_x;
520                 layout->tile_h = layout->prv_h + 2 * layout->prv_border_y + textheight;
521                 layout->width = (int)(BLI_rctf_size_x(&v2d->cur) - 2 * layout->tile_border_x);
522                 layout->columns = layout->width / (layout->tile_w + 2 * layout->tile_border_x);
523                 if (layout->columns > 0)
524                         layout->rows = numfiles / layout->columns + 1;  // XXX dirty, modulo is zero
525                 else {
526                         layout->columns = 1;
527                         layout->rows = numfiles + 1; // XXX dirty, modulo is zero
528                 }
529                 layout->height = sfile->layout->rows * (layout->tile_h + 2 * layout->tile_border_y) + layout->tile_border_y * 2;
530                 layout->flag = FILE_LAYOUT_VER;
531         }
532         else {
533                 int column_space = 0.6f * UI_UNIT_X;
534                 int column_icon_space = 0.2f * UI_UNIT_X;
535
536                 layout->prv_w = 0;
537                 layout->prv_h = 0;
538                 layout->tile_border_x = 0.4f * UI_UNIT_X;
539                 layout->tile_border_y = 0.1f * UI_UNIT_Y;
540                 layout->prv_border_x = 0;
541                 layout->prv_border_y = 0;
542                 layout->tile_h = textheight * 3 / 2;
543                 layout->height = (int)(BLI_rctf_size_y(&v2d->cur) - 2 * layout->tile_border_y);
544                 layout->rows = layout->height / (layout->tile_h + 2 * layout->tile_border_y);
545
546                 column_widths(params, layout);
547
548                 if (params->display == FILE_SHORTDISPLAY) {
549                         maxlen = ICON_DEFAULT_WIDTH_SCALE + column_icon_space +
550                                  (int)layout->column_widths[COLUMN_NAME] + column_space +
551                                  (int)layout->column_widths[COLUMN_SIZE] + column_space;
552                 }
553                 else {
554                         maxlen = ICON_DEFAULT_WIDTH_SCALE + column_icon_space +
555                                  (int)layout->column_widths[COLUMN_NAME] + column_space +
556                                  (int)layout->column_widths[COLUMN_DATE] + column_space +
557                                  (int)layout->column_widths[COLUMN_TIME] + column_space +
558                                  (int)layout->column_widths[COLUMN_SIZE] + column_space;
559
560                 }
561                 layout->tile_w = maxlen;
562                 if (layout->rows > 0)
563                         layout->columns = numfiles / layout->rows + 1;  // XXX dirty, modulo is zero
564                 else {
565                         layout->rows = 1;
566                         layout->columns = numfiles + 1; // XXX dirty, modulo is zero
567                 }
568                 layout->width = sfile->layout->columns * (layout->tile_w + 2 * layout->tile_border_x) + layout->tile_border_x * 2;
569                 layout->flag = FILE_LAYOUT_HOR;
570         }
571         layout->dirty = false;
572 }
573
574 FileLayout *ED_fileselect_get_layout(struct SpaceFile *sfile, ARegion *ar)
575 {
576         if (!sfile->layout) {
577                 ED_fileselect_init_layout(sfile, ar);
578         }
579         return sfile->layout;
580 }
581
582 void ED_file_change_dir(bContext *C)
583 {
584         wmWindowManager *wm = CTX_wm_manager(C);
585         SpaceFile *sfile = CTX_wm_space_file(C);
586         ScrArea *sa = CTX_wm_area(C);
587
588         if (sfile->params) {
589                 ED_fileselect_clear(wm, sa, sfile);
590
591                 /* Clear search string, it is very rare to want to keep that filter while changing dir,
592                  * and usually very annoying to keep it actually! */
593                 sfile->params->filter_search[0] = '\0';
594                 sfile->params->active_file = -1;
595
596                 if (!filelist_is_dir(sfile->files, sfile->params->dir)) {
597                         BLI_strncpy(sfile->params->dir, filelist_dir(sfile->files), sizeof(sfile->params->dir));
598                         /* could return but just refresh the current dir */
599                 }
600                 filelist_setdir(sfile->files, sfile->params->dir);
601
602                 if (folderlist_clear_next(sfile))
603                         folderlist_free(sfile->folders_next);
604
605                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
606
607                 file_draw_check(C);
608         }
609 }
610
611 int file_select_match(struct SpaceFile *sfile, const char *pattern, char *matched_file)
612 {
613         int match = 0;
614
615         int i;
616         FileDirEntry *file;
617         int n = filelist_files_ensure(sfile->files);
618
619         /* select any file that matches the pattern, this includes exact match
620          * if the user selects a single file by entering the filename
621          */
622         for (i = 0; i < n; i++) {
623                 file = filelist_file(sfile->files, i);
624                 /* Do not check whether file is a file or dir here! Causes T44243 (we do accept dirs at this stage). */
625                 if (fnmatch(pattern, file->relpath, 0) == 0) {
626                         filelist_entry_select_set(sfile->files, file, FILE_SEL_ADD, FILE_SEL_SELECTED, CHECK_ALL);
627                         if (!match) {
628                                 BLI_strncpy(matched_file, file->relpath, FILE_MAX);
629                         }
630                         match++;
631                 }
632         }
633
634         return match;
635 }
636
637 int autocomplete_directory(struct bContext *C, char *str, void *UNUSED(arg_v))
638 {
639         SpaceFile *sfile = CTX_wm_space_file(C);
640         int match = AUTOCOMPLETE_NO_MATCH;
641
642         /* search if str matches the beginning of name */
643         if (str[0] && sfile->files) {
644                 char dirname[FILE_MAX];
645
646                 DIR *dir;
647                 struct dirent *de;
648
649                 BLI_split_dir_part(str, dirname, sizeof(dirname));
650
651                 dir = opendir(dirname);
652
653                 if (dir) {
654                         AutoComplete *autocpl = UI_autocomplete_begin(str, FILE_MAX);
655
656                         while ((de = readdir(dir)) != NULL) {
657                                 if (FILENAME_IS_CURRPAR(de->d_name)) {
658                                         /* pass */
659                                 }
660                                 else {
661                                         char path[FILE_MAX];
662                                         BLI_stat_t status;
663
664                                         BLI_join_dirfile(path, sizeof(path), dirname, de->d_name);
665
666                                         if (BLI_stat(path, &status) == 0) {
667                                                 if (S_ISDIR(status.st_mode)) { /* is subdir */
668                                                         UI_autocomplete_update_name(autocpl, path);
669                                                 }
670                                         }
671                                 }
672                         }
673                         closedir(dir);
674
675                         match = UI_autocomplete_end(autocpl, str);
676                         if (match == AUTOCOMPLETE_FULL_MATCH) {
677                                 BLI_add_slash(str);
678                         }
679                 }
680         }
681
682         return match;
683 }
684
685 int autocomplete_file(struct bContext *C, char *str, void *UNUSED(arg_v))
686 {
687         SpaceFile *sfile = CTX_wm_space_file(C);
688         int match = AUTOCOMPLETE_NO_MATCH;
689
690         /* search if str matches the beginning of name */
691         if (str[0] && sfile->files) {
692                 AutoComplete *autocpl = UI_autocomplete_begin(str, FILE_MAX);
693                 int nentries = filelist_files_ensure(sfile->files);
694                 int i;
695
696                 for (i = 0; i < nentries; ++i) {
697                         FileDirEntry *file = filelist_file(sfile->files, i);
698                         UI_autocomplete_update_name(autocpl, file->relpath);
699                 }
700                 match = UI_autocomplete_end(autocpl, str);
701         }
702
703         return match;
704 }
705
706 void ED_fileselect_clear(wmWindowManager *wm, ScrArea *sa, SpaceFile *sfile)
707 {
708         /* only NULL in rare cases - [#29734] */
709         if (sfile->files) {
710                 filelist_readjob_stop(wm, sa);
711                 filelist_freelib(sfile->files);
712                 filelist_clear(sfile->files);
713         }
714
715         sfile->params->highlight_file = -1;
716         WM_main_add_notifier(NC_SPACE | ND_SPACE_FILE_LIST, NULL);
717 }
718
719 void ED_fileselect_exit(wmWindowManager *wm, ScrArea *sa, SpaceFile *sfile)
720 {
721         if (!sfile) return;
722         if (sfile->op) {
723                 WM_event_fileselect_event(wm, sfile->op, EVT_FILESELECT_EXTERNAL_CANCEL);
724                 sfile->op = NULL;
725         }
726
727         folderlist_free(sfile->folders_prev);
728         folderlist_free(sfile->folders_next);
729
730         if (sfile->files) {
731                 ED_fileselect_clear(wm, sa, sfile);
732                 filelist_free(sfile->files);
733                 MEM_freeN(sfile->files);
734                 sfile->files = NULL;
735         }
736
737 }