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
84 FileSelectParams* ED_fileselect_get_params(struct SpaceFile *sfile)
85 {
86         if (!sfile->params) {
87                 ED_fileselect_set_params(sfile, "", NULL, "/", 0, FILE_SHORTDISPLAY, 0, FILE_SORT_ALPHA);
88         }
89         return sfile->params;
90 }
91
92 short ED_fileselect_set_params(SpaceFile *sfile, const char *title, const char *last_dir, const char *path,
93                                                            short flag, short display, short filter, short sort)
94 {
95         char name[FILE_MAX], dir[FILE_MAX], file[FILE_MAX];
96         FileSelectParams *params;
97
98         if (!sfile->params) {
99                 sfile->params= MEM_callocN(sizeof(FileSelectParams), "fileselparams");
100         }
101
102         params = sfile->params;
103
104         params->flag = flag;
105         params->display = display;
106         params->filter = filter;
107         params->sort = sort;
108
109         BLI_strncpy(params->title, title, sizeof(params->title));
110
111         if(last_dir){
112                 BLI_strncpy(params->dir, last_dir, sizeof(params->dir));
113         }
114         else {
115                 BLI_strncpy(name, path, sizeof(name));
116                 BLI_convertstringcode(name, G.sce);
117
118                 BLI_split_dirfile(name, dir, file);
119                 BLI_strncpy(params->file, file, sizeof(params->file));
120                 BLI_strncpy(params->dir, dir, sizeof(params->dir));
121                 BLI_make_file_string(G.sce, params->dir, dir, ""); /* XXX needed ? - also solve G.sce */                        
122         }
123
124         return 1;
125 }
126
127 void ED_fileselect_reset_params(SpaceFile *sfile)
128 {
129         sfile->params->flag = 0;
130         sfile->params->title[0] = '\0';
131 }
132
133
134 int ED_fileselect_layout_offset(FileLayout* layout, int x, int y)
135 {
136         int offsetx, offsety;
137         int active_file;
138
139         if (layout == NULL)
140                 return NULL;
141         
142         offsetx = (x)/(layout->tile_w + 2*layout->tile_border_x);
143         offsety = (y)/(layout->tile_h + 2*layout->tile_border_y);
144         
145         if (offsetx > layout->columns-1) offsetx = -1 ;
146         if (offsety > layout->rows-1) offsety = -1 ;
147
148         if (layout->flag & FILE_LAYOUT_HOR) 
149                 active_file = layout->rows*offsetx + offsety;
150         else
151                 active_file = offsetx + layout->columns*offsety;
152         return active_file;
153 }
154
155 void ED_fileselect_layout_tilepos(FileLayout* layout, int tile, short *x, short *y)
156 {
157         if (layout->flag == FILE_LAYOUT_HOR) {
158                 *x = layout->tile_border_x + (tile/layout->rows)*(layout->tile_w+2*layout->tile_border_x);
159                 *y = layout->tile_border_y + (tile%layout->rows)*(layout->tile_h+2*layout->tile_border_y);
160         } else {
161                 *x = layout->tile_border_x + ((tile)%layout->columns)*(layout->tile_w+2*layout->tile_border_x);
162                 *y = layout->tile_border_y + ((tile)/layout->columns)*(layout->tile_h+2*layout->tile_border_y);
163         }
164 }
165
166 float file_string_width(const char* str)
167 {
168         uiStyle *style= U.uistyles.first;
169         uiStyleFontSet(&style->widget);
170         return BLF_width((char *)str);
171 }
172
173 float file_font_pointsize()
174 {
175         float s;
176         char tmp[2] = "X";
177         uiStyle *style= U.uistyles.first;
178         uiStyleFontSet(&style->widget);
179         s = BLF_height(tmp);
180         return style->widget.points;
181 }
182
183 static void column_widths(struct FileList* files, struct FileLayout* layout)
184 {
185         int i;
186         int numfiles = filelist_numfiles(files);
187
188         for (i=0; i<MAX_FILE_COLUMN; ++i) {
189                 layout->column_widths[i] = 0;
190         }
191
192         for (i=0; (i < numfiles); ++i)
193         {
194                 struct direntry* file = filelist_file(files, i);        
195                 if (file) {
196                         int len;
197                         len = file_string_width(file->relname);
198                         if (len > layout->column_widths[COLUMN_NAME]) layout->column_widths[COLUMN_NAME] = len;
199                         len = file_string_width(file->date);
200                         if (len > layout->column_widths[COLUMN_DATE]) layout->column_widths[COLUMN_DATE] = len;
201                         len = file_string_width(file->time);
202                         if (len > layout->column_widths[COLUMN_TIME]) layout->column_widths[COLUMN_TIME] = len;
203                         len = file_string_width(file->size);
204                         if (len > layout->column_widths[COLUMN_SIZE]) layout->column_widths[COLUMN_SIZE] = len;
205                         len = file_string_width(file->mode1);
206                         if (len > layout->column_widths[COLUMN_MODE1]) layout->column_widths[COLUMN_MODE1] = len;
207                         len = file_string_width(file->mode2);
208                         if (len > layout->column_widths[COLUMN_MODE2]) layout->column_widths[COLUMN_MODE2] = len;
209                         len = file_string_width(file->mode3);
210                         if (len > layout->column_widths[COLUMN_MODE3]) layout->column_widths[COLUMN_MODE3] = len;
211                         len = file_string_width(file->owner);
212                         if (len > layout->column_widths[COLUMN_OWNER]) layout->column_widths[COLUMN_OWNER] = len;
213                 }
214         }
215 }
216
217 void ED_fileselect_init_layout(struct SpaceFile *sfile, struct ARegion *ar)
218 {
219         FileSelectParams* params = ED_fileselect_get_params(sfile);
220         View2D *v2d= &ar->v2d;
221         int maxlen = 0;
222         int numfiles = filelist_numfiles(sfile->files);
223         int textheight = file_font_pointsize();
224         if (sfile->layout == 0) {
225                 sfile->layout = MEM_callocN(sizeof(struct FileLayout), "file_layout");
226         }
227         if (params->display == FILE_IMGDISPLAY) {
228                 sfile->layout->prv_w = 96;
229                 sfile->layout->prv_h = 96;
230                 sfile->layout->tile_border_x = 6;
231                 sfile->layout->tile_border_y = 6;
232                 sfile->layout->prv_border_x = 6;
233                 sfile->layout->prv_border_y = 6;
234                 sfile->layout->tile_w = sfile->layout->prv_w + 2*sfile->layout->prv_border_x;
235                 sfile->layout->tile_h = sfile->layout->prv_h + 2*sfile->layout->prv_border_y + textheight;
236                 sfile->layout->width= (v2d->cur.xmax - v2d->cur.xmin - 2*sfile->layout->tile_border_x);
237                 sfile->layout->columns= sfile->layout->width / (sfile->layout->tile_w + 2*sfile->layout->tile_border_x);
238                 if(sfile->layout->columns > 0)
239                         sfile->layout->rows= numfiles/sfile->layout->columns + 1; // XXX dirty, modulo is zero
240                 else {
241                         sfile->layout->columns = 1;
242                         sfile->layout->rows= numfiles + 1; // XXX dirty, modulo is zero
243                 }
244                 sfile->layout->height= sfile->layout->rows*(sfile->layout->tile_h+2*sfile->layout->tile_border_y) + sfile->layout->tile_border_y*2;
245                 sfile->layout->flag = FILE_LAYOUT_VER;
246         } else {
247                 sfile->layout->prv_w = 0;
248                 sfile->layout->prv_h = 0;
249                 sfile->layout->tile_border_x = 8;
250                 sfile->layout->tile_border_y = 2;
251                 sfile->layout->prv_border_x = 0;
252                 sfile->layout->prv_border_y = 0;
253                 sfile->layout->tile_h = textheight*3/2;
254                 sfile->layout->height= v2d->cur.ymax - v2d->cur.ymin;
255                 sfile->layout->rows = sfile->layout->height / (sfile->layout->tile_h + 2*sfile->layout->tile_border_y);;
256         
257                 column_widths(sfile->files, sfile->layout);
258
259                 if (params->display == FILE_SHORTDISPLAY) {
260                         maxlen = sfile->layout->column_widths[COLUMN_NAME] +
261                                          sfile->layout->column_widths[COLUMN_SIZE];
262                         maxlen += 20+2*10; // for icon and space between columns
263                 } else {
264                         maxlen = sfile->layout->column_widths[COLUMN_NAME] +
265                                          sfile->layout->column_widths[COLUMN_DATE] +
266                                          sfile->layout->column_widths[COLUMN_TIME] +
267                                          sfile->layout->column_widths[COLUMN_SIZE];
268                                         /* XXX add mode1, mode2, mode3, owner columns for non-windows platforms */
269                         maxlen += 20+4*10; // for icon and space between columns
270                 }
271                 sfile->layout->tile_w = maxlen + 40;
272                 if(sfile->layout->rows > 0)
273                         sfile->layout->columns = numfiles/sfile->layout->rows + 1; // XXX dirty, modulo is zero
274                 else {
275                         sfile->layout->rows = 1;
276                         sfile->layout->columns = numfiles + 1; // XXX dirty, modulo is zero
277                 }
278                 sfile->layout->width = sfile->layout->columns * (sfile->layout->tile_w + 2*sfile->layout->tile_border_x) + sfile->layout->tile_border_x*2;
279                 sfile->layout->flag = FILE_LAYOUT_HOR;
280         } 
281 }
282
283 FileLayout* ED_fileselect_get_layout(struct SpaceFile *sfile, struct ARegion *ar)
284 {
285         if (!sfile->layout) {
286                 ED_fileselect_init_layout(sfile, ar);
287         }
288         return sfile->layout;
289 }
290
291 void file_change_dir(struct SpaceFile *sfile)
292 {
293         if (sfile->params) {
294                 filelist_setdir(sfile->files, sfile->params->dir);
295
296                 if(folderlist_clear_next(sfile))
297                         folderlist_free(sfile->folders_next);
298
299                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
300
301                 filelist_free(sfile->files);
302                 sfile->params->active_file = -1;
303         }
304 }