2.5
[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
79         /* header */
80         ar= MEM_callocN(sizeof(ARegion), "header for file");
81         BLI_addtail(&sfile->regionbase, ar);
82         ar->regiontype= RGN_TYPE_HEADER;
83         ar->alignment= RGN_ALIGN_BOTTOM;
84
85         /* channel list region */
86         ar= MEM_callocN(sizeof(ARegion), "channel area for file");
87         BLI_addtail(&sfile->regionbase, ar);
88         ar->regiontype= RGN_TYPE_CHANNELS;
89         ar->alignment= RGN_ALIGN_LEFT;
90
91         /* ui list region */
92         ar= MEM_callocN(sizeof(ARegion), "ui area for file");
93         BLI_addtail(&sfile->regionbase, ar);
94         ar->regiontype= RGN_TYPE_UI;
95         ar->alignment= RGN_ALIGN_TOP;
96
97         /* main area */
98         ar= MEM_callocN(sizeof(ARegion), "main area for file");
99         BLI_addtail(&sfile->regionbase, ar);
100         ar->regiontype= RGN_TYPE_WINDOW;
101         ar->v2d.scroll = (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
102         ar->v2d.align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
103         ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPZOOM|V2D_KEEPASPECT);
104         ar->v2d.keeptot= V2D_KEEPTOT_STRICT;
105         ar->v2d.minzoom= ar->v2d.maxzoom= 1.0f;
106
107         return (SpaceLink *)sfile;
108 }
109
110 /* not spacelink itself */
111 static void file_free(SpaceLink *sl)
112 {       
113         SpaceFile *sfile= (SpaceFile *) sl;
114         
115         if(sfile->files) {
116                 filelist_free(sfile->files);
117                 filelist_freelib(sfile->files);
118                 MEM_freeN(sfile->files);
119                 sfile->files= NULL;
120         }
121
122         if (sfile->params) {
123                 if(sfile->params->pupmenu)
124                         MEM_freeN(sfile->params->pupmenu);
125                 MEM_freeN(sfile->params);
126                 sfile->params= NULL;
127         }
128         
129         if (sfile->op) {
130                 WM_operator_free(sfile->op);
131         }
132 }
133
134
135 /* spacetype; init callback */
136 static void file_init(struct wmWindowManager *wm, ScrArea *sa)
137 {
138         SpaceFile *sfile= sa->spacedata.first;  /* XXX get through context? */
139         if (sfile->params) {
140                 ED_fileselect_reset_params(sfile);
141         }
142         if (sfile->files) {
143                 filelist_free(sfile->files);
144                 filelist_freelib(sfile->files);
145                 MEM_freeN(sfile->files);
146                 sfile->files= NULL;
147         }
148 }
149
150 static SpaceLink *file_duplicate(SpaceLink *sl)
151 {
152         SpaceFile *sfileo= (SpaceFile*)sl;
153         SpaceFile *sfilen= MEM_dupallocN(sl);
154         
155         /* clear or remove stuff from old */
156         sfilen->op = NULL; // XXX check if operator can be duplicated
157
158         sfilen->params= MEM_dupallocN(sfileo->params);
159         if (!sfilen->params) {
160                 sfilen->params= MEM_callocN(sizeof(FileSelectParams), "fileselparams");
161                 ED_fileselect_set_params(sfilen, FILE_UNIX, "", "/", 0, 0, 0);
162                 sfilen->params->pupmenu = NULL;
163         }
164         
165         sfilen->files = filelist_new();
166         filelist_setdir(sfilen->files, sfilen->params->dir);
167         filelist_settype(sfilen->files, sfilen->params->type);
168
169         return (SpaceLink *)sfilen;
170 }
171
172
173 /* add handlers, stuff you only do once or on area/region changes */
174 static void file_main_area_init(wmWindowManager *wm, ARegion *ar)
175 {
176         ListBase *keymap;
177         
178         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
179         
180         /* own keymap */
181         keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);  /* XXX weak? */
182         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
183 }
184
185 static void file_main_area_draw(const bContext *C, ARegion *ar)
186 {
187         /* draw entirely, view changes should be handled here */
188         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
189         FileSelectParams* params = sfile->params;
190         View2D *v2d= &ar->v2d;
191         View2DScrollers *scrollers;
192         float col[3];
193         
194         if (!sfile->files) {
195                 sfile->files = filelist_new();
196                 filelist_setdir(sfile->files, params->dir);
197                 filelist_settype(sfile->files, params->type);
198                 params->active_file = -1; // added this so it opens nicer (ton)
199         }
200
201         if (filelist_empty(sfile->files))
202         {
203                 unsigned int filter = 0;
204                 filelist_hidedot(sfile->files, params->flag & FILE_HIDE_DOT);
205                 if (params->flag & FILE_FILTER) {
206                         filter = params->filter ;
207                 } else {
208                         filter = 0;
209                 }
210
211                 filelist_setfilter(sfile->files, filter);
212                 filelist_readdir(sfile->files);
213                 
214                 if(params->sort!=FILE_SORTALPHA) filelist_sort(sfile->files, params->sort);             
215         }
216
217         /* clear and setup matrix */
218         UI_GetThemeColor3fv(TH_BACK, col);
219         glClearColor(col[0], col[1], col[2], 0.0);
220         glClear(GL_COLOR_BUFFER_BIT);
221         
222         /* sets tile/border settings in sfile */
223         file_calc_previews(C, ar);
224
225         /* on first read, find active file */
226         if (params->active_file == -1) {
227                 wmEvent *event= CTX_wm_window(C)->eventstate;
228                 file_hilight_set(sfile, ar, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin);
229                 
230         }
231         
232         /* data... */
233         UI_view2d_view_ortho(C, v2d);
234         
235         if (params->display) {
236                 file_draw_previews(C, ar);
237         } else {
238                 file_draw_list(C, ar);
239         }
240         
241         
242         /* reset view matrix */
243         UI_view2d_view_restore(C);
244         
245         /* scrollers */
246         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
247         UI_view2d_scrollers_draw(C, v2d, scrollers);
248         UI_view2d_scrollers_free(scrollers);
249
250 }
251
252 void file_operatortypes(void)
253 {
254         WM_operatortype_append(ED_FILE_OT_select);
255         WM_operatortype_append(ED_FILE_OT_select_all);
256         WM_operatortype_append(ED_FILE_OT_border_select);
257         WM_operatortype_append(ED_FILE_OT_select_bookmark);
258         WM_operatortype_append(ED_FILE_OT_loadimages);
259         WM_operatortype_append(ED_FILE_OT_highlight);
260         WM_operatortype_append(ED_FILE_OT_load);
261         WM_operatortype_append(ED_FILE_OT_cancel);
262 }
263
264 void file_keymap(struct wmWindowManager *wm)
265 {
266         ListBase *keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);
267         WM_keymap_add_item(keymap, "ED_FILE_OT_select", LEFTMOUSE, KM_PRESS, 0, 0);
268         WM_keymap_add_item(keymap, "ED_FILE_OT_select_all", AKEY, KM_PRESS, 0, 0);
269         WM_keymap_add_item(keymap, "ED_FILE_OT_border_select", BKEY, KM_PRESS, 0, 0);
270         WM_keymap_add_item(keymap, "ED_FILE_OT_highlight", MOUSEMOVE, KM_ANY, 0, 0);
271         WM_keymap_add_item(keymap, "ED_FILE_OT_load", RETKEY, KM_PRESS, 0, 0);
272         WM_keymap_add_item(keymap, "ED_FILE_OT_cancel", ESCKEY, KM_PRESS, 0, 0);
273         
274         WM_keymap_add_item(keymap, "ED_FILE_OT_loadimages", TIMER1, KM_ANY, KM_ANY, 0);
275
276         keymap= WM_keymap_listbase(wm, "FileBookmark", SPACE_FILE, 0);
277         WM_keymap_add_item(keymap, "ED_FILE_OT_select_bookmark", LEFTMOUSE, KM_PRESS, 0, 0);
278 }
279
280
281 static void file_channel_area_init(wmWindowManager *wm, ARegion *ar)
282 {
283         ListBase *keymap;
284
285         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
286
287         /* own keymap */
288         keymap= WM_keymap_listbase(wm, "FileBookmark", SPACE_FILE, 0);
289         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, NULL, NULL);
290 }
291
292 static void file_channel_area_draw(const bContext *C, ARegion *ar)
293 {
294         View2D *v2d= &ar->v2d;
295         float col[3];
296
297         UI_GetThemeColor3fv(TH_BACK, col);
298         glClearColor(col[0], col[1], col[2], 0.0);
299         glClear(GL_COLOR_BUFFER_BIT);
300
301         /* data... */
302         UI_view2d_view_ortho(C, v2d);
303
304         file_draw_fsmenu(C, ar);
305 }
306
307 /* add handlers, stuff you only do once or on area/region changes */
308 static void file_header_area_init(wmWindowManager *wm, ARegion *ar)
309 {
310         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
311 }
312
313 static void file_header_area_draw(const bContext *C, ARegion *ar)
314 {
315         float col[3];
316         
317         /* clear */
318         if(ED_screen_area_active(C))
319                 UI_GetThemeColor3fv(TH_HEADER, col);
320         else
321                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
322         
323         glClearColor(col[0], col[1], col[2], 0.0);
324         glClear(GL_COLOR_BUFFER_BIT);
325         
326         /* set view2d view matrix for scrolling (without scrollers) */
327         UI_view2d_view_ortho(C, &ar->v2d);
328         
329         file_header_buttons(C, ar);
330         
331         /* restore view matrix? */
332         UI_view2d_view_restore(C);
333 }
334
335 /* add handlers, stuff you only do once or on area/region changes */
336 static void file_ui_area_init(wmWindowManager *wm, ARegion *ar)
337 {
338         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
339 }
340
341 static void file_ui_area_draw(const bContext *C, ARegion *ar)
342 {
343         float col[3];
344         /* clear */
345         UI_GetThemeColor3fv(TH_BACK, col);
346         glClearColor(col[0], col[1], col[2], 0.0);
347         glClear(GL_COLOR_BUFFER_BIT);
348
349         /* set view2d view matrix for scrolling (without scrollers) */
350         UI_view2d_view_ortho(C, &ar->v2d);
351
352         file_draw_buttons(C, ar);
353
354         UI_view2d_view_restore(C);
355 }
356
357 //static void file_main_area_listener(ARegion *ar, wmNotifier *wmn)
358 //{
359         /* context changes */
360 //}
361
362 /* only called once, from space/spacetypes.c */
363 void ED_spacetype_file(void)
364 {
365         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype file");
366         ARegionType *art;
367         
368         st->spaceid= SPACE_FILE;
369         
370         st->new= file_new;
371         st->free= file_free;
372         st->init= file_init;
373         st->duplicate= file_duplicate;
374         st->operatortypes= file_operatortypes;
375         st->keymap= file_keymap;
376         
377         /* regions: main window */
378         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
379         art->regionid = RGN_TYPE_WINDOW;
380         art->init= file_main_area_init;
381         art->draw= file_main_area_draw;
382         // art->listener= file_main_area_listener;
383         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
384         BLI_addhead(&st->regiontypes, art);
385         
386         /* regions: header */
387         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
388         art->regionid = RGN_TYPE_HEADER;
389         art->minsizey= HEADERY;
390         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
391         art->init= file_header_area_init;
392         art->draw= file_header_area_draw;
393         BLI_addhead(&st->regiontypes, art);
394         
395         /* regions: ui */
396         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
397         art->regionid = RGN_TYPE_UI;
398         art->minsizey= 100;
399         art->keymapflag= ED_KEYMAP_UI;
400         art->init= file_ui_area_init;
401         art->draw= file_ui_area_draw;
402         BLI_addhead(&st->regiontypes, art);
403
404         /* regions: channels (directories) */
405         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
406         art->regionid = RGN_TYPE_CHANNELS;
407         art->minsizex= 200;
408         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
409         art->init= file_channel_area_init;
410         art->draw= file_channel_area_draw;
411         BLI_addhead(&st->regiontypes, art);
412         
413         BKE_spacetype_register(st);
414
415 }
416
417 void ED_file_init(void)
418 {
419         char name[FILE_MAX];
420         BLI_make_file_string("/", name, BLI_gethome(), ".Bfs");
421         fsmenu_read_file(name);
422         filelist_init_icons();
423 }
424
425 void ED_file_exit(void)
426 {
427         fsmenu_free();
428         filelist_free_icons();
429 }