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