Cleanup: remove redundant, invalid info from headers
[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 blender/editors/space_file/filesel.c
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_fileops_types.h"
52 #include "BLI_fnmatch.h"
53
54 #include "BKE_appdir.h"
55 #include "BKE_context.h"
56 #include "BKE_global.h"
57 #include "BKE_main.h"
58
59 #include "BLF_api.h"
60
61
62 #include "ED_fileselect.h"
63
64 #include "WM_api.h"
65 #include "WM_types.h"
66
67
68 #include "RNA_access.h"
69
70 #include "UI_interface.h"
71 #include "UI_interface_icons.h"
72
73 #include "file_intern.h"
74 #include "filelist.h"
75
76 FileSelectParams *ED_fileselect_get_params(struct SpaceFile *sfile)
77 {
78         if (!sfile->params) {
79                 ED_fileselect_set_params(sfile);
80         }
81         return sfile->params;
82 }
83
84 /**
85  * \note RNA_struct_property_is_set_ex is used here because we want
86  *       the previously used settings to be used here rather then overriding them */
87 short ED_fileselect_set_params(SpaceFile *sfile)
88 {
89         FileSelectParams *params;
90         wmOperator *op = sfile->op;
91
92         const char *blendfile_path = BKE_main_blendfile_path_from_global();
93
94         /* create new parameters if necessary */
95         if (!sfile->params) {
96                 sfile->params = MEM_callocN(sizeof(FileSelectParams), "fileselparams");
97                 /* set path to most recently opened .blend */
98                 BLI_split_dirfile(blendfile_path, sfile->params->dir, sfile->params->file, sizeof(sfile->params->dir), sizeof(sfile->params->file));
99                 sfile->params->filter_glob[0] = '\0';
100                 /* set the default thumbnails size */
101                 sfile->params->thumbnail_size = 128;
102         }
103
104         params = sfile->params;
105
106         /* set the parameters from the operator, if it exists */
107         if (op) {
108                 PropertyRNA *prop;
109                 const bool is_files = (RNA_struct_find_property(op->ptr, "files") != NULL);
110                 const bool is_filepath = (RNA_struct_find_property(op->ptr, "filepath") != NULL);
111                 const bool is_filename = (RNA_struct_find_property(op->ptr, "filename") != NULL);
112                 const bool is_directory = (RNA_struct_find_property(op->ptr, "directory") != NULL);
113                 const bool is_relative_path = (RNA_struct_find_property(op->ptr, "relative_path") != NULL);
114
115                 BLI_strncpy_utf8(params->title, RNA_struct_ui_name(op->type->srna), sizeof(params->title));
116
117                 if ((prop = RNA_struct_find_property(op->ptr, "filemode"))) {
118                         params->type = RNA_property_int_get(op->ptr, prop);
119                 }
120                 else {
121                         params->type = FILE_SPECIAL;
122                 }
123
124                 if (is_filepath && RNA_struct_property_is_set_ex(op->ptr, "filepath", false)) {
125                         char name[FILE_MAX];
126                         RNA_string_get(op->ptr, "filepath", name);
127                         if (params->type == FILE_LOADLIB) {
128                                 BLI_strncpy(params->dir, name, sizeof(params->dir));
129                                 sfile->params->file[0] = '\0';
130                         }
131                         else {
132                                 BLI_split_dirfile(name, sfile->params->dir, sfile->params->file, sizeof(sfile->params->dir), sizeof(sfile->params->file));
133                         }
134                 }
135                 else {
136                         if (is_directory && RNA_struct_property_is_set_ex(op->ptr, "directory", false)) {
137                                 RNA_string_get(op->ptr, "directory", params->dir);
138                                 sfile->params->file[0] = '\0';
139                         }
140
141                         if (is_filename && RNA_struct_property_is_set_ex(op->ptr, "filename", false)) {
142                                 RNA_string_get(op->ptr, "filename", params->file);
143                         }
144                 }
145
146                 if (params->dir[0]) {
147                         BLI_cleanup_dir(blendfile_path, params->dir);
148                         BLI_path_abs(params->dir, blendfile_path);
149                 }
150
151                 if (is_directory == true && is_filename == false && is_filepath == false && is_files == false) {
152                         params->flag |= FILE_DIRSEL_ONLY;
153                 }
154                 else {
155                         params->flag &= ~FILE_DIRSEL_ONLY;
156                 }
157
158                 params->filter = 0;
159                 if ((prop = RNA_struct_find_property(op->ptr, "filter_blender")))
160                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_BLENDER : 0;
161                 if ((prop = RNA_struct_find_property(op->ptr, "filter_blenlib")))
162                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_BLENDERLIB : 0;
163                 if ((prop = RNA_struct_find_property(op->ptr, "filter_backup")))
164                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_BLENDER_BACKUP : 0;
165                 if ((prop = RNA_struct_find_property(op->ptr, "filter_image")))
166                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_IMAGE : 0;
167                 if ((prop = RNA_struct_find_property(op->ptr, "filter_movie")))
168                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_MOVIE : 0;
169                 if ((prop = RNA_struct_find_property(op->ptr, "filter_python")))
170                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_PYSCRIPT : 0;
171                 if ((prop = RNA_struct_find_property(op->ptr, "filter_font")))
172                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_FTFONT : 0;
173                 if ((prop = RNA_struct_find_property(op->ptr, "filter_sound")))
174                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_SOUND : 0;
175                 if ((prop = RNA_struct_find_property(op->ptr, "filter_text")))
176                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_TEXT : 0;
177                 if ((prop = RNA_struct_find_property(op->ptr, "filter_folder")))
178                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_FOLDER : 0;
179                 if ((prop = RNA_struct_find_property(op->ptr, "filter_btx")))
180                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_BTX : 0;
181                 if ((prop = RNA_struct_find_property(op->ptr, "filter_collada")))
182                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_COLLADA : 0;
183                 if ((prop = RNA_struct_find_property(op->ptr, "filter_alembic")))
184                         params->filter |= RNA_property_boolean_get(op->ptr, prop) ? FILE_TYPE_ALEMBIC : 0;
185                 if ((prop = RNA_struct_find_property(op->ptr, "filter_glob"))) {
186                         /* Protection against pyscripts not setting proper size limit... */
187                         char *tmp = RNA_property_string_get_alloc(
188                                         op->ptr, prop, params->filter_glob, sizeof(params->filter_glob), NULL);
189                         if (tmp != params->filter_glob) {
190                                 BLI_strncpy(params->filter_glob, tmp, sizeof(params->filter_glob));
191                                 MEM_freeN(tmp);
192
193                                 /* Fix stupid things that truncating might have generated,
194                                  * like last group being a 'match everything' wildcard-only one... */
195                                 BLI_path_extension_glob_validate(params->filter_glob);
196                         }
197                         params->filter |= (FILE_TYPE_OPERATOR | FILE_TYPE_FOLDER);
198                 }
199                 else {
200                         params->filter_glob[0] = '\0';
201                 }
202
203                 if (params->filter != 0) {
204                         if (U.uiflag & USER_FILTERFILEEXTS) {
205                                 params->flag |= FILE_FILTER;
206                         }
207                         else {
208                                 params->flag &= ~FILE_FILTER;
209                         }
210                 }
211
212                 /* For now, always init filterid to 'all true' */
213                 params->filter_id = FILTER_ID_AC | FILTER_ID_AR | FILTER_ID_BR | FILTER_ID_CA | FILTER_ID_CU | FILTER_ID_GD |
214                                     FILTER_ID_GR | FILTER_ID_IM | FILTER_ID_LA | FILTER_ID_LS | FILTER_ID_LT | FILTER_ID_MA |
215                                     FILTER_ID_MB | FILTER_ID_MC | FILTER_ID_ME | FILTER_ID_MSK | FILTER_ID_NT | FILTER_ID_OB |
216                                     FILTER_ID_PA | FILTER_ID_PAL | FILTER_ID_PC | FILTER_ID_SCE | FILTER_ID_SPK | FILTER_ID_SO |
217                                     FILTER_ID_TE | FILTER_ID_TXT | FILTER_ID_VF | FILTER_ID_WO | FILTER_ID_CF;
218
219                 if (U.uiflag & USER_HIDE_DOT) {
220                         params->flag |= FILE_HIDE_DOT;
221                 }
222                 else {
223                         params->flag &= ~FILE_HIDE_DOT;
224                 }
225
226
227                 if (params->type == FILE_LOADLIB) {
228                         params->flag |= RNA_boolean_get(op->ptr, "link") ? FILE_LINK : 0;
229                         params->flag |= RNA_boolean_get(op->ptr, "autoselect") ? FILE_AUTOSELECT : 0;
230                         params->flag |= RNA_boolean_get(op->ptr, "active_layer") ? FILE_ACTIVELAY : 0;
231                 }
232
233                 if ((prop = RNA_struct_find_property(op->ptr, "display_type"))) {
234                         params->display = RNA_property_enum_get(op->ptr, prop);
235                 }
236
237                 if ((prop = RNA_struct_find_property(op->ptr, "sort_method"))) {
238                         params->sort = RNA_property_enum_get(op->ptr, prop);
239                 }
240                 else {
241                         params->sort = FILE_SORT_ALPHA;
242                 }
243
244                 if (params->display == FILE_DEFAULTDISPLAY) {
245                         if (U.uiflag & USER_SHOW_THUMBNAILS) {
246                                 if (params->filter & (FILE_TYPE_IMAGE | FILE_TYPE_MOVIE | FILE_TYPE_FTFONT))
247                                         params->display = FILE_IMGDISPLAY;
248                                 else
249                                         params->display = FILE_SHORTDISPLAY;
250                         }
251                         else {
252                                 params->display = FILE_SHORTDISPLAY;
253                         }
254                 }
255
256                 if (is_relative_path) {
257                         if ((prop = RNA_struct_find_property(op->ptr, "relative_path"))) {
258                                 if (!RNA_property_is_set_ex(op->ptr, prop, false)) {
259                                         RNA_property_boolean_set(op->ptr, prop, (U.flag & USER_RELPATHS) != 0);
260                                 }
261                         }
262                 }
263         }
264         else {
265                 /* default values, if no operator */
266                 params->type = FILE_UNIX;
267                 params->flag |= FILE_HIDE_DOT;
268                 params->flag &= ~FILE_DIRSEL_ONLY;
269                 params->display = FILE_SHORTDISPLAY;
270                 params->sort = FILE_SORT_ALPHA;
271                 params->filter = 0;
272                 params->filter_glob[0] = '\0';
273         }
274
275         /* operator has no setting for this */
276         params->active_file = -1;
277
278
279         /* initialize the list with previous folders */
280         if (!sfile->folders_prev)
281                 sfile->folders_prev = folderlist_new();
282
283         if (!sfile->params->dir[0]) {
284                 if (blendfile_path[0] != '\0') {
285                         BLI_split_dir_part(blendfile_path, sfile->params->dir, sizeof(sfile->params->dir));
286                 }
287                 else {
288                         const char *doc_path = BKE_appdir_folder_default();
289                         if (doc_path) {
290                                 BLI_strncpy(sfile->params->dir, doc_path, sizeof(sfile->params->dir));
291                         }
292                 }
293         }
294
295         folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
296
297         /* switching thumbnails needs to recalc layout [#28809] */
298         if (sfile->layout) {
299                 sfile->layout->dirty = true;
300         }
301
302         return 1;
303 }
304
305 void ED_fileselect_reset_params(SpaceFile *sfile)
306 {
307         sfile->params->type = FILE_UNIX;
308         sfile->params->flag = 0;
309         sfile->params->title[0] = '\0';
310         sfile->params->active_file = -1;
311 }
312
313 /**
314  * Sets FileSelectParams->file (name of selected file)
315  */
316 void fileselect_file_set(SpaceFile *sfile, const int index)
317 {
318         const struct FileDirEntry *file = filelist_file(sfile->files, index);
319         if (file && file->relpath && file->relpath[0] && !(file->typeflag & FILE_TYPE_FOLDER)) {
320                 BLI_strncpy(sfile->params->file, file->relpath, FILE_MAXFILE);
321         }
322 }
323
324 int ED_fileselect_layout_numfiles(FileLayout *layout, ARegion *ar)
325 {
326         int numfiles;
327
328         /* Values in pixels.
329          *
330          * - *_item: size of each (row|col), (including padding)
331          * - *_view: (x|y) size of the view.
332          * - *_over: extra pixels, to take into account, when the fit isnt exact
333          *   (needed since you may see the end of the previous column and the beginning of the next).
334          *
335          * Could be more clever and take scrolling into account,
336          * but for now don't bother.
337          */
338         if (layout->flag & FILE_LAYOUT_HOR) {
339                 const int x_item = layout->tile_w + (2 * layout->tile_border_x);
340                 const int x_view = (int)(BLI_rctf_size_x(&ar->v2d.cur));
341                 const int x_over = x_item - (x_view % x_item);
342                 numfiles = (int)((float)(x_view + x_over) / (float)(x_item));
343                 return numfiles * layout->rows;
344         }
345         else {
346                 const int y_item = layout->tile_h + (2 * layout->tile_border_y);
347                 const int y_view = (int)(BLI_rctf_size_y(&ar->v2d.cur));
348                 const int y_over = y_item - (y_view % y_item);
349                 numfiles = (int)((float)(y_view + y_over) / (float)(y_item));
350                 return numfiles * layout->columns;
351         }
352 }
353
354 static bool is_inside(int x, int y, int cols, int rows)
355 {
356         return ((x >= 0) && (x < cols) && (y >= 0) && (y < rows));
357 }
358
359 FileSelection ED_fileselect_layout_offset_rect(FileLayout *layout, const rcti *rect)
360 {
361         int colmin, colmax, rowmin, rowmax;
362         FileSelection sel;
363         sel.first = sel.last = -1;
364
365         if (layout == NULL)
366                 return sel;
367
368         colmin = (rect->xmin) / (layout->tile_w + 2 * layout->tile_border_x);
369         rowmin = (rect->ymin) / (layout->tile_h + 2 * layout->tile_border_y);
370         colmax = (rect->xmax) / (layout->tile_w + 2 * layout->tile_border_x);
371         rowmax = (rect->ymax) / (layout->tile_h + 2 * layout->tile_border_y);
372
373         if (is_inside(colmin, rowmin, layout->columns, layout->rows) ||
374             is_inside(colmax, rowmax, layout->columns, layout->rows) )
375         {
376                 CLAMP(colmin, 0, layout->columns - 1);
377                 CLAMP(rowmin, 0, layout->rows - 1);
378                 CLAMP(colmax, 0, layout->columns - 1);
379                 CLAMP(rowmax, 0, layout->rows - 1);
380         }
381
382         if ((colmin > layout->columns - 1) || (rowmin > layout->rows - 1)) {
383                 sel.first = -1;
384         }
385         else {
386                 if (layout->flag & FILE_LAYOUT_HOR)
387                         sel.first = layout->rows * colmin + rowmin;
388                 else
389                         sel.first = colmin + layout->columns * rowmin;
390         }
391         if ((colmax > layout->columns - 1) || (rowmax > layout->rows - 1)) {
392                 sel.last = -1;
393         }
394         else {
395                 if (layout->flag & FILE_LAYOUT_HOR)
396                         sel.last = layout->rows * colmax + rowmax;
397                 else
398                         sel.last = colmax + layout->columns * rowmax;
399         }
400
401         return sel;
402 }
403
404 int ED_fileselect_layout_offset(FileLayout *layout, int x, int y)
405 {
406         int offsetx, offsety;
407         int active_file;
408
409         if (layout == NULL)
410                 return -1;
411
412         offsetx = (x) / (layout->tile_w + 2 * layout->tile_border_x);
413         offsety = (y) / (layout->tile_h + 2 * layout->tile_border_y);
414
415         if (offsetx > layout->columns - 1) return -1;
416         if (offsety > layout->rows - 1) return -1;
417
418         if (layout->flag & FILE_LAYOUT_HOR)
419                 active_file = layout->rows * offsetx + offsety;
420         else
421                 active_file = offsetx + layout->columns * offsety;
422         return active_file;
423 }
424
425 void ED_fileselect_layout_tilepos(FileLayout *layout, int tile, int *x, int *y)
426 {
427         if (layout->flag == FILE_LAYOUT_HOR) {
428                 *x = layout->tile_border_x + (tile / layout->rows) * (layout->tile_w + 2 * layout->tile_border_x);
429                 *y = layout->tile_border_y + (tile % layout->rows) * (layout->tile_h + 2 * layout->tile_border_y);
430         }
431         else {
432                 *x = layout->tile_border_x + ((tile) % layout->columns) * (layout->tile_w + 2 * layout->tile_border_x);
433                 *y = layout->tile_border_y + ((tile) / layout->columns) * (layout->tile_h + 2 * layout->tile_border_y);
434         }
435 }
436
437 float file_string_width(const char *str)
438 {
439         uiStyle *style = UI_style_get();
440         float width;
441
442         UI_fontstyle_set(&style->widget);
443         if (style->widget.kerning == 1) {  /* for BLF_width */
444                 BLF_enable(style->widget.uifont_id, BLF_KERNING_DEFAULT);
445         }
446
447         width = BLF_width(style->widget.uifont_id, str, BLF_DRAW_STR_DUMMY_MAX);
448
449         if (style->widget.kerning == 1) {
450                 BLF_disable(style->widget.uifont_id, BLF_KERNING_DEFAULT);
451         }
452
453         return width;
454 }
455
456 float file_font_pointsize(void)
457 {
458 #if 0
459         float s;
460         char tmp[2] = "X";
461         uiStyle *style = UI_style_get();
462         UI_fontstyle_set(&style->widget);
463         s = BLF_height(style->widget.uifont_id, tmp);
464         return style->widget.points;
465 #else
466         uiStyle *style = UI_style_get();
467         UI_fontstyle_set(&style->widget);
468         return style->widget.points * UI_DPI_FAC;
469 #endif
470 }
471
472 static void column_widths(FileSelectParams *params, struct FileLayout *layout)
473 {
474         int i;
475         const bool small_size = SMALL_SIZE_CHECK(params->thumbnail_size);
476
477         for (i = 0; i < MAX_FILE_COLUMN; ++i) {
478                 layout->column_widths[i] = 0;
479         }
480
481         layout->column_widths[COLUMN_NAME] = ((float)params->thumbnail_size / 8.0f) * UI_UNIT_X;
482         /* Biggest possible reasonable values... */
483         layout->column_widths[COLUMN_DATE] = file_string_width(small_size ? "23/08/89" : "23-Dec-89");
484         layout->column_widths[COLUMN_TIME] = file_string_width("23:59");
485         layout->column_widths[COLUMN_SIZE] = file_string_width(small_size ? "98.7 M" : "98.7 MiB");
486 }
487
488 void ED_fileselect_init_layout(struct SpaceFile *sfile, ARegion *ar)
489 {
490         FileSelectParams *params = ED_fileselect_get_params(sfile);
491         FileLayout *layout = NULL;
492         View2D *v2d = &ar->v2d;
493         int maxlen = 0;
494         int numfiles;
495         int textheight;
496
497         if (sfile->layout == NULL) {
498                 sfile->layout = MEM_callocN(sizeof(struct FileLayout), "file_layout");
499                 sfile->layout->dirty = true;
500         }
501         else if (sfile->layout->dirty == false) {
502                 return;
503         }
504
505         numfiles = filelist_files_ensure(sfile->files);
506         textheight = (int)file_font_pointsize();
507         layout = sfile->layout;
508         layout->textheight = textheight;
509
510         if (params->display == FILE_IMGDISPLAY) {
511                 layout->prv_w = ((float)params->thumbnail_size / 20.0f) * UI_UNIT_X;
512                 layout->prv_h = ((float)params->thumbnail_size / 20.0f) * UI_UNIT_Y;
513                 layout->tile_border_x = 0.3f * UI_UNIT_X;
514                 layout->tile_border_y = 0.3f * UI_UNIT_X;
515                 layout->prv_border_x = 0.3f * UI_UNIT_X;
516                 layout->prv_border_y = 0.3f * UI_UNIT_Y;
517                 layout->tile_w = layout->prv_w + 2 * layout->prv_border_x;
518                 layout->tile_h = layout->prv_h + 2 * layout->prv_border_y + textheight;
519                 layout->width = (int)(BLI_rctf_size_x(&v2d->cur) - 2 * layout->tile_border_x);
520                 layout->columns = layout->width / (layout->tile_w + 2 * layout->tile_border_x);
521                 if (layout->columns > 0)
522                         layout->rows = numfiles / layout->columns + 1;  // XXX dirty, modulo is zero
523                 else {
524                         layout->columns = 1;
525                         layout->rows = numfiles + 1; // XXX dirty, modulo is zero
526                 }
527                 layout->height = sfile->layout->rows * (layout->tile_h + 2 * layout->tile_border_y) + layout->tile_border_y * 2;
528                 layout->flag = FILE_LAYOUT_VER;
529         }
530         else {
531                 int column_space = 0.6f * UI_UNIT_X;
532                 int column_icon_space = 0.2f * UI_UNIT_X;
533
534                 layout->prv_w = 0;
535                 layout->prv_h = 0;
536                 layout->tile_border_x = 0.4f * UI_UNIT_X;
537                 layout->tile_border_y = 0.1f * UI_UNIT_Y;
538                 layout->prv_border_x = 0;
539                 layout->prv_border_y = 0;
540                 layout->tile_h = textheight * 3 / 2;
541                 layout->height = (int)(BLI_rctf_size_y(&v2d->cur) - 2 * layout->tile_border_y);
542                 layout->rows = layout->height / (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 }