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(sfile->params, 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         SpaceFile *sfile= sa->spacedata.first;  /* XXX get through context? */
145         if (!sfile->params) {
146                 sfile->params= MEM_callocN(sizeof(FileSelectParams), "fileselparams");
147                 ED_fileselect_set_params(sfile->params, FILE_UNIX, "", "/", 0, 0, 0);
148         }
149         if (!sfile->files) {
150                 sfile->files = filelist_new();
151         }
152         
153         filelist_setdir(sfile->files, sfile->params->dir);
154         filelist_settype(sfile->files, sfile->params->type);
155 }
156
157 static SpaceLink *file_duplicate(SpaceLink *sl)
158 {
159         SpaceFile *sfileo= (SpaceFile*)sl;
160         SpaceFile *sfilen= MEM_dupallocN(sl);
161         
162         /* clear or remove stuff from old */
163         sfilen->op = NULL; // XXX check if operator can be duplicated
164
165         sfilen->params= MEM_dupallocN(sfileo->params);
166         if (!sfilen->params) {
167                 sfilen->params= MEM_callocN(sizeof(FileSelectParams), "fileselparams");
168                 ED_fileselect_set_params(sfilen->params, FILE_UNIX, "", "/", 0, 0, 0);
169                 sfilen->params->pupmenu = NULL;
170         }
171         sfilen->files = filelist_new();
172         filelist_setdir(sfilen->files, sfilen->params->dir);
173         filelist_settype(sfilen->files, sfilen->params->type);
174
175         return (SpaceLink *)sfilen;
176 }
177
178
179 /* add handlers, stuff you only do once or on area/region changes */
180 static void file_main_area_init(wmWindowManager *wm, ARegion *ar)
181 {
182         ListBase *keymap;
183         
184         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
185         
186         /* own keymap */
187         keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);  /* XXX weak? */
188         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
189 }
190
191 static void file_main_area_draw(const bContext *C, ARegion *ar)
192 {
193         /* draw entirely, view changes should be handled here */
194         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
195         FileSelectParams* params = sfile->params;
196         View2D *v2d= &ar->v2d;
197         View2DScrollers *scrollers;
198         float col[3];
199         
200         if (!sfile->files) {
201                 sfile->files = filelist_new();
202                 filelist_setdir(sfile->files, params->dir);
203                 filelist_settype(sfile->files, params->type);
204         }
205
206         if (filelist_empty(sfile->files))
207         {
208                 unsigned int filter = 0;
209                 filelist_hidedot(sfile->files, params->flag & FILE_HIDE_DOT);
210                 if (params->flag & FILE_FILTER) {
211                         filter = params->filter ;
212                 } else {
213                         filter = 0;
214                 }
215
216                 filelist_setfilter(sfile->files, filter);
217                 filelist_readdir(sfile->files);
218                 
219                 if(params->sort!=FILE_SORTALPHA) filelist_sort(sfile->files, params->sort);             
220         }
221
222
223         /* clear and setup matrix */
224         UI_GetThemeColor3fv(TH_BACK, col);
225         glClearColor(col[0], col[1], col[2], 0.0);
226         glClear(GL_COLOR_BUFFER_BIT);
227         
228         file_calc_previews(C,ar);
229
230
231         /* data... */
232         UI_view2d_view_ortho(C, v2d);
233         
234         if (params->display) {
235                 file_draw_previews(C, ar);
236         } else {
237                 file_draw_list(C, ar);
238         }
239         
240         
241         /* reset view matrix */
242         UI_view2d_view_restore(C);
243         
244         /* scrollers */
245         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
246         UI_view2d_scrollers_draw(C, v2d, scrollers);
247         UI_view2d_scrollers_free(scrollers);
248
249 }
250
251 void file_operatortypes(void)
252 {
253         WM_operatortype_append(ED_FILE_OT_select);
254         WM_operatortype_append(ED_FILE_OT_select_bookmark);
255 }
256
257 void file_keymap(struct wmWindowManager *wm)
258 {
259         ListBase *keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);
260         WM_keymap_add_item(keymap, "ED_FILE_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
261
262         keymap= WM_keymap_listbase(wm, "FileBookmark", SPACE_FILE, 0);
263         WM_keymap_add_item(keymap, "ED_FILE_OT_select_bookmark", SELECTMOUSE, KM_PRESS, 0, 0);
264 }
265
266
267 static void file_channel_area_init(wmWindowManager *wm, ARegion *ar)
268 {
269         ListBase *keymap;
270
271         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
272
273         /* own keymap */
274         keymap= WM_keymap_listbase(wm, "FileBookmark", SPACE_FILE, 0);
275         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, NULL, NULL);
276 }
277
278 static void file_channel_area_draw(const bContext *C, ARegion *ar)
279 {
280         View2D *v2d= &ar->v2d;
281         float col[3];
282
283         UI_GetThemeColor3fv(TH_BACK, col);
284         glClearColor(col[0], col[1], col[2], 0.0);
285         glClear(GL_COLOR_BUFFER_BIT);
286
287         /* data... */
288         UI_view2d_view_ortho(C, v2d);
289
290         file_draw_fsmenu(C, ar);
291 }
292
293 /* add handlers, stuff you only do once or on area/region changes */
294 static void file_header_area_init(wmWindowManager *wm, ARegion *ar)
295 {
296         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
297 }
298
299 static void file_header_area_draw(const bContext *C, ARegion *ar)
300 {
301         float col[3];
302         
303         /* clear */
304         if(ED_screen_area_active(C))
305                 UI_GetThemeColor3fv(TH_HEADER, col);
306         else
307                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
308         
309         glClearColor(col[0], col[1], col[2], 0.0);
310         glClear(GL_COLOR_BUFFER_BIT);
311         
312         /* set view2d view matrix for scrolling (without scrollers) */
313         UI_view2d_view_ortho(C, &ar->v2d);
314         
315         file_header_buttons(C, ar);
316         
317         /* restore view matrix? */
318         UI_view2d_view_restore(C);
319 }
320
321 /* add handlers, stuff you only do once or on area/region changes */
322 static void file_ui_area_init(wmWindowManager *wm, ARegion *ar)
323 {
324         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
325 }
326
327 static void file_ui_area_draw(const bContext *C, ARegion *ar)
328 {
329         float col[3];
330         /* clear */
331         UI_GetThemeColor3fv(TH_BACK, col);
332         glClearColor(col[0], col[1], col[2], 0.0);
333         glClear(GL_COLOR_BUFFER_BIT);
334
335         /* set view2d view matrix for scrolling (without scrollers) */
336         UI_view2d_view_ortho(C, &ar->v2d);
337
338         file_draw_buttons(C, ar);
339
340         UI_view2d_view_restore(C);
341 }
342
343 //static void file_main_area_listener(ARegion *ar, wmNotifier *wmn)
344 //{
345         /* context changes */
346 //}
347
348 /* only called once, from space/spacetypes.c */
349 void ED_spacetype_file(void)
350 {
351         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype file");
352         ARegionType *art;
353         
354         st->spaceid= SPACE_FILE;
355         
356         st->new= file_new;
357         st->free= file_free;
358         st->init= file_init;
359         st->duplicate= file_duplicate;
360         st->operatortypes= file_operatortypes;
361         st->keymap= file_keymap;
362         
363         /* regions: main window */
364         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
365         art->regionid = RGN_TYPE_WINDOW;
366         art->init= file_main_area_init;
367         art->draw= file_main_area_draw;
368         // art->listener= file_main_area_listener;
369         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
370         BLI_addhead(&st->regiontypes, art);
371         
372         /* regions: header */
373         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
374         art->regionid = RGN_TYPE_HEADER;
375         art->minsizey= HEADERY;
376         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
377         art->init= file_header_area_init;
378         art->draw= file_header_area_draw;
379         BLI_addhead(&st->regiontypes, art);
380         
381         /* regions: ui */
382         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
383         art->regionid = RGN_TYPE_UI;
384         art->minsizey= 100;
385         art->keymapflag= ED_KEYMAP_UI;
386         art->init= file_ui_area_init;
387         art->draw= file_ui_area_draw;
388         BLI_addhead(&st->regiontypes, art);
389
390         /* regions: channels (directories) */
391         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
392         art->regionid = RGN_TYPE_CHANNELS;
393         art->minsizex= 200;
394         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
395         art->init= file_channel_area_init;
396         art->draw= file_channel_area_draw;
397         BLI_addhead(&st->regiontypes, art);
398         
399         BKE_spacetype_register(st);
400
401 }
402
403 void ED_file_init(void)
404 {
405         char name[FILE_MAX];
406         BLI_make_file_string("/", name, BLI_gethome(), ".Bfs");
407         fsmenu_read_file(name);
408         filelist_init_icons();
409 }
410
411 void ED_file_exit(void)
412 {
413         fsmenu_free();
414         filelist_free_icons();
415 }