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