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