7839cf5bf4bd906bd754e8f70a57a678ba342426
[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_freelib(sfile->files);
522                 filelist_free(sfile->files);
523                 MEM_freeN(sfile->files);
524                 sfile->files= NULL;
525         }
526         
527         return OPERATOR_FINISHED;
528 }
529
530 int file_operator_poll(bContext *C)
531 {
532         int poll = ED_operator_file_active(C);
533         SpaceFile *sfile= CTX_wm_space_file(C);
534
535         if (!sfile || !sfile->op) poll= 0;
536
537         return poll;
538 }
539
540 void FILE_OT_cancel(struct wmOperatorType *ot)
541 {
542         /* identifiers */
543         ot->name= "Cancel File Load";
544         ot->idname= "FILE_OT_cancel";
545         
546         /* api callbacks */
547         ot->exec= file_cancel_exec;
548         ot->poll= file_operator_poll;
549 }
550
551 /* sends events now, so things get handled on windowqueue level */
552 int file_exec(bContext *C, wmOperator *unused)
553 {
554         SpaceFile *sfile= CTX_wm_space_file(C);
555         char name[FILE_MAX];
556         
557         if(sfile->op) {
558                 wmOperator *op= sfile->op;
559                 
560                 sfile->op = NULL;
561                 RNA_string_set(op->ptr, "filename", sfile->params->file);
562                 BLI_strncpy(name, sfile->params->dir, sizeof(name));
563                 RNA_string_set(op->ptr, "directory", name);
564                 strcat(name, sfile->params->file);
565
566                 if(RNA_struct_find_property(op->ptr, "relative_paths"))
567                         if(RNA_boolean_get(op->ptr, "relative_paths"))
568                                 BLI_makestringcode(G.sce, name);
569
570                 RNA_string_set(op->ptr, "path", name);
571                 
572                 /* some ops have multiple files to select */
573                 {
574                         PointerRNA itemptr;
575                         int i, numfiles = filelist_numfiles(sfile->files);
576                         struct direntry *file;
577                         if(RNA_struct_find_property(op->ptr, "files")) {
578                                 for (i=0; i<numfiles; i++) {
579                                         file = filelist_file(sfile->files, i);
580                                         if(file->flags & ACTIVE) {
581                                                 if ((file->type & S_IFDIR)==0) {
582                                                         RNA_collection_add(op->ptr, "files", &itemptr);
583                                                         RNA_string_set(&itemptr, "name", file->relname);
584                                                 }
585                                         }
586                                 }
587                         }
588                         
589                         if(RNA_struct_find_property(op->ptr, "dirs")) {
590                                 for (i=0; i<numfiles; i++) {
591                                         file = filelist_file(sfile->files, i);
592                                         if(file->flags & ACTIVE) {
593                                                 if ((file->type & S_IFDIR)) {
594                                                         RNA_collection_add(op->ptr, "dirs", &itemptr);
595                                                         RNA_string_set(&itemptr, "name", file->relname);
596                                                 }
597                                         }
598                                 }
599                         }
600                 }
601                 
602                 folderlist_free(sfile->folders_prev);
603                 folderlist_free(sfile->folders_next);
604
605                 fsmenu_insert_entry(fsmenu_get(), FS_CATEGORY_RECENT, sfile->params->dir,0, 1);
606                 BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
607                 fsmenu_write_file(fsmenu_get(), name);
608                 WM_event_fileselect_event(C, op, EVT_FILESELECT_EXEC);
609
610                 filelist_freelib(sfile->files);
611                 filelist_free(sfile->files);
612                 MEM_freeN(sfile->files);
613                 sfile->files= NULL;
614         }
615                                 
616         return OPERATOR_FINISHED;
617 }
618
619 void FILE_OT_execute(struct wmOperatorType *ot)
620 {
621         /* identifiers */
622         ot->name= "Execute File Window";
623         ot->idname= "FILE_OT_execute";
624         
625         /* api callbacks */
626         ot->exec= file_exec;
627         ot->poll= file_operator_poll; 
628 }
629
630
631 int file_parent_exec(bContext *C, wmOperator *unused)
632 {
633         SpaceFile *sfile= CTX_wm_space_file(C);
634         
635         if(sfile->params) {
636                 if (BLI_has_parent(sfile->params->dir)) {
637                         BLI_parent_dir(sfile->params->dir);
638                         BLI_cleanup_dir(G.sce, sfile->params->dir);
639                         file_change_dir(sfile);
640                         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
641                 }
642         }               
643         
644         return OPERATOR_FINISHED;
645
646 }
647
648
649 void FILE_OT_parent(struct wmOperatorType *ot)
650 {
651         /* identifiers */
652         ot->name= "Parent File";
653         ot->idname= "FILE_OT_parent";
654         
655         /* api callbacks */
656         ot->exec= file_parent_exec;
657         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
658 }
659
660
661 int file_refresh_exec(bContext *C, wmOperator *unused)
662 {
663         SpaceFile *sfile= CTX_wm_space_file(C);
664         
665         file_change_dir(sfile);
666
667         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
668
669         return OPERATOR_FINISHED;
670
671 }
672
673 void FILE_OT_previous(struct wmOperatorType *ot)
674 {
675         /* identifiers */
676         ot->name= "Previous Folder";
677         ot->idname= "FILE_OT_previous";
678         
679         /* api callbacks */
680         ot->exec= file_previous_exec;
681         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
682 }
683
684 int file_previous_exec(bContext *C, wmOperator *unused)
685 {
686         SpaceFile *sfile= CTX_wm_space_file(C);
687
688         if(sfile->params) {
689                 if (!sfile->folders_next)
690                         sfile->folders_next = folderlist_new();
691
692                 folderlist_pushdir(sfile->folders_next, sfile->params->dir);
693                 folderlist_popdir(sfile->folders_prev, sfile->params->dir);
694                 folderlist_pushdir(sfile->folders_next, sfile->params->dir);
695
696                 file_change_dir(sfile);
697         }
698         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
699
700         return OPERATOR_FINISHED;
701 }
702
703 void FILE_OT_next(struct wmOperatorType *ot)
704 {
705         /* identifiers */
706         ot->name= "Next Folder";
707         ot->idname= "FILE_OT_next";
708         
709         /* api callbacks */
710         ot->exec= file_next_exec;
711         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
712 }
713
714 int file_next_exec(bContext *C, wmOperator *unused)
715 {
716         SpaceFile *sfile= CTX_wm_space_file(C);
717                 if(sfile->params) {
718                         if (!sfile->folders_next)
719                         sfile->folders_next = folderlist_new();
720
721                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
722                 folderlist_popdir(sfile->folders_next, sfile->params->dir);
723
724                 // update folder_prev so we can check for it in folderlist_clear_next()
725                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
726
727                 file_change_dir(sfile);
728         }               
729         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
730
731         return OPERATOR_FINISHED;
732 }
733
734 int file_directory_new_exec(bContext *C, wmOperator *unused)
735 {
736         char tmpstr[FILE_MAX];
737         char tmpdir[FILE_MAXFILE];
738         int i = 1;
739
740         SpaceFile *sfile= CTX_wm_space_file(C);
741         
742         if(sfile->params) {
743                  
744                 BLI_strncpy(tmpstr, sfile->params->dir, FILE_MAX);
745                 BLI_join_dirfile(tmpstr, tmpstr, "New Folder");
746                 while (BLI_exists(tmpstr)) {
747                         BLI_snprintf(tmpdir, FILE_MAXFILE, "New Folder(%d)", i++);
748                         BLI_strncpy(tmpstr, sfile->params->dir, FILE_MAX);
749                         BLI_join_dirfile(tmpstr, tmpstr, tmpdir);
750                 }
751                 BLI_recurdir_fileops(tmpstr);
752                 if (BLI_exists(tmpstr)) {
753                         BLI_strncpy(sfile->params->renamefile, tmpdir, FILE_MAXFILE);
754                 } else {
755                         filelist_free(sfile->files);
756                         filelist_parent(sfile->files);
757                         BLI_strncpy(sfile->params->dir, filelist_dir(sfile->files), FILE_MAX);
758                 } 
759         }               
760         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
761
762         return OPERATOR_FINISHED;
763 }
764
765
766 void FILE_OT_directory_new(struct wmOperatorType *ot)
767 {
768         /* identifiers */
769         ot->name= "Create New Directory";
770         ot->idname= "FILE_OT_directory_new";
771         ot->description= "Create a new directory";
772         
773         /* api callbacks */
774         ot->invoke= WM_operator_confirm;
775         ot->exec= file_directory_new_exec;
776         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
777 }
778
779 int file_directory_exec(bContext *C, wmOperator *unused)
780 {
781         char tmpstr[FILE_MAX];
782
783         SpaceFile *sfile= CTX_wm_space_file(C);
784         
785         if(sfile->params) {
786
787                 if ( sfile->params->dir[0] == '~' ) {
788                         if (sfile->params->dir[1] == '\0') {
789                                 BLI_strncpy(sfile->params->dir, BLI_gethome(), sizeof(sfile->params->dir) );
790                         } else {
791                                 /* replace ~ with home */
792                                 char homestr[FILE_MAX];
793                                 char *d = &sfile->params->dir[1];
794
795                                 while ( (*d == '\\') || (*d == '/') )
796                                         d++;
797                                 BLI_strncpy(homestr,  BLI_gethome(), FILE_MAX);
798                                 BLI_join_dirfile(tmpstr, homestr, d);
799                                 BLI_strncpy(sfile->params->dir, tmpstr, sizeof(sfile->params->dir));
800                         }
801                 }
802 #ifdef WIN32
803                 if (sfile->params->dir[0] == '\0')
804                         get_default_root(sfile->params->dir);
805 #endif
806                 BLI_cleanup_dir(G.sce, sfile->params->dir);
807                 BLI_add_slash(sfile->params->dir);
808                 file_change_dir(sfile);
809                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
810         }               
811         
812
813         return OPERATOR_FINISHED;
814 }
815
816 int file_filename_exec(bContext *C, wmOperator *unused)
817 {
818         SpaceFile *sfile= CTX_wm_space_file(C);
819         
820         if(sfile->params) {
821                 if (file_select_match(sfile, sfile->params->file))
822                 {
823                         sfile->params->file[0] = '\0';
824                         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_PARAMS, NULL);
825                 }
826         }               
827
828         return OPERATOR_FINISHED;
829 }
830
831
832 void FILE_OT_refresh(struct wmOperatorType *ot)
833 {
834         /* identifiers */
835         ot->name= "Refresh Filelist";
836         ot->idname= "FILE_OT_refresh";
837         
838         /* api callbacks */
839         ot->exec= file_refresh_exec;
840         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
841 }
842
843 int file_hidedot_exec(bContext *C, wmOperator *unused)
844 {
845         SpaceFile *sfile= CTX_wm_space_file(C);
846         
847         if(sfile->params) {
848                 sfile->params->flag ^= FILE_HIDE_DOT;
849                 filelist_free(sfile->files);
850                 sfile->params->active_file = -1;
851                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
852         }
853         
854         return OPERATOR_FINISHED;
855
856 }
857
858
859 void FILE_OT_hidedot(struct wmOperatorType *ot)
860 {
861         /* identifiers */
862         ot->name= "Toggle Hide Dot Files";
863         ot->idname= "FILE_OT_hidedot";
864         
865         /* api callbacks */
866         ot->exec= file_hidedot_exec;
867         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
868 }
869
870 struct ARegion *file_buttons_region(struct ScrArea *sa)
871 {
872         ARegion *ar, *arnew;
873         
874         for(ar= sa->regionbase.first; ar; ar= ar->next)
875                 if(ar->regiontype==RGN_TYPE_CHANNELS)
876                         return ar;
877
878         /* add subdiv level; after header */
879         for(ar= sa->regionbase.first; ar; ar= ar->next)
880                 if(ar->regiontype==RGN_TYPE_HEADER)
881                         break;
882         
883         /* is error! */
884         if(ar==NULL) return NULL;
885         
886         arnew= MEM_callocN(sizeof(ARegion), "buttons for file panels");
887         
888         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
889         arnew->regiontype= RGN_TYPE_CHANNELS;
890         arnew->alignment= RGN_ALIGN_LEFT;
891         
892         arnew->flag = RGN_FLAG_HIDDEN;
893         
894         return arnew;
895 }
896
897 int file_bookmark_toggle_exec(bContext *C, wmOperator *unused)
898 {
899         ScrArea *sa= CTX_wm_area(C);
900         ARegion *ar= file_buttons_region(sa);
901         
902         if(ar)
903                 ED_region_toggle_hidden(C, ar);
904
905         return OPERATOR_FINISHED;
906 }
907
908 void FILE_OT_bookmark_toggle(struct wmOperatorType *ot)
909 {
910         /* identifiers */
911         ot->name= "Toggle Bookmarks";
912         ot->idname= "FILE_OT_bookmark_toggle";
913         
914         /* api callbacks */
915         ot->exec= file_bookmark_toggle_exec;
916         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
917 }
918
919
920 int file_filenum_exec(bContext *C, wmOperator *op)
921 {
922         SpaceFile *sfile= CTX_wm_space_file(C);
923         ScrArea *sa= CTX_wm_area(C);
924         
925         int inc = RNA_int_get(op->ptr, "increment");
926         if(sfile->params && (inc != 0)) {
927                 BLI_newname(sfile->params->file, inc);
928                 ED_area_tag_redraw(sa);
929                 // WM_event_add_notifier(C, NC_WINDOW, NULL);
930         }
931         
932         return OPERATOR_FINISHED;
933
934 }
935
936 void FILE_OT_filenum(struct wmOperatorType *ot)
937 {
938         /* identifiers */
939         ot->name= "Increment Number in Filename";
940         ot->idname= "FILE_OT_filenum";
941         
942         /* api callbacks */
943         ot->exec= file_filenum_exec;
944         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
945
946         /* props */
947         RNA_def_int(ot->srna, "increment", 1, 0, 100, "Increment", "", 0,100);
948 }
949
950 int file_rename_exec(bContext *C, wmOperator *op)
951 {
952         ScrArea *sa= CTX_wm_area(C);
953         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
954         
955         if(sfile->params) {
956                 int idx = sfile->params->active_file;
957                 int numfiles = filelist_numfiles(sfile->files);
958                 if ( (0<=idx) && (idx<numfiles) ) {
959                         struct direntry *file= filelist_file(sfile->files, idx);
960                         file->flags |= EDITING;
961                 }
962                 ED_area_tag_redraw(sa);
963         }
964         
965         return OPERATOR_FINISHED;
966
967 }
968
969 int file_rename_poll(bContext *C)
970 {
971         int poll = ED_operator_file_active(C);
972         SpaceFile *sfile= CTX_wm_space_file(C);
973
974         if (sfile && sfile->params) {
975                 if (sfile->params->active_file < 0) { 
976                         poll= 0;
977                 } else {
978                         char dir[FILE_MAX], group[FILE_MAX];    
979                         if (filelist_islibrary(sfile->files, dir, group)) poll= 0;
980                 }
981         }
982         else
983                 poll= 0;
984         return poll;
985 }
986
987 void FILE_OT_rename(struct wmOperatorType *ot)
988 {
989         /* identifiers */
990         ot->name= "Rename File or Directory";
991         ot->idname= "FILE_OT_rename";
992         
993         /* api callbacks */
994         ot->exec= file_rename_exec;
995         ot->poll= file_rename_poll; 
996
997 }
998
999 int file_delete_poll(bContext *C)
1000 {
1001         int poll = ED_operator_file_active(C);
1002         SpaceFile *sfile= CTX_wm_space_file(C);
1003         struct direntry* file;
1004
1005         if (sfile && sfile->params) {
1006                 if (sfile->params->active_file < 0) { 
1007                         poll= 0;
1008                 } else {
1009                         char dir[FILE_MAX], group[FILE_MAX];    
1010                         if (filelist_islibrary(sfile->files, dir, group)) poll= 0;
1011                         file = filelist_file(sfile->files, sfile->params->active_file);
1012                         if (file && S_ISDIR(file->type)) poll= 0;
1013                 }
1014         }
1015         else
1016                 poll= 0;
1017                 
1018         return poll;
1019 }
1020
1021 int file_delete_exec(bContext *C, wmOperator *op)
1022 {
1023         char str[FILE_MAX];
1024         SpaceFile *sfile= CTX_wm_space_file(C);
1025         struct direntry* file;
1026         
1027         
1028         file = filelist_file(sfile->files, sfile->params->active_file);
1029         BLI_make_file_string(G.sce, str, sfile->params->dir, file->relname);
1030         BLI_delete(str, 0, 0);  
1031         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
1032         
1033         return OPERATOR_FINISHED;
1034
1035 }
1036
1037 void FILE_OT_delete(struct wmOperatorType *ot)
1038 {
1039         /* identifiers */
1040         ot->name= "Delete File";
1041         ot->idname= "FILE_OT_delete";
1042         
1043         /* api callbacks */
1044         ot->invoke= WM_operator_confirm;
1045         ot->exec= file_delete_exec;
1046         ot->poll= file_delete_poll; /* <- important, handler is on window level */
1047 }
1048