NLA SoC: Merge from 2.5
[blender.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 "BIF_gl.h"
40
41 #include "BLO_readfile.h"
42
43 #include "BLI_blenlib.h"
44 #include "BLI_arithb.h"
45 #include "BLI_rand.h"
46
47 #include "BKE_colortools.h"
48 #include "BKE_context.h"
49 #include "BKE_screen.h"
50
51 #include "ED_space_api.h"
52 #include "ED_screen.h"
53 #include "ED_fileselect.h"
54
55 #include "IMB_imbuf_types.h"
56 #include "IMB_thumbs.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "UI_interface.h"
62 #include "UI_resources.h"
63 #include "UI_view2d.h"
64
65
66 #include "ED_markers.h"
67 #include "ED_fileselect.h"
68
69 #include "file_intern.h"        // own include
70 #include "fsmenu.h"
71 #include "filelist.h"
72
73 /* ******************** default callbacks for file space ***************** */
74
75 static SpaceLink *file_new(const bContext *C)
76 {
77         ARegion *ar;
78         SpaceFile *sfile;
79         
80         sfile= MEM_callocN(sizeof(SpaceFile), "initfile");
81         sfile->spacetype= SPACE_FILE;
82
83         /* header */
84         ar= MEM_callocN(sizeof(ARegion), "header for file");
85         BLI_addtail(&sfile->regionbase, ar);
86         ar->regiontype= RGN_TYPE_HEADER;
87         ar->alignment= RGN_ALIGN_TOP;
88
89         /* channel list region */
90         ar= MEM_callocN(sizeof(ARegion), "channel area for file");
91         BLI_addtail(&sfile->regionbase, ar);
92         ar->regiontype= RGN_TYPE_CHANNELS;
93         ar->alignment= RGN_ALIGN_LEFT;  
94
95         /* ui list region */
96         ar= MEM_callocN(sizeof(ARegion), "ui area for file");
97         BLI_addtail(&sfile->regionbase, ar);
98         ar->regiontype= RGN_TYPE_UI;
99         ar->alignment= RGN_ALIGN_TOP;
100
101         /* main area */
102         ar= MEM_callocN(sizeof(ARegion), "main area for file");
103         BLI_addtail(&sfile->regionbase, ar);
104         ar->regiontype= RGN_TYPE_WINDOW;
105         ar->v2d.scroll = (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
106         ar->v2d.align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
107         ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPZOOM|V2D_KEEPASPECT);
108         ar->v2d.keeptot= V2D_KEEPTOT_STRICT;
109         ar->v2d.minzoom= ar->v2d.maxzoom= 1.0f;
110
111         return (SpaceLink *)sfile;
112 }
113
114 /* not spacelink itself */
115 static void file_free(SpaceLink *sl)
116 {       
117         SpaceFile *sfile= (SpaceFile *) sl;
118         
119         if(sfile->files) {
120                 filelist_free(sfile->files);
121                 MEM_freeN(sfile->files);
122                 sfile->files= NULL;
123         }
124
125         if (sfile->params) {
126                 if(sfile->params->pupmenu)
127                         MEM_freeN(sfile->params->pupmenu);
128                 MEM_freeN(sfile->params);
129                 sfile->params= NULL;
130         }
131
132         if (sfile->layout) {
133                 MEM_freeN(sfile->layout);
134                 sfile->layout = NULL;
135         }
136 }
137
138
139 /* spacetype; init callback, area size changes, screen set, etc */
140 static void file_init(struct wmWindowManager *wm, ScrArea *sa)
141 {
142 }
143
144
145 static SpaceLink *file_duplicate(SpaceLink *sl)
146 {
147         SpaceFile *sfileo= (SpaceFile*)sl;
148         SpaceFile *sfilen= MEM_dupallocN(sl);
149         
150         /* clear or remove stuff from old */
151         sfilen->op = NULL; /* file window doesn't own operators */
152
153         sfilen->files = filelist_new();
154         
155         if(sfileo->params) {
156                 sfilen->params= MEM_dupallocN(sfileo->params);
157         
158                 filelist_setdir(sfilen->files, sfilen->params->dir);
159         }
160         if (sfileo->layout) {
161                 sfilen->layout= MEM_dupallocN(sfileo->layout);
162         }
163         return (SpaceLink *)sfilen;
164 }
165
166 static void file_refresh(const bContext *C, ScrArea *sa)
167 {
168         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
169         FileSelectParams *params = ED_fileselect_get_params(sfile);
170
171         if (!sfile->files) {
172                 sfile->files = filelist_new();
173                 filelist_setdir(sfile->files, params->dir);
174                 params->active_file = -1; // added this so it opens nicer (ton)
175         }
176         filelist_hidedot(sfile->files, params->flag & FILE_HIDE_DOT);
177         if (filelist_empty(sfile->files))
178         {
179                 filelist_readdir(sfile->files);
180         }
181         filelist_setfilter(sfile->files, params->flag & FILE_FILTER ? params->filter : 0);      
182         if(params->sort!=FILE_SORT_NONE) filelist_sort(sfile->files, params->sort);             
183 }
184
185 static void file_listener(ScrArea *sa, wmNotifier *wmn)
186 {
187         SpaceFile* sfile = (SpaceFile*)sa->spacedata.first;
188
189         /* context changes */
190         switch(wmn->category) {
191                 case NC_FILE:
192                         switch (wmn->data) {
193                                 case ND_FILELIST:
194                                         if (sfile->files) filelist_free(sfile->files);
195                                         ED_area_tag_refresh(sa);
196                                         ED_area_tag_redraw(sa);
197                                         break;
198                                 case ND_PARAMS:
199                                         ED_area_tag_refresh(sa);
200                                         ED_area_tag_redraw(sa);
201                                         break;
202                         }
203                         break;
204         }
205 }
206
207 /* add handlers, stuff you only do once or on area/region changes */
208 static void file_main_area_init(wmWindowManager *wm, ARegion *ar)
209 {
210         ListBase *keymap;
211         
212         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
213         
214         /* own keymap */
215         keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);  /* XXX weak? */
216         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
217                                                            
218
219 }
220
221 static void file_main_area_draw(const bContext *C, ARegion *ar)
222 {
223         /* draw entirely, view changes should be handled here */
224         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
225         FileSelectParams *params = ED_fileselect_get_params(sfile);
226         FileLayout *layout=NULL;
227
228         View2D *v2d= &ar->v2d;
229         View2DScrollers *scrollers;
230         float col[3];
231
232         layout = ED_fileselect_get_layout(sfile, ar);
233
234         /* clear and setup matrix */
235         UI_GetThemeColor3fv(TH_BACK, col);
236         glClearColor(col[0], col[1], col[2], 0.0);
237         glClear(GL_COLOR_BUFFER_BIT);
238         
239         /* Allow dynamically sliders to be set, saves notifiers etc. */
240         if (layout && (layout->flag == FILE_LAYOUT_VER)) {
241                 v2d->scroll = V2D_SCROLL_RIGHT;
242                 v2d->keepofs &= ~V2D_LOCKOFS_Y;
243                 v2d->keepofs |= V2D_LOCKOFS_X;
244         }
245         else {
246                 v2d->scroll = V2D_SCROLL_BOTTOM;
247                 v2d->keepofs &= ~V2D_LOCKOFS_X;
248                 v2d->keepofs |= V2D_LOCKOFS_Y;
249         }
250         /* v2d has initialized flag, so this call will only set the mask correct */
251         UI_view2d_region_reinit(v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
252
253         /* sets tile/border settings in sfile */
254         file_calc_previews(C, ar);
255
256         /* set view */
257         UI_view2d_view_ortho(C, v2d);
258         
259         /* on first read, find active file */
260         if (params->active_file == -1) {
261                 wmEvent *event= CTX_wm_window(C)->eventstate;
262                 file_hilight_set(sfile, ar, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin);
263         }
264         
265         if (params->display == FILE_IMGDISPLAY) {
266                 file_draw_previews(C, ar);
267         } else {
268                 file_draw_list(C, ar);
269         }
270         
271         
272         /* reset view matrix */
273         UI_view2d_view_restore(C);
274         
275         /* scrollers */
276         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
277         UI_view2d_scrollers_draw(C, v2d, scrollers);
278         UI_view2d_scrollers_free(scrollers);
279
280 }
281
282 void file_operatortypes(void)
283 {
284         WM_operatortype_append(FILE_OT_select);
285         WM_operatortype_append(FILE_OT_select_all_toggle);
286         WM_operatortype_append(FILE_OT_select_border);
287         WM_operatortype_append(FILE_OT_select_bookmark);
288         WM_operatortype_append(FILE_OT_loadimages);
289         WM_operatortype_append(FILE_OT_highlight);
290         WM_operatortype_append(FILE_OT_exec);
291         WM_operatortype_append(FILE_OT_cancel);
292         WM_operatortype_append(FILE_OT_parent);
293         WM_operatortype_append(FILE_OT_refresh);
294         WM_operatortype_append(FILE_OT_bookmark_toggle);
295         WM_operatortype_append(FILE_OT_add_bookmark);
296         WM_operatortype_append(FILE_OT_delete_bookmark);
297         WM_operatortype_append(FILE_OT_hidedot);
298 }
299
300 /* NOTE: do not add .blend file reading on this level */
301 void file_keymap(struct wmWindowManager *wm)
302 {
303         ListBase *keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);
304         WM_keymap_add_item(keymap, "FILE_OT_bookmark_toggle", NKEY, KM_PRESS, 0, 0);
305         WM_keymap_add_item(keymap, "FILE_OT_select", LEFTMOUSE, KM_PRESS, 0, 0);
306         WM_keymap_add_item(keymap, "FILE_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
307         WM_keymap_add_item(keymap, "FILE_OT_select_border", BKEY, KM_PRESS, 0, 0);
308         WM_keymap_add_item(keymap, "FILE_OT_highlight", MOUSEMOVE, KM_ANY, 0, 0);
309         WM_keymap_add_item(keymap, "FILE_OT_parent", PKEY, KM_PRESS, 0, 0);
310         WM_keymap_add_item(keymap, "FILE_OT_add_bookmark", BKEY, KM_PRESS, KM_CTRL, 0);
311         WM_keymap_add_item(keymap, "FILE_OT_hidedot", HKEY, KM_PRESS, 0, 0);
312         WM_keymap_add_item(keymap, "FILE_OT_loadimages", TIMER1, KM_ANY, KM_ANY, 0);
313         
314         keymap= WM_keymap_listbase(wm, "FileBookmark", SPACE_FILE, 0);
315         WM_keymap_add_item(keymap, "FILE_OT_select_bookmark", LEFTMOUSE, KM_PRESS, 0, 0);
316 }
317
318
319 static void file_channel_area_init(wmWindowManager *wm, ARegion *ar)
320 {
321         ED_region_panels_init(wm, ar);
322 }
323
324 static void file_channel_area_draw(const bContext *C, ARegion *ar)
325 {
326         ED_region_panels(C, ar, 1, NULL);
327 }
328
329 static void file_channel_area_listener(ARegion *ar, wmNotifier *wmn)
330 {
331         /* context changes */
332         switch(wmn->category) {
333                 
334         }
335 }
336
337 /* add handlers, stuff you only do once or on area/region changes */
338 static void file_header_area_init(wmWindowManager *wm, ARegion *ar)
339 {
340         ED_region_header_init(ar);
341 }
342
343 static void file_header_area_draw(const bContext *C, ARegion *ar)
344 {
345         ED_region_header(C, ar);
346 }
347
348 /* add handlers, stuff you only do once or on area/region changes */
349 static void file_ui_area_init(wmWindowManager *wm, ARegion *ar)
350 {
351         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
352 }
353
354 static void file_ui_area_draw(const bContext *C, ARegion *ar)
355 {
356         float col[3];
357         /* clear */
358         UI_GetThemeColor3fv(TH_PANEL, col);
359         glClearColor(col[0], col[1], col[2], 0.0);
360         glClear(GL_COLOR_BUFFER_BIT);
361
362         /* set view2d view matrix for scrolling (without scrollers) */
363         UI_view2d_view_ortho(C, &ar->v2d);
364
365         file_draw_buttons(C, ar);
366
367         UI_view2d_view_restore(C);
368 }
369
370 //static void file_main_area_listener(ARegion *ar, wmNotifier *wmn)
371 //{
372         /* context changes */
373 //}
374
375 /* only called once, from space/spacetypes.c */
376 void ED_spacetype_file(void)
377 {
378         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype file");
379         ARegionType *art;
380         
381         st->spaceid= SPACE_FILE;
382         
383         st->new= file_new;
384         st->free= file_free;
385         st->init= file_init;
386         st->duplicate= file_duplicate;
387         st->refresh= file_refresh;
388         st->listener= file_listener;
389         st->operatortypes= file_operatortypes;
390         st->keymap= file_keymap;
391         
392         /* regions: main window */
393         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
394         art->regionid = RGN_TYPE_WINDOW;
395         art->init= file_main_area_init;
396         art->draw= file_main_area_draw;
397         // art->listener= file_main_area_listener;
398         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
399         BLI_addhead(&st->regiontypes, art);
400         
401         /* regions: header */
402         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
403         art->regionid = RGN_TYPE_HEADER;
404         art->minsizey= HEADERY;
405         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
406         art->init= file_header_area_init;
407         art->draw= file_header_area_draw;
408         // art->listener= file_header_area_listener;
409         BLI_addhead(&st->regiontypes, art);
410         
411         /* regions: ui */
412         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
413         art->regionid = RGN_TYPE_UI;
414         art->minsizey= 60;
415         art->keymapflag= ED_KEYMAP_UI;
416         art->init= file_ui_area_init;
417         art->draw= file_ui_area_draw;
418         BLI_addhead(&st->regiontypes, art);
419
420         /* regions: channels (directories) */
421         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
422         art->regionid = RGN_TYPE_CHANNELS;
423         art->minsizex= 240;
424         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
425         art->listener= file_channel_area_listener;
426         art->init= file_channel_area_init;
427         art->draw= file_channel_area_draw;
428         BLI_addhead(&st->regiontypes, art);
429         file_panels_register(art);
430
431
432         BKE_spacetype_register(st);
433
434 }
435
436 void ED_file_init(void)
437 {
438         char name[FILE_MAX];
439         BLI_make_file_string("/", name, BLI_gethome(), ".Bfs");
440         fsmenu_read_file(fsmenu_get(), name);
441         filelist_init_icons();
442         IMB_thumb_makedirs();
443 }
444
445 void ED_file_exit(void)
446 {
447         fsmenu_free(fsmenu_get());
448         filelist_free_icons();
449 }