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