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