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