2.5 filebrowser
authorAndrea Weikert <elubie@gmx.net>
Thu, 20 Aug 2009 18:39:25 +0000 (18:39 +0000)
committerAndrea Weikert <elubie@gmx.net>
Thu, 20 Aug 2009 18:39:25 +0000 (18:39 +0000)
* code cleanup: remove duplication in drawing, join list and preview drawing

source/blender/editors/include/ED_fileselect.h
source/blender/editors/space_file/file_draw.c
source/blender/editors/space_file/filesel.c
source/blender/editors/space_file/space_file.c

index c56807ad09abb5a8eb04a708bb2eadff185e3f80..57ab6a5f8f61bebbeeee373d7144a014b5694d3b 100644 (file)
@@ -65,6 +65,7 @@ typedef struct FileLayout
        short height;
        short flag;
        short dirty;
+       short textheight;
        float column_widths[MAX_FILE_COLUMN];
 } FileLayout;
 
index 5a07c6a75505240547301babc7a374d142f9a0e7..00024ffa961b9eb857bf504647ac8fcdfbe749fe 100644 (file)
@@ -353,59 +353,9 @@ void file_calc_previews(const bContext *C, ARegion *ar)
        UI_view2d_totRect_set(v2d, sfile->layout->width, sfile->layout->height);
 }
 
-void file_draw_previews(const bContext *C, ARegion *ar)
+static void file_draw_preview(short sx, short sy, ImBuf *imb, FileLayout *layout, short dropshadow)
 {
-       SpaceFile *sfile= CTX_wm_space_file(C);
-       FileSelectParams* params= ED_fileselect_get_params(sfile);
-       FileLayout* layout= ED_fileselect_get_layout(sfile, ar);
-       View2D *v2d= &ar->v2d;
-       struct FileList* files = sfile->files;
-       int numfiles;
-       struct direntry *file;
-       short sx, sy;
-       ImBuf* imb=0;
-       int i;
-       int colorid = 0;
-       int offset;
-       int is_icon;
-
-       if (!files) return;
-
-       filelist_imgsize(files,sfile->layout->prv_w,sfile->layout->prv_h);
-       numfiles = filelist_numfiles(files);
-
-       sx = v2d->cur.xmin + layout->tile_border_x;
-       sy = v2d->cur.ymax - layout->tile_border_y;
-       
-       offset = ED_fileselect_layout_offset(layout, 0, 0);
-       if (offset<0) offset=0;
-       for (i=offset; (i < numfiles) && (i < (offset+(layout->rows+2)*layout->columns)); ++i)
-       {
-               ED_fileselect_layout_tilepos(layout, i, &sx, &sy);
-               sx += v2d->tot.xmin+2;
-               sy = v2d->tot.ymax - sy;
-               file = filelist_file(files, i); 
-
-               if (file->flags & ACTIVE) {
-                       colorid = TH_HILITE;
-                       draw_tile(sx - 1, sy, sfile->layout->tile_w + 1, sfile->layout->tile_h, colorid,0);
-               } else if (params->active_file == i) {
-                       colorid = TH_ACTIVE;
-                       draw_tile(sx - 1, sy, sfile->layout->tile_w + 1, sfile->layout->tile_h, colorid,0);
-               }
-               
-               if ( (file->flags & IMAGEFILE) /* || (file->flags & MOVIEFILE) */)
-               {                       
-                       filelist_loadimage(files, i);
-               }
-               is_icon = 0;
-               imb = filelist_getimage(files, i);
-               if (!imb) {
-                       imb = filelist_geticon(files,i);
-                       is_icon = 1;
-               }
-
-               if (imb) {
+       if (imb) {
                        float fx, fy;
                        float dx, dy;
                        short xco, yco;
@@ -433,15 +383,15 @@ void file_draw_previews(const bContext *C, ARegion *ar)
                        ey = (short)scaledy;
                        fx = ((float)layout->prv_w - (float)ex)/2.0f;
                        fy = ((float)layout->prv_h - (float)ey)/2.0f;
-                       dx = (fx + 0.5f + sfile->layout->prv_border_x);
-                       dy = (fy + 0.5f - sfile->layout->prv_border_y);
+                       dx = (fx + 0.5f + layout->prv_border_x);
+                       dy = (fy + 0.5f - layout->prv_border_y);
                        xco = (float)sx + dx;
-                       yco = (float)sy - sfile->layout->prv_h + dy;
+                       yco = (float)sy - layout->prv_h + dy;
 
                        glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA);
                        
                        /* shadow */
-                       if (!is_icon && (file->flags & IMAGEFILE))
+                       if (dropshadow)
                                uiDrawBoxShadow(220, xco, yco, xco + ex, yco + ey);
                        
                        glEnable(GL_BLEND);
@@ -451,7 +401,7 @@ void file_draw_previews(const bContext *C, ARegion *ar)
                        glaDrawPixelsTexScaled(xco, yco, imb->x, imb->y, GL_UNSIGNED_BYTE, imb->rect, scale, scale);
                        
                        /* border */
-                       if (!is_icon && (file->flags & IMAGEFILE)) {
+                       if (dropshadow) {
                                glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
                                fdrawbox(xco, yco, xco + ex, yco + ey);
                        }
@@ -459,41 +409,6 @@ void file_draw_previews(const bContext *C, ARegion *ar)
                        glDisable(GL_BLEND);
                        imb = 0;
                }
-
-               /* shadow */
-               UI_ThemeColorShade(TH_BACK, -20);
-               
-               
-               if (S_ISDIR(file->type)) {
-                       glColor4f(1.0f, 1.0f, 0.9f, 1.0f);
-               }
-               else if (file->flags & IMAGEFILE) {
-                       UI_ThemeColor(TH_SEQ_IMAGE);
-               }
-               else if (file->flags & MOVIEFILE) {
-                       UI_ThemeColor(TH_SEQ_MOVIE);
-               }
-               else if (file->flags & BLENDERFILE) {
-                       UI_ThemeColor(TH_SEQ_SCENE);
-               }
-               else {
-                       if (params->active_file == i) {
-                               UI_ThemeColor(TH_GRID); /* grid used for active text */
-                       } else if (file->flags & ACTIVE) {
-                               UI_ThemeColor(TH_TEXT_HI);                      
-                       } else {
-                               UI_ThemeColor(TH_TEXT);
-                       }
-               }
-
-               file_draw_string(sx + layout->prv_border_x, sy+4, file->relname, layout->tile_w, layout->tile_h, FILE_SHORTEN_END);
-
-               if (!sfile->loadimage_timer)
-                       sfile->loadimage_timer= WM_event_add_window_timer(CTX_wm_window(C), TIMER1, 1.0/30.0);  /* max 30 frames/sec. */
-
-       }
-
-       uiSetRoundBox(0);
 }
 
 static void renamebutton_cb(bContext *C, void *arg1, char *oldname)
@@ -523,6 +438,39 @@ static void renamebutton_cb(bContext *C, void *arg1, char *oldname)
        }
 }
 
+
+static void draw_background(FileLayout *layout, View2D *v2d)
+{
+       int i;
+       short sy;
+
+       /* alternating flat shade background */
+       for (i=0; (i <= layout->rows); i+=2)
+       {
+               sy = v2d->cur.ymax - i*(layout->tile_h+2*layout->tile_border_y) - layout->tile_border_y;
+
+               UI_ThemeColorShade(TH_BACK, -7);
+               glRectf(v2d->cur.xmin, sy, v2d->cur.xmax, sy+layout->tile_h+2*layout->tile_border_y);
+               
+       }
+}
+
+static void draw_dividers(FileLayout *layout, View2D *v2d)
+{
+       short sx;
+
+       /* vertical column dividers */
+       sx = v2d->tot.xmin;
+       while (sx < v2d->cur.xmax) {
+               sx += (layout->tile_w+2*layout->tile_border_x);
+               
+               UI_ThemeColorShade(TH_BACK, 30);
+               sdrawline(sx+1,  v2d->cur.ymax - layout->tile_border_y ,  sx+1,  v2d->cur.ymin); 
+               UI_ThemeColorShade(TH_BACK, -30);
+               sdrawline(sx,  v2d->cur.ymax - layout->tile_border_y ,  sx,  v2d->cur.ymin); 
+       }
+}
+
 void file_draw_list(const bContext *C, ARegion *ar)
 {
        SpaceFile *sfile= CTX_wm_space_file(C);
@@ -531,6 +479,7 @@ void file_draw_list(const bContext *C, ARegion *ar)
        View2D *v2d= &ar->v2d;
        struct FileList* files = sfile->files;
        struct direntry *file;
+       ImBuf *imb;
        int numfiles;
        int numfiles_layout;
        int colorid = 0;
@@ -538,6 +487,7 @@ void file_draw_list(const bContext *C, ARegion *ar)
        int offset;
        int i;
        float sw, spos;
+       short is_icon;
 
        numfiles = filelist_numfiles(files);
        
@@ -547,26 +497,11 @@ void file_draw_list(const bContext *C, ARegion *ar)
        offset = ED_fileselect_layout_offset(layout, 0, 0);
        if (offset<0) offset=0;
 
-       /* alternating flat shade background */
-       for (i=0; (i <= layout->rows); i+=2)
-       {
-               sx = v2d->cur.xmin;
-               sy = v2d->cur.ymax - i*(layout->tile_h+2*layout->tile_border_y) - layout->tile_border_y;
+       if (params->display != FILE_IMGDISPLAY) {
 
-               UI_ThemeColorShade(TH_BACK, -7);
-               glRectf(v2d->cur.xmin, sy, v2d->cur.xmax, sy+layout->tile_h+2*layout->tile_border_y);
-               
-       }
+               draw_background(layout, v2d);
        
-       /* vertical column dividers */
-       sx = v2d->tot.xmin;
-       while (sx < ar->v2d.cur.xmax) {
-               sx += (sfile->layout->tile_w+2*sfile->layout->tile_border_x);
-               
-               UI_ThemeColorShade(TH_BACK, 30);
-               sdrawline(sx+1,  ar->v2d.cur.ymax - layout->tile_border_y ,  sx+1,  ar->v2d.cur.ymin); 
-               UI_ThemeColorShade(TH_BACK, -30);
-               sdrawline(sx,  ar->v2d.cur.ymax - layout->tile_border_y ,  sx,  ar->v2d.cur.ymin); 
+               draw_dividers(layout, v2d);
        }
 
        sx = ar->v2d.cur.xmin + layout->tile_border_x;
@@ -594,16 +529,33 @@ void file_draw_list(const bContext *C, ARegion *ar)
                }
 
                spos = sx;
-               file_draw_icon(spos, sy-3, get_file_icon(file), ICON_DEFAULT_WIDTH, ICON_DEFAULT_WIDTH);
-               spos += ICON_DEFAULT_WIDTH + 4;
-               
+
+               if ( FILE_IMGDISPLAY == params->display ) {
+                       if ( (file->flags & IMAGEFILE) /* || (file->flags & MOVIEFILE) */) {                    
+                               filelist_loadimage(files, i);
+                       }
+                       is_icon = 0;
+                       imb = filelist_getimage(files, i);
+                       if (!imb) {
+                               imb = filelist_geticon(files,i);
+                               is_icon = 1;
+                       }
+                       
+                       file_draw_preview(sx, sy, imb, layout, !is_icon && (file->flags & IMAGEFILE));
+
+               } else {
+                               file_draw_icon(spos, sy-3, get_file_icon(file), ICON_DEFAULT_WIDTH, ICON_DEFAULT_WIDTH);
+                               spos += ICON_DEFAULT_WIDTH + 4;
+               }
+
                UI_ThemeColor4(TH_TEXT);
-               
+
                sw = file_string_width(file->relname);
                if (file->flags & EDITING) {
+                       short but_width = (FILE_IMGDISPLAY == params->display) ? layout->tile_w : layout->column_widths[COLUMN_NAME];
                        uiBlock *block = uiBeginBlock(C, ar, "FileName", UI_EMBOSS);
                        uiBut *but = uiDefBut(block, TEX, 1, "", spos, sy-layout->tile_h-3, 
-                               layout->column_widths[COLUMN_NAME], layout->tile_h, file->relname, 1.0f, (float)FILE_MAX,0,0,"");
+                               but_width, layout->textheight*2, file->relname, 1.0f, (float)FILE_MAX,0,0,"");
                        uiButSetRenameFunc(but, renamebutton_cb, file);
                        if ( 0 == uiButActiveOnly(C, block, but)) {
                                file->flags &= ~EDITING;
@@ -611,36 +563,42 @@ void file_draw_list(const bContext *C, ARegion *ar)
                        uiEndBlock(C, block);
                        uiDrawBlock(C, block);
                } else {
-                       file_draw_string(spos, sy, file->relname, sw, layout->tile_h, FILE_SHORTEN_END);
+                       float name_width = (FILE_IMGDISPLAY == params->display) ? layout->tile_w : sw;
+                       file_draw_string(spos, sy, file->relname, name_width, layout->tile_h, FILE_SHORTEN_END);
                }
-               spos += layout->column_widths[COLUMN_NAME] + 12;
-               if (params->display == FILE_SHOWSHORT) {
+
+               uiSetRoundBox(0);
+
+               if (params->display == FILE_SHORTDISPLAY) {
+                       spos += layout->column_widths[COLUMN_NAME] + 12;
                        if (!(file->type & S_IFDIR)) {
                                sw = file_string_width(file->size);
                                spos += layout->column_widths[COLUMN_SIZE] + 12 - sw;
                                file_draw_string(spos, sy, file->size, sw+1, layout->tile_h, FILE_SHORTEN_END); 
                        }
-               } else {
-#if 0 // XXX TODO: add this for non-windows systems
+               } else if (params->display == FILE_LONGDISPLAY) {
+                       spos += layout->column_widths[COLUMN_NAME] + 12;
+
+#ifndef WIN32
                        /* rwx rwx rwx */
                        spos += 20;
-                       sw = UI_GetStringWidth(file->mode1);
-                       file_draw_string(spos, sy, file->mode1, sw, layout->tile_h); 
-                       
-                       spos += 30;
-                       sw = UI_GetStringWidth(file->mode2);
-                       file_draw_string(spos, sy, file->mode2, sw, layout->tile_h);
+                       sw = file_string_width(file->mode1);
+                       file_draw_string(spos, sy, file->mode1, sw, layout->tile_h, FILE_SHORTEN_END); 
+                       spos += layout->column_widths[COLUMN_MODE1] + 12;
 
-                       spos += 30;
-                       sw = UI_GetStringWidth(file->mode3);
-                       file_draw_string(spos, sy, file->mode3, sw, layout->tile_h);
-                       
-                       spos += 30;
-                       sw = UI_GetStringWidth(file->owner);
-                       file_draw_string(spos, sy, file->owner, sw, layout->tile_h);
+                       sw = file_string_width(file->mode2);
+                       file_draw_string(spos, sy, file->mode2, sw, layout->tile_h, FILE_SHORTEN_END);
+                       spos += layout->column_widths[COLUMN_MODE2] + 12;
+
+                       sw = file_string_width(file->mode3);
+                       file_draw_string(spos, sy, file->mode3, sw, layout->tile_h, FILE_SHORTEN_END);
+                       spos += layout->column_widths[COLUMN_MODE3] + 12;
+
+                       sw = file_string_width(file->owner);
+                       file_draw_string(spos, sy, file->owner, sw, layout->tile_h, FILE_SHORTEN_END);
+                       spos += layout->column_widths[COLUMN_OWNER] + 12;
 #endif
 
-                       
                        sw = file_string_width(file->date);
                        file_draw_string(spos, sy, file->date, sw, layout->tile_h, FILE_SHORTEN_END);
                        spos += layout->column_widths[COLUMN_DATE] + 12;
@@ -656,6 +614,9 @@ void file_draw_list(const bContext *C, ARegion *ar)
                        }
                }
        }
+
+       if (!sfile->loadimage_timer)
+               sfile->loadimage_timer= WM_event_add_window_timer(CTX_wm_window(C), TIMER1, 1.0/30.0);  /* max 30 frames/sec. */
 }
 
 
index 8ee7d3515b507fe036702f6c9c908acd601a32f4..f300505933f27141e0f3e1f75aae24a9a3143f23 100644 (file)
@@ -287,6 +287,7 @@ void ED_fileselect_init_layout(struct SpaceFile *sfile, struct ARegion *ar)
        numfiles = filelist_numfiles(sfile->files);
        textheight = file_font_pointsize();
        layout = sfile->layout;
+       layout->textheight = textheight;
 
        if (params->display == FILE_IMGDISPLAY) {
                layout->prv_w = 96;
index ea640eab090e3abdd71d02a36dc8726d07d67abe..22ad03f3523426ef8fa2983bc83f2704f80ee125 100644 (file)
@@ -310,12 +310,7 @@ static void file_main_area_draw(const bContext *C, ARegion *ar)
                file_hilight_set(sfile, ar, event->x, event->y);
        }
        
-       if (params->display == FILE_IMGDISPLAY) {
-               file_draw_previews(C, ar);
-       } else {
-               file_draw_list(C, ar);
-       }
-       
+       file_draw_list(C, ar);
        
        /* reset view matrix */
        UI_view2d_view_restore(C);