2.50: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r19323...
[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_windowmanager_types.h"
49
50 #include "MEM_guardedalloc.h"
51
52 #include "BLI_blenlib.h"
53 #include "BLI_linklist.h"
54 #include "BLI_storage_types.h"
55 #include "BLI_dynstr.h"
56
57 #include "BKE_context.h"
58 #include "BKE_screen.h"
59 #include "BKE_global.h"
60
61 #include "DNA_userdef_types.h"
62
63 #include "ED_screen.h"
64 #include "ED_util.h"
65 #include "ED_fileselect.h"
66
67 #include "WM_api.h"
68 #include "WM_types.h"
69
70 #include "BIF_gl.h"
71 #include "BIF_glutil.h"
72
73 #include "UI_interface.h"
74 #include "UI_resources.h"
75 #include "UI_view2d.h"
76
77 #include "file_intern.h"
78 #include "filelist.h"
79
80
81 FileSelectParams* ED_fileselect_get_params(struct SpaceFile *sfile)
82 {
83         if (!sfile->params) {
84                 ED_fileselect_set_params(sfile, FILE_UNIX, "", "/", 0, FILE_SHORTDISPLAY, 0);
85         }
86         return sfile->params;
87 }
88
89 short ED_fileselect_set_params(SpaceFile *sfile, int type, const char *title, const char *path,
90                                                            short flag, short display, short filter)
91 {
92         char name[FILE_MAX], dir[FILE_MAX], file[FILE_MAX];
93         FileSelectParams *params;
94
95         if (!sfile->params) {
96                 sfile->params= MEM_callocN(sizeof(FileSelectParams), "fileselparams");
97         }
98
99         params = sfile->params;
100
101         params->type = type;
102         params->flag = flag;
103         params->display = display;
104         params->filter = filter;
105
106         BLI_strncpy(params->title, title, sizeof(params->title));
107         
108         BLI_strncpy(name, path, sizeof(name));
109         BLI_convertstringcode(name, G.sce);
110         
111         switch(type) {
112                 case FILE_MAIN:
113                         break;
114                 case FILE_LOADLIB:
115                         break;
116                 case FILE_BLENDER:
117                 case FILE_LOADFONT:
118                 default:
119                         {
120                                 BLI_split_dirfile(name, dir, file);
121                                 BLI_strncpy(params->file, file, sizeof(params->file));
122                                 BLI_strncpy(params->dir, dir, sizeof(params->dir));
123                                 BLI_make_file_string(G.sce, params->dir, dir, ""); /* XXX needed ? - also solve G.sce */                        
124                         }
125                         break;
126         }
127
128         return 1;
129 }
130
131 void ED_fileselect_reset_params(SpaceFile *sfile)
132 {
133         sfile->params->type = FILE_UNIX;
134         sfile->params->flag = 0;
135         sfile->params->title[0] = '\0';
136 }
137
138
139 int ED_fileselect_layout_offset(FileLayout* layout, int x, int y)
140 {
141         int offsetx, offsety;
142         int active_file;
143
144         offsetx = (x)/(layout->tile_w + 2*layout->tile_border_x);
145         offsety = (y)/(layout->tile_h + 2*layout->tile_border_y);
146         
147         if (offsetx > layout->columns-1) offsetx = layout->columns-1 ;
148         if (offsety > layout->rows-1) offsety = layout->rows-1 ;
149
150         if (layout->flag & FILE_LAYOUT_HOR) 
151                 active_file = layout->rows*offsetx + offsety;
152         else
153                 active_file = offsetx + layout->columns*offsety;
154         printf("OFFSET %d %d %d %d %d\n", x,y, offsetx, offsety, active_file);
155         return active_file;
156 }
157
158 void ED_fileselect_layout_tilepos(FileLayout* layout, int tile, short *x, short *y)
159 {
160         if (layout->flag == FILE_LAYOUT_HOR) {
161                 *x = layout->tile_border_x + (tile/layout->rows)*(layout->tile_w+2*layout->tile_border_x);
162                 *y = layout->tile_border_y + (tile%layout->rows)*(layout->tile_h+2*layout->tile_border_y);
163         } else {
164                 *x = layout->tile_border_x + ((tile)%layout->columns)*(layout->tile_w+2*layout->tile_border_x);
165                 *y = layout->tile_border_y + ((tile)/layout->columns)*(layout->tile_h+2*layout->tile_border_y);
166         }
167 }
168
169
170 void ED_fileselect_init_layout(struct SpaceFile *sfile, struct ARegion *ar)
171 {
172         FileSelectParams* params = ED_fileselect_get_params(sfile);
173         View2D *v2d= &ar->v2d;
174         int maxlen = 0;
175         int numfiles = filelist_numfiles(sfile->files);
176
177         if (sfile->layout == 0) {
178                 sfile->layout = MEM_callocN(sizeof(struct FileLayout), "file_layout");
179         }
180
181         if (params->display == FILE_IMGDISPLAY) {
182                 sfile->layout->prv_w = 96;
183                 sfile->layout->prv_h = 96;
184                 sfile->layout->tile_border_x = 6;
185                 sfile->layout->tile_border_y = 6;
186                 sfile->layout->prv_border_x = 6;
187                 sfile->layout->prv_border_y = 6;
188                 sfile->layout->tile_w = sfile->layout->prv_w + 2*sfile->layout->prv_border_x;
189                 sfile->layout->tile_h = sfile->layout->prv_h + 2*sfile->layout->prv_border_y + 12; // XXX 12 = font h
190                 sfile->layout->width= (v2d->cur.xmax - v2d->cur.xmin - 2*sfile->layout->tile_border_x);
191                 sfile->layout->columns= sfile->layout->width / (sfile->layout->tile_w + 2*sfile->layout->tile_border_x);
192                 if(sfile->layout->columns > 0)
193                         sfile->layout->rows= numfiles/sfile->layout->columns + 1; // XXX dirty, modulo is zero
194                 else {
195                         sfile->layout->columns = 1;
196                         sfile->layout->rows= numfiles + 1; // XXX dirty, modulo is zero
197                 }
198                 sfile->layout->height= sfile->layout->rows*(sfile->layout->tile_h+2*sfile->layout->tile_border_y) + sfile->layout->tile_border_y*2;
199                 sfile->layout->flag = FILE_LAYOUT_VER;
200         } else {
201                 sfile->layout->prv_w = 0;
202                 sfile->layout->prv_h = 0;
203                 sfile->layout->tile_border_x = 8;
204                 sfile->layout->tile_border_y = 2;
205                 sfile->layout->prv_border_x = 0;
206                 sfile->layout->prv_border_y = 0;
207                 sfile->layout->tile_h = 12*3/2; // XXX 12 = font h
208                 sfile->layout->height= v2d->cur.ymax - v2d->cur.ymin;
209                 sfile->layout->rows = sfile->layout->height / (sfile->layout->tile_h + 2*sfile->layout->tile_border_y);;
210         
211                 if (params->display == FILE_SHORTDISPLAY) {
212                         maxlen = filelist_column_len(sfile->files, COLUMN_NAME) +
213                                          filelist_column_len(sfile->files, COLUMN_SIZE);
214                         maxlen += 20+2*10; // for icon and space between columns
215                 } else {
216                         maxlen = filelist_column_len(sfile->files, COLUMN_NAME) +
217                              filelist_column_len(sfile->files, COLUMN_DATE) + 
218                              filelist_column_len(sfile->files, COLUMN_TIME) +
219                                  filelist_column_len(sfile->files, COLUMN_SIZE) /* +
220                                  filelist_column_len(sfile->files, COLUMN_MODE1) +
221                                  filelist_column_len(sfile->files, COLUMN_MODE2) +
222                                  filelist_column_len(sfile->files, COLUMN_MODE3) +
223                                  filelist_column_len(sfile->files, COLUMN_OWNER) */ ;
224                         maxlen += 20+4*10; // for icon and space between columns
225                 }
226                 sfile->layout->tile_w = maxlen + 40;
227                 if(sfile->layout->rows > 0)
228                         sfile->layout->columns = numfiles/sfile->layout->rows + 1; // XXX dirty, modulo is zero
229                 else {
230                         sfile->layout->rows = 1;
231                         sfile->layout->columns = numfiles + 1; // XXX dirty, modulo is zero
232                 }
233                 sfile->layout->width = sfile->layout->columns * (sfile->layout->tile_w + 2*sfile->layout->tile_border_x) + sfile->layout->tile_border_x*2;
234                 sfile->layout->flag = FILE_LAYOUT_HOR;
235         } 
236 }
237
238 FileLayout* ED_fileselect_get_layout(struct SpaceFile *sfile, struct ARegion *ar)
239 {
240         if (!sfile->layout) {
241                 ED_fileselect_init_layout(sfile, ar);
242         }
243         return sfile->layout;
244 }