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