3b17f0c2c268e2692085dd6df1b4e5612c3afcd4
[blender.git] / source / blender / src / filesel.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37 #ifdef WIN32
38 #include <io.h>
39 #include <direct.h>
40 #include "BLI_winstuff.h"
41 #else
42 #include <unistd.h>
43 #include <sys/times.h>
44 #endif   
45
46 #include <sys/stat.h>
47 #include <sys/types.h>
48 #include "MEM_guardedalloc.h"
49
50 #include "BMF_Api.h"
51
52 #include "BLI_blenlib.h"
53 #include "BLI_arithb.h"
54 #include "BLI_linklist.h"
55 #include "BLI_storage_types.h"
56 #include "BLI_dynstr.h"
57
58 #include "IMB_imbuf.h"
59
60 #include "DNA_armature_types.h"
61 #include "DNA_action_types.h"
62 #include "DNA_curve_types.h"
63 #include "DNA_image_types.h"
64 #include "DNA_ipo_types.h"
65 #include "DNA_material_types.h"
66 #include "DNA_mesh_types.h"
67 #include "DNA_object_types.h"
68 #include "DNA_texture_types.h"
69 #include "DNA_space_types.h"
70 #include "DNA_scene_types.h"
71 #include "DNA_screen_types.h"
72 #include "DNA_userdef_types.h"
73 #include "DNA_vfont_types.h"
74 #include "DNA_view3d_types.h"
75
76 #include "BKE_action.h"
77 #include "BKE_constraint.h"
78 #include "BKE_curve.h"
79 #include "BKE_depsgraph.h"
80 #include "BKE_font.h"
81 #include "BKE_global.h"
82 #include "BKE_library.h"
83 #include "BKE_main.h"
84 #include "BKE_material.h"
85 #include "BKE_utildefines.h"
86
87 #include "BIF_gl.h"
88 #include "BIF_interface.h"
89 #include "BIF_toolbox.h"
90 #include "BIF_mywindow.h"
91 #include "BIF_editview.h"
92 #include "BIF_space.h"
93 #include "BIF_screen.h"
94 #include "BIF_resources.h"
95
96 #include "BLO_readfile.h"
97
98 #include "BDR_editcurve.h"
99 #include "BDR_editobject.h"
100
101 #include "BPI_script.h"
102 #include "BSE_filesel.h"
103 #include "BSE_view.h"
104
105 #include "mydevice.h"
106 #include "blendef.h"
107 #include "nla.h"
108
109 #include "BIF_fsmenu.h"  /* include ourselves */
110
111 #if defined WIN32 || defined __BeOS
112         int fnmatch(){return 0;}
113 #else
114         #include <fnmatch.h>
115 #endif
116
117 #ifndef WIN32
118 #include <sys/param.h>
119 #endif
120
121 #define FILESELHEAD             60
122 #define FILESEL_DY              16
123
124 /* for events */
125 #define NOTACTIVE                       0
126 #define ACTIVATE                        1
127 #define INACTIVATE                      2
128 /* for state of file */
129 #define ACTIVE                          2
130
131 #define STARTSWITH(x, y) (strncmp(x, y, sizeof(x) - 1) == 0)
132
133 static int is_a_library(SpaceFile *sfile, char *dir, char *group);
134 static void do_library_append(SpaceFile *sfile);
135 static void library_to_filelist(SpaceFile *sfile);
136 static void filesel_select_objects(struct SpaceFile *sfile);
137 static void active_file_object(struct SpaceFile *sfile);
138 static int groupname_to_code(char *group);
139
140 extern void countall(void);
141
142 /* local globals */
143
144 static rcti scrollrct, textrct, bar;
145 static int filebuty1, filebuty2, page_ofs, collumwidth, selecting=0;
146 static int filetoname= 0;
147 static float pixels_to_ofs;
148 static char otherdir[FILE_MAXDIR];
149 static ScrArea *otherarea;
150
151 /* FSMENU HANDLING */
152
153         /* FSMenuEntry's without paths indicate seperators */
154 typedef struct _FSMenuEntry FSMenuEntry;
155 struct _FSMenuEntry {
156         FSMenuEntry *next;
157
158         char *path;
159 };
160
161 static FSMenuEntry *fsmenu= 0;
162
163 int fsmenu_get_nentries(void)
164 {
165         FSMenuEntry *fsme;
166         int count= 0;
167
168         for (fsme= fsmenu; fsme; fsme= fsme->next) 
169                 count++;
170
171         return count;
172 }
173 int fsmenu_is_entry_a_seperator(int idx)
174 {
175         FSMenuEntry *fsme;
176
177         for (fsme= fsmenu; fsme && idx; fsme= fsme->next)
178                 idx--;
179
180         return (fsme && !fsme->path)?1:0;
181 }
182 char *fsmenu_get_entry(int idx)
183 {
184         FSMenuEntry *fsme;
185
186         for (fsme= fsmenu; fsme && idx; fsme= fsme->next)
187                 idx--;
188
189         return fsme?fsme->path:NULL;
190 }
191 char *fsmenu_build_menu(void)
192 {
193         DynStr *ds= BLI_dynstr_new();
194         FSMenuEntry *fsme;
195         char *menustr;
196
197         for (fsme= fsmenu; fsme; fsme= fsme->next) {
198                 if (!fsme->path) {
199                                 /* clean consecutive seperators and ignore trailing ones */
200                         if (fsme->next) {
201                                 if (fsme->next->path) {
202                                         BLI_dynstr_append(ds, "%l|");
203                                 } else {
204                                         FSMenuEntry *next= fsme->next;
205                                         fsme->next= next->next;
206                                         MEM_freeN(next);
207                                 }
208                         }
209                 } else {
210                         BLI_dynstr_append(ds, fsme->path);
211                         if (fsme->next) BLI_dynstr_append(ds, "|");
212                 }
213         }
214
215         menustr= BLI_dynstr_get_cstring(ds);
216         BLI_dynstr_free(ds);
217         return menustr;
218 }
219 static FSMenuEntry *fsmenu_get_last_separator(void) 
220 {
221         FSMenuEntry *fsme, *lsep=NULL;
222
223         for (fsme= fsmenu; fsme; fsme= fsme->next)
224                 if (!fsme->path)
225                         lsep= fsme;
226
227         return lsep;
228 }
229 void fsmenu_insert_entry(char *path, int sorted)
230 {
231         FSMenuEntry *prev= fsmenu_get_last_separator();
232         FSMenuEntry *fsme= prev?prev->next:fsmenu;
233
234         for (; fsme; prev= fsme, fsme= fsme->next) {
235                 if (fsme->path) {
236                         if (BLI_streq(path, fsme->path)) {
237                                 return;
238                         } else if (sorted && strcmp(path, fsme->path)<0) {
239                                 break;
240                         }
241                 }
242         }
243         
244         fsme= MEM_mallocN(sizeof(*fsme), "fsme");
245         fsme->path= BLI_strdup(path);
246
247         if (prev) {
248                 fsme->next= prev->next;
249                 prev->next= fsme;
250         } else {
251                 fsme->next= fsmenu;
252                 fsmenu= fsme;
253         }
254 }
255 void fsmenu_append_seperator(void)
256 {
257         if (fsmenu) {
258                 FSMenuEntry *fsme= fsmenu;
259
260                 while (fsme->next) fsme= fsme->next;
261
262                 fsme->next= MEM_mallocN(sizeof(*fsme), "fsme");
263                 fsme->next->next= NULL;
264                 fsme->next->path= NULL;
265         }
266 }
267 void fsmenu_remove_entry(int idx)
268 {
269         FSMenuEntry *prev= NULL, *fsme= fsmenu;
270
271         for (fsme= fsmenu; fsme && idx; prev= fsme, fsme= fsme->next)
272                 if (fsme->path)
273                         idx--;
274
275         if (fsme) {
276                 if (prev) {
277                         prev->next= fsme->next;
278                 } else {
279                         fsmenu= fsme->next;
280                 }
281
282                 MEM_freeN(fsme->path);
283                 MEM_freeN(fsme);
284         }
285 }
286 void fsmenu_free(void)
287 {
288         FSMenuEntry *fsme= fsmenu;
289
290         while (fsme) {
291                 FSMenuEntry *n= fsme->next;
292
293                 if (fsme->path) MEM_freeN(fsme->path);
294                 MEM_freeN(fsme);
295
296                 fsme= n;
297         }
298 }
299
300 /* ******************* SORT ******************* */
301
302 static int compare_name(const void *a1, const void *a2)
303 {
304         const struct direntry *entry1=a1, *entry2=a2;
305
306         /* type is is equal to stat.st_mode */
307
308         if (S_ISDIR(entry1->type)){
309                 if (S_ISDIR(entry2->type)==0) return (-1);
310         } else{
311                 if (S_ISDIR(entry2->type)) return (1);
312         }
313         if (S_ISREG(entry1->type)){
314                 if (S_ISREG(entry2->type)==0) return (-1);
315         } else{
316                 if (S_ISREG(entry2->type)) return (1);
317         }
318         if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
319         if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
320         
321         /* make sure "." and ".." are always first */
322         if( strcmp(entry1->relname, ".")==0 ) return (-1);
323         if( strcmp(entry2->relname, ".")==0 ) return (1);
324         if( strcmp(entry1->relname, "..")==0 ) return (-1);
325         
326         return (BLI_strcasecmp(entry1->relname,entry2->relname));
327 }
328
329 static int compare_date(const void *a1, const void *a2) 
330 {
331         const struct direntry *entry1=a1, *entry2=a2;
332         
333         /* type is equal to stat.st_mode */
334
335         if (S_ISDIR(entry1->type)){
336                 if (S_ISDIR(entry2->type)==0) return (-1);
337         } else{
338                 if (S_ISDIR(entry2->type)) return (1);
339         }
340         if (S_ISREG(entry1->type)){
341                 if (S_ISREG(entry2->type)==0) return (-1);
342         } else{
343                 if (S_ISREG(entry2->type)) return (1);
344         }
345         if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
346         if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
347
348         /* make sure "." and ".." are always first */
349         if( strcmp(entry1->relname, ".")==0 ) return (-1);
350         if( strcmp(entry2->relname, ".")==0 ) return (1);
351         if( strcmp(entry1->relname, "..")==0 ) return (-1);
352         
353         if ( entry1->s.st_mtime < entry2->s.st_mtime) return 1;
354         if ( entry1->s.st_mtime > entry2->s.st_mtime) return -1;
355         
356         else return BLI_strcasecmp(entry1->relname,entry2->relname);
357 }
358
359 static int compare_size(const void *a1, const void *a2) 
360 {
361         const struct direntry *entry1=a1, *entry2=a2;
362
363         /* type is equal to stat.st_mode */
364
365         if (S_ISDIR(entry1->type)){
366                 if (S_ISDIR(entry2->type)==0) return (-1);
367         } else{
368                 if (S_ISDIR(entry2->type)) return (1);
369         }
370         if (S_ISREG(entry1->type)){
371                 if (S_ISREG(entry2->type)==0) return (-1);
372         } else{
373                 if (S_ISREG(entry2->type)) return (1);
374         }
375         if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
376         if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
377
378         /* make sure "." and ".." are always first */
379         if( strcmp(entry1->relname, ".")==0 ) return (-1);
380         if( strcmp(entry2->relname, ".")==0 ) return (1);
381         if( strcmp(entry1->relname, "..")==0 ) return (-1);
382         
383         if ( entry1->s.st_size < entry2->s.st_size) return 1;
384         if ( entry1->s.st_size > entry2->s.st_size) return -1;
385         else return BLI_strcasecmp(entry1->relname,entry2->relname);
386 }
387
388 static int compare_extension(const void *a1, const void *a2) {
389         const struct direntry *entry1=a1, *entry2=a2;
390         char *sufix1, *sufix2;
391         char *nil="";
392
393         if (!(sufix1= strstr (entry1->relname, ".blend.gz"))) 
394                 sufix1= strrchr (entry1->relname, '.');
395         if (!(sufix2= strstr (entry2->relname, ".blend.gz")))
396                 sufix2= strrchr (entry2->relname, '.');
397         if (!sufix1) sufix1= nil;
398         if (!sufix2) sufix2= nil;
399
400         /* type is is equal to stat.st_mode */
401
402         if (S_ISDIR(entry1->type)){
403                 if (S_ISDIR(entry2->type)==0) return (-1);
404         } else{
405                 if (S_ISDIR(entry2->type)) return (1);
406         }
407         if (S_ISREG(entry1->type)){
408                 if (S_ISREG(entry2->type)==0) return (-1);
409         } else{
410                 if (S_ISREG(entry2->type)) return (1);
411         }
412         if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
413         if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
414         
415         /* make sure "." and ".." are always first */
416         if( strcmp(entry1->relname, ".")==0 ) return (-1);
417         if( strcmp(entry2->relname, ".")==0 ) return (1);
418         if( strcmp(entry1->relname, "..")==0 ) return (-1);
419         if( strcmp(entry2->relname, "..")==0 ) return (-1);
420         
421         return (BLI_strcasecmp(sufix1, sufix2));
422 }
423
424 /* **************************************** */
425
426 void clear_global_filesel_vars()
427 {
428         selecting= 0;
429 }
430
431
432 void filesel_statistics(SpaceFile *sfile, int *totfile, int *selfile, float *totlen, float *sellen)
433 {
434         double len;
435         int a;
436         
437         *totfile= *selfile= 0;
438         *totlen= *sellen= 0;
439         
440         if(sfile->filelist==0) return;
441         
442         for(a=0; a<sfile->totfile; a++) {
443                 if( (sfile->filelist[a].type & S_IFDIR)==0 ) {
444                         (*totfile) ++;
445
446                         len = sfile->filelist[a].s.st_size;
447                         (*totlen) += (float)(len/1048576.0);            
448
449                         if(sfile->filelist[a].flags & ACTIVE) {
450                                 (*selfile) ++;
451                                 (*sellen) += (float)(len/1048576.0);
452                         }
453                 }
454         }
455 }
456
457 /* *************** HELP FUNCTIONS ******************* */
458
459 /* This is a really ugly function... its purpose is to
460  * take the space file name and clean it up, replacing
461  * excess file entry stuff (like /tmp/../tmp/../)
462  */
463
464 void checkdir(char *dir)
465 {
466         short a;
467         char *start, *eind;
468         char tmp[FILE_MAXDIR+FILE_MAXFILE];
469
470         BLI_make_file_string(G.sce, tmp, dir, "");
471         strcpy(dir, tmp);
472         
473 #ifdef WIN32
474         if(dir[0]=='.') {       /* happens for example in FILE_MAIN */
475                 dir[0]= '\\';
476                 dir[1]= 0;
477                 return;
478         }       
479
480         while ( (start = strstr(dir, "\\..\\")) ) {
481                 eind = start + strlen("\\..\\") - 1;
482                 a = start-dir-1;
483                 while (a>0) {
484                         if (dir[a] == '\\') break;
485                         a--;
486                 }
487                 strcpy(dir+a,eind);
488         }
489
490         while ( (start = strstr(dir,"\\.\\")) ){
491                 eind = start + strlen("\\.\\") - 1;
492                 strcpy(start,eind);
493         }
494
495         while ( (start = strstr(dir,"\\\\" )) ){
496                 eind = start + strlen("\\\\") - 1;
497                 strcpy(start,eind);
498         }
499
500         if((a = strlen(dir))){                          /* remove the '\\' at the end */
501                 while(a>0 && dir[a-1] == '\\'){
502                         a--;
503                         dir[a] = 0;
504                 }
505         }
506
507         strcat(dir, "\\");
508 #else   
509         if(dir[0]=='.') {       /* happens, for example in FILE_MAIN */
510                 dir[0]= '/';
511                 dir[1]= 0;
512                 return;
513         }       
514         
515         while ( (start = strstr(dir, "/../")) ) {
516                 eind = start + strlen("/../") - 1;
517                 a = start-dir-1;
518                 while (a>0) {
519                         if (dir[a] == '/') break;
520                         a--;
521                 }
522                 strcpy(dir+a,eind);
523         }
524
525         while ( (start = strstr(dir,"/./")) ){
526                 eind = start + strlen("/./") - 1;
527                 strcpy(start,eind);
528         }
529
530         while ( (start = strstr(dir,"//" )) ){
531                 eind = start + strlen("//") - 1;
532                 strcpy(start,eind);
533         }
534
535         if( (a = strlen(dir)) ){                                /* remove all '/' at the end */
536                 while(dir[a-1] == '/'){
537                         a--;
538                         dir[a] = 0;
539                         if (a<=0) break;
540                 }
541         }
542
543         strcat(dir, "/");
544 #endif
545 }
546
547 /* not called when browsing .blend itself */
548 void test_flags_file(SpaceFile *sfile)
549 {
550         struct direntry *file;
551         int num;
552
553         file= sfile->filelist;
554         
555         for(num=0; num<sfile->totfile; num++, file++) {
556                 file->flags= 0;
557                 file->type= file->s.st_mode;    /* restore the mess below */ 
558
559                         /* Don't check extensions for directories */ 
560                 if (file->type & S_IFDIR)
561                         continue;
562                         
563                 if(sfile->type==FILE_BLENDER || sfile->type==FILE_LOADLIB) {
564                         if(BLO_has_bfile_extension(file->relname)) {
565                                 file->flags |= BLENDERFILE;
566                                 
567                                 if(sfile->type==FILE_LOADLIB) {
568                                         char name[FILE_MAXDIR+FILE_MAXFILE];
569                                         strcpy(name, sfile->dir);
570                                         strcat(name, file->relname);
571                                         
572                                         /* prevent current file being used as acceptable dir */
573                                         if (BLI_streq(G.main->name, name)==0) {
574                                                 file->type &= ~S_IFMT;
575                                                 file->type |= S_IFDIR;
576                                         }
577                                 }
578                         }
579                 } else if (sfile->type==FILE_SPECIAL){
580                         if(BLI_testextensie(file->relname, ".py")) {
581                                 file->flags |= PYSCRIPTFILE;                    
582                         } else if( BLI_testextensie(file->relname, ".ttf")
583                                         || BLI_testextensie(file->relname, ".ttc")
584                                         || BLI_testextensie(file->relname, ".pfb")
585                                         || BLI_testextensie(file->relname, ".otf")
586                                         || BLI_testextensie(file->relname, ".otc")) {
587                                 file->flags |= FTFONTFILE;                      
588                         } else if (G.have_libtiff &&
589                                         (BLI_testextensie(file->relname, ".tif")
590                                         ||      BLI_testextensie(file->relname, ".tiff"))) {
591                                         file->flags |= IMAGEFILE;                       
592                         } else if (G.have_quicktime){
593                                 if(             BLI_testextensie(file->relname, ".jpg")
594                                         ||      BLI_testextensie(file->relname, ".jpeg")
595                                         ||      BLI_testextensie(file->relname, ".hdr")
596                                         ||      BLI_testextensie(file->relname, ".tga")
597                                         ||      BLI_testextensie(file->relname, ".rgb")
598                                         ||      BLI_testextensie(file->relname, ".bmp")
599                                         ||      BLI_testextensie(file->relname, ".png")
600                                         ||      BLI_testextensie(file->relname, ".iff")
601                                         ||      BLI_testextensie(file->relname, ".lbm")
602                                         ||      BLI_testextensie(file->relname, ".gif")
603                                         ||      BLI_testextensie(file->relname, ".psd")
604                                         ||      BLI_testextensie(file->relname, ".tif")
605                                         ||      BLI_testextensie(file->relname, ".tiff")
606                                         ||      BLI_testextensie(file->relname, ".pct")
607                                         ||      BLI_testextensie(file->relname, ".pict")
608                                         ||      BLI_testextensie(file->relname, ".pntg") //macpaint
609                                         ||      BLI_testextensie(file->relname, ".qtif")
610                                         ||      BLI_testextensie(file->relname, ".sgi")) {
611                                         file->flags |= IMAGEFILE;                       
612                                 }
613                                 else if(BLI_testextensie(file->relname, ".avi")
614                                         ||      BLI_testextensie(file->relname, ".flc")
615                                         ||      BLI_testextensie(file->relname, ".mov")
616                                         ||      BLI_testextensie(file->relname, ".movie")
617                                         ||      BLI_testextensie(file->relname, ".mv")) {
618                                         file->flags |= MOVIEFILE;                       
619                                 }
620                         } else { // no quicktime
621                                 if(BLI_testextensie(file->relname, ".jpg")
622                                         ||      BLI_testextensie(file->relname, ".hdr")
623                                         ||      BLI_testextensie(file->relname, ".tga")
624                                         ||      BLI_testextensie(file->relname, ".rgb")
625                                         ||      BLI_testextensie(file->relname, ".bmp")
626                                         ||      BLI_testextensie(file->relname, ".png")
627                                         ||      BLI_testextensie(file->relname, ".iff")
628                                         ||      BLI_testextensie(file->relname, ".lbm")
629                                         ||      BLI_testextensie(file->relname, ".sgi")) {
630                                         file->flags |= IMAGEFILE;                       
631                                 }
632                                 else if(BLI_testextensie(file->relname, ".avi")
633                                         ||      BLI_testextensie(file->relname, ".mv")) {
634                                         file->flags |= MOVIEFILE;                       
635                                 }
636                                 else if(BLI_testextensie(file->relname, ".wav")) {
637                                         file->flags |= SOUNDFILE;
638                                 }                               
639                         }
640                 }
641         }       
642 }
643
644
645 void sort_filelist(SpaceFile *sfile)
646 {
647         struct direntry *file;
648         int num;/*  , act= 0; */
649         
650         switch(sfile->sort) {
651         case FILE_SORTALPHA:
652                 qsort(sfile->filelist, sfile->totfile, sizeof(struct direntry), compare_name);  
653                 break;
654         case FILE_SORTDATE:
655                 qsort(sfile->filelist, sfile->totfile, sizeof(struct direntry), compare_date);  
656                 break;
657         case FILE_SORTSIZE:
658                 qsort(sfile->filelist, sfile->totfile, sizeof(struct direntry), compare_size);  
659                 break;
660         case FILE_SORTEXTENS:
661                 qsort(sfile->filelist, sfile->totfile, sizeof(struct direntry), compare_extension);     
662         }
663         
664         sfile->act= -1;
665
666         file= sfile->filelist;
667         for(num=0; num<sfile->totfile; num++, file++) {
668                 file->flags &= ~HILITE;
669         }
670
671 }
672
673 void read_dir(SpaceFile *sfile)
674 {
675         int num, len;
676         char wdir[FILE_MAXDIR];
677
678         /* sfile->act is used for example in databrowse: double names of library objects */
679         sfile->act= -1;
680
681         if(sfile->type==FILE_MAIN) {
682                 main_to_filelist(sfile);
683                 return;
684         }
685         else if(sfile->type==FILE_LOADLIB) {
686                 library_to_filelist(sfile);
687                 if(sfile->libfiledata) return;
688         }
689
690         BLI_hide_dot_files(sfile->flag & FILE_HIDE_DOT);
691         
692         BLI_getwdN(wdir);
693         sfile->totfile= BLI_getdir(sfile->dir, &(sfile->filelist));
694         chdir(wdir);
695         
696         if(sfile->sort!=FILE_SORTALPHA) sort_filelist(sfile);
697         
698         sfile->maxnamelen= 0;
699
700         for (num=0; num<sfile->totfile; num++) {
701                 
702                 len = BMF_GetStringWidth(G.font, sfile->filelist[num].relname);
703                 if (len > sfile->maxnamelen) sfile->maxnamelen = len;
704                 
705                 if(filetoname) {
706                         if(strcmp(sfile->file, sfile->filelist[num].relname)==0) {
707                                 
708                                 sfile->ofs= num-( sfile->collums*(curarea->winy-FILESELHEAD-20)/(2*FILESEL_DY));
709                                 filetoname= 0;
710                         }
711                 }
712         }
713         test_flags_file(sfile);
714         
715         filetoname= 0;
716 }
717
718 void freefilelist(SpaceFile *sfile)
719 {
720         int num;
721
722         num= sfile->totfile-1;
723
724         if (sfile->filelist==0) return;
725         
726         for(; num>=0; num--){
727                 MEM_freeN(sfile->filelist[num].relname);
728                 
729                 if (sfile->filelist[num].string) MEM_freeN(sfile->filelist[num].string);
730         }
731         free(sfile->filelist);
732         sfile->filelist= 0;
733 }
734
735 static void split_sfile(SpaceFile *sfile, char *s1)
736 {
737         char string[FILE_MAXDIR+FILE_MAXFILE], dir[FILE_MAXDIR], file[FILE_MAXFILE];
738
739         strcpy(string, s1);
740
741         BLI_split_dirfile(string, dir, file);
742         
743         if(sfile->filelist) {
744                 if(strcmp(dir, sfile->dir)!=0) {
745                         freefilelist(sfile);
746                 }
747                 else test_flags_file(sfile);
748         }
749         strcpy(sfile->file, file);
750                 
751         BLI_make_file_string(G.sce, sfile->dir, dir, "");
752 }
753
754
755 void parent(SpaceFile *sfile)
756 {
757         short a;
758         char *dir;
759         
760         /* if databrowse: no parent */
761         if(sfile->type==FILE_MAIN && sfile->returnfunc) return;
762
763         dir= sfile->dir;
764         
765 #ifdef WIN32
766         if( (a = strlen(dir)) ) {                               /* remove all '/' at the end */
767                 while(dir[a-1] == '\\') {
768                         a--;
769                         dir[a] = 0;
770                         if (a<=0) break;
771                 }
772         }
773         if( (a = strlen(dir)) ) {                               /* then remove all until '/' */
774                 while(dir[a-1] != '\\') {
775                         a--;
776                         dir[a] = 0;
777                         if (a<=0) break;
778                 }
779         }
780         if( (a = strlen(dir)) ) {
781                 if (dir[a-1] != '\\') strcat(dir,"\\");
782         }
783         else if(sfile->type!=FILE_MAIN) strcpy(dir,"\\");
784 #else
785         if( (a = strlen(dir)) ) {                               /* remove all '/' at the end */
786                 while(dir[a-1] == '/') {
787                         a--;
788                         dir[a] = 0;
789                         if (a<=0) break;
790                 }
791         }
792         if( (a = strlen(dir)) ) {                               /* then remove until '/' */
793                 while(dir[a-1] != '/') {
794                         a--;
795                         dir[a] = 0;
796                         if (a<=0) break;
797                 }
798         }
799         if ( (a = strlen(dir)) ) {
800                 if (dir[a-1] != '/') strcat(dir,"/");
801         }
802         else if(sfile->type!=FILE_MAIN) strcpy(dir,"/");
803 #endif
804         
805         /* to be sure */
806         BLI_make_exist(sfile->dir);
807
808         freefilelist(sfile);
809         sfile->ofs= 0;
810         scrarea_queue_winredraw(curarea);
811 }
812
813 void swapselect_file(SpaceFile *sfile)
814 {
815         struct direntry *file;
816         int num, act= 0;
817         
818         file= sfile->filelist;
819         for(num=0; num<sfile->totfile; num++, file++) {
820                 if(file->flags & ACTIVE) {
821                         act= 1;
822                         break;
823                 }
824         }
825         file= sfile->filelist+2;
826         for(num=2; num<sfile->totfile; num++, file++) {
827                 if(act) file->flags &= ~ACTIVE;
828                 else file->flags |= ACTIVE;
829         }
830 }
831
832 static int find_active_file(SpaceFile *sfile, short x, short y)
833 {
834         int ofs;
835         
836         if(y > textrct.ymax) y= textrct.ymax;
837         if(y <= textrct.ymin) y= textrct.ymin+1;
838         
839         ofs= (x-textrct.xmin)/collumwidth;
840         if(ofs<0) ofs= 0;
841         ofs*= (textrct.ymax-textrct.ymin);
842
843         return sfile->ofs+ (ofs+textrct.ymax-y)/FILESEL_DY;
844         
845 }
846
847
848 /* ********************** DRAW ******************************* */
849
850 static void calc_file_rcts(SpaceFile *sfile)
851 {
852         int tot, h, len;
853         float fac, start, totfile;
854         
855         scrollrct.xmin= 15;
856         scrollrct.xmax= 35;
857         scrollrct.ymin= 10;
858         scrollrct.ymax= curarea->winy-10-FILESELHEAD;
859         
860         textrct.xmin= scrollrct.xmax+10;
861         textrct.xmax= curarea->winx-10;
862         textrct.ymin= scrollrct.ymin;
863         textrct.ymax= scrollrct.ymax;
864         
865         if(textrct.xmax-textrct.xmin <60) textrct.xmax= textrct.xmin+60;
866         
867         len= (textrct.ymax-textrct.ymin) % FILESEL_DY;
868         textrct.ymin+= len;
869         scrollrct.ymin+= len;
870         
871         filebuty1= curarea->winy-FILESELHEAD;
872         filebuty2= filebuty1+FILESELHEAD/2 -6;
873         
874         
875         /* amount of collums */
876         len= sfile->maxnamelen+25;
877         
878         if(sfile->type==FILE_MAIN) len+= 100;
879         else if(sfile->flag & FILE_SHOWSHORT) len+= 100;
880         else len+= 380;
881         
882         sfile->collums= (textrct.xmax-textrct.xmin)/len;
883         
884         if(sfile->collums<1) sfile->collums= 1;
885         else if(sfile->collums>8) sfile->collums= 8;
886
887         /* this flag aint yet defined in user menu, needed? */
888 //      if((U.flag & USER_FSCOLLUM)==0) sfile->collums= 1;
889         
890         collumwidth= (textrct.xmax-textrct.xmin)/sfile->collums;
891         
892
893         totfile= sfile->totfile + 0.5f;
894
895         tot= (int)(FILESEL_DY*totfile);
896         if(tot) fac= ((float)sfile->collums*(scrollrct.ymax-scrollrct.ymin))/( (float)tot);
897         else fac= 1.0;
898         
899         if(sfile->ofs<0) sfile->ofs= 0;
900         
901         if(tot) start= ( (float)sfile->ofs)/(totfile);
902         else start= 0.0;
903         if(fac>1.0) fac= 1.0f;
904
905         if(start+fac>1.0) {
906                 sfile->ofs= (short)ceil((1.0-fac)*totfile);
907                 start= ( (float)sfile->ofs)/(totfile);
908                 fac= 1.0f-start;
909         }
910
911         bar.xmin= scrollrct.xmin+2;
912         bar.xmax= scrollrct.xmax-2;
913         h= (scrollrct.ymax-scrollrct.ymin)-4;
914         bar.ymax= (int)(scrollrct.ymax-2- start*h);
915         bar.ymin= (int)(bar.ymax- fac*h);
916
917         pixels_to_ofs= (totfile)/(float)(h+3);
918         page_ofs= (int)(fac*totfile);
919 }
920
921 int filescrollselect= 0;
922
923 static void draw_filescroll(SpaceFile *sfile)
924 {
925
926         if(scrollrct.ymin+10 >= scrollrct.ymax) return;
927         
928         BIF_ThemeColor(TH_BACK);
929         glRecti(scrollrct.xmin,  scrollrct.ymin,  scrollrct.xmax,  scrollrct.ymax);
930
931         uiEmboss(scrollrct.xmin, scrollrct.ymin, scrollrct.xmax, scrollrct.ymax, 1);
932
933         BIF_ThemeColor(TH_HEADER);
934         glRecti(bar.xmin+2,  bar.ymin+2,  bar.xmax-2,  bar.ymax-2);
935
936         uiEmboss(bar.xmin+2, bar.ymin+2, bar.xmax-2, bar.ymax-2, filescrollselect);
937         
938 }
939
940 static void linerect(int id, int x, int y)
941 {
942         if(id & ACTIVE) {
943                 if(id & HILITE) BIF_ThemeColorShade(TH_HILITE, 20);
944                 else BIF_ThemeColor(TH_HILITE);
945         }
946         else if(id & HILITE) BIF_ThemeColorShade(TH_BACK, 20);
947         else BIF_ThemeColor(TH_BACK);
948         
949         glRects(x-17,  y-3,  x+collumwidth-21,  y+11);
950
951 }
952
953 static void print_line(SpaceFile *sfile, struct direntry *files, int x, int y)
954 {
955         int boxcol=0;
956         char *s;
957
958         boxcol= files->flags & (HILITE + ACTIVE);
959
960         if(boxcol) {
961                 linerect(boxcol, x, y);
962         }
963
964         // this is where the little boxes in the file view are being drawn according to the file type
965         if(files->flags & BLENDERFILE) {
966                 cpack(0xA0A0);
967                 glRects(x-14,  y,  x-8,  y+7);
968         }
969         else if(files->flags & PSXFILE) {
970                 cpack(0xA060B0);
971                 glRects(x-14,  y,  x-8,  y+7);
972         }
973         else if(files->flags & IMAGEFILE) {
974                 cpack(0xF08040);
975                 glRects(x-14,  y,  x-8,  y+7);
976         }
977         else if(files->flags & MOVIEFILE) {
978                 cpack(0x70A070);
979                 glRects(x-14,  y,  x-8,  y+7);
980         }
981         else if(files->flags & PYSCRIPTFILE) {
982                 cpack(0x4477dd);
983                 glRects(x-14,  y,  x-8,  y+7);
984         }
985         else if(files->flags & SOUNDFILE) {
986                 cpack(0xa0a000);
987                 glRects(x-14,  y,  x-8,  y+7);
988         }       
989         else if(files->flags & FTFONTFILE) {
990                 cpack(0xff2371);
991                 glRects(x-14,  y,  x-8,  y+7);
992         }
993         
994         if(S_ISDIR(files->type)) BIF_ThemeColor(TH_TEXT_HI);
995         else BIF_ThemeColor(TH_TEXT);
996
997         s = files->string;
998         if(s) {
999                 glRasterPos2i(x,  y);
1000                 BMF_DrawString(G.font, files->relname);
1001                 
1002                 x += sfile->maxnamelen + 100;
1003
1004                 glRasterPos2i(x - BMF_GetStringWidth(G.font, files->size),  y);
1005                 BMF_DrawString(G.font, files->size);
1006
1007                 if(sfile->flag & FILE_SHOWSHORT) return;
1008
1009 #ifndef WIN32
1010                 /* rwx rwx rwx */
1011                         x += 20; glRasterPos2i(x, y); 
1012                         BMF_DrawString(G.font, files->mode1); 
1013                 
1014                         x += 30; glRasterPos2i(x, y); 
1015                         BMF_DrawString(G.font, files->mode2); 
1016                 
1017                         x += 30; glRasterPos2i(x, y); 
1018                         BMF_DrawString(G.font, files->mode3); 
1019                 
1020                 /* owner time date */
1021                         x += 30; glRasterPos2i(x, y); 
1022                         BMF_DrawString(G.font, files->owner); 
1023 #endif
1024                 
1025                         x += 60; glRasterPos2i(x, y); 
1026                         BMF_DrawString(G.font, files->time); 
1027                 
1028                         x += 50; glRasterPos2i(x, y); 
1029                         BMF_DrawString(G.font, files->date); 
1030         }
1031         else {
1032                 glRasterPos2i(x,  y);
1033                 BMF_DrawString(G.font, files->relname);
1034                 
1035                 if(files->nr) { /* extra info */
1036                         x+= sfile->maxnamelen+20;
1037                         glRasterPos2i(x,  y);
1038                         BMF_DrawString(G.font, files->extra);
1039                 }
1040         }
1041 }
1042
1043
1044 static int calc_filesel_line(SpaceFile *sfile, int nr, int *valx, int *valy)
1045 {
1046         /* get screen coordinate of a line */
1047         int val, coll;
1048
1049         nr-= sfile->ofs;
1050
1051         /* amount of lines */
1052         val= (textrct.ymax-textrct.ymin)/FILESEL_DY;
1053         if (val == 0) coll = 0;
1054         else coll= nr/val;
1055         nr -= coll*val;
1056         
1057         *valy= textrct.ymax-FILESEL_DY+3 - nr*FILESEL_DY;
1058         *valx= coll*collumwidth + textrct.xmin+20;
1059         
1060         if(nr<0 || coll > sfile->collums) return 0;
1061         return 1;
1062 }
1063
1064 static void set_active_file(SpaceFile *sfile, int act)
1065 {
1066         struct direntry *file;
1067         int num, redraw= 0;
1068         unsigned int newflag;
1069         int old=0, newi=0;
1070         
1071         file= sfile->filelist;
1072         if(file==0) return;
1073         
1074         for(num=0; num<sfile->totfile; num++, file++) {
1075                 if(num==act) {
1076                         
1077                         if(selecting && num>1) {
1078                                 newflag= HILITE | (file->flags & ~ACTIVE);
1079                                 if(selecting==ACTIVATE) newflag |= ACTIVE;
1080                         
1081                                 if(file->flags != newflag) redraw|= 1;
1082                                 file->flags= newflag;
1083                         }
1084                         else {
1085                                 if(file->flags & HILITE);
1086                                 else {
1087                                         file->flags |= HILITE;
1088                                         redraw|= 2;
1089                                         newi= num;
1090                                 }
1091                         }
1092                 }
1093                 else {
1094                         if(file->flags & HILITE) {
1095                                 file->flags &= ~HILITE;
1096                                 redraw|= 2;
1097                                 old= num;
1098                         }
1099                 }
1100                         
1101         }
1102         // removed frontbuffer draw here
1103         if(redraw) {
1104                 scrarea_queue_winredraw(curarea);
1105         }
1106 }
1107
1108
1109 static void draw_filetext(SpaceFile *sfile)
1110 {
1111         struct direntry *files;
1112         int a, x, y;
1113         short mval[2];
1114         
1115         if(textrct.ymin+10 >= textrct.ymax) return;
1116
1117
1118         /* box */
1119         BIF_ThemeColor(TH_BACK);
1120         glRecti(textrct.xmin,  textrct.ymin,  textrct.xmax,  textrct.ymax);
1121
1122         /* collums */
1123         x= textrct.xmin+collumwidth;
1124         for(a=1; a<sfile->collums; a++, x+= collumwidth) {
1125                 cpack(0x303030);
1126                 sdrawline(x,  textrct.ymin,  x,  textrct.ymax); 
1127                 cpack(0xB0B0B0);
1128                 sdrawline(x+1,  textrct.ymin,  x+1,  textrct.ymax); 
1129         }
1130
1131         if(sfile->filelist==0) return;
1132         
1133         /* test: if mouse is not in area: clear HILITE */
1134         getmouseco_areawin(mval);
1135
1136         if(mval[0]<0 || mval[0]>curarea->winx) {
1137                 files= sfile->filelist+sfile->ofs;
1138                 for(a= sfile->ofs; a<sfile->totfile; a++, files++) files->flags &= ~HILITE;
1139         }
1140         
1141         files= sfile->filelist+sfile->ofs;
1142         for(a= sfile->ofs; a<sfile->totfile; a++, files++) {
1143         
1144                 if( calc_filesel_line(sfile, a, &x, &y)==0 ) break;
1145                 print_line(sfile, files, x, y);
1146         }
1147
1148         /* clear drawing errors, with text at the right hand side: */
1149         BIF_ThemeColor(TH_HEADER);
1150         glRecti(textrct.xmax,  textrct.ymin,  textrct.xmax+10,  textrct.ymax);
1151         uiEmboss(textrct.xmin, textrct.ymin, textrct.xmax, textrct.ymax, 1);
1152 }
1153
1154 void drawfilespace(ScrArea *sa, void *spacedata)
1155 {
1156         SpaceFile *sfile;
1157         uiBlock *block;
1158         float col[3];
1159         int act, loadbutton;
1160         short mval[2];
1161         char name[20];
1162         char *menu;
1163
1164         myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
1165
1166         BIF_GetThemeColor3fv(TH_HEADER, col);   // basic undrawn color is border
1167         glClearColor(col[0], col[1], col[2], 0.0); 
1168         glClear(GL_COLOR_BUFFER_BIT);
1169
1170         sfile= sa->spacedata.first;     
1171         if(sfile->filelist==NULL) {
1172                 read_dir(sfile);
1173                 
1174                 calc_file_rcts(sfile);
1175                 
1176                 /* calculate act */ 
1177                 getmouseco_areawin(mval);
1178                 act= find_active_file(sfile, mval[0], mval[1]);
1179                 if(act>=0 && act<sfile->totfile)
1180                         sfile->filelist[act].flags |= HILITE;
1181         }
1182         else calc_file_rcts(sfile);
1183
1184         /* HEADER */
1185         sprintf(name, "win %d", sa->win);
1186         block= uiNewBlock(&sa->uiblocks, name, UI_EMBOSS, UI_HELV, sa->win);
1187         
1188         uiSetButLock( sfile->type==FILE_MAIN && sfile->returnfunc, NULL);
1189
1190         /* space available for load/save buttons? */
1191         loadbutton= MAX2(80, 20+BMF_GetStringWidth(G.font, sfile->title));
1192         if(textrct.xmax-textrct.xmin > loadbutton+20) {
1193                 if(sfile->title[0]==0) loadbutton= 0;
1194         }
1195         else loadbutton= 0;
1196
1197         uiDefBut(block, TEX,1,"",       textrct.xmin, filebuty1, textrct.xmax-textrct.xmin-loadbutton, 21, sfile->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
1198         uiDefBut(block, TEX,2,"",       textrct.xmin, filebuty2, textrct.xmax-textrct.xmin-loadbutton, 21, sfile->dir, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
1199
1200         if(loadbutton) {
1201                 uiSetCurFont(block, UI_HELV);
1202                 uiDefBut(block, BUT,        5, sfile->title,    textrct.xmax-loadbutton, filebuty2, loadbutton, 21, sfile->dir, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
1203                 uiDefBut(block, BUT,        6, "Cancel",        textrct.xmax-loadbutton, filebuty1, loadbutton, 21, sfile->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
1204         }
1205
1206         menu= fsmenu_build_menu();
1207         if(menu[0])     // happens when no .Bfs is there, and first time browse
1208                 uiDefButS(block, MENU,  3, menu, scrollrct.xmin, filebuty1, scrollrct.xmax-scrollrct.xmin, 21, &sfile->menu, 0, 0, 0, 0, "");
1209         MEM_freeN(menu);
1210
1211         uiDefBut(block, BUT,            4, "P", scrollrct.xmin, filebuty2, scrollrct.xmax-scrollrct.xmin, 21, 0, 0, 0, 0, 0, "Move to the parent directory (PKEY)");
1212
1213         uiDrawBlock(block);
1214
1215         draw_filescroll(sfile);
1216         draw_filetext(sfile);
1217         
1218         /* others diskfree etc ? */
1219         scrarea_queue_headredraw(sa);   
1220         
1221         myortho2(-0.375, (float)(sa->winx)-0.375, -0.375, (float)(sa->winy)-0.375);
1222         draw_area_emboss(sa);
1223         
1224         sa->win_swap= WIN_BACK_OK;
1225 }
1226
1227 static void do_filescroll(SpaceFile *sfile)
1228 {
1229         short mval[2], oldy, yo;
1230         
1231         calc_file_rcts(sfile);
1232         
1233         filescrollselect= 1;
1234         
1235         /* for beauty */
1236         scrarea_do_windraw(curarea);
1237         screen_swapbuffers();
1238         
1239         getmouseco_areawin(mval);
1240         oldy= yo= mval[1];
1241         
1242         while(get_mbut()&L_MOUSE) {
1243                 getmouseco_areawin(mval);
1244                 
1245                 if(yo!=mval[1]) {
1246                         int dy= floor(0.5+((float)(oldy-mval[1]))*pixels_to_ofs);
1247                         
1248                         if(dy) {
1249                                 sfile->ofs+= dy;
1250                                 if(sfile->ofs<0) {
1251                                         sfile->ofs= 0;
1252                                         oldy= mval[1];
1253                                 }
1254                                 else oldy= floor(0.5+ (float)oldy - (float)dy/pixels_to_ofs);
1255         
1256                                 scrarea_do_windraw(curarea);
1257                                 screen_swapbuffers();
1258         
1259                         }
1260                         
1261                         yo= mval[1];
1262                 }
1263                 else BIF_wait_for_statechange();
1264         }
1265         filescrollselect= 0;
1266
1267         /* for beauty */
1268         scrarea_do_windraw(curarea);
1269         screen_swapbuffers();
1270         
1271 }
1272
1273 static void do_filescrollwheel(SpaceFile *sfile, int move)
1274 {
1275         // by phase
1276         int lines, rt;
1277
1278         calc_file_rcts(sfile);
1279
1280         lines = (int)(textrct.ymax-textrct.ymin)/FILESEL_DY;
1281         rt = lines * sfile->collums;
1282
1283         if(sfile->totfile > rt) {
1284                 sfile->ofs+= move;
1285                 if( sfile->ofs + rt > sfile->totfile + 1)
1286                         sfile->ofs = sfile->totfile - rt + 1;
1287         }
1288
1289         if(sfile->ofs<0) {
1290                 sfile->ofs= 0;
1291         }
1292 }
1293
1294 void activate_fileselect(int type, char *title, char *file, void (*func)(char *))
1295 {
1296         SpaceFile *sfile;
1297         char group[24], name[FILE_MAXDIR], temp[FILE_MAXDIR];
1298         
1299         if(curarea==0) return;
1300         if(curarea->win==0) return;
1301         
1302         newspace(curarea, SPACE_FILE);
1303         scrarea_queue_winredraw(curarea);
1304         
1305         /* sometime double, when area already is SPACE_FILE with a different file name */
1306         if(curarea->headwin) addqueue(curarea->headwin, CHANGED, 1);
1307
1308         name[2]= 0;
1309         strcpy(name, file);
1310         
1311         sfile= curarea->spacedata.first;
1312         /* sfile wants a (*)(short), but get (*)(char*) */
1313         sfile->returnfunc= func;
1314         sfile->type= type;
1315         sfile->ofs= 0;
1316         /* sfile->act is used for databrowse: double names of library objects */
1317         sfile->act= -1;
1318         
1319         if(BLI_convertstringcode(name, G.sce, G.scene->r.cfra)) sfile->flag |= FILE_STRINGCODE;
1320         else sfile->flag &= ~FILE_STRINGCODE;
1321
1322         if (U.uiflag & USER_HIDE_DOT)
1323                 sfile->flag |= FILE_HIDE_DOT;
1324
1325         if(type==FILE_MAIN) {
1326                 char *groupname;
1327                 
1328                 strcpy(sfile->file, name+2);
1329
1330                 groupname = BLO_idcode_to_name( GS(name) );
1331                 if (groupname) {
1332                         strcpy(sfile->dir, groupname);
1333                         strcat(sfile->dir, "/");
1334                 }
1335
1336                 /* free all */
1337                 if(sfile->libfiledata) BLO_blendhandle_close(sfile->libfiledata);
1338                 sfile->libfiledata= 0;
1339                 
1340                 freefilelist(sfile);
1341         }
1342         else if(type==FILE_LOADLIB) {
1343                 strcpy(sfile->dir, name);
1344                 if( is_a_library(sfile, temp, group) ) {
1345                         /* force a reload of the library-filelist */
1346                         freefilelist(sfile);
1347                 }
1348                 else {
1349                         split_sfile(sfile, name);
1350                         if(sfile->libfiledata) BLO_blendhandle_close(sfile->libfiledata);
1351                         sfile->libfiledata= NULL;
1352                 }
1353         }
1354         else {  /* FILE_BLENDER */
1355                 split_sfile(sfile, name);       /* test filelist too */
1356                 checkdir(sfile->dir);
1357
1358                 /* free: filelist and libfiledata became incorrect */
1359                 if(sfile->libfiledata) BLO_blendhandle_close(sfile->libfiledata);
1360                 sfile->libfiledata= 0;
1361         }
1362         BLI_strncpy(sfile->title, title, sizeof(sfile->title));
1363         filetoname= 1;
1364 }
1365
1366 void activate_imageselect(int type, char *title, char *file, void (*func)(char *))
1367 {
1368         SpaceImaSel *simasel;
1369         char dir[FILE_MAXDIR], name[FILE_MAXFILE];
1370         
1371         if(curarea==0) return;
1372         if(curarea->win==0) return;
1373         
1374         newspace(curarea, SPACE_IMASEL);
1375         
1376         /* sometimes double, when area is already SPACE_FILE with a different file name */
1377         addqueue(curarea->headwin, CHANGED, 1);
1378         addqueue(curarea->win, CHANGED, 1);
1379
1380         name[2]= 0;
1381         strcpy(name, file);
1382
1383         simasel= curarea->spacedata.first;
1384         simasel->returnfunc= func;
1385
1386         if(BLI_convertstringcode(name, G.sce, G.scene->r.cfra)) simasel->mode |= IMS_STRINGCODE;
1387         else simasel->mode &= ~IMS_STRINGCODE;
1388         
1389         BLI_split_dirfile(name, dir, simasel->file);
1390         checkdir(simasel->dir);
1391         if(strcmp(dir, simasel->dir)!=0) simasel->fase= 0;
1392         strcpy(simasel->dir, dir);
1393         
1394         BLI_strncpy(simasel->title, title, sizeof(simasel->title));
1395
1396         
1397         
1398         /* filetoname= 1; */
1399 }
1400
1401
1402 void activate_databrowse(ID *id, int idcode, int fromcode, int retval, short *menup, void (*func)(unsigned short))
1403 {
1404         ListBase *lb;
1405         SpaceFile *sfile;
1406         char str[32];
1407         
1408         if(id==0) {
1409                 lb= wich_libbase(G.main, idcode);
1410                 id= lb->last;
1411         }
1412         
1413         if(id) strcpy(str, id->name);
1414         else return;
1415         
1416         activate_fileselect(FILE_MAIN, "SELECT DATABLOCK", str, (void (*) (char*))func);
1417         
1418         sfile= curarea->spacedata.first;
1419         sfile->retval= retval;
1420         sfile->ipotype= fromcode;
1421         sfile->menup= menup;
1422 }
1423
1424 void filesel_prevspace()
1425 {
1426         SpaceFile *sfile;
1427         
1428         sfile= curarea->spacedata.first;
1429         if(sfile->next) {
1430         
1431                 BLI_remlink(&curarea->spacedata, sfile);
1432                 BLI_addtail(&curarea->spacedata, sfile);
1433
1434                 sfile= curarea->spacedata.first;
1435
1436                 if (sfile->spacetype == SPACE_SCRIPT) {
1437                         SpaceScript *sc = (SpaceScript *)sfile;
1438                         if (sc->script) sc->script->flags &=~SCRIPT_FILESEL;
1439                 }
1440
1441                 newspace(curarea, sfile->spacetype);
1442         }
1443         else newspace(curarea, SPACE_INFO);
1444 }
1445
1446 static int countselect(SpaceFile *sfile)
1447 {
1448         int a, count=0;
1449
1450         for(a=0; a<sfile->totfile; a++) {
1451                 if(sfile->filelist[a].flags & ACTIVE) {
1452                         count++;
1453                 }
1454         }
1455         return count;
1456 }
1457
1458 static int getotherdir(void)
1459 {
1460         ScrArea *sa;
1461         SpaceFile *sfile=0;
1462         
1463         sa= G.curscreen->areabase.first;
1464         while(sa) {
1465                 if(sa!=curarea) {
1466                         if(sa->spacetype==SPACE_FILE) {
1467                                 
1468                                 /* already found one */
1469                                 if(sfile) return 0;
1470                 
1471                                 sfile= sa->spacedata.first;
1472
1473                                 if(sfile->type & FILE_UNIX) {
1474                                         otherarea= sa;
1475                                         BLI_make_file_string(G.sce, otherdir, sfile->dir, "");
1476                                 }
1477                                 else sfile= 0;
1478                         }
1479                 }
1480                 sa= sa->next;
1481         }
1482         if(sfile) return 1;
1483         return 0;
1484 }
1485
1486 static void reread_other_fs(void)
1487 {
1488         SpaceFile *sfile;
1489         
1490         /* watch it: only call when getotherdir returned OK */
1491         
1492         sfile= otherarea->spacedata.first;
1493         freefilelist(sfile);
1494         scrarea_queue_winredraw(otherarea);
1495 }
1496
1497
1498 void free_filesel_spec(char *dir)
1499 {
1500         /* all filesels with 'dir' are freed */
1501         bScreen *sc;
1502                 
1503         sc= G.main->screen.first;
1504         while(sc) {
1505                 ScrArea *sa= sc->areabase.first;
1506                 while(sa) {
1507                         SpaceLink  *sl= sa->spacedata.first;
1508                         while(sl) {
1509                                 if(sl->spacetype==SPACE_FILE) {
1510                                         SpaceFile *sfile= (SpaceFile*) sl;
1511                                         if (BLI_streq(sfile->dir, dir)) {
1512                                                 freefilelist(sfile);
1513                                         }
1514                                 }
1515                                 sl= sl->next;
1516                         }
1517                         sa= sa->next;
1518                 }
1519                 sc= sc->id.next;
1520         }
1521 }
1522
1523
1524 static void filesel_execute(SpaceFile *sfile)
1525 {
1526         struct direntry *files;
1527         char name[FILE_MAXDIR];
1528         int a;
1529         
1530         filesel_prevspace();
1531
1532         if(sfile->type==FILE_LOADLIB) {
1533                 do_library_append(sfile);
1534                 BIF_undo_push("Append from file");
1535                 allqueue(REDRAWALL, 1);
1536         }
1537         else if(sfile->returnfunc) {
1538                 fsmenu_insert_entry(sfile->dir, 1);
1539         
1540                 if(sfile->type==FILE_MAIN) { /* DATABROWSE */
1541                         if (sfile->menup) {     /* with value pointing to ID block index */
1542                                 int notfound = 1;
1543
1544                                 /*      Need special handling since hiding .* datablocks means that
1545                                         sfile->act is no longer the same as files->nr.
1546
1547                                         Also, toggle HIDE_DOT on and off can make sfile->act not longer
1548                                         correct (meaning it doesn't point to the correct item in the filelist.
1549                                         
1550                                         sfile->file is always correct, so first with check if, for the item
1551                                         corresponding to sfile->act, the name is the same.
1552
1553                                         If it isn't (or if sfile->act is not good), go over filelist and take
1554                                         the correct one.
1555
1556                                         This means that selecting a datablock than hiding it makes it
1557                                         unselectable. Not really a problem.
1558
1559                                         - theeth
1560                                  */
1561
1562                                 *sfile->menup= -1;
1563
1564                                 if(sfile->act>=0) {
1565                                         if(sfile->filelist) {
1566                                                 files= sfile->filelist+sfile->act;
1567                                                 if ( strcmp(files->relname, sfile->file)==0) {
1568                                                         notfound = 0;
1569                                                         *sfile->menup= files->nr;
1570                                                 }
1571                                         }       
1572                                 }
1573                                 if (notfound) {
1574                                         for(a=0; a<sfile->totfile; a++) {
1575                                                 if( strcmp(sfile->filelist[a].relname, sfile->file)==0) {
1576                                                         *sfile->menup= sfile->filelist[a].nr;
1577                                                         break;
1578                                                 }
1579                                         }
1580                                 }
1581                         }
1582                         sfile->returnfunc((char*) (long)sfile->retval);
1583                 }
1584                 else {
1585                         if(strncmp(sfile->title, "Save", 4)==0) free_filesel_spec(sfile->dir);
1586                         if(strncmp(sfile->title, "Export", 6)==0) free_filesel_spec(sfile->dir);
1587                         
1588                         strcpy(name, sfile->dir);
1589                         strcat(name, sfile->file);
1590                         
1591                         if(sfile->flag & FILE_STRINGCODE) BLI_makestringcode(G.sce, name);
1592
1593                         sfile->returnfunc(name);
1594                 }
1595         }
1596 }
1597
1598 static void do_filesel_buttons(short event, SpaceFile *sfile)
1599 {
1600         char butname[FILE_MAXDIR];
1601         
1602         if (event == 1) {
1603                 if (strchr(sfile->file, '*') || strchr(sfile->file, '?') || strchr(sfile->file, '[')) {
1604                         int i, match = FALSE;
1605                         
1606                         for (i = 2; i < sfile->totfile; i++) {
1607                                 if (fnmatch(sfile->file, sfile->filelist[i].relname, 0) == 0) {
1608                                         sfile->filelist[i].flags |= ACTIVE;
1609                                         match = TRUE;
1610                                 }
1611                         }
1612                         if (match) strcpy(sfile->file, "");
1613                         if(sfile->type==FILE_MAIN) filesel_select_objects(sfile);
1614                         scrarea_queue_winredraw(curarea);
1615                 }
1616         }
1617         else if(event== 2) {
1618                 /* reuse the butname variable */
1619                 checkdir(sfile->dir);
1620
1621                 BLI_make_file_string(G.sce, butname, sfile->dir, "");
1622                 /* strip the trailing slash if its a real dir */
1623                 if (strlen(butname)!=1)
1624                         butname[strlen(butname)-1]=0;
1625                 
1626                 if(sfile->type & FILE_UNIX) {
1627                         if (!BLI_exists(butname)) {
1628                                 if (okee("Makedir")) {
1629                                         BLI_recurdir_fileops(butname);
1630                                         if (!BLI_exists(butname)) parent(sfile);
1631                                 } else parent(sfile);
1632                         }
1633                 }
1634                 freefilelist(sfile);
1635                 sfile->ofs= 0;
1636                 scrarea_queue_winredraw(curarea);
1637         }
1638         else if(event== 3) {
1639                 char *selected= fsmenu_get_entry(sfile->menu-1);
1640                 
1641                 /* which string */
1642                 if (selected) {
1643                         strcpy(sfile->dir, selected);
1644                         BLI_make_exist(sfile->dir);
1645                         checkdir(sfile->dir);
1646                         freefilelist(sfile);
1647                         sfile->ofs= 0;
1648                         scrarea_queue_winredraw(curarea);
1649                 }
1650
1651                 sfile->act= -1;
1652                 
1653         }
1654         else if(event== 4) parent(sfile);
1655         else if(event== 5) {
1656                 if(sfile->type) filesel_execute(sfile);
1657         }
1658         else if(event== 6) filesel_prevspace();
1659         
1660 }
1661
1662 /****/
1663
1664 typedef void (*ReplaceFP)(ID *oldblock, ID *newblock);
1665
1666 static void change_id_link(void *linkpv, void *newlinkv) {
1667         ID **linkp= (ID**) linkpv;
1668         ID *newlink= newlinkv;
1669
1670         if (*linkp) {
1671                 (*linkp)->us--;
1672         }
1673         (*linkp)= newlink;
1674         if (newlink) {
1675                 id_us_plus(newlink);
1676         }
1677 }
1678
1679 static void replace_image(ID *oldblock, ID *newblock) {
1680         Image *oldima= (Image*) oldblock;
1681         Image *newima= (Image*) newblock;
1682         bScreen *sc;
1683         Scene *sce;
1684         Tex *tex;
1685         Mesh *me;
1686
1687         for (tex= G.main->tex.first; tex; tex= tex->id.next) {
1688                 if (tex->env && tex->env->type == ENV_LOAD && tex->env->ima == oldima)
1689                         change_id_link(&tex->env->ima, newima);
1690                 if (tex->ima == oldima)
1691                         change_id_link(&tex->ima, newima);
1692         }
1693
1694         for (sce= G.main->scene.first; sce; sce= sce->id.next) {
1695                 if (sce->ima == oldima)
1696                         change_id_link(&sce->ima, newima);
1697         }
1698
1699         for (sc= G.main->screen.first; sc; sc= sc->id.next) {
1700                 ScrArea *sa;
1701
1702                 for (sa= sc->areabase.first; sa; sa= sa->next) {
1703                         SpaceLink *sl;
1704
1705                         for (sl= sa->spacedata.first; sl; sl= sl->next) {
1706                                 if (sl->spacetype == SPACE_VIEW3D) {
1707                                         View3D *v3d= (View3D*) sl;
1708                                         BGpic *bgp= v3d->bgpic;
1709
1710                                         if (bgp && bgp->ima == oldima) 
1711                                                 change_id_link(&bgp->ima, newima);
1712                                 } else if (sl->spacetype == SPACE_IMAGE) {
1713                                         SpaceImage *sima= (SpaceImage*) sl;
1714                                         
1715                                         if (sima->image == oldima)
1716                                                 change_id_link(&sima->image, newima);
1717                                 }
1718                         }
1719                 }
1720         }
1721
1722         for (me= G.main->mesh.first; me; me= me->id.next) {
1723                 TFace *tfaces= me->tface;
1724
1725                 if (tfaces) {
1726                         int i;
1727
1728                         for (i=0; i<me->totface; i++) {
1729                                 TFace *tf= &tfaces[i];
1730
1731                                 if (tf->tpage == oldima) {
1732                                                 /* not change_id_link, tpage's aren't owners :(
1733                                                  * see hack below.
1734                                                  */
1735                                         tf->tpage= newima;
1736                                 }
1737                         }
1738                 }
1739         }
1740
1741                 /* Nasty hack, necessary because tpages don't act
1742                  * as a user, so there lots of image user count
1743                  * munging occurs... this will ensure the image
1744                  * really dies.
1745                  */
1746         oldima->id.us= 0;
1747 }
1748
1749 static void replace_material(ID *oldblock, ID *newblock)
1750 {
1751         Material *old= (Material*) oldblock;
1752         Material *new= (Material*) newblock;
1753         Material ***matarar;
1754         ID *id;
1755         Object *ob;
1756         int a;
1757         
1758         ob= G.main->object.first;
1759         while(ob) {
1760                 if(ob->totcol && ob->id.lib==0) {
1761                         matarar= give_matarar(ob);
1762                         for(a=1; a<=ob->totcol; a++) {
1763                                 if(ob->mat[a-1] == old) {
1764                                         if(old) old->id.us--;
1765                                         id_us_plus((ID *)new);
1766                                         ob->mat[a-1]= new;
1767                                 }
1768                                 id= ob->data;
1769                                 if( (*matarar)[a-1] == old  && id->lib==0) {
1770                                         if(old) old->id.us--;
1771                                         id_us_plus((ID *)new);
1772                                         (*matarar)[a-1]= new;
1773                                 }
1774                         }
1775                 }
1776                 ob= ob->id.next;
1777         }
1778 }
1779
1780 static ReplaceFP get_id_replace_function(int idcode) {
1781         switch (idcode) {
1782         case ID_MA:
1783                 return &replace_material;
1784         case ID_IM:
1785                 return &replace_image;
1786         default:
1787                 return NULL;
1788         }
1789 }
1790
1791 static void databrowse_replace(SpaceFile *sfile, int idcode)
1792 {
1793         ReplaceFP replace_func= get_id_replace_function(idcode);
1794
1795         if (!replace_func) {
1796                 error("Replacing %s blocks is unsupported", BLO_idcode_to_name(idcode));
1797         } else if (sfile->act==-1) {
1798                 error("Select target with leftmouse");
1799         } else {
1800                 ID *target= (ID*) sfile->filelist[sfile->act].poin;
1801
1802                 if (target) {
1803                         char buf[128];
1804
1805                         sprintf(buf, "Replace with %s: %s", BLO_idcode_to_name(idcode), target->name+2);
1806
1807                         if (okee(buf)) {
1808                                 int i;
1809
1810                                 for (i = 0; i <sfile->totfile; i++)
1811                                         if ((sfile->filelist[i].flags&ACTIVE) && sfile->filelist[i].poin!=target)
1812                                                 replace_func(sfile->filelist[i].poin, target);
1813                         }
1814                 }
1815         }
1816
1817         freefilelist(sfile);
1818         scrarea_queue_winredraw(curarea);
1819 }
1820
1821 static void fs_fake_users(SpaceFile *sfile)
1822 {
1823         ID *id;
1824         int a;
1825         
1826         /* only for F4 DATABROWSE */
1827         if(sfile->returnfunc) return;
1828         
1829         for(a=0; a<sfile->totfile; a++) {
1830                 if(sfile->filelist[a].flags & ACTIVE) {
1831                         id= (ID *)sfile->filelist[a].poin;
1832                         if(id) {
1833                                 if( id->flag & LIB_FAKEUSER) {
1834                                         id->flag -= LIB_FAKEUSER;
1835                                         id->us--;
1836                                 }
1837                                 else {
1838                                         id->flag |= LIB_FAKEUSER;
1839                                         id->us++;
1840                                 }
1841                         }
1842                 }
1843         }
1844         freefilelist(sfile);
1845         scrarea_queue_winredraw(curarea);
1846 }
1847
1848
1849 static int get_hilited_entry(SpaceFile *sfile)
1850 {
1851         int a;
1852
1853         for(a=0; a<sfile->totfile; a++) {
1854                 if(sfile->filelist[a].flags & HILITE) {
1855                         return a;
1856                 }
1857         }
1858         return -1;
1859 }
1860
1861
1862 void winqreadfilespace(ScrArea *sa, void *spacedata, BWinEvent *evt)
1863 {
1864         unsigned short event= evt->event;
1865         short val= evt->val;
1866         static int acto=0;
1867         SpaceFile *sfile;
1868         int act, do_draw= 0, i, test, ret = 0;
1869         short qual, mval[2];
1870         char str[FILE_MAXDIR+FILE_MAXFILE+12];
1871         
1872         sfile= curarea->spacedata.first;
1873         if(sfile==0) return;
1874         if(sfile->filelist==0) {
1875                 return;
1876         }
1877         
1878         if(curarea->win==0) return;
1879         calc_file_rcts(sfile);
1880         getmouseco_areawin(mval);
1881
1882         /* prevent looping */
1883         if(selecting && !(get_mbut() & R_MOUSE)) selecting= 0;
1884
1885         if(val) {
1886
1887                 if( event!=RETKEY && event!=PADENTER)
1888                         if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
1889
1890                 switch(event) {
1891                 
1892                 case UI_BUT_EVENT:
1893                         do_filesel_buttons(val, sfile);
1894                         break;          
1895                 
1896                 case WHEELDOWNMOUSE:
1897                         do_filescrollwheel(sfile, U.wheellinescroll);
1898                         act= find_active_file(sfile, mval[0], mval[1]);
1899                         set_active_file(sfile, act);
1900                         do_draw= 1;
1901                         break;
1902                 case WHEELUPMOUSE:
1903                         do_filescrollwheel(sfile, -U.wheellinescroll);
1904                         act= find_active_file(sfile, mval[0], mval[1]);
1905                         set_active_file(sfile, act);
1906                         do_draw= 1;
1907                         break;
1908
1909                 case LEFTMOUSE:
1910                 case MIDDLEMOUSE:
1911                         if(mval[0]>scrollrct.xmin && mval[0]<scrollrct.xmax && mval[1]>scrollrct.ymin && mval[1]<scrollrct.ymax) {
1912                                 do_filescroll(sfile);
1913                         }
1914                         else if(mval[0]>textrct.xmin && mval[0]<textrct.xmax && mval[1]>textrct.ymin && mval[1]<textrct.ymax) {
1915                                 
1916                                 /* sfile->act is used in databrowse: double names of library objects */
1917                                 
1918                                 sfile->act= act= find_active_file(sfile, mval[0], mval[1]);
1919                                 
1920                                 if(act>=0 && act<sfile->totfile) {
1921                                         if(S_ISDIR(sfile->filelist[act].type)) {
1922                                                 strcat(sfile->dir, sfile->filelist[act].relname);
1923                                                 strcat(sfile->dir,"/");
1924                                                 checkdir(sfile->dir);
1925                                                 freefilelist(sfile);
1926                                                 sfile->ofs= 0;
1927                                                 do_draw= 1;
1928                                         }
1929                                         else {
1930                                                 if( strcmp(sfile->file, sfile->filelist[act].relname)) {
1931                                                         do_draw= 1;
1932                                                         strcpy(sfile->file, sfile->filelist[act].relname);
1933                                                 }
1934                                                 if(event==MIDDLEMOUSE && sfile->type) filesel_execute(sfile);
1935                                         }
1936                                 }
1937                         }
1938                         break;
1939                 case RIGHTMOUSE:
1940                         act= find_active_file(sfile, mval[0], mval[1]);
1941                         acto= act;
1942                         if(act>=0 && act<sfile->totfile) {
1943
1944                                 if (sfile->filelist[act].flags & ACTIVE) {
1945                                         sfile->filelist[act].flags &= ~ACTIVE;
1946                                         selecting = INACTIVATE;
1947                                 }
1948                                 else {
1949                                         test= sfile->filelist[act].relname[0];
1950                                         if (act>=2 || test!='.') sfile->filelist[act].flags |= ACTIVE;
1951                                         
1952                                         selecting = ACTIVATE;
1953                                 }
1954                                 do_draw= 1;
1955                         }
1956                         break;
1957                 case MOUSEY:
1958                         act= find_active_file(sfile, mval[0], mval[1]);
1959                         if (act!=acto) {
1960                                 set_active_file(sfile, act);
1961                         }
1962                         if(selecting && act!=acto) {
1963                                         
1964                                 while(1) {
1965                                         if (acto >= 2 && acto < sfile->totfile) {
1966                                                 if (selecting == ACTIVATE) sfile->filelist[acto].flags |= ACTIVE;
1967                                                 else if (selecting == INACTIVATE) sfile->filelist[acto].flags &= ~ACTIVE;
1968                                         }
1969                                         if (acto < act) acto++;
1970                                         else if (acto > act) acto--;
1971                                         else break;
1972                                         
1973                                 }
1974
1975                         }
1976                         acto= act;
1977                         break;
1978                 
1979                 case PAGEUPKEY:
1980                         sfile->ofs-= page_ofs;
1981                         do_draw= 1;
1982                         break;
1983                 case PAGEDOWNKEY:
1984                         sfile->ofs+= page_ofs;
1985                         do_draw= 1;
1986                         break;
1987                 case HOMEKEY:
1988                         sfile->ofs= 0;
1989                         do_draw= 1;
1990                         break;
1991                 case ENDKEY:
1992                         sfile->ofs= sfile->totfile;
1993                         do_draw= 1;
1994                         break;
1995                 
1996                 case AKEY:
1997                         swapselect_file(sfile);
1998                         if(sfile->type==FILE_MAIN) filesel_select_objects(sfile);
1999                         do_draw= 1;
2000                         break;
2001                         
2002                 case BKEY:
2003                 case CKEY:
2004                 case LKEY:
2005                         if(event==LKEY && sfile->type==FILE_MAIN && (G.qual & LR_CTRLKEY)) {
2006                                 databrowse_replace(sfile, groupname_to_code(sfile->dir));
2007                                 break;
2008                         }
2009                         /* pass */
2010                 case MKEY:
2011                         if(sfile->type==FILE_MAIN) break;
2012
2013                         if(!countselect(sfile)) {
2014                                 error("No files selected");
2015                                 break;
2016                         }
2017                         
2018                         if(!getotherdir()) {
2019                                 error("No second fileselect");
2020                                 break;
2021                         }
2022                         
2023                         if (!strcmp(sfile->dir, otherdir)) {
2024                                 error("Same directories");
2025                                 break;
2026                         }
2027
2028                         if(event==BKEY) sprintf(str, "Backup to %s", otherdir);
2029                         else if(event==CKEY) sprintf(str, "Copy to %s", otherdir);
2030                         else if(event==LKEY) sprintf(str, "Linked copy to %s", otherdir);
2031                         else if(event==MKEY) sprintf(str, "Move to %s", otherdir);
2032                                         
2033                         if (!okee(str)) break;
2034
2035                         for (i = 0; i<sfile->totfile; i++){
2036                                 if (sfile->filelist[i].flags & ACTIVE) {                        
2037                                         BLI_make_file_string(G.sce, str, sfile->dir, sfile->filelist[i].relname);
2038
2039                                         if(event==BKEY) ret= BLI_backup(sfile->filelist[i].relname, sfile->dir, otherdir);
2040                                         else if(event==CKEY) ret= BLI_copy_fileops(str, otherdir);
2041                                         else if(event==LKEY) ret= BLI_link(str, otherdir);
2042                                         else if(event==MKEY) ret= BLI_move(str, otherdir);
2043
2044                                         if (ret) {error("Command failed, see console"); break;}
2045                                         else sfile->filelist[i].flags &= ~ACTIVE;
2046                                 }
2047                         }
2048                         do_draw= 1;
2049                         if(event==BKEY || event==MKEY) 
2050                                 freefilelist(sfile);
2051                                 
2052                         reread_other_fs();
2053                         
2054                         break;
2055
2056                 case XKEY:
2057                         test = get_hilited_entry(sfile);
2058
2059                         if (test != -1 && !(S_ISDIR(sfile->filelist[test].type))){
2060                                 BLI_make_file_string(G.sce, str, sfile->dir, sfile->filelist[test].relname);
2061
2062                                 if( okee("Remove %s", str) ) {
2063                                         ret = BLI_delete(str, 0, 0);
2064                                         if (ret) {
2065                                                 error("Command failed, see console");
2066                                         } else {
2067                                                 freefilelist(sfile);
2068                                                 do_draw= 1;
2069                                         }
2070                                 }
2071                         }
2072                         break;
2073
2074                 case RKEY:
2075                         if(sfile->type==FILE_MAIN) {
2076                                 databrowse_replace(sfile, groupname_to_code(sfile->dir));
2077                                 break;
2078                         }
2079                         /* pass to TKEY! */
2080                         
2081                 case TKEY:
2082                         if(sfile->type==FILE_MAIN) break;
2083                         
2084                         if(!countselect(sfile)) {
2085                                 error("No files selected");
2086                                 break;
2087                         }
2088
2089                         if(event==TKEY) sprintf(str, "Touch");
2090                         else if(event==RKEY) sprintf(str, "Remove from %s", sfile->dir);
2091                         
2092                         qual= G.qual;   /* because after okee() you released the SHIFT */
2093                         if (!okee(str)) break;
2094                         
2095                         for (i = 0; i <sfile->totfile; i++) {
2096                                 if (sfile->filelist[i].flags & ACTIVE) {
2097                                         BLI_make_file_string(G.sce, str, sfile->dir, sfile->filelist[i].relname);
2098
2099                                         if(event==TKEY) ret= BLI_touch(str);
2100                                         else if(event==RKEY) {
2101                                                 if(qual & LR_SHIFTKEY) ret= BLI_delete(str, 0, 1);
2102                                                 else if(S_ISDIR(sfile->filelist[i].type)) ret= BLI_delete(str, 1, 0);
2103                                                 else ret= BLI_delete(str, 0, 0);
2104                                         }
2105
2106                                         if (ret) {error("Command failed, see console"); break;}
2107                                         else sfile->filelist[i].flags &= ~ACTIVE;
2108                                 }
2109                         }
2110                         do_draw= 1;
2111                         freefilelist(sfile);
2112
2113                         break;
2114                                 
2115                 case PKEY:
2116                         if(G.qual & LR_SHIFTKEY) {
2117                                 extern char bprogname[];        /* usiblender.c */
2118                         
2119                                 sprintf(str, "%s -a \"%s%s\"", bprogname, sfile->dir, sfile->file);
2120                                 system(str);
2121                         }
2122                         else 
2123                                 parent(sfile);
2124                                 
2125                         break;
2126
2127                 case IKEY:
2128                         if(sfile->type==FILE_MAIN) break;
2129                         
2130                         sprintf(str, "$IMAGEEDITOR %s%s", sfile->dir, sfile->file);
2131                         system(str);
2132                         break;
2133                 
2134                 case EKEY:
2135                         if(sfile->type==FILE_MAIN) break;
2136                         
2137                         sprintf(str, "$WINEDITOR %s%s", sfile->dir, sfile->file);
2138                         system(str);
2139                         break;
2140                 
2141                 case FKEY:
2142                         if(sfile->type==FILE_MAIN) {
2143                                 fs_fake_users(sfile);
2144                         }
2145                         break;
2146                                 
2147                 case PADPLUSKEY:
2148                 case EQUALKEY:
2149                         if (G.qual & LR_CTRLKEY) BLI_newname(sfile->file, +100);
2150                         else if (G.qual & LR_SHIFTKEY) BLI_newname(sfile->file, +10);
2151                         else BLI_newname(sfile->file, +1);
2152                         
2153                         do_draw= 1;
2154                         break;
2155                         
2156                 case PADMINUS:
2157                 case MINUSKEY:
2158                         if (G.qual & LR_CTRLKEY) BLI_newname(sfile->file, -100);
2159                         else if (G.qual & LR_SHIFTKEY) BLI_newname(sfile->file, -10);
2160                         else BLI_newname(sfile->file, -1);
2161                         
2162                         do_draw= 1;
2163                         break;
2164                         
2165                 case BACKSLASHKEY:
2166                 case SLASHKEY:
2167                         if(sfile->type==FILE_MAIN) break;
2168
2169 #ifdef WIN32
2170                         strcpy(sfile->dir, "\\");
2171 #else
2172                         strcpy(sfile->dir, "/");
2173 #endif
2174                         freefilelist(sfile);
2175                         sfile->ofs= 0;
2176                         do_draw= 1;
2177                         break;
2178                 case PERIODKEY:
2179                         freefilelist(sfile);
2180                         do_draw= 1;
2181                         break;
2182                 case ESCKEY:
2183                         filesel_prevspace();
2184                         break;
2185                 case PADENTER:
2186                 case RETKEY:
2187                         if(sfile->type) filesel_execute(sfile);
2188                         break;
2189                 }
2190         }
2191         else if(event==RIGHTMOUSE) {
2192                 selecting = NOTACTIVE;
2193                 if(sfile->type==FILE_MAIN) filesel_select_objects(sfile);
2194         }
2195         else if(event==LEFTMOUSE) {
2196                 if(sfile->type==FILE_MAIN) active_file_object(sfile);
2197         }
2198
2199                 /* XXX, stupid patch, curarea can become undone
2200                  * because of file loading... fixme zr
2201                  */
2202         if(do_draw && curarea) scrarea_queue_winredraw(curarea);
2203 }
2204
2205
2206
2207
2208 /* ************* LIBRARY FILESEL ******************* */
2209
2210 static int groupname_to_code(char *group)
2211 {
2212         char buf[32];
2213         char *lslash;
2214         
2215         BLI_strncpy(buf, group, 31);
2216         lslash= BLI_last_slash(buf);
2217         if (lslash)
2218                 lslash[0]= '\0';
2219
2220         return BLO_idcode_from_name(buf);
2221 }
2222
2223 static int is_a_library(SpaceFile *sfile, char *dir, char *group)
2224 {
2225         /* return ok when a blenderfile, in dir is the filename,
2226          * in group the type of libdata
2227          */
2228         int len;
2229         char *fd;
2230         
2231         strcpy(dir, sfile->dir);
2232         len= strlen(dir);
2233         if(len<7) return 0;
2234         if( dir[len-1] != '/' && dir[len-1] != '\\') return 0;
2235                 
2236         group[0]= 0;
2237         dir[len-1]= 0;
2238
2239         /* Find the last slash */
2240         fd= (strrchr(dir, '/')>strrchr(dir, '\\'))?strrchr(dir, '/'):strrchr(dir, '\\');
2241
2242         if(fd==0) return 0;
2243         *fd= 0;
2244         if(BLO_has_bfile_extension(fd+1)) {
2245                 *fd= '/';
2246         }
2247         else {
2248                 strcpy(group, fd+1);
2249                         
2250                 /* Find the last slash */
2251                 fd= (strrchr(dir, '/')>strrchr(dir, '\\'))?strrchr(dir, '/'):strrchr(dir, '\\');
2252                 if (!fd || !BLO_has_bfile_extension(fd+1)) return 0;
2253         }
2254         return 1;
2255 }
2256
2257 /* orange hack... :) */
2258 static void do_sync_pose(Library *lib)
2259 {
2260         Object *ob, *obt;
2261         Base *base;
2262         bArmature *arm;
2263         
2264         /* find the armature and the pose from library */
2265         for(ob= G.main->object.first; ob; ob= ob->id.next)
2266                 if(ob->type==OB_ARMATURE && ob->id.lib==lib)
2267                         break;
2268         
2269         if(ob==NULL || ob->pose==NULL) {
2270                 error("No pose appended");
2271                 return;
2272         }
2273         
2274         arm= ob->data;
2275
2276         /* for all visible objects in this scene */
2277         for(base= G.scene->base.first; base; base= base->next) {
2278                 if((base->flag & SELECT)) {
2279                         obt= base->object;
2280                         if(obt->type==OB_ARMATURE && obt->pose && ob!=obt) {
2281                                 char str[128];
2282                                 
2283                                 sprintf(str, "Replace Object %s", obt->id.name);
2284                                 if(okee(str)) {
2285                                         bPoseChannel *chan;
2286                                         bArmature *oldarm= obt->data;
2287                                         
2288                                         /* link armature */
2289                                         oldarm->id.us--;
2290                                         obt->data= arm;
2291                                         arm->id.us++;
2292                                         
2293                                         /* link pose */
2294                                         free_pose_channels(obt->pose);
2295                                         MEM_freeN(obt->pose);
2296                                         copy_pose(&obt->pose, ob->pose, 1);
2297                                         
2298                                         /* relink */
2299                                         ob->id.newid= &obt->id;
2300                                         for (chan = obt->pose->chanbase.first; chan; chan=chan->next){
2301                                                 relink_constraints(&chan->constraints);
2302                                         }
2303                                         
2304                                         obt->pose->flag |= POSE_RECALC;
2305                                         obt->recalc |= OB_RECALC_DATA;
2306                                 }
2307                         }
2308                 }
2309         }
2310
2311         /* prevent saving in file, unlink from scene */
2312         for(base= G.scene->base.first; base; base= base->next) {
2313                 if(base->object==ob)
2314                         break;
2315         }
2316         
2317         if(base) {
2318                 free_and_unlink_base(base);
2319         }
2320
2321         DAG_scene_sort(G.scene);        // for accidentally appended other objects
2322 }
2323
2324 static void do_library_append(SpaceFile *sfile)
2325 {
2326         Library *lib;
2327         char dir[FILE_MAXDIR], group[32];
2328         
2329         if ( is_a_library(sfile, dir, group)==0 ) {
2330                 error("Not a library");
2331         } else if (!sfile->libfiledata) {
2332                 error("Library not loaded");
2333         } else if (group[0]==0) {
2334                 error("Nothing indicated");
2335         } else if (BLI_streq(G.main->name, dir)) {
2336                 error("Cannot use current file as library");
2337         } else {
2338                 Object *ob;
2339                 int idcode = groupname_to_code(group);
2340                 
2341                 BLO_library_append(sfile, dir, idcode); /* warning; if relative, it changes the *dir to relative path */
2342
2343                 /* DISPLISTS? */
2344                 ob= G.main->object.first;
2345                 while(ob) {
2346                         if(ob->id.lib) {
2347                                 ob->recalc |= OB_RECALC;
2348                         }
2349                         ob= ob->id.next;
2350                 }
2351         
2352                 /* and now find the latest append lib file */
2353                 lib= G.main->library.first;
2354                 while(lib) {
2355                         if (BLI_streq(dir, lib->name)) break;
2356                         lib= lib->id.next;
2357                 }
2358                 
2359                 if(lib) {
2360                         if(sfile->flag & FILE_SYNCPOSE)
2361                                 do_sync_pose(lib);
2362                         if((sfile->flag & FILE_LINK)==0) 
2363                                 all_local(lib);
2364                 }
2365                 
2366                 /* in sfile->dir is the whole lib name */
2367                 strcpy(G.lib, sfile->dir);
2368                 
2369         }
2370 }
2371
2372 static void library_to_filelist(SpaceFile *sfile)
2373 {
2374         LinkNode *l, *names;
2375         int ok, i, nnames, idcode;
2376         char filename[FILE_MAXDIR+FILE_MAXFILE];
2377         char dir[FILE_MAXDIR], group[24];
2378         
2379         /* name test */
2380         ok= is_a_library(sfile, dir, group);
2381         if (!ok) {
2382                 /* free */
2383                 if(sfile->libfiledata) BLO_blendhandle_close(sfile->libfiledata);
2384                 sfile->libfiledata= 0;
2385                 return;
2386         }
2387         
2388         BLI_strncpy(filename, G.sce, sizeof(filename)); // G.sce = last file loaded, for UI
2389         
2390         /* there we go */
2391         /* for the time being only read filedata when libfiledata==0 */
2392         if (sfile->libfiledata==0) {
2393                 sfile->libfiledata= BLO_blendhandle_from_file(dir);     // this sets G.sce, we dont want it
2394                 
2395                 if(sfile->libfiledata==0) return;
2396         }
2397         
2398         idcode= groupname_to_code(group);
2399
2400                 // memory for strings is passed into filelist[i].relname
2401                 // and free'd in freefilelist
2402         if (idcode) {
2403                 names= BLO_blendhandle_get_datablock_names(sfile->libfiledata, idcode);
2404         } else {
2405                 names= BLO_blendhandle_get_linkable_groups(sfile->libfiledata);
2406         }
2407         
2408         nnames= BLI_linklist_length(names);
2409
2410         sfile->totfile= nnames + 2;
2411         sfile->filelist= malloc(sfile->totfile * sizeof(*sfile->filelist));
2412         memset(sfile->filelist, 0, sfile->totfile * sizeof(*sfile->filelist));
2413
2414         sfile->filelist[0].relname= BLI_strdup(".");
2415         sfile->filelist[0].type |= S_IFDIR;
2416         sfile->filelist[1].relname= BLI_strdup("..");
2417         sfile->filelist[1].type |= S_IFDIR;
2418                 
2419         for (i=0, l= names; i<nnames; i++, l= l->next) {
2420                 char *blockname= BLI_strdup(l->link); 
2421
2422                 sfile->filelist[i + 2].relname= blockname;
2423                 if (!idcode)
2424                         sfile->filelist[i + 2].type |= S_IFDIR;
2425         }
2426                 
2427         BLI_linklist_free(names, free);
2428         
2429         qsort(sfile->filelist, sfile->totfile, sizeof(struct direntry), compare_name);
2430         
2431         sfile->maxnamelen= 0;
2432         for(i=0; i<sfile->totfile; i++) {
2433                 int len = BMF_GetStringWidth(G.font, sfile->filelist[i].relname);
2434                 if (len > sfile->maxnamelen)
2435                         sfile->maxnamelen = len;
2436         }
2437         
2438         BLI_strncpy(G.sce, filename, sizeof(filename)); // prevent G.sce to change
2439
2440 }
2441
2442 /* ******************* DATA SELECT ********************* */
2443
2444 static void filesel_select_objects(SpaceFile *sfile)
2445 {
2446         Object *ob;
2447         Base *base;
2448         Scene *sce;
2449         int a;
2450         
2451         /* only when F4 DATABROWSE */
2452         if(sfile->returnfunc) return;
2453         
2454         if( strcmp(sfile->dir, "Object/")==0 ) {
2455                 for(a=0; a<sfile->totfile; a++) {
2456                         
2457                         ob= (Object *)sfile->filelist[a].poin;
2458                         
2459                         if(ob) {
2460                                 if(sfile->filelist[a].flags & ACTIVE) ob->flag |= SELECT;
2461                                 else ob->flag &= ~SELECT;
2462                         }
2463
2464                 }
2465                 base= FIRSTBASE;
2466                 while(base) {
2467                         base->flag= base->object->flag;
2468                         base= base->next;
2469                 }
2470                 countall();
2471                 allqueue(REDRAWVIEW3D, 0);
2472                 allqueue(REDRAWOOPS, 0);
2473         }
2474         else if( strcmp(sfile->dir, "Scene/")==0 ) {
2475                 
2476                 for(a=0; a<sfile->totfile; a++) {
2477                         
2478                         sce= (Scene *)sfile->filelist[a].poin;
2479                         if(sce) {
2480                                 if(sfile->filelist[a].flags & ACTIVE) sce->r.scemode |= R_BG_RENDER;
2481                                 else sce->r.scemode &= ~R_BG_RENDER;
2482                         }
2483
2484                 }
2485                 allqueue(REDRAWBUTSSCENE, 0);
2486         }
2487 }
2488
2489 static void active_file_object(SpaceFile *sfile)
2490 {
2491         Object *ob;
2492         
2493         /* only when F4 DATABROWSE */
2494         if(sfile->returnfunc) return;
2495         
2496         if( strcmp(sfile->dir, "Object/")==0 ) {
2497                 if(sfile->act >= 0) {
2498                         
2499                         ob= (Object *)sfile->filelist[sfile->act].poin;
2500                         
2501                         if(ob) {
2502                                 set_active_object(ob);
2503                                 if(BASACT && BASACT->object==ob) {
2504                                         BASACT->flag |= SELECT;
2505                                         sfile->filelist[sfile->act].flags |= ACTIVE;
2506                                         allqueue(REDRAWVIEW3D, 0);
2507                                         allqueue(REDRAWOOPS, 0);
2508                                         scrarea_queue_winredraw(curarea);
2509                                 }
2510                         }
2511                 }
2512         }
2513 }
2514
2515
2516 void main_to_filelist(SpaceFile *sfile)
2517 {
2518         ID *id;
2519         struct direntry *files, *firstlib = NULL;
2520         ListBase *lb;
2521         int a, fake, idcode, len, ok, totlib, totbl;
2522         short hide = 0;
2523
2524         if (sfile->flag & FILE_HIDE_DOT)
2525                 hide = 1;
2526
2527         if(sfile->dir[0]=='/') sfile->dir[0]= 0;
2528         
2529         if(sfile->dir[0]) {
2530                 idcode= groupname_to_code(sfile->dir);
2531                 if(idcode==0) sfile->dir[0]= 0;
2532         }
2533         
2534         if( sfile->dir[0]==0) {
2535                 
2536                 /* make directories */
2537                 sfile->totfile= 21;
2538                 sfile->filelist= (struct direntry *)malloc(sfile->totfile * sizeof(struct direntry));
2539                 
2540                 for(a=0; a<sfile->totfile; a++) {
2541                         memset( &(sfile->filelist[a]), 0 , sizeof(struct direntry));
2542                         sfile->filelist[a].type |= S_IFDIR;
2543                 }
2544                 
2545                 sfile->filelist[0].relname= BLI_strdup("..");
2546                 sfile->filelist[1].relname= BLI_strdup(".");
2547                 sfile->filelist[2].relname= BLI_strdup("Scene");
2548                 sfile->filelist[3].relname= BLI_strdup("Object");
2549                 sfile->filelist[4].relname= BLI_strdup("Mesh");
2550                 sfile->filelist[5].relname= BLI_strdup("Curve");
2551                 sfile->filelist[6].relname= BLI_strdup("Metaball");
2552                 sfile->filelist[7].relname= BLI_strdup("Material");
2553                 sfile->filelist[8].relname= BLI_strdup("Texture");
2554                 sfile->filelist[9].relname= BLI_strdup("Image");
2555                 sfile->filelist[10].relname= BLI_strdup("Wave");
2556                 sfile->filelist[11].relname= BLI_strdup("Lattice");
2557                 sfile->filelist[12].relname= BLI_strdup("Lamp");
2558                 sfile->filelist[13].relname= BLI_strdup("Camera");
2559                 sfile->filelist[14].relname= BLI_strdup("Ipo");
2560                 sfile->filelist[15].relname= BLI_strdup("World");
2561                 sfile->filelist[16].relname= BLI_strdup("Screen");
2562                 sfile->filelist[17].relname= BLI_strdup("VFont");
2563                 sfile->filelist[18].relname= BLI_strdup("Text");
2564                 sfile->filelist[19].relname= BLI_strdup("Armature");
2565                 sfile->filelist[20].relname= BLI_strdup("Action");
2566                 qsort(sfile->filelist, sfile->totfile, sizeof(struct direntry), compare_name);
2567         }
2568         else {
2569
2570                 /* make files */
2571                 idcode= groupname_to_code(sfile->dir);
2572                 
2573                 lb= wich_libbase(G.main, idcode );
2574                 if(lb==0) return;
2575                 
2576                 id= lb->first;
2577                 sfile->totfile= 0;
2578                 while(id) {
2579                         if(sfile->returnfunc && idcode==ID_IP) {
2580                                 if(sfile->ipotype== ((Ipo *)id)->blocktype) sfile->totfile++;
2581                         }
2582                         else if (hide==0 || id->name[2] != '.')
2583                                 sfile->totfile++;
2584
2585                         id= id->next;
2586                 }
2587                 
2588                 if(sfile->returnfunc==0) sfile->totfile+= 2;
2589                 sfile->filelist= (struct direntry *)malloc(sfile->totfile * sizeof(struct direntry));
2590                 
2591                 files= sfile->filelist;
2592                 
2593                 if(sfile->returnfunc==0) {
2594                         memset( &(sfile->filelist[0]), 0 , sizeof(struct direntry));
2595                         sfile->filelist[0].relname= BLI_strdup(".");
2596                         sfile->filelist[0].type |= S_IFDIR;
2597                         memset( &(sfile->filelist[1]), 0 , sizeof(struct direntry));
2598                         sfile->filelist[1].relname= BLI_strdup("..");
2599                         sfile->filelist[1].type |= S_IFDIR;
2600                 
2601                         files+= 2;
2602                 }
2603                 
2604                 id= lb->first;
2605                 totlib= totbl= 0;
2606                 
2607                 while(id) {
2608                         
2609                         ok= 0;
2610                         if(sfile->returnfunc && idcode==ID_IP) {
2611                                 if(sfile->ipotype== ((Ipo *)id)->blocktype) ok= 1;
2612                         }
2613                         else ok= 1;
2614                         
2615                         if(ok) {
2616                 
2617                                 if (hide==0 || id->name[2] != '.') {
2618                                         memset( files, 0 , sizeof(struct direntry));
2619                                         files->relname= BLI_strdup(id->name+2);
2620                                         
2621                                         if(sfile->returnfunc==0) { /* F4 DATA BROWSE */
2622                                                 if(idcode==ID_OB) {
2623                                                         if( ((Object *)id)->flag & SELECT) files->flags |= ACTIVE;
2624                                                 }
2625                                                 else if(idcode==ID_SCE) {
2626                                                         if( ((Scene *)id)->r.scemode & R_BG_RENDER) files->flags |= ACTIVE;
2627                                                 }
2628                                         }
2629                                         files->nr= totbl+1;
2630                                         files->poin= id;
2631                                         fake= id->flag & LIB_FAKEUSER;
2632                                         
2633                                         if(id->lib && fake) sprintf(files->extra, "LF %d", id->us);
2634                                         else if(id->lib) sprintf(files->extra, "L    %d", id->us);
2635                                         else if(fake) sprintf(files->extra, "F    %d", id->us);
2636                                         else sprintf(files->extra, "      %d", id->us);
2637                                         
2638                                         if(id->lib) {
2639                                                 if(totlib==0) firstlib= files;
2640                                                 totlib++;
2641                                         }
2642                                         
2643                                         files++;
2644                                 }
2645                                 totbl++;
2646                         }
2647                         
2648                         id= id->next;
2649                 }
2650                 
2651                 /* only qsort of libraryblokken */
2652                 if(totlib>1) {
2653                         qsort(firstlib, totlib, sizeof(struct direntry), compare_name);
2654                 }
2655         }
2656
2657         sfile->maxnamelen= 0;
2658         for(a=0; a<sfile->totfile; a++) {
2659                 len = BMF_GetStringWidth(G.font, sfile->filelist[a].relname);
2660                 if (len > sfile->maxnamelen) sfile->maxnamelen = len;
2661                 
2662                 if(filetoname) {
2663                         if( strcmp(sfile->file, sfile->filelist[a].relname)==0) {
2664                                 sfile->ofs= a-( sfile->collums*(curarea->winy-FILESELHEAD-10)/(2*FILESEL_DY));
2665                                 filetoname= 0;
2666                                 if(sfile->returnfunc) sfile->filelist[a].flags |= ACTIVE;
2667                         }
2668                 }
2669         }
2670 }
2671
2672
2673 void clever_numbuts_filesel()
2674 {
2675         SpaceFile *sfile;
2676         char orgname[FILE_MAXDIR+FILE_MAXFILE+12];
2677         char filename[FILE_MAXDIR+FILE_MAXFILE+12];
2678         char newname[FILE_MAXDIR+FILE_MAXFILE+12];
2679         int test;
2680         int len;
2681         
2682         sfile= curarea->spacedata.first;
2683
2684         if(sfile->type==FILE_MAIN) return;
2685         
2686         len = 110;
2687         test = get_hilited_entry(sfile);
2688
2689         if (test != -1 && !(S_ISDIR(sfile->filelist[test].type))){
2690                 BLI_make_file_string(G.sce, orgname, sfile->dir, sfile->filelist[test].relname);
2691                 strcpy(filename, sfile->filelist[test].relname);
2692                 
2693                 add_numbut(0, TEX, "", 0, len, filename, "Rename File");
2694
2695                 if( do_clever_numbuts("Rename File", 1, REDRAW) ) {
2696                         BLI_make_file_string(G.sce, newname, sfile->dir, filename);
2697
2698                         if( strcmp(orgname, newname) != 0 ) {
2699                                 BLI_rename(orgname, newname);
2700                                 freefilelist(sfile);
2701                         }
2702                 }
2703
2704                 scrarea_queue_winredraw(curarea);
2705         }
2706 }
2707