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