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