2.5 filebrowser
[blender.git] / source / blender / editors / space_file / file_ops.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL 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. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Andrea Weikert (c) 2008 Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include "BKE_context.h"
30 #include "BKE_screen.h"
31 #include "BKE_global.h"
32
33 #include "BLI_blenlib.h"
34 #include "BLI_storage_types.h"
35 #ifdef WIN32
36 #include "BLI_winstuff.h"
37 #endif
38 #include "DNA_space_types.h"
39 #include "DNA_userdef_types.h"
40
41 #include "ED_space_api.h"
42 #include "ED_screen.h"
43 #include "ED_fileselect.h"
44
45 #include "MEM_guardedalloc.h"
46
47 #include "RNA_access.h"
48 #include "RNA_define.h"
49
50 #include "UI_interface.h"
51 #include "UI_view2d.h"
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "file_intern.h"
57 #include "filelist.h"
58 #include "fsmenu.h"
59
60 #include <stdlib.h>
61 #include <string.h>
62 #include <stdio.h>
63
64 /* for events */
65 #define NOTACTIVE                       0
66 #define ACTIVATE                        1
67 #define INACTIVATE                      2
68
69 /* ---------- FILE SELECTION ------------ */
70
71 static int find_file_mouse(SpaceFile *sfile, struct ARegion* ar, short x, short y, short clamp)
72 {
73         float fx,fy;
74         int active_file = -1;
75         int numfiles = filelist_numfiles(sfile->files);
76         View2D* v2d = &ar->v2d;
77
78         UI_view2d_region_to_view(v2d, x, y, &fx, &fy);
79
80         active_file = ED_fileselect_layout_offset(sfile->layout, v2d->tot.xmin + fx, v2d->tot.ymax - fy);
81
82         if(active_file < 0) {
83                 if(clamp)       active_file=  0;
84                 else            active_file= -1;
85         }
86         else if(active_file >= numfiles) {
87                 if(clamp)       active_file=  numfiles-1;
88                 else            active_file= -1;
89         }
90         
91         return active_file;
92 }
93
94
95 static void file_deselect_all(SpaceFile* sfile)
96 {
97         int numfiles = filelist_numfiles(sfile->files);
98         int i;
99
100         for ( i=0; i < numfiles; ++i) {
101                 struct direntry* file = filelist_file(sfile->files, i);
102                 if (file && (file->flags & ACTIVE)) {
103                         file->flags &= ~ACTIVE;
104                 }
105         }
106 }
107
108 typedef enum FileSelect { FILE_SELECT_DIR = 1, 
109   FILE_SELECT_FILE = 2 } FileSelect;
110
111
112 static FileSelect file_select(SpaceFile* sfile, ARegion* ar, const rcti* rect, short val)
113 {
114         int first_file = -1;
115         int last_file = -1;
116         int act_file;
117         short selecting = (val == LEFTMOUSE);
118         FileSelect retval = FILE_SELECT_FILE;
119
120         FileSelectParams *params = ED_fileselect_get_params(sfile);
121         // FileLayout *layout = ED_fileselect_get_layout(sfile, ar);
122
123         int numfiles = filelist_numfiles(sfile->files);
124
125         params->selstate = NOTACTIVE;
126         first_file = find_file_mouse(sfile, ar, rect->xmin, rect->ymax, 1);
127         last_file = find_file_mouse(sfile, ar, rect->xmax, rect->ymin, 1);
128         
129         /* select all valid files between first and last indicated */
130         if ( (first_file >= 0) && (first_file < numfiles) && (last_file >= 0) && (last_file < numfiles) ) {
131                 for (act_file = first_file; act_file <= last_file; act_file++) {
132                         struct direntry* file = filelist_file(sfile->files, act_file);
133                         if (selecting) 
134                                 file->flags |= ACTIVE;
135                         else
136                                 file->flags &= ~ACTIVE;
137                 }
138         }
139
140         /* make the last file active */
141         if (selecting && (last_file >= 0 && last_file < numfiles)) {
142                 struct direntry* file = filelist_file(sfile->files, last_file);
143                 params->active_file = last_file;
144
145                 if(file && S_ISDIR(file->type)) {
146                         /* the path is too long and we are not going up! */
147                         if (strcmp(file->relname, "..") && strlen(params->dir) + strlen(file->relname) >= FILE_MAX ) 
148                         {
149                                 // XXX error("Path too long, cannot enter this directory");
150                         } else {
151                                 if (strcmp(file->relname, "..")==0) {    
152                                         /* avoids /../../ */     
153                                         BLI_parent_dir(params->dir);     
154                                 } else {
155                                         BLI_cleanup_dir(G.sce, params->dir);
156                                         strcat(params->dir, file->relname);
157                                         BLI_add_slash(params->dir);
158                                 }
159
160                                 file_change_dir(sfile);
161                                 retval = FILE_SELECT_DIR;
162                         }
163                 }
164                 else if (file)
165                 {
166                         if (file->relname) {
167                                 BLI_strncpy(params->file, file->relname, FILE_MAXFILE);
168                         }
169                         
170                 }       
171         }
172         return retval;
173 }
174
175
176
177 static int file_border_select_exec(bContext *C, wmOperator *op)
178 {
179         ARegion *ar= CTX_wm_region(C);
180         SpaceFile *sfile= CTX_wm_space_file(C);
181         short val;
182         rcti rect;
183
184         val= RNA_int_get(op->ptr, "event_type");
185         rect.xmin= RNA_int_get(op->ptr, "xmin");
186         rect.ymin= RNA_int_get(op->ptr, "ymin");
187         rect.xmax= RNA_int_get(op->ptr, "xmax");
188         rect.ymax= RNA_int_get(op->ptr, "ymax");
189
190         BLI_isect_rcti(&(ar->v2d.mask), &rect, &rect);
191         
192         if (FILE_SELECT_DIR == file_select(sfile, ar, &rect, val )) {
193                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
194         } else {
195                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_PARAMS, NULL);
196         }
197         return OPERATOR_FINISHED;
198 }
199
200 void FILE_OT_select_border(wmOperatorType *ot)
201 {
202         /* identifiers */
203         ot->name= "Activate/Select File";
204         ot->idname= "FILE_OT_select_border";
205         
206         /* api callbacks */
207         ot->invoke= WM_border_select_invoke;
208         ot->exec= file_border_select_exec;
209         ot->modal= WM_border_select_modal;
210
211         /* rna */
212         RNA_def_int(ot->srna, "event_type", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
213         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
214         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
215         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
216         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
217
218         ot->poll= ED_operator_file_active;
219 }
220
221 static int file_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
222 {
223         ARegion *ar= CTX_wm_region(C);
224         SpaceFile *sfile= CTX_wm_space_file(C);
225         short val;
226         rcti rect;
227
228         if(ar->regiontype != RGN_TYPE_WINDOW)
229                 return OPERATOR_CANCELLED;
230
231         rect.xmin = rect.xmax = event->x - ar->winrct.xmin;
232         rect.ymin = rect.ymax = event->y - ar->winrct.ymin;
233         val = event->val;
234
235         if(!BLI_in_rcti(&ar->v2d.mask, rect.xmin, rect.ymin))
236                 return OPERATOR_CANCELLED;
237
238         /* single select, deselect all selected first */
239         file_deselect_all(sfile);
240
241         if (FILE_SELECT_DIR == file_select(sfile, ar, &rect, val ))
242                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
243         else
244                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_PARAMS, NULL);
245
246         WM_event_add_mousemove(C); /* for directory changes */
247         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_PARAMS, NULL);
248
249         return OPERATOR_FINISHED;
250 }
251
252 void FILE_OT_select(wmOperatorType *ot)
253 {
254         /* identifiers */
255         ot->name= "Activate/Select File";
256         ot->idname= "FILE_OT_select";
257         
258         /* api callbacks */
259         ot->invoke= file_select_invoke;
260
261         /* rna */
262
263         ot->poll= ED_operator_file_active;
264 }
265
266 static int file_select_all_invoke(bContext *C, wmOperator *op, wmEvent *event)
267 {
268         ScrArea *sa= CTX_wm_area(C);
269         SpaceFile *sfile= CTX_wm_space_file(C);
270         int numfiles = filelist_numfiles(sfile->files);
271         int i;
272         int select = 1;
273
274         /* if any file is selected, deselect all first */
275         for ( i=0; i < numfiles; ++i) {
276                 struct direntry* file = filelist_file(sfile->files, i);
277                 if (file && (file->flags & ACTIVE)) {
278                         file->flags &= ~ACTIVE;
279                         select = 0;
280                         ED_area_tag_redraw(sa);
281                 }
282         }
283         /* select all only if previously no file was selected */
284         if (select) {
285                 for ( i=0; i < numfiles; ++i) {
286                         struct direntry* file = filelist_file(sfile->files, i);
287                         if(file && !S_ISDIR(file->type)) {
288                                 file->flags |= ACTIVE;
289                                 ED_area_tag_redraw(sa);
290                         }
291                 }
292         }
293         return OPERATOR_FINISHED;
294 }
295
296 void FILE_OT_select_all_toggle(wmOperatorType *ot)
297 {
298         /* identifiers */
299         ot->name= "Select/Deselect all files";
300         ot->idname= "FILE_OT_select_all_toggle";
301         
302         /* api callbacks */
303         ot->invoke= file_select_all_invoke;
304
305         /* rna */
306
307         ot->poll= ED_operator_file_active;
308 }
309
310 /* ---------- BOOKMARKS ----------- */
311
312 static int bookmark_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
313 {
314         SpaceFile *sfile= CTX_wm_space_file(C);
315
316         if(RNA_struct_find_property(op->ptr, "dir")) {
317                 char entry[256];
318                 FileSelectParams* params = sfile->params;
319
320                 RNA_string_get(op->ptr, "dir", entry);
321                 BLI_strncpy(params->dir, entry, sizeof(params->dir));
322                 BLI_cleanup_dir(G.sce, params->dir);
323                 file_change_dir(sfile);                         
324
325                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
326         }
327         
328         return OPERATOR_FINISHED;
329 }
330
331 void FILE_OT_select_bookmark(wmOperatorType *ot)
332 {
333         /* identifiers */
334         ot->name= "Select Directory";
335         ot->idname= "FILE_OT_select_bookmark";
336         
337         /* api callbacks */
338         ot->invoke= bookmark_select_invoke;
339         ot->poll= ED_operator_file_active;
340
341         RNA_def_string(ot->srna, "dir", "", 256, "Dir", "");
342 }
343
344 static int bookmark_add_invoke(bContext *C, wmOperator *op, wmEvent *event)
345 {
346         ScrArea *sa= CTX_wm_area(C);
347         SpaceFile *sfile= CTX_wm_space_file(C);
348         struct FSMenu* fsmenu = fsmenu_get();
349         struct FileSelectParams* params= ED_fileselect_get_params(sfile);
350
351         if (params->dir[0] != '\0') {
352                 char name[FILE_MAX];
353         
354                 fsmenu_insert_entry(fsmenu, FS_CATEGORY_BOOKMARKS, params->dir, 0, 1);
355                 BLI_make_file_string("/", name, BLI_gethome(), ".Bfs");
356                 fsmenu_write_file(fsmenu, name);
357         }
358
359         ED_area_tag_redraw(sa);
360         return OPERATOR_FINISHED;
361 }
362
363 void FILE_OT_add_bookmark(wmOperatorType *ot)
364 {
365         /* identifiers */
366         ot->name= "Add Bookmark";
367         ot->idname= "FILE_OT_add_bookmark";
368         
369         /* api callbacks */
370         ot->invoke= bookmark_add_invoke;
371         ot->poll= ED_operator_file_active;
372 }
373
374 static int bookmark_delete_invoke(bContext *C, wmOperator *op, wmEvent *event)
375 {
376         ScrArea *sa= CTX_wm_area(C);
377         struct FSMenu* fsmenu = fsmenu_get();
378         int nentries = fsmenu_get_nentries(fsmenu, FS_CATEGORY_BOOKMARKS);
379         
380         if(RNA_struct_find_property(op->ptr, "index")) {
381                 int index = RNA_int_get(op->ptr, "index");
382                 if ( (index >-1) && (index < nentries)) {
383                         char name[FILE_MAX];
384                         
385                         fsmenu_remove_entry(fsmenu, FS_CATEGORY_BOOKMARKS, index);
386                         BLI_make_file_string("/", name, BLI_gethome(), ".Bfs");
387                         fsmenu_write_file(fsmenu, name);
388                         ED_area_tag_redraw(sa);
389                 }
390         }
391
392         return OPERATOR_FINISHED;
393 }
394
395 void FILE_OT_delete_bookmark(wmOperatorType *ot)
396 {
397         /* identifiers */
398         ot->name= "Delete Bookmark";
399         ot->idname= "FILE_OT_delete_bookmark";
400         
401         /* api callbacks */
402         ot->invoke= bookmark_delete_invoke;
403         ot->poll= ED_operator_file_active;
404
405         RNA_def_int(ot->srna, "index", -1, -1, 20000, "Index", "", -1, 20000);
406 }
407
408
409 static int loadimages_invoke(bContext *C, wmOperator *op, wmEvent *event)
410 {
411         ScrArea *sa= CTX_wm_area(C);
412         SpaceFile *sfile= CTX_wm_space_file(C);
413         if (sfile->files) {
414                 filelist_loadimage_timer(sfile->files);
415                 if (filelist_changed(sfile->files)) {
416                         ED_area_tag_redraw(sa);
417                 }
418         }
419
420         return OPERATOR_FINISHED;
421 }
422
423 void FILE_OT_loadimages(wmOperatorType *ot)
424 {
425         
426         /* identifiers */
427         ot->name= "Load Images";
428         ot->idname= "FILE_OT_loadimages";
429         
430         /* api callbacks */
431         ot->invoke= loadimages_invoke;
432         
433         ot->poll= ED_operator_file_active;
434 }
435
436 int file_hilight_set(SpaceFile *sfile, ARegion *ar, int mx, int my)
437 {
438         FileSelectParams* params;
439         int numfiles, actfile, origfile;
440         
441         if(sfile==NULL || sfile->files==NULL) return 0;
442
443         numfiles = filelist_numfiles(sfile->files);
444         params = ED_fileselect_get_params(sfile);
445
446         origfile= params->active_file;
447
448         mx -= ar->winrct.xmin;
449         my -= ar->winrct.ymin;
450
451         if(BLI_in_rcti(&ar->v2d.mask, mx, my)) {
452                 actfile = find_file_mouse(sfile, ar, mx , my, 0);
453
454                 if((actfile >= 0) && (actfile < numfiles))
455                         params->active_file=actfile;
456                 else
457                         params->active_file= -1;
458         }
459         else
460                 params->active_file= -1;
461
462         return (params->active_file != origfile);
463 }
464
465 static int file_highlight_invoke(bContext *C, wmOperator *op, wmEvent *event)
466 {
467         ARegion *ar= CTX_wm_region(C);
468         SpaceFile *sfile= CTX_wm_space_file(C);
469
470         if(!file_hilight_set(sfile, ar, event->x, event->y))
471                 return OPERATOR_CANCELLED;
472
473         ED_area_tag_redraw(CTX_wm_area(C));
474         
475         return OPERATOR_FINISHED;
476 }
477
478 void FILE_OT_highlight(struct wmOperatorType *ot)
479 {
480         /* identifiers */
481         ot->name= "Highlight File";
482         ot->idname= "FILE_OT_highlight";
483         
484         /* api callbacks */
485         ot->invoke= file_highlight_invoke;
486         ot->poll= ED_operator_file_active;
487 }
488
489 int file_cancel_exec(bContext *C, wmOperator *unused)
490 {
491         SpaceFile *sfile= CTX_wm_space_file(C);
492
493         folderlist_free(sfile->folders_prev);
494         folderlist_free(sfile->folders_next);
495
496         WM_event_fileselect_event(C, sfile->op, EVT_FILESELECT_CANCEL);
497         sfile->op = NULL;
498         
499         if (sfile->files) {
500                 filelist_free(sfile->files);
501                 MEM_freeN(sfile->files);
502                 sfile->files= NULL;
503         }
504         
505         return OPERATOR_FINISHED;
506 }
507
508 void FILE_OT_cancel(struct wmOperatorType *ot)
509 {
510         /* identifiers */
511         ot->name= "Cancel File Load";
512         ot->idname= "FILE_OT_cancel";
513         
514         /* api callbacks */
515         ot->exec= file_cancel_exec;
516         ot->poll= ED_operator_file_active;
517 }
518
519 /* sends events now, so things get handled on windowqueue level */
520 int file_exec(bContext *C, wmOperator *unused)
521 {
522         SpaceFile *sfile= CTX_wm_space_file(C);
523         char name[FILE_MAX];
524         
525         if(sfile->op) {
526                 wmOperator *op= sfile->op;
527                 
528                 sfile->op = NULL;
529                 RNA_string_set(op->ptr, "filename", sfile->params->file);
530                 BLI_strncpy(name, sfile->params->dir, sizeof(name));
531                 RNA_string_set(op->ptr, "directory", name);
532                 strcat(name, sfile->params->file);
533
534                 if ( RNA_boolean_get(op->ptr, "relative_paths") ) {
535                         BLI_makestringcode(G.sce, name);
536                 }
537                 RNA_string_set(op->ptr, "path", name);
538                 
539                 /* some ops have multiple files to select */
540                 {
541                         PointerRNA itemptr;
542                         int i, numfiles = filelist_numfiles(sfile->files);
543                         struct direntry *file;
544                         if(RNA_struct_find_property(op->ptr, "files")) {
545                                 for (i=0; i<numfiles; i++) {
546                                         file = filelist_file(sfile->files, i);
547                                         if(file->flags & ACTIVE) {
548                                                 if ((file->type & S_IFDIR)==0) {
549                                                         RNA_collection_add(op->ptr, "files", &itemptr);
550                                                         RNA_string_set(&itemptr, "name", file->relname);
551                                                 }
552                                         }
553                                 }
554                         }
555                         
556                         if(RNA_struct_find_property(op->ptr, "dirs")) {
557                                 for (i=0; i<numfiles; i++) {
558                                         file = filelist_file(sfile->files, i);
559                                         if(file->flags & ACTIVE) {
560                                                 if ((file->type & S_IFDIR)) {
561                                                         RNA_collection_add(op->ptr, "dirs", &itemptr);
562                                                         RNA_string_set(&itemptr, "name", file->relname);
563                                                 }
564                                         }
565                                 }
566                         }
567                 }
568                 
569                 folderlist_free(sfile->folders_prev);
570                 folderlist_free(sfile->folders_next);
571
572                 fsmenu_insert_entry(fsmenu_get(), FS_CATEGORY_RECENT, sfile->params->dir,0, 1);
573                 BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
574                 fsmenu_write_file(fsmenu_get(), name);
575                 WM_event_fileselect_event(C, op, EVT_FILESELECT_EXEC);
576
577                 filelist_free(sfile->files);
578                 MEM_freeN(sfile->files);
579                 sfile->files= NULL;
580         }
581                                 
582         return OPERATOR_FINISHED;
583 }
584
585 void FILE_OT_execute(struct wmOperatorType *ot)
586 {
587         /* identifiers */
588         ot->name= "Execute File Window";
589         ot->idname= "FILE_OT_execute";
590         
591         /* api callbacks */
592         ot->exec= file_exec;
593         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
594 }
595
596
597 int file_parent_exec(bContext *C, wmOperator *unused)
598 {
599         SpaceFile *sfile= CTX_wm_space_file(C);
600         
601         if(sfile->params) {
602                 if (BLI_has_parent(sfile->params->dir)) {
603                         BLI_parent_dir(sfile->params->dir);
604                         BLI_cleanup_dir(G.sce, sfile->params->dir);
605                         file_change_dir(sfile);
606                         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
607                 }
608         }               
609         
610         return OPERATOR_FINISHED;
611
612 }
613
614
615 void FILE_OT_parent(struct wmOperatorType *ot)
616 {
617         /* identifiers */
618         ot->name= "Parent File";
619         ot->idname= "FILE_OT_parent";
620         
621         /* api callbacks */
622         ot->exec= file_parent_exec;
623         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
624 }
625
626
627 int file_refresh_exec(bContext *C, wmOperator *unused)
628 {
629         SpaceFile *sfile= CTX_wm_space_file(C);
630         
631         file_change_dir(sfile);
632
633         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
634
635         return OPERATOR_FINISHED;
636
637 }
638
639 void FILE_OT_previous(struct wmOperatorType *ot)
640 {
641         /* identifiers */
642         ot->name= "Previous Folder";
643         ot->idname= "FILE_OT_previous";
644         
645         /* api callbacks */
646         ot->exec= file_previous_exec;
647         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
648 }
649
650 int file_previous_exec(bContext *C, wmOperator *unused)
651 {
652         SpaceFile *sfile= CTX_wm_space_file(C);
653
654         if(sfile->params) {
655                 if (!sfile->folders_next)
656                         sfile->folders_next = folderlist_new();
657
658                 folderlist_pushdir(sfile->folders_next, sfile->params->dir);
659                 folderlist_popdir(sfile->folders_prev, sfile->params->dir);
660                 folderlist_pushdir(sfile->folders_next, sfile->params->dir);
661
662                 file_change_dir(sfile);
663         }
664         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
665
666         return OPERATOR_FINISHED;
667 }
668
669 void FILE_OT_next(struct wmOperatorType *ot)
670 {
671         /* identifiers */
672         ot->name= "Next Folder";
673         ot->idname= "FILE_OT_next";
674         
675         /* api callbacks */
676         ot->exec= file_next_exec;
677         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
678 }
679
680 int file_next_exec(bContext *C, wmOperator *unused)
681 {
682         SpaceFile *sfile= CTX_wm_space_file(C);
683                 if(sfile->params) {
684                         if (!sfile->folders_next)
685                         sfile->folders_next = folderlist_new();
686
687                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
688                 folderlist_popdir(sfile->folders_next, sfile->params->dir);
689
690                 // update folder_prev so we can check for it in folderlist_clear_next()
691                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
692
693                 file_change_dir(sfile);
694         }               
695         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
696
697         return OPERATOR_FINISHED;
698 }
699
700 int file_directory_new_exec(bContext *C, wmOperator *unused)
701 {
702         char tmpstr[FILE_MAX];
703         char tmpdir[FILE_MAXFILE];
704         int i = 1;
705
706         SpaceFile *sfile= CTX_wm_space_file(C);
707         
708         if(sfile->params) {
709                  
710                 BLI_strncpy(tmpstr, sfile->params->dir, FILE_MAX);
711                 BLI_join_dirfile(tmpstr, tmpstr, "New Folder");
712                 while (BLI_exists(tmpstr)) {
713                         BLI_snprintf(tmpdir, FILE_MAXFILE, "New Folder(%d)", i++);
714                         BLI_strncpy(tmpstr, sfile->params->dir, FILE_MAX);
715                         BLI_join_dirfile(tmpstr, tmpstr, tmpdir);
716                 }
717                 BLI_recurdir_fileops(tmpstr);
718                 if (BLI_exists(tmpstr)) {
719                         BLI_strncpy(sfile->params->renamefile, tmpdir, FILE_MAXFILE);
720                 } else {
721                         filelist_free(sfile->files);
722                         filelist_parent(sfile->files);
723                         BLI_strncpy(sfile->params->dir, filelist_dir(sfile->files), FILE_MAX);
724                 } 
725         }               
726         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
727
728         return OPERATOR_FINISHED;
729 }
730
731
732 void FILE_OT_directory_new(struct wmOperatorType *ot)
733 {
734         /* identifiers */
735         ot->name= "Create New Directory";
736         ot->idname= "FILE_OT_directory_new";
737         ot->description= "Create a new directory";
738         
739         /* api callbacks */
740         ot->invoke= WM_operator_confirm;
741         ot->exec= file_directory_new_exec;
742         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
743 }
744
745 int file_directory_exec(bContext *C, wmOperator *unused)
746 {
747         char tmpstr[FILE_MAX];
748
749         SpaceFile *sfile= CTX_wm_space_file(C);
750         
751         if(sfile->params) {
752
753                 if ( sfile->params->dir[0] == '~' ) {
754                         if (sfile->params->dir[1] == '\0') {
755                                 BLI_strncpy(sfile->params->dir, BLI_gethome(), sizeof(sfile->params->dir) );
756                         } else {
757                                 /* replace ~ with home */
758                                 char homestr[FILE_MAX];
759                                 char *d = &sfile->params->dir[1];
760
761                                 while ( (*d == '\\') || (*d == '/') )
762                                         d++;
763                                 BLI_strncpy(homestr,  BLI_gethome(), FILE_MAX);
764                                 BLI_join_dirfile(tmpstr, homestr, d);
765                                 BLI_strncpy(sfile->params->dir, tmpstr, sizeof(sfile->params->dir));
766                         }
767                 }
768 #ifdef WIN32
769                 if (sfile->params->dir[0] == '\0')
770                         get_default_root(sfile->params->dir);
771 #endif
772                 BLI_cleanup_dir(G.sce, sfile->params->dir);
773                 BLI_add_slash(sfile->params->dir);
774                 file_change_dir(sfile);
775                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
776         }               
777         
778
779         return OPERATOR_FINISHED;
780 }
781
782 int file_filename_exec(bContext *C, wmOperator *unused)
783 {
784         SpaceFile *sfile= CTX_wm_space_file(C);
785         
786         if(sfile->params) {
787                 if (file_select_match(sfile, sfile->params->file))
788                 {
789                         sfile->params->file[0] = '\0';
790                         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_PARAMS, NULL);
791                 }
792         }               
793
794         return OPERATOR_FINISHED;
795 }
796
797
798 void FILE_OT_refresh(struct wmOperatorType *ot)
799 {
800         /* identifiers */
801         ot->name= "Refresh Filelist";
802         ot->idname= "FILE_OT_refresh";
803         
804         /* api callbacks */
805         ot->exec= file_refresh_exec;
806         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
807 }
808
809 int file_hidedot_exec(bContext *C, wmOperator *unused)
810 {
811         SpaceFile *sfile= CTX_wm_space_file(C);
812         
813         if(sfile->params) {
814                 sfile->params->flag ^= FILE_HIDE_DOT;
815                 filelist_free(sfile->files);
816                 sfile->params->active_file = -1;
817                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
818         }
819         
820         return OPERATOR_FINISHED;
821
822 }
823
824
825 void FILE_OT_hidedot(struct wmOperatorType *ot)
826 {
827         /* identifiers */
828         ot->name= "Toggle Hide Dot Files";
829         ot->idname= "FILE_OT_hidedot";
830         
831         /* api callbacks */
832         ot->exec= file_hidedot_exec;
833         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
834 }
835
836 struct ARegion *file_buttons_region(struct ScrArea *sa)
837 {
838         ARegion *ar, *arnew;
839         
840         for(ar= sa->regionbase.first; ar; ar= ar->next)
841                 if(ar->regiontype==RGN_TYPE_CHANNELS)
842                         return ar;
843
844         /* add subdiv level; after header */
845         for(ar= sa->regionbase.first; ar; ar= ar->next)
846                 if(ar->regiontype==RGN_TYPE_HEADER)
847                         break;
848         
849         /* is error! */
850         if(ar==NULL) return NULL;
851         
852         arnew= MEM_callocN(sizeof(ARegion), "buttons for file panels");
853         
854         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
855         arnew->regiontype= RGN_TYPE_CHANNELS;
856         arnew->alignment= RGN_ALIGN_LEFT;
857         
858         arnew->flag = RGN_FLAG_HIDDEN;
859         
860         return arnew;
861 }
862
863 int file_bookmark_toggle_exec(bContext *C, wmOperator *unused)
864 {
865         ScrArea *sa= CTX_wm_area(C);
866         ARegion *ar= file_buttons_region(sa);
867         
868         if(ar)
869                 ED_region_toggle_hidden(C, ar);
870
871         return OPERATOR_FINISHED;
872 }
873
874 void FILE_OT_bookmark_toggle(struct wmOperatorType *ot)
875 {
876         /* identifiers */
877         ot->name= "Toggle Bookmarks";
878         ot->idname= "FILE_OT_bookmark_toggle";
879         
880         /* api callbacks */
881         ot->exec= file_bookmark_toggle_exec;
882         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
883 }
884
885
886 int file_filenum_exec(bContext *C, wmOperator *op)
887 {
888         SpaceFile *sfile= CTX_wm_space_file(C);
889         ScrArea *sa= CTX_wm_area(C);
890         
891         int inc = RNA_int_get(op->ptr, "increment");
892         if(sfile->params && (inc != 0)) {
893                 BLI_newname(sfile->params->file, inc);
894                 ED_area_tag_redraw(sa);
895                 // WM_event_add_notifier(C, NC_WINDOW, NULL);
896         }
897         
898         return OPERATOR_FINISHED;
899
900 }
901
902 void FILE_OT_filenum(struct wmOperatorType *ot)
903 {
904         /* identifiers */
905         ot->name= "Increment Number in Filename";
906         ot->idname= "FILE_OT_filenum";
907         
908         /* api callbacks */
909         ot->exec= file_filenum_exec;
910         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
911
912         /* props */
913         RNA_def_int(ot->srna, "increment", 1, 0, 100, "Increment", "", 0,100);
914 }
915
916 int file_rename_exec(bContext *C, wmOperator *op)
917 {
918         ScrArea *sa= CTX_wm_area(C);
919         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
920         
921         if(sfile->params) {
922                 int idx = sfile->params->active_file;
923                 int numfiles = filelist_numfiles(sfile->files);
924                 if ( (0<=idx) && (idx<numfiles) ) {
925                         struct direntry *file= filelist_file(sfile->files, idx);
926                         file->flags |= EDITING;
927                 }
928                 ED_area_tag_redraw(sa);
929         }
930         
931         return OPERATOR_FINISHED;
932
933 }
934
935 int file_rename_poll(bContext *C)
936 {
937         int poll = ED_operator_file_active(C);
938         SpaceFile *sfile= CTX_wm_space_file(C);
939
940         if (sfile && sfile->params) {
941                 if (sfile->params->active_file < 0) { 
942                         poll= 0;
943                 } else {
944                         char dir[FILE_MAX], group[FILE_MAX];    
945                         if (filelist_islibrary(sfile->files, dir, group)) poll= 0;
946                 }
947         }
948         else
949                 poll= 0;
950         return poll;
951 }
952
953 void FILE_OT_rename(struct wmOperatorType *ot)
954 {
955         /* identifiers */
956         ot->name= "Rename File or Directory";
957         ot->idname= "FILE_OT_rename";
958         
959         /* api callbacks */
960         ot->exec= file_rename_exec;
961         ot->poll= file_rename_poll; 
962
963 }
964
965 int file_delete_poll(bContext *C)
966 {
967         int poll = ED_operator_file_active(C);
968         SpaceFile *sfile= CTX_wm_space_file(C);
969         struct direntry* file;
970
971         if (sfile && sfile->params) {
972                 if (sfile->params->active_file < 0) { 
973                         poll= 0;
974                 } else {
975                         char dir[FILE_MAX], group[FILE_MAX];    
976                         if (filelist_islibrary(sfile->files, dir, group)) poll= 0;
977                         file = filelist_file(sfile->files, sfile->params->active_file);
978                         if (file && S_ISDIR(file->type)) poll= 0;
979                 }
980         }
981         else
982                 poll= 0;
983                 
984         return poll;
985 }
986
987 int file_delete_exec(bContext *C, wmOperator *op)
988 {
989         char str[FILE_MAX];
990         SpaceFile *sfile= CTX_wm_space_file(C);
991         struct direntry* file;
992         
993         
994         file = filelist_file(sfile->files, sfile->params->active_file);
995         BLI_make_file_string(G.sce, str, sfile->params->dir, file->relname);
996         BLI_delete(str, 0, 0);  
997         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
998         
999         return OPERATOR_FINISHED;
1000
1001 }
1002
1003 void FILE_OT_delete(struct wmOperatorType *ot)
1004 {
1005         /* identifiers */
1006         ot->name= "Delete File";
1007         ot->idname= "FILE_OT_delete";
1008         
1009         /* api callbacks */
1010         ot->invoke= WM_operator_confirm;
1011         ot->exec= file_delete_exec;
1012         ot->poll= file_delete_poll; /* <- important, handler is on window level */
1013 }
1014