patch [#34103]
[blender.git] / source / blender / editors / space_file / space_file.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_file/space_file.c
28  *  \ingroup spfile
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35
36 #include "RNA_access.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BIF_gl.h"
41
42 #include "BLO_readfile.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_math.h"
46 #include "BLI_rand.h"
47 #include "BLI_utildefines.h"
48
49 #include "BKE_context.h"
50 #include "BKE_screen.h"
51 #include "BKE_global.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_resources.h"
64 #include "UI_view2d.h"
65
66
67 #include "file_intern.h"    // own include
68 #include "fsmenu.h"
69 #include "filelist.h"
70
71 /* ******************** default callbacks for file space ***************** */
72
73 static SpaceLink *file_new(const bContext *UNUSED(C))
74 {
75         ARegion *ar;
76         SpaceFile *sfile;
77         
78         sfile = MEM_callocN(sizeof(SpaceFile), "initfile");
79         sfile->spacetype = SPACE_FILE;
80
81         /* header */
82         ar = MEM_callocN(sizeof(ARegion), "header for file");
83         BLI_addtail(&sfile->regionbase, ar);
84         ar->regiontype = RGN_TYPE_HEADER;
85         ar->alignment = RGN_ALIGN_TOP;
86
87         /* channel list region */
88         ar = MEM_callocN(sizeof(ARegion), "channel area for file");
89         BLI_addtail(&sfile->regionbase, ar);
90         ar->regiontype = RGN_TYPE_CHANNELS;
91         ar->alignment = RGN_ALIGN_LEFT;
92
93         /* ui list region */
94         ar = MEM_callocN(sizeof(ARegion), "ui area for file");
95         BLI_addtail(&sfile->regionbase, ar);
96         ar->regiontype = RGN_TYPE_UI;
97         ar->alignment = RGN_ALIGN_TOP;
98
99         /* main area */
100         ar = MEM_callocN(sizeof(ARegion), "main area for file");
101         BLI_addtail(&sfile->regionbase, ar);
102         ar->regiontype = RGN_TYPE_WINDOW;
103         ar->v2d.scroll = (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
104         ar->v2d.align = (V2D_ALIGN_NO_NEG_X | V2D_ALIGN_NO_POS_Y);
105         ar->v2d.keepzoom = (V2D_LOCKZOOM_X | V2D_LOCKZOOM_Y | V2D_LIMITZOOM | V2D_KEEPASPECT);
106         ar->v2d.keeptot = V2D_KEEPTOT_STRICT;
107         ar->v2d.minzoom = ar->v2d.maxzoom = 1.0f;
108
109         return (SpaceLink *)sfile;
110 }
111
112 /* not spacelink itself */
113 static void file_free(SpaceLink *sl)
114 {       
115         SpaceFile *sfile = (SpaceFile *) sl;
116         
117         if (sfile->files) {
118                 // XXXXX would need to do thumbnails_stop here, but no context available
119                 filelist_freelib(sfile->files);
120                 filelist_free(sfile->files);
121                 MEM_freeN(sfile->files);
122                 sfile->files = NULL;
123         }
124
125         if (sfile->folders_prev) {
126                 folderlist_free(sfile->folders_prev);
127                 MEM_freeN(sfile->folders_prev);
128                 sfile->folders_prev = NULL;
129         }
130
131         if (sfile->folders_next) {
132                 folderlist_free(sfile->folders_next);
133                 MEM_freeN(sfile->folders_next);
134                 sfile->folders_next = NULL;
135         }
136
137         if (sfile->params) {
138                 MEM_freeN(sfile->params);
139                 sfile->params = NULL;
140         }
141
142         if (sfile->layout) {
143                 MEM_freeN(sfile->layout);
144                 sfile->layout = NULL;
145         }
146 }
147
148
149 /* spacetype; init callback, area size changes, screen set, etc */
150 static void file_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
151 {
152         SpaceFile *sfile = (SpaceFile *)sa->spacedata.first;
153         //printf("file_init\n");
154
155         /* refresh system directory list */
156         fsmenu_refresh_system_category(fsmenu_get());
157
158         if (sfile->layout) sfile->layout->dirty = TRUE;
159 }
160
161
162 static SpaceLink *file_duplicate(SpaceLink *sl)
163 {
164         SpaceFile *sfileo = (SpaceFile *)sl;
165         SpaceFile *sfilen = MEM_dupallocN(sl);
166         
167         /* clear or remove stuff from old */
168         sfilen->op = NULL; /* file window doesn't own operators */
169
170         if (sfileo->params) {
171                 sfilen->files = filelist_new(sfileo->params->type);
172                 sfilen->params = MEM_dupallocN(sfileo->params);
173                 filelist_setdir(sfilen->files, sfilen->params->dir);
174         }
175
176         if (sfileo->folders_prev)
177                 sfilen->folders_prev = folderlist_duplicate(sfileo->folders_prev);
178
179         if (sfileo->folders_next)
180                 sfilen->folders_next = folderlist_duplicate(sfileo->folders_next);
181         
182         if (sfileo->layout) {
183                 sfilen->layout = MEM_dupallocN(sfileo->layout);
184         }
185         return (SpaceLink *)sfilen;
186 }
187
188 static void file_refresh(const bContext *C, ScrArea *UNUSED(sa))
189 {
190         SpaceFile *sfile = CTX_wm_space_file(C);
191         FileSelectParams *params = ED_fileselect_get_params(sfile);
192
193         if (!sfile->folders_prev)
194                 sfile->folders_prev = folderlist_new();
195         if (!sfile->files) {
196                 sfile->files = filelist_new(params->type);
197                 filelist_setdir(sfile->files, params->dir);
198                 params->active_file = -1; // added this so it opens nicer (ton)
199         }
200         filelist_hidedot(sfile->files, params->flag & FILE_HIDE_DOT);
201         filelist_setfilter(sfile->files, params->flag & FILE_FILTER ? params->filter : 0);
202         filelist_setfilter_types(sfile->files, params->filter_glob);
203
204         if (filelist_empty(sfile->files)) {
205                 thumbnails_stop(sfile->files, C);
206                 filelist_readdir(sfile->files);
207                 if (params->sort != FILE_SORT_NONE) {
208                         filelist_sort(sfile->files, params->sort);
209                 }
210                 BLI_strncpy(params->dir, filelist_dir(sfile->files), FILE_MAX);
211                 if (params->display == FILE_IMGDISPLAY) {
212                         thumbnails_start(sfile->files, C);
213                 }
214         }
215         else {
216                 if (params->sort != FILE_SORT_NONE) {
217                         thumbnails_stop(sfile->files, C);
218                         filelist_sort(sfile->files, params->sort);
219                         if (params->display == FILE_IMGDISPLAY) {
220                                 thumbnails_start(sfile->files, C);
221                         }
222                 }
223                 else {
224                         if (params->display == FILE_IMGDISPLAY) {
225                                 if (!thumbnails_running(sfile->files, C)) {
226                                         thumbnails_start(sfile->files, C);
227                                 }
228                         }
229                         else {
230                                 /* stop any running thumbnail jobs if we're not 
231                                  * displaying them - speedup for NFS */
232                                 thumbnails_stop(sfile->files, C);
233                         }
234                         filelist_filter(sfile->files);
235                 }
236         }
237         
238         if (params->renamefile[0] != '\0') {
239                 int idx = filelist_find(sfile->files, params->renamefile);
240                 if (idx >= 0) {
241                         struct direntry *file = filelist_file(sfile->files, idx);
242                         if (file) {
243                                 file->selflag |= EDITING_FILE;
244                         }
245                 }
246                 BLI_strncpy(sfile->params->renameedit, sfile->params->renamefile, sizeof(sfile->params->renameedit));
247                 params->renamefile[0] = '\0';
248         }
249         if (sfile->layout) sfile->layout->dirty = TRUE;
250
251 }
252
253 static void file_listener(ScrArea *sa, wmNotifier *wmn)
254 {
255         /* SpaceFile *sfile = (SpaceFile *)sa->spacedata.first; */
256
257         /* context changes */
258         switch (wmn->category) {
259                 case NC_SPACE:
260                         switch (wmn->data) {
261                                 case ND_SPACE_FILE_LIST:
262                                         ED_area_tag_refresh(sa);
263                                         ED_area_tag_redraw(sa);
264                                         break;
265                                 case ND_SPACE_FILE_PARAMS:
266                                         ED_area_tag_refresh(sa);
267                                         ED_area_tag_redraw(sa);
268                                         break;
269                         }
270                         break;
271         }
272 }
273
274 /* add handlers, stuff you only do once or on area/region changes */
275 static void file_main_area_init(wmWindowManager *wm, ARegion *ar)
276 {
277         wmKeyMap *keymap;
278         
279         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
280         
281         /* own keymaps */
282         keymap = WM_keymap_find(wm->defaultconf, "File Browser", SPACE_FILE, 0);
283         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
284
285         keymap = WM_keymap_find(wm->defaultconf, "File Browser Main", SPACE_FILE, 0);
286         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
287 }
288
289 static void file_main_area_listener(ARegion *ar, wmNotifier *wmn)
290 {
291         /* context changes */
292         switch (wmn->category) {
293                 case NC_SPACE:
294                         switch (wmn->data) {
295                                 case ND_SPACE_FILE_LIST:
296                                         ED_region_tag_redraw(ar);
297                                         break;
298                                 case ND_SPACE_FILE_PARAMS:
299                                         ED_region_tag_redraw(ar);
300                                         break;
301                         }
302                         break;
303         }
304 }
305
306 static void file_main_area_draw(const bContext *C, ARegion *ar)
307 {
308         /* draw entirely, view changes should be handled here */
309         SpaceFile *sfile = CTX_wm_space_file(C);
310         FileSelectParams *params = ED_fileselect_get_params(sfile);
311         FileLayout *layout = NULL;
312
313         View2D *v2d = &ar->v2d;
314         View2DScrollers *scrollers;
315         float col[3];
316
317         /* Needed, because filelist is not initialized on loading */
318         if (!sfile->files || filelist_empty(sfile->files))
319                 file_refresh(C, NULL);
320
321         layout = ED_fileselect_get_layout(sfile, ar);
322
323         /* clear and setup matrix */
324         UI_GetThemeColor3fv(TH_BACK, col);
325         glClearColor(col[0], col[1], col[2], 0.0);
326         glClear(GL_COLOR_BUFFER_BIT);
327         
328         /* Allow dynamically sliders to be set, saves notifiers etc. */
329         if (layout && (layout->flag == FILE_LAYOUT_VER)) {
330                 v2d->scroll = V2D_SCROLL_RIGHT;
331                 v2d->keepofs &= ~V2D_LOCKOFS_Y;
332                 v2d->keepofs |= V2D_LOCKOFS_X;
333         }
334         else {
335                 v2d->scroll = V2D_SCROLL_BOTTOM;
336                 v2d->keepofs &= ~V2D_LOCKOFS_X;
337                 v2d->keepofs |= V2D_LOCKOFS_Y;
338                 
339                 /* XXX this happens on scaling down Screen (like from startup.blend) */
340                 /* view2d has no type specific for filewindow case, which doesnt scroll vertically */
341                 if (v2d->cur.ymax < 0) {
342                         v2d->cur.ymin -= v2d->cur.ymax;
343                         v2d->cur.ymax = 0;
344                 }
345         }
346         /* v2d has initialized flag, so this call will only set the mask correct */
347         UI_view2d_region_reinit(v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
348
349         /* sets tile/border settings in sfile */
350         file_calc_previews(C, ar);
351
352         /* set view */
353         UI_view2d_view_ortho(v2d);
354         
355         /* on first read, find active file */
356         if (params->active_file == -1) {
357                 wmEvent *event = CTX_wm_window(C)->eventstate;
358                 file_highlight_set(sfile, ar, event->x, event->y);
359         }
360         
361         file_draw_list(C, ar);
362         
363         /* reset view matrix */
364         UI_view2d_view_restore(C);
365         
366         /* scrollers */
367         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
368         UI_view2d_scrollers_draw(C, v2d, scrollers);
369         UI_view2d_scrollers_free(scrollers);
370
371 }
372
373 static void file_operatortypes(void)
374 {
375         WM_operatortype_append(FILE_OT_select);
376         WM_operatortype_append(FILE_OT_select_all_toggle);
377         WM_operatortype_append(FILE_OT_select_border);
378         WM_operatortype_append(FILE_OT_select_bookmark);
379         WM_operatortype_append(FILE_OT_highlight);
380         WM_operatortype_append(FILE_OT_execute);
381         WM_operatortype_append(FILE_OT_cancel);
382         WM_operatortype_append(FILE_OT_parent);
383         WM_operatortype_append(FILE_OT_previous);
384         WM_operatortype_append(FILE_OT_next);
385         WM_operatortype_append(FILE_OT_refresh);
386         WM_operatortype_append(FILE_OT_bookmark_toggle);
387         WM_operatortype_append(FILE_OT_bookmark_add);
388         WM_operatortype_append(FILE_OT_delete_bookmark);
389         WM_operatortype_append(FILE_OT_reset_recent);
390         WM_operatortype_append(FILE_OT_hidedot);
391         WM_operatortype_append(FILE_OT_filenum);
392         WM_operatortype_append(FILE_OT_directory_new);
393         WM_operatortype_append(FILE_OT_delete);
394         WM_operatortype_append(FILE_OT_rename);
395         WM_operatortype_append(FILE_OT_smoothscroll);
396         WM_operatortype_append(FILE_OT_directory);
397 }
398
399 /* NOTE: do not add .blend file reading on this level */
400 static void file_keymap(struct wmKeyConfig *keyconf)
401 {
402         wmKeyMapItem *kmi;
403         /* keys for all areas */
404         wmKeyMap *keymap = WM_keymap_find(keyconf, "File Browser", SPACE_FILE, 0);
405         WM_keymap_add_item(keymap, "FILE_OT_bookmark_toggle", NKEY, KM_PRESS, 0, 0);
406         WM_keymap_add_item(keymap, "FILE_OT_parent", PKEY, KM_PRESS, 0, 0);
407         WM_keymap_add_item(keymap, "FILE_OT_bookmark_add", BKEY, KM_PRESS, KM_CTRL, 0);
408         WM_keymap_add_item(keymap, "FILE_OT_hidedot", HKEY, KM_PRESS, 0, 0);
409         WM_keymap_add_item(keymap, "FILE_OT_previous", BACKSPACEKEY, KM_PRESS, 0, 0);
410         WM_keymap_add_item(keymap, "FILE_OT_next", BACKSPACEKEY, KM_PRESS, KM_SHIFT, 0);
411         WM_keymap_add_item(keymap, "FILE_OT_directory_new", IKEY, KM_PRESS, 0, 0);  /* XXX needs button */
412         WM_keymap_add_item(keymap, "FILE_OT_delete", XKEY, KM_PRESS, 0, 0);
413         WM_keymap_add_item(keymap, "FILE_OT_delete", DELKEY, KM_PRESS, 0, 0);
414         WM_keymap_verify_item(keymap, "FILE_OT_smoothscroll", TIMER1, KM_ANY, KM_ANY, 0);
415
416         /* keys for main area */
417         keymap = WM_keymap_find(keyconf, "File Browser Main", SPACE_FILE, 0);
418         kmi = WM_keymap_add_item(keymap, "FILE_OT_execute", LEFTMOUSE, KM_DBL_CLICK, 0, 0);
419         RNA_boolean_set(kmi->ptr, "need_active", TRUE);
420
421         /* left mouse selects and opens */
422         WM_keymap_add_item(keymap, "FILE_OT_select", LEFTMOUSE, KM_CLICK, 0, 0);
423         kmi = WM_keymap_add_item(keymap, "FILE_OT_select", LEFTMOUSE, KM_CLICK, KM_SHIFT, 0);
424         RNA_boolean_set(kmi->ptr, "extend", TRUE);
425         kmi = WM_keymap_add_item(keymap, "FILE_OT_select", LEFTMOUSE, KM_CLICK, KM_ALT, 0);
426         RNA_boolean_set(kmi->ptr, "extend", TRUE);
427         RNA_boolean_set(kmi->ptr, "fill", TRUE);
428
429         /* right mouse selects without opening */
430         kmi = WM_keymap_add_item(keymap, "FILE_OT_select", RIGHTMOUSE, KM_CLICK, 0, 0);
431         RNA_boolean_set(kmi->ptr, "open", FALSE);
432         kmi = WM_keymap_add_item(keymap, "FILE_OT_select", RIGHTMOUSE, KM_CLICK, KM_SHIFT, 0);
433         RNA_boolean_set(kmi->ptr, "extend", TRUE);
434         RNA_boolean_set(kmi->ptr, "open", FALSE);
435         kmi = WM_keymap_add_item(keymap, "FILE_OT_select", RIGHTMOUSE, KM_CLICK, KM_ALT, 0);
436         RNA_boolean_set(kmi->ptr, "extend", TRUE);
437         RNA_boolean_set(kmi->ptr, "fill", TRUE);
438         RNA_boolean_set(kmi->ptr, "open", FALSE);
439
440         WM_keymap_add_item(keymap, "FILE_OT_select_all_toggle", AKEY, KM_PRESS, 0, 0);
441         WM_keymap_add_item(keymap, "FILE_OT_refresh", PADPERIOD, KM_PRESS, 0, 0);
442         WM_keymap_add_item(keymap, "FILE_OT_select_border", BKEY, KM_PRESS, 0, 0);
443         WM_keymap_add_item(keymap, "FILE_OT_select_border", EVT_TWEAK_L, KM_ANY, 0, 0);
444         WM_keymap_add_item(keymap, "FILE_OT_rename", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
445         WM_keymap_add_item(keymap, "FILE_OT_highlight", MOUSEMOVE, KM_ANY, KM_ANY, 0);
446         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, 0, 0);
447         RNA_int_set(kmi->ptr, "increment", 1);
448         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, KM_SHIFT, 0);
449         RNA_int_set(kmi->ptr, "increment", 10);
450         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
451         RNA_int_set(kmi->ptr, "increment", 100);
452         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, 0, 0);
453         RNA_int_set(kmi->ptr, "increment", -1);
454         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, KM_SHIFT, 0);
455         RNA_int_set(kmi->ptr, "increment", -10);
456         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, KM_CTRL, 0);
457         RNA_int_set(kmi->ptr, "increment", -100);
458         
459         
460         /* keys for button area (top) */
461         keymap = WM_keymap_find(keyconf, "File Browser Buttons", SPACE_FILE, 0);
462         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, 0, 0);
463         RNA_int_set(kmi->ptr, "increment", 1);
464         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, KM_SHIFT, 0);
465         RNA_int_set(kmi->ptr, "increment", 10);
466         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADPLUSKEY, KM_PRESS, KM_CTRL, 0);
467         RNA_int_set(kmi->ptr, "increment", 100);
468         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, 0, 0);
469         RNA_int_set(kmi->ptr, "increment", -1);
470         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, KM_SHIFT, 0);
471         RNA_int_set(kmi->ptr, "increment", -10);
472         kmi = WM_keymap_add_item(keymap, "FILE_OT_filenum", PADMINUS, KM_PRESS, KM_CTRL, 0);
473         RNA_int_set(kmi->ptr, "increment", -100);
474 }
475
476
477 static void file_channel_area_init(wmWindowManager *wm, ARegion *ar)
478 {
479         wmKeyMap *keymap;
480
481         ar->v2d.scroll = V2D_SCROLL_RIGHT | V2D_SCROLL_VERTICAL_HIDE;
482         ED_region_panels_init(wm, ar);
483
484         /* own keymaps */
485         keymap = WM_keymap_find(wm->defaultconf, "File Browser", SPACE_FILE, 0);
486         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
487 }
488
489 static void file_channel_area_draw(const bContext *C, ARegion *ar)
490 {
491         ED_region_panels(C, ar, 1, NULL, -1);
492 }
493
494 static void file_channel_area_listener(ARegion *UNUSED(ar), wmNotifier *wmn)
495 {
496         /* context changes */
497         switch (wmn->category) {
498                 
499         }
500 }
501
502 /* add handlers, stuff you only do once or on area/region changes */
503 static void file_header_area_init(wmWindowManager *wm, ARegion *ar)
504 {
505         wmKeyMap *keymap;
506         
507         ED_region_header_init(ar);
508         
509         keymap = WM_keymap_find(wm->defaultconf, "File Browser", SPACE_FILE, 0);
510         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
511 }
512
513 static void file_header_area_draw(const bContext *C, ARegion *ar)
514 {
515         ED_region_header(C, ar);
516 }
517
518 /* add handlers, stuff you only do once or on area/region changes */
519 static void file_ui_area_init(wmWindowManager *wm, ARegion *ar)
520 {
521         wmKeyMap *keymap;
522
523         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
524
525         /* own keymap */
526         keymap = WM_keymap_find(wm->defaultconf, "File Browser", SPACE_FILE, 0);
527         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
528
529         keymap = WM_keymap_find(wm->defaultconf, "File Browser Buttons", SPACE_FILE, 0);
530         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
531 }
532
533 static void file_ui_area_draw(const bContext *C, ARegion *ar)
534 {
535         float col[3];
536         /* clear */
537         UI_GetThemeColor3fv(TH_BACK, col);
538         glClearColor(col[0], col[1], col[2], 0.0);
539         glClear(GL_COLOR_BUFFER_BIT);
540
541         /* scrolling here is just annoying, disable it */
542         ar->v2d.cur.ymax = BLI_rctf_size_y(&ar->v2d.cur);
543         ar->v2d.cur.ymin = 0;
544
545         /* set view2d view matrix for scrolling (without scrollers) */
546         UI_view2d_view_ortho(&ar->v2d);
547
548
549         file_draw_buttons(C, ar);
550
551         UI_view2d_view_restore(C);
552 }
553
554 static void file_ui_area_listener(ARegion *ar, wmNotifier *wmn)
555 {
556         /* context changes */
557         switch (wmn->category) {
558                 case NC_SPACE:
559                         switch (wmn->data) {
560                                 case ND_SPACE_FILE_LIST:
561                                         ED_region_tag_redraw(ar);
562                                         break;
563                         }
564                         break;
565         }
566 }
567
568 /* only called once, from space/spacetypes.c */
569 void ED_spacetype_file(void)
570 {
571         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype file");
572         ARegionType *art;
573         
574         st->spaceid = SPACE_FILE;
575         strncpy(st->name, "File", BKE_ST_MAXNAME);
576         
577         st->new = file_new;
578         st->free = file_free;
579         st->init = file_init;
580         st->duplicate = file_duplicate;
581         st->refresh = file_refresh;
582         st->listener = file_listener;
583         st->operatortypes = file_operatortypes;
584         st->keymap = file_keymap;
585         
586         /* regions: main window */
587         art = MEM_callocN(sizeof(ARegionType), "spacetype file region");
588         art->regionid = RGN_TYPE_WINDOW;
589         art->init = file_main_area_init;
590         art->draw = file_main_area_draw;
591         art->listener = file_main_area_listener;
592         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
593         BLI_addhead(&st->regiontypes, art);
594         
595         /* regions: header */
596         art = MEM_callocN(sizeof(ARegionType), "spacetype file region");
597         art->regionid = RGN_TYPE_HEADER;
598         art->prefsizey = HEADERY;
599         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_HEADER;
600         art->init = file_header_area_init;
601         art->draw = file_header_area_draw;
602         // art->listener = file_header_area_listener;
603         BLI_addhead(&st->regiontypes, art);
604         
605         /* regions: ui */
606         art = MEM_callocN(sizeof(ARegionType), "spacetype file region");
607         art->regionid = RGN_TYPE_UI;
608         art->prefsizey = 60;
609         art->keymapflag = ED_KEYMAP_UI;
610         art->listener = file_ui_area_listener;
611         art->init = file_ui_area_init;
612         art->draw = file_ui_area_draw;
613         BLI_addhead(&st->regiontypes, art);
614
615         /* regions: channels (directories) */
616         art = MEM_callocN(sizeof(ARegionType), "spacetype file region");
617         art->regionid = RGN_TYPE_CHANNELS;
618         art->prefsizex = 240;
619         art->keymapflag = ED_KEYMAP_UI;
620         art->listener = file_channel_area_listener;
621         art->init = file_channel_area_init;
622         art->draw = file_channel_area_draw;
623         BLI_addhead(&st->regiontypes, art);
624         file_panels_register(art);
625
626         BKE_spacetype_register(st);
627
628 }
629
630 void ED_file_init(void)
631 {
632         const char * const cfgdir = BLI_get_folder(BLENDER_USER_CONFIG, NULL);
633         
634         fsmenu_read_system(fsmenu_get(), TRUE);
635
636         if (cfgdir) {
637                 char name[FILE_MAX];
638                 BLI_make_file_string("/", name, cfgdir, BLENDER_BOOKMARK_FILE);
639                 fsmenu_read_bookmarks(fsmenu_get(), name);
640         }
641         
642         if (G.background == FALSE) {
643                 filelist_init_icons();
644         }
645
646         IMB_thumb_makedirs();
647 }
648
649 void ED_file_exit(void)
650 {
651         fsmenu_free(fsmenu_get());
652
653         if (G.background == FALSE) {
654                 filelist_free_icons();
655         }
656 }