Added a NULL check that was needed.
[blender.git] / source / blender / src / filesel.c
index 860f4060975af69b1ea1e84e50b51d547a140a03..1bdca25a88d0fc149074c0db16140b15d0cc5ac8 100644 (file)
@@ -64,6 +64,7 @@
 #include "DNA_ipo_types.h"
 #include "DNA_material_types.h"
 #include "DNA_mesh_types.h"
+#include "DNA_meshdata_types.h"
 #include "DNA_object_types.h"
 #include "DNA_texture_types.h"
 #include "DNA_space_types.h"
 #include "BKE_material.h"
 #include "BKE_utildefines.h"
 
+#include "BIF_editview.h"
+#include "BIF_filelist.h"
 #include "BIF_gl.h"
 #include "BIF_interface.h"
-#include "BIF_toolbox.h"
+#include "BIF_language.h"
 #include "BIF_mywindow.h"
-#include "BIF_editview.h"
+#include "BIF_resources.h"
 #include "BIF_space.h"
 #include "BIF_screen.h"
-#include "BIF_resources.h"
+#include "BIF_toolbox.h"
+#include "BIF_usiblender.h"
 
 #include "BLO_readfile.h"
 
 
 #include "BIF_fsmenu.h"  /* include ourselves */
 
+#ifdef INTERNATIONAL
+#include "FTF_Api.h"
+#endif
+
 #if defined WIN32 || defined __BeOS
-       int fnmatch(){return 0;}
+static int fnmatch(const char *pattern, const char *string, int flags)
+{
+       return 0;
+}
 #else
        #include <fnmatch.h>
 #endif
 
 #define STARTSWITH(x, y) (strncmp(x, y, sizeof(x) - 1) == 0)
 
+/* button events */
+#define B_FS_FILENAME  1
+#define B_FS_DIRNAME   2
+#define B_FS_DIR_MENU  3
+#define B_FS_PARDIR    4
+#define B_FS_LOAD      5
+#define B_FS_CANCEL    6
+#define B_FS_LIBNAME   7
+
+/* max length of library group name within filesel */
+#define GROUP_MAX 32
+
 static int is_a_library(SpaceFile *sfile, char *dir, char *group);
 static void do_library_append(SpaceFile *sfile);
 static void library_to_filelist(SpaceFile *sfile);
@@ -139,164 +162,15 @@ static int groupname_to_code(char *group);
 
 extern void countall(void);
 
-/* local globals */
+/* very bad local globals */
 
 static rcti scrollrct, textrct, bar;
 static int filebuty1, filebuty2, page_ofs, collumwidth, selecting=0;
 static int filetoname= 0;
 static float pixels_to_ofs;
-static char otherdir[FILE_MAXDIR];
+static char otherdir[FILE_MAX];
 static ScrArea *otherarea;
 
-/* FSMENU HANDLING */
-
-       /* FSMenuEntry's without paths indicate seperators */
-typedef struct _FSMenuEntry FSMenuEntry;
-struct _FSMenuEntry {
-       FSMenuEntry *next;
-
-       char *path;
-};
-
-static FSMenuEntry *fsmenu= 0;
-
-int fsmenu_get_nentries(void)
-{
-       FSMenuEntry *fsme;
-       int count= 0;
-
-       for (fsme= fsmenu; fsme; fsme= fsme->next) 
-               count++;
-
-       return count;
-}
-int fsmenu_is_entry_a_seperator(int idx)
-{
-       FSMenuEntry *fsme;
-
-       for (fsme= fsmenu; fsme && idx; fsme= fsme->next)
-               idx--;
-
-       return (fsme && !fsme->path)?1:0;
-}
-char *fsmenu_get_entry(int idx)
-{
-       FSMenuEntry *fsme;
-
-       for (fsme= fsmenu; fsme && idx; fsme= fsme->next)
-               idx--;
-
-       return fsme?fsme->path:NULL;
-}
-char *fsmenu_build_menu(void)
-{
-       DynStr *ds= BLI_dynstr_new();
-       FSMenuEntry *fsme;
-       char *menustr;
-
-       for (fsme= fsmenu; fsme; fsme= fsme->next) {
-               if (!fsme->path) {
-                               /* clean consecutive seperators and ignore trailing ones */
-                       if (fsme->next) {
-                               if (fsme->next->path) {
-                                       BLI_dynstr_append(ds, "%l|");
-                               } else {
-                                       FSMenuEntry *next= fsme->next;
-                                       fsme->next= next->next;
-                                       MEM_freeN(next);
-                               }
-                       }
-               } else {
-                       BLI_dynstr_append(ds, fsme->path);
-                       if (fsme->next) BLI_dynstr_append(ds, "|");
-               }
-       }
-
-       menustr= BLI_dynstr_get_cstring(ds);
-       BLI_dynstr_free(ds);
-       return menustr;
-}
-static FSMenuEntry *fsmenu_get_last_separator(void) 
-{
-       FSMenuEntry *fsme, *lsep=NULL;
-
-       for (fsme= fsmenu; fsme; fsme= fsme->next)
-               if (!fsme->path)
-                       lsep= fsme;
-
-       return lsep;
-}
-void fsmenu_insert_entry(char *path, int sorted)
-{
-       FSMenuEntry *prev= fsmenu_get_last_separator();
-       FSMenuEntry *fsme= prev?prev->next:fsmenu;
-
-       for (; fsme; prev= fsme, fsme= fsme->next) {
-               if (fsme->path) {
-                       if (BLI_streq(path, fsme->path)) {
-                               return;
-                       } else if (sorted && strcmp(path, fsme->path)<0) {
-                               break;
-                       }
-               }
-       }
-       
-       fsme= MEM_mallocN(sizeof(*fsme), "fsme");
-       fsme->path= BLI_strdup(path);
-
-       if (prev) {
-               fsme->next= prev->next;
-               prev->next= fsme;
-       } else {
-               fsme->next= fsmenu;
-               fsmenu= fsme;
-       }
-}
-void fsmenu_append_seperator(void)
-{
-       if (fsmenu) {
-               FSMenuEntry *fsme= fsmenu;
-
-               while (fsme->next) fsme= fsme->next;
-
-               fsme->next= MEM_mallocN(sizeof(*fsme), "fsme");
-               fsme->next->next= NULL;
-               fsme->next->path= NULL;
-       }
-}
-void fsmenu_remove_entry(int idx)
-{
-       FSMenuEntry *prev= NULL, *fsme= fsmenu;
-
-       for (fsme= fsmenu; fsme && idx; prev= fsme, fsme= fsme->next)
-               if (fsme->path)
-                       idx--;
-
-       if (fsme) {
-               if (prev) {
-                       prev->next= fsme->next;
-               } else {
-                       fsmenu= fsme->next;
-               }
-
-               MEM_freeN(fsme->path);
-               MEM_freeN(fsme);
-       }
-}
-void fsmenu_free(void)
-{
-       FSMenuEntry *fsme= fsmenu;
-
-       while (fsme) {
-               FSMenuEntry *n= fsme->next;
-
-               if (fsme->path) MEM_freeN(fsme->path);
-               MEM_freeN(fsme);
-
-               fsme= n;
-       }
-}
-
 /* ******************* SORT ******************* */
 
 static int compare_name(const void *a1, const void *a2)
@@ -422,13 +296,13 @@ static int compare_extension(const void *a1, const void *a2) {
 }
 
 /* **************************************** */
-
-void clear_global_filesel_vars()
+static int filesel_has_func(SpaceFile *sfile)
 {
-       selecting= 0;
+       if(sfile->returnfunc || sfile->returnfunc_event || sfile->returnfunc_args)
+               return 1;
+       return 0;
 }
 
-
 void filesel_statistics(SpaceFile *sfile, int *totfile, int *selfile, float *totlen, float *sellen)
 {
        double len;
@@ -478,8 +352,8 @@ void test_flags_file(SpaceFile *sfile)
                                file->flags |= BLENDERFILE;
                                
                                if(sfile->type==FILE_LOADLIB) {
-                                       char name[FILE_MAXDIR+FILE_MAXFILE];
-                                       strcpy(name, sfile->dir);
+                                       char name[FILE_MAX];
+                                       BLI_strncpy(name, sfile->dir, sizeof(name));
                                        strcat(name, file->relname);
                                        
                                        /* prevent current file being used as acceptable dir */
@@ -489,7 +363,7 @@ void test_flags_file(SpaceFile *sfile)
                                        }
                                }
                        }
-               } else if (sfile->type==FILE_SPECIAL){
+               } else if (sfile->type==FILE_SPECIAL || sfile->type==FILE_LOADFONT){
                        if(BLI_testextensie(file->relname, ".py")) {
                                file->flags |= PYSCRIPTFILE;                    
                        } else if( BLI_testextensie(file->relname, ".ttf")
@@ -513,6 +387,9 @@ void test_flags_file(SpaceFile *sfile)
                                        ||      BLI_testextensie(file->relname, ".rgb")
                                        ||      BLI_testextensie(file->relname, ".bmp")
                                        ||      BLI_testextensie(file->relname, ".png")
+#ifdef WITH_DDS
+                                       ||      BLI_testextensie(file->relname, ".dds")
+#endif
                                        ||      BLI_testextensie(file->relname, ".iff")
                                        ||      BLI_testextensie(file->relname, ".lbm")
                                        ||      BLI_testextensie(file->relname, ".gif")
@@ -523,6 +400,8 @@ void test_flags_file(SpaceFile *sfile)
                                        ||      BLI_testextensie(file->relname, ".pict")
                                        ||      BLI_testextensie(file->relname, ".pntg") //macpaint
                                        ||      BLI_testextensie(file->relname, ".qtif")
+                                       ||  BLI_testextensie(file->relname, ".cin")
+                                       ||  BLI_testextensie(file->relname, ".dpx")
                                        ||      BLI_testextensie(file->relname, ".sgi")) {
                                        file->flags |= IMAGEFILE;                       
                                }
@@ -542,8 +421,13 @@ void test_flags_file(SpaceFile *sfile)
                                        ||      BLI_testextensie(file->relname, ".rgb")
                                        ||      BLI_testextensie(file->relname, ".bmp")
                                        ||      BLI_testextensie(file->relname, ".png")
+#ifdef WITH_DDS
+                                       ||      BLI_testextensie(file->relname, ".dds")
+#endif
                                        ||      BLI_testextensie(file->relname, ".iff")
                                        ||      BLI_testextensie(file->relname, ".lbm")
+                                       ||  BLI_testextensie(file->relname, ".cin")
+                                       ||  BLI_testextensie(file->relname, ".dpx")
                                        ||      BLI_testextensie(file->relname, ".sgi")) {
                                        file->flags |= IMAGEFILE;                       
                                }
@@ -591,7 +475,7 @@ void sort_filelist(SpaceFile *sfile)
 void read_dir(SpaceFile *sfile)
 {
        int num, len;
-       char wdir[FILE_MAXDIR];
+       char wdir[FILE_MAX];
 
        /* sfile->act is used for example in databrowse: double names of library objects */
        sfile->act= -1;
@@ -652,9 +536,9 @@ void freefilelist(SpaceFile *sfile)
 
 static void split_sfile(SpaceFile *sfile, char *s1)
 {
-       char string[FILE_MAXDIR+FILE_MAXFILE], dir[FILE_MAXDIR], file[FILE_MAXFILE];
+       char string[FILE_MAX], dir[FILE_MAX], file[FILE_MAX];
 
-       strcpy(string, s1);
+       BLI_strncpy(string, s1, sizeof(string));
 
        BLI_split_dirfile(string, dir, file);
        
@@ -664,7 +548,7 @@ static void split_sfile(SpaceFile *sfile, char *s1)
                }
                else test_flags_file(sfile);
        }
-       strcpy(sfile->file, file);
+       BLI_strncpy(sfile->file, file, sizeof(sfile->file));
                
        BLI_make_file_string(G.sce, sfile->dir, dir, "");
 }
@@ -676,7 +560,7 @@ void parent(SpaceFile *sfile)
        char *dir;
        
        /* if databrowse: no parent */
-       if(sfile->type==FILE_MAIN && sfile->returnfunc) return;
+       if(sfile->type==FILE_MAIN && filesel_has_func(sfile)) return;
 
        dir= sfile->dir;
        
@@ -698,7 +582,9 @@ void parent(SpaceFile *sfile)
        if( (a = strlen(dir)) ) {
                if (dir[a-1] != '\\') strcat(dir,"\\");
        }
-       else if(sfile->type!=FILE_MAIN) strcpy(dir,"\\");
+       else if(sfile->type!=FILE_MAIN) { 
+               get_default_root(dir);
+       }
 #else
        if( (a = strlen(dir)) ) {                               /* remove all '/' at the end */
                while(dir[a-1] == '/') {
@@ -915,8 +801,28 @@ static void print_line(SpaceFile *sfile, struct direntry *files, int x, int y)
        s = files->string;
        if(s) {
                glRasterPos2i(x,  y);
-               BMF_DrawString(G.font, files->relname);
-               
+#ifdef WITH_ICONV
+               {
+                       struct LANGMenuEntry *lme;
+                       lme = find_language(U.language);
+
+                       if ((lme !=NULL) && (!strcmp(lme->code, "ja_JP") || 
+                               !strcmp(lme->code, "zh_CN")))
+                       {
+                               BIF_RasterPos((float)x, (float)y);
+#ifdef WIN32
+                               BIF_DrawString(G.font, files->relname, ((U.transopts & USER_TR_MENUS) | CONVERT_TO_UTF8));
+#else
+                               BIF_DrawString(G.font, files->relname, (U.transopts & USER_TR_MENUS));
+#endif
+                       } else {
+                               BMF_DrawString(G.font, files->relname);
+                       }
+               }
+#else
+                       BMF_DrawString(G.font, files->relname);
+#endif /* WITH_ICONV */
+
                x += sfile->maxnamelen + 100;
 
                glRasterPos2i(x - BMF_GetStringWidth(G.font, files->size),  y);
@@ -1069,6 +975,21 @@ static void draw_filetext(SpaceFile *sfile)
        uiEmboss(textrct.xmin, textrct.ymin, textrct.xmax, textrct.ymax, 1);
 }
 
+static char *library_string(void)
+{
+       Library *lib;
+       char *str;
+       int nr=0, tot= BLI_countlist(&G.main->library);
+       
+       if(tot==0) return NULL;
+       str= MEM_callocN(tot*(FILE_MAXDIR+FILE_MAX), "filesel lib menu");
+       
+       for(tot=0, lib= G.main->library.first; lib; lib= lib->id.next, nr++) {
+               tot+= sprintf(str+tot, "%s %%x%d|", lib->name, nr);
+       }
+       return str;
+}
+
 void drawfilespace(ScrArea *sa, void *spacedata)
 {
        SpaceFile *sfile;
@@ -1077,7 +998,7 @@ void drawfilespace(ScrArea *sa, void *spacedata)
        int act, loadbutton;
        short mval[2];
        char name[20];
-       char *menu;
+       char *menu, *strp= NULL;
 
        myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
 
@@ -1099,11 +1020,16 @@ void drawfilespace(ScrArea *sa, void *spacedata)
        }
        else calc_file_rcts(sfile);
 
+       /* check if we load library, extra button */
+       if(sfile->type==FILE_LOADLIB)
+               strp= library_string();
+       
        /* HEADER */
        sprintf(name, "win %d", sa->win);
        block= uiNewBlock(&sa->uiblocks, name, UI_EMBOSS, UI_HELV, sa->win);
        
-       uiSetButLock( sfile->type==FILE_MAIN && sfile->returnfunc, NULL);
+       /* browse 1 datablock */
+       uiSetButLock( sfile->type==FILE_MAIN && filesel_has_func(sfile), NULL);
 
        /* space available for load/save buttons? */
        loadbutton= MAX2(80, 20+BMF_GetStringWidth(G.font, sfile->title));
@@ -1113,28 +1039,33 @@ void drawfilespace(ScrArea *sa, void *spacedata)
        else loadbutton= 0;
 
        uiBlockBeginAlign(block);
-       uiDefBut(block, TEX,2,"",       textrct.xmin, filebuty2, textrct.xmax-textrct.xmin-loadbutton, 21, sfile->dir, 0.0, (float)FILE_MAXFILE-1, 0, 0, "Directory, enter a directory and press enter to create it"); /* Directory input */
+       uiDefBut(block, TEX, B_FS_DIRNAME,"",   textrct.xmin + (strp?20:0), filebuty2, textrct.xmax-textrct.xmin-loadbutton - (strp?20:0), 21, sfile->dir, 0.0, (float)FILE_MAXFILE-1, 0, 0, "Directory, enter a directory and press enter to create it"); /* Directory input */
        if(loadbutton) {
                uiSetCurFont(block, UI_HELV);
-               uiDefBut(block, BUT,        5, sfile->title,    textrct.xmax-loadbutton, filebuty2, loadbutton, 21, sfile->dir, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
+               uiDefBut(block, BUT, B_FS_LOAD, sfile->title,   textrct.xmax-loadbutton, filebuty2, loadbutton, 21, sfile->dir, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
        }
        uiBlockEndAlign(block);
        
        uiBlockBeginAlign(block);
-       uiDefBut(block, TEX,1,"",       textrct.xmin, filebuty1, textrct.xmax-textrct.xmin-loadbutton, 21, sfile->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, "File, increment version number with (+/-)"); /* File input */
+       uiDefBut(block, TEX, B_FS_FILENAME,"",  textrct.xmin, filebuty1, textrct.xmax-textrct.xmin-loadbutton, 21, sfile->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, "File, increment version number with (+/-)"); /* File input */
        if(loadbutton) {
                uiSetCurFont(block, UI_HELV);
-               uiDefBut(block, BUT,        6, "Cancel",        textrct.xmax-loadbutton, filebuty1, loadbutton, 21, sfile->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
+               uiDefBut(block, BUT, B_FS_CANCEL, "Cancel",     textrct.xmax-loadbutton, filebuty1, loadbutton, 21, sfile->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
        }
        uiBlockEndAlign(block);
        
        menu= fsmenu_build_menu();
-       if(menu[0])     // happens when no .Bfs is there, and first time browse
-               uiDefButS(block, MENU,  3, menu, scrollrct.xmin, filebuty1, scrollrct.xmax-scrollrct.xmin, 21, &sfile->menu, 0, 0, 0, 0, "");
+       if(menu[0])     /* happens when no .Bfs is there, and first time browse */
+               uiDefButS(block, MENU, B_FS_DIR_MENU, menu, scrollrct.xmin, filebuty1, scrollrct.xmax-scrollrct.xmin, 21, &sfile->menu, 0, 0, 0, 0, "");
        MEM_freeN(menu);
 
-       uiDefBut(block, BUT,            4, "P", scrollrct.xmin, filebuty2, scrollrct.xmax-scrollrct.xmin, 21, 0, 0, 0, 0, 0, "Move to the parent directory (PKEY)");
-
+       uiBlockBeginAlign(block);
+       uiDefBut(block, BUT, B_FS_PARDIR, "P", scrollrct.xmin, filebuty2, scrollrct.xmax-scrollrct.xmin, 21, 0, 0, 0, 0, 0, "Move to the parent directory (PKEY)");
+       if(strp) {
+               uiDefIconTextButS(block, MENU, B_FS_LIBNAME, ICON_LIBRARY_DEHLT, strp, scrollrct.xmin+20, filebuty2, scrollrct.xmax-scrollrct.xmin, 21, &sfile->menu, 0, 0, 0, 0, "");
+               MEM_freeN(strp);
+       }
+                        
        uiDrawBlock(block);
 
        draw_filescroll(sfile);
@@ -1149,6 +1080,7 @@ void drawfilespace(ScrArea *sa, void *spacedata)
        sa->win_swap= WIN_BACK_OK;
 }
 
+
 static void do_filescroll(SpaceFile *sfile)
 {
        short mval[2], oldy, yo;
@@ -1216,10 +1148,15 @@ static void do_filescrollwheel(SpaceFile *sfile, int move)
        }
 }
 
-void activate_fileselect(int type, char *title, char *file, void (*func)(char *))
+/* the complete call; pulldown menu, and three callback types */
+static void activate_fileselect_(int type, char *title, char *file, short *menup, char *pupmenu,
+                                                                                void (*func)(char *),
+                                                                                void (*func_event)(unsigned short),
+                                                                                void (*func_args)(char *, void *arg1, void *arg2),
+                                                                                void *arg1, void *arg2)
 {
        SpaceFile *sfile;
-       char group[24], name[FILE_MAXDIR], temp[FILE_MAXDIR];
+       char group[GROUP_MAX], name[FILE_MAX], temp[FILE_MAX];
        
        if(curarea==0) return;
        if(curarea->win==0) return;
@@ -1231,18 +1168,31 @@ void activate_fileselect(int type, char *title, char *file, void (*func)(char *)
        if(curarea->headwin) addqueue(curarea->headwin, CHANGED, 1);
 
        name[2]= 0;
-       strcpy(name, file);
+       BLI_strncpy(name, file, sizeof(name));
        
        sfile= curarea->spacedata.first;
-       /* sfile wants a (*)(short), but get (*)(char*) */
+
        sfile->returnfunc= func;
+       sfile->returnfunc_event= func_event;
+       sfile->returnfunc_args= func_args;
+       sfile->arg1= arg1;
+       sfile->arg2= arg2;
+       
        sfile->type= type;
        sfile->ofs= 0;
+       
+       if(sfile->pupmenu)
+               MEM_freeN(sfile->pupmenu);
+       sfile->pupmenu= pupmenu;
+       sfile->menup= menup;
+       
        /* sfile->act is used for databrowse: double names of library objects */
        sfile->act= -1;
-       
-       if(BLI_convertstringcode(name, G.sce, G.scene->r.cfra)) sfile->flag |= FILE_STRINGCODE;
-       else sfile->flag &= ~FILE_STRINGCODE;
+
+       if(G.relbase_valid && BLI_convertstringcode(name, G.sce, G.scene->r.cfra))
+               sfile->flag |= FILE_STRINGCODE;
+       else
+               sfile->flag &= ~FILE_STRINGCODE;
 
        if (U.uiflag & USER_HIDE_DOT)
                sfile->flag |= FILE_HIDE_DOT;
@@ -1250,11 +1200,11 @@ void activate_fileselect(int type, char *title, char *file, void (*func)(char *)
        if(type==FILE_MAIN) {
                char *groupname;
                
-               strcpy(sfile->file, name+2);
+               BLI_strncpy(sfile->file, name+2, sizeof(sfile->file));
 
                groupname = BLO_idcode_to_name( GS(name) );
                if (groupname) {
-                       strcpy(sfile->dir, groupname);
+                       BLI_strncpy(sfile->dir, groupname, sizeof(sfile->dir) - 1);
                        strcat(sfile->dir, "/");
                }
 
@@ -1265,7 +1215,7 @@ void activate_fileselect(int type, char *title, char *file, void (*func)(char *)
                freefilelist(sfile);
        }
        else if(type==FILE_LOADLIB) {
-               strcpy(sfile->dir, name);
+               BLI_strncpy(sfile->dir, name, sizeof(sfile->dir));
                if( is_a_library(sfile, temp, group) ) {
                        /* force a reload of the library-filelist */
                        freefilelist(sfile);
@@ -1276,7 +1226,7 @@ void activate_fileselect(int type, char *title, char *file, void (*func)(char *)
                        sfile->libfiledata= NULL;
                }
        }
-       else {  /* FILE_BLENDER */
+       else {  /* FILE_BLENDER or FILE_LOADFONT */
                split_sfile(sfile, name);       /* test filelist too */
                BLI_cleanup_dir(G.sce, sfile->dir);
 
@@ -1288,43 +1238,43 @@ void activate_fileselect(int type, char *title, char *file, void (*func)(char *)
        filetoname= 1;
 }
 
-void activate_imageselect(int type, char *title, char *file, void (*func)(char *))
+void activate_fileselect(int type, char *title, char *file, void (*func)(char *))
 {
-       SpaceImaSel *simasel;
-       char dir[FILE_MAXDIR], name[FILE_MAXFILE];
-       
-       if(curarea==0) return;
-       if(curarea->win==0) return;
-       
-       newspace(curarea, SPACE_IMASEL);
-       
-       /* sometimes double, when area is already SPACE_FILE with a different file name */
-       addqueue(curarea->headwin, CHANGED, 1);
-       addqueue(curarea->win, CHANGED, 1);
+       activate_fileselect_(type, title, file, NULL, NULL, func, NULL, NULL, NULL, NULL);
+}
 
-       name[2]= 0;
-       strcpy(name, file);
+void activate_fileselect_menu(int type, char *title, char *file, char *pupmenu, short *menup, void (*func)(char *))
+{
+       activate_fileselect_(type, title, file, menup, pupmenu, func, NULL, NULL, NULL, NULL);
+}
 
-       simasel= curarea->spacedata.first;
-       simasel->returnfunc= func;
+void activate_fileselect_args(int type, char *title, char *file, void (*func)(char *, void *, void *), void *arg1, void *arg2)
+{
+       activate_fileselect_(type, title, file, NULL, NULL, NULL, NULL, func, arg1, arg2);
+}
 
-       if(BLI_convertstringcode(name, G.sce, G.scene->r.cfra)) simasel->mode |= IMS_STRINGCODE;
-       else simasel->mode &= ~IMS_STRINGCODE;
+void activate_databrowse(ID *id, int idcode, int fromcode, int retval, short *menup, void (*func)(unsigned short))
+{
+       ListBase *lb;
+       SpaceFile *sfile;
+       char str[32];
        
-       BLI_split_dirfile(name, dir, simasel->file);
-       BLI_cleanup_dir(G.sce, simasel->dir);
-       if(strcmp(dir, simasel->dir)!=0) simasel->fase= 0;
-       strcpy(simasel->dir, dir);
+       if(id==NULL) {
+               lb= wich_libbase(G.main, idcode);
+               id= lb->first;
+       }
        
-       BLI_strncpy(simasel->title, title, sizeof(simasel->title));
-
+       if(id) BLI_strncpy(str, id->name, sizeof(str));
+       else return;
        
+       activate_fileselect_(FILE_MAIN, "SELECT DATABLOCK", str, menup, NULL, NULL, func, NULL, NULL, NULL);
        
-       /* filetoname= 1; */
+       sfile= curarea->spacedata.first;
+       sfile->retval= retval;
+       sfile->ipotype= fromcode;
 }
 
-
-void activate_databrowse(ID *id, int idcode, int fromcode, int retval, short *menup, void (*func)(unsigned short))
+void activate_databrowse_args(struct ID *id, int idcode, int fromcode, short *menup, void (*func)(char *, void *, void *), void *arg1, void *arg2)
 {
        ListBase *lb;
        SpaceFile *sfile;
@@ -1335,22 +1285,27 @@ void activate_databrowse(ID *id, int idcode, int fromcode, int retval, short *me
                id= lb->first;
        }
        
-       if(id) strcpy(str, id->name);
+       if(id) BLI_strncpy(str, id->name, sizeof(str));
        else return;
        
-       activate_fileselect(FILE_MAIN, "SELECT DATABLOCK", str, (void (*) (char*))func);
+       activate_fileselect_(FILE_MAIN, "SELECT DATABLOCK", str, menup, NULL, NULL, NULL, func, arg1, arg2);
        
        sfile= curarea->spacedata.first;
-       sfile->retval= retval;
        sfile->ipotype= fromcode;
-       sfile->menup= menup;
 }
 
 void filesel_prevspace()
 {
-       SpaceFile *sfile;
+       SpaceFile *sfile= curarea->spacedata.first;
        
-       sfile= curarea->spacedata.first;
+       /* cleanup */
+       if(sfile->spacetype==SPACE_FILE) {
+               if(sfile->pupmenu) {
+                       MEM_freeN(sfile->pupmenu);
+                       sfile->pupmenu= NULL;
+               }
+       }
+
        if(sfile->next) {
        
                BLI_remlink(&curarea->spacedata, sfile);
@@ -1445,22 +1400,29 @@ void free_filesel_spec(char *dir)
        }
 }
 
-
+/* NOTE: this is called for file read, after the execfunc no UI memory is valid! */
 static void filesel_execute(SpaceFile *sfile)
 {
        struct direntry *files;
-       char name[FILE_MAXDIR];
+       char name[FILE_MAX];
        int a;
        
        filesel_prevspace();
 
        if(sfile->type==FILE_LOADLIB) {
+               if(sfile->flag & FILE_STRINGCODE) {
+                       if (!G.relbase_valid) {
+                               okee("You have to save the .blend file before using relative paths! Using absolute path instead.");
+                               sfile->flag &= ~FILE_STRINGCODE;
+                       }
+               }
+
                do_library_append(sfile);
                BIF_undo_push("Append from file");
                allqueue(REDRAWALL, 1);
        }
-       else if(sfile->returnfunc) {
-               fsmenu_insert_entry(sfile->dir, 1);
+       else if(filesel_has_func(sfile)) {
+               fsmenu_insert_entry(sfile->dir, 1, 0);
        
                if(sfile->type==FILE_MAIN) { /* DATABROWSE */
                        if (sfile->menup) {     /* with value pointing to ID block index */
@@ -1504,27 +1466,36 @@ static void filesel_execute(SpaceFile *sfile)
                                        }
                                }
                        }
-                       sfile->returnfunc((char*) (long)sfile->retval);
+                       if(sfile->returnfunc_event)
+                               sfile->returnfunc_event(sfile->retval);
+                       else if(sfile->returnfunc_args)
+                               sfile->returnfunc_args(NULL, sfile->arg1, sfile->arg2);
                }
                else {
                        if(strncmp(sfile->title, "Save", 4)==0) free_filesel_spec(sfile->dir);
                        if(strncmp(sfile->title, "Export", 6)==0) free_filesel_spec(sfile->dir);
                        
-                       strcpy(name, sfile->dir);
+                       BLI_strncpy(name, sfile->dir, sizeof(name));
                        strcat(name, sfile->file);
                        
-                       if(sfile->flag & FILE_STRINGCODE) BLI_makestringcode(G.sce, name);
-
-                       sfile->returnfunc(name);
+                       if(sfile->flag & FILE_STRINGCODE) {
+                               /* still weak, but we don't want saving files to make relative paths */
+                               if(strncmp(sfile->title, "Save", 4))
+                                       BLI_makestringcode(G.sce, name);
+                       }
+                       if(sfile->returnfunc)
+                               sfile->returnfunc(name);
+                       else if(sfile->returnfunc_args)
+                               sfile->returnfunc_args(name, sfile->arg1, sfile->arg2);
                }
        }
 }
 
 static void do_filesel_buttons(short event, SpaceFile *sfile)
 {
-       char butname[FILE_MAXDIR];
+       char butname[FILE_MAX];
        
-       if (event == 1) {
+       if (event == B_FS_FILENAME) {
                if (strchr(sfile->file, '*') || strchr(sfile->file, '?') || strchr(sfile->file, '[')) {
                        int i, match = FALSE;
                        
@@ -1534,16 +1505,18 @@ static void do_filesel_buttons(short event, SpaceFile *sfile)
                                        match = TRUE;
                                }
                        }
-                       if (match) strcpy(sfile->file, "");
+                       if (match) sfile->file[0] = '\0';
                        if(sfile->type==FILE_MAIN) filesel_select_objects(sfile);
                        scrarea_queue_winredraw(curarea);
                }
        }
-       else if(event== 2) {
+       else if(event== B_FS_DIRNAME) {
                /* reuse the butname variable */
                BLI_cleanup_dir(G.sce, sfile->dir);
 
                BLI_make_file_string(G.sce, butname, sfile->dir, "");
+               BLI_strncpy(sfile->dir, butname, sizeof(sfile->dir));
+
                /* strip the trailing slash if its a real dir */
                if (strlen(butname)!=1)
                        butname[strlen(butname)-1]=0;
@@ -1560,12 +1533,12 @@ static void do_filesel_buttons(short event, SpaceFile *sfile)
                sfile->ofs= 0;
                scrarea_queue_winredraw(curarea);
        }
-       else if(event== 3) {
+       else if(event== B_FS_DIR_MENU) {
                char *selected= fsmenu_get_entry(sfile->menu-1);
                
                /* which string */
                if (selected) {
-                       strcpy(sfile->dir, selected);
+                       BLI_strncpy(sfile->dir, selected, sizeof(sfile->dir));
                        BLI_make_exist(sfile->dir);
                        BLI_cleanup_dir(G.sce, sfile->dir);
                        freefilelist(sfile);
@@ -1576,11 +1549,26 @@ static void do_filesel_buttons(short event, SpaceFile *sfile)
                sfile->act= -1;
                
        }
-       else if(event== 4) parent(sfile);
-       else if(event== 5) {
-               if(sfile->type) filesel_execute(sfile);
+       else if(event== B_FS_PARDIR) 
+               parent(sfile);
+       else if(event== B_FS_LOAD) {
+               if(sfile->type) 
+                       filesel_execute(sfile);
+       }
+       else if(event== B_FS_CANCEL) 
+               filesel_prevspace();
+       else if(event== B_FS_LIBNAME) {
+               Library *lib= BLI_findlink(&G.main->library, sfile->menu);
+               if(lib) {
+                       BLI_strncpy(sfile->dir, lib->filename, sizeof(sfile->dir));
+                       BLI_make_exist(sfile->dir);
+                       BLI_cleanup_dir(G.sce, sfile->dir);
+                       freefilelist(sfile);
+                       sfile->ofs= 0;
+                       scrarea_queue_winredraw(curarea);
+                       sfile->act= -1;
+               }
        }
-       else if(event== 6) filesel_prevspace();
        
 }
 
@@ -1645,19 +1633,20 @@ static void replace_image(ID *oldblock, ID *newblock) {
        }
 
        for (me= G.main->mesh.first; me; me= me->id.next) {
-               TFace *tfaces= me->tface;
-
-               if (tfaces) {
-                       int i;
-
-                       for (i=0; i<me->totface; i++) {
-                               TFace *tf= &tfaces[i];
-
-                               if (tf->tpage == oldima) {
-                                               /* not change_id_link, tpage's aren't owners :(
-                                                * see hack below.
-                                                */
-                                       tf->tpage= newima;
+               int i, a;
+               MTFace *tface;
+
+               for(i=0; i<me->fdata.totlayer; i++) {
+                       if(me->fdata.layers[i].type == CD_MTFACE) {
+                               tface= (MTFace*)me->fdata.layers[i].data;
+
+                               for (a=0; a<me->totface; a++, tface++) {
+                                       if (tface->tpage == oldima) {
+                                                       /* not change_id_link, tpage's aren't owners :(
+                                                        * see hack below.
+                                                        */
+                                               tface->tpage= newima;
+                                       }
                                }
                        }
                }
@@ -1749,7 +1738,7 @@ static void fs_fake_users(SpaceFile *sfile)
        int a;
        
        /* only for F4 DATABROWSE */
-       if(sfile->returnfunc) return;
+       if(filesel_has_func(sfile)) return;
        
        for(a=0; a<sfile->totfile; a++) {
                if(sfile->filelist[a].flags & ACTIVE) {
@@ -1792,7 +1781,7 @@ void winqreadfilespace(ScrArea *sa, void *spacedata, BWinEvent *evt)
        SpaceFile *sfile;
        int act, do_draw= 0, i, test, ret = 0;
        short qual, mval[2];
-       char str[FILE_MAXDIR+FILE_MAXFILE+12];
+       char str[FILE_MAX+12];
        
        sfile= curarea->spacedata.first;
        if(sfile==0) return;
@@ -1847,14 +1836,23 @@ void winqreadfilespace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                                                strcat(sfile->dir, sfile->filelist[act].relname);
                                                strcat(sfile->dir,"/");
                                                BLI_cleanup_dir(G.sce, sfile->dir);
-                                               freefilelist(sfile);
+                                               freefilelist(sfile);                                            
                                                sfile->ofs= 0;
                                                do_draw= 1;
                                        }
                                        else {
                                                if( strcmp(sfile->file, sfile->filelist[act].relname)) {
+                                                       char tmpstr[240];
                                                        do_draw= 1;
-                                                       strcpy(sfile->file, sfile->filelist[act].relname);
+                                                       BLI_strncpy(sfile->file, sfile->filelist[act].relname, sizeof(sfile->file));
+                                                       if (sfile->f_fp) {
+                                                               sprintf (tmpstr, "%s%s", sfile->dir, sfile->file);
+                                                               /* printf ("%s\n", tmpstr); */
+                                                               #ifdef INTERNATIONAL
+                                                               if (!FTF_GetNewFont ((const unsigned char *)tmpstr, 0, U.fontsize))
+                                                                       error ("No font file");
+                                                               #endif
+                                                       }
                                                }
                                                if(event==MIDDLEMOUSE && sfile->type) filesel_execute(sfile);
                                        }
@@ -2040,8 +2038,11 @@ void winqreadfilespace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                case PKEY:
                        if(G.qual & LR_SHIFTKEY) {
                                extern char bprogname[];        /* usiblender.c */
-                       
+#ifdef WIN32                   
                                sprintf(str, "%s -a \"%s%s\"", bprogname, sfile->dir, sfile->file);
+#else
+                               sprintf(str, "\"%s\" -a \"%s%s\"", bprogname, sfile->dir, sfile->file);
+#endif
                                system(str);
                        }
                        else 
@@ -2092,9 +2093,9 @@ void winqreadfilespace(ScrArea *sa, void *spacedata, BWinEvent *evt)
                        if(sfile->type==FILE_MAIN) break;
 
 #ifdef WIN32
-                       strcpy(sfile->dir, "\\");
+                       BLI_strncpy(sfile->dir, "\\", sizeof(sfile->dir));
 #else
-                       strcpy(sfile->dir, "/");
+                       BLI_strncpy(sfile->dir, "/", sizeof(sfile->dir));
 #endif
                        freefilelist(sfile);
                        sfile->ofs= 0;
@@ -2134,10 +2135,10 @@ void winqreadfilespace(ScrArea *sa, void *spacedata, BWinEvent *evt)
 
 static int groupname_to_code(char *group)
 {
-       char buf[32];
+       char buf[GROUP_MAX];
        char *lslash;
        
-       BLI_strncpy(buf, group, 31);
+       BLI_strncpy(buf, group, GROUP_MAX);
        lslash= BLI_last_slash(buf);
        if (lslash)
                lslash[0]= '\0';
@@ -2157,7 +2158,7 @@ static int is_a_library(SpaceFile *sfile, char *dir, char *group)
        len= strlen(dir);
        if(len<7) return 0;
        if( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
-               
+       
        group[0]= 0;
        dir[len-1]= 0;
 
@@ -2167,89 +2168,27 @@ static int is_a_library(SpaceFile *sfile, char *dir, char *group)
        if(fd==0) return 0;
        *fd= 0;
        if(BLO_has_bfile_extension(fd+1)) {
-               *fd= '/';
+               /* the last part of the dir is a .blend file, no group follows */
+               *fd= '/'; /* put back the removed slash separating the dir and the .blend file name */
        }
-       else {
-               strcpy(group, fd+1);
-                       
+       else {          
+               char *gp = fd+1; // in case we have a .blend file, gp points to the group
+
                /* Find the last slash */
                fd= (strrchr(dir, '/')>strrchr(dir, '\\'))?strrchr(dir, '/'):strrchr(dir, '\\');
                if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
-       }
-       return 1;
-}
-
-/* orange hack... :) */
-static void do_sync_pose(Library *lib)
-{
-       Object *ob, *obt;
-       Base *base;
-       bArmature *arm;
-       
-       /* find the armature and the pose from library */
-       for(ob= G.main->object.first; ob; ob= ob->id.next)
-               if(ob->type==OB_ARMATURE && ob->id.lib==lib)
-                       break;
-       
-       if(ob==NULL || ob->pose==NULL) {
-               error("No pose appended");
-               return;
-       }
-       
-       arm= ob->data;
 
-       /* for all visible objects in this scene */
-       for(base= G.scene->base.first; base; base= base->next) {
-               if((base->flag & SELECT)) {
-                       obt= base->object;
-                       if(obt->type==OB_ARMATURE && obt->pose && ob!=obt) {
-                               char str[128];
-                               
-                               sprintf(str, "Replace Object %s", obt->id.name);
-                               if(okee(str)) {
-                                       bPoseChannel *chan;
-                                       bArmature *oldarm= obt->data;
-                                       
-                                       /* link armature */
-                                       oldarm->id.us--;
-                                       obt->data= arm;
-                                       arm->id.us++;
-                                       
-                                       /* link pose */
-                                       free_pose_channels(obt->pose);
-                                       MEM_freeN(obt->pose);
-                                       copy_pose(&obt->pose, ob->pose, 1);
-                                       
-                                       /* relink */
-                                       ob->id.newid= &obt->id;
-                                       for (chan = obt->pose->chanbase.first; chan; chan=chan->next){
-                                               relink_constraints(&chan->constraints);
-                                       }
-                                       
-                                       obt->pose->flag |= POSE_RECALC;
-                                       obt->recalc |= OB_RECALC_DATA;
-                               }
-                       }
-               }
+               /* now we know that we are in a blend file and it is safe to 
+                  assume that gp actually points to a group */
+               BLI_strncpy(group, gp, GROUP_MAX);
        }
-
-       /* prevent saving in file, unlink from scene */
-       for(base= G.scene->base.first; base; base= base->next) {
-               if(base->object==ob)
-                       break;
-       }
-       
-       if(base) {
-               free_and_unlink_base(base);
-       }
-
-       DAG_scene_sort(G.scene);        // for accidentally appended other objects
+       return 1;
 }
 
 static void do_library_append(SpaceFile *sfile)
 {
        Library *lib;
-       char dir[FILE_MAXDIR], group[32];
+       char dir[FILE_MAX], group[GROUP_MAX];
        
        if ( is_a_library(sfile, dir, group)==0 ) {
                error("Not a library");
@@ -2263,8 +2202,12 @@ static void do_library_append(SpaceFile *sfile)
                Object *ob;
                int idcode = groupname_to_code(group);
                
+               if((sfile->flag & FILE_LINK)==0)
+                       /* tag everything, all untagged data can be made local */
+                       flag_all_listbases_ids(LIB_APPEND_TAG, 1);
+               
                BLO_library_append(sfile, dir, idcode);
-
+               
                /* DISPLISTS? */
                ob= G.main->object.first;
                while(ob) {
@@ -2281,16 +2224,18 @@ static void do_library_append(SpaceFile *sfile)
                        lib= lib->id.next;
                }
                
-               if(lib) {
-                       if(sfile->flag & FILE_SYNCPOSE)
-                               do_sync_pose(lib);
-                       if((sfile->flag & FILE_LINK)==0) 
-                               all_local(lib);
+               /* make local */
+               if(lib && (sfile->flag & FILE_LINK)==0) {
+                       all_local(lib, 1);
+                       /* important we unset, otherwise these object wont
+                        * link into other scenes from this blend file */
+                       flag_all_listbases_ids(LIB_APPEND_TAG, 0);
                }
                
-               /* in sfile->dir is the whole lib name */
-               strcpy(G.lib, sfile->dir);
+               DAG_scene_sort(G.scene);
                
+               /* in sfile->dir is the whole lib name */
+               BLI_strncpy(G.lib, sfile->dir, sizeof(G.lib) );
        }
 }
 
@@ -2298,8 +2243,8 @@ static void library_to_filelist(SpaceFile *sfile)
 {
        LinkNode *l, *names;
        int ok, i, nnames, idcode;
-       char filename[FILE_MAXDIR+FILE_MAXFILE];
-       char dir[FILE_MAXDIR], group[24];
+       char filename[FILE_MAX];
+       char dir[FILE_MAX], group[GROUP_MAX];
        
        /* name test */
        ok= is_a_library(sfile, dir, group);
@@ -2374,14 +2319,14 @@ static void filesel_select_objects(SpaceFile *sfile)
        int a;
        
        /* only when F4 DATABROWSE */
-       if(sfile->returnfunc) return;
+       if(filesel_has_func(sfile)) return;
        
        if( strcmp(sfile->dir, "Object/")==0 ) {
                for(a=0; a<sfile->totfile; a++) {
                        
                        ob= (Object *)sfile->filelist[a].poin;
                        
-                       if(ob) {
+                       if(ob && (ob->flag & OB_RESTRICT_VIEW)==0) {
                                if(sfile->filelist[a].flags & ACTIVE) ob->flag |= SELECT;
                                else ob->flag &= ~SELECT;
                        }
@@ -2416,14 +2361,14 @@ static void active_file_object(SpaceFile *sfile)
        Object *ob;
        
        /* only when F4 DATABROWSE */
-       if(sfile->returnfunc) return;
+       if(filesel_has_func(sfile)) return;
        
        if( strcmp(sfile->dir, "Object/")==0 ) {
                if(sfile->act >= 0) {
                        
                        ob= (Object *)sfile->filelist[sfile->act].poin;
                        
-                       if(ob) {
+                       if(ob && (ob->flag & OB_RESTRICT_VIEW)==0) {
                                set_active_object(ob);
                                if(BASACT && BASACT->object==ob) {
                                        BASACT->flag |= SELECT;
@@ -2459,7 +2404,7 @@ void main_to_filelist(SpaceFile *sfile)
        if( sfile->dir[0]==0) {
                
                /* make directories */
-               sfile->totfile= 23;
+               sfile->totfile= 24;
                sfile->filelist= (struct direntry *)malloc(sfile->totfile * sizeof(struct direntry));
                
                for(a=0; a<sfile->totfile; a++) {
@@ -2490,6 +2435,7 @@ void main_to_filelist(SpaceFile *sfile)
                sfile->filelist[20].relname= BLI_strdup("Armature");
                sfile->filelist[21].relname= BLI_strdup("Action");
                sfile->filelist[22].relname= BLI_strdup("NodeTree");
+               sfile->filelist[23].relname= BLI_strdup("Brush");
                
                qsort(sfile->filelist, sfile->totfile, sizeof(struct direntry), compare_name);
        }
@@ -2504,7 +2450,7 @@ void main_to_filelist(SpaceFile *sfile)
                id= lb->first;
                sfile->totfile= 0;
                while(id) {
-                       if(sfile->returnfunc && idcode==ID_IP) {
+                       if(filesel_has_func(sfile) && idcode==ID_IP) {
                                if(sfile->ipotype== ((Ipo *)id)->blocktype) sfile->totfile++;
                        }
                        else if (hide==0 || id->name[2] != '.')
@@ -2513,12 +2459,12 @@ void main_to_filelist(SpaceFile *sfile)
                        id= id->next;
                }
                
-               if(sfile->returnfunc==0) sfile->totfile+= 2;
+               if(!filesel_has_func(sfile)) sfile->totfile+= 2;
                sfile->filelist= (struct direntry *)malloc(sfile->totfile * sizeof(struct direntry));
                
                files= sfile->filelist;
                
-               if(sfile->returnfunc==0) {
+               if(!filesel_has_func(sfile)) {
                        memset( &(sfile->filelist[0]), 0 , sizeof(struct direntry));
                        sfile->filelist[0].relname= BLI_strdup(".");
                        sfile->filelist[0].type |= S_IFDIR;
@@ -2535,7 +2481,7 @@ void main_to_filelist(SpaceFile *sfile)
                while(id) {
                        
                        ok= 0;
-                       if(sfile->returnfunc && idcode==ID_IP) {
+                       if(filesel_has_func(sfile) && idcode==ID_IP) {
                                if(sfile->ipotype== ((Ipo *)id)->blocktype) ok= 1;
                        }
                        else ok= 1;
@@ -2547,11 +2493,14 @@ void main_to_filelist(SpaceFile *sfile)
                                        if(id->lib==NULL)
                                                files->relname= BLI_strdup(id->name+2);
                                        else {
-                                               files->relname= MEM_mallocN(FILE_MAXDIR+FILE_MAXFILE+32, "filename for lib");
-                                               sprintf(files->relname, "%s | %s", id->lib->name, id->name+2);
+                                               char tmp[FILE_MAX], fi[FILE_MAXFILE];
+                                               BLI_strncpy(tmp, id->lib->name, FILE_MAX);
+                                               BLI_splitdirstring(tmp, fi);
+                                               files->relname= MEM_mallocN(FILE_MAXFILE+32, "filename for lib");
+                                               sprintf(files->relname, "%s / %s", fi, id->name+2);
                                        }
                                        
-                                       if(sfile->returnfunc==0) { /* F4 DATA BROWSE */
+                                       if(!filesel_has_func(sfile)) { /* F4 DATA BROWSE */
                                                if(idcode==ID_OB) {
                                                        if( ((Object *)id)->flag & SELECT) files->flags |= ACTIVE;
                                                }
@@ -2596,7 +2545,7 @@ void main_to_filelist(SpaceFile *sfile)
                        if( strcmp(sfile->file, sfile->filelist[a].relname)==0) {
                                sfile->ofs= a-( sfile->collums*(curarea->winy-FILESELHEAD-10)/(2*FILESEL_DY));
                                filetoname= 0;
-                               if(sfile->returnfunc) sfile->filelist[a].flags |= ACTIVE;
+                               if(filesel_has_func(sfile)) sfile->filelist[a].flags |= ACTIVE;
                        }
                }
        }
@@ -2606,9 +2555,9 @@ void main_to_filelist(SpaceFile *sfile)
 void clever_numbuts_filesel()
 {
        SpaceFile *sfile;
-       char orgname[FILE_MAXDIR+FILE_MAXFILE+12];
-       char filename[FILE_MAXDIR+FILE_MAXFILE+12];
-       char newname[FILE_MAXDIR+FILE_MAXFILE+12];
+       char orgname[FILE_MAX+12];
+       char filename[FILE_MAX+12];
+       char newname[FILE_MAX+12];
        int test;
        int len;
        
@@ -2621,7 +2570,7 @@ void clever_numbuts_filesel()
 
        if (test != -1 && !(S_ISDIR(sfile->filelist[test].type))){
                BLI_make_file_string(G.sce, orgname, sfile->dir, sfile->filelist[test].relname);
-               strcpy(filename, sfile->filelist[test].relname);
+               BLI_strncpy(filename, sfile->filelist[test].relname, sizeof(filename));
                
                add_numbut(0, TEX, "", 0, len, filename, "Rename File");