Merge with trunk r37475.
[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                 BLI_strncpy(params->title, op->type->name, sizeof(params->title));
125
126                 if(RNA_struct_find_property(op->ptr, "filemode"))
127                         params->type = RNA_int_get(op->ptr, "filemode");
128                 else
129                         params->type = FILE_SPECIAL;
130
131                 if (RNA_struct_find_property(op->ptr, "filepath") && RNA_property_is_set(op->ptr, "filepath")) {
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);
140                         }
141                 }
142                 else {
143                         if (RNA_struct_find_property(op->ptr, "directory") && RNA_property_is_set(op->ptr, "directory")) {
144                                 RNA_string_get(op->ptr, "directory", params->dir);
145                                 sfile->params->file[0]= '\0';
146                         }
147
148                         if (RNA_struct_find_property(op->ptr, "filename") && RNA_property_is_set(op->ptr, "filename")) {
149                                 RNA_string_get(op->ptr, "filename", params->file);
150                         }
151                 }
152
153                 if(params->dir[0]) {
154                         BLI_cleanup_dir(G.main->name, params->dir);
155                         BLI_path_abs(params->dir, G.main->name);
156                 }
157
158                 params->filter = 0;
159                 if(RNA_struct_find_property(op->ptr, "filter_blender"))
160                         params->filter |= RNA_boolean_get(op->ptr, "filter_blender") ? BLENDERFILE : 0;
161                 if(RNA_struct_find_property(op->ptr, "filter_image"))
162                         params->filter |= RNA_boolean_get(op->ptr, "filter_image") ? IMAGEFILE : 0;
163                 if(RNA_struct_find_property(op->ptr, "filter_movie"))
164                         params->filter |= RNA_boolean_get(op->ptr, "filter_movie") ? MOVIEFILE : 0;
165                 if(RNA_struct_find_property(op->ptr, "filter_text"))
166                         params->filter |= RNA_boolean_get(op->ptr, "filter_text") ? TEXTFILE : 0;
167                 if(RNA_struct_find_property(op->ptr, "filter_python"))
168                         params->filter |= RNA_boolean_get(op->ptr, "filter_python") ? PYSCRIPTFILE : 0;
169                 if(RNA_struct_find_property(op->ptr, "filter_font"))
170                         params->filter |= RNA_boolean_get(op->ptr, "filter_font") ? FTFONTFILE : 0;
171                 if(RNA_struct_find_property(op->ptr, "filter_sound"))
172                         params->filter |= RNA_boolean_get(op->ptr, "filter_sound") ? SOUNDFILE : 0;
173                 if(RNA_struct_find_property(op->ptr, "filter_text"))
174                         params->filter |= RNA_boolean_get(op->ptr, "filter_text") ? TEXTFILE : 0;
175                 if(RNA_struct_find_property(op->ptr, "filter_folder"))
176                         params->filter |= RNA_boolean_get(op->ptr, "filter_folder") ? FOLDERFILE : 0;
177                 if(RNA_struct_find_property(op->ptr, "filter_btx"))
178                         params->filter |= RNA_boolean_get(op->ptr, "filter_btx") ? BTXFILE : 0;
179                 if(RNA_struct_find_property(op->ptr, "filter_collada"))
180                         params->filter |= RNA_boolean_get(op->ptr, "filter_collada") ? COLLADAFILE : 0;
181                 if (RNA_struct_find_property(op->ptr, "filter_glob")) {
182                         RNA_string_get(op->ptr, "filter_glob", params->filter_glob);
183                         params->filter |= (OPERATORFILE|FOLDERFILE);
184                 }
185                 else {
186                         params->filter_glob[0] = '\0';
187                 }
188
189                 if (params->filter != 0) {
190                         if (U.uiflag & USER_FILTERFILEEXTS) {
191                                 params->flag |= FILE_FILTER;
192                         } else {
193                                 params->flag &= ~FILE_FILTER;
194                         }
195                 }
196
197                 if (U.uiflag & USER_HIDE_DOT) {
198                         params->flag |= FILE_HIDE_DOT;
199                 } else {
200                         params->flag &= ~FILE_HIDE_DOT;
201                 }
202                 
203
204                 if (params->type == FILE_LOADLIB) {
205                         params->flag |= RNA_boolean_get(op->ptr, "link") ? FILE_LINK : 0;
206                         params->flag |= RNA_boolean_get(op->ptr, "autoselect") ? FILE_AUTOSELECT : 0;
207                         params->flag |= RNA_boolean_get(op->ptr, "active_layer") ? FILE_ACTIVELAY : 0;
208                 }
209                 
210                 if (U.uiflag & USER_SHOW_THUMBNAILS) {
211                         if(params->filter & (IMAGEFILE|MOVIEFILE))
212                                 params->display= FILE_IMGDISPLAY;
213                         else
214                                 params->display= FILE_SHORTDISPLAY;
215                 } else {
216                         params->display= FILE_SHORTDISPLAY;
217                 }
218
219         } else {
220                 /* default values, if no operator */
221                 params->type = FILE_UNIX;
222                 params->flag |= FILE_HIDE_DOT;
223                 params->display = FILE_SHORTDISPLAY;
224                 params->filter = 0;
225                 params->filter_glob[0] = '\0';
226                 params->sort = FILE_SORT_ALPHA;
227         }
228
229
230         /* initialize the list with previous folders */
231         if (!sfile->folders_prev)
232                 sfile->folders_prev = folderlist_new();
233         folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
234
235         return 1;
236 }
237
238 void ED_fileselect_reset_params(SpaceFile *sfile)
239 {
240         sfile->params->type = FILE_UNIX;
241         sfile->params->flag = 0;
242         sfile->params->title[0] = '\0';
243 }
244
245 int ED_fileselect_layout_numfiles(FileLayout* layout, struct ARegion *ar)
246 {
247         int numfiles;
248
249         if (layout->flag & FILE_LAYOUT_HOR) {
250                 int width = (int)(ar->v2d.cur.xmax - ar->v2d.cur.xmin - 2*layout->tile_border_x);
251                 numfiles = (int)((float)width / (float)layout->tile_w + 0.5f);
252                 return numfiles*layout->rows;
253         } else {
254                 int height = (int)(ar->v2d.cur.ymax - ar->v2d.cur.ymin - 2*layout->tile_border_y);
255                 numfiles = (int)((float)height/(float)layout->tile_h + 0.5f);
256                 return numfiles*layout->columns;
257         }
258 }
259
260 static int is_inside(int x, int y, int cols, int rows)
261 {
262         return ( (x >= 0) && (x<cols) && (y>=0) && (y<rows) );
263 }
264
265 FileSelection ED_fileselect_layout_offset_rect(FileLayout* layout, const rcti* rect)
266 {
267         int colmin, colmax, rowmin, rowmax;
268         FileSelection sel;
269         sel.first = sel.last = -1;
270
271         if (layout == NULL)
272                 return sel;
273         
274         colmin = (rect->xmin)/(layout->tile_w + 2*layout->tile_border_x);
275         rowmin = (rect->ymin)/(layout->tile_h + 2*layout->tile_border_y);
276         colmax = (rect->xmax)/(layout->tile_w + 2*layout->tile_border_x);
277         rowmax = (rect->ymax)/(layout->tile_h + 2*layout->tile_border_y);
278         
279         if ( is_inside(colmin, rowmin, layout->columns, layout->rows) || 
280                  is_inside(colmax, rowmax, layout->columns, layout->rows) ) {
281                 CLAMP(colmin, 0, layout->columns-1);
282                 CLAMP(rowmin, 0, layout->rows-1);
283                 CLAMP(colmax, 0, layout->columns-1);
284                 CLAMP(rowmax, 0, layout->rows-1);
285         } 
286         
287         if  ( (colmin > layout->columns-1) || (rowmin > layout->rows-1) ) {
288                 sel.first = -1;
289         } else {
290                 if (layout->flag & FILE_LAYOUT_HOR) 
291                         sel.first = layout->rows*colmin + rowmin;
292                 else
293                         sel.first = colmin + layout->columns*rowmin;
294         }
295         if  ( (colmax > layout->columns-1) || (rowmax > layout->rows-1) ) {
296                 sel.last = -1;
297         } else {
298                 if (layout->flag & FILE_LAYOUT_HOR) 
299                         sel.last = layout->rows*colmax + rowmax;
300                 else
301                         sel.last = colmax + layout->columns*rowmax;
302         }
303
304         return sel;
305 }
306
307 int ED_fileselect_layout_offset(FileLayout* layout, int x, int y)
308 {
309         int offsetx, offsety;
310         int active_file;
311
312         if (layout == NULL)
313                 return -1;
314         
315         offsetx = (x)/(layout->tile_w + 2*layout->tile_border_x);
316         offsety = (y)/(layout->tile_h + 2*layout->tile_border_y);
317         
318         if (offsetx > layout->columns-1) return -1 ;
319         if (offsety > layout->rows-1) return -1 ;
320         
321         if (layout->flag & FILE_LAYOUT_HOR) 
322                 active_file = layout->rows*offsetx + offsety;
323         else
324                 active_file = offsetx + layout->columns*offsety;
325         return active_file;
326 }
327
328 void ED_fileselect_layout_tilepos(FileLayout* layout, int tile, int *x, int *y)
329 {
330         if (layout->flag == FILE_LAYOUT_HOR) {
331                 *x = layout->tile_border_x + (tile/layout->rows)*(layout->tile_w+2*layout->tile_border_x);
332                 *y = layout->tile_border_y + (tile%layout->rows)*(layout->tile_h+2*layout->tile_border_y);
333         } else {
334                 *x = layout->tile_border_x + ((tile)%layout->columns)*(layout->tile_w+2*layout->tile_border_x);
335                 *y = layout->tile_border_y + ((tile)/layout->columns)*(layout->tile_h+2*layout->tile_border_y);
336         }
337 }
338
339 /* Shorten a string to a given width w. 
340    If front is set, shorten from the front,
341    otherwise shorten from the end. */
342 float file_shorten_string(char* string, float w, int front)
343 {       
344         char temp[FILE_MAX];
345         short shortened = 0;
346         float sw = 0;
347         float pad = 0;
348
349         if (w <= 0) {
350                 *string = '\0';
351                 return 0.0;
352         }
353
354         sw = file_string_width(string);
355         if (front == 1) {
356                 char *s = string;
357                 BLI_strncpy(temp, "...", 4);
358                 pad = file_string_width(temp);
359                 while ((*s) && (sw+pad>w)) {
360                         s++;
361                         sw = file_string_width(s);
362                         shortened = 1;
363                 }
364                 if (shortened) {
365                         int slen = strlen(s);                   
366                         BLI_strncpy(temp+3, s, slen+1);
367                         temp[slen+4] = '\0';
368                         BLI_strncpy(string, temp, slen+4);
369                 }
370         } else {
371                 char *s = string;
372                 while (sw>w) {
373                         int slen = strlen(string);
374                         string[slen-1] = '\0';
375                         sw = file_string_width(s);
376                         shortened = 1;
377                 }
378
379                 if (shortened) {
380                         int slen = strlen(string);
381                         if (slen > 3) {
382                                 BLI_strncpy(string+slen-3, "...", 4);                           
383                         }
384                 }
385         }
386         
387         return sw;
388 }
389
390 float file_string_width(const char* str)
391 {
392         uiStyle *style= U.uistyles.first;
393         uiStyleFontSet(&style->widget);
394         return BLF_width(style->widget.uifont_id, str);
395 }
396
397 float file_font_pointsize(void)
398 {
399 #if 0
400         float s;
401         char tmp[2] = "X";
402         uiStyle *style= U.uistyles.first;
403         uiStyleFontSet(&style->widget);
404         s = BLF_height(style->widget.uifont_id, tmp);
405         return style->widget.points;
406 #else
407         uiStyle *style= U.uistyles.first;
408         uiStyleFontSet(&style->widget);
409         return style->widget.points * UI_DPI_FAC;
410 #endif
411 }
412
413 static void column_widths(struct FileList* files, struct FileLayout* layout)
414 {
415         int i;
416         int numfiles = filelist_numfiles(files);
417
418         for (i=0; i<MAX_FILE_COLUMN; ++i) {
419                 layout->column_widths[i] = 0;
420         }
421
422         for (i=0; (i < numfiles); ++i)
423         {
424                 struct direntry* file = filelist_file(files, i);        
425                 if (file) {
426                         float len;
427                         len = file_string_width(file->relname);
428                         if (len > layout->column_widths[COLUMN_NAME]) layout->column_widths[COLUMN_NAME] = len;
429                         len = file_string_width(file->date);
430                         if (len > layout->column_widths[COLUMN_DATE]) layout->column_widths[COLUMN_DATE] = len;
431                         len = file_string_width(file->time);
432                         if (len > layout->column_widths[COLUMN_TIME]) layout->column_widths[COLUMN_TIME] = len;
433                         len = file_string_width(file->size);
434                         if (len > layout->column_widths[COLUMN_SIZE]) layout->column_widths[COLUMN_SIZE] = len;
435                         len = file_string_width(file->mode1);
436                         if (len > layout->column_widths[COLUMN_MODE1]) layout->column_widths[COLUMN_MODE1] = len;
437                         len = file_string_width(file->mode2);
438                         if (len > layout->column_widths[COLUMN_MODE2]) layout->column_widths[COLUMN_MODE2] = len;
439                         len = file_string_width(file->mode3);
440                         if (len > layout->column_widths[COLUMN_MODE3]) layout->column_widths[COLUMN_MODE3] = len;
441                         len = file_string_width(file->owner);
442                         if (len > layout->column_widths[COLUMN_OWNER]) layout->column_widths[COLUMN_OWNER] = len;
443                 }
444         }
445 }
446
447 void ED_fileselect_init_layout(struct SpaceFile *sfile, struct ARegion *ar)
448 {
449         FileSelectParams *params = ED_fileselect_get_params(sfile);
450         FileLayout *layout= NULL;
451         View2D *v2d= &ar->v2d;
452         int maxlen = 0;
453         int numfiles;
454         int textheight;
455         if (sfile->layout == NULL) {
456                 sfile->layout = MEM_callocN(sizeof(struct FileLayout), "file_layout");
457                 sfile->layout->dirty = 1;
458         } 
459
460         if (!sfile->layout->dirty) return;
461
462         numfiles = filelist_numfiles(sfile->files);
463         textheight = (int)file_font_pointsize();
464         layout = sfile->layout;
465         layout->textheight = textheight;
466
467         if (params->display == FILE_IMGDISPLAY) {
468                 layout->prv_w = 96;
469                 layout->prv_h = 96;
470                 layout->tile_border_x = 6;
471                 layout->tile_border_y = 6;
472                 layout->prv_border_x = 6;
473                 layout->prv_border_y = 6;
474                 layout->tile_w = layout->prv_w + 2*layout->prv_border_x;
475                 layout->tile_h = layout->prv_h + 2*layout->prv_border_y + textheight;
476                 layout->width= (int)(v2d->cur.xmax - v2d->cur.xmin - 2*layout->tile_border_x);
477                 layout->columns= layout->width / (layout->tile_w + 2*layout->tile_border_x);
478                 if(layout->columns > 0)
479                         layout->rows= numfiles/layout->columns + 1; // XXX dirty, modulo is zero
480                 else {
481                         layout->columns = 1;
482                         layout->rows= numfiles + 1; // XXX dirty, modulo is zero
483                 }
484                 layout->height= sfile->layout->rows*(layout->tile_h+2*layout->tile_border_y) + layout->tile_border_y*2;
485                 layout->flag = FILE_LAYOUT_VER;
486         } else {
487                 layout->prv_w = 0;
488                 layout->prv_h = 0;
489                 layout->tile_border_x = 8;
490                 layout->tile_border_y = 2;
491                 layout->prv_border_x = 0;
492                 layout->prv_border_y = 0;
493                 layout->tile_h = textheight*3/2;
494                 layout->height= (int)(v2d->cur.ymax - v2d->cur.ymin - 2*layout->tile_border_y);
495                 layout->rows = layout->height / (layout->tile_h + 2*layout->tile_border_y);
496
497                 column_widths(sfile->files, layout);
498
499                 if (params->display == FILE_SHORTDISPLAY) {
500                         maxlen = ICON_DEFAULT_WIDTH_SCALE + 4 +
501                                          (int)layout->column_widths[COLUMN_NAME] + 12 +
502                                          (int)layout->column_widths[COLUMN_SIZE] + 12;
503                 } else {
504                         maxlen = ICON_DEFAULT_WIDTH_SCALE + 4 +
505                                          (int)layout->column_widths[COLUMN_NAME] + 12 +
506 #ifndef WIN32
507                                          (int)layout->column_widths[COLUMN_MODE1] + 12 +
508                                          (int)layout->column_widths[COLUMN_MODE2] + 12 +
509                                          (int)layout->column_widths[COLUMN_MODE3] + 12 +
510                                          (int)layout->column_widths[COLUMN_OWNER] + 12 +
511 #endif
512                                          (int)layout->column_widths[COLUMN_DATE] + 12 +
513                                          (int)layout->column_widths[COLUMN_TIME] + 12 +
514                                          (int)layout->column_widths[COLUMN_SIZE] + 12;
515
516                 }
517                 layout->tile_w = maxlen;
518                 if(layout->rows > 0)
519                         layout->columns = numfiles/layout->rows + 1; // XXX dirty, modulo is zero
520                 else {
521                         layout->rows = 1;
522                         layout->columns = numfiles + 1; // XXX dirty, modulo is zero
523                 }
524                 layout->width = sfile->layout->columns * (layout->tile_w + 2*layout->tile_border_x) + layout->tile_border_x*2;
525                 layout->flag = FILE_LAYOUT_HOR;
526         }
527         layout->dirty= 0;
528 }
529
530 FileLayout* ED_fileselect_get_layout(struct SpaceFile *sfile, struct ARegion *ar)
531 {
532         if (!sfile->layout) {
533                 ED_fileselect_init_layout(sfile, ar);
534         }
535         return sfile->layout;
536 }
537
538 void file_change_dir(bContext *C, int checkdir)
539 {
540         SpaceFile *sfile= CTX_wm_space_file(C);
541
542         if (sfile->params) {
543
544                 ED_fileselect_clear(C, sfile);
545
546                 if(checkdir && BLI_is_dir(sfile->params->dir)==0) {
547                         BLI_strncpy(sfile->params->dir, filelist_dir(sfile->files), sizeof(sfile->params->dir));
548                         /* could return but just refresh the current dir */
549                 }
550                 filelist_setdir(sfile->files, sfile->params->dir);
551                 
552                 if(folderlist_clear_next(sfile))
553                         folderlist_free(sfile->folders_next);
554
555                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
556
557         }
558 }
559
560 int file_select_match(struct SpaceFile *sfile, const char *pattern)
561 {
562         int match = 0;
563         if (strchr(pattern, '*') || strchr(pattern, '?') || strchr(pattern, '[')) {
564                 int i;
565                 struct direntry *file;
566                 int n = filelist_numfiles(sfile->files);
567
568                 for (i = 0; i < n; i++) {
569                         file = filelist_file(sfile->files, i);
570                         if (fnmatch(pattern, file->relname, 0) == 0) {
571                                 file->selflag |= SELECTED_FILE;
572                                 match = 1;
573                         }
574                 }
575         }
576         return match;
577 }
578
579 void autocomplete_directory(struct bContext *C, char *str, void *UNUSED(arg_v))
580 {
581         SpaceFile *sfile= CTX_wm_space_file(C);
582
583         /* search if str matches the beginning of name */
584         if(str[0] && sfile->files) {
585                 char dirname[FILE_MAX];
586
587                 DIR *dir;
588                 struct dirent *de;
589                 
590                 BLI_split_dirfile(str, dirname, NULL);
591
592                 dir = opendir(dirname);
593
594                 if(dir) {
595                         AutoComplete *autocpl= autocomplete_begin(str, FILE_MAX);
596
597                         while ((de = readdir(dir)) != NULL) {
598                                 if (strcmp(".", de->d_name)==0 || strcmp("..", de->d_name)==0) {
599                                         /* pass */
600                                 }
601                                 else {
602                                         char path[FILE_MAX];
603                                         struct stat status;
604                                         
605                                         BLI_join_dirfile(path, sizeof(path), dirname, de->d_name);
606
607                                         if (stat(path, &status) == 0) {
608                                                 if (S_ISDIR(status.st_mode)) { /* is subdir */
609                                                         autocomplete_do_name(autocpl, path);
610                                                 }
611                                         }
612                                 }
613                         }
614                         closedir(dir);
615
616                         autocomplete_end(autocpl, str);
617                         if (BLI_exists(str)) {
618                                 BLI_add_slash(str);
619                         } else {
620                                 BLI_strncpy(sfile->params->dir, str, sizeof(sfile->params->dir));
621                         }
622                 }
623         }
624 }
625
626 void autocomplete_file(struct bContext *C, char *str, void *UNUSED(arg_v))
627 {
628         SpaceFile *sfile= CTX_wm_space_file(C);
629
630         /* search if str matches the beginning of name */
631         if(str[0] && sfile->files) {
632                 AutoComplete *autocpl= autocomplete_begin(str, FILE_MAX);
633                 int nentries = filelist_numfiles(sfile->files);
634                 int i;
635
636                 for(i= 0; i<nentries; ++i) {
637                         struct direntry* file = filelist_file(sfile->files, i);
638                         if (file && S_ISREG(file->type)) {
639                                 autocomplete_do_name(autocpl, file->relname);
640                         }
641                 }
642                 autocomplete_end(autocpl, str);
643         }
644 }
645
646 void ED_fileselect_clear(struct bContext *C, struct SpaceFile *sfile)
647 {
648         thumbnails_stop(sfile->files, C);
649         filelist_freelib(sfile->files);
650         filelist_free(sfile->files);
651         sfile->params->active_file = -1;
652         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
653 }
654
655 void ED_fileselect_exit(struct bContext *C, struct SpaceFile *sfile)
656 {
657         if(!sfile) return;
658         if(sfile->op) {
659                 WM_event_fileselect_event(C, sfile->op, EVT_FILESELECT_EXTERNAL_CANCEL);
660                 sfile->op = NULL;
661         }
662
663         folderlist_free(sfile->folders_prev);
664         folderlist_free(sfile->folders_next);
665         
666         if (sfile->files) {
667                 ED_fileselect_clear(C, sfile);
668                 MEM_freeN(sfile->files);
669                 sfile->files= NULL;
670         }
671
672 }