Updated descriptions from Ron Walker
[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);
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);                         
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);
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);
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);
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);
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
799                 if ( sfile->params->dir[0] == '~' ) {
800                         if (sfile->params->dir[1] == '\0') {
801                                 BLI_strncpy(sfile->params->dir, BLI_gethome(), sizeof(sfile->params->dir) );
802                         } else {
803                                 /* replace ~ with home */
804                                 char homestr[FILE_MAX];
805                                 char *d = &sfile->params->dir[1];
806
807                                 while ( (*d == '\\') || (*d == '/') )
808                                         d++;
809                                 BLI_strncpy(homestr,  BLI_gethome(), FILE_MAX);
810                                 BLI_join_dirfile(tmpstr, homestr, d);
811                                 BLI_strncpy(sfile->params->dir, tmpstr, sizeof(sfile->params->dir));
812                         }
813                 }
814 #ifdef WIN32
815                 if (sfile->params->dir[0] == '\0')
816                         get_default_root(sfile->params->dir);
817 #endif
818                 BLI_cleanup_dir(G.sce, sfile->params->dir);
819                 BLI_add_slash(sfile->params->dir);
820                 file_change_dir(sfile);
821                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
822         }               
823         
824
825         return OPERATOR_FINISHED;
826 }
827
828 int file_filename_exec(bContext *C, wmOperator *unused)
829 {
830         SpaceFile *sfile= CTX_wm_space_file(C);
831         
832         if(sfile->params) {
833                 if (file_select_match(sfile, sfile->params->file))
834                 {
835                         sfile->params->file[0] = '\0';
836                         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_PARAMS, NULL);
837                 }
838         }               
839
840         return OPERATOR_FINISHED;
841 }
842
843
844 void FILE_OT_refresh(struct wmOperatorType *ot)
845 {
846         /* identifiers */
847         ot->name= "Refresh Filelist";
848         ot->description= "Refresh the file list.";
849         ot->idname= "FILE_OT_refresh";
850         
851         /* api callbacks */
852         ot->exec= file_refresh_exec;
853         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
854 }
855
856 int file_hidedot_exec(bContext *C, wmOperator *unused)
857 {
858         SpaceFile *sfile= CTX_wm_space_file(C);
859         
860         if(sfile->params) {
861                 sfile->params->flag ^= FILE_HIDE_DOT;
862                 filelist_free(sfile->files);
863                 sfile->params->active_file = -1;
864                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
865         }
866         
867         return OPERATOR_FINISHED;
868
869 }
870
871
872 void FILE_OT_hidedot(struct wmOperatorType *ot)
873 {
874         /* identifiers */
875         ot->name= "Toggle Hide Dot Files";
876         ot->description= "Toggle hide hidden dot files.";
877         ot->idname= "FILE_OT_hidedot";
878         
879         /* api callbacks */
880         ot->exec= file_hidedot_exec;
881         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
882 }
883
884 struct ARegion *file_buttons_region(struct ScrArea *sa)
885 {
886         ARegion *ar, *arnew;
887         
888         for(ar= sa->regionbase.first; ar; ar= ar->next)
889                 if(ar->regiontype==RGN_TYPE_CHANNELS)
890                         return ar;
891
892         /* add subdiv level; after header */
893         for(ar= sa->regionbase.first; ar; ar= ar->next)
894                 if(ar->regiontype==RGN_TYPE_HEADER)
895                         break;
896         
897         /* is error! */
898         if(ar==NULL) return NULL;
899         
900         arnew= MEM_callocN(sizeof(ARegion), "buttons for file panels");
901         
902         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
903         arnew->regiontype= RGN_TYPE_CHANNELS;
904         arnew->alignment= RGN_ALIGN_LEFT;
905         
906         arnew->flag = RGN_FLAG_HIDDEN;
907         
908         return arnew;
909 }
910
911 int file_bookmark_toggle_exec(bContext *C, wmOperator *unused)
912 {
913         ScrArea *sa= CTX_wm_area(C);
914         ARegion *ar= file_buttons_region(sa);
915         
916         if(ar)
917                 ED_region_toggle_hidden(C, ar);
918
919         return OPERATOR_FINISHED;
920 }
921
922 void FILE_OT_bookmark_toggle(struct wmOperatorType *ot)
923 {
924         /* identifiers */
925         ot->name= "Toggle Bookmarks";
926         ot->description= "Toggle bookmarks display.";
927         ot->idname= "FILE_OT_bookmark_toggle";
928         
929         /* api callbacks */
930         ot->exec= file_bookmark_toggle_exec;
931         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
932 }
933
934
935 int file_filenum_exec(bContext *C, wmOperator *op)
936 {
937         SpaceFile *sfile= CTX_wm_space_file(C);
938         ScrArea *sa= CTX_wm_area(C);
939         
940         int inc = RNA_int_get(op->ptr, "increment");
941         if(sfile->params && (inc != 0)) {
942                 BLI_newname(sfile->params->file, inc);
943                 ED_area_tag_redraw(sa);
944                 // WM_event_add_notifier(C, NC_WINDOW, NULL);
945         }
946         
947         return OPERATOR_FINISHED;
948
949 }
950
951 void FILE_OT_filenum(struct wmOperatorType *ot)
952 {
953         /* identifiers */
954         ot->name= "Increment Number in Filename";
955         ot->description= "Increment number in filename.";
956         ot->idname= "FILE_OT_filenum";
957         
958         /* api callbacks */
959         ot->exec= file_filenum_exec;
960         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
961
962         /* props */
963         RNA_def_int(ot->srna, "increment", 1, 0, 100, "Increment", "", 0,100);
964 }
965
966 int file_rename_exec(bContext *C, wmOperator *op)
967 {
968         ScrArea *sa= CTX_wm_area(C);
969         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
970         
971         if(sfile->params) {
972                 int idx = sfile->params->active_file;
973                 int numfiles = filelist_numfiles(sfile->files);
974                 if ( (0<=idx) && (idx<numfiles) ) {
975                         struct direntry *file= filelist_file(sfile->files, idx);
976                         file->flags |= EDITING;
977                 }
978                 ED_area_tag_redraw(sa);
979         }
980         
981         return OPERATOR_FINISHED;
982
983 }
984
985 int file_rename_poll(bContext *C)
986 {
987         int poll = ED_operator_file_active(C);
988         SpaceFile *sfile= CTX_wm_space_file(C);
989
990         if (sfile && sfile->params) {
991                 if (sfile->params->active_file < 0) { 
992                         poll= 0;
993                 } else {
994                         char dir[FILE_MAX], group[FILE_MAX];    
995                         if (filelist_islibrary(sfile->files, dir, group)) poll= 0;
996                 }
997         }
998         else
999                 poll= 0;
1000         return poll;
1001 }
1002
1003 void FILE_OT_rename(struct wmOperatorType *ot)
1004 {
1005         /* identifiers */
1006         ot->name= "Rename File or Directory";
1007         ot->description= "Rename file or file directory.";
1008         ot->idname= "FILE_OT_rename";
1009         
1010         /* api callbacks */
1011         ot->exec= file_rename_exec;
1012         ot->poll= file_rename_poll; 
1013
1014 }
1015
1016 int file_delete_poll(bContext *C)
1017 {
1018         int poll = ED_operator_file_active(C);
1019         SpaceFile *sfile= CTX_wm_space_file(C);
1020         struct direntry* file;
1021
1022         if (sfile && sfile->params) {
1023                 if (sfile->params->active_file < 0) { 
1024                         poll= 0;
1025                 } else {
1026                         char dir[FILE_MAX], group[FILE_MAX];    
1027                         if (filelist_islibrary(sfile->files, dir, group)) poll= 0;
1028                         file = filelist_file(sfile->files, sfile->params->active_file);
1029                         if (file && S_ISDIR(file->type)) poll= 0;
1030                 }
1031         }
1032         else
1033                 poll= 0;
1034                 
1035         return poll;
1036 }
1037
1038 int file_delete_exec(bContext *C, wmOperator *op)
1039 {
1040         char str[FILE_MAX];
1041         SpaceFile *sfile= CTX_wm_space_file(C);
1042         struct direntry* file;
1043         
1044         
1045         file = filelist_file(sfile->files, sfile->params->active_file);
1046         BLI_make_file_string(G.sce, str, sfile->params->dir, file->relname);
1047         BLI_delete(str, 0, 0);  
1048         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_FILE_LIST, NULL);
1049         
1050         return OPERATOR_FINISHED;
1051
1052 }
1053
1054 void FILE_OT_delete(struct wmOperatorType *ot)
1055 {
1056         /* identifiers */
1057         ot->name= "Delete File";
1058         ot->description= "Delete selected file.";
1059         ot->idname= "FILE_OT_delete";
1060         
1061         /* api callbacks */
1062         ot->invoke= WM_operator_confirm;
1063         ot->exec= file_delete_exec;
1064         ot->poll= file_delete_poll; /* <- important, handler is on window level */
1065 }
1066