NLA SoC: Merge from 2.5
[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
145 int ED_fileselect_layout_offset(FileLayout* layout, int x, int y)
146 {
147         int offsetx, offsety;
148         int active_file;
149
150         if (layout == NULL)
151                 return NULL;
152         
153         offsetx = (x)/(layout->tile_w + 2*layout->tile_border_x);
154         offsety = (y)/(layout->tile_h + 2*layout->tile_border_y);
155         
156         if (offsetx > layout->columns-1) offsetx = -1 ;
157         if (offsety > layout->rows-1) offsety = -1 ;
158
159         if (layout->flag & FILE_LAYOUT_HOR) 
160                 active_file = layout->rows*offsetx + offsety;
161         else
162                 active_file = offsetx + layout->columns*offsety;
163         return active_file;
164 }
165
166 void ED_fileselect_layout_tilepos(FileLayout* layout, int tile, short *x, short *y)
167 {
168         if (layout->flag == FILE_LAYOUT_HOR) {
169                 *x = layout->tile_border_x + (tile/layout->rows)*(layout->tile_w+2*layout->tile_border_x);
170                 *y = layout->tile_border_y + (tile%layout->rows)*(layout->tile_h+2*layout->tile_border_y);
171         } else {
172                 *x = layout->tile_border_x + ((tile)%layout->columns)*(layout->tile_w+2*layout->tile_border_x);
173                 *y = layout->tile_border_y + ((tile)/layout->columns)*(layout->tile_h+2*layout->tile_border_y);
174         }
175 }
176
177 float file_string_width(const char* str)
178 {
179         uiStyle *style= U.uistyles.first;
180         uiStyleFontSet(&style->widget);
181         return BLF_width((char *)str);
182 }
183
184 float file_font_pointsize()
185 {
186         float s;
187         char tmp[2] = "X";
188         uiStyle *style= U.uistyles.first;
189         uiStyleFontSet(&style->widget);
190         s = BLF_height(tmp);
191         return style->widget.points;
192 }
193
194 static void column_widths(struct FileList* files, struct FileLayout* layout)
195 {
196         int i;
197         int numfiles = filelist_numfiles(files);
198
199         for (i=0; i<MAX_FILE_COLUMN; ++i) {
200                 layout->column_widths[i] = 0;
201         }
202
203         for (i=0; (i < numfiles); ++i)
204         {
205                 struct direntry* file = filelist_file(files, i);        
206                 if (file) {
207                         int len;
208                         len = file_string_width(file->relname);
209                         if (len > layout->column_widths[COLUMN_NAME]) layout->column_widths[COLUMN_NAME] = len;
210                         len = file_string_width(file->date);
211                         if (len > layout->column_widths[COLUMN_DATE]) layout->column_widths[COLUMN_DATE] = len;
212                         len = file_string_width(file->time);
213                         if (len > layout->column_widths[COLUMN_TIME]) layout->column_widths[COLUMN_TIME] = len;
214                         len = file_string_width(file->size);
215                         if (len > layout->column_widths[COLUMN_SIZE]) layout->column_widths[COLUMN_SIZE] = len;
216                         len = file_string_width(file->mode1);
217                         if (len > layout->column_widths[COLUMN_MODE1]) layout->column_widths[COLUMN_MODE1] = len;
218                         len = file_string_width(file->mode2);
219                         if (len > layout->column_widths[COLUMN_MODE2]) layout->column_widths[COLUMN_MODE2] = len;
220                         len = file_string_width(file->mode3);
221                         if (len > layout->column_widths[COLUMN_MODE3]) layout->column_widths[COLUMN_MODE3] = len;
222                         len = file_string_width(file->owner);
223                         if (len > layout->column_widths[COLUMN_OWNER]) layout->column_widths[COLUMN_OWNER] = len;
224                 }
225         }
226 }
227
228 void ED_fileselect_init_layout(struct SpaceFile *sfile, struct ARegion *ar)
229 {
230         FileSelectParams* params = ED_fileselect_get_params(sfile);
231         View2D *v2d= &ar->v2d;
232         int maxlen = 0;
233         int numfiles = filelist_numfiles(sfile->files);
234         int textheight = file_font_pointsize();
235         if (sfile->layout == 0) {
236                 sfile->layout = MEM_callocN(sizeof(struct FileLayout), "file_layout");
237         }
238         if (params->display == FILE_IMGDISPLAY) {
239                 sfile->layout->prv_w = 96;
240                 sfile->layout->prv_h = 96;
241                 sfile->layout->tile_border_x = 6;
242                 sfile->layout->tile_border_y = 6;
243                 sfile->layout->prv_border_x = 6;
244                 sfile->layout->prv_border_y = 6;
245                 sfile->layout->tile_w = sfile->layout->prv_w + 2*sfile->layout->prv_border_x;
246                 sfile->layout->tile_h = sfile->layout->prv_h + 2*sfile->layout->prv_border_y + textheight;
247                 sfile->layout->width= (v2d->cur.xmax - v2d->cur.xmin - 2*sfile->layout->tile_border_x);
248                 sfile->layout->columns= sfile->layout->width / (sfile->layout->tile_w + 2*sfile->layout->tile_border_x);
249                 if(sfile->layout->columns > 0)
250                         sfile->layout->rows= numfiles/sfile->layout->columns + 1; // XXX dirty, modulo is zero
251                 else {
252                         sfile->layout->columns = 1;
253                         sfile->layout->rows= numfiles + 1; // XXX dirty, modulo is zero
254                 }
255                 sfile->layout->height= sfile->layout->rows*(sfile->layout->tile_h+2*sfile->layout->tile_border_y) + sfile->layout->tile_border_y*2;
256                 sfile->layout->flag = FILE_LAYOUT_VER;
257         } else {
258                 sfile->layout->prv_w = 0;
259                 sfile->layout->prv_h = 0;
260                 sfile->layout->tile_border_x = 8;
261                 sfile->layout->tile_border_y = 2;
262                 sfile->layout->prv_border_x = 0;
263                 sfile->layout->prv_border_y = 0;
264                 sfile->layout->tile_h = textheight*3/2;
265                 sfile->layout->height= v2d->cur.ymax - v2d->cur.ymin;
266                 sfile->layout->rows = sfile->layout->height / (sfile->layout->tile_h + 2*sfile->layout->tile_border_y);;
267         
268                 column_widths(sfile->files, sfile->layout);
269
270                 if (params->display == FILE_SHORTDISPLAY) {
271                         maxlen = sfile->layout->column_widths[COLUMN_NAME] +
272                                          sfile->layout->column_widths[COLUMN_SIZE];
273                         maxlen += 20+2*10; // for icon and space between columns
274                 } else {
275                         maxlen = sfile->layout->column_widths[COLUMN_NAME] +
276                                          sfile->layout->column_widths[COLUMN_DATE] +
277                                          sfile->layout->column_widths[COLUMN_TIME] +
278                                          sfile->layout->column_widths[COLUMN_SIZE];
279                                         /* XXX add mode1, mode2, mode3, owner columns for non-windows platforms */
280                         maxlen += 20+4*10; // for icon and space between columns
281                 }
282                 sfile->layout->tile_w = maxlen + 40;
283                 if(sfile->layout->rows > 0)
284                         sfile->layout->columns = numfiles/sfile->layout->rows + 1; // XXX dirty, modulo is zero
285                 else {
286                         sfile->layout->rows = 1;
287                         sfile->layout->columns = numfiles + 1; // XXX dirty, modulo is zero
288                 }
289                 sfile->layout->width = sfile->layout->columns * (sfile->layout->tile_w + 2*sfile->layout->tile_border_x) + sfile->layout->tile_border_x*2;
290                 sfile->layout->flag = FILE_LAYOUT_HOR;
291         } 
292 }
293
294 FileLayout* ED_fileselect_get_layout(struct SpaceFile *sfile, struct ARegion *ar)
295 {
296         if (!sfile->layout) {
297                 ED_fileselect_init_layout(sfile, ar);
298         }
299         return sfile->layout;
300 }
301
302 void file_change_dir(struct SpaceFile *sfile)
303 {
304         if (sfile->params && BLI_exists(sfile->params->dir)) {
305                 filelist_setdir(sfile->files, sfile->params->dir);
306
307                 if(folderlist_clear_next(sfile))
308                         folderlist_free(sfile->folders_next);
309
310                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
311
312                 filelist_free(sfile->files);
313                 sfile->params->active_file = -1;
314         }
315 }
316
317 int file_select_match(struct SpaceFile *sfile, const char *pattern)
318 {
319         int match = 0;
320         if (strchr(pattern, '*') || strchr(pattern, '?') || strchr(pattern, '[')) {
321                 int i;
322                 struct direntry *file;
323                 int n = filelist_numfiles(sfile->files);
324
325                 for (i = 0; i < n; i++) {
326                         file = filelist_file(sfile->files, i);
327                         if (fnmatch(pattern, file->relname, 0) == 0) {
328                                 file->flags |= ACTIVE;
329                                 match = 1;
330                         }
331                 }
332         }
333         return match;
334 }
335
336
337 void autocomplete_directory(struct bContext *C, char *str, void *arg_v)
338 {
339         char tmp[FILE_MAX];
340         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
341
342         /* search if str matches the beginning of name */
343         if(str[0] && sfile->files) {
344                 AutoComplete *autocpl= autocomplete_begin(str, FILE_MAX);
345                 int nentries = filelist_numfiles(sfile->files);
346                 int i;
347
348                 for(i= 0; i<nentries; ++i) {
349                         struct direntry* file = filelist_file(sfile->files, i);
350                         char* dir = filelist_dir(sfile->files);
351                         if (file && S_ISDIR(file->type))        {
352                                 BLI_make_file_string(G.sce, tmp, dir, file->relname);
353                                 autocomplete_do_name(autocpl,tmp);
354                         }
355                 }
356                 autocomplete_end(autocpl, str);
357         }
358 }