Clear some compiler warnings by commenting some functions, adding others to headers.
[blender.git] / source / blender / editors / space_file / file_draw.c
index 46ed6987752e4d58331c6754eea8f2913761f56b..79c1cce4656010cb5a8783cb174c33adb7760220 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * $Id:
+ * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
@@ -15,7 +15,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2008 Blender Foundation.
  * All rights reserved.
  * ***** END GPL LICENSE BLOCK *****
  */
 
+#include <math.h>
 #include <string.h>
 
 #include "BLI_blenlib.h"
+#include "BLI_utildefines.h"
 #include "BLI_dynstr.h"
 #include "BLI_storage_types.h"
 #ifdef WIN32
 #include "BIF_gl.h"
 #include "BIF_glutil.h"
 
-#include "BKE_colortools.h"
 #include "BKE_context.h"
-#include "BKE_screen.h"
 #include "BKE_global.h"
-#include "BKE_utildefines.h"
+#include "BKE_main.h"
 
 #include "BLF_api.h"
 
-#include "DNA_space_types.h"
-#include "DNA_scene_types.h"
-#include "DNA_screen_types.h"
-#include "DNA_userdef_types.h"
-#include "DNA_windowmanager_types.h"
-
-#include "ED_datafiles.h"
-
 #include "IMB_imbuf_types.h"
-#include "IMB_imbuf.h"
  
 #include "MEM_guardedalloc.h"
 
-#include "PIL_time.h"
+#include "DNA_userdef_types.h"
 
 #include "RNA_access.h"
 
@@ -71,7 +62,6 @@
 #include "UI_resources.h"
 #include "UI_view2d.h"
 
-#include "WM_api.h"
 #include "WM_types.h"
 
 #include "fsmenu.h"
 
 /* button events */
 enum {
-       B_REDR  = 0,
-       B_FS_EXEC,
-       B_FS_CANCEL,
-       B_FS_PARENT,
        B_FS_DIRNAME,
        B_FS_FILENAME
-} eFile_ButEvents;
+} /*eFile_ButEvents*/;
 
 
-static void do_file_buttons(bContext *C, void *arg, int event)
+static void do_file_buttons(bContext *C, void *UNUSED(arg), int event)
 {
        switch(event) {
-               case B_FS_EXEC:
-                       file_exec(C, NULL);     /* file_ops.c */
-                       break;
-               case B_FS_CANCEL:
-                       file_cancel_exec(C, NULL); /* file_ops.c */
-                       break;
-               case B_FS_PARENT:
-                       file_parent_exec(C, NULL); /* file_ops.c */
-                       break;
                case B_FS_FILENAME:
                        file_filename_exec(C, NULL);
                        break;
@@ -133,40 +110,51 @@ static void do_file_buttons(bContext *C, void *arg, int event)
 void file_draw_buttons(const bContext *C, ARegion *ar)
 {
        /* Button layout. */
-       const short min_x      = 10;
-       const short max_x      = ar->winx - 10;
-       const short line1_y    = IMASEL_BUTTONS_HEIGHT/2 + IMASEL_BUTTONS_MARGIN*2;
-       const short line2_y    = IMASEL_BUTTONS_MARGIN;
-       const short input_minw = 20;
-       const short btn_h      = UI_UNIT_Y;
-       const short btn_fn_w   = UI_UNIT_X;
-       const short btn_minw   = 80;
-       const short btn_margin = 20;
-       const short separator  = 4;
+       const int max_x      = ar->winx - 10;
+       const int line1_y    = IMASEL_BUTTONS_HEIGHT/2 + IMASEL_BUTTONS_MARGIN*2;
+       const int line2_y    = IMASEL_BUTTONS_MARGIN;
+       const int input_minw = 20;
+       const int btn_h      = UI_UNIT_Y;
+       const int btn_fn_w   = UI_UNIT_X;
+       const int btn_minw   = 80;
+       const int btn_margin = 20;
+       const int separator  = 4;
 
        /* Additional locals. */
-       char  name[20];
-       short loadbutton;
-       short fnumbuttons;
-       short available_w = max_x - min_x;
-       short line1_w     = available_w;
-       short line2_w     = available_w;
+       char  name[32];
+       int loadbutton;
+       int fnumbuttons;
+       int min_x       = 10;
+       int chan_offs   = 0;
+       int available_w = max_x - min_x;
+       int line1_w     = available_w;
+       int line2_w     = available_w;
        
        uiBut*            but;
        uiBlock*          block;
        SpaceFile*        sfile  = CTX_wm_space_file(C);
        FileSelectParams* params = ED_fileselect_get_params(sfile);
+       ARegion*                  artmp;
        
        /* Initialize UI block. */
-       sprintf(name, "win %p", ar);
+       sprintf(name, "win %p", (void *)ar);
        block = uiBeginBlock(C, ar, name, UI_EMBOSS);
        uiBlockSetHandleFunc(block, do_file_buttons, NULL);
+
+       /* exception to make space for collapsed region icon */
+       for (artmp=CTX_wm_area(C)->regionbase.first; artmp; artmp=artmp->next) {
+               if (artmp->regiontype == RGN_TYPE_CHANNELS && artmp->flag & RGN_FLAG_HIDDEN) {
+                       chan_offs = 16;
+                       min_x += chan_offs;
+                       available_w -= chan_offs;
+               }
+       }
        
        /* Is there enough space for the execute / cancel buttons? */
        loadbutton = UI_GetStringWidth(sfile->params->title) + btn_margin;
        if (loadbutton < btn_minw) {
                loadbutton = MAX2(btn_minw, 
-                                 btn_margin + UI_GetStringWidth(params->title));
+                                                 btn_margin + UI_GetStringWidth(params->title));
        }
        
        if (available_w <= loadbutton + separator + input_minw 
@@ -187,42 +175,59 @@ void file_draw_buttons(const bContext *C, ARegion *ar)
        
        /* Text input fields for directory and file. */
        if (available_w > 0) {
-               but = uiDefBut(block, TEX, B_FS_DIRNAME, "",
-                        min_x, line1_y, line1_w, btn_h, 
-                        params->dir, 0.0, (float)FILE_MAX-1, 0, 0, 
-                        "File path.");
+               int overwrite_alert= file_draw_check_exists(sfile);
+               /* callbacks for operator check functions */
+               uiBlockSetFunc(block, file_draw_check_cb, NULL, NULL);
+
+               but = uiDefButTextO(block, TEX, "FILE_OT_directory", 0, "",
+                                min_x, line1_y, line1_w-chan_offs, btn_h, 
+                                params->dir, 0.0, (float)FILE_MAX-1, 0, 0, 
+                                "File path.");
                uiButSetCompleteFunc(but, autocomplete_directory, NULL);
-               uiDefBut(block, TEX, B_FS_FILENAME, "",
-                        min_x, line2_y, line2_w, btn_h,
-                        params->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, 
-                        "File name.");
+               uiButSetFlag(but, UI_BUT_NO_UTF8);
+
+               but = uiDefBut(block, TEX, B_FS_FILENAME, "",
+                                min_x, line2_y, line2_w-chan_offs, btn_h,
+                                params->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, 
+                                overwrite_alert ?"File name, overwrite existing." : "File name.");
+               uiButSetCompleteFunc(but, autocomplete_file, NULL);
+               uiButSetFlag(but, UI_BUT_NO_UTF8);
+               
+               /* check if this overrides a file and if the operator option is used */
+               if(overwrite_alert) {
+                       uiButSetFlag(but, UI_BUT_REDALERT);
+               }
+               
+               /* clear func */
+               uiBlockSetFunc(block, NULL, NULL, NULL);
        }
        
        /* Filename number increment / decrement buttons. */
        if (fnumbuttons) {
                uiBlockBeginAlign(block);
                but = uiDefIconButO(block, BUT, "FILE_OT_filenum", 0, ICON_ZOOMOUT,
-                       min_x + line2_w + separator, line2_y, 
-                       btn_fn_w, btn_h, 
-                       "Decrement the filename number.");    
+                               min_x + line2_w + separator - chan_offs, line2_y, 
+                               btn_fn_w, btn_h, 
+                               "Decrement the filename number");    
                RNA_int_set(uiButGetOperatorPtrRNA(but), "increment", -1); 
        
                but = uiDefIconButO(block, BUT, "FILE_OT_filenum", 0, ICON_ZOOMIN, 
-                       min_x + line2_w + separator + btn_fn_w, line2_y, 
-                       btn_fn_w, btn_h, 
-                       "Increment the filename number.");    
+                               min_x + line2_w + separator + btn_fn_w - chan_offs, line2_y, 
+                               btn_fn_w, btn_h, 
+                               "Increment the filename number");    
                RNA_int_set(uiButGetOperatorPtrRNA(but), "increment", 1); 
                uiBlockEndAlign(block);
        }
        
        /* Execute / cancel buttons. */
        if(loadbutton) {
-               uiDefBut(block, BUT, B_FS_EXEC, params->title, 
-                 max_x - loadbutton, line1_y, loadbutton, btn_h, 
-                 params->dir, 0.0, (float)FILE_MAXFILE-1, 0, 0, params->title);
-               uiDefBut(block, BUT, B_FS_CANCEL, "Cancel", 
-                 max_x - loadbutton, line2_y, loadbutton, btn_h, 
-                 params->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, "Cancel.");
+               
+               uiDefButO(block, BUT, "FILE_OT_execute", WM_OP_EXEC_REGION_WIN, params->title,
+                       max_x - loadbutton, line1_y, loadbutton, btn_h, 
+                       params->title);
+               uiDefButO(block, BUT, "FILE_OT_cancel", WM_OP_EXEC_REGION_WIN, "Cancel",
+                       max_x - loadbutton, line2_y, loadbutton, btn_h, 
+                       "Cancel");
        }
        
        uiEndBlock(C, block);
@@ -230,70 +235,29 @@ void file_draw_buttons(const bContext *C, ARegion *ar)
 }
 
 
-static void draw_tile(short sx, short sy, short width, short height, int colorid, int shade)
+static void draw_tile(int sx, int sy, int width, int height, int colorid, int shade)
 {      
        UI_ThemeColorShade(colorid, shade);
        uiSetRoundBox(15);      
-       uiRoundBox(sx, sy - height, sx + width, sy, 6);
+       uiRoundBox(sx, sy - height, sx + width, sy, 5);
 }
 
-#define FILE_SHORTEN_END                               0
-#define FILE_SHORTEN_FRONT                             1
-
-static float shorten_string(char* string, float w, int flag)
-{      
-       char temp[FILE_MAX];
-       short shortened = 0;
-       float sw = 0;
-       float pad = 0;
-
-       if (w <= 0) {
-               *string = '\0';
-               return 0.0;
-       }
-
-       sw = file_string_width(string);
-       if (flag == FILE_SHORTEN_FRONT) {
-               char *s = string;
-               BLI_strncpy(temp, "...", 4);
-               pad = file_string_width(temp);
-               while ((*s) && (sw+pad>w)) {
-                       s++;
-                       sw = file_string_width(s);
-                       shortened = 1;
-               }
-               if (shortened) {
-                       int slen = strlen(s);                   
-                       BLI_strncpy(temp+3, s, slen+1);
-                       temp[slen+4] = '\0';
-                       BLI_strncpy(string, temp, slen+4);
-               }
-       } else {
-               char *s = string;
-               while (sw>w) {
-                       int slen = strlen(string);
-                       string[slen-1] = '\0';
-                       sw = file_string_width(s);
-                       shortened = 1;
-               }
-
-               if (shortened) {
-                       int slen = strlen(string);
-                       if (slen > 3) {
-                               BLI_strncpy(string+slen-3, "...", 4);                           
-                       }
-               }
-       }
-       
-       return sw;
-}
 
 static int get_file_icon(struct direntry *file)
 {
-       if (file->type & S_IFDIR)
+       if (file->type & S_IFDIR) {
+               if ( strcmp(file->relname, "..") == 0) {
+                               return  ICON_FILE_PARENT;
+               }
+               if(file->flags & BLENDERFILE) {
+                       return ICON_FILE_BLEND;
+               }
                return ICON_FILE_FOLDER;
+       }
        else if (file->flags & BLENDERFILE)
                return ICON_FILE_BLEND;
+       else if (file->flags & BLENDERFILE_BACKUP)
+               return ICON_FILE_BLEND;
        else if (file->flags & IMAGEFILE)
                return ICON_FILE_IMAGE;
        else if (file->flags & MOVIEFILE)
@@ -306,42 +270,49 @@ static int get_file_icon(struct direntry *file)
                return ICON_FILE_SOUND;
        else if (file->flags & FTFONTFILE) 
                return ICON_FILE_FONT;
+       else if (file->flags & BTXFILE) 
+               return ICON_FILE_BLANK;
+       else if (file->flags & COLLADAFILE) 
+               return ICON_FILE_BLANK;
        else
                return ICON_FILE_BLANK;
 }
 
-static void file_draw_icon(short sx, short sy, int icon, short width, short height)
+static void file_draw_icon(uiBlock *block, char *path, int sx, int sy, int icon, int width, int height)
 {
+       uiBut *but;
        float x,y;
-       int blend=0;
+       /*float alpha=1.0f;*/
        
        x = (float)(sx);
        y = (float)(sy-height);
        
-       if (icon == ICON_FILE_BLANK) blend = -80;
-       
-       glEnable(GL_BLEND);
-       
-       UI_icon_draw_aspect_blended(x, y, icon, 1.f, blend);
+       /*if (icon == ICON_FILE_BLANK) alpha = 0.375f;*/
+               
+       but= uiDefIconBut(block, LABEL, 0, icon, x, y, width, height, NULL, 0.0, 0.0, 0, 0, "");
+       uiButSetDragPath(but, path);
 }
 
 
-static void file_draw_string(short sx, short sy, const char* string, float width, short height, int flag)
+static void file_draw_string(int sx, int sy, const char* string, float width, int height, short align)
 {
-       short soffs;
+       uiStyle *style= U.uistyles.first;
+       uiFontStyle fs = style->widgetlabel;
+       rcti rect;
        char fname[FILE_MAXFILE];
-       float sw;
-       float x,y;
 
-       BLI_strncpy(fname,string, FILE_MAXFILE);
-       sw = shorten_string(fname, width, flag );
+       fs.align = align;
 
-       soffs = (width - sw) / 2;
-       x = (float)(sx);
-       y = (float)(sy-height);
+       BLI_strncpy(fname,string, FILE_MAXFILE);
+       file_shorten_string(fname, width+1.0, 0);
 
-       BLF_position(x, y, 0);
-       BLF_draw(fname);
+       /* no text clipping needed, uiStyleFontDraw does it but is a bit too strict (for buttons it works) */
+       rect.xmin = sx;
+       rect.xmax = sx + ceil(width+4.0f);
+       rect.ymin = sy - height;
+       rect.ymax = sy;
+       
+       uiStyleFontDraw(&fs, &rect, fname);
 }
 
 void file_calc_previews(const bContext *C, ARegion *ar)
@@ -350,68 +321,74 @@ void file_calc_previews(const bContext *C, ARegion *ar)
        View2D *v2d= &ar->v2d;
        
        ED_fileselect_init_layout(sfile, ar);
-       UI_view2d_totRect_set(v2d, sfile->layout->width, sfile->layout->height);
+       /* +SCROLL_HEIGHT is bad hack to work around issue in UI_view2d_totRect_set */
+       UI_view2d_totRect_set(v2d, sfile->layout->width, sfile->layout->height+V2D_SCROLL_HEIGHT);
 }
 
-static void file_draw_preview(short sx, short sy, ImBuf *imb, FileLayout *layout, short dropshadow)
+static void file_draw_preview(uiBlock *block, struct direntry *file, int sx, int sy, ImBuf *imb, FileLayout *layout, short dropshadow)
 {
        if (imb) {
-                       float fx, fy;
-                       float dx, dy;
-                       short xco, yco;
-                       float scaledx, scaledy;
-                       float scale;
-                       short ex, ey;
-
-                       if ( (imb->x > layout->prv_w) || (imb->y > layout->prv_h) ) {
-                               if (imb->x > imb->y) {
-                                       scaledx = (float)layout->prv_w;
-                                       scaledy =  ( (float)imb->y/(float)imb->x )*layout->prv_w;
-                                       scale = scaledx/imb->x;
-                               }
-                               else {
-                                       scaledy = (float)layout->prv_h;
-                                       scaledx =  ( (float)imb->x/(float)imb->y )*layout->prv_h;
-                                       scale = scaledy/imb->y;
-                               }
-                       } else {
-                               scaledx = (float)imb->x;
-                               scaledy = (float)imb->y;
-                               scale = 1.0;
+               uiBut *but;
+               float fx, fy;
+               float dx, dy;
+               int xco, yco;
+               float scaledx, scaledy;
+               float scale;
+               int ex, ey;
+               
+               if ( (imb->x > layout->prv_w) || (imb->y > layout->prv_h) ) {
+                       if (imb->x > imb->y) {
+                               scaledx = (float)layout->prv_w;
+                               scaledy =  ( (float)imb->y/(float)imb->x )*layout->prv_w;
+                               scale = scaledx/imb->x;
                        }
-                       ex = (short)scaledx;
-                       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 + layout->prv_border_x);
-                       dy = (fy + 0.5f - layout->prv_border_y);
-                       xco = (float)sx + dx;
-                       yco = (float)sy - layout->prv_h + dy;
-
-                       glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA);
-                       
-                       /* shadow */
-                       if (dropshadow)
-                               uiDrawBoxShadow(220, xco, yco, xco + ex, yco + ey);
-                       
-                       glEnable(GL_BLEND);
-                       
-                       /* the image */
-                       glColor4f(1.0, 1.0, 1.0, 1.0);
-                       glaDrawPixelsTexScaled(xco, yco, imb->x, imb->y, GL_UNSIGNED_BYTE, imb->rect, scale, scale);
-                       
-                       /* border */
-                       if (dropshadow) {
-                               glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
-                               fdrawbox(xco, yco, xco + ex, yco + ey);
+                       else {
+                               scaledy = (float)layout->prv_h;
+                               scaledx =  ( (float)imb->x/(float)imb->y )*layout->prv_h;
+                               scale = scaledy/imb->y;
                        }
-                       
-                       glDisable(GL_BLEND);
-                       imb = 0;
+               } else {
+                       scaledx = (float)imb->x;
+                       scaledy = (float)imb->y;
+                       scale = 1.0;
+               }
+               ex = (int)scaledx;
+               ey = (int)scaledy;
+               fx = ((float)layout->prv_w - (float)ex)/2.0f;
+               fy = ((float)layout->prv_h - (float)ey)/2.0f;
+               dx = (fx + 0.5f + layout->prv_border_x);
+               dy = (fy + 0.5f - layout->prv_border_y);
+               xco = (float)sx + dx;
+               yco = (float)sy - layout->prv_h + dy;
+               
+               glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA);
+               
+               /* shadow */
+               if (dropshadow)
+                       uiDrawBoxShadow(220, xco, yco, xco + ex, yco + ey);
+               
+               glEnable(GL_BLEND);
+               
+               /* the image */
+               glColor4f(1.0, 1.0, 1.0, 1.0);
+               glaDrawPixelsTexScaled(xco, yco, imb->x, imb->y, GL_UNSIGNED_BYTE, imb->rect, scale, scale);
+               
+               /* border */
+               if (dropshadow) {
+                       glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
+                       fdrawbox(xco, yco, xco + ex, yco + ey);
                }
+               
+               /* dragregion */
+               but= uiDefBut(block, LABEL, 0, "", xco, yco, ex, ey, NULL, 0.0, 0.0, 0, 0, "");
+               uiButSetDragImage(but, file->path, get_file_icon(file), imb, scale);
+               
+               glDisable(GL_BLEND);
+               imb = 0;
+       }
 }
 
-static void renamebutton_cb(bContext *C, void *arg1, char *oldname)
+static void renamebutton_cb(bContext *C, void *UNUSED(arg1), char *oldname)
 {
        char newname[FILE_MAX+12];
        char orgname[FILE_MAX+12];
@@ -419,21 +396,25 @@ static void renamebutton_cb(bContext *C, void *arg1, char *oldname)
        SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
        ARegion* ar = CTX_wm_region(C);
 
+#if 0
        struct direntry *file = (struct direntry *)arg1;
+#endif
 
-       BLI_make_file_string(G.sce, orgname, sfile->params->dir, oldname);
-       BLI_strncpy(filename, file->relname, sizeof(filename));
-       BLI_make_file_string(G.sce, newname, sfile->params->dir, filename);
+       BLI_make_file_string(G.main->name, orgname, sfile->params->dir, oldname);
+       BLI_strncpy(filename, sfile->params->renameedit, sizeof(filename));
+       BLI_make_file_string(G.main->name, newname, sfile->params->dir, filename);
 
        if( strcmp(orgname, newname) != 0 ) {
                if (!BLI_exists(newname)) {
                        BLI_rename(orgname, newname);
                        /* to make sure we show what is on disk */
-                       filelist_free(sfile->files);
-               } else {
-                       BLI_strncpy(file->relname, oldname, strlen(oldname)+1);
+#if 0          /* this is cleared anyway, no need */
+                       MEM_freeN(file->relname);
+                       file->relname= BLI_strdup(sfile->params->renameedit);
+#endif
+                       ED_fileselect_clear(C, sfile);
                }
-               
+
                ED_region_tag_redraw(ar);
        }
 }
@@ -442,7 +423,7 @@ static void renamebutton_cb(bContext *C, void *arg1, char *oldname)
 static void draw_background(FileLayout *layout, View2D *v2d)
 {
        int i;
-       short sy;
+       int sy;
 
        /* alternating flat shade background */
        for (i=0; (i <= layout->rows); i+=2)
@@ -457,7 +438,7 @@ static void draw_background(FileLayout *layout, View2D *v2d)
 
 static void draw_dividers(FileLayout *layout, View2D *v2d)
 {
-       short sx;
+       int sx;
 
        /* vertical column dividers */
        sx = v2d->tot.xmin;
@@ -480,23 +461,20 @@ void file_draw_list(const bContext *C, ARegion *ar)
        struct FileList* files = sfile->files;
        struct direntry *file;
        ImBuf *imb;
+       uiBlock *block = uiBeginBlock(C, ar, "FileNames", UI_EMBOSS);
        int numfiles;
        int numfiles_layout;
        int colorid = 0;
-       short sx, sy;
+       int sx, sy;
        int offset;
+       int textwidth, textheight;
        int i;
-       float sw, spos;
        short is_icon;
+       short align;
+
 
        numfiles = filelist_numfiles(files);
        
-       sx = ar->v2d.tot.xmin + layout->tile_border_x/2;
-       sy = ar->v2d.cur.ymax - layout->tile_border_y;
-
-       offset = ED_fileselect_layout_offset(layout, 0, 0);
-       if (offset<0) offset=0;
-
        if (params->display != FILE_IMGDISPLAY) {
 
                draw_background(layout, v2d);
@@ -504,11 +482,23 @@ void file_draw_list(const bContext *C, ARegion *ar)
                draw_dividers(layout, v2d);
        }
 
-       sx = ar->v2d.cur.xmin + layout->tile_border_x;
-       sy = ar->v2d.cur.ymax - layout->tile_border_y;
-       
+       offset = ED_fileselect_layout_offset(layout, 0, ar->v2d.cur.xmin, -ar->v2d.cur.ymax);
+       if (offset<0) offset=0;
+
        numfiles_layout = ED_fileselect_layout_numfiles(layout, ar);
 
+       /* adjust, so the next row is already drawn when scrolling */
+       if (layout->flag & FILE_LAYOUT_HOR) {
+               numfiles_layout += layout->rows;
+       } else {
+               numfiles_layout += layout->columns;
+       }
+
+       textwidth =( FILE_IMGDISPLAY == params->display) ? layout->tile_w : layout->column_widths[COLUMN_NAME];
+       textheight = layout->textheight*3.0/2.0 + 0.5;
+
+       align = ( FILE_IMGDISPLAY == params->display) ? UI_STYLE_TEXT_CENTER : UI_STYLE_TEXT_LEFT;
+
        for (i=offset; (i < numfiles) && (i<offset+numfiles_layout); ++i)
        {
                ED_fileselect_layout_tilepos(layout, i, &sx, &sy);
@@ -517,23 +507,22 @@ void file_draw_list(const bContext *C, ARegion *ar)
 
                file = filelist_file(files, i); 
                
+               UI_ThemeColor4(TH_TEXT);
+
+
                if (!(file->flags & EDITING)) {
                        if (params->active_file == i) {
-                               if (file->flags & ACTIVE) colorid= TH_HILITE;
+                               if (file->flags & ACTIVEFILE) colorid= TH_HILITE;
                                else colorid = TH_BACK;
-                               draw_tile(sx-2, sy-3, layout->tile_w+2, sfile->layout->tile_h+layout->tile_border_y, colorid,20);
-                       } else if (file->flags & ACTIVE) {
+                               draw_tile(sx, sy-1, layout->tile_w+4, sfile->layout->tile_h+layout->tile_border_y, colorid,20);
+                       } else if (file->flags & ACTIVEFILE) {
                                colorid = TH_HILITE;
-                               draw_tile(sx-2, sy-3, layout->tile_w+2, sfile->layout->tile_h+layout->tile_border_y, colorid,0);
+                               draw_tile(sx, sy-1, layout->tile_w+4, sfile->layout->tile_h+layout->tile_border_y, colorid,0);
                        } 
                }
-
-               spos = sx;
+               uiSetRoundBox(0);
 
                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) {
@@ -541,82 +530,68 @@ void file_draw_list(const bContext *C, ARegion *ar)
                                is_icon = 1;
                        }
                        
-                       file_draw_preview(sx, sy, imb, layout, !is_icon && (file->flags & IMAGEFILE));
-
+                       file_draw_preview(block, file, 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;
+                       file_draw_icon(block, file->path, sx, sy-3, get_file_icon(file), ICON_DEFAULT_WIDTH, ICON_DEFAULT_WIDTH);
+                       sx += 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, 
-                               but_width, layout->textheight*2, file->relname, 1.0f, (float)FILE_MAX,0,0,"");
+                       uiBut *but = uiDefBut(block, TEX, 1, "", sx , sy-layout->tile_h-3, 
+                               textwidth, textheight, sfile->params->renameedit, 1.0f, (float)sizeof(sfile->params->renameedit),0,0,"");
                        uiButSetRenameFunc(but, renamebutton_cb, file);
+                       uiButSetFlag(but, UI_BUT_NO_UTF8); /* allow non utf8 names */
                        if ( 0 == uiButActiveOnly(C, block, but)) {
                                file->flags &= ~EDITING;
                        }
-                       uiEndBlock(C, block);
-                       uiDrawBlock(C, block);
-               } else {
-                       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);
                }
 
-               uiSetRoundBox(0);
+               if (!(file->flags & EDITING))  {
+                       int tpos = (FILE_IMGDISPLAY == params->display) ? sy - layout->tile_h + layout->textheight : sy;
+                       file_draw_string(sx+1, tpos, file->relname, textwidth, textheight, align);
+               }
 
                if (params->display == FILE_SHORTDISPLAY) {
-                       spos += layout->column_widths[COLUMN_NAME] + 12;
+                       sx += 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); 
+                               file_draw_string(sx, sy, file->size, layout->column_widths[COLUMN_SIZE], layout->tile_h, align);        
+                               sx += layout->column_widths[COLUMN_SIZE] + 12;
                        }
                } else if (params->display == FILE_LONGDISPLAY) {
-                       spos += layout->column_widths[COLUMN_NAME] + 12;
+                       sx += layout->column_widths[COLUMN_NAME] + 12;
 
 #ifndef WIN32
                        /* rwx rwx rwx */
-                       spos += 20;
-                       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;
-
-                       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;
+                       file_draw_string(sx, sy, file->mode1, layout->column_widths[COLUMN_MODE1], layout->tile_h, align); 
+                       sx += layout->column_widths[COLUMN_MODE1] + 12;
+
+                       file_draw_string(sx, sy, file->mode2, layout->column_widths[COLUMN_MODE2], layout->tile_h, align);
+                       sx += layout->column_widths[COLUMN_MODE2] + 12;
+
+                       file_draw_string(sx, sy, file->mode3, layout->column_widths[COLUMN_MODE3], layout->tile_h, align);
+                       sx += layout->column_widths[COLUMN_MODE3] + 12;
+
+                       file_draw_string(sx, sy, file->owner, layout->column_widths[COLUMN_OWNER] , layout->tile_h, align);
+                       sx += 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;
+                       file_draw_string(sx, sy, file->date, layout->column_widths[COLUMN_DATE], layout->tile_h, align);
+                       sx += layout->column_widths[COLUMN_DATE] + 12;
 
-                       sw = file_string_width(file->time);
-                       file_draw_string(spos, sy, file->time, sw, layout->tile_h, FILE_SHORTEN_END); 
-                       spos += layout->column_widths[COLUMN_TIME] + 12;
+                       file_draw_string(sx, sy, file->time, layout->column_widths[COLUMN_TIME] , layout->tile_h, align); 
+                       sx += layout->column_widths[COLUMN_TIME] + 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, layout->tile_h, FILE_SHORTEN_END);
+                               file_draw_string(sx, sy, file->size, layout->column_widths[COLUMN_SIZE], layout->tile_h, align);
+                               sx += layout->column_widths[COLUMN_SIZE] + 12;
                        }
                }
        }
 
-       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. */
-}
+       uiEndBlock(C, block);
+       uiDrawBlock(C, block);
 
+}