2.5 filebrowser
[blender-staging.git] / source / blender / editors / space_file / space_file.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
32 #include "DNA_object_types.h"
33 #include "DNA_space_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLO_readfile.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_arithb.h"
43 #include "BLI_rand.h"
44
45 #include "BKE_colortools.h"
46 #include "BKE_context.h"
47 #include "BKE_screen.h"
48
49 #include "ED_space_api.h"
50 #include "ED_screen.h"
51 #include "ED_fileselect.h"
52
53 #include "BIF_gl.h"
54
55 #include "WM_api.h"
56 #include "WM_types.h"
57
58 #include "UI_interface.h"
59 #include "UI_resources.h"
60 #include "UI_view2d.h"
61
62 #include "ED_markers.h"
63 #include "ED_fileselect.h"
64
65 #include "file_intern.h"        // own include
66 #include "fsmenu.h"
67 #include "filelist.h"
68
69 /* ******************** default callbacks for file space ***************** */
70
71 static SpaceLink *file_new(const bContext *C)
72 {
73         ARegion *ar;
74         SpaceFile *sfile;
75         
76         sfile= MEM_callocN(sizeof(SpaceFile), "initfile");
77         sfile->spacetype= SPACE_FILE;   
78         sfile->params= MEM_callocN(sizeof(FileSelectParams), "fileselparams");
79         sfile->files = filelist_new();
80         
81         ED_fileselect_set_params(C, FILE_UNIX, "", "/", 0, 0, 0);
82         filelist_setdir(sfile->files, sfile->params->dir);
83         filelist_settype(sfile->files, sfile->params->type);
84
85         /* header */
86         ar= MEM_callocN(sizeof(ARegion), "header for file");
87         BLI_addtail(&sfile->regionbase, ar);
88         ar->regiontype= RGN_TYPE_HEADER;
89         ar->alignment= RGN_ALIGN_BOTTOM;
90
91         /* channel list region */
92         ar= MEM_callocN(sizeof(ARegion), "channel area for file");
93         BLI_addtail(&sfile->regionbase, ar);
94         ar->regiontype= RGN_TYPE_CHANNELS;
95         ar->alignment= RGN_ALIGN_LEFT;
96
97         /* ui list region */
98         ar= MEM_callocN(sizeof(ARegion), "ui area for file");
99         BLI_addtail(&sfile->regionbase, ar);
100         ar->regiontype= RGN_TYPE_UI;
101         ar->alignment= RGN_ALIGN_TOP;
102
103         /* main area */
104         ar= MEM_callocN(sizeof(ARegion), "main area for file");
105         BLI_addtail(&sfile->regionbase, ar);
106         ar->regiontype= RGN_TYPE_WINDOW;
107         ar->v2d.scroll = (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
108         ar->v2d.align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
109         ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPZOOM|V2D_KEEPASPECT);
110         ar->v2d.keeptot= V2D_KEEPTOT_STRICT;
111         ar->v2d.minzoom= ar->v2d.maxzoom= 1.0f;
112
113         return (SpaceLink *)sfile;
114 }
115
116 /* not spacelink itself */
117 static void file_free(SpaceLink *sl)
118 {       
119         SpaceFile *sfile= (SpaceFile *) sl;
120         
121         if(sfile->files) {
122                 filelist_free(sfile->files);
123                 filelist_freelib(sfile->files);
124                 MEM_freeN(sfile->files);
125                 sfile->files = 0;
126         }
127
128         if (sfile->params) {
129                 if(sfile->params->pupmenu)
130                         MEM_freeN(sfile->params->pupmenu);
131                 MEM_freeN(sfile->params);
132                 sfile->params = 0;
133         }
134         
135         if (sfile->op) {
136                 WM_operator_free(sfile->op);
137         }
138 }
139
140
141 /* spacetype; init callback */
142 static void file_init(struct wmWindowManager *wm, ScrArea *sa)
143 {
144 }
145
146 static SpaceLink *file_duplicate(SpaceLink *sl)
147 {
148         SpaceFile *sfileo= (SpaceFile*)sl;
149         SpaceFile *sfilen= MEM_dupallocN(sl);
150         
151         /* clear or remove stuff from old */
152         sfilen->op = NULL; // XXX check if operator can be duplicated
153
154         sfilen->params= MEM_dupallocN(sfileo->params);
155         sfilen->params->pupmenu = NULL;
156         sfilen->files = filelist_new();
157         filelist_setdir(sfilen->files, sfilen->params->dir);
158         filelist_settype(sfilen->files, sfilen->params->type);
159
160         return (SpaceLink *)sfilen;
161 }
162
163
164 /* add handlers, stuff you only do once or on area/region changes */
165 static void file_main_area_init(wmWindowManager *wm, ARegion *ar)
166 {
167         ListBase *keymap;
168         
169         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
170         
171         /* own keymap */
172         keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);  /* XXX weak? */
173         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
174 }
175
176 static void file_main_area_draw(const bContext *C, ARegion *ar)
177 {
178         /* draw entirely, view changes should be handled here */
179         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
180         FileSelectParams* params = ED_fileselect_get_params(C);
181         View2D *v2d= &ar->v2d;
182         View2DScrollers *scrollers;
183         float col[3];
184         
185         if (!sfile->files) {
186                 sfile->files = filelist_new();
187                 filelist_setdir(sfile->files, params->dir);
188                 filelist_settype(sfile->files, params->type);
189         }
190
191         if (filelist_empty(sfile->files))
192         {
193                 unsigned int filter = 0;
194                 filelist_hidedot(sfile->files, params->flag & FILE_HIDE_DOT);
195                 if (params->flag & FILE_FILTER) {
196                         filter = params->filter ;
197                 } else {
198                         filter = 0;
199                 }
200
201                 filelist_setfilter(sfile->files, filter);
202                 filelist_readdir(sfile->files);
203                 
204                 if(params->sort!=FILE_SORTALPHA) filelist_sort(sfile->files, params->sort);             
205         }
206
207
208         /* clear and setup matrix */
209         UI_GetThemeColor3fv(TH_BACK, col);
210         glClearColor(col[0], col[1], col[2], 0.0);
211         glClear(GL_COLOR_BUFFER_BIT);
212         
213         file_calc_previews(C,ar);
214
215
216         /* data... */
217         UI_view2d_view_ortho(C, v2d);
218         
219         if (params->display) {
220                 file_draw_previews(C, ar);
221         } else {
222                 file_draw_list(C, ar);
223         }
224         
225         
226         /* reset view matrix */
227         UI_view2d_view_restore(C);
228         
229         /* scrollers */
230         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
231         UI_view2d_scrollers_draw(C, v2d, scrollers);
232         UI_view2d_scrollers_free(scrollers);
233
234 }
235
236 void file_operatortypes(void)
237 {
238         WM_operatortype_append(ED_FILE_OT_select);
239         WM_operatortype_append(ED_FILE_OT_select_bookmark);
240 }
241
242 void file_keymap(struct wmWindowManager *wm)
243 {
244         ListBase *keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);
245         WM_keymap_add_item(keymap, "ED_FILE_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
246
247         keymap= WM_keymap_listbase(wm, "FileBookmark", SPACE_FILE, 0);
248         WM_keymap_add_item(keymap, "ED_FILE_OT_select_bookmark", SELECTMOUSE, KM_PRESS, 0, 0);
249 }
250
251
252 static void file_channel_area_init(wmWindowManager *wm, ARegion *ar)
253 {
254         ListBase *keymap;
255
256         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
257
258         /* own keymap */
259         keymap= WM_keymap_listbase(wm, "FileBookmark", SPACE_FILE, 0);
260         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, NULL, NULL);
261 }
262
263 static void file_channel_area_draw(const bContext *C, ARegion *ar)
264 {
265         View2D *v2d= &ar->v2d;
266         float col[3];
267
268         UI_GetThemeColor3fv(TH_BACK, col);
269         glClearColor(col[0], col[1], col[2], 0.0);
270         glClear(GL_COLOR_BUFFER_BIT);
271
272         /* data... */
273         UI_view2d_view_ortho(C, v2d);
274
275         file_draw_fsmenu(C, ar);
276 }
277
278 /* add handlers, stuff you only do once or on area/region changes */
279 static void file_header_area_init(wmWindowManager *wm, ARegion *ar)
280 {
281         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
282 }
283
284 static void file_header_area_draw(const bContext *C, ARegion *ar)
285 {
286         float col[3];
287         
288         /* clear */
289         if(ED_screen_area_active(C))
290                 UI_GetThemeColor3fv(TH_HEADER, col);
291         else
292                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
293         
294         glClearColor(col[0], col[1], col[2], 0.0);
295         glClear(GL_COLOR_BUFFER_BIT);
296         
297         /* set view2d view matrix for scrolling (without scrollers) */
298         UI_view2d_view_ortho(C, &ar->v2d);
299         
300         file_header_buttons(C, ar);
301         
302         /* restore view matrix? */
303         UI_view2d_view_restore(C);
304 }
305
306 /* add handlers, stuff you only do once or on area/region changes */
307 static void file_ui_area_init(wmWindowManager *wm, ARegion *ar)
308 {
309         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
310 }
311
312 static void file_ui_area_draw(const bContext *C, ARegion *ar)
313 {
314         float col[3];
315         /* clear */
316         UI_GetThemeColor3fv(TH_BACK, col);
317         glClearColor(col[0], col[1], col[2], 0.0);
318         glClear(GL_COLOR_BUFFER_BIT);
319
320         /* set view2d view matrix for scrolling (without scrollers) */
321         UI_view2d_view_ortho(C, &ar->v2d);
322
323         file_draw_buttons(C, ar);
324
325         UI_view2d_view_restore(C);
326 }
327
328 //static void file_main_area_listener(ARegion *ar, wmNotifier *wmn)
329 //{
330         /* context changes */
331 //}
332
333 /* only called once, from space/spacetypes.c */
334 void ED_spacetype_file(void)
335 {
336         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype file");
337         ARegionType *art;
338         
339         st->spaceid= SPACE_FILE;
340         
341         st->new= file_new;
342         st->free= file_free;
343         st->init= file_init;
344         st->duplicate= file_duplicate;
345         st->operatortypes= file_operatortypes;
346         st->keymap= file_keymap;
347         
348         /* regions: main window */
349         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
350         art->regionid = RGN_TYPE_WINDOW;
351         art->init= file_main_area_init;
352         art->draw= file_main_area_draw;
353         // art->listener= file_main_area_listener;
354         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
355         BLI_addhead(&st->regiontypes, art);
356         
357         /* regions: header */
358         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
359         art->regionid = RGN_TYPE_HEADER;
360         art->minsizey= HEADERY;
361         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
362         art->init= file_header_area_init;
363         art->draw= file_header_area_draw;
364         BLI_addhead(&st->regiontypes, art);
365         
366         /* regions: ui */
367         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
368         art->regionid = RGN_TYPE_UI;
369         art->minsizey= 100;
370         art->keymapflag= ED_KEYMAP_UI;
371         art->init= file_ui_area_init;
372         art->draw= file_ui_area_draw;
373         BLI_addhead(&st->regiontypes, art);
374
375         /* regions: channels (directories) */
376         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
377         art->regionid = RGN_TYPE_CHANNELS;
378         art->minsizex= 200;
379         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
380         art->init= file_channel_area_init;
381         art->draw= file_channel_area_draw;
382         BLI_addhead(&st->regiontypes, art);
383         
384         BKE_spacetype_register(st);
385
386 }
387
388 void ED_file_init(void)
389 {
390         char name[FILE_MAX];
391         BLI_make_file_string("/", name, BLI_gethome(), ".Bfs");
392         fsmenu_read_file(name);
393         filelist_init_icons();
394 }
395
396 void ED_file_exit(void)
397 {
398         fsmenu_free();
399         filelist_free_icons();
400 }