2.5 filebrowser
[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                 params->type = RNA_int_get(op->ptr, "type");
128
129                 if (RNA_property_is_set(op->ptr, "path")) {
130                         RNA_string_get(op->ptr, "path", name);
131                         if (params->type == FILE_LOADLIB) {
132                                 BLI_strncpy(params->dir, name, sizeof(params->dir));
133                                 BLI_cleanup_dir(G.sce, params->dir);    
134                         } else { 
135                                 /* if operator has path set, use it, otherwise keep the last */
136                                 BLI_convertstringcode(name, G.sce);
137                                 BLI_split_dirfile(name, dir, file);
138                                 BLI_strncpy(params->file, file, sizeof(params->file));
139                                 BLI_make_file_string(G.sce, params->dir, dir, ""); /* XXX needed ? - also solve G.sce */
140                         }
141                 }
142                 params->filter = 0;
143                 params->filter |= RNA_boolean_get(op->ptr, "filter_blender") ? BLENDERFILE : 0;
144                 params->filter |= RNA_boolean_get(op->ptr, "filter_image") ? IMAGEFILE : 0;
145                 params->filter |= RNA_boolean_get(op->ptr, "filter_movie") ? MOVIEFILE : 0;
146                 params->filter |= RNA_boolean_get(op->ptr, "filter_text") ? TEXTFILE : 0;
147                 params->filter |= RNA_boolean_get(op->ptr, "filter_python") ? PYSCRIPTFILE : 0;
148                 params->filter |= RNA_boolean_get(op->ptr, "filter_font") ? FTFONTFILE : 0;
149                 params->filter |= RNA_boolean_get(op->ptr, "filter_sound") ? SOUNDFILE : 0;
150                 params->filter |= RNA_boolean_get(op->ptr, "filter_text") ? TEXTFILE : 0;
151                 params->filter |= RNA_boolean_get(op->ptr, "filter_folder") ? FOLDERFILE : 0;
152                 if (params->filter != 0)
153                         params->flag |= FILE_FILTER;
154
155                 if (params->type == FILE_LOADLIB) {
156                         params->flag |= FILE_HIDE_DOT;
157                         params->flag |= RNA_boolean_get(op->ptr, "link") ? FILE_LINK : 0;
158                         params->flag |= RNA_boolean_get(op->ptr, "autoselect") ? FILE_AUTOSELECT : 0;
159                         params->flag |= RNA_boolean_get(op->ptr, "active_layer") ? FILE_ACTIVELAY : 0;
160                 }
161
162                 if(params->filter & (IMAGEFILE|MOVIEFILE))
163                         params->display= FILE_IMGDISPLAY;
164                 else
165                         params->display= FILE_SHORTDISPLAY;
166                 
167         } else {
168                 /* default values, if no operator */
169                 params->flag |= FILE_HIDE_DOT;
170                 params->display = FILE_SHORTDISPLAY;
171                 params->filter = 0;
172                 params->sort = FILE_SORT_ALPHA;
173         }
174
175         /* new params, refresh file list */
176         if(sfile->files) filelist_free(sfile->files);
177
178         return 1;
179 }
180
181 void ED_fileselect_reset_params(SpaceFile *sfile)
182 {
183         sfile->params->flag = 0;
184         sfile->params->title[0] = '\0';
185 }
186
187 int ED_fileselect_layout_numfiles(FileLayout* layout, struct ARegion *ar)
188 {
189         int numfiles;
190
191         if (layout->flag & FILE_LAYOUT_HOR) {
192                 short width = ar->v2d.cur.xmax - ar->v2d.cur.xmin - 2*layout->tile_border_x;
193                 numfiles = width/layout->tile_w + 1;
194         } else {
195                 short height = ar->v2d.cur.ymax - ar->v2d.cur.ymin - 2*layout->tile_border_y;
196                 numfiles = height/layout->tile_h + 1;
197         }
198
199         return layout->columns*layout->rows;
200 }
201
202 int ED_fileselect_layout_offset(FileLayout* layout, int x, int y)
203 {
204         int offsetx, offsety;
205         int active_file;
206
207         if (layout == NULL)
208                 return 0;
209         
210         offsetx = (x)/(layout->tile_w + 2*layout->tile_border_x);
211         offsety = (y)/(layout->tile_h + 2*layout->tile_border_y);
212         
213         if (offsetx > layout->columns-1) return -1 ;
214         if (offsety > layout->rows-1) return -1 ;
215
216         if (layout->flag & FILE_LAYOUT_HOR) 
217                 active_file = layout->rows*offsetx + offsety;
218         else
219                 active_file = offsetx + layout->columns*offsety;
220         return active_file;
221 }
222
223 void ED_fileselect_layout_tilepos(FileLayout* layout, int tile, short *x, short *y)
224 {
225         if (layout->flag == FILE_LAYOUT_HOR) {
226                 *x = layout->tile_border_x + (tile/layout->rows)*(layout->tile_w+2*layout->tile_border_x);
227                 *y = layout->tile_border_y + (tile%layout->rows)*(layout->tile_h+2*layout->tile_border_y);
228         } else {
229                 *x = layout->tile_border_x + ((tile)%layout->columns)*(layout->tile_w+2*layout->tile_border_x);
230                 *y = layout->tile_border_y + ((tile)/layout->columns)*(layout->tile_h+2*layout->tile_border_y);
231         }
232 }
233
234 float file_string_width(const char* str)
235 {
236         uiStyle *style= U.uistyles.first;
237         uiStyleFontSet(&style->widget);
238         return BLF_width((char *)str);
239 }
240
241 float file_font_pointsize()
242 {
243         float s;
244         char tmp[2] = "X";
245         uiStyle *style= U.uistyles.first;
246         uiStyleFontSet(&style->widget);
247         s = BLF_height(tmp);
248         return style->widget.points;
249 }
250
251 static void column_widths(struct FileList* files, struct FileLayout* layout)
252 {
253         int i;
254         int numfiles = filelist_numfiles(files);
255
256         for (i=0; i<MAX_FILE_COLUMN; ++i) {
257                 layout->column_widths[i] = 0;
258         }
259
260         for (i=0; (i < numfiles); ++i)
261         {
262                 struct direntry* file = filelist_file(files, i);        
263                 if (file) {
264                         int len;
265                         len = file_string_width(file->relname);
266                         if (len > layout->column_widths[COLUMN_NAME]) layout->column_widths[COLUMN_NAME] = len;
267                         len = file_string_width(file->date);
268                         if (len > layout->column_widths[COLUMN_DATE]) layout->column_widths[COLUMN_DATE] = len;
269                         len = file_string_width(file->time);
270                         if (len > layout->column_widths[COLUMN_TIME]) layout->column_widths[COLUMN_TIME] = len;
271                         len = file_string_width(file->size);
272                         if (len > layout->column_widths[COLUMN_SIZE]) layout->column_widths[COLUMN_SIZE] = len;
273                         len = file_string_width(file->mode1);
274                         if (len > layout->column_widths[COLUMN_MODE1]) layout->column_widths[COLUMN_MODE1] = len;
275                         len = file_string_width(file->mode2);
276                         if (len > layout->column_widths[COLUMN_MODE2]) layout->column_widths[COLUMN_MODE2] = len;
277                         len = file_string_width(file->mode3);
278                         if (len > layout->column_widths[COLUMN_MODE3]) layout->column_widths[COLUMN_MODE3] = len;
279                         len = file_string_width(file->owner);
280                         if (len > layout->column_widths[COLUMN_OWNER]) layout->column_widths[COLUMN_OWNER] = len;
281                 }
282         }
283 }
284
285 void ED_fileselect_init_layout(struct SpaceFile *sfile, struct ARegion *ar)
286 {
287         FileSelectParams *params = ED_fileselect_get_params(sfile);
288         FileLayout *layout=0;
289         View2D *v2d= &ar->v2d;
290         int maxlen = 0;
291         int numfiles;
292         int textheight;
293         if (sfile->layout == 0) {
294                 sfile->layout = MEM_callocN(sizeof(struct FileLayout), "file_layout");
295                 sfile->layout->dirty = 1;
296         } 
297
298         if (!sfile->layout->dirty) return;
299
300         numfiles = filelist_numfiles(sfile->files);
301         textheight = file_font_pointsize();
302         layout = sfile->layout;
303         layout->textheight = textheight;
304
305         if (params->display == FILE_IMGDISPLAY) {
306                 layout->prv_w = 96;
307                 layout->prv_h = 96;
308                 layout->tile_border_x = 6;
309                 layout->tile_border_y = 6;
310                 layout->prv_border_x = 6;
311                 layout->prv_border_y = 6;
312                 layout->tile_w = layout->prv_w + 2*layout->prv_border_x;
313                 layout->tile_h = layout->prv_h + 2*layout->prv_border_y + textheight;
314                 layout->width= (v2d->cur.xmax - v2d->cur.xmin - 2*layout->tile_border_x);
315                 layout->columns= layout->width / (layout->tile_w + 2*layout->tile_border_x);
316                 if(layout->columns > 0)
317                         layout->rows= numfiles/layout->columns + 1; // XXX dirty, modulo is zero
318                 else {
319                         layout->columns = 1;
320                         layout->rows= numfiles + 1; // XXX dirty, modulo is zero
321                 }
322                 layout->height= sfile->layout->rows*(layout->tile_h+2*layout->tile_border_y) + layout->tile_border_y*2;
323                 layout->flag = FILE_LAYOUT_VER;
324         } else {
325                 layout->prv_w = 0;
326                 layout->prv_h = 0;
327                 layout->tile_border_x = 8;
328                 layout->tile_border_y = 2;
329                 layout->prv_border_x = 0;
330                 layout->prv_border_y = 0;
331                 layout->tile_h = textheight*3/2;
332                 layout->height= v2d->cur.ymax - v2d->cur.ymin - 2*layout->tile_border_y;
333                 layout->rows = layout->height / (layout->tile_h + 2*layout->tile_border_y);
334         
335                 column_widths(sfile->files, layout);
336
337                 if (params->display == FILE_SHORTDISPLAY) {
338                         maxlen = layout->column_widths[COLUMN_NAME] +
339                                          layout->column_widths[COLUMN_SIZE];
340                         maxlen += 20+2*10; // for icon and space between columns
341                 } else {
342                         maxlen = layout->column_widths[COLUMN_NAME] +
343                                          layout->column_widths[COLUMN_DATE] +
344                                          layout->column_widths[COLUMN_TIME] +
345                                          layout->column_widths[COLUMN_SIZE];
346                                         /* XXX add mode1, mode2, mode3, owner columns for non-windows platforms */
347                         maxlen += 20+4*10; // for icon and space between columns
348                 }
349                 layout->tile_w = maxlen + 40;
350                 if(layout->rows > 0)
351                         layout->columns = numfiles/layout->rows + 1; // XXX dirty, modulo is zero
352                 else {
353                         layout->rows = 1;
354                         layout->columns = numfiles + 1; // XXX dirty, modulo is zero
355                 }
356                 layout->width = sfile->layout->columns * (layout->tile_w + 2*layout->tile_border_x) + layout->tile_border_x*2;
357                 layout->flag = FILE_LAYOUT_HOR;
358         }
359         layout->dirty= 0;
360 }
361
362 FileLayout* ED_fileselect_get_layout(struct SpaceFile *sfile, struct ARegion *ar)
363 {
364         if (!sfile->layout) {
365                 ED_fileselect_init_layout(sfile, ar);
366         }
367         return sfile->layout;
368 }
369
370 void file_change_dir(struct SpaceFile *sfile)
371 {
372         if (sfile->params) { 
373                 filelist_setdir(sfile->files, sfile->params->dir);
374
375                 if(folderlist_clear_next(sfile))
376                         folderlist_free(sfile->folders_next);
377
378                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
379
380                 filelist_free(sfile->files);
381                 sfile->params->active_file = -1;
382         }
383 }
384
385 int file_select_match(struct SpaceFile *sfile, const char *pattern)
386 {
387         int match = 0;
388         if (strchr(pattern, '*') || strchr(pattern, '?') || strchr(pattern, '[')) {
389                 int i;
390                 struct direntry *file;
391                 int n = filelist_numfiles(sfile->files);
392
393                 for (i = 0; i < n; i++) {
394                         file = filelist_file(sfile->files, i);
395                         if (fnmatch(pattern, file->relname, 0) == 0) {
396                                 file->flags |= ACTIVE;
397                                 match = 1;
398                         }
399                 }
400         }
401         return match;
402 }
403
404
405 void autocomplete_directory(struct bContext *C, char *str, void *arg_v)
406 {
407         char tmp[FILE_MAX];
408         SpaceFile *sfile= CTX_wm_space_file(C);
409
410         /* search if str matches the beginning of name */
411         if(str[0] && sfile->files) {
412                 AutoComplete *autocpl= autocomplete_begin(str, FILE_MAX);
413                 int nentries = filelist_numfiles(sfile->files);
414                 int i;
415
416                 for(i= 0; i<nentries; ++i) {
417                         struct direntry* file = filelist_file(sfile->files, i);
418                         const char* dir = filelist_dir(sfile->files);
419                         if (file && S_ISDIR(file->type))        {
420                                 BLI_make_file_string(G.sce, tmp, dir, file->relname);
421                                 autocomplete_do_name(autocpl,tmp);
422                         }
423                 }
424                 autocomplete_end(autocpl, str);
425                 if (BLI_exists(str)) {
426                         BLI_add_slash(str);
427                 }
428         }
429 }