doxygen: blender/editors tagged.
[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 = ar->v2d.cur.xmax - ar->v2d.cur.xmin - 2*layout->tile_border_x;
251                 numfiles = (float)width/(float)layout->tile_w+0.5;
252                 return numfiles*layout->rows;
253         } else {
254                 int height = ar->v2d.cur.ymax - ar->v2d.cur.ymin - 2*layout->tile_border_y;
255                 numfiles = (float)height/(float)layout->tile_h+0.5;
256                 return numfiles*layout->columns;
257         }
258 }
259
260 int ED_fileselect_layout_offset(FileLayout* layout, int clamp_bounds, int x, int y)
261 {
262         int offsetx, offsety;
263         int active_file;
264
265         if (layout == NULL)
266                 return 0;
267         
268         offsetx = (x)/(layout->tile_w + 2*layout->tile_border_x);
269         offsety = (y)/(layout->tile_h + 2*layout->tile_border_y);
270         
271         if (clamp_bounds) {
272                 CLAMP(offsetx, 0, layout->columns-1);
273                 CLAMP(offsety, 0, layout->rows-1);
274         } else {
275                 if (offsetx > layout->columns-1) return -1 ;
276                 if (offsety > layout->rows-1) return -1 ;
277         }
278         
279         if (layout->flag & FILE_LAYOUT_HOR) 
280                 active_file = layout->rows*offsetx + offsety;
281         else
282                 active_file = offsetx + layout->columns*offsety;
283         return active_file;
284 }
285
286 void ED_fileselect_layout_tilepos(FileLayout* layout, int tile, int *x, int *y)
287 {
288         if (layout->flag == FILE_LAYOUT_HOR) {
289                 *x = layout->tile_border_x + (tile/layout->rows)*(layout->tile_w+2*layout->tile_border_x);
290                 *y = layout->tile_border_y + (tile%layout->rows)*(layout->tile_h+2*layout->tile_border_y);
291         } else {
292                 *x = layout->tile_border_x + ((tile)%layout->columns)*(layout->tile_w+2*layout->tile_border_x);
293                 *y = layout->tile_border_y + ((tile)/layout->columns)*(layout->tile_h+2*layout->tile_border_y);
294         }
295 }
296
297 /* Shorten a string to a given width w. 
298    If front is set, shorten from the front,
299    otherwise shorten from the end. */
300 float file_shorten_string(char* string, float w, int front)
301 {       
302         char temp[FILE_MAX];
303         short shortened = 0;
304         float sw = 0;
305         float pad = 0;
306
307         if (w <= 0) {
308                 *string = '\0';
309                 return 0.0;
310         }
311
312         sw = file_string_width(string);
313         if (front == 1) {
314                 char *s = string;
315                 BLI_strncpy(temp, "...", 4);
316                 pad = file_string_width(temp);
317                 while ((*s) && (sw+pad>w)) {
318                         s++;
319                         sw = file_string_width(s);
320                         shortened = 1;
321                 }
322                 if (shortened) {
323                         int slen = strlen(s);                   
324                         BLI_strncpy(temp+3, s, slen+1);
325                         temp[slen+4] = '\0';
326                         BLI_strncpy(string, temp, slen+4);
327                 }
328         } else {
329                 char *s = string;
330                 while (sw>w) {
331                         int slen = strlen(string);
332                         string[slen-1] = '\0';
333                         sw = file_string_width(s);
334                         shortened = 1;
335                 }
336
337                 if (shortened) {
338                         int slen = strlen(string);
339                         if (slen > 3) {
340                                 BLI_strncpy(string+slen-3, "...", 4);                           
341                         }
342                 }
343         }
344         
345         return sw;
346 }
347
348 float file_string_width(const char* str)
349 {
350         uiStyle *style= U.uistyles.first;
351         uiStyleFontSet(&style->widget);
352         return BLF_width(style->widget.uifont_id, str);
353 }
354
355 float file_font_pointsize(void)
356 {
357 #if 0
358         float s;
359         char tmp[2] = "X";
360         uiStyle *style= U.uistyles.first;
361         uiStyleFontSet(&style->widget);
362         s = BLF_height(style->widget.uifont_id, tmp);
363         return style->widget.points;
364 #else
365         uiStyle *style= U.uistyles.first;
366         uiStyleFontSet(&style->widget);
367         return style->widget.points;
368 #endif
369 }
370
371 static void column_widths(struct FileList* files, struct FileLayout* layout)
372 {
373         int i;
374         int numfiles = filelist_numfiles(files);
375
376         for (i=0; i<MAX_FILE_COLUMN; ++i) {
377                 layout->column_widths[i] = 0;
378         }
379
380         for (i=0; (i < numfiles); ++i)
381         {
382                 struct direntry* file = filelist_file(files, i);        
383                 if (file) {
384                         int len;
385                         len = file_string_width(file->relname);
386                         if (len > layout->column_widths[COLUMN_NAME]) layout->column_widths[COLUMN_NAME] = len;
387                         len = file_string_width(file->date);
388                         if (len > layout->column_widths[COLUMN_DATE]) layout->column_widths[COLUMN_DATE] = len;
389                         len = file_string_width(file->time);
390                         if (len > layout->column_widths[COLUMN_TIME]) layout->column_widths[COLUMN_TIME] = len;
391                         len = file_string_width(file->size);
392                         if (len > layout->column_widths[COLUMN_SIZE]) layout->column_widths[COLUMN_SIZE] = len;
393                         len = file_string_width(file->mode1);
394                         if (len > layout->column_widths[COLUMN_MODE1]) layout->column_widths[COLUMN_MODE1] = len;
395                         len = file_string_width(file->mode2);
396                         if (len > layout->column_widths[COLUMN_MODE2]) layout->column_widths[COLUMN_MODE2] = len;
397                         len = file_string_width(file->mode3);
398                         if (len > layout->column_widths[COLUMN_MODE3]) layout->column_widths[COLUMN_MODE3] = len;
399                         len = file_string_width(file->owner);
400                         if (len > layout->column_widths[COLUMN_OWNER]) layout->column_widths[COLUMN_OWNER] = len;
401                 }
402         }
403 }
404
405 void ED_fileselect_init_layout(struct SpaceFile *sfile, struct ARegion *ar)
406 {
407         FileSelectParams *params = ED_fileselect_get_params(sfile);
408         FileLayout *layout=0;
409         View2D *v2d= &ar->v2d;
410         int maxlen = 0;
411         int numfiles;
412         int textheight;
413         if (sfile->layout == 0) {
414                 sfile->layout = MEM_callocN(sizeof(struct FileLayout), "file_layout");
415                 sfile->layout->dirty = 1;
416         } 
417
418         if (!sfile->layout->dirty) return;
419
420         numfiles = filelist_numfiles(sfile->files);
421         textheight = file_font_pointsize();
422         layout = sfile->layout;
423         layout->textheight = textheight;
424
425         if (params->display == FILE_IMGDISPLAY) {
426                 layout->prv_w = 96;
427                 layout->prv_h = 96;
428                 layout->tile_border_x = 6;
429                 layout->tile_border_y = 6;
430                 layout->prv_border_x = 6;
431                 layout->prv_border_y = 6;
432                 layout->tile_w = layout->prv_w + 2*layout->prv_border_x;
433                 layout->tile_h = layout->prv_h + 2*layout->prv_border_y + textheight;
434                 layout->width= (v2d->cur.xmax - v2d->cur.xmin - 2*layout->tile_border_x);
435                 layout->columns= layout->width / (layout->tile_w + 2*layout->tile_border_x);
436                 if(layout->columns > 0)
437                         layout->rows= numfiles/layout->columns + 1; // XXX dirty, modulo is zero
438                 else {
439                         layout->columns = 1;
440                         layout->rows= numfiles + 1; // XXX dirty, modulo is zero
441                 }
442                 layout->height= sfile->layout->rows*(layout->tile_h+2*layout->tile_border_y) + layout->tile_border_y*2;
443                 layout->flag = FILE_LAYOUT_VER;
444         } else {
445                 layout->prv_w = 0;
446                 layout->prv_h = 0;
447                 layout->tile_border_x = 8;
448                 layout->tile_border_y = 2;
449                 layout->prv_border_x = 0;
450                 layout->prv_border_y = 0;
451                 layout->tile_h = textheight*3/2;
452                 layout->height= v2d->cur.ymax - v2d->cur.ymin - 2*layout->tile_border_y;
453                 layout->rows = layout->height / (layout->tile_h + 2*layout->tile_border_y);
454         
455                 column_widths(sfile->files, layout);
456
457                 if (params->display == FILE_SHORTDISPLAY) {
458                         maxlen = ICON_DEFAULT_WIDTH + 4 +
459                                          layout->column_widths[COLUMN_NAME] + 12 +
460                                          layout->column_widths[COLUMN_SIZE] + 12;
461                 } else {
462                         maxlen = ICON_DEFAULT_WIDTH + 4 +
463                                          layout->column_widths[COLUMN_NAME] + 12 +
464 #ifndef WIN32
465                                          layout->column_widths[COLUMN_MODE1] + 12 +
466                                          layout->column_widths[COLUMN_MODE2] + 12 +
467                                          layout->column_widths[COLUMN_MODE3] + 12 +
468                                          layout->column_widths[COLUMN_OWNER] + 12 +
469 #endif
470                                          layout->column_widths[COLUMN_DATE] + 12 +
471                                          layout->column_widths[COLUMN_TIME] + 12 +
472                                          layout->column_widths[COLUMN_SIZE] + 12;
473
474                 }
475                 layout->tile_w = maxlen;
476                 if(layout->rows > 0)
477                         layout->columns = numfiles/layout->rows + 1; // XXX dirty, modulo is zero
478                 else {
479                         layout->rows = 1;
480                         layout->columns = numfiles + 1; // XXX dirty, modulo is zero
481                 }
482                 layout->width = sfile->layout->columns * (layout->tile_w + 2*layout->tile_border_x) + layout->tile_border_x*2;
483                 layout->flag = FILE_LAYOUT_HOR;
484         }
485         layout->dirty= 0;
486 }
487
488 FileLayout* ED_fileselect_get_layout(struct SpaceFile *sfile, struct ARegion *ar)
489 {
490         if (!sfile->layout) {
491                 ED_fileselect_init_layout(sfile, ar);
492         }
493         return sfile->layout;
494 }
495
496 void file_change_dir(bContext *C, int checkdir)
497 {
498         SpaceFile *sfile= CTX_wm_space_file(C);
499
500         if (sfile->params) {
501
502                 ED_fileselect_clear(C, sfile);
503
504                 if(checkdir && BLI_is_dir(sfile->params->dir)==0) {
505                         BLI_strncpy(sfile->params->dir, filelist_dir(sfile->files), sizeof(sfile->params->dir));
506                         /* could return but just refresh the current dir */
507                 }
508                 filelist_setdir(sfile->files, sfile->params->dir);
509                 
510                 if(folderlist_clear_next(sfile))
511                         folderlist_free(sfile->folders_next);
512
513                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
514
515         }
516 }
517
518 int file_select_match(struct SpaceFile *sfile, const char *pattern)
519 {
520         int match = 0;
521         if (strchr(pattern, '*') || strchr(pattern, '?') || strchr(pattern, '[')) {
522                 int i;
523                 struct direntry *file;
524                 int n = filelist_numfiles(sfile->files);
525
526                 for (i = 0; i < n; i++) {
527                         file = filelist_file(sfile->files, i);
528                         if (fnmatch(pattern, file->relname, 0) == 0) {
529                                 file->flags |= ACTIVEFILE;
530                                 match = 1;
531                         }
532                 }
533         }
534         return match;
535 }
536
537 void autocomplete_directory(struct bContext *C, char *str, void *UNUSED(arg_v))
538 {
539         SpaceFile *sfile= CTX_wm_space_file(C);
540
541         /* search if str matches the beginning of name */
542         if(str[0] && sfile->files) {
543                 char dirname[FILE_MAX];
544
545                 DIR *dir;
546                 struct dirent *de;
547                 
548                 BLI_split_dirfile(str, dirname, NULL);
549
550                 dir = opendir(dirname);
551
552                 if(dir) {
553                         AutoComplete *autocpl= autocomplete_begin(str, FILE_MAX);
554
555                         while ((de = readdir(dir)) != NULL) {
556                                 if (strcmp(".", de->d_name)==0 || strcmp("..", de->d_name)==0) {
557                                         /* pass */
558                                 }
559                                 else {
560                                         char path[FILE_MAX];
561                                         struct stat status;
562                                         
563                                         BLI_join_dirfile(path, sizeof(path), dirname, de->d_name);
564
565                                         if (stat(path, &status) == 0) {
566                                                 if (S_ISDIR(status.st_mode)) { /* is subdir */
567                                                         autocomplete_do_name(autocpl, path);
568                                                 }
569                                         }
570                                 }
571                         }
572                         closedir(dir);
573
574                         autocomplete_end(autocpl, str);
575                         if (BLI_exists(str)) {
576                                 BLI_add_slash(str);
577                         } else {
578                                 BLI_strncpy(sfile->params->dir, str, sizeof(sfile->params->dir));
579                         }
580                 }
581         }
582 }
583
584 void autocomplete_file(struct bContext *C, char *str, void *UNUSED(arg_v))
585 {
586         SpaceFile *sfile= CTX_wm_space_file(C);
587
588         /* search if str matches the beginning of name */
589         if(str[0] && sfile->files) {
590                 AutoComplete *autocpl= autocomplete_begin(str, FILE_MAX);
591                 int nentries = filelist_numfiles(sfile->files);
592                 int i;
593
594                 for(i= 0; i<nentries; ++i) {
595                         struct direntry* file = filelist_file(sfile->files, i);
596                         if (file && S_ISREG(file->type)) {
597                                 autocomplete_do_name(autocpl, file->relname);
598                         }
599                 }
600                 autocomplete_end(autocpl, str);
601         }
602 }
603
604 void ED_fileselect_clear(struct bContext *C, struct SpaceFile *sfile)
605 {
606         thumbnails_stop(sfile->files, C);
607         filelist_freelib(sfile->files);
608         filelist_free(sfile->files);
609         sfile->params->active_file = -1;
610         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
611 }
612
613 void ED_fileselect_exit(struct bContext *C, struct SpaceFile *sfile)
614 {
615         if(!sfile) return;
616         if(sfile->op)
617                 WM_event_fileselect_event(C, sfile->op, EVT_FILESELECT_EXTERNAL_CANCEL);
618         sfile->op = NULL;
619
620         folderlist_free(sfile->folders_prev);
621         folderlist_free(sfile->folders_next);
622         
623         if (sfile->files) {
624                 ED_fileselect_clear(C, sfile);
625                 MEM_freeN(sfile->files);
626                 sfile->files= NULL;
627         }
628
629 }