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 "RNA_access.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BIF_gl.h"
42
43 #include "BLO_readfile.h"
44
45 #include "BLI_blenlib.h"
46 #include "BLI_arithb.h"
47 #include "BLI_rand.h"
48
49 #include "BKE_colortools.h"
50 #include "BKE_context.h"
51 #include "BKE_screen.h"
52
53 #include "ED_space_api.h"
54 #include "ED_screen.h"
55 #include "ED_fileselect.h"
56
57 #include "IMB_imbuf_types.h"
58 #include "IMB_thumbs.h"
59
60 #include "WM_api.h"
61 #include "WM_types.h"
62
63 #include "UI_interface.h"
64 #include "UI_resources.h"
65 #include "UI_view2d.h"
66
67
68 #include "ED_markers.h"
69 #include "ED_fileselect.h"
70
71 #include "file_intern.h"        // own include
72 #include "fsmenu.h"
73 #include "filelist.h"
74
75 /* ******************** default callbacks for file space ***************** */
76
77 static SpaceLink *file_new(const bContext *C)
78 {
79         ARegion *ar;
80         SpaceFile *sfile;
81         
82         sfile= MEM_callocN(sizeof(SpaceFile), "initfile");
83         sfile->spacetype= SPACE_FILE;
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_TOP;
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                 MEM_freeN(sfile->files);
124                 sfile->files= NULL;
125         }
126
127         if(sfile->folders_prev) {
128                 folderlist_free(sfile->folders_prev);
129                 MEM_freeN(sfile->folders_prev);
130                 sfile->folders_prev= NULL;
131         }
132
133         if(sfile->folders_next) {
134                 folderlist_free(sfile->folders_next);
135                 MEM_freeN(sfile->folders_next);
136                 sfile->folders_next= NULL;
137         }
138
139         if (sfile->params) {
140                 if(sfile->params->pupmenu)
141                         MEM_freeN(sfile->params->pupmenu);
142                 MEM_freeN(sfile->params);
143                 sfile->params= NULL;
144         }
145
146         if (sfile->layout) {
147                 MEM_freeN(sfile->layout);
148                 sfile->layout = NULL;
149         }
150 }
151
152
153 /* spacetype; init callback, area size changes, screen set, etc */
154 static void file_init(struct wmWindowManager *wm, ScrArea *sa)
155 {
156         printf("file_init\n");
157 }
158
159
160 static SpaceLink *file_duplicate(SpaceLink *sl)
161 {
162         SpaceFile *sfileo= (SpaceFile*)sl;
163         SpaceFile *sfilen= MEM_dupallocN(sl);
164         
165         /* clear or remove stuff from old */
166         sfilen->op = NULL; /* file window doesn't own operators */
167
168         sfilen->files = filelist_new();
169         if(sfileo->folders_prev)
170                 sfilen->folders_prev = MEM_dupallocN(sfileo->folders_prev);
171
172         if(sfileo->folders_next)
173                 sfilen->folders_next = MEM_dupallocN(sfileo->folders_next);
174
175         if(sfileo->params) {
176                 sfilen->params= MEM_dupallocN(sfileo->params);
177                 file_change_dir(sfilen);
178         }
179         if (sfileo->layout) {
180                 sfilen->layout= MEM_dupallocN(sfileo->layout);
181         }
182         return (SpaceLink *)sfilen;
183 }
184
185 static void file_refresh(const bContext *C, ScrArea *sa)
186 {
187         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
188         FileSelectParams *params = ED_fileselect_get_params(sfile);
189
190         if (!sfile->folders_prev)
191                 sfile->folders_prev = folderlist_new();
192         if (!sfile->files) {
193                 sfile->files = filelist_new();
194                 file_change_dir(sfile);
195                 params->active_file = -1; // added this so it opens nicer (ton)
196         }
197         filelist_hidedot(sfile->files, params->flag & FILE_HIDE_DOT);
198         if (filelist_empty(sfile->files))
199         {
200                 filelist_readdir(sfile->files);
201         }
202         filelist_setfilter(sfile->files, params->flag & FILE_FILTER ? params->filter : 0);      
203         if(params->sort!=FILE_SORT_NONE) filelist_sort(sfile->files, params->sort);             
204
205         if (sfile->layout) sfile->layout->dirty= 1;
206
207 }
208
209 static void file_listener(ScrArea *sa, wmNotifier *wmn)
210 {
211         SpaceFile* sfile = (SpaceFile*)sa->spacedata.first;
212
213         /* context changes */
214         switch(wmn->category) {
215                 case NC_FILE:
216                         switch (wmn->data) {
217                                 case ND_FILELIST:
218                                         if (sfile->files) filelist_free(sfile->files);
219                                         ED_area_tag_refresh(sa);
220                                         break;
221                                 case ND_PARAMS:
222                                         ED_area_tag_refresh(sa);
223                                         break;
224                         }
225                         break;
226         }
227 }
228
229 /* add handlers, stuff you only do once or on area/region changes */
230 static void file_main_area_init(wmWindowManager *wm, ARegion *ar)
231 {
232         ListBase *keymap;
233         
234         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
235         
236         /* own keymaps */
237         keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);
238         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
239
240         keymap= WM_keymap_listbase(wm, "FileMain", SPACE_FILE, 0);
241         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
242                                                            
243
244 }
245
246 static void file_main_area_listener(ARegion *ar, wmNotifier *wmn)
247 {
248         /* context changes */
249         switch(wmn->category) {
250                 case NC_FILE:
251                         switch (wmn->data) {
252                                 case ND_FILELIST:
253                                         ED_region_tag_redraw(ar);
254                                         break;
255                                 case ND_PARAMS:
256                                         ED_region_tag_redraw(ar);
257                                         break;
258                         }
259                         break;
260         }
261 }
262
263 static void file_main_area_draw(const bContext *C, ARegion *ar)
264 {
265         /* draw entirely, view changes should be handled here */
266         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
267         FileSelectParams *params = ED_fileselect_get_params(sfile);
268         FileLayout *layout=NULL;
269
270         View2D *v2d= &ar->v2d;
271         View2DScrollers *scrollers;
272         float col[3];
273
274         /* Needed, because filelist is not initialized on loading */
275         if (!sfile->files)
276                 file_refresh(C, NULL);
277
278         layout = ED_fileselect_get_layout(sfile, ar);
279
280         /* clear and setup matrix */
281         UI_GetThemeColor3fv(TH_BACK, col);
282         glClearColor(col[0], col[1], col[2], 0.0);
283         glClear(GL_COLOR_BUFFER_BIT);
284         
285         /* Allow dynamically sliders to be set, saves notifiers etc. */
286         if (layout && (layout->flag == FILE_LAYOUT_VER)) {
287                 v2d->scroll = V2D_SCROLL_RIGHT;
288                 v2d->keepofs &= ~V2D_LOCKOFS_Y;
289                 v2d->keepofs |= V2D_LOCKOFS_X;
290         }
291         else {
292                 v2d->scroll = V2D_SCROLL_BOTTOM;
293                 v2d->keepofs &= ~V2D_LOCKOFS_X;
294                 v2d->keepofs |= V2D_LOCKOFS_Y;
295         }
296         /* v2d has initialized flag, so this call will only set the mask correct */
297         UI_view2d_region_reinit(v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
298
299         /* sets tile/border settings in sfile */
300         file_calc_previews(C, ar);
301
302         /* set view */
303         UI_view2d_view_ortho(C, v2d);
304         
305         /* on first read, find active file */
306         if (params->active_file == -1) {
307                 wmEvent *event= CTX_wm_window(C)->eventstate;
308                 file_hilight_set(sfile, ar, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin);
309         }
310         
311         if (params->display == FILE_IMGDISPLAY) {
312                 file_draw_previews(C, ar);
313         } else {
314                 file_draw_list(C, ar);
315         }
316         
317         
318         /* reset view matrix */
319         UI_view2d_view_restore(C);
320         
321         /* scrollers */
322         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
323         UI_view2d_scrollers_draw(C, v2d, scrollers);
324         UI_view2d_scrollers_free(scrollers);
325
326 }
327
328 void file_operatortypes(void)
329 {
330         WM_operatortype_append(FILE_OT_select);
331         WM_operatortype_append(FILE_OT_select_all_toggle);
332         WM_operatortype_append(FILE_OT_select_border);
333         WM_operatortype_append(FILE_OT_select_bookmark);
334         WM_operatortype_append(FILE_OT_loadimages);
335         WM_operatortype_append(FILE_OT_highlight);
336         WM_operatortype_append(FILE_OT_exec);
337         WM_operatortype_append(FILE_OT_cancel);
338         WM_operatortype_append(FILE_OT_parent);
339         WM_operatortype_append(FILE_OT_previous);
340         WM_operatortype_append(FILE_OT_next);
341         WM_operatortype_append(FILE_OT_refresh);
342         WM_operatortype_append(FILE_OT_bookmark_toggle);
343         WM_operatortype_append(FILE_OT_add_bookmark);
344         WM_operatortype_append(FILE_OT_delete_bookmark);
345         WM_operatortype_append(FILE_OT_hidedot);
346         WM_operatortype_append(FILE_OT_filenum);
347         WM_operatortype_append(FILE_OT_directory_new);
348         WM_operatortype_append(FILE_OT_delete);
349 }
350
351 /* NOTE: do not add .blend file reading on this level */
352 void file_keymap(struct wmWindowManager *wm)
353 {
354         wmKeymapItem *kmi;
355         /* keys for all areas */
356         ListBase *keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);
357         WM_keymap_add_item(keymap, "FILE_OT_bookmark_toggle", NKEY, KM_PRESS, 0, 0);
358         WM_keymap_add_item(keymap, "FILE_OT_parent", PKEY, KM_PRESS, 0, 0);
359         WM_keymap_add_item(keymap, "FILE_OT_add_bookmark", BKEY, KM_PRESS, KM_CTRL, 0);
360         WM_keymap_add_item(keymap, "FILE_OT_hidedot", HKEY, KM_PRESS, 0, 0);
361         WM_keymap_add_item(keymap, "FILE_OT_previous", BACKSPACEKEY, KM_PRESS, 0, 0);
362         WM_keymap_add_item(keymap, "FILE_OT_next", BACKSPACEKEY, KM_PRESS, KM_SHIFT, 0);
363         /* WM_keymap_add_item(keymap, "FILE_OT_directory_new", IKEY, KM_PRESS, 0, 0); */ /* XXX needs button */
364         WM_keymap_add_item(keymap, "FILE_OT_delete", XKEY, KM_PRESS, 0, 0);
365
366         /* keys for main area */
367         keymap= WM_keymap_listbase(wm, "FileMain", SPACE_FILE, 0);
368         WM_keymap_add_item(keymap, "FILE_OT_select", LEFTMOUSE, KM_PRESS, 0, 0);
369         WM_keymap_add_item(keymap, "FILE_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
370         WM_keymap_add_item(keymap, "FILE_OT_select_border", BKEY, KM_PRESS, 0, 0);
371         WM_keymap_add_item(keymap, "FILE_OT_highlight", MOUSEMOVE, KM_ANY, 0, 0);
372         WM_keymap_add_item(keymap, "FILE_OT_loadimages", TIMER1, KM_ANY, KM_ANY, 0);
373         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, 0, 0);
374         RNA_int_set(kmi->ptr, "increment", 1);
375         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, KM_SHIFT, 0);
376         RNA_int_set(kmi->ptr, "increment", 10);
377         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
378         RNA_int_set(kmi->ptr, "increment", 100);
379         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, 0,0);
380         RNA_int_set(kmi->ptr, "increment", -1);
381         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, KM_SHIFT, 0);
382         RNA_int_set(kmi->ptr, "increment", -10);
383         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, KM_CTRL, 0);
384         RNA_int_set(kmi->ptr, "increment",-100);
385         
386         /* keys for button area (top) */
387         keymap= WM_keymap_listbase(wm, "FileButtons", SPACE_FILE, 0);
388         WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, 0, 0);
389         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, 0, 0);
390         RNA_int_set(kmi->ptr, "increment", 1);
391         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, KM_SHIFT, 0);
392         RNA_int_set(kmi->ptr, "increment", 10);
393         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
394         RNA_int_set(kmi->ptr, "increment", 100);
395         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, 0, 0);
396         RNA_int_set(kmi->ptr, "increment", -1);
397         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, KM_SHIFT,0);
398         RNA_int_set(kmi->ptr, "increment", -10);
399         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, KM_CTRL,0);
400         RNA_int_set(kmi->ptr, "increment",-100);
401 }
402
403
404 static void file_channel_area_init(wmWindowManager *wm, ARegion *ar)
405 {
406         ListBase *keymap;
407
408         ED_region_panels_init(wm, ar);
409
410         /* own keymaps */
411         keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);  
412         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
413 }
414
415 static void file_channel_area_draw(const bContext *C, ARegion *ar)
416 {
417         ED_region_panels(C, ar, 1, NULL);
418 }
419
420 static void file_channel_area_listener(ARegion *ar, wmNotifier *wmn)
421 {
422         /* context changes */
423         switch(wmn->category) {
424                 
425         }
426 }
427
428 /* add handlers, stuff you only do once or on area/region changes */
429 static void file_header_area_init(wmWindowManager *wm, ARegion *ar)
430 {
431         ED_region_header_init(ar);
432 }
433
434 static void file_header_area_draw(const bContext *C, ARegion *ar)
435 {
436         ED_region_header(C, ar);
437 }
438
439 /* add handlers, stuff you only do once or on area/region changes */
440 static void file_ui_area_init(wmWindowManager *wm, ARegion *ar)
441 {
442         ListBase *keymap;
443
444         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
445
446         /* own keymap */
447         keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);  /* XXX weak? */
448         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
449
450         keymap= WM_keymap_listbase(wm, "FileButtons", SPACE_FILE, 0);   /* XXX weak? */
451         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
452 }
453
454 static void file_ui_area_draw(const bContext *C, ARegion *ar)
455 {
456         float col[3];
457         /* clear */
458         UI_GetThemeColor3fv(TH_PANEL, col);
459         glClearColor(col[0], col[1], col[2], 0.0);
460         glClear(GL_COLOR_BUFFER_BIT);
461
462         /* set view2d view matrix for scrolling (without scrollers) */
463         UI_view2d_view_ortho(C, &ar->v2d);
464
465         file_draw_buttons(C, ar);
466
467         UI_view2d_view_restore(C);
468 }
469
470 //static void file_main_area_listener(ARegion *ar, wmNotifier *wmn)
471 //{
472         /* context changes */
473 //}
474
475 /* only called once, from space/spacetypes.c */
476 void ED_spacetype_file(void)
477 {
478         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype file");
479         ARegionType *art;
480         
481         st->spaceid= SPACE_FILE;
482         
483         st->new= file_new;
484         st->free= file_free;
485         st->init= file_init;
486         st->duplicate= file_duplicate;
487         st->refresh= file_refresh;
488         st->listener= file_listener;
489         st->operatortypes= file_operatortypes;
490         st->keymap= file_keymap;
491         
492         /* regions: main window */
493         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
494         art->regionid = RGN_TYPE_WINDOW;
495         art->init= file_main_area_init;
496         art->draw= file_main_area_draw;
497         art->listener= file_main_area_listener;
498         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
499         BLI_addhead(&st->regiontypes, art);
500         
501         /* regions: header */
502         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
503         art->regionid = RGN_TYPE_HEADER;
504         art->minsizey= HEADERY;
505         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
506         art->init= file_header_area_init;
507         art->draw= file_header_area_draw;
508         // art->listener= file_header_area_listener;
509         BLI_addhead(&st->regiontypes, art);
510         
511         /* regions: ui */
512         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
513         art->regionid = RGN_TYPE_UI;
514         art->minsizey= 60;
515         art->keymapflag= ED_KEYMAP_UI;
516         art->init= file_ui_area_init;
517         art->draw= file_ui_area_draw;
518         BLI_addhead(&st->regiontypes, art);
519
520         /* regions: channels (directories) */
521         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
522         art->regionid = RGN_TYPE_CHANNELS;
523         art->minsizex= 240;
524         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
525         art->listener= file_channel_area_listener;
526         art->init= file_channel_area_init;
527         art->draw= file_channel_area_draw;
528         BLI_addhead(&st->regiontypes, art);
529         file_panels_register(art);
530
531         BKE_spacetype_register(st);
532
533 }
534
535 void ED_file_init(void)
536 {
537         char name[FILE_MAX];
538         BLI_make_file_string("/", name, BLI_gethome(), ".Bfs");
539         fsmenu_read_file(fsmenu_get(), name);
540         filelist_init_icons();
541         IMB_thumb_makedirs();
542 }
543
544 void ED_file_exit(void)
545 {
546         fsmenu_free(fsmenu_get());
547         filelist_free_icons();
548 }