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