fix for own error with recent patch edits, globbing wasnt being cleared so importing...
[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 #include <string.h>
30 #include <stdio.h>
31 #include <math.h>
32
33 #include <sys/stat.h>
34 #include <sys/types.h>
35
36 #ifdef WIN32
37 #include <io.h>
38 #include <direct.h>
39 #include "BLI_winstuff.h"
40 #else
41 #include <unistd.h>
42 #include <sys/times.h>
43 #endif   
44
45 /* path/file handeling stuff */
46 #ifndef WIN32
47   #include <dirent.h>
48   #include <unistd.h>
49 #else
50   #include <io.h>
51   #include "BLI_winstuff.h"
52 #endif
53
54 #include "DNA_space_types.h"
55 #include "DNA_screen_types.h"
56 #include "DNA_userdef_types.h"
57
58 #include "MEM_guardedalloc.h"
59
60 #include "BLI_blenlib.h"
61 #include "BLI_linklist.h"
62 #include "BLI_storage_types.h"
63 #include "BLI_dynstr.h"
64
65 #include "BKE_context.h"
66 #include "BKE_global.h"
67
68 #include "BLF_api.h"
69
70
71 #include "ED_fileselect.h"
72
73 #include "WM_api.h"
74 #include "WM_types.h"
75
76
77 #include "RNA_access.h"
78
79 #include "UI_interface.h"
80
81 #include "file_intern.h"
82 #include "filelist.h"
83
84 #if defined WIN32 && !defined _LIBC
85 # include "BLI_fnmatch.h" /* use fnmatch included in blenlib */
86 #else
87 # include <fnmatch.h>
88 #endif
89
90 FileSelectParams* ED_fileselect_get_params(struct SpaceFile *sfile)
91 {
92         if (!sfile->params) {
93                 ED_fileselect_set_params(sfile);
94         }
95         return sfile->params;
96 }
97
98 short ED_fileselect_set_params(SpaceFile *sfile)
99 {
100         FileSelectParams *params;
101         wmOperator *op = sfile->op;
102
103         /* create new parameters if necessary */
104         if (!sfile->params) {
105                 sfile->params= MEM_callocN(sizeof(FileSelectParams), "fileselparams");
106                 /* set path to most recently opened .blend */
107                 BLI_split_dirfile(G.sce, sfile->params->dir, sfile->params->file);
108                 sfile->params->filter_glob[0] = '\0';
109         }
110
111         params = sfile->params;
112
113         /* set the parameters from the operator, if it exists */
114         if (op) {
115                 BLI_strncpy(params->title, op->type->name, sizeof(params->title));
116
117                 if(RNA_struct_find_property(op->ptr, "filemode"))
118                         params->type = RNA_int_get(op->ptr, "filemode");
119                 else
120                         params->type = FILE_SPECIAL;
121
122                 if (RNA_struct_find_property(op->ptr, "filepath") && RNA_property_is_set(op->ptr, "filepath")) {
123                         char name[FILE_MAX];
124                         RNA_string_get(op->ptr, "filepath", name);
125                         if (params->type == FILE_LOADLIB) {
126                                 BLI_strncpy(params->dir, name, sizeof(params->dir));
127                                 sfile->params->file[0]= '\0';
128                         }
129                         else {
130                                 BLI_split_dirfile(name, sfile->params->dir, sfile->params->file);
131                         }
132                 }
133                 else {
134                         if (RNA_struct_find_property(op->ptr, "directory") && RNA_property_is_set(op->ptr, "directory")) {
135                                 RNA_string_get(op->ptr, "directory", params->dir);
136                                 sfile->params->file[0]= '\0';
137                         }
138
139                         if (RNA_struct_find_property(op->ptr, "filename") && RNA_property_is_set(op->ptr, "filename")) {
140                                 RNA_string_get(op->ptr, "filename", params->file);
141                         }
142                 }
143
144                 if(params->dir[0]) {
145                         BLI_cleanup_dir(G.sce, params->dir);
146                         BLI_path_abs(params->dir, G.sce);
147                 }
148
149                 params->filter = 0;
150                 if(RNA_struct_find_property(op->ptr, "filter_blender"))
151                         params->filter |= RNA_boolean_get(op->ptr, "filter_blender") ? BLENDERFILE : 0;
152                 if(RNA_struct_find_property(op->ptr, "filter_image"))
153                         params->filter |= RNA_boolean_get(op->ptr, "filter_image") ? IMAGEFILE : 0;
154                 if(RNA_struct_find_property(op->ptr, "filter_movie"))
155                         params->filter |= RNA_boolean_get(op->ptr, "filter_movie") ? MOVIEFILE : 0;
156                 if(RNA_struct_find_property(op->ptr, "filter_text"))
157                         params->filter |= RNA_boolean_get(op->ptr, "filter_text") ? TEXTFILE : 0;
158                 if(RNA_struct_find_property(op->ptr, "filter_python"))
159                         params->filter |= RNA_boolean_get(op->ptr, "filter_python") ? PYSCRIPTFILE : 0;
160                 if(RNA_struct_find_property(op->ptr, "filter_font"))
161                         params->filter |= RNA_boolean_get(op->ptr, "filter_font") ? FTFONTFILE : 0;
162                 if(RNA_struct_find_property(op->ptr, "filter_sound"))
163                         params->filter |= RNA_boolean_get(op->ptr, "filter_sound") ? SOUNDFILE : 0;
164                 if(RNA_struct_find_property(op->ptr, "filter_text"))
165                         params->filter |= RNA_boolean_get(op->ptr, "filter_text") ? TEXTFILE : 0;
166                 if(RNA_struct_find_property(op->ptr, "filter_folder"))
167                         params->filter |= RNA_boolean_get(op->ptr, "filter_folder") ? FOLDERFILE : 0;
168                 if(RNA_struct_find_property(op->ptr, "filter_btx"))
169                         params->filter |= RNA_boolean_get(op->ptr, "filter_btx") ? BTXFILE : 0;
170                 if(RNA_struct_find_property(op->ptr, "filter_collada"))
171                         params->filter |= RNA_boolean_get(op->ptr, "filter_collada") ? COLLADAFILE : 0;
172                 if (RNA_struct_find_property(op->ptr, "filter_glob")) {
173                         RNA_string_get(op->ptr, "filter_glob", params->filter_glob);
174                         params->filter |= (OPERATORFILE|FOLDERFILE);
175                 }
176                 else {
177                         params->filter_glob[0] = '\0';
178                 }
179
180                 if (params->filter != 0) {
181                         if (U.uiflag & USER_FILTERFILEEXTS) {
182                                 params->flag |= FILE_FILTER;
183                         } else {
184                                 params->flag &= ~FILE_FILTER;
185                         }
186                 }
187
188                 if (U.uiflag & USER_HIDE_DOT) {
189                         params->flag |= FILE_HIDE_DOT;
190                 } else {
191                         params->flag &= ~FILE_HIDE_DOT;
192                 }
193                 
194
195                 if (params->type == FILE_LOADLIB) {
196                         params->flag |= RNA_boolean_get(op->ptr, "link") ? FILE_LINK : 0;
197                         params->flag |= RNA_boolean_get(op->ptr, "autoselect") ? FILE_AUTOSELECT : 0;
198                         params->flag |= RNA_boolean_get(op->ptr, "active_layer") ? FILE_ACTIVELAY : 0;
199                 }
200
201                 if(params->filter & (IMAGEFILE|MOVIEFILE))
202                         params->display= FILE_IMGDISPLAY;
203                 else
204                         params->display= FILE_SHORTDISPLAY;
205                 
206         } else {
207                 /* default values, if no operator */
208                 params->type = FILE_UNIX;
209                 params->flag |= FILE_HIDE_DOT;
210                 params->display = FILE_SHORTDISPLAY;
211                 params->filter = 0;
212                 params->filter_glob[0] = '\0';
213                 params->sort = FILE_SORT_ALPHA;
214         }
215
216
217         /* initialize the list with previous folders */
218         if (!sfile->folders_prev)
219                 sfile->folders_prev = folderlist_new();
220         folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
221
222         return 1;
223 }
224
225 void ED_fileselect_reset_params(SpaceFile *sfile)
226 {
227         sfile->params->type = FILE_UNIX;
228         sfile->params->flag = 0;
229         sfile->params->title[0] = '\0';
230 }
231
232 int ED_fileselect_layout_numfiles(FileLayout* layout, struct ARegion *ar)
233 {
234         int numfiles;
235
236         if (layout->flag & FILE_LAYOUT_HOR) {
237                 int width = ar->v2d.cur.xmax - ar->v2d.cur.xmin - 2*layout->tile_border_x;
238                 numfiles = (float)width/(float)layout->tile_w+0.5;
239                 return numfiles*layout->rows;
240         } else {
241                 int height = ar->v2d.cur.ymax - ar->v2d.cur.ymin - 2*layout->tile_border_y;
242                 numfiles = (float)height/(float)layout->tile_h+0.5;
243                 return numfiles*layout->columns;
244         }
245 }
246
247 int ED_fileselect_layout_offset(FileLayout* layout, int clamp_bounds, int x, int y)
248 {
249         int offsetx, offsety;
250         int active_file;
251
252         if (layout == NULL)
253                 return 0;
254         
255         offsetx = (x)/(layout->tile_w + 2*layout->tile_border_x);
256         offsety = (y)/(layout->tile_h + 2*layout->tile_border_y);
257         
258         if (clamp_bounds) {
259                 CLAMP(offsetx, 0, layout->columns-1);
260                 CLAMP(offsety, 0, layout->rows-1);
261         } else {
262                 if (offsetx > layout->columns-1) return -1 ;
263                 if (offsety > layout->rows-1) return -1 ;
264         }
265         
266         if (layout->flag & FILE_LAYOUT_HOR) 
267                 active_file = layout->rows*offsetx + offsety;
268         else
269                 active_file = offsetx + layout->columns*offsety;
270         return active_file;
271 }
272
273 void ED_fileselect_layout_tilepos(FileLayout* layout, int tile, int *x, int *y)
274 {
275         if (layout->flag == FILE_LAYOUT_HOR) {
276                 *x = layout->tile_border_x + (tile/layout->rows)*(layout->tile_w+2*layout->tile_border_x);
277                 *y = layout->tile_border_y + (tile%layout->rows)*(layout->tile_h+2*layout->tile_border_y);
278         } else {
279                 *x = layout->tile_border_x + ((tile)%layout->columns)*(layout->tile_w+2*layout->tile_border_x);
280                 *y = layout->tile_border_y + ((tile)/layout->columns)*(layout->tile_h+2*layout->tile_border_y);
281         }
282 }
283
284 float file_string_width(const char* str)
285 {
286         uiStyle *style= U.uistyles.first;
287         uiStyleFontSet(&style->widget);
288         return BLF_width(style->widget.uifont_id, (char *)str);
289 }
290
291 float file_font_pointsize()
292 {
293         float s;
294         char tmp[2] = "X";
295         uiStyle *style= U.uistyles.first;
296         uiStyleFontSet(&style->widget);
297         s = BLF_height(style->widget.uifont_id, tmp);
298         return style->widget.points;
299 }
300
301 static void column_widths(struct FileList* files, struct FileLayout* layout)
302 {
303         int i;
304         int numfiles = filelist_numfiles(files);
305
306         for (i=0; i<MAX_FILE_COLUMN; ++i) {
307                 layout->column_widths[i] = 0;
308         }
309
310         for (i=0; (i < numfiles); ++i)
311         {
312                 struct direntry* file = filelist_file(files, i);        
313                 if (file) {
314                         int len;
315                         len = file_string_width(file->relname);
316                         if (len > layout->column_widths[COLUMN_NAME]) layout->column_widths[COLUMN_NAME] = len + 20;
317                         len = file_string_width(file->date);
318                         if (len > layout->column_widths[COLUMN_DATE]) layout->column_widths[COLUMN_DATE] = len;
319                         len = file_string_width(file->time);
320                         if (len > layout->column_widths[COLUMN_TIME]) layout->column_widths[COLUMN_TIME] = len;
321                         len = file_string_width(file->size);
322                         if (len > layout->column_widths[COLUMN_SIZE]) layout->column_widths[COLUMN_SIZE] = len;
323                         len = file_string_width(file->mode1);
324                         if (len > layout->column_widths[COLUMN_MODE1]) layout->column_widths[COLUMN_MODE1] = len;
325                         len = file_string_width(file->mode2);
326                         if (len > layout->column_widths[COLUMN_MODE2]) layout->column_widths[COLUMN_MODE2] = len;
327                         len = file_string_width(file->mode3);
328                         if (len > layout->column_widths[COLUMN_MODE3]) layout->column_widths[COLUMN_MODE3] = len;
329                         len = file_string_width(file->owner);
330                         if (len > layout->column_widths[COLUMN_OWNER]) layout->column_widths[COLUMN_OWNER] = len;
331                 }
332         }
333 }
334
335 void ED_fileselect_init_layout(struct SpaceFile *sfile, struct ARegion *ar)
336 {
337         FileSelectParams *params = ED_fileselect_get_params(sfile);
338         FileLayout *layout=0;
339         View2D *v2d= &ar->v2d;
340         int maxlen = 0;
341         int numfiles;
342         int textheight;
343         if (sfile->layout == 0) {
344                 sfile->layout = MEM_callocN(sizeof(struct FileLayout), "file_layout");
345                 sfile->layout->dirty = 1;
346         } 
347
348         if (!sfile->layout->dirty) return;
349
350         numfiles = filelist_numfiles(sfile->files);
351         textheight = file_font_pointsize();
352         layout = sfile->layout;
353         layout->textheight = textheight;
354
355         if (params->display == FILE_IMGDISPLAY) {
356                 layout->prv_w = 96;
357                 layout->prv_h = 96;
358                 layout->tile_border_x = 6;
359                 layout->tile_border_y = 6;
360                 layout->prv_border_x = 6;
361                 layout->prv_border_y = 6;
362                 layout->tile_w = layout->prv_w + 2*layout->prv_border_x;
363                 layout->tile_h = layout->prv_h + 2*layout->prv_border_y + textheight;
364                 layout->width= (v2d->cur.xmax - v2d->cur.xmin - 2*layout->tile_border_x);
365                 layout->columns= layout->width / (layout->tile_w + 2*layout->tile_border_x);
366                 if(layout->columns > 0)
367                         layout->rows= numfiles/layout->columns + 1; // XXX dirty, modulo is zero
368                 else {
369                         layout->columns = 1;
370                         layout->rows= numfiles + 1; // XXX dirty, modulo is zero
371                 }
372                 layout->height= sfile->layout->rows*(layout->tile_h+2*layout->tile_border_y) + layout->tile_border_y*2;
373                 layout->flag = FILE_LAYOUT_VER;
374         } else {
375                 layout->prv_w = 0;
376                 layout->prv_h = 0;
377                 layout->tile_border_x = 8;
378                 layout->tile_border_y = 2;
379                 layout->prv_border_x = 0;
380                 layout->prv_border_y = 0;
381                 layout->tile_h = textheight*3/2;
382                 layout->height= v2d->cur.ymax - v2d->cur.ymin - 2*layout->tile_border_y;
383                 layout->rows = layout->height / (layout->tile_h + 2*layout->tile_border_y);
384         
385                 column_widths(sfile->files, layout);
386
387                 if (params->display == FILE_SHORTDISPLAY) {
388                         maxlen = layout->column_widths[COLUMN_NAME] + 12 +
389                                          layout->column_widths[COLUMN_SIZE];
390                         maxlen += 20; // for icon
391                 } else {
392                         maxlen = layout->column_widths[COLUMN_NAME] + 12 +
393 #ifndef WIN32
394                                          layout->column_widths[COLUMN_MODE1] + 12 +
395                                          layout->column_widths[COLUMN_MODE2] + 12 +
396                                          layout->column_widths[COLUMN_MODE3] + 12 +
397                                          layout->column_widths[COLUMN_OWNER] + 12 +
398 #endif
399                                          layout->column_widths[COLUMN_DATE] + 12 +
400                                          layout->column_widths[COLUMN_TIME] + 12 +
401                                          layout->column_widths[COLUMN_SIZE];
402                         maxlen += 20; // for icon
403                 }
404                 layout->tile_w = maxlen;
405                 if(layout->rows > 0)
406                         layout->columns = numfiles/layout->rows + 1; // XXX dirty, modulo is zero
407                 else {
408                         layout->rows = 1;
409                         layout->columns = numfiles + 1; // XXX dirty, modulo is zero
410                 }
411                 layout->width = sfile->layout->columns * (layout->tile_w + 2*layout->tile_border_x) + layout->tile_border_x*2;
412                 layout->flag = FILE_LAYOUT_HOR;
413         }
414         layout->dirty= 0;
415 }
416
417 FileLayout* ED_fileselect_get_layout(struct SpaceFile *sfile, struct ARegion *ar)
418 {
419         if (!sfile->layout) {
420                 ED_fileselect_init_layout(sfile, ar);
421         }
422         return sfile->layout;
423 }
424
425 void file_change_dir(bContext *C, int checkdir)
426 {
427         SpaceFile *sfile= CTX_wm_space_file(C);
428
429         if (sfile->params) {
430
431                 ED_fileselect_clear(C, sfile);
432
433                 if(checkdir && BLI_is_dir(sfile->params->dir)==0) {
434                         BLI_strncpy(sfile->params->dir, filelist_dir(sfile->files), sizeof(sfile->params->dir));
435                         /* could return but just refresh the current dir */
436                 }
437                 filelist_setdir(sfile->files, sfile->params->dir);
438                 
439                 if(folderlist_clear_next(sfile))
440                         folderlist_free(sfile->folders_next);
441
442                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
443
444         }
445 }
446
447 int file_select_match(struct SpaceFile *sfile, const char *pattern)
448 {
449         int match = 0;
450         if (strchr(pattern, '*') || strchr(pattern, '?') || strchr(pattern, '[')) {
451                 int i;
452                 struct direntry *file;
453                 int n = filelist_numfiles(sfile->files);
454
455                 for (i = 0; i < n; i++) {
456                         file = filelist_file(sfile->files, i);
457                         if (fnmatch(pattern, file->relname, 0) == 0) {
458                                 file->flags |= ACTIVEFILE;
459                                 match = 1;
460                         }
461                 }
462         }
463         return match;
464 }
465
466 void autocomplete_directory(struct bContext *C, char *str, void *arg_v)
467 {
468         SpaceFile *sfile= CTX_wm_space_file(C);
469
470         /* search if str matches the beginning of name */
471         if(str[0] && sfile->files) {
472                 char dirname[FILE_MAX];
473
474                 DIR *dir;
475                 struct dirent *de;
476                 
477                 BLI_split_dirfile(str, dirname, NULL);
478
479                 dir = opendir(dirname);
480
481                 if(dir) {
482                         AutoComplete *autocpl= autocomplete_begin(str, FILE_MAX);
483
484                         while ((de = readdir(dir)) != NULL) {
485                                 if (strcmp(".", de->d_name)==0 || strcmp("..", de->d_name)==0) {
486                                         /* pass */
487                                 }
488                                 else {
489                                         char path[FILE_MAX];
490                                         struct stat status;
491                                         
492                                         BLI_join_dirfile(path, dirname, de->d_name);
493
494                                         if (stat(path, &status) == 0) {
495                                                 if (S_ISDIR(status.st_mode)) { /* is subdir */
496                                                         autocomplete_do_name(autocpl, path);
497                                                 }
498                                         }
499                                 }
500                         }
501                         closedir(dir);
502
503                         autocomplete_end(autocpl, str);
504                         if (BLI_exists(str)) {
505                                 BLI_add_slash(str);
506                         } else {
507                                 BLI_strncpy(sfile->params->dir, str, sizeof(sfile->params->dir));
508                         }
509                 }
510         }
511 }
512
513 void autocomplete_file(struct bContext *C, char *str, void *arg_v)
514 {
515         SpaceFile *sfile= CTX_wm_space_file(C);
516
517         /* search if str matches the beginning of name */
518         if(str[0] && sfile->files) {
519                 AutoComplete *autocpl= autocomplete_begin(str, FILE_MAX);
520                 int nentries = filelist_numfiles(sfile->files);
521                 int i;
522
523                 for(i= 0; i<nentries; ++i) {
524                         struct direntry* file = filelist_file(sfile->files, i);
525                         if (file && S_ISREG(file->type)) {
526                                 autocomplete_do_name(autocpl, file->relname);
527                         }
528                 }
529                 autocomplete_end(autocpl, str);
530         }
531 }
532
533 void ED_fileselect_clear(struct bContext *C, struct SpaceFile *sfile)
534 {
535         thumbnails_stop(sfile->files, C);
536         filelist_freelib(sfile->files);
537         filelist_free(sfile->files);
538         sfile->params->active_file = -1;
539         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
540 }
541
542 void ED_fileselect_exit(struct bContext *C, struct SpaceFile *sfile)
543 {
544         if(!sfile) return;
545         if(sfile->op)
546                 WM_event_fileselect_event(C, sfile->op, EVT_FILESELECT_EXTERNAL_CANCEL);
547         sfile->op = NULL;
548
549         folderlist_free(sfile->folders_prev);
550         folderlist_free(sfile->folders_next);
551         
552         if (sfile->files) {
553                 ED_fileselect_clear(C, sfile);
554                 MEM_freeN(sfile->files);
555                 sfile->files= NULL;
556         }
557
558 }