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