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