2.5 filebrowser
[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 "ED_screen.h"
62 #include "ED_util.h"
63 #include "ED_fileselect.h"
64
65 #include "WM_api.h"
66 #include "WM_types.h"
67
68 #include "BIF_gl.h"
69 #include "BIF_glutil.h"
70
71 #include "UI_interface.h"
72 #include "UI_resources.h"
73 #include "UI_view2d.h"
74
75 #include "file_intern.h"
76 #include "filelist.h"
77
78
79 FileSelectParams* ED_fileselect_get_params(const struct bContext *C)
80 {
81         SpaceLink *sl= CTX_wm_space_data(C);
82         SpaceFile *sfile;
83         FileSelectParams *params = 0;
84
85         if (sl->spacetype == SPACE_FILE) {
86                 sfile= (SpaceFile*)sl;
87                 params = sfile->params;
88         }
89         return params;
90 }
91
92 short ED_fileselect_set_params(const struct bContext *C, 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         SpaceLink *sl= CTX_wm_space_data(C);
97         SpaceFile *sfile;
98         FileSelectParams *params;
99
100         if (sl->spacetype != SPACE_FILE) {
101                 return 0;
102         }
103         sfile= (SpaceFile*)sl;
104
105         if (!sfile->params) {
106                 sfile->params = MEM_callocN(sizeof(FileSelectParams), "fileselparams");
107         }
108         params = sfile->params;
109
110         params->type = type;
111         params->flag = flag;
112         params->display = display;
113         params->filter = filter;
114
115         BLI_strncpy(params->title, title, sizeof(params->title));
116         
117         BLI_strncpy(name, path, sizeof(name));
118         BLI_convertstringcode(name, G.sce);
119         
120         switch(type) {
121                 case FILE_MAIN:
122                         break;
123                 case FILE_LOADLIB:
124                         break;
125                 case FILE_BLENDER:
126                 case FILE_LOADFONT:
127                 default:
128                         {
129                                 BLI_split_dirfile(name, dir, file);
130                                 BLI_strncpy(params->file, file, sizeof(params->file));
131                                 BLI_strncpy(params->dir, dir, sizeof(params->dir));
132                                 BLI_make_file_string(G.sce, params->dir, dir, ""); /* XXX needed ? - also solve G.sce */
133                                 
134                                 filelist_settype(sfile->files, type);
135                                 filelist_setdir(sfile->files, params->dir);
136                                 BLI_cleanup_dir(G.sce, params->dir); /* XXX solve G.sce */
137
138                                 /* free: filelist and libfiledata became incorrect */
139                                 if (sfile->files) {
140                                         filelist_free(sfile->files);
141                                         filelist_freelib(sfile->files);
142                                 }                               
143                         }
144                         break;
145         }
146
147         return 1;
148 }
149