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