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