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