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 "UI_interface.h"
77 #include "UI_resources.h"
78 #include "UI_view2d.h"
79
80 #include "file_intern.h"
81 #include "filelist.h"
82
83 #if defined __BeOS
84 static int fnmatch(const char *pattern, const char *string, int flags)
85 {
86         return 0;
87 }
88 #elif defined WIN32 && !defined _LIBC
89         /* use fnmatch included in blenlib */
90         #include "BLI_fnmatch.h"
91 #else
92         #include <fnmatch.h>
93 #endif
94
95 FileSelectParams* ED_fileselect_get_params(struct SpaceFile *sfile)
96 {
97         if (!sfile->params) {
98                 ED_fileselect_set_params(sfile, "", NULL, "/", 0, FILE_SHORTDISPLAY, 0, FILE_SORT_ALPHA);
99         }
100         return sfile->params;
101 }
102
103 short ED_fileselect_set_params(SpaceFile *sfile, const char *title, const char *last_dir, const char *path,
104                                                            short flag, short display, short filter, short sort)
105 {
106         char name[FILE_MAX], dir[FILE_MAX], file[FILE_MAX];
107         FileSelectParams *params;
108
109         if (!sfile->params) {
110                 sfile->params= MEM_callocN(sizeof(FileSelectParams), "fileselparams");
111         }
112
113         params = sfile->params;
114
115         params->flag = flag;
116         params->display = display;
117         params->filter = filter;
118         params->sort = sort;
119
120         BLI_strncpy(params->title, title, sizeof(params->title));
121
122         if(last_dir){
123                 BLI_strncpy(params->dir, last_dir, sizeof(params->dir));
124         }
125         else {
126                 BLI_strncpy(name, path, sizeof(name));
127                 BLI_convertstringcode(name, G.sce);
128
129                 BLI_split_dirfile(name, dir, file);
130                 BLI_strncpy(params->file, file, sizeof(params->file));
131                 BLI_strncpy(params->dir, dir, sizeof(params->dir));
132                 BLI_make_file_string(G.sce, params->dir, dir, ""); /* XXX needed ? - also solve G.sce */                        
133         }
134
135         return 1;
136 }
137
138 void ED_fileselect_reset_params(SpaceFile *sfile)
139 {
140         sfile->params->flag = 0;
141         sfile->params->title[0] = '\0';
142 }
143
144 int ED_fileselect_layout_numfiles(FileLayout* layout, struct ARegion *ar)
145 {
146         int numfiles;
147         short width, height;
148
149         if (layout->flag & FILE_LAYOUT_HOR) {
150                 short width = ar->v2d.cur.xmax - ar->v2d.cur.xmin - 2*layout->tile_border_x;
151                 numfiles = width/layout->tile_w + 1;
152         } else {
153                 short height = ar->v2d.cur.ymax - ar->v2d.cur.ymin - 2*layout->tile_border_y;
154                 numfiles = height/layout->tile_h + 1;
155         }
156
157         return layout->columns*layout->rows;
158 }
159
160 int ED_fileselect_layout_offset(FileLayout* layout, int x, int y)
161 {
162         int offsetx, offsety;
163         int active_file;
164
165         if (layout == NULL)
166                 return 0;
167         
168         offsetx = (x)/(layout->tile_w + 2*layout->tile_border_x);
169         offsety = (y)/(layout->tile_h + 2*layout->tile_border_y);
170         
171         if (offsetx > layout->columns-1) offsetx = -1 ;
172         if (offsety > layout->rows-1) offsety = -1 ;
173
174         if (layout->flag & FILE_LAYOUT_HOR) 
175                 active_file = layout->rows*offsetx + offsety;
176         else
177                 active_file = offsetx + layout->columns*offsety;
178         return active_file;
179 }
180
181 void ED_fileselect_layout_tilepos(FileLayout* layout, int tile, short *x, short *y)
182 {
183         if (layout->flag == FILE_LAYOUT_HOR) {
184                 *x = layout->tile_border_x + (tile/layout->rows)*(layout->tile_w+2*layout->tile_border_x);
185                 *y = layout->tile_border_y + (tile%layout->rows)*(layout->tile_h+2*layout->tile_border_y);
186         } else {
187                 *x = layout->tile_border_x + ((tile)%layout->columns)*(layout->tile_w+2*layout->tile_border_x);
188                 *y = layout->tile_border_y + ((tile)/layout->columns)*(layout->tile_h+2*layout->tile_border_y);
189         }
190 }
191
192 float file_string_width(const char* str)
193 {
194         uiStyle *style= U.uistyles.first;
195         uiStyleFontSet(&style->widget);
196         return BLF_width((char *)str);
197 }
198
199 float file_font_pointsize()
200 {
201         float s;
202         char tmp[2] = "X";
203         uiStyle *style= U.uistyles.first;
204         uiStyleFontSet(&style->widget);
205         s = BLF_height(tmp);
206         return style->widget.points;
207 }
208
209 static void column_widths(struct FileList* files, struct FileLayout* layout)
210 {
211         int i;
212         int numfiles = filelist_numfiles(files);
213
214         for (i=0; i<MAX_FILE_COLUMN; ++i) {
215                 layout->column_widths[i] = 0;
216         }
217
218         for (i=0; (i < numfiles); ++i)
219         {
220                 struct direntry* file = filelist_file(files, i);        
221                 if (file) {
222                         int len;
223                         len = file_string_width(file->relname);
224                         if (len > layout->column_widths[COLUMN_NAME]) layout->column_widths[COLUMN_NAME] = len;
225                         len = file_string_width(file->date);
226                         if (len > layout->column_widths[COLUMN_DATE]) layout->column_widths[COLUMN_DATE] = len;
227                         len = file_string_width(file->time);
228                         if (len > layout->column_widths[COLUMN_TIME]) layout->column_widths[COLUMN_TIME] = len;
229                         len = file_string_width(file->size);
230                         if (len > layout->column_widths[COLUMN_SIZE]) layout->column_widths[COLUMN_SIZE] = len;
231                         len = file_string_width(file->mode1);
232                         if (len > layout->column_widths[COLUMN_MODE1]) layout->column_widths[COLUMN_MODE1] = len;
233                         len = file_string_width(file->mode2);
234                         if (len > layout->column_widths[COLUMN_MODE2]) layout->column_widths[COLUMN_MODE2] = len;
235                         len = file_string_width(file->mode3);
236                         if (len > layout->column_widths[COLUMN_MODE3]) layout->column_widths[COLUMN_MODE3] = len;
237                         len = file_string_width(file->owner);
238                         if (len > layout->column_widths[COLUMN_OWNER]) layout->column_widths[COLUMN_OWNER] = len;
239                 }
240         }
241 }
242
243 void ED_fileselect_init_layout(struct SpaceFile *sfile, struct ARegion *ar)
244 {
245         FileSelectParams *params = ED_fileselect_get_params(sfile);
246         FileLayout *layout=0;
247         View2D *v2d= &ar->v2d;
248         int maxlen = 0;
249         int numfiles;
250         int textheight;
251         if (sfile->layout == 0) {
252                 sfile->layout = MEM_callocN(sizeof(struct FileLayout), "file_layout");
253                 sfile->layout->dirty = 1;
254         } 
255
256         if (!sfile->layout->dirty) return;
257
258         numfiles = filelist_numfiles(sfile->files);
259         textheight = file_font_pointsize();
260         layout = sfile->layout;
261
262         if (params->display == FILE_IMGDISPLAY) {
263                 layout->prv_w = 96;
264                 layout->prv_h = 96;
265                 layout->tile_border_x = 6;
266                 layout->tile_border_y = 6;
267                 layout->prv_border_x = 6;
268                 layout->prv_border_y = 6;
269                 layout->tile_w = layout->prv_w + 2*layout->prv_border_x;
270                 layout->tile_h = layout->prv_h + 2*layout->prv_border_y + textheight;
271                 layout->width= (v2d->cur.xmax - v2d->cur.xmin - 2*layout->tile_border_x);
272                 layout->columns= layout->width / (layout->tile_w + 2*layout->tile_border_x);
273                 if(layout->columns > 0)
274                         layout->rows= numfiles/layout->columns + 1; // XXX dirty, modulo is zero
275                 else {
276                         layout->columns = 1;
277                         layout->rows= numfiles + 1; // XXX dirty, modulo is zero
278                 }
279                 layout->height= sfile->layout->rows*(layout->tile_h+2*layout->tile_border_y) + layout->tile_border_y*2;
280                 layout->flag = FILE_LAYOUT_VER;
281         } else {
282                 layout->prv_w = 0;
283                 layout->prv_h = 0;
284                 layout->tile_border_x = 8;
285                 layout->tile_border_y = 2;
286                 layout->prv_border_x = 0;
287                 layout->prv_border_y = 0;
288                 layout->tile_h = textheight*3/2;
289                 layout->height= v2d->cur.ymax - v2d->cur.ymin - 2*layout->tile_border_y;
290                 layout->rows = layout->height / (layout->tile_h + 2*layout->tile_border_y);
291         
292                 column_widths(sfile->files, layout);
293
294                 if (params->display == FILE_SHORTDISPLAY) {
295                         maxlen = layout->column_widths[COLUMN_NAME] +
296                                          layout->column_widths[COLUMN_SIZE];
297                         maxlen += 20+2*10; // for icon and space between columns
298                 } else {
299                         maxlen = layout->column_widths[COLUMN_NAME] +
300                                          layout->column_widths[COLUMN_DATE] +
301                                          layout->column_widths[COLUMN_TIME] +
302                                          layout->column_widths[COLUMN_SIZE];
303                                         /* XXX add mode1, mode2, mode3, owner columns for non-windows platforms */
304                         maxlen += 20+4*10; // for icon and space between columns
305                 }
306                 layout->tile_w = maxlen + 40;
307                 if(layout->rows > 0)
308                         layout->columns = numfiles/layout->rows + 1; // XXX dirty, modulo is zero
309                 else {
310                         layout->rows = 1;
311                         layout->columns = numfiles + 1; // XXX dirty, modulo is zero
312                 }
313                 layout->width = sfile->layout->columns * (layout->tile_w + 2*layout->tile_border_x) + layout->tile_border_x*2;
314                 layout->flag = FILE_LAYOUT_HOR;
315         }
316         layout->dirty= 0;
317 }
318
319 FileLayout* ED_fileselect_get_layout(struct SpaceFile *sfile, struct ARegion *ar)
320 {
321         if (!sfile->layout) {
322                 ED_fileselect_init_layout(sfile, ar);
323         }
324         return sfile->layout;
325 }
326
327 void file_change_dir(struct SpaceFile *sfile)
328 {
329         if (sfile->params) { 
330                 if (BLI_exists(sfile->params->dir)) {
331                         filelist_setdir(sfile->files, sfile->params->dir);
332
333                         if(folderlist_clear_next(sfile))
334                                 folderlist_free(sfile->folders_next);
335
336                         folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
337
338                         filelist_free(sfile->files);
339                         sfile->params->active_file = -1;
340                 } else {
341                         BLI_strncpy(sfile->params->dir, filelist_dir(sfile->files), FILE_MAX);
342                 }
343         }
344 }
345
346 int file_select_match(struct SpaceFile *sfile, const char *pattern)
347 {
348         int match = 0;
349         if (strchr(pattern, '*') || strchr(pattern, '?') || strchr(pattern, '[')) {
350                 int i;
351                 struct direntry *file;
352                 int n = filelist_numfiles(sfile->files);
353
354                 for (i = 0; i < n; i++) {
355                         file = filelist_file(sfile->files, i);
356                         if (fnmatch(pattern, file->relname, 0) == 0) {
357                                 file->flags |= ACTIVE;
358                                 match = 1;
359                         }
360                 }
361         }
362         return match;
363 }
364
365
366 void autocomplete_directory(struct bContext *C, char *str, void *arg_v)
367 {
368         char tmp[FILE_MAX];
369         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
370
371         /* search if str matches the beginning of name */
372         if(str[0] && sfile->files) {
373                 AutoComplete *autocpl= autocomplete_begin(str, FILE_MAX);
374                 int nentries = filelist_numfiles(sfile->files);
375                 int i;
376
377                 for(i= 0; i<nentries; ++i) {
378                         struct direntry* file = filelist_file(sfile->files, i);
379                         const char* dir = filelist_dir(sfile->files);
380                         if (file && S_ISDIR(file->type))        {
381                                 BLI_make_file_string(G.sce, tmp, dir, file->relname);
382                                 autocomplete_do_name(autocpl,tmp);
383                         }
384                 }
385                 autocomplete_end(autocpl, str);
386                 if (BLI_exists(str)) {
387                         BLI_add_slash(str);
388                 }
389         }
390 }