2.5 filebrowser
[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 "MEM_guardedalloc.h"
38
39 #include "BIF_gl.h"
40
41 #include "BLO_readfile.h"
42
43 #include "BLI_blenlib.h"
44 #include "BLI_arithb.h"
45 #include "BLI_rand.h"
46
47 #include "BKE_colortools.h"
48 #include "BKE_context.h"
49 #include "BKE_screen.h"
50
51 #include "ED_space_api.h"
52 #include "ED_screen.h"
53 #include "ED_fileselect.h"
54
55 #include "IMB_imbuf_types.h"
56 #include "IMB_thumbs.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "UI_interface.h"
62 #include "UI_resources.h"
63 #include "UI_view2d.h"
64
65 #include "ED_markers.h"
66 #include "ED_fileselect.h"
67
68 #include "file_intern.h"        // own include
69 #include "fsmenu.h"
70 #include "filelist.h"
71
72 /* ******************** default callbacks for file space ***************** */
73
74 static SpaceLink *file_new(const bContext *C)
75 {
76         ARegion *ar;
77         SpaceFile *sfile;
78         
79         sfile= MEM_callocN(sizeof(SpaceFile), "initfile");
80         sfile->spacetype= SPACE_FILE;
81
82         /* header */
83         ar= MEM_callocN(sizeof(ARegion), "header for file");
84         BLI_addtail(&sfile->regionbase, ar);
85         ar->regiontype= RGN_TYPE_HEADER;
86         ar->alignment= RGN_ALIGN_BOTTOM;
87
88         /* channel list region */
89         ar= MEM_callocN(sizeof(ARegion), "channel area for file");
90         BLI_addtail(&sfile->regionbase, ar);
91         ar->regiontype= RGN_TYPE_CHANNELS;
92         ar->alignment= RGN_ALIGN_LEFT;
93
94         /* ui list region */
95         ar= MEM_callocN(sizeof(ARegion), "ui area for file");
96         BLI_addtail(&sfile->regionbase, ar);
97         ar->regiontype= RGN_TYPE_UI;
98         ar->alignment= RGN_ALIGN_TOP;
99
100         /* main area */
101         ar= MEM_callocN(sizeof(ARegion), "main area for file");
102         BLI_addtail(&sfile->regionbase, ar);
103         ar->regiontype= RGN_TYPE_WINDOW;
104         ar->v2d.scroll = (V2D_SCROLL_RIGHT | V2D_SCROLL_BOTTOM);
105         ar->v2d.align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
106         ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPZOOM|V2D_KEEPASPECT);
107         ar->v2d.keeptot= V2D_KEEPTOT_STRICT;
108         ar->v2d.minzoom= ar->v2d.maxzoom= 1.0f;
109
110         return (SpaceLink *)sfile;
111 }
112
113 /* not spacelink itself */
114 static void file_free(SpaceLink *sl)
115 {       
116         SpaceFile *sfile= (SpaceFile *) sl;
117         
118         if(sfile->files) {
119                 filelist_free(sfile->files);
120                 filelist_freelib(sfile->files);
121                 MEM_freeN(sfile->files);
122                 sfile->files= NULL;
123         }
124
125         if (sfile->params) {
126                 if(sfile->params->pupmenu)
127                         MEM_freeN(sfile->params->pupmenu);
128                 MEM_freeN(sfile->params);
129                 sfile->params= NULL;
130         }
131 }
132
133
134 /* spacetype; init callback, area size changes, screen set, etc */
135 static void file_init(struct wmWindowManager *wm, ScrArea *sa)
136 {
137 }
138
139
140 static SpaceLink *file_duplicate(SpaceLink *sl)
141 {
142         SpaceFile *sfileo= (SpaceFile*)sl;
143         SpaceFile *sfilen= MEM_dupallocN(sl);
144         
145         /* clear or remove stuff from old */
146         sfilen->op = NULL; /* file window doesn't own operators */
147
148         sfilen->files = filelist_new();
149         
150         if(sfileo->params) {
151                 sfilen->params= MEM_dupallocN(sfileo->params);
152         
153                 filelist_setdir(sfilen->files, sfilen->params->dir);
154                 filelist_settype(sfilen->files, sfilen->params->type);
155         }
156         return (SpaceLink *)sfilen;
157 }
158
159
160 /* add handlers, stuff you only do once or on area/region changes */
161 static void file_main_area_init(wmWindowManager *wm, ARegion *ar)
162 {
163         ListBase *keymap;
164         
165         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
166         
167         /* own keymap */
168         keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);  /* XXX weak? */
169         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
170                                                            
171
172 }
173
174 static void file_main_area_draw(const bContext *C, ARegion *ar)
175 {
176         /* draw entirely, view changes should be handled here */
177         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
178         FileSelectParams* params = sfile->params;
179         View2D *v2d= &ar->v2d;
180         View2DScrollers *scrollers;
181         float col[3];
182         
183         if (!sfile->files) {
184                 sfile->files = filelist_new();
185                 filelist_setdir(sfile->files, params->dir);
186                 filelist_settype(sfile->files, params->type);
187                 params->active_file = -1; // added this so it opens nicer (ton)
188         }
189
190         if (filelist_empty(sfile->files))
191         {
192                 unsigned int filter = 0;
193                 filelist_hidedot(sfile->files, params->flag & FILE_HIDE_DOT);
194                 if (params->flag & FILE_FILTER) {
195                         filter = params->filter ;
196                 } else {
197                         filter = 0;
198                 }
199
200                 filelist_setfilter(sfile->files, filter);
201                 filelist_readdir(sfile->files);
202                 
203                 if(params->sort!=FILE_SORTALPHA) filelist_sort(sfile->files, params->sort);             
204         }
205
206         /* clear and setup matrix */
207         UI_GetThemeColor3fv(TH_BACK, col);
208         glClearColor(col[0], col[1], col[2], 0.0);
209         glClear(GL_COLOR_BUFFER_BIT);
210         
211         /* Allow dynamically sliders to be set, saves notifiers etc. */
212         if (sfile->params && sfile->params->display)
213                 v2d->scroll = V2D_SCROLL_RIGHT;
214         else
215                 v2d->scroll = V2D_SCROLL_BOTTOM;
216                 /* v2d has initialized flag, so this call will only set the mask correct */
217         UI_view2d_region_reinit(v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
218
219         /* sets tile/border settings in sfile */
220         file_calc_previews(C, ar);
221
222         /* set view */
223         UI_view2d_view_ortho(C, v2d);
224         
225         /* on first read, find active file */
226         if (params->active_file == -1) {
227                 wmEvent *event= CTX_wm_window(C)->eventstate;
228                 file_hilight_set(sfile, ar, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin);
229         }
230         
231         if (params->display) {
232                 file_draw_previews(C, ar);
233         } else {
234                 file_draw_list(C, ar);
235         }
236         
237         
238         /* reset view matrix */
239         UI_view2d_view_restore(C);
240         
241         /* scrollers */
242         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
243         UI_view2d_scrollers_draw(C, v2d, scrollers);
244         UI_view2d_scrollers_free(scrollers);
245
246 }
247
248 void file_operatortypes(void)
249 {
250         WM_operatortype_append(FILE_OT_select);
251         WM_operatortype_append(FILE_OT_select_all);
252         WM_operatortype_append(FILE_OT_border_select);
253         WM_operatortype_append(FILE_OT_select_bookmark);
254         WM_operatortype_append(FILE_OT_loadimages);
255         WM_operatortype_append(FILE_OT_highlight);
256         WM_operatortype_append(FILE_OT_exec);
257         WM_operatortype_append(FILE_OT_cancel);
258         WM_operatortype_append(FILE_OT_parent);
259 }
260
261 /* NOTE: do not add .blend file reading on this level */
262 void file_keymap(struct wmWindowManager *wm)
263 {
264         ListBase *keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);
265         WM_keymap_add_item(keymap, "FILE_OT_select", LEFTMOUSE, KM_PRESS, 0, 0);
266         WM_keymap_add_item(keymap, "FILE_OT_select_all", AKEY, KM_PRESS, 0, 0);
267         WM_keymap_add_item(keymap, "FILE_OT_border_select", BKEY, KM_PRESS, 0, 0);
268         WM_keymap_add_item(keymap, "FILE_OT_highlight", MOUSEMOVE, KM_ANY, 0, 0);
269         WM_keymap_add_item(keymap, "FILE_OT_parent", PKEY, KM_PRESS, 0, 0);
270         
271         WM_keymap_add_item(keymap, "FILE_OT_loadimages", TIMER1, KM_ANY, KM_ANY, 0);
272
273         keymap= WM_keymap_listbase(wm, "FileBookmark", SPACE_FILE, 0);
274         WM_keymap_add_item(keymap, "FILE_OT_select_bookmark", LEFTMOUSE, KM_PRESS, 0, 0);
275 }
276
277
278 static void file_channel_area_init(wmWindowManager *wm, ARegion *ar)
279 {
280         ListBase *keymap;
281
282         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
283
284         /* own keymap */
285         keymap= WM_keymap_listbase(wm, "FileBookmark", SPACE_FILE, 0);
286         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, NULL, NULL);
287 }
288
289 static void file_channel_area_draw(const bContext *C, ARegion *ar)
290 {
291         View2D *v2d= &ar->v2d;
292         float col[3];
293
294         UI_GetThemeColor3fv(TH_BACK, col);
295         glClearColor(col[0], col[1], col[2], 0.0);
296         glClear(GL_COLOR_BUFFER_BIT);
297
298         /* data... */
299         UI_view2d_view_ortho(C, v2d);
300
301         file_draw_fsmenu(C, ar);
302 }
303
304 /* add handlers, stuff you only do once or on area/region changes */
305 static void file_header_area_init(wmWindowManager *wm, ARegion *ar)
306 {
307         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
308 }
309
310 static void file_header_area_draw(const bContext *C, ARegion *ar)
311 {
312         float col[3];
313         
314         /* clear */
315         if(ED_screen_area_active(C))
316                 UI_GetThemeColor3fv(TH_HEADER, col);
317         else
318                 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
319         
320         glClearColor(col[0], col[1], col[2], 0.0);
321         glClear(GL_COLOR_BUFFER_BIT);
322         
323         /* set view2d view matrix for scrolling (without scrollers) */
324         UI_view2d_view_ortho(C, &ar->v2d);
325         
326         file_header_buttons(C, ar);
327         
328         /* restore view matrix? */
329         UI_view2d_view_restore(C);
330 }
331
332 /* add handlers, stuff you only do once or on area/region changes */
333 static void file_ui_area_init(wmWindowManager *wm, ARegion *ar)
334 {
335         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
336 }
337
338 static void file_ui_area_draw(const bContext *C, ARegion *ar)
339 {
340         float col[3];
341         /* clear */
342         UI_GetThemeColor3fv(TH_BACK, col);
343         glClearColor(col[0], col[1], col[2], 0.0);
344         glClear(GL_COLOR_BUFFER_BIT);
345
346         /* set view2d view matrix for scrolling (without scrollers) */
347         UI_view2d_view_ortho(C, &ar->v2d);
348
349         file_draw_buttons(C, ar);
350
351         UI_view2d_view_restore(C);
352 }
353
354 //static void file_main_area_listener(ARegion *ar, wmNotifier *wmn)
355 //{
356         /* context changes */
357 //}
358
359 /* only called once, from space/spacetypes.c */
360 void ED_spacetype_file(void)
361 {
362         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype file");
363         ARegionType *art;
364         
365         st->spaceid= SPACE_FILE;
366         
367         st->new= file_new;
368         st->free= file_free;
369         st->init= file_init;
370         st->duplicate= file_duplicate;
371         st->operatortypes= file_operatortypes;
372         st->keymap= file_keymap;
373         
374         /* regions: main window */
375         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
376         art->regionid = RGN_TYPE_WINDOW;
377         art->init= file_main_area_init;
378         art->draw= file_main_area_draw;
379         // art->listener= file_main_area_listener;
380         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
381         BLI_addhead(&st->regiontypes, art);
382         
383         /* regions: header */
384         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
385         art->regionid = RGN_TYPE_HEADER;
386         art->minsizey= HEADERY;
387         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
388         art->init= file_header_area_init;
389         art->draw= file_header_area_draw;
390         BLI_addhead(&st->regiontypes, art);
391         
392         /* regions: ui */
393         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
394         art->regionid = RGN_TYPE_UI;
395         art->minsizey= 100;
396         art->keymapflag= ED_KEYMAP_UI;
397         art->init= file_ui_area_init;
398         art->draw= file_ui_area_draw;
399         BLI_addhead(&st->regiontypes, art);
400
401         /* regions: channels (directories) */
402         art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
403         art->regionid = RGN_TYPE_CHANNELS;
404         art->minsizex= 200;
405         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
406         art->init= file_channel_area_init;
407         art->draw= file_channel_area_draw;
408         BLI_addhead(&st->regiontypes, art);
409         
410         BKE_spacetype_register(st);
411
412 }
413
414 void ED_file_init(void)
415 {
416         char name[FILE_MAX];
417         BLI_make_file_string("/", name, BLI_gethome(), ".Bfs");
418         fsmenu_read_file(name);
419         filelist_init_icons();
420         IMB_thumb_makedirs();
421 }
422
423 void ED_file_exit(void)
424 {
425         fsmenu_free();
426         filelist_free_icons();
427 }