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