a03026d018433c85546d3ef8ebf80b98cb7115cf
[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 "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         filelist_setfilter(sfile->files, params->flag & FILE_FILTER ? params->filter : 0);      
199         if (filelist_empty(sfile->files))
200         {
201                 filelist_readdir(sfile->files);
202         }
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 || filelist_empty(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         WM_operatortype_append(FILE_OT_rename);
350 }
351
352 /* NOTE: do not add .blend file reading on this level */
353 void file_keymap(struct wmWindowManager *wm)
354 {
355         wmKeymapItem *kmi;
356         /* keys for all areas */
357         ListBase *keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);
358         WM_keymap_add_item(keymap, "FILE_OT_bookmark_toggle", NKEY, KM_PRESS, 0, 0);
359         WM_keymap_add_item(keymap, "FILE_OT_parent", PKEY, KM_PRESS, 0, 0);
360         WM_keymap_add_item(keymap, "FILE_OT_add_bookmark", BKEY, KM_PRESS, KM_CTRL, 0);
361         WM_keymap_add_item(keymap, "FILE_OT_hidedot", HKEY, KM_PRESS, 0, 0);
362         WM_keymap_add_item(keymap, "FILE_OT_previous", BACKSPACEKEY, KM_PRESS, 0, 0);
363         WM_keymap_add_item(keymap, "FILE_OT_next", BACKSPACEKEY, KM_PRESS, KM_SHIFT, 0);
364         WM_keymap_add_item(keymap, "FILE_OT_directory_new", IKEY, KM_PRESS, 0, 0);  /* XXX needs button */
365         WM_keymap_add_item(keymap, "FILE_OT_delete", XKEY, KM_PRESS, 0, 0);
366
367         /* keys for main area */
368         keymap= WM_keymap_listbase(wm, "FileMain", SPACE_FILE, 0);
369         WM_keymap_add_item(keymap, "FILE_OT_select", LEFTMOUSE, KM_PRESS, 0, 0);
370         WM_keymap_add_item(keymap, "FILE_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
371         WM_keymap_add_item(keymap, "FILE_OT_select_border", BKEY, KM_PRESS, 0, 0);
372         WM_keymap_add_item(keymap, "FILE_OT_rename", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
373         WM_keymap_add_item(keymap, "FILE_OT_highlight", MOUSEMOVE, KM_ANY, 0, 0);
374         WM_keymap_add_item(keymap, "FILE_OT_loadimages", TIMER1, KM_ANY, KM_ANY, 0);
375         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, 0, 0);
376         RNA_int_set(kmi->ptr, "increment", 1);
377         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, KM_SHIFT, 0);
378         RNA_int_set(kmi->ptr, "increment", 10);
379         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
380         RNA_int_set(kmi->ptr, "increment", 100);
381         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, 0,0);
382         RNA_int_set(kmi->ptr, "increment", -1);
383         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, KM_SHIFT, 0);
384         RNA_int_set(kmi->ptr, "increment", -10);
385         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, KM_CTRL, 0);
386         RNA_int_set(kmi->ptr, "increment",-100);
387         
388         /* keys for button area (top) */
389         keymap= WM_keymap_listbase(wm, "FileButtons", SPACE_FILE, 0);
390         WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, 0, 0);
391         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, 0, 0);
392         RNA_int_set(kmi->ptr, "increment", 1);
393         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, KM_SHIFT, 0);
394         RNA_int_set(kmi->ptr, "increment", 10);
395         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
396         RNA_int_set(kmi->ptr, "increment", 100);
397         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, 0, 0);
398         RNA_int_set(kmi->ptr, "increment", -1);
399         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, KM_SHIFT,0);
400         RNA_int_set(kmi->ptr, "increment", -10);
401         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, KM_CTRL,0);
402         RNA_int_set(kmi->ptr, "increment",-100);
403 }
404
405
406 static void file_channel_area_init(wmWindowManager *wm, ARegion *ar)
407 {
408         ListBase *keymap;
409
410         ED_region_panels_init(wm, ar);
411
412         /* own keymaps */
413         keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);  
414         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
415 }
416
417 static void file_channel_area_draw(const bContext *C, ARegion *ar)
418 {
419         ED_region_panels(C, ar, 1, NULL);
420 }
421
422 static void file_channel_area_listener(ARegion *ar, wmNotifier *wmn)
423 {
424         /* context changes */
425         switch(wmn->category) {
426                 
427         }
428 }
429
430 /* add handlers, stuff you only do once or on area/region changes */
431 static void file_header_area_init(wmWindowManager *wm, ARegion *ar)
432 {
433         ED_region_header_init(ar);
434 }
435
436 static void file_header_area_draw(const bContext *C, ARegion *ar)
437 {
438         ED_region_header(C, ar);
439 }
440
441 /* add handlers, stuff you only do once or on area/region changes */
442 static void file_ui_area_init(wmWindowManager *wm, ARegion *ar)
443 {
444         ListBase *keymap;
445
446         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
447
448         /* own keymap */
449         keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);  /* XXX weak? */
450         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
451
452         keymap= WM_keymap_listbase(wm, "FileButtons", SPACE_FILE, 0);   /* XXX weak? */
453         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
454 }
455
456 static void file_ui_area_draw(const bContext *C, ARegion *ar)
457 {
458         float col[3];
459         /* clear */
460         UI_GetThemeColor3fv(TH_PANEL, col);
461         glClearColor(col[0], col[1], col[2], 0.0);
462         glClear(GL_COLOR_BUFFER_BIT);
463
464         /* set view2d view matrix for scrolling (without scrollers) */
465         UI_view2d_view_ortho(C, &ar->v2d);
466
467         file_draw_buttons(C, ar);
468
469         UI_view2d_view_restore(C);
470 }
471
472 static void file_ui_area_listener(ARegion *ar, wmNotifier *wmn)
473 {
474         /* context changes */
475         switch(wmn->category) {
476                 case NC_FILE:
477                         switch (wmn->data) {
478                                 case ND_FILELIST:
479                                         ED_region_tag_redraw(ar);
480                                         break;
481                         }
482                         break;
483         }
484 }
485
486 /* only called once, from space/spacetypes.c */
487 void ED_spacetype_file(void)
488 {
489         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype file");
490         ARegionType *art;
491         
492         st->spaceid= SPACE_FILE;
493         
494         st->new= file_new;
495         st->free= file_free;
496         st->init= file_init;
497         st->duplicate= file_duplicate;
498         st->refresh= file_refresh;
499         st->listener= file_listener;
500         st->operatortypes= file_operatortypes;
501         st->keymap= file_keymap;
502         
503         /* regions: main window */
504         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
505         art->regionid = RGN_TYPE_WINDOW;
506         art->init= file_main_area_init;
507         art->draw= file_main_area_draw;
508         art->listener= file_main_area_listener;
509         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
510         BLI_addhead(&st->regiontypes, art);
511         
512         /* regions: header */
513         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
514         art->regionid = RGN_TYPE_HEADER;
515         art->minsizey= HEADERY;
516         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
517         art->init= file_header_area_init;
518         art->draw= file_header_area_draw;
519         // art->listener= file_header_area_listener;
520         BLI_addhead(&st->regiontypes, art);
521         
522         /* regions: ui */
523         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
524         art->regionid = RGN_TYPE_UI;
525         art->minsizey= 60;
526         art->keymapflag= ED_KEYMAP_UI;
527         art->listener= file_ui_area_listener;
528         art->init= file_ui_area_init;
529         art->draw= file_ui_area_draw;
530         BLI_addhead(&st->regiontypes, art);
531
532         /* regions: channels (directories) */
533         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
534         art->regionid = RGN_TYPE_CHANNELS;
535         art->minsizex= 240;
536         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
537         art->listener= file_channel_area_listener;
538         art->init= file_channel_area_init;
539         art->draw= file_channel_area_draw;
540         BLI_addhead(&st->regiontypes, art);
541         file_panels_register(art);
542
543         BKE_spacetype_register(st);
544
545 }
546
547 void ED_file_init(void)
548 {
549         char name[FILE_MAX];
550         BLI_make_file_string("/", name, BLI_gethome(), ".Bfs");
551         fsmenu_read_file(fsmenu_get(), name);
552         filelist_init_icons();
553         IMB_thumb_makedirs();
554 }
555
556 void ED_file_exit(void)
557 {
558         fsmenu_free(fsmenu_get());
559         filelist_free_icons();
560 }