Undo revision 23130 which was a merge with 2.5, a messy one because I did something...
[blender.git] / source / blender / editors / space_file / file_ops.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Andrea Weikert (c) 2008 Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include "BKE_context.h"
30 #include "BKE_screen.h"
31 #include "BKE_global.h"
32
33 #include "BLI_blenlib.h"
34 #include "BLI_storage_types.h"
35 #ifdef WIN32
36 #include "BLI_winstuff.h"
37 #endif
38 #include "DNA_space_types.h"
39 #include "DNA_userdef_types.h"
40
41 #include "ED_space_api.h"
42 #include "ED_screen.h"
43 #include "ED_fileselect.h"
44
45 #include "MEM_guardedalloc.h"
46
47 #include "RNA_access.h"
48 #include "RNA_define.h"
49
50 #include "UI_interface.h"
51 #include "UI_view2d.h"
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "file_intern.h"
57 #include "filelist.h"
58 #include "fsmenu.h"
59
60 #include <stdlib.h>
61 #include <string.h>
62 #include <stdio.h>
63
64 /* for events */
65 #define NOTACTIVE                       0
66 #define ACTIVATE                        1
67 #define INACTIVATE                      2
68
69 /* ---------- FILE SELECTION ------------ */
70
71 static int find_file_mouse(SpaceFile *sfile, struct ARegion* ar, short x, short y, short clamp)
72 {
73         float fx,fy;
74         int active_file = -1;
75         int numfiles = filelist_numfiles(sfile->files);
76         View2D* v2d = &ar->v2d;
77
78         UI_view2d_region_to_view(v2d, x, y, &fx, &fy);
79
80         active_file = ED_fileselect_layout_offset(sfile->layout, v2d->tot.xmin + fx, v2d->tot.ymax - fy);
81
82         if(active_file < 0) {
83                 if(clamp)       active_file=  0;
84                 else            active_file= -1;
85         }
86         else if(active_file >= numfiles) {
87                 if(clamp)       active_file=  numfiles-1;
88                 else            active_file= -1;
89         }
90         
91         return active_file;
92 }
93
94
95 static void file_deselect_all(SpaceFile* sfile)
96 {
97         int numfiles = filelist_numfiles(sfile->files);
98         int i;
99
100         for ( i=0; i < numfiles; ++i) {
101                 struct direntry* file = filelist_file(sfile->files, i);
102                 if (file && (file->flags & ACTIVE)) {
103                         file->flags &= ~ACTIVE;
104                 }
105         }
106 }
107
108 typedef enum FileSelect { FILE_SELECT_DIR = 1, 
109   FILE_SELECT_FILE = 2 } FileSelect;
110
111
112 static FileSelect file_select(SpaceFile* sfile, ARegion* ar, const rcti* rect, short val)
113 {
114         int first_file = -1;
115         int last_file = -1;
116         int act_file;
117         short selecting = (val == LEFTMOUSE);
118         FileSelect retval = FILE_SELECT_FILE;
119
120         FileSelectParams *params = ED_fileselect_get_params(sfile);
121         // FileLayout *layout = ED_fileselect_get_layout(sfile, ar);
122
123         int numfiles = filelist_numfiles(sfile->files);
124
125         params->selstate = NOTACTIVE;
126         first_file = find_file_mouse(sfile, ar, rect->xmin, rect->ymax, 1);
127         last_file = find_file_mouse(sfile, ar, rect->xmax, rect->ymin, 1);
128         
129         /* select all valid files between first and last indicated */
130         if ( (first_file >= 0) && (first_file < numfiles) && (last_file >= 0) && (last_file < numfiles) ) {
131                 for (act_file = first_file; act_file <= last_file; act_file++) {
132                         struct direntry* file = filelist_file(sfile->files, act_file);
133                         if (selecting) 
134                                 file->flags |= ACTIVE;
135                         else
136                                 file->flags &= ~ACTIVE;
137                 }
138         }
139
140         /* make the last file active */
141         if (selecting && (last_file >= 0 && last_file < numfiles)) {
142                 struct direntry* file = filelist_file(sfile->files, last_file);
143                 params->active_file = last_file;
144
145                 if(file && S_ISDIR(file->type)) {
146                         /* the path is too long and we are not going up! */
147                         if (strcmp(file->relname, "..") && strlen(params->dir) + strlen(file->relname) >= FILE_MAX ) 
148                         {
149                                 // XXX error("Path too long, cannot enter this directory");
150                         } else {
151                                 if (strcmp(file->relname, "..")==0) {    
152                                         /* avoids /../../ */     
153                                         BLI_parent_dir(params->dir);     
154                                 } else {
155                                         BLI_cleanup_dir(G.sce, params->dir);
156                                         strcat(params->dir, file->relname);
157                                         BLI_add_slash(params->dir);
158                                 }
159
160                                 file_change_dir(sfile);
161                                 retval = FILE_SELECT_DIR;
162                         }
163                 }
164                 else if (file)
165                 {
166                         if (file->relname) {
167                                 BLI_strncpy(params->file, file->relname, FILE_MAXFILE);
168                         }
169                         
170                 }       
171         }
172         return retval;
173 }
174
175
176
177 static int file_border_select_exec(bContext *C, wmOperator *op)
178 {
179         ARegion *ar= CTX_wm_region(C);
180         SpaceFile *sfile= CTX_wm_space_file(C);
181         short val;
182         rcti rect;
183
184         val= RNA_int_get(op->ptr, "event_type");
185         rect.xmin= RNA_int_get(op->ptr, "xmin");
186         rect.ymin= RNA_int_get(op->ptr, "ymin");
187         rect.xmax= RNA_int_get(op->ptr, "xmax");
188         rect.ymax= RNA_int_get(op->ptr, "ymax");
189
190         BLI_isect_rcti(&(ar->v2d.mask), &rect, &rect);
191         
192         if (FILE_SELECT_DIR == file_select(sfile, ar, &rect, val )) {
193                 WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
194         } else {
195                 WM_event_add_notifier(C, NC_FILE|ND_PARAMS, NULL);
196         }
197         return OPERATOR_FINISHED;
198 }
199
200 void FILE_OT_select_border(wmOperatorType *ot)
201 {
202         /* identifiers */
203         ot->name= "Activate/Select File";
204         ot->idname= "FILE_OT_select_border";
205         
206         /* api callbacks */
207         ot->invoke= WM_border_select_invoke;
208         ot->exec= file_border_select_exec;
209         ot->modal= WM_border_select_modal;
210
211         /* rna */
212         RNA_def_int(ot->srna, "event_type", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
213         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
214         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
215         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
216         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
217
218         ot->poll= ED_operator_file_active;
219 }
220
221 static int file_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
222 {
223         ARegion *ar= CTX_wm_region(C);
224         SpaceFile *sfile= CTX_wm_space_file(C);
225         short val;
226         rcti rect;
227
228         if(ar->regiontype != RGN_TYPE_WINDOW)
229                 return OPERATOR_CANCELLED;
230
231         rect.xmin = rect.xmax = event->x - ar->winrct.xmin;
232         rect.ymin = rect.ymax = event->y - ar->winrct.ymin;
233         val = event->val;
234
235         if(!BLI_in_rcti(&ar->v2d.mask, rect.xmin, rect.ymin))
236                 return OPERATOR_CANCELLED;
237
238         /* single select, deselect all selected first */
239         file_deselect_all(sfile);
240
241         if (FILE_SELECT_DIR == file_select(sfile, ar, &rect, val ))
242                 WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
243         else
244                 WM_event_add_notifier(C, NC_FILE|ND_PARAMS, NULL);
245
246         WM_event_add_mousemove(C); /* for directory changes */
247         WM_event_add_notifier(C, NC_FILE|ND_PARAMS, NULL);
248
249         return OPERATOR_FINISHED;
250 }
251
252 void FILE_OT_select(wmOperatorType *ot)
253 {
254         /* identifiers */
255         ot->name= "Activate/Select File";
256         ot->idname= "FILE_OT_select";
257         
258         /* api callbacks */
259         ot->invoke= file_select_invoke;
260
261         /* rna */
262
263         ot->poll= ED_operator_file_active;
264 }
265
266 static int file_select_all_invoke(bContext *C, wmOperator *op, wmEvent *event)
267 {
268         ScrArea *sa= CTX_wm_area(C);
269         SpaceFile *sfile= CTX_wm_space_file(C);
270         int numfiles = filelist_numfiles(sfile->files);
271         int i;
272         int select = 1;
273
274         /* if any file is selected, deselect all first */
275         for ( i=0; i < numfiles; ++i) {
276                 struct direntry* file = filelist_file(sfile->files, i);
277                 if (file && (file->flags & ACTIVE)) {
278                         file->flags &= ~ACTIVE;
279                         select = 0;
280                         ED_area_tag_redraw(sa);
281                 }
282         }
283         /* select all only if previously no file was selected */
284         if (select) {
285                 for ( i=0; i < numfiles; ++i) {
286                         struct direntry* file = filelist_file(sfile->files, i);
287                         if(file && !S_ISDIR(file->type)) {
288                                 file->flags |= ACTIVE;
289                                 ED_area_tag_redraw(sa);
290                         }
291                 }
292         }
293         return OPERATOR_FINISHED;
294 }
295
296 void FILE_OT_select_all_toggle(wmOperatorType *ot)
297 {
298         /* identifiers */
299         ot->name= "Select/Deselect all files";
300         ot->idname= "FILE_OT_select_all_toggle";
301         
302         /* api callbacks */
303         ot->invoke= file_select_all_invoke;
304
305         /* rna */
306
307         ot->poll= ED_operator_file_active;
308 }
309
310 /* ---------- BOOKMARKS ----------- */
311
312 static int bookmark_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
313 {
314         SpaceFile *sfile= CTX_wm_space_file(C);
315
316         if(RNA_struct_find_property(op->ptr, "dir")) {
317                 char entry[256];
318                 FileSelectParams* params = sfile->params;
319
320                 RNA_string_get(op->ptr, "dir", entry);
321                 BLI_strncpy(params->dir, entry, sizeof(params->dir));
322                 BLI_cleanup_dir(G.sce, params->dir);
323                 file_change_dir(sfile);                         
324
325                 WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
326         }
327         
328         return OPERATOR_FINISHED;
329 }
330
331 void FILE_OT_select_bookmark(wmOperatorType *ot)
332 {
333         /* identifiers */
334         ot->name= "Select Directory";
335         ot->idname= "FILE_OT_select_bookmark";
336         
337         /* api callbacks */
338         ot->invoke= bookmark_select_invoke;
339         ot->poll= ED_operator_file_active;
340
341         RNA_def_string(ot->srna, "dir", "", 256, "Dir", "");
342 }
343
344 static int bookmark_add_invoke(bContext *C, wmOperator *op, wmEvent *event)
345 {
346         ScrArea *sa= CTX_wm_area(C);
347         SpaceFile *sfile= CTX_wm_space_file(C);
348         struct FSMenu* fsmenu = fsmenu_get();
349         struct FileSelectParams* params= ED_fileselect_get_params(sfile);
350
351         if (params->dir[0] != '\0') {
352                 char name[FILE_MAX];
353         
354                 fsmenu_insert_entry(fsmenu, FS_CATEGORY_BOOKMARKS, params->dir, 0, 1);
355                 BLI_make_file_string("/", name, BLI_gethome(), ".Bfs");
356                 fsmenu_write_file(fsmenu, name);
357         }
358
359         ED_area_tag_redraw(sa);
360         return OPERATOR_FINISHED;
361 }
362
363 void FILE_OT_add_bookmark(wmOperatorType *ot)
364 {
365         /* identifiers */
366         ot->name= "Add Bookmark";
367         ot->idname= "FILE_OT_add_bookmark";
368         
369         /* api callbacks */
370         ot->invoke= bookmark_add_invoke;
371         ot->poll= ED_operator_file_active;
372 }
373
374 static int bookmark_delete_invoke(bContext *C, wmOperator *op, wmEvent *event)
375 {
376         ScrArea *sa= CTX_wm_area(C);
377         struct FSMenu* fsmenu = fsmenu_get();
378         int nentries = fsmenu_get_nentries(fsmenu, FS_CATEGORY_BOOKMARKS);
379         
380         if(RNA_struct_find_property(op->ptr, "index")) {
381                 int index = RNA_int_get(op->ptr, "index");
382                 if ( (index >-1) && (index < nentries)) {
383                         char name[FILE_MAX];
384                         
385                         fsmenu_remove_entry(fsmenu, FS_CATEGORY_BOOKMARKS, index);
386                         BLI_make_file_string("/", name, BLI_gethome(), ".Bfs");
387                         fsmenu_write_file(fsmenu, name);
388                         ED_area_tag_redraw(sa);
389                 }
390         }
391
392         return OPERATOR_FINISHED;
393 }
394
395 void FILE_OT_delete_bookmark(wmOperatorType *ot)
396 {
397         /* identifiers */
398         ot->name= "Delete Bookmark";
399         ot->idname= "FILE_OT_delete_bookmark";
400         
401         /* api callbacks */
402         ot->invoke= bookmark_delete_invoke;
403         ot->poll= ED_operator_file_active;
404
405         RNA_def_int(ot->srna, "index", -1, -1, 20000, "Index", "", -1, 20000);
406 }
407
408
409 static int loadimages_invoke(bContext *C, wmOperator *op, wmEvent *event)
410 {
411         ScrArea *sa= CTX_wm_area(C);
412         SpaceFile *sfile= CTX_wm_space_file(C);
413         if (sfile->files) {
414                 filelist_loadimage_timer(sfile->files);
415                 if (filelist_changed(sfile->files)) {
416                         ED_area_tag_redraw(sa);
417                 }
418         }
419
420         return OPERATOR_FINISHED;
421 }
422
423 void FILE_OT_loadimages(wmOperatorType *ot)
424 {
425         
426         /* identifiers */
427         ot->name= "Load Images";
428         ot->idname= "FILE_OT_loadimages";
429         
430         /* api callbacks */
431         ot->invoke= loadimages_invoke;
432         
433         ot->poll= ED_operator_file_active;
434 }
435
436 int file_hilight_set(SpaceFile *sfile, ARegion *ar, int mx, int my)
437 {
438         FileSelectParams* params;
439         int numfiles, actfile, origfile;
440         
441         if(sfile==NULL || sfile->files==NULL) return 0;
442
443         numfiles = filelist_numfiles(sfile->files);
444         params = ED_fileselect_get_params(sfile);
445
446         origfile= params->active_file;
447
448         mx -= ar->winrct.xmin;
449         my -= ar->winrct.ymin;
450
451         if(BLI_in_rcti(&ar->v2d.mask, mx, my)) {
452                 actfile = find_file_mouse(sfile, ar, mx , my, 0);
453
454                 if((actfile >= 0) && (actfile < numfiles))
455                         params->active_file=actfile;
456                 else
457                         params->active_file= -1;
458         }
459         else
460                 params->active_file= -1;
461
462         return (params->active_file != origfile);
463 }
464
465 static int file_highlight_invoke(bContext *C, wmOperator *op, wmEvent *event)
466 {
467         ARegion *ar= CTX_wm_region(C);
468         SpaceFile *sfile= CTX_wm_space_file(C);
469
470         if(!file_hilight_set(sfile, ar, event->x, event->y))
471                 return OPERATOR_CANCELLED;
472
473         ED_area_tag_redraw(CTX_wm_area(C));
474         
475         return OPERATOR_FINISHED;
476 }
477
478 void FILE_OT_highlight(struct wmOperatorType *ot)
479 {
480         /* identifiers */
481         ot->name= "Highlight File";
482         ot->idname= "FILE_OT_highlight";
483         
484         /* api callbacks */
485         ot->invoke= file_highlight_invoke;
486         ot->poll= ED_operator_file_active;
487 }
488
489 int file_cancel_exec(bContext *C, wmOperator *unused)
490 {
491         SpaceFile *sfile= CTX_wm_space_file(C);
492
493         folderlist_free(sfile->folders_prev);
494         folderlist_free(sfile->folders_next);
495
496         WM_event_fileselect_event(C, sfile->op, EVT_FILESELECT_CANCEL);
497         sfile->op = NULL;
498         
499         return OPERATOR_FINISHED;
500 }
501
502 void FILE_OT_cancel(struct wmOperatorType *ot)
503 {
504         /* identifiers */
505         ot->name= "Cancel File Load";
506         ot->idname= "FILE_OT_cancel";
507         
508         /* api callbacks */
509         ot->exec= file_cancel_exec;
510         ot->poll= ED_operator_file_active;
511 }
512
513 /* sends events now, so things get handled on windowqueue level */
514 int file_exec(bContext *C, wmOperator *unused)
515 {
516         SpaceFile *sfile= CTX_wm_space_file(C);
517         char name[FILE_MAX];
518         
519         if(sfile->op) {
520                 wmOperator *op= sfile->op;
521                 
522                 sfile->op = NULL;
523                 BLI_strncpy(name, sfile->params->dir, sizeof(name));
524                 strcat(name, sfile->params->file);
525                 RNA_string_set(op->ptr, "filename", name);
526                 
527                 /* some ops have multiple files to select */
528                 {
529                         PointerRNA itemptr;
530                         int i, numfiles = filelist_numfiles(sfile->files);
531                         struct direntry *file;
532                         if(RNA_struct_find_property(op->ptr, "files")) {
533                                 for (i=0; i<numfiles; i++) {
534                                         file = filelist_file(sfile->files, i);
535                                         if(file->flags & ACTIVE) {
536                                                 if ((file->type & S_IFDIR)==0) {
537                                                         RNA_collection_add(op->ptr, "files", &itemptr);
538                                                         RNA_string_set(&itemptr, "name", file->relname);
539                                                 }
540                                         }
541                                 }
542                         }
543                         
544                         if(RNA_struct_find_property(op->ptr, "dirs")) {
545                                 for (i=0; i<numfiles; i++) {
546                                         file = filelist_file(sfile->files, i);
547                                         if(file->flags & ACTIVE) {
548                                                 if ((file->type & S_IFDIR)) {
549                                                         RNA_collection_add(op->ptr, "dirs", &itemptr);
550                                                         RNA_string_set(&itemptr, "name", file->relname);
551                                                 }
552                                         }
553                                 }
554                         }
555                 }
556                 
557                 folderlist_free(sfile->folders_prev);
558                 folderlist_free(sfile->folders_next);
559
560                 fsmenu_insert_entry(fsmenu_get(), FS_CATEGORY_RECENT, sfile->params->dir,0, 1);
561                 BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
562                 fsmenu_write_file(fsmenu_get(), name);
563                 WM_event_fileselect_event(C, op, EVT_FILESELECT_EXEC);
564         }
565                                 
566         return OPERATOR_FINISHED;
567 }
568
569 void FILE_OT_exec(struct wmOperatorType *ot)
570 {
571         /* identifiers */
572         ot->name= "Execute File Window";
573         ot->idname= "FILE_OT_exec";
574         
575         /* api callbacks */
576         ot->exec= file_exec;
577         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
578 }
579
580
581 int file_parent_exec(bContext *C, wmOperator *unused)
582 {
583         SpaceFile *sfile= CTX_wm_space_file(C);
584         
585         if(sfile->params) {
586                 if (BLI_has_parent(sfile->params->dir)) {
587                         BLI_parent_dir(sfile->params->dir);
588                         BLI_cleanup_dir(G.sce, sfile->params->dir);
589                         file_change_dir(sfile);
590                         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
591                 }
592         }               
593         
594         return OPERATOR_FINISHED;
595
596 }
597
598
599 void FILE_OT_parent(struct wmOperatorType *ot)
600 {
601         /* identifiers */
602         ot->name= "Parent File";
603         ot->idname= "FILE_OT_parent";
604         
605         /* api callbacks */
606         ot->exec= file_parent_exec;
607         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
608 }
609
610
611 int file_refresh_exec(bContext *C, wmOperator *unused)
612 {
613         SpaceFile *sfile= CTX_wm_space_file(C);
614         
615         file_change_dir(sfile);
616
617         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
618
619         return OPERATOR_FINISHED;
620
621 }
622
623 void FILE_OT_previous(struct wmOperatorType *ot)
624 {
625         /* identifiers */
626         ot->name= "Previous Folder";
627         ot->idname= "FILE_OT_previous";
628         
629         /* api callbacks */
630         ot->exec= file_previous_exec;
631         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
632 }
633
634 int file_previous_exec(bContext *C, wmOperator *unused)
635 {
636         SpaceFile *sfile= CTX_wm_space_file(C);
637
638         if(sfile->params) {
639                 if (!sfile->folders_next)
640                         sfile->folders_next = folderlist_new();
641
642                 folderlist_pushdir(sfile->folders_next, sfile->params->dir);
643                 folderlist_popdir(sfile->folders_prev, sfile->params->dir);
644                 folderlist_pushdir(sfile->folders_next, sfile->params->dir);
645
646                 file_change_dir(sfile);
647         }
648         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
649
650         return OPERATOR_FINISHED;
651 }
652
653 void FILE_OT_next(struct wmOperatorType *ot)
654 {
655         /* identifiers */
656         ot->name= "Next Folder";
657         ot->idname= "FILE_OT_next";
658         
659         /* api callbacks */
660         ot->exec= file_next_exec;
661         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
662 }
663
664 int file_next_exec(bContext *C, wmOperator *unused)
665 {
666         SpaceFile *sfile= CTX_wm_space_file(C);
667                 if(sfile->params) {
668                         if (!sfile->folders_next)
669                         sfile->folders_next = folderlist_new();
670
671                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
672                 folderlist_popdir(sfile->folders_next, sfile->params->dir);
673
674                 // update folder_prev so we can check for it in folderlist_clear_next()
675                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
676
677                 file_change_dir(sfile);
678         }               
679         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
680
681         return OPERATOR_FINISHED;
682 }
683
684 int file_directory_new_exec(bContext *C, wmOperator *unused)
685 {
686         char tmpstr[FILE_MAX];
687         char tmpdir[FILE_MAXFILE];
688         int i = 1;
689
690         SpaceFile *sfile= CTX_wm_space_file(C);
691         
692         if(sfile->params) {
693                  
694                 BLI_strncpy(tmpstr, sfile->params->dir, FILE_MAX);
695                 BLI_join_dirfile(tmpstr, tmpstr, "New Folder");
696                 while (BLI_exists(tmpstr)) {
697                         BLI_snprintf(tmpdir, FILE_MAXFILE, "New Folder(%d)", i++);
698                         BLI_strncpy(tmpstr, sfile->params->dir, FILE_MAX);
699                         BLI_join_dirfile(tmpstr, tmpstr, tmpdir);
700                 }
701                 BLI_recurdir_fileops(tmpstr);
702                 if (!BLI_exists(tmpstr)) {
703                         filelist_free(sfile->files);
704                         filelist_parent(sfile->files);
705                         BLI_strncpy(sfile->params->dir, filelist_dir(sfile->files), FILE_MAX);
706                 } 
707         }               
708         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
709
710         return OPERATOR_FINISHED;
711 }
712
713
714 void FILE_OT_directory_new(struct wmOperatorType *ot)
715 {
716         /* identifiers */
717         ot->name= "Create New Directory";
718         ot->idname= "FILE_OT_directory_new";
719         ot->description= "Create a new directory";
720         
721         /* api callbacks */
722         ot->invoke= WM_operator_confirm;
723         ot->exec= file_directory_new_exec;
724         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
725 }
726
727 int file_directory_exec(bContext *C, wmOperator *unused)
728 {
729         char tmpstr[FILE_MAX];
730
731         SpaceFile *sfile= CTX_wm_space_file(C);
732         
733         if(sfile->params) {
734
735                 if ( sfile->params->dir[0] == '~' ) {
736                         if (sfile->params->dir[1] == '\0') {
737                                 BLI_strncpy(sfile->params->dir, BLI_gethome(), sizeof(sfile->params->dir) );
738                         } else {
739                                 /* replace ~ with home */
740                                 char homestr[FILE_MAX];
741                                 char *d = &sfile->params->dir[1];
742
743                                 while ( (*d == '\\') || (*d == '/') )
744                                         d++;
745                                 BLI_strncpy(homestr,  BLI_gethome(), FILE_MAX);
746                                 BLI_join_dirfile(tmpstr, homestr, d);
747                                 BLI_strncpy(sfile->params->dir, tmpstr, sizeof(sfile->params->dir));
748                         }
749                 }
750 #ifdef WIN32
751                 if (sfile->params->dir[0] == '\0')
752                         get_default_root(sfile->params->dir);
753 #endif
754                 BLI_cleanup_dir(G.sce, sfile->params->dir);
755                 BLI_add_slash(sfile->params->dir);
756                 file_change_dir(sfile);
757                 WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
758         }               
759         
760
761         return OPERATOR_FINISHED;
762 }
763
764 int file_filename_exec(bContext *C, wmOperator *unused)
765 {
766         SpaceFile *sfile= CTX_wm_space_file(C);
767         
768         if(sfile->params) {
769                 if (file_select_match(sfile, sfile->params->file))
770                 {
771                         sfile->params->file[0] = '\0';
772                         WM_event_add_notifier(C, NC_FILE|ND_PARAMS, NULL);
773                 }
774         }               
775
776         return OPERATOR_FINISHED;
777 }
778
779
780 void FILE_OT_refresh(struct wmOperatorType *ot)
781 {
782         /* identifiers */
783         ot->name= "Refresh Filelist";
784         ot->idname= "FILE_OT_refresh";
785         
786         /* api callbacks */
787         ot->exec= file_refresh_exec;
788         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
789 }
790
791 int file_hidedot_exec(bContext *C, wmOperator *unused)
792 {
793         SpaceFile *sfile= CTX_wm_space_file(C);
794         
795         if(sfile->params) {
796                 sfile->params->flag ^= FILE_HIDE_DOT;
797                 filelist_free(sfile->files);
798                 sfile->params->active_file = -1;
799                 WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
800         }
801         
802         return OPERATOR_FINISHED;
803
804 }
805
806
807 void FILE_OT_hidedot(struct wmOperatorType *ot)
808 {
809         /* identifiers */
810         ot->name= "Toggle Hide Dot Files";
811         ot->idname= "FILE_OT_hidedot";
812         
813         /* api callbacks */
814         ot->exec= file_hidedot_exec;
815         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
816 }
817
818 struct ARegion *file_buttons_region(struct ScrArea *sa)
819 {
820         ARegion *ar, *arnew;
821         
822         for(ar= sa->regionbase.first; ar; ar= ar->next)
823                 if(ar->regiontype==RGN_TYPE_CHANNELS)
824                         return ar;
825
826         /* add subdiv level; after header */
827         for(ar= sa->regionbase.first; ar; ar= ar->next)
828                 if(ar->regiontype==RGN_TYPE_HEADER)
829                         break;
830         
831         /* is error! */
832         if(ar==NULL) return NULL;
833         
834         arnew= MEM_callocN(sizeof(ARegion), "buttons for file panels");
835         
836         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
837         arnew->regiontype= RGN_TYPE_CHANNELS;
838         arnew->alignment= RGN_ALIGN_LEFT;
839         
840         arnew->flag = RGN_FLAG_HIDDEN;
841         
842         return arnew;
843 }
844
845 int file_bookmark_toggle_exec(bContext *C, wmOperator *unused)
846 {
847         ScrArea *sa= CTX_wm_area(C);
848         ARegion *ar= file_buttons_region(sa);
849         
850         if(ar) {
851                 ar->flag ^= RGN_FLAG_HIDDEN;
852                 ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
853                 
854                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
855                 ED_area_tag_redraw(sa);
856         }
857         return OPERATOR_FINISHED;
858 }
859
860 void FILE_OT_bookmark_toggle(struct wmOperatorType *ot)
861 {
862         /* identifiers */
863         ot->name= "Toggle Bookmarks";
864         ot->idname= "FILE_OT_bookmark_toggle";
865         
866         /* api callbacks */
867         ot->exec= file_bookmark_toggle_exec;
868         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
869 }
870
871
872 int file_filenum_exec(bContext *C, wmOperator *op)
873 {
874         SpaceFile *sfile= CTX_wm_space_file(C);
875         
876         int inc = RNA_int_get(op->ptr, "increment");
877         if(sfile->params && (inc != 0)) {
878                 BLI_newname(sfile->params->file, inc);
879                 WM_event_add_notifier(C, NC_WINDOW, NULL);
880         }
881         
882         return OPERATOR_FINISHED;
883
884 }
885
886 void FILE_OT_filenum(struct wmOperatorType *ot)
887 {
888         /* identifiers */
889         ot->name= "Increment Number in Filename";
890         ot->idname= "FILE_OT_filenum";
891         
892         /* api callbacks */
893         ot->exec= file_filenum_exec;
894         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
895
896         /* props */
897         RNA_def_int(ot->srna, "increment", 1, 0, 100, "Increment", "", 0,100);
898 }
899
900 int file_rename_exec(bContext *C, wmOperator *op)
901 {
902         ScrArea *sa= CTX_wm_area(C);
903         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
904         
905         if(sfile->params) {
906                 int idx = sfile->params->active_file;
907                 int numfiles = filelist_numfiles(sfile->files);
908                 if ( (0<=idx) && (idx<numfiles) ) {
909                         struct direntry *file= filelist_file(sfile->files, idx);
910                         file->flags |= EDITING;
911                 }
912                 ED_area_tag_redraw(sa);
913         }
914         
915         return OPERATOR_FINISHED;
916
917 }
918
919 void FILE_OT_rename(struct wmOperatorType *ot)
920 {
921         /* identifiers */
922         ot->name= "Rename File or Directory";
923         ot->idname= "FILE_OT_rename";
924         
925         /* api callbacks */
926         ot->exec= file_rename_exec;
927         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
928
929 }
930
931 int file_delete_poll(bContext *C)
932 {
933         int poll = ED_operator_file_active(C);
934         SpaceFile *sfile= CTX_wm_space_file(C);
935         struct direntry* file;
936
937         if (sfile && sfile->params) {
938                 if (sfile->params->active_file < 0) { 
939                         poll= 0;
940                 } else {
941                         file = filelist_file(sfile->files, sfile->params->active_file);
942                         if (file && S_ISDIR(file->type)) poll= 0;
943                 }
944         }
945         else
946                 poll= 0;
947                 
948         return poll;
949 }
950
951 int file_delete_exec(bContext *C, wmOperator *op)
952 {
953         char str[FILE_MAX];
954         SpaceFile *sfile= CTX_wm_space_file(C);
955         struct direntry* file;
956         
957         
958         file = filelist_file(sfile->files, sfile->params->active_file);
959         BLI_make_file_string(G.sce, str, sfile->params->dir, file->relname);
960         BLI_delete(str, 0, 0);  
961         WM_event_add_notifier(C, NC_FILE | ND_FILELIST, NULL);
962         
963         return OPERATOR_FINISHED;
964
965 }
966
967 void FILE_OT_delete(struct wmOperatorType *ot)
968 {
969         /* identifiers */
970         ot->name= "Delete File";
971         ot->idname= "FILE_OT_delete";
972         
973         /* api callbacks */
974         ot->invoke= WM_operator_confirm;
975         ot->exec= file_delete_exec;
976         ot->poll= file_delete_poll; /* <- important, handler is on window level */
977 }
978