7cdd1d89041d7388a9b731c75e4fef8e9db5390a
[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= CTX_wm_space_file(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                                         ED_area_tag_redraw(sa);
221                                         break;
222                                 case ND_PARAMS:
223                                         ED_area_tag_refresh(sa);
224                                         ED_area_tag_redraw(sa);
225                                         break;
226                         }
227                         break;
228         }
229 }
230
231 /* add handlers, stuff you only do once or on area/region changes */
232 static void file_main_area_init(wmWindowManager *wm, ARegion *ar)
233 {
234         ListBase *keymap;
235         
236         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
237         
238         /* own keymaps */
239         keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);
240         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
241
242         keymap= WM_keymap_listbase(wm, "FileMain", SPACE_FILE, 0);
243         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
244                                                            
245
246 }
247
248 static void file_main_area_listener(ARegion *ar, wmNotifier *wmn)
249 {
250         /* context changes */
251         switch(wmn->category) {
252                 case NC_FILE:
253                         switch (wmn->data) {
254                                 case ND_FILELIST:
255                                         ED_region_tag_redraw(ar);
256                                         break;
257                                 case ND_PARAMS:
258                                         ED_region_tag_redraw(ar);
259                                         break;
260                         }
261                         break;
262         }
263 }
264
265 static void file_main_area_draw(const bContext *C, ARegion *ar)
266 {
267         /* draw entirely, view changes should be handled here */
268         SpaceFile *sfile= CTX_wm_space_file(C);
269         FileSelectParams *params = ED_fileselect_get_params(sfile);
270         FileLayout *layout=NULL;
271
272         View2D *v2d= &ar->v2d;
273         View2DScrollers *scrollers;
274         float col[3];
275
276         /* Needed, because filelist is not initialized on loading */
277         if (!sfile->files || filelist_empty(sfile->files))
278                 file_refresh(C, NULL);
279
280         layout = ED_fileselect_get_layout(sfile, ar);
281
282         /* clear and setup matrix */
283         UI_GetThemeColor3fv(TH_BACK, col);
284         glClearColor(col[0], col[1], col[2], 0.0);
285         glClear(GL_COLOR_BUFFER_BIT);
286         
287         /* Allow dynamically sliders to be set, saves notifiers etc. */
288         if (layout && (layout->flag == FILE_LAYOUT_VER)) {
289                 v2d->scroll = V2D_SCROLL_RIGHT;
290                 v2d->keepofs &= ~V2D_LOCKOFS_Y;
291                 v2d->keepofs |= V2D_LOCKOFS_X;
292         }
293         else {
294                 v2d->scroll = V2D_SCROLL_BOTTOM;
295                 v2d->keepofs &= ~V2D_LOCKOFS_X;
296                 v2d->keepofs |= V2D_LOCKOFS_Y;
297         }
298         /* v2d has initialized flag, so this call will only set the mask correct */
299         UI_view2d_region_reinit(v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
300
301         /* sets tile/border settings in sfile */
302         file_calc_previews(C, ar);
303
304         /* set view */
305         UI_view2d_view_ortho(C, v2d);
306         
307         /* on first read, find active file */
308         if (params->active_file == -1) {
309                 wmEvent *event= CTX_wm_window(C)->eventstate;
310                 file_hilight_set(sfile, ar, event->x, event->y);
311         }
312         
313         if (params->display == FILE_IMGDISPLAY) {
314                 file_draw_previews(C, ar);
315         } else {
316                 file_draw_list(C, ar);
317         }
318         
319         
320         /* reset view matrix */
321         UI_view2d_view_restore(C);
322         
323         /* scrollers */
324         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
325         UI_view2d_scrollers_draw(C, v2d, scrollers);
326         UI_view2d_scrollers_free(scrollers);
327
328 }
329
330 void file_operatortypes(void)
331 {
332         WM_operatortype_append(FILE_OT_select);
333         WM_operatortype_append(FILE_OT_select_all_toggle);
334         WM_operatortype_append(FILE_OT_select_border);
335         WM_operatortype_append(FILE_OT_select_bookmark);
336         WM_operatortype_append(FILE_OT_loadimages);
337         WM_operatortype_append(FILE_OT_highlight);
338         WM_operatortype_append(FILE_OT_exec);
339         WM_operatortype_append(FILE_OT_cancel);
340         WM_operatortype_append(FILE_OT_parent);
341         WM_operatortype_append(FILE_OT_previous);
342         WM_operatortype_append(FILE_OT_next);
343         WM_operatortype_append(FILE_OT_refresh);
344         WM_operatortype_append(FILE_OT_bookmark_toggle);
345         WM_operatortype_append(FILE_OT_add_bookmark);
346         WM_operatortype_append(FILE_OT_delete_bookmark);
347         WM_operatortype_append(FILE_OT_hidedot);
348         WM_operatortype_append(FILE_OT_filenum);
349         WM_operatortype_append(FILE_OT_directory_new);
350         WM_operatortype_append(FILE_OT_delete);
351         WM_operatortype_append(FILE_OT_rename);
352 }
353
354 /* NOTE: do not add .blend file reading on this level */
355 void file_keymap(struct wmWindowManager *wm)
356 {
357         wmKeymapItem *kmi;
358         /* keys for all areas */
359         ListBase *keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);
360         WM_keymap_add_item(keymap, "FILE_OT_bookmark_toggle", NKEY, KM_PRESS, 0, 0);
361         WM_keymap_add_item(keymap, "FILE_OT_parent", PKEY, KM_PRESS, 0, 0);
362         WM_keymap_add_item(keymap, "FILE_OT_add_bookmark", BKEY, KM_PRESS, KM_CTRL, 0);
363         WM_keymap_add_item(keymap, "FILE_OT_hidedot", HKEY, KM_PRESS, 0, 0);
364         WM_keymap_add_item(keymap, "FILE_OT_previous", BACKSPACEKEY, KM_PRESS, 0, 0);
365         WM_keymap_add_item(keymap, "FILE_OT_next", BACKSPACEKEY, KM_PRESS, KM_SHIFT, 0);
366         WM_keymap_add_item(keymap, "FILE_OT_directory_new", IKEY, KM_PRESS, 0, 0);  /* XXX needs button */
367         WM_keymap_add_item(keymap, "FILE_OT_delete", XKEY, KM_PRESS, 0, 0);
368
369         /* keys for main area */
370         keymap= WM_keymap_listbase(wm, "FileMain", SPACE_FILE, 0);
371         WM_keymap_add_item(keymap, "FILE_OT_select", LEFTMOUSE, KM_PRESS, 0, 0);
372         WM_keymap_add_item(keymap, "FILE_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
373         WM_keymap_add_item(keymap, "FILE_OT_select_border", BKEY, KM_PRESS, 0, 0);
374         WM_keymap_add_item(keymap, "FILE_OT_rename", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
375         WM_keymap_add_item(keymap, "FILE_OT_highlight", MOUSEMOVE, KM_ANY, 0, 0);
376         WM_keymap_add_item(keymap, "FILE_OT_loadimages", TIMER1, KM_ANY, KM_ANY, 0);
377         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, 0, 0);
378         RNA_int_set(kmi->ptr, "increment", 1);
379         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, KM_SHIFT, 0);
380         RNA_int_set(kmi->ptr, "increment", 10);
381         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
382         RNA_int_set(kmi->ptr, "increment", 100);
383         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, 0,0);
384         RNA_int_set(kmi->ptr, "increment", -1);
385         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, KM_SHIFT, 0);
386         RNA_int_set(kmi->ptr, "increment", -10);
387         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, KM_CTRL, 0);
388         RNA_int_set(kmi->ptr, "increment",-100);
389         
390         /* keys for button area (top) */
391         keymap= WM_keymap_listbase(wm, "FileButtons", SPACE_FILE, 0);
392         WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, 0, 0);
393         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, 0, 0);
394         RNA_int_set(kmi->ptr, "increment", 1);
395         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, KM_SHIFT, 0);
396         RNA_int_set(kmi->ptr, "increment", 10);
397         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
398         RNA_int_set(kmi->ptr, "increment", 100);
399         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, 0, 0);
400         RNA_int_set(kmi->ptr, "increment", -1);
401         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, KM_SHIFT,0);
402         RNA_int_set(kmi->ptr, "increment", -10);
403         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, KM_CTRL,0);
404         RNA_int_set(kmi->ptr, "increment",-100);
405 }
406
407
408 static void file_channel_area_init(wmWindowManager *wm, ARegion *ar)
409 {
410         ListBase *keymap;
411
412         ED_region_panels_init(wm, ar);
413
414         /* own keymaps */
415         keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);  
416         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
417 }
418
419 static void file_channel_area_draw(const bContext *C, ARegion *ar)
420 {
421         ED_region_panels(C, ar, 1, NULL);
422 }
423
424 static void file_channel_area_listener(ARegion *ar, wmNotifier *wmn)
425 {
426         /* context changes */
427         switch(wmn->category) {
428                 
429         }
430 }
431
432 /* add handlers, stuff you only do once or on area/region changes */
433 static void file_header_area_init(wmWindowManager *wm, ARegion *ar)
434 {
435         ED_region_header_init(ar);
436 }
437
438 static void file_header_area_draw(const bContext *C, ARegion *ar)
439 {
440         ED_region_header(C, ar);
441 }
442
443 /* add handlers, stuff you only do once or on area/region changes */
444 static void file_ui_area_init(wmWindowManager *wm, ARegion *ar)
445 {
446         ListBase *keymap;
447
448         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
449
450         /* own keymap */
451         keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);  /* XXX weak? */
452         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
453
454         keymap= WM_keymap_listbase(wm, "FileButtons", SPACE_FILE, 0);   /* XXX weak? */
455         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
456 }
457
458 static void file_ui_area_draw(const bContext *C, ARegion *ar)
459 {
460         float col[3];
461         /* clear */
462         UI_GetThemeColor3fv(TH_PANEL, col);
463         glClearColor(col[0], col[1], col[2], 0.0);
464         glClear(GL_COLOR_BUFFER_BIT);
465
466         /* set view2d view matrix for scrolling (without scrollers) */
467         UI_view2d_view_ortho(C, &ar->v2d);
468
469         file_draw_buttons(C, ar);
470
471         UI_view2d_view_restore(C);
472 }
473
474 static void file_ui_area_listener(ARegion *ar, wmNotifier *wmn)
475 {
476         /* context changes */
477         switch(wmn->category) {
478                 case NC_FILE:
479                         switch (wmn->data) {
480                                 case ND_FILELIST:
481                                         ED_region_tag_redraw(ar);
482                                         break;
483                         }
484                         break;
485         }
486 }
487
488 /* only called once, from space/spacetypes.c */
489 void ED_spacetype_file(void)
490 {
491         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype file");
492         ARegionType *art;
493         
494         st->spaceid= SPACE_FILE;
495         
496         st->new= file_new;
497         st->free= file_free;
498         st->init= file_init;
499         st->duplicate= file_duplicate;
500         st->refresh= file_refresh;
501         st->listener= file_listener;
502         st->operatortypes= file_operatortypes;
503         st->keymap= file_keymap;
504         
505         /* regions: main window */
506         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
507         art->regionid = RGN_TYPE_WINDOW;
508         art->init= file_main_area_init;
509         art->draw= file_main_area_draw;
510         art->listener= file_main_area_listener;
511         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
512         BLI_addhead(&st->regiontypes, art);
513         
514         /* regions: header */
515         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
516         art->regionid = RGN_TYPE_HEADER;
517         art->minsizey= HEADERY;
518         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
519         art->init= file_header_area_init;
520         art->draw= file_header_area_draw;
521         // art->listener= file_header_area_listener;
522         BLI_addhead(&st->regiontypes, art);
523         
524         /* regions: ui */
525         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
526         art->regionid = RGN_TYPE_UI;
527         art->minsizey= 60;
528         art->keymapflag= ED_KEYMAP_UI;
529         art->listener= file_ui_area_listener;
530         art->init= file_ui_area_init;
531         art->draw= file_ui_area_draw;
532         BLI_addhead(&st->regiontypes, art);
533
534         /* regions: channels (directories) */
535         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
536         art->regionid = RGN_TYPE_CHANNELS;
537         art->minsizex= 240;
538         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
539         art->listener= file_channel_area_listener;
540         art->init= file_channel_area_init;
541         art->draw= file_channel_area_draw;
542         BLI_addhead(&st->regiontypes, art);
543         file_panels_register(art);
544
545         BKE_spacetype_register(st);
546
547 }
548
549 void ED_file_init(void)
550 {
551         char name[FILE_MAX];
552         BLI_make_file_string("/", name, BLI_gethome(), ".Bfs");
553         fsmenu_read_file(fsmenu_get(), name);
554         filelist_init_icons();
555         IMB_thumb_makedirs();
556 }
557
558 void ED_file_exit(void)
559 {
560         fsmenu_free(fsmenu_get());
561         filelist_free_icons();
562 }