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