5d1a765f45700ba52d238aa62ebe57681993980f
[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                         params->flag |= FILE_FILTER;
165
166                 if (U.uiflag & USER_HIDE_DOT) {
167                         params->flag |= FILE_HIDE_DOT;
168                 } else {
169                         params->flag &= ~FILE_HIDE_DOT;
170                 }
171                 
172
173                 if (params->type == FILE_LOADLIB) {
174                         params->flag |= RNA_boolean_get(op->ptr, "link") ? FILE_LINK : 0;
175                         params->flag |= RNA_boolean_get(op->ptr, "autoselect") ? FILE_AUTOSELECT : 0;
176                         params->flag |= RNA_boolean_get(op->ptr, "active_layer") ? FILE_ACTIVELAY : 0;
177                 }
178
179                 if(params->filter & (IMAGEFILE|MOVIEFILE))
180                         params->display= FILE_IMGDISPLAY;
181                 else
182                         params->display= FILE_SHORTDISPLAY;
183                 
184         } else {
185                 /* default values, if no operator */
186                 params->type = FILE_UNIX;
187                 params->flag |= FILE_HIDE_DOT;
188                 params->display = FILE_SHORTDISPLAY;
189                 params->filter = 0;
190                 params->sort = FILE_SORT_ALPHA;
191         }
192         return 1;
193 }
194
195 void ED_fileselect_reset_params(SpaceFile *sfile)
196 {
197         sfile->params->type = FILE_UNIX;
198         sfile->params->flag = 0;
199         sfile->params->title[0] = '\0';
200 }
201
202 int ED_fileselect_layout_numfiles(FileLayout* layout, struct ARegion *ar)
203 {
204         int numfiles;
205
206         if (layout->flag & FILE_LAYOUT_HOR) {
207                 int width = ar->v2d.cur.xmax - ar->v2d.cur.xmin - 2*layout->tile_border_x;
208                 numfiles = width/layout->tile_w + 1;
209                 return numfiles*layout->rows;
210         } else {
211                 int height = ar->v2d.cur.ymax - ar->v2d.cur.ymin - 2*layout->tile_border_y;
212                 numfiles = height/layout->tile_h + 1;
213                 return numfiles*layout->columns;
214         }
215 }
216
217 int ED_fileselect_layout_offset(FileLayout* layout, int clamp_bounds, int x, int y)
218 {
219         int offsetx, offsety;
220         int active_file;
221
222         if (layout == NULL)
223                 return 0;
224         
225         offsetx = (x)/(layout->tile_w + 2*layout->tile_border_x);
226         offsety = (y)/(layout->tile_h + 2*layout->tile_border_y);
227         
228         if (clamp_bounds) {
229                 CLAMP(offsetx, 0, layout->columns-1);
230                 CLAMP(offsety, 0, layout->rows-1);
231         } else {
232                 if (offsetx > layout->columns-1) return -1 ;
233                 if (offsety > layout->rows-1) return -1 ;
234         }
235         
236         if (layout->flag & FILE_LAYOUT_HOR) 
237                 active_file = layout->rows*offsetx + offsety;
238         else
239                 active_file = offsetx + layout->columns*offsety;
240         return active_file;
241 }
242
243 void ED_fileselect_layout_tilepos(FileLayout* layout, int tile, int *x, int *y)
244 {
245         if (layout->flag == FILE_LAYOUT_HOR) {
246                 *x = layout->tile_border_x + (tile/layout->rows)*(layout->tile_w+2*layout->tile_border_x);
247                 *y = layout->tile_border_y + (tile%layout->rows)*(layout->tile_h+2*layout->tile_border_y);
248         } else {
249                 *x = layout->tile_border_x + ((tile)%layout->columns)*(layout->tile_w+2*layout->tile_border_x);
250                 *y = layout->tile_border_y + ((tile)/layout->columns)*(layout->tile_h+2*layout->tile_border_y);
251         }
252 }
253
254 float file_string_width(const char* str)
255 {
256         uiStyle *style= U.uistyles.first;
257         uiStyleFontSet(&style->widget);
258         return BLF_width((char *)str);
259 }
260
261 float file_font_pointsize()
262 {
263         float s;
264         char tmp[2] = "X";
265         uiStyle *style= U.uistyles.first;
266         uiStyleFontSet(&style->widget);
267         s = BLF_height(tmp);
268         return style->widget.points;
269 }
270
271 static void column_widths(struct FileList* files, struct FileLayout* layout)
272 {
273         int i;
274         int numfiles = filelist_numfiles(files);
275
276         for (i=0; i<MAX_FILE_COLUMN; ++i) {
277                 layout->column_widths[i] = 0;
278         }
279
280         for (i=0; (i < numfiles); ++i)
281         {
282                 struct direntry* file = filelist_file(files, i);        
283                 if (file) {
284                         int len;
285                         len = file_string_width(file->relname);
286                         if (len > layout->column_widths[COLUMN_NAME]) layout->column_widths[COLUMN_NAME] = len + 20;
287                         len = file_string_width(file->date);
288                         if (len > layout->column_widths[COLUMN_DATE]) layout->column_widths[COLUMN_DATE] = len;
289                         len = file_string_width(file->time);
290                         if (len > layout->column_widths[COLUMN_TIME]) layout->column_widths[COLUMN_TIME] = len;
291                         len = file_string_width(file->size);
292                         if (len > layout->column_widths[COLUMN_SIZE]) layout->column_widths[COLUMN_SIZE] = len;
293                         len = file_string_width(file->mode1);
294                         if (len > layout->column_widths[COLUMN_MODE1]) layout->column_widths[COLUMN_MODE1] = len;
295                         len = file_string_width(file->mode2);
296                         if (len > layout->column_widths[COLUMN_MODE2]) layout->column_widths[COLUMN_MODE2] = len;
297                         len = file_string_width(file->mode3);
298                         if (len > layout->column_widths[COLUMN_MODE3]) layout->column_widths[COLUMN_MODE3] = len;
299                         len = file_string_width(file->owner);
300                         if (len > layout->column_widths[COLUMN_OWNER]) layout->column_widths[COLUMN_OWNER] = len;
301                 }
302         }
303 }
304
305 void ED_fileselect_init_layout(struct SpaceFile *sfile, struct ARegion *ar)
306 {
307         FileSelectParams *params = ED_fileselect_get_params(sfile);
308         FileLayout *layout=0;
309         View2D *v2d= &ar->v2d;
310         int maxlen = 0;
311         int numfiles;
312         int textheight;
313         if (sfile->layout == 0) {
314                 sfile->layout = MEM_callocN(sizeof(struct FileLayout), "file_layout");
315                 sfile->layout->dirty = 1;
316         } 
317
318         if (!sfile->layout->dirty) return;
319
320         numfiles = filelist_numfiles(sfile->files);
321         textheight = file_font_pointsize();
322         layout = sfile->layout;
323         layout->textheight = textheight;
324
325         if (params->display == FILE_IMGDISPLAY) {
326                 layout->prv_w = 96;
327                 layout->prv_h = 96;
328                 layout->tile_border_x = 6;
329                 layout->tile_border_y = 6;
330                 layout->prv_border_x = 6;
331                 layout->prv_border_y = 6;
332                 layout->tile_w = layout->prv_w + 2*layout->prv_border_x;
333                 layout->tile_h = layout->prv_h + 2*layout->prv_border_y + textheight;
334                 layout->width= (v2d->cur.xmax - v2d->cur.xmin - 2*layout->tile_border_x);
335                 layout->columns= layout->width / (layout->tile_w + 2*layout->tile_border_x);
336                 if(layout->columns > 0)
337                         layout->rows= numfiles/layout->columns + 1; // XXX dirty, modulo is zero
338                 else {
339                         layout->columns = 1;
340                         layout->rows= numfiles + 1; // XXX dirty, modulo is zero
341                 }
342                 layout->height= sfile->layout->rows*(layout->tile_h+2*layout->tile_border_y) + layout->tile_border_y*2;
343                 layout->flag = FILE_LAYOUT_VER;
344         } else {
345                 layout->prv_w = 0;
346                 layout->prv_h = 0;
347                 layout->tile_border_x = 8;
348                 layout->tile_border_y = 2;
349                 layout->prv_border_x = 0;
350                 layout->prv_border_y = 0;
351                 layout->tile_h = textheight*3/2;
352                 layout->height= v2d->cur.ymax - v2d->cur.ymin - 2*layout->tile_border_y;
353                 layout->rows = layout->height / (layout->tile_h + 2*layout->tile_border_y);
354         
355                 column_widths(sfile->files, layout);
356
357                 if (params->display == FILE_SHORTDISPLAY) {
358                         maxlen = layout->column_widths[COLUMN_NAME] + 12 +
359                                          layout->column_widths[COLUMN_SIZE];
360                         maxlen += 20; // for icon
361                 } else {
362                         maxlen = layout->column_widths[COLUMN_NAME] + 12 +
363 #ifndef WIN32
364                                          layout->column_widths[COLUMN_MODE1] + 12 +
365                                          layout->column_widths[COLUMN_MODE2] + 12 +
366                                          layout->column_widths[COLUMN_MODE3] + 12 +
367                                          layout->column_widths[COLUMN_OWNER] + 12 +
368 #endif
369                                          layout->column_widths[COLUMN_DATE] + 12 +
370                                          layout->column_widths[COLUMN_TIME] + 12 +
371                                          layout->column_widths[COLUMN_SIZE];
372                         maxlen += 20; // for icon
373                 }
374                 layout->tile_w = maxlen;
375                 if(layout->rows > 0)
376                         layout->columns = numfiles/layout->rows + 1; // XXX dirty, modulo is zero
377                 else {
378                         layout->rows = 1;
379                         layout->columns = numfiles + 1; // XXX dirty, modulo is zero
380                 }
381                 layout->width = sfile->layout->columns * (layout->tile_w + 2*layout->tile_border_x) + layout->tile_border_x*2;
382                 layout->flag = FILE_LAYOUT_HOR;
383         }
384         layout->dirty= 0;
385 }
386
387 FileLayout* ED_fileselect_get_layout(struct SpaceFile *sfile, struct ARegion *ar)
388 {
389         if (!sfile->layout) {
390                 ED_fileselect_init_layout(sfile, ar);
391         }
392         return sfile->layout;
393 }
394
395 void file_change_dir(bContext *C, int checkdir)
396 {
397         SpaceFile *sfile= CTX_wm_space_file(C);
398
399         if (sfile->params) {
400
401                 ED_fileselect_clear(C, sfile);
402
403                 if(checkdir && BLI_is_dir(sfile->params->dir)==0) {
404                         BLI_strncpy(sfile->params->dir, filelist_dir(sfile->files), sizeof(sfile->params->dir));
405                         /* could return but just refresh the current dir */
406                 }
407                 filelist_setdir(sfile->files, sfile->params->dir);
408                 
409                 if(folderlist_clear_next(sfile))
410                         folderlist_free(sfile->folders_next);
411
412                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
413
414         }
415 }
416
417 int file_select_match(struct SpaceFile *sfile, const char *pattern)
418 {
419         int match = 0;
420         if (strchr(pattern, '*') || strchr(pattern, '?') || strchr(pattern, '[')) {
421                 int i;
422                 struct direntry *file;
423                 int n = filelist_numfiles(sfile->files);
424
425                 for (i = 0; i < n; i++) {
426                         file = filelist_file(sfile->files, i);
427                         if (fnmatch(pattern, file->relname, 0) == 0) {
428                                 file->flags |= ACTIVE;
429                                 match = 1;
430                         }
431                 }
432         }
433         return match;
434 }
435
436
437 void autocomplete_directory(struct bContext *C, char *str, void *arg_v)
438 {
439         char tmp[FILE_MAX];
440         SpaceFile *sfile= CTX_wm_space_file(C);
441
442         /* search if str matches the beginning of name */
443         if(str[0] && sfile->files) {
444                 AutoComplete *autocpl= autocomplete_begin(str, FILE_MAX);
445                 int nentries = filelist_numfiles(sfile->files);
446                 int i;
447
448                 for(i= 0; i<nentries; ++i) {
449                         struct direntry* file = filelist_file(sfile->files, i);
450                         const char* dir = filelist_dir(sfile->files);
451                         if (file && S_ISDIR(file->type))        {
452                                 // BLI_make_file_string(G.sce, tmp, dir, file->relname);
453                                 BLI_join_dirfile(tmp, dir, file->relname);
454                                 autocomplete_do_name(autocpl,tmp);
455                         }
456                 }
457                 autocomplete_end(autocpl, str);
458                 if (BLI_exists(str)) {
459                         BLI_add_slash(str);
460                 } else {
461                         BLI_make_exist(str);
462                 }
463         }
464 }
465
466 void ED_fileselect_clear(struct bContext *C, struct SpaceFile *sfile)
467 {
468         thumbnails_stop(sfile->files, C);
469         filelist_freelib(sfile->files);
470         filelist_free(sfile->files);
471         sfile->params->active_file = -1;
472         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
473 }
474
475 void ED_fileselect_exit(struct bContext *C, struct SpaceFile *sfile)
476 {
477         thumbnails_stop(sfile->files, C);
478 }