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