Cycles: svn merge -r40358:40411 ^/trunk/blender
[blender.git] / source / blender / editors / space_file / filesel.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/editors/space_file/filesel.c
30  *  \ingroup spfile
31  */
32
33
34 #include <string.h>
35 #include <stdio.h>
36 #include <math.h>
37
38 #include <sys/stat.h>
39 #include <sys/types.h>
40
41 #ifdef WIN32
42 #include <io.h>
43 #include <direct.h>
44 #include "BLI_winstuff.h"
45 #else
46 #include <unistd.h>
47 #include <sys/times.h>
48 #endif   
49
50 /* path/file handeling stuff */
51 #ifndef WIN32
52   #include <dirent.h>
53   #include <unistd.h>
54 #else
55   #include <io.h>
56   #include "BLI_winstuff.h"
57 #endif
58
59 #include "DNA_space_types.h"
60 #include "DNA_screen_types.h"
61 #include "DNA_userdef_types.h"
62
63 #include "MEM_guardedalloc.h"
64
65 #include "BLI_blenlib.h"
66 #include "BLI_linklist.h"
67 #include "BLI_path_util.h"
68 #include "BLI_storage_types.h"
69 #include "BLI_dynstr.h"
70 #include "BLI_utildefines.h"
71
72 #include "BKE_context.h"
73 #include "BKE_global.h"
74 #include "BKE_main.h"
75
76 #include "BLF_api.h"
77
78
79 #include "ED_fileselect.h"
80
81 #include "WM_api.h"
82 #include "WM_types.h"
83
84
85 #include "RNA_access.h"
86
87 #include "UI_interface.h"
88 #include "UI_interface_icons.h"
89
90 #include "file_intern.h"
91 #include "filelist.h"
92
93 #if defined WIN32 && !defined _LIBC
94 # include "BLI_fnmatch.h" /* use fnmatch included in blenlib */
95 #else
96 # include <fnmatch.h>
97 #endif
98
99 FileSelectParams* ED_fileselect_get_params(struct SpaceFile *sfile)
100 {
101         if (!sfile->params) {
102                 ED_fileselect_set_params(sfile);
103         }
104         return sfile->params;
105 }
106
107 short ED_fileselect_set_params(SpaceFile *sfile)
108 {
109         FileSelectParams *params;
110         wmOperator *op = sfile->op;
111
112         /* create new parameters if necessary */
113         if (!sfile->params) {
114                 sfile->params= MEM_callocN(sizeof(FileSelectParams), "fileselparams");
115                 /* set path to most recently opened .blend */
116                 BLI_split_dirfile(G.main->name, sfile->params->dir, sfile->params->file);
117                 sfile->params->filter_glob[0] = '\0';
118         }
119
120         params = sfile->params;
121
122         /* set the parameters from the operator, if it exists */
123         if (op) {
124                 const short is_files= (RNA_struct_find_property(op->ptr, "files") != NULL);
125                 const short is_filepath= (RNA_struct_find_property(op->ptr, "filepath") != NULL);
126                 const short is_filename= (RNA_struct_find_property(op->ptr, "filename") != NULL);
127                 const short is_directory= (RNA_struct_find_property(op->ptr, "directory") != NULL);
128
129                 BLI_strncpy(params->title, op->type->name, sizeof(params->title));
130
131                 if(RNA_struct_find_property(op->ptr, "filemode"))
132                         params->type = RNA_int_get(op->ptr, "filemode");
133                 else
134                         params->type = FILE_SPECIAL;
135
136                 if (is_filepath && RNA_property_is_set(op->ptr, "filepath")) {
137                         char name[FILE_MAX];
138                         RNA_string_get(op->ptr, "filepath", name);
139                         if (params->type == FILE_LOADLIB) {
140                                 BLI_strncpy(params->dir, name, sizeof(params->dir));
141                                 sfile->params->file[0]= '\0';
142                         }
143                         else {
144                                 BLI_split_dirfile(name, sfile->params->dir, sfile->params->file);
145                         }
146                 }
147                 else {
148                         if (is_directory && RNA_property_is_set(op->ptr, "directory")) {
149                                 RNA_string_get(op->ptr, "directory", params->dir);
150                                 sfile->params->file[0]= '\0';
151                         }
152
153                         if (is_filename && RNA_property_is_set(op->ptr, "filename")) {
154                                 RNA_string_get(op->ptr, "filename", params->file);
155                         }
156                 }
157
158                 if(params->dir[0]) {
159                         BLI_cleanup_dir(G.main->name, params->dir);
160                         BLI_path_abs(params->dir, G.main->name);
161                 }
162
163                 if(is_directory==TRUE && is_filename==FALSE && is_filepath==FALSE && is_files==FALSE) {
164                         params->flag |= FILE_DIRSEL_ONLY;
165                 }
166                 else {
167                         params->flag &= ~FILE_DIRSEL_ONLY;
168                 }
169
170                 params->filter = 0;
171                 if(RNA_struct_find_property(op->ptr, "filter_blender"))
172                         params->filter |= RNA_boolean_get(op->ptr, "filter_blender") ? BLENDERFILE : 0;
173                 if(RNA_struct_find_property(op->ptr, "filter_image"))
174                         params->filter |= RNA_boolean_get(op->ptr, "filter_image") ? IMAGEFILE : 0;
175                 if(RNA_struct_find_property(op->ptr, "filter_movie"))
176                         params->filter |= RNA_boolean_get(op->ptr, "filter_movie") ? MOVIEFILE : 0;
177                 if(RNA_struct_find_property(op->ptr, "filter_text"))
178                         params->filter |= RNA_boolean_get(op->ptr, "filter_text") ? TEXTFILE : 0;
179                 if(RNA_struct_find_property(op->ptr, "filter_python"))
180                         params->filter |= RNA_boolean_get(op->ptr, "filter_python") ? PYSCRIPTFILE : 0;
181                 if(RNA_struct_find_property(op->ptr, "filter_font"))
182                         params->filter |= RNA_boolean_get(op->ptr, "filter_font") ? FTFONTFILE : 0;
183                 if(RNA_struct_find_property(op->ptr, "filter_sound"))
184                         params->filter |= RNA_boolean_get(op->ptr, "filter_sound") ? SOUNDFILE : 0;
185                 if(RNA_struct_find_property(op->ptr, "filter_text"))
186                         params->filter |= RNA_boolean_get(op->ptr, "filter_text") ? TEXTFILE : 0;
187                 if(RNA_struct_find_property(op->ptr, "filter_folder"))
188                         params->filter |= RNA_boolean_get(op->ptr, "filter_folder") ? FOLDERFILE : 0;
189                 if(RNA_struct_find_property(op->ptr, "filter_btx"))
190                         params->filter |= RNA_boolean_get(op->ptr, "filter_btx") ? BTXFILE : 0;
191                 if(RNA_struct_find_property(op->ptr, "filter_collada"))
192                         params->filter |= RNA_boolean_get(op->ptr, "filter_collada") ? COLLADAFILE : 0;
193                 if (RNA_struct_find_property(op->ptr, "filter_glob")) {
194                         RNA_string_get(op->ptr, "filter_glob", params->filter_glob);
195                         params->filter |= (OPERATORFILE|FOLDERFILE);
196                 }
197                 else {
198                         params->filter_glob[0] = '\0';
199                 }
200
201                 if (params->filter != 0) {
202                         if (U.uiflag & USER_FILTERFILEEXTS) {
203                                 params->flag |= FILE_FILTER;
204                         } else {
205                                 params->flag &= ~FILE_FILTER;
206                         }
207                 }
208
209                 if (U.uiflag & USER_HIDE_DOT) {
210                         params->flag |= FILE_HIDE_DOT;
211                 } else {
212                         params->flag &= ~FILE_HIDE_DOT;
213                 }
214                 
215
216                 if (params->type == FILE_LOADLIB) {
217                         params->flag |= RNA_boolean_get(op->ptr, "link") ? FILE_LINK : 0;
218                         params->flag |= RNA_boolean_get(op->ptr, "autoselect") ? FILE_AUTOSELECT : 0;
219                         params->flag |= RNA_boolean_get(op->ptr, "active_layer") ? FILE_ACTIVELAY : 0;
220                 }
221                 
222                 if (U.uiflag & USER_SHOW_THUMBNAILS) {
223                         if(params->filter & (IMAGEFILE|MOVIEFILE))
224                                 params->display= FILE_IMGDISPLAY;
225                         else
226                                 params->display= FILE_SHORTDISPLAY;
227                 } else {
228                         params->display= FILE_SHORTDISPLAY;
229                 }
230
231         }
232         else {
233                 /* default values, if no operator */
234                 params->type = FILE_UNIX;
235                 params->flag |= FILE_HIDE_DOT;
236                 params->flag &= ~FILE_DIRSEL_ONLY;
237                 params->display = FILE_SHORTDISPLAY;
238                 params->filter = 0;
239                 params->filter_glob[0] = '\0';
240                 params->sort = FILE_SORT_ALPHA;
241         }
242
243
244         /* initialize the list with previous folders */
245         if (!sfile->folders_prev)
246                 sfile->folders_prev = folderlist_new();
247         folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
248
249         return 1;
250 }
251
252 void ED_fileselect_reset_params(SpaceFile *sfile)
253 {
254         sfile->params->type = FILE_UNIX;
255         sfile->params->flag = 0;
256         sfile->params->title[0] = '\0';
257 }
258
259 int ED_fileselect_layout_numfiles(FileLayout* layout, struct ARegion *ar)
260 {
261         int numfiles;
262
263         if (layout->flag & FILE_LAYOUT_HOR) {
264                 int width = (int)(ar->v2d.cur.xmax - ar->v2d.cur.xmin - 2*layout->tile_border_x);
265                 numfiles = (int)((float)width / (float)layout->tile_w + 0.5f);
266                 return numfiles*layout->rows;
267         } else {
268                 int height = (int)(ar->v2d.cur.ymax - ar->v2d.cur.ymin - 2*layout->tile_border_y);
269                 numfiles = (int)((float)height/(float)layout->tile_h + 0.5f);
270                 return numfiles*layout->columns;
271         }
272 }
273
274 static int is_inside(int x, int y, int cols, int rows)
275 {
276         return ( (x >= 0) && (x<cols) && (y>=0) && (y<rows) );
277 }
278
279 FileSelection ED_fileselect_layout_offset_rect(FileLayout* layout, const rcti* rect)
280 {
281         int colmin, colmax, rowmin, rowmax;
282         FileSelection sel;
283         sel.first = sel.last = -1;
284
285         if (layout == NULL)
286                 return sel;
287         
288         colmin = (rect->xmin)/(layout->tile_w + 2*layout->tile_border_x);
289         rowmin = (rect->ymin)/(layout->tile_h + 2*layout->tile_border_y);
290         colmax = (rect->xmax)/(layout->tile_w + 2*layout->tile_border_x);
291         rowmax = (rect->ymax)/(layout->tile_h + 2*layout->tile_border_y);
292         
293         if ( is_inside(colmin, rowmin, layout->columns, layout->rows) || 
294                  is_inside(colmax, rowmax, layout->columns, layout->rows) ) {
295                 CLAMP(colmin, 0, layout->columns-1);
296                 CLAMP(rowmin, 0, layout->rows-1);
297                 CLAMP(colmax, 0, layout->columns-1);
298                 CLAMP(rowmax, 0, layout->rows-1);
299         } 
300         
301         if  ( (colmin > layout->columns-1) || (rowmin > layout->rows-1) ) {
302                 sel.first = -1;
303         } else {
304                 if (layout->flag & FILE_LAYOUT_HOR) 
305                         sel.first = layout->rows*colmin + rowmin;
306                 else
307                         sel.first = colmin + layout->columns*rowmin;
308         }
309         if  ( (colmax > layout->columns-1) || (rowmax > layout->rows-1) ) {
310                 sel.last = -1;
311         } else {
312                 if (layout->flag & FILE_LAYOUT_HOR) 
313                         sel.last = layout->rows*colmax + rowmax;
314                 else
315                         sel.last = colmax + layout->columns*rowmax;
316         }
317
318         return sel;
319 }
320
321 int ED_fileselect_layout_offset(FileLayout* layout, int x, int y)
322 {
323         int offsetx, offsety;
324         int active_file;
325
326         if (layout == NULL)
327                 return -1;
328         
329         offsetx = (x)/(layout->tile_w + 2*layout->tile_border_x);
330         offsety = (y)/(layout->tile_h + 2*layout->tile_border_y);
331         
332         if (offsetx > layout->columns-1) return -1 ;
333         if (offsety > layout->rows-1) return -1 ;
334         
335         if (layout->flag & FILE_LAYOUT_HOR) 
336                 active_file = layout->rows*offsetx + offsety;
337         else
338                 active_file = offsetx + layout->columns*offsety;
339         return active_file;
340 }
341
342 void ED_fileselect_layout_tilepos(FileLayout* layout, int tile, int *x, int *y)
343 {
344         if (layout->flag == FILE_LAYOUT_HOR) {
345                 *x = layout->tile_border_x + (tile/layout->rows)*(layout->tile_w+2*layout->tile_border_x);
346                 *y = layout->tile_border_y + (tile%layout->rows)*(layout->tile_h+2*layout->tile_border_y);
347         } else {
348                 *x = layout->tile_border_x + ((tile)%layout->columns)*(layout->tile_w+2*layout->tile_border_x);
349                 *y = layout->tile_border_y + ((tile)/layout->columns)*(layout->tile_h+2*layout->tile_border_y);
350         }
351 }
352
353 /* Shorten a string to a given width w. 
354    If front is set, shorten from the front,
355    otherwise shorten from the end. */
356 float file_shorten_string(char* string, float w, int front)
357 {       
358         char temp[FILE_MAX];
359         short shortened = 0;
360         float sw = 0;
361         float pad = 0;
362
363         if (w <= 0) {
364                 *string = '\0';
365                 return 0.0;
366         }
367
368         sw = file_string_width(string);
369         if (front == 1) {
370                 char *s = string;
371                 BLI_strncpy(temp, "...", 4);
372                 pad = file_string_width(temp);
373                 while ((*s) && (sw+pad>w)) {
374                         s++;
375                         sw = file_string_width(s);
376                         shortened = 1;
377                 }
378                 if (shortened) {
379                         int slen = strlen(s);
380                         BLI_strncpy(temp+3, s, slen+1);
381                         temp[slen+4] = '\0';
382                         BLI_strncpy(string, temp, slen+4);
383                 }
384         } else {
385                 char *s = string;
386                 while (sw>w) {
387                         int slen = strlen(string);
388                         string[slen-1] = '\0';
389                         sw = file_string_width(s);
390                         shortened = 1;
391                 }
392
393                 if (shortened) {
394                         int slen = strlen(string);
395                         if (slen > 3) {
396                                 BLI_strncpy(string+slen-3, "...", 4);
397                         }
398                 }
399         }
400         
401         return sw;
402 }
403
404 float file_string_width(const char* str)
405 {
406         uiStyle *style= UI_GetStyle();
407         uiStyleFontSet(&style->widget);
408         return BLF_width(style->widget.uifont_id, str);
409 }
410
411 float file_font_pointsize(void)
412 {
413 #if 0
414         float s;
415         char tmp[2] = "X";
416         uiStyle *style= UI_GetStyle();
417         uiStyleFontSet(&style->widget);
418         s = BLF_height(style->widget.uifont_id, tmp);
419         return style->widget.points;
420 #else
421         uiStyle *style= UI_GetStyle();
422         uiStyleFontSet(&style->widget);
423         return style->widget.points * UI_DPI_FAC;
424 #endif
425 }
426
427 static void column_widths(struct FileList* files, struct FileLayout* layout)
428 {
429         int i;
430         int numfiles = filelist_numfiles(files);
431
432         for (i=0; i<MAX_FILE_COLUMN; ++i) {
433                 layout->column_widths[i] = 0;
434         }
435
436         for (i=0; (i < numfiles); ++i)
437         {
438                 struct direntry* file = filelist_file(files, i);        
439                 if (file) {
440                         float len;
441                         len = file_string_width(file->relname);
442                         if (len > layout->column_widths[COLUMN_NAME]) layout->column_widths[COLUMN_NAME] = len;
443                         len = file_string_width(file->date);
444                         if (len > layout->column_widths[COLUMN_DATE]) layout->column_widths[COLUMN_DATE] = len;
445                         len = file_string_width(file->time);
446                         if (len > layout->column_widths[COLUMN_TIME]) layout->column_widths[COLUMN_TIME] = len;
447                         len = file_string_width(file->size);
448                         if (len > layout->column_widths[COLUMN_SIZE]) layout->column_widths[COLUMN_SIZE] = len;
449                         len = file_string_width(file->mode1);
450                         if (len > layout->column_widths[COLUMN_MODE1]) layout->column_widths[COLUMN_MODE1] = len;
451                         len = file_string_width(file->mode2);
452                         if (len > layout->column_widths[COLUMN_MODE2]) layout->column_widths[COLUMN_MODE2] = len;
453                         len = file_string_width(file->mode3);
454                         if (len > layout->column_widths[COLUMN_MODE3]) layout->column_widths[COLUMN_MODE3] = len;
455                         len = file_string_width(file->owner);
456                         if (len > layout->column_widths[COLUMN_OWNER]) layout->column_widths[COLUMN_OWNER] = len;
457                 }
458         }
459 }
460
461 void ED_fileselect_init_layout(struct SpaceFile *sfile, struct ARegion *ar)
462 {
463         FileSelectParams *params = ED_fileselect_get_params(sfile);
464         FileLayout *layout= NULL;
465         View2D *v2d= &ar->v2d;
466         int maxlen = 0;
467         int numfiles;
468         int textheight;
469         if (sfile->layout == NULL) {
470                 sfile->layout = MEM_callocN(sizeof(struct FileLayout), "file_layout");
471                 sfile->layout->dirty = 1;
472         } 
473
474         if (!sfile->layout->dirty) return;
475
476         numfiles = filelist_numfiles(sfile->files);
477         textheight = (int)file_font_pointsize();
478         layout = sfile->layout;
479         layout->textheight = textheight;
480
481         if (params->display == FILE_IMGDISPLAY) {
482                 layout->prv_w = 96;
483                 layout->prv_h = 96;
484                 layout->tile_border_x = 6;
485                 layout->tile_border_y = 6;
486                 layout->prv_border_x = 6;
487                 layout->prv_border_y = 6;
488                 layout->tile_w = layout->prv_w + 2*layout->prv_border_x;
489                 layout->tile_h = layout->prv_h + 2*layout->prv_border_y + textheight;
490                 layout->width= (int)(v2d->cur.xmax - v2d->cur.xmin - 2*layout->tile_border_x);
491                 layout->columns= layout->width / (layout->tile_w + 2*layout->tile_border_x);
492                 if(layout->columns > 0)
493                         layout->rows= numfiles/layout->columns + 1; // XXX dirty, modulo is zero
494                 else {
495                         layout->columns = 1;
496                         layout->rows= numfiles + 1; // XXX dirty, modulo is zero
497                 }
498                 layout->height= sfile->layout->rows*(layout->tile_h+2*layout->tile_border_y) + layout->tile_border_y*2;
499                 layout->flag = FILE_LAYOUT_VER;
500         } else {
501                 layout->prv_w = 0;
502                 layout->prv_h = 0;
503                 layout->tile_border_x = 8;
504                 layout->tile_border_y = 2;
505                 layout->prv_border_x = 0;
506                 layout->prv_border_y = 0;
507                 layout->tile_h = textheight*3/2;
508                 layout->height= (int)(v2d->cur.ymax - v2d->cur.ymin - 2*layout->tile_border_y);
509                 layout->rows = layout->height / (layout->tile_h + 2*layout->tile_border_y);
510
511                 column_widths(sfile->files, layout);
512
513                 if (params->display == FILE_SHORTDISPLAY) {
514                         maxlen = ICON_DEFAULT_WIDTH_SCALE + 4 +
515                                          (int)layout->column_widths[COLUMN_NAME] + 12 +
516                                          (int)layout->column_widths[COLUMN_SIZE] + 12;
517                 } else {
518                         maxlen = ICON_DEFAULT_WIDTH_SCALE + 4 +
519                                          (int)layout->column_widths[COLUMN_NAME] + 12 +
520 #ifndef WIN32
521                                          (int)layout->column_widths[COLUMN_MODE1] + 12 +
522                                          (int)layout->column_widths[COLUMN_MODE2] + 12 +
523                                          (int)layout->column_widths[COLUMN_MODE3] + 12 +
524                                          (int)layout->column_widths[COLUMN_OWNER] + 12 +
525 #endif
526                                          (int)layout->column_widths[COLUMN_DATE] + 12 +
527                                          (int)layout->column_widths[COLUMN_TIME] + 12 +
528                                          (int)layout->column_widths[COLUMN_SIZE] + 12;
529
530                 }
531                 layout->tile_w = maxlen;
532                 if(layout->rows > 0)
533                         layout->columns = numfiles/layout->rows + 1; // XXX dirty, modulo is zero
534                 else {
535                         layout->rows = 1;
536                         layout->columns = numfiles + 1; // XXX dirty, modulo is zero
537                 }
538                 layout->width = sfile->layout->columns * (layout->tile_w + 2*layout->tile_border_x) + layout->tile_border_x*2;
539                 layout->flag = FILE_LAYOUT_HOR;
540         }
541         layout->dirty= 0;
542 }
543
544 FileLayout* ED_fileselect_get_layout(struct SpaceFile *sfile, struct ARegion *ar)
545 {
546         if (!sfile->layout) {
547                 ED_fileselect_init_layout(sfile, ar);
548         }
549         return sfile->layout;
550 }
551
552 void file_change_dir(bContext *C, int checkdir)
553 {
554         SpaceFile *sfile= CTX_wm_space_file(C);
555
556         if (sfile->params) {
557
558                 ED_fileselect_clear(C, sfile);
559
560                 if(checkdir && BLI_is_dir(sfile->params->dir)==0) {
561                         BLI_strncpy(sfile->params->dir, filelist_dir(sfile->files), sizeof(sfile->params->dir));
562                         /* could return but just refresh the current dir */
563                 }
564                 filelist_setdir(sfile->files, sfile->params->dir);
565                 
566                 if(folderlist_clear_next(sfile))
567                         folderlist_free(sfile->folders_next);
568
569                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
570
571         }
572 }
573
574 int file_select_match(struct SpaceFile *sfile, const char *pattern)
575 {
576         int match = 0;
577         if (strchr(pattern, '*') || strchr(pattern, '?') || strchr(pattern, '[')) {
578                 int i;
579                 struct direntry *file;
580                 int n = filelist_numfiles(sfile->files);
581
582                 for (i = 0; i < n; i++) {
583                         file = filelist_file(sfile->files, i);
584                         if (fnmatch(pattern, file->relname, 0) == 0) {
585                                 file->selflag |= SELECTED_FILE;
586                                 match = 1;
587                         }
588                 }
589         }
590         return match;
591 }
592
593 void autocomplete_directory(struct bContext *C, char *str, void *UNUSED(arg_v))
594 {
595         SpaceFile *sfile= CTX_wm_space_file(C);
596
597         /* search if str matches the beginning of name */
598         if(str[0] && sfile->files) {
599                 char dirname[FILE_MAX];
600
601                 DIR *dir;
602                 struct dirent *de;
603                 
604                 BLI_split_dirfile(str, dirname, NULL);
605
606                 dir = opendir(dirname);
607
608                 if(dir) {
609                         AutoComplete *autocpl= autocomplete_begin(str, FILE_MAX);
610
611                         while ((de = readdir(dir)) != NULL) {
612                                 if (strcmp(".", de->d_name)==0 || strcmp("..", de->d_name)==0) {
613                                         /* pass */
614                                 }
615                                 else {
616                                         char path[FILE_MAX];
617                                         struct stat status;
618                                         
619                                         BLI_join_dirfile(path, sizeof(path), dirname, de->d_name);
620
621                                         if (stat(path, &status) == 0) {
622                                                 if (S_ISDIR(status.st_mode)) { /* is subdir */
623                                                         autocomplete_do_name(autocpl, path);
624                                                 }
625                                         }
626                                 }
627                         }
628                         closedir(dir);
629
630                         autocomplete_end(autocpl, str);
631                         if (BLI_exists(str)) {
632                                 BLI_add_slash(str);
633                         } else {
634                                 BLI_strncpy(sfile->params->dir, str, sizeof(sfile->params->dir));
635                         }
636                 }
637         }
638 }
639
640 void autocomplete_file(struct bContext *C, char *str, void *UNUSED(arg_v))
641 {
642         SpaceFile *sfile= CTX_wm_space_file(C);
643
644         /* search if str matches the beginning of name */
645         if(str[0] && sfile->files) {
646                 AutoComplete *autocpl= autocomplete_begin(str, FILE_MAX);
647                 int nentries = filelist_numfiles(sfile->files);
648                 int i;
649
650                 for(i= 0; i<nentries; ++i) {
651                         struct direntry* file = filelist_file(sfile->files, i);
652                         if (file && S_ISREG(file->type)) {
653                                 autocomplete_do_name(autocpl, file->relname);
654                         }
655                 }
656                 autocomplete_end(autocpl, str);
657         }
658 }
659
660 void ED_fileselect_clear(struct bContext *C, struct SpaceFile *sfile)
661 {
662         thumbnails_stop(sfile->files, C);
663         filelist_freelib(sfile->files);
664         filelist_free(sfile->files);
665         sfile->params->active_file = -1;
666         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
667 }
668
669 void ED_fileselect_exit(struct bContext *C, struct SpaceFile *sfile)
670 {
671         if(!sfile) return;
672         if(sfile->op) {
673                 WM_event_fileselect_event(C, sfile->op, EVT_FILESELECT_EXTERNAL_CANCEL);
674                 sfile->op = NULL;
675         }
676
677         folderlist_free(sfile->folders_prev);
678         folderlist_free(sfile->folders_next);
679         
680         if (sfile->files) {
681                 ED_fileselect_clear(C, sfile);
682                 MEM_freeN(sfile->files);
683                 sfile->files= NULL;
684         }
685
686 }