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