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