4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2008 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
32 #include "DNA_object_types.h"
33 #include "DNA_space_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
37 #include "MEM_guardedalloc.h"
41 #include "BLO_readfile.h"
43 #include "BLI_blenlib.h"
44 #include "BLI_arithb.h"
47 #include "BKE_colortools.h"
48 #include "BKE_context.h"
49 #include "BKE_screen.h"
51 #include "ED_space_api.h"
52 #include "ED_screen.h"
53 #include "ED_fileselect.h"
55 #include "IMB_imbuf_types.h"
56 #include "IMB_thumbs.h"
61 #include "UI_interface.h"
62 #include "UI_resources.h"
63 #include "UI_view2d.h"
66 #include "ED_markers.h"
67 #include "ED_fileselect.h"
69 #include "file_intern.h" // own include
73 /* ******************** default callbacks for file space ***************** */
75 static SpaceLink *file_new(const bContext *C)
80 sfile= MEM_callocN(sizeof(SpaceFile), "initfile");
81 sfile->spacetype= SPACE_FILE;
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;
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;
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;
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;
111 return (SpaceLink *)sfile;
114 /* not spacelink itself */
115 static void file_free(SpaceLink *sl)
117 SpaceFile *sfile= (SpaceFile *) sl;
120 filelist_free(sfile->files);
121 MEM_freeN(sfile->files);
126 if(sfile->params->pupmenu)
127 MEM_freeN(sfile->params->pupmenu);
128 MEM_freeN(sfile->params);
133 MEM_freeN(sfile->layout);
134 sfile->layout = NULL;
139 /* spacetype; init callback, area size changes, screen set, etc */
140 static void file_init(struct wmWindowManager *wm, ScrArea *sa)
145 static SpaceLink *file_duplicate(SpaceLink *sl)
147 SpaceFile *sfileo= (SpaceFile*)sl;
148 SpaceFile *sfilen= MEM_dupallocN(sl);
150 /* clear or remove stuff from old */
151 sfilen->op = NULL; /* file window doesn't own operators */
153 sfilen->files = filelist_new();
156 sfilen->params= MEM_dupallocN(sfileo->params);
158 filelist_setdir(sfilen->files, sfilen->params->dir);
159 filelist_settype(sfilen->files, sfilen->params->type);
161 if (sfileo->layout) {
162 sfilen->layout= MEM_dupallocN(sfileo->layout);
164 return (SpaceLink *)sfilen;
168 /* add handlers, stuff you only do once or on area/region changes */
169 static void file_main_area_init(wmWindowManager *wm, ARegion *ar)
173 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
176 keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0); /* XXX weak? */
177 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
182 static void file_main_area_draw(const bContext *C, ARegion *ar)
184 /* draw entirely, view changes should be handled here */
185 SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
186 FileSelectParams* params = sfile->params;
187 View2D *v2d= &ar->v2d;
188 View2DScrollers *scrollers;
192 sfile->files = filelist_new();
193 filelist_setdir(sfile->files, params->dir);
194 filelist_settype(sfile->files, params->type);
195 params->active_file = -1; // added this so it opens nicer (ton)
198 if (filelist_empty(sfile->files))
200 unsigned int filter = 0;
201 filelist_hidedot(sfile->files, params->flag & FILE_HIDE_DOT);
202 if (params->flag & FILE_FILTER) {
203 filter = params->filter ;
208 filelist_setfilter(sfile->files, filter);
209 filelist_readdir(sfile->files);
211 if(params->sort!=FILE_SORTALPHA) filelist_sort(sfile->files, params->sort);
214 /* clear and setup matrix */
215 UI_GetThemeColor3fv(TH_BACK, col);
216 glClearColor(col[0], col[1], col[2], 0.0);
217 glClear(GL_COLOR_BUFFER_BIT);
219 /* Allow dynamically sliders to be set, saves notifiers etc. */
220 if (sfile->params && ( (sfile->params->display == FILE_IMGDISPLAY) || (sfile->params->display == FILE_LONGDISPLAY)) ) {
221 v2d->scroll = V2D_SCROLL_RIGHT;
222 v2d->keepofs &= ~V2D_LOCKOFS_Y;
223 v2d->keepofs |= V2D_LOCKOFS_X;
226 v2d->scroll = V2D_SCROLL_BOTTOM;
227 v2d->keepofs &= ~V2D_LOCKOFS_X;
228 v2d->keepofs |= V2D_LOCKOFS_Y;
230 /* v2d has initialized flag, so this call will only set the mask correct */
231 UI_view2d_region_reinit(v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
233 /* sets tile/border settings in sfile */
234 file_calc_previews(C, ar);
237 UI_view2d_view_ortho(C, v2d);
239 /* on first read, find active file */
240 if (params->active_file == -1) {
241 wmEvent *event= CTX_wm_window(C)->eventstate;
242 file_hilight_set(sfile, ar, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin);
245 if (params->display == FILE_IMGDISPLAY) {
246 file_draw_previews(C, ar);
248 file_draw_list(C, ar);
252 /* reset view matrix */
253 UI_view2d_view_restore(C);
256 scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
257 UI_view2d_scrollers_draw(C, v2d, scrollers);
258 UI_view2d_scrollers_free(scrollers);
262 void file_operatortypes(void)
264 WM_operatortype_append(FILE_OT_select);
265 WM_operatortype_append(FILE_OT_select_all);
266 WM_operatortype_append(FILE_OT_border_select);
267 WM_operatortype_append(FILE_OT_select_bookmark);
268 WM_operatortype_append(FILE_OT_loadimages);
269 WM_operatortype_append(FILE_OT_highlight);
270 WM_operatortype_append(FILE_OT_exec);
271 WM_operatortype_append(FILE_OT_cancel);
272 WM_operatortype_append(FILE_OT_parent);
273 WM_operatortype_append(FILE_OT_bookmark_toggle);
276 /* NOTE: do not add .blend file reading on this level */
277 void file_keymap(struct wmWindowManager *wm)
279 ListBase *keymap= WM_keymap_listbase(wm, "File", SPACE_FILE, 0);
280 WM_keymap_add_item(keymap, "FILE_OT_select", LEFTMOUSE, KM_PRESS, 0, 0);
281 WM_keymap_add_item(keymap, "FILE_OT_select_all", AKEY, KM_PRESS, 0, 0);
282 WM_keymap_add_item(keymap, "FILE_OT_border_select", BKEY, KM_PRESS, 0, 0);
283 WM_keymap_add_item(keymap, "FILE_OT_highlight", MOUSEMOVE, KM_ANY, 0, 0);
284 WM_keymap_add_item(keymap, "FILE_OT_parent", PKEY, KM_PRESS, 0, 0);
286 WM_keymap_add_item(keymap, "FILE_OT_loadimages", TIMER1, KM_ANY, KM_ANY, 0);
288 keymap= WM_keymap_listbase(wm, "FileBookmark", SPACE_FILE, 0);
289 WM_keymap_add_item(keymap, "FILE_OT_select_bookmark", LEFTMOUSE, KM_PRESS, 0, 0);
293 static void file_channel_area_init(wmWindowManager *wm, ARegion *ar)
297 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
300 keymap= WM_keymap_listbase(wm, "FileBookmark", SPACE_FILE, 0);
301 WM_event_add_keymap_handler_bb(&ar->handlers, keymap, NULL, NULL);
304 static void file_channel_area_draw(const bContext *C, ARegion *ar)
306 View2D *v2d= &ar->v2d;
309 UI_GetThemeColor3fv(TH_PANEL, col);
310 glClearColor(col[0], col[1], col[2], 0.0);
311 glClear(GL_COLOR_BUFFER_BIT);
314 UI_view2d_view_ortho(C, v2d);
316 file_draw_fsmenu(C, ar);
319 /* add handlers, stuff you only do once or on area/region changes */
320 static void file_header_area_init(wmWindowManager *wm, ARegion *ar)
322 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
325 static void file_header_area_draw(const bContext *C, ARegion *ar)
330 if(ED_screen_area_active(C))
331 UI_GetThemeColor3fv(TH_HEADER, col);
333 UI_GetThemeColor3fv(TH_HEADERDESEL, col);
335 glClearColor(col[0], col[1], col[2], 0.0);
336 glClear(GL_COLOR_BUFFER_BIT);
338 /* set view2d view matrix for scrolling (without scrollers) */
339 UI_view2d_view_ortho(C, &ar->v2d);
341 file_header_buttons(C, ar);
343 /* restore view matrix? */
344 UI_view2d_view_restore(C);
347 /* add handlers, stuff you only do once or on area/region changes */
348 static void file_ui_area_init(wmWindowManager *wm, ARegion *ar)
350 UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_HEADER, ar->winx, ar->winy);
353 static void file_ui_area_draw(const bContext *C, ARegion *ar)
357 UI_GetThemeColor3fv(TH_PANEL, col);
358 glClearColor(col[0], col[1], col[2], 0.0);
359 glClear(GL_COLOR_BUFFER_BIT);
361 /* set view2d view matrix for scrolling (without scrollers) */
362 UI_view2d_view_ortho(C, &ar->v2d);
364 file_draw_buttons(C, ar);
366 UI_view2d_view_restore(C);
369 //static void file_main_area_listener(ARegion *ar, wmNotifier *wmn)
371 /* context changes */
374 /* only called once, from space/spacetypes.c */
375 void ED_spacetype_file(void)
377 SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype file");
380 st->spaceid= SPACE_FILE;
385 st->duplicate= file_duplicate;
386 st->operatortypes= file_operatortypes;
387 st->keymap= file_keymap;
389 /* regions: main window */
390 art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
391 art->regionid = RGN_TYPE_WINDOW;
392 art->init= file_main_area_init;
393 art->draw= file_main_area_draw;
394 // art->listener= file_main_area_listener;
395 art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
396 BLI_addhead(&st->regiontypes, art);
398 /* regions: header */
399 art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
400 art->regionid = RGN_TYPE_HEADER;
401 art->minsizey= HEADERY;
402 art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
403 art->init= file_header_area_init;
404 art->draw= file_header_area_draw;
405 BLI_addhead(&st->regiontypes, art);
408 art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
409 art->regionid = RGN_TYPE_UI;
411 art->keymapflag= ED_KEYMAP_UI;
412 art->init= file_ui_area_init;
413 art->draw= file_ui_area_draw;
414 BLI_addhead(&st->regiontypes, art);
416 /* regions: channels (directories) */
417 art= MEM_callocN(sizeof(ARegionType), "spacetype file region");
418 art->regionid = RGN_TYPE_CHANNELS;
420 art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
421 art->init= file_channel_area_init;
422 art->draw= file_channel_area_draw;
423 BLI_addhead(&st->regiontypes, art);
425 BKE_spacetype_register(st);
429 void ED_file_init(void)
432 BLI_make_file_string("/", name, BLI_gethome(), ".Bfs");
433 fsmenu_read_file(name);
434 filelist_init_icons();
435 IMB_thumb_makedirs();
438 void ED_file_exit(void)
441 filelist_free_icons();