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 *****
31 #include "BLI_blenlib.h"
32 #include "BLI_dynstr.h"
33 #include "BLI_storage_types.h"
35 #include "BLI_winstuff.h"
39 #include "BIF_glutil.h"
41 #include "BKE_colortools.h"
42 #include "BKE_context.h"
43 #include "BKE_screen.h"
44 #include "BKE_global.h"
45 #include "BKE_utildefines.h"
49 #include "DNA_space_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_userdef_types.h"
53 #include "DNA_windowmanager_types.h"
55 #include "ED_datafiles.h"
57 #include "IMB_imbuf_types.h"
58 #include "IMB_imbuf.h"
60 #include "MEM_guardedalloc.h"
64 #include "RNA_access.h"
66 #include "ED_fileselect.h"
67 #include "ED_screen.h"
69 #include "UI_interface.h"
70 #include "UI_interface_icons.h"
71 #include "UI_resources.h"
72 #include "UI_view2d.h"
80 #include "file_intern.h" // own include
83 #define IMASEL_BUTTONS_HEIGHT 40
84 #define TILE_BORDER_X 8
85 #define TILE_BORDER_Y 8
96 static void do_file_buttons(bContext *C, void *arg, int event)
100 file_exec(C, NULL); /* file_ops.c */
103 file_cancel_exec(C, NULL); /* file_ops.c */
106 file_parent_exec(C, NULL); /* file_ops.c */
111 /* note; this function uses pixelspace (0, 0, winx, winy), not view2d */
112 void file_draw_buttons(const bContext *C, ARegion *ar)
114 SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
115 FileSelectParams* params = ED_fileselect_get_params(sfile);
120 int filebuty1, filebuty2;
123 float xmax = ar->winx - 10;
125 filebuty1= ar->winy - IMASEL_BUTTONS_HEIGHT - 12;
126 filebuty2= filebuty1 + IMASEL_BUTTONS_HEIGHT/2 + 4;
129 sprintf(name, "win %p", ar);
130 block = uiBeginBlock(C, ar, name, UI_EMBOSS);
131 uiBlockSetHandleFunc(block, do_file_buttons, NULL);
134 uiSetButLock( filelist_gettype(simasel->files)==FILE_MAIN && simasel->returnfunc, NULL);
137 /* space available for load/save buttons? */
138 slen = UI_GetStringWidth(sfile->params->title);
139 loadbutton= slen > 60 ? slen + 20 : MAX2(80, 20+UI_GetStringWidth(params->title));
140 if(ar->winx > loadbutton+20) {
141 if(params->title[0]==0) {
149 uiDefBut(block, TEX, 0 /* XXX B_FS_FILENAME */,"", xmin+2, filebuty1, xmax-xmin-loadbutton-4, 21, params->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
150 uiDefBut(block, TEX, 0 /* XXX B_FS_DIRNAME */,"", xmin+2, filebuty2, xmax-xmin-loadbutton-4, 21, params->dir, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
153 uiDefBut(block, BUT, B_FS_EXEC, params->title, xmax-loadbutton, filebuty2, loadbutton, 21, params->dir, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
154 uiDefBut(block, BUT, B_FS_CANCEL, "Cancel", xmax-loadbutton, filebuty1, loadbutton, 21, params->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
157 uiEndBlock(C, block);
158 uiDrawBlock(C, block);
162 static void draw_tile(short sx, short sy, short width, short height, int colorid, int shade)
164 /* TODO: BIF_ThemeColor seems to need this to show the color, not sure why? - elubie */
165 //glEnable(GL_BLEND);
166 //glColor4ub(0, 0, 0, 100);
167 //glDisable(GL_BLEND);
168 /* I think it was a missing glDisable() - ton */
170 UI_ThemeColorShade(colorid, shade);
172 uiRoundBox(sx, sy - height, sx + width, sy, 6);
175 #define FILE_SHORTEN_END 0
176 #define FILE_SHORTEN_FRONT 1
178 static float shorten_string(char* string, float w, int flag)
190 sw = file_string_width(string);
191 if (flag == FILE_SHORTEN_FRONT) {
193 BLI_strncpy(temp, "...", 4);
194 pad = file_string_width(temp);
195 while ((*s) && (sw+pad>w)) {
197 sw = file_string_width(s);
201 int slen = strlen(s);
202 BLI_strncpy(temp+3, s, slen+1);
204 BLI_strncpy(string, temp, slen+4);
209 int slen = strlen(string);
210 string[slen-1] = '\0';
211 sw = file_string_width(s);
216 int slen = strlen(string);
218 BLI_strncpy(string+slen-3, "...", 4);
226 static int get_file_icon(struct direntry *file)
228 if (file->type & S_IFDIR)
229 return ICON_FILE_FOLDER;
230 else if (file->flags & BLENDERFILE)
231 return ICON_FILE_BLEND;
232 else if (file->flags & IMAGEFILE)
233 return ICON_FILE_IMAGE;
234 else if (file->flags & MOVIEFILE)
235 return ICON_FILE_MOVIE;
236 else if (file->flags & PYSCRIPTFILE)
237 return ICON_FILE_SCRIPT;
238 else if (file->flags & PYSCRIPTFILE)
239 return ICON_FILE_SCRIPT;
240 else if (file->flags & SOUNDFILE)
241 return ICON_FILE_SOUND;
242 else if (file->flags & FTFONTFILE)
243 return ICON_FILE_FONT;
245 return ICON_FILE_BLANK;
248 static void file_draw_icon(short sx, short sy, int icon, short width, short height)
254 y = (float)(sy-height);
256 if (icon == ICON_FILE_BLANK) blend = -80;
260 UI_icon_draw_aspect_blended(x, y, icon, 1.f, blend);
264 static void file_draw_string(short sx, short sy, const char* string, float width, short height, int flag)
267 char fname[FILE_MAXFILE];
271 BLI_strncpy(fname,string, FILE_MAXFILE);
272 sw = shorten_string(fname, width, flag );
274 soffs = (width - sw) / 2;
276 y = (float)(sy-height);
278 BLF_position(x, y, 0);
282 void file_calc_previews(const bContext *C, ARegion *ar)
284 SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
285 View2D *v2d= &ar->v2d;
287 ED_fileselect_init_layout(sfile, ar);
288 UI_view2d_totRect_set(v2d, sfile->layout->width, sfile->layout->height);
291 void file_draw_previews(const bContext *C, ARegion *ar)
293 SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
294 FileSelectParams* params= ED_fileselect_get_params(sfile);
295 FileLayout* layout= ED_fileselect_get_layout(sfile, ar);
296 View2D *v2d= &ar->v2d;
297 struct FileList* files = sfile->files;
299 struct direntry *file;
309 filelist_imgsize(files,sfile->layout->prv_w,sfile->layout->prv_h);
310 numfiles = filelist_numfiles(files);
312 sx = v2d->cur.xmin + layout->tile_border_x;
313 sy = v2d->cur.ymax - layout->tile_border_y;
315 offset = ED_fileselect_layout_offset(layout, 0, 0);
316 if (offset<0) offset=0;
317 for (i=offset; (i < numfiles) && (i < (offset+(layout->rows+2)*layout->columns)); ++i)
319 ED_fileselect_layout_tilepos(layout, i, &sx, &sy);
320 sx += v2d->tot.xmin+2;
321 sy = v2d->tot.ymax - sy;
322 file = filelist_file(files, i);
324 if (file->flags & ACTIVE) {
326 draw_tile(sx - 1, sy, sfile->layout->tile_w + 1, sfile->layout->tile_h, colorid,0);
327 } else if (params->active_file == i) {
329 draw_tile(sx - 1, sy, sfile->layout->tile_w + 1, sfile->layout->tile_h, colorid,0);
332 if ( (file->flags & IMAGEFILE) /* || (file->flags & MOVIEFILE) */)
334 filelist_loadimage(files, i);
337 imb = filelist_getimage(files, i);
339 imb = filelist_geticon(files,i);
347 float scaledx, scaledy;
351 if ( (imb->x > layout->prv_w) || (imb->y > layout->prv_h) ) {
352 if (imb->x > imb->y) {
353 scaledx = (float)layout->prv_w;
354 scaledy = ( (float)imb->y/(float)imb->x )*layout->prv_w;
355 scale = scaledx/imb->x;
358 scaledy = (float)layout->prv_h;
359 scaledx = ( (float)imb->x/(float)imb->y )*layout->prv_h;
360 scale = scaledy/imb->y;
363 scaledx = (float)imb->x;
364 scaledy = (float)imb->y;
369 fx = ((float)layout->prv_w - (float)ex)/2.0f;
370 fy = ((float)layout->prv_h - (float)ey)/2.0f;
371 dx = (fx + 0.5f + sfile->layout->prv_border_x);
372 dy = (fy + 0.5f - sfile->layout->prv_border_y);
373 xco = (float)sx + dx;
374 yco = (float)sy - sfile->layout->prv_h + dy;
376 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
379 if (!is_icon && (file->flags & IMAGEFILE))
380 uiDrawBoxShadow(220, xco, yco, xco + ex, yco + ey);
385 glColor4f(1.0, 1.0, 1.0, 1.0);
386 glaDrawPixelsTexScaled(xco, yco, imb->x, imb->y, GL_UNSIGNED_BYTE, imb->rect, scale, scale);
389 if (!is_icon && (file->flags & IMAGEFILE)) {
390 glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
391 fdrawbox(xco, yco, xco + ex, yco + ey);
399 UI_ThemeColorShade(TH_BACK, -20);
402 if (S_ISDIR(file->type)) {
403 glColor4f(1.0f, 1.0f, 0.9f, 1.0f);
405 else if (file->flags & IMAGEFILE) {
406 UI_ThemeColor(TH_SEQ_IMAGE);
408 else if (file->flags & MOVIEFILE) {
409 UI_ThemeColor(TH_SEQ_MOVIE);
411 else if (file->flags & BLENDERFILE) {
412 UI_ThemeColor(TH_SEQ_SCENE);
415 if (params->active_file == i) {
416 UI_ThemeColor(TH_GRID); /* grid used for active text */
417 } else if (file->flags & ACTIVE) {
418 UI_ThemeColor(TH_TEXT_HI);
420 UI_ThemeColor(TH_TEXT);
424 file_draw_string(sx + layout->prv_border_x, sy+4, file->relname, layout->tile_w, layout->tile_h, FILE_SHORTEN_END);
426 if (!sfile->loadimage_timer)
427 sfile->loadimage_timer= WM_event_add_window_timer(CTX_wm_window(C), TIMER1, 1.0/30.0); /* max 30 frames/sec. */
435 void file_draw_list(const bContext *C, ARegion *ar)
437 SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
438 FileSelectParams* params = ED_fileselect_get_params(sfile);
439 FileLayout* layout= ED_fileselect_get_layout(sfile, ar);
440 View2D *v2d= &ar->v2d;
441 struct FileList* files = sfile->files;
442 struct direntry *file;
450 numfiles = filelist_numfiles(files);
452 sx = ar->v2d.tot.xmin + layout->tile_border_x/2;
453 sy = ar->v2d.cur.ymax - layout->tile_border_y;
455 offset = ED_fileselect_layout_offset(layout, 0, 0);
456 if (offset<0) offset=0;
458 /* alternating flat shade background */
459 for (i=0; (i <= layout->rows); i+=2)
462 sy = v2d->cur.ymax - i*(layout->tile_h+2*layout->tile_border_y) - layout->tile_border_y;
464 UI_ThemeColorShade(TH_BACK, -7);
465 glRectf(v2d->cur.xmin, sy, v2d->cur.xmax, sy+layout->tile_h+2*layout->tile_border_y);
469 /* vertical column dividers */
471 while (sx < ar->v2d.cur.xmax) {
472 sx += (sfile->layout->tile_w+2*sfile->layout->tile_border_x);
474 UI_ThemeColorShade(TH_BACK, 30);
475 sdrawline(sx+1, ar->v2d.cur.ymax - layout->tile_border_y , sx+1, ar->v2d.cur.ymin);
476 UI_ThemeColorShade(TH_BACK, -30);
477 sdrawline(sx, ar->v2d.cur.ymax - layout->tile_border_y , sx, ar->v2d.cur.ymin);
480 sx = ar->v2d.cur.xmin + layout->tile_border_x;
481 sy = ar->v2d.cur.ymax - layout->tile_border_y;
483 for (i=offset; (i < numfiles); ++i)
485 ED_fileselect_layout_tilepos(layout, i, &sx, &sy);
486 sx += v2d->tot.xmin+2;
487 sy = v2d->tot.ymax - sy;
489 file = filelist_file(files, i);
491 if (params->active_file == i) {
492 if (file->flags & ACTIVE) colorid= TH_HILITE;
493 else colorid = TH_BACK;
494 draw_tile(sx-2, sy-3, layout->tile_w+2, sfile->layout->tile_h+layout->tile_border_y, colorid,20);
495 } else if (file->flags & ACTIVE) {
497 draw_tile(sx-2, sy-3, layout->tile_w+2, sfile->layout->tile_h+layout->tile_border_y, colorid,0);
501 file_draw_icon(spos, sy-3, get_file_icon(file), ICON_DEFAULT_WIDTH, ICON_DEFAULT_WIDTH);
502 spos += ICON_DEFAULT_WIDTH + 4;
504 UI_ThemeColor4(TH_TEXT);
506 sw = file_string_width(file->relname);
507 file_draw_string(spos, sy, file->relname, sw, layout->tile_h, FILE_SHORTEN_END);
508 spos += layout->column_widths[COLUMN_NAME] + 12;
509 if (params->display == FILE_SHOWSHORT) {
510 if (!(file->type & S_IFDIR)) {
511 sw = file_string_width(file->size);
512 spos += layout->column_widths[COLUMN_SIZE] + 12 - sw;
513 file_draw_string(spos, sy, file->size, sw+1, layout->tile_h, FILE_SHORTEN_END);
516 #if 0 // XXX TODO: add this for non-windows systems
519 sw = UI_GetStringWidth(file->mode1);
520 file_draw_string(spos, sy, file->mode1, sw, layout->tile_h);
523 sw = UI_GetStringWidth(file->mode2);
524 file_draw_string(spos, sy, file->mode2, sw, layout->tile_h);
527 sw = UI_GetStringWidth(file->mode3);
528 file_draw_string(spos, sy, file->mode3, sw, layout->tile_h);
531 sw = UI_GetStringWidth(file->owner);
532 file_draw_string(spos, sy, file->owner, sw, layout->tile_h);
536 sw = file_string_width(file->date);
537 file_draw_string(spos, sy, file->date, sw, layout->tile_h, FILE_SHORTEN_END);
538 spos += layout->column_widths[COLUMN_DATE] + 12;
540 sw = file_string_width(file->time);
541 file_draw_string(spos, sy, file->time, sw, layout->tile_h, FILE_SHORTEN_END);
542 spos += layout->column_widths[COLUMN_TIME] + 12;
544 if (!(file->type & S_IFDIR)) {
545 sw = file_string_width(file->size);
546 spos += layout->column_widths[COLUMN_SIZE] + 12 - sw;
547 file_draw_string(spos, sy, file->size, sw, layout->tile_h, FILE_SHORTEN_END);
553 static void file_draw_fsmenu_category_name(ARegion *ar, const char *category_name, short *starty)
556 int bmwidth = ar->v2d.cur.xmax - ar->v2d.cur.xmin - 2*TILE_BORDER_X - ICON_DEFAULT_WIDTH - 4;
557 int fontsize = file_font_pointsize();
559 sx = ar->v2d.cur.xmin + TILE_BORDER_X;
562 UI_ThemeColor(TH_TEXT_HI);
563 file_draw_string(sx, sy, category_name, bmwidth, fontsize, FILE_SHORTEN_END);
570 static void file_draw_fsmenu_category(const bContext *C, ARegion *ar, FSMenuCategory category, short *starty)
572 struct FSMenu* fsmenu = fsmenu_get();
573 char bookmark[FILE_MAX];
574 int nentries = fsmenu_get_nentries(fsmenu, category);
576 short sx, sy, xpos, ypos;
577 int bmwidth = ar->v2d.cur.xmax - ar->v2d.cur.xmin - 2*TILE_BORDER_X - ICON_DEFAULT_WIDTH - 4;
578 int fontsize = file_font_pointsize();
582 sx = ar->v2d.cur.xmin + TILE_BORDER_X;
586 case FS_CATEGORY_SYSTEM:
587 cat_icon = ICON_DISK_DRIVE; break;
588 case FS_CATEGORY_BOOKMARKS:
589 cat_icon = ICON_BOOKMARKS; break;
590 case FS_CATEGORY_RECENT:
591 cat_icon = ICON_FILE_FOLDER; break;
594 for (i=0; i< nentries && (sy > ar->v2d.cur.ymin) ;++i) {
595 char *fname = fsmenu_get_entry(fsmenu, category, i);
599 BLI_strncpy(bookmark, fname, FILE_MAX);
601 sl = strlen(bookmark)-1;
603 while (bookmark[sl] == '\\' || bookmark[sl] == '/') {
609 if (fsmenu_is_selected(fsmenu, category, i) ) {
610 UI_ThemeColor(TH_HILITE);
611 uiRoundBox(sx, sy - fontsize*2.0f, ar->v2d.cur.xmax - TILE_BORDER_X, sy, 4.0f);
612 UI_ThemeColor(TH_TEXT);
614 UI_ThemeColor(TH_TEXT_HI);
618 ypos = sy - (TILE_BORDER_Y * 0.5);
620 file_draw_icon(xpos, ypos, cat_icon, ICON_DEFAULT_WIDTH, ICON_DEFAULT_WIDTH);
621 xpos += ICON_DEFAULT_WIDTH + 4;
622 file_draw_string(xpos, ypos, bookmark, bmwidth, fontsize, FILE_SHORTEN_FRONT);
624 fsmenu_set_pos(fsmenu, category, i, xpos, ypos);
631 void file_draw_fsmenu_operator(const bContext *C, ARegion *ar, wmOperator *op, short *starty)
633 uiStyle *style= U.uistyles.first;
640 block= uiBeginBlock(C, ar, "file_options", UI_EMBOSS);
641 layout= uiBlockLayout(block, UI_LAYOUT_VERTICAL, UI_LAYOUT_PANEL, TILE_BORDER_X, sy, ar->winx-2*TILE_BORDER_X, 20, style);
643 RNA_STRUCT_BEGIN(op->ptr, prop) {
644 if(strcmp(RNA_property_identifier(prop), "rna_type") == 0)
646 if(strcmp(RNA_property_identifier(prop), "filename") == 0)
649 uiItemFullR(layout, NULL, 0, op->ptr, prop, -1, 0, 0, 0, 0);
653 uiBlockLayoutResolve(C, block, NULL, &sy);
654 uiEndBlock(C, block);
655 uiDrawBlock(C, block);
660 void file_draw_fsmenu(const bContext *C, ARegion *ar)
662 SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
663 int linestep = file_font_pointsize()*2.0f;
664 short sy= ar->v2d.cur.ymax-2*TILE_BORDER_Y;
666 file_draw_fsmenu_category_name(ar, "SYSTEM", &sy);
667 file_draw_fsmenu_category(C, ar, FS_CATEGORY_SYSTEM, &sy);
669 file_draw_fsmenu_category_name(ar, "BOOKMARKS", &sy);
670 file_draw_fsmenu_category(C, ar, FS_CATEGORY_BOOKMARKS, &sy);
672 file_draw_fsmenu_category_name(ar, "RECENT", &sy);
673 file_draw_fsmenu_category(C, ar, FS_CATEGORY_RECENT, &sy);
677 file_draw_fsmenu_category_name(ar, "OPTIONS", &sy);
678 file_draw_fsmenu_operator(C, ar, sfile->op, &sy);