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