NLA SoC: Merge from 2.5
[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 static void file_select(SpaceFile* sfile, ARegion* ar, const rcti* rect, short val)
109 {
110         int first_file = -1;
111         int last_file = -1;
112         int act_file;
113         short selecting = (val == LEFTMOUSE);
114         FileSelectParams *params = ED_fileselect_get_params(sfile);
115         // FileLayout *layout = ED_fileselect_get_layout(sfile, ar);
116
117         int numfiles = filelist_numfiles(sfile->files);
118
119         params->selstate = NOTACTIVE;
120         first_file = find_file_mouse(sfile, ar, rect->xmin, rect->ymax, 1);
121         last_file = find_file_mouse(sfile, ar, rect->xmax, rect->ymin, 1);
122         
123         /* select all valid files between first and last indicated */
124         if ( (first_file >= 0) && (first_file < numfiles) && (last_file >= 0) && (last_file < numfiles) ) {
125                 for (act_file = first_file; act_file <= last_file; act_file++) {
126                         struct direntry* file = filelist_file(sfile->files, act_file);
127                         if (selecting) 
128                                 file->flags |= ACTIVE;
129                         else
130                                 file->flags &= ~ACTIVE;
131                 }
132         }
133
134         /* make the last file active */
135         if (selecting && (last_file >= 0 && last_file < numfiles)) {
136                 struct direntry* file = filelist_file(sfile->files, last_file);
137                 params->active_file = last_file;
138
139                 if(file && S_ISDIR(file->type)) {
140                         /* the path is too long! */
141                         if (strlen(params->dir) + strlen(file->relname) >= FILE_MAX ) 
142                         {
143                                 // XXX error("Path too long, cannot enter this directory");
144                         } else {
145                                 BLI_cleanup_dir(G.sce, params->dir);
146                                 strcat(params->dir, file->relname);
147                                 BLI_add_slash(params->dir);
148                                 params->file[0] = '\0';
149                                 file_change_dir(sfile);
150                         }
151                 }
152                 else if (file)
153                 {
154                         if (file->relname) {
155                                 BLI_strncpy(params->file, file->relname, FILE_MAXFILE);
156                                 /* XXX
157                                 if(event==MIDDLEMOUSE && filelist_gettype(sfile->files)) 
158                                         imasel_execute(sfile);
159                                 */
160                         }
161                         
162                 }       
163                 /* XXX
164                 if(BIF_filelist_gettype(sfile->files)==FILE_MAIN) {
165                         active_imasel_object(sfile);
166                 }
167                 */
168         }
169 }
170
171
172
173 static int file_border_select_exec(bContext *C, wmOperator *op)
174 {
175         ARegion *ar= CTX_wm_region(C);
176         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
177         short val;
178         rcti rect;
179
180         val= RNA_int_get(op->ptr, "event_type");
181         rect.xmin= RNA_int_get(op->ptr, "xmin");
182         rect.ymin= RNA_int_get(op->ptr, "ymin");
183         rect.xmax= RNA_int_get(op->ptr, "xmax");
184         rect.ymax= RNA_int_get(op->ptr, "ymax");
185
186         BLI_isect_rcti(&(ar->v2d.mask), &rect, &rect);
187         
188         file_select(sfile, ar, &rect, val );
189         WM_event_add_notifier(C, NC_WINDOW, NULL);
190         return OPERATOR_FINISHED;
191 }
192
193 void FILE_OT_select_border(wmOperatorType *ot)
194 {
195         /* identifiers */
196         ot->name= "Activate/Select File";
197         ot->idname= "FILE_OT_select_border";
198         
199         /* api callbacks */
200         ot->invoke= WM_border_select_invoke;
201         ot->exec= file_border_select_exec;
202         ot->modal= WM_border_select_modal;
203
204         /* rna */
205         RNA_def_int(ot->srna, "event_type", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
206         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
207         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
208         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
209         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
210
211         ot->poll= ED_operator_file_active;
212 }
213
214 static int file_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
215 {
216         ARegion *ar= CTX_wm_region(C);
217         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
218         short val;
219         rcti rect;
220
221         rect.xmin = rect.xmax = event->x - ar->winrct.xmin;
222         rect.ymin = rect.ymax = event->y - ar->winrct.ymin;
223         val = event->val;
224
225         if (BLI_in_rcti(&ar->v2d.mask, rect.xmin, rect.ymin)) { 
226
227                 /* single select, deselect all selected first */
228                 file_deselect_all(sfile);
229                 file_select(sfile, ar, &rect, val );
230                 WM_event_add_notifier(C, NC_FILE|ND_PARAMS, NULL);
231         }
232         return OPERATOR_FINISHED;
233 }
234
235 void FILE_OT_select(wmOperatorType *ot)
236 {
237         /* identifiers */
238         ot->name= "Activate/Select File";
239         ot->idname= "FILE_OT_select";
240         
241         /* api callbacks */
242         ot->invoke= file_select_invoke;
243
244         /* rna */
245
246         ot->poll= ED_operator_file_active;
247 }
248
249 static int file_select_all_invoke(bContext *C, wmOperator *op, wmEvent *event)
250 {
251         ScrArea *sa= CTX_wm_area(C);
252         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
253         int numfiles = filelist_numfiles(sfile->files);
254         int i;
255         int select = 1;
256
257         /* if any file is selected, deselect all first */
258         for ( i=0; i < numfiles; ++i) {
259                 struct direntry* file = filelist_file(sfile->files, i);
260                 if (file && (file->flags & ACTIVE)) {
261                         file->flags &= ~ACTIVE;
262                         select = 0;
263                         ED_area_tag_redraw(sa);
264                 }
265         }
266         /* select all only if previously no file was selected */
267         if (select) {
268                 for ( i=0; i < numfiles; ++i) {
269                         struct direntry* file = filelist_file(sfile->files, i);
270                         if(file && !S_ISDIR(file->type)) {
271                                 file->flags |= ACTIVE;
272                                 ED_area_tag_redraw(sa);
273                         }
274                 }
275         }
276         return OPERATOR_FINISHED;
277 }
278
279 void FILE_OT_select_all_toggle(wmOperatorType *ot)
280 {
281         /* identifiers */
282         ot->name= "Select/Deselect all files";
283         ot->idname= "FILE_OT_select_all_toggle";
284         
285         /* api callbacks */
286         ot->invoke= file_select_all_invoke;
287
288         /* rna */
289
290         ot->poll= ED_operator_file_active;
291 }
292
293 /* ---------- BOOKMARKS ----------- */
294
295 static int bookmark_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
296 {
297         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
298
299         if(RNA_struct_find_property(op->ptr, "dir")) {
300                 char entry[256];
301                 FileSelectParams* params = sfile->params;
302
303                 RNA_string_get(op->ptr, "dir", entry);
304                 BLI_strncpy(params->dir, entry, sizeof(params->dir));
305                 BLI_cleanup_dir(G.sce, params->dir);
306                 file_change_dir(sfile);                         
307                 params->file[0] = '\0';
308
309                 WM_event_add_notifier(C, NC_FILE|ND_PARAMS, NULL);
310         }
311         
312         return OPERATOR_FINISHED;
313 }
314
315 void FILE_OT_select_bookmark(wmOperatorType *ot)
316 {
317         /* identifiers */
318         ot->name= "Select Directory";
319         ot->idname= "FILE_OT_select_bookmark";
320         
321         /* api callbacks */
322         ot->invoke= bookmark_select_invoke;
323         ot->poll= ED_operator_file_active;
324
325         RNA_def_string(ot->srna, "dir", "", 256, "Dir", "");
326 }
327
328 static int bookmark_add_invoke(bContext *C, wmOperator *op, wmEvent *event)
329 {
330         ScrArea *sa= CTX_wm_area(C);
331         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
332         struct FSMenu* fsmenu = fsmenu_get();
333         struct FileSelectParams* params= ED_fileselect_get_params(sfile);
334
335         if (params->dir[0] != '\0') {
336                 char name[FILE_MAX];
337         
338                 fsmenu_insert_entry(fsmenu, FS_CATEGORY_BOOKMARKS, params->dir, 0, 1);
339                 BLI_make_file_string("/", name, BLI_gethome(), ".Bfs");
340                 fsmenu_write_file(fsmenu, name);
341         }
342
343         ED_area_tag_redraw(sa);
344         return OPERATOR_FINISHED;
345 }
346
347 void FILE_OT_add_bookmark(wmOperatorType *ot)
348 {
349         /* identifiers */
350         ot->name= "Add Bookmark";
351         ot->idname= "FILE_OT_add_bookmark";
352         
353         /* api callbacks */
354         ot->invoke= bookmark_add_invoke;
355         ot->poll= ED_operator_file_active;
356 }
357
358 static int bookmark_delete_invoke(bContext *C, wmOperator *op, wmEvent *event)
359 {
360         ScrArea *sa= CTX_wm_area(C);
361         struct FSMenu* fsmenu = fsmenu_get();
362         int nentries = fsmenu_get_nentries(fsmenu, FS_CATEGORY_BOOKMARKS);
363         
364         if(RNA_struct_find_property(op->ptr, "index")) {
365                 int index = RNA_int_get(op->ptr, "index");
366                 if ( (index >-1) && (index < nentries)) {
367                         char name[FILE_MAX];
368                         
369                         fsmenu_remove_entry(fsmenu, FS_CATEGORY_BOOKMARKS, index);
370                         BLI_make_file_string("/", name, BLI_gethome(), ".Bfs");
371                         fsmenu_write_file(fsmenu, name);
372                         ED_area_tag_redraw(sa);
373                 }
374         }
375
376         return OPERATOR_FINISHED;
377 }
378
379 void FILE_OT_delete_bookmark(wmOperatorType *ot)
380 {
381         /* identifiers */
382         ot->name= "Delete Bookmark";
383         ot->idname= "FILE_OT_delete_bookmark";
384         
385         /* api callbacks */
386         ot->invoke= bookmark_delete_invoke;
387         ot->poll= ED_operator_file_active;
388
389         RNA_def_int(ot->srna, "index", -1, -1, 20000, "Index", "", -1, 20000);
390 }
391
392
393 static int loadimages_invoke(bContext *C, wmOperator *op, wmEvent *event)
394 {
395         ScrArea *sa= CTX_wm_area(C);
396         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
397         if (sfile->files) {
398                 filelist_loadimage_timer(sfile->files);
399                 if (filelist_changed(sfile->files)) {
400                         ED_area_tag_redraw(sa);
401                 }
402         }
403
404         return OPERATOR_FINISHED;
405 }
406
407 void FILE_OT_loadimages(wmOperatorType *ot)
408 {
409         
410         /* identifiers */
411         ot->name= "Load Images";
412         ot->idname= "FILE_OT_loadimages";
413         
414         /* api callbacks */
415         ot->invoke= loadimages_invoke;
416         
417         ot->poll= ED_operator_file_active;
418 }
419
420 int file_hilight_set(SpaceFile *sfile, ARegion *ar, int mx, int my)
421 {
422         FileSelectParams* params;
423         int numfiles, actfile;
424         
425         if(sfile==NULL || sfile->files==NULL) return 0;
426         
427         numfiles = filelist_numfiles(sfile->files);
428         params = ED_fileselect_get_params(sfile);
429
430         actfile = find_file_mouse(sfile, ar, mx , my, 0);
431         
432         if (params && (actfile >= 0) && (actfile < numfiles) ) {
433                 params->active_file=actfile;
434                 return 1;
435         } 
436         params->active_file= -1;
437         return 0;
438 }
439
440 static int file_highlight_invoke(bContext *C, wmOperator *op, wmEvent *event)
441 {
442         ARegion *ar= CTX_wm_region(C);
443         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
444         
445         if( file_hilight_set(sfile, ar, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin)) {
446                 ED_area_tag_redraw(CTX_wm_area(C));
447         }
448         
449         return OPERATOR_FINISHED;
450 }
451
452 void FILE_OT_highlight(struct wmOperatorType *ot)
453 {
454         /* identifiers */
455         ot->name= "Highlight File";
456         ot->idname= "FILE_OT_highlight";
457         
458         /* api callbacks */
459         ot->invoke= file_highlight_invoke;
460         ot->poll= ED_operator_file_active;
461 }
462
463 int file_cancel_exec(bContext *C, wmOperator *unused)
464 {
465         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
466
467         folderlist_free(sfile->folders_prev);
468         folderlist_free(sfile->folders_next);
469
470         WM_event_fileselect_event(C, sfile->op, EVT_FILESELECT_CANCEL);
471         sfile->op = NULL;
472         
473         return OPERATOR_FINISHED;
474 }
475
476 void FILE_OT_cancel(struct wmOperatorType *ot)
477 {
478         /* identifiers */
479         ot->name= "Cancel File Load";
480         ot->idname= "FILE_OT_cancel";
481         
482         /* api callbacks */
483         ot->exec= file_cancel_exec;
484         ot->poll= ED_operator_file_active;
485 }
486
487 /* sends events now, so things get handled on windowqueue level */
488 int file_exec(bContext *C, wmOperator *unused)
489 {
490         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
491         char name[FILE_MAX];
492         
493         if(sfile->op) {
494                 wmOperator *op= sfile->op;
495                 
496                 sfile->op = NULL;
497                 BLI_strncpy(name, sfile->params->dir, sizeof(name));
498                 strcat(name, sfile->params->file);
499                 RNA_string_set(op->ptr, "filename", name);
500                 
501                 /* some ops have multiple files to select */
502                 {
503                         PointerRNA itemptr;
504                         int i, numfiles = filelist_numfiles(sfile->files);
505                         struct direntry *file;
506                         if(RNA_struct_find_property(op->ptr, "files")) {
507                                 for (i=0; i<numfiles; i++) {
508                                         file = filelist_file(sfile->files, i);
509                                         if(file->flags & ACTIVE) {
510                                                 if ((file->type & S_IFDIR)==0) {
511                                                         RNA_collection_add(op->ptr, "files", &itemptr);
512                                                         RNA_string_set(&itemptr, "name", file->relname);
513                                                 }
514                                         }
515                                 }
516                         }
517                         
518                         if(RNA_struct_find_property(op->ptr, "dirs")) {
519                                 for (i=0; i<numfiles; i++) {
520                                         file = filelist_file(sfile->files, i);
521                                         if(file->flags & ACTIVE) {
522                                                 if ((file->type & S_IFDIR)) {
523                                                         RNA_collection_add(op->ptr, "dirs", &itemptr);
524                                                         RNA_string_set(&itemptr, "name", file->relname);
525                                                 }
526                                         }
527                                 }
528                         }
529                 }
530                 
531                 folderlist_free(sfile->folders_prev);
532                 folderlist_free(sfile->folders_next);
533
534                 fsmenu_insert_entry(fsmenu_get(), FS_CATEGORY_RECENT, sfile->params->dir,0, 1);
535                 BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
536                 fsmenu_write_file(fsmenu_get(), name);
537                 WM_event_fileselect_event(C, op, EVT_FILESELECT_EXEC);
538         }
539                                 
540         return OPERATOR_FINISHED;
541 }
542
543 void FILE_OT_exec(struct wmOperatorType *ot)
544 {
545         /* identifiers */
546         ot->name= "Execute File Window";
547         ot->idname= "FILE_OT_exec";
548         
549         /* api callbacks */
550         ot->exec= file_exec;
551         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
552 }
553
554
555 int file_parent_exec(bContext *C, wmOperator *unused)
556 {
557         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
558         
559         if(sfile->params) {
560                 if (BLI_has_parent(sfile->params->dir)) {
561                         BLI_parent_dir(sfile->params->dir);
562                         file_change_dir(sfile);
563                         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
564                 }
565         }               
566         
567         return OPERATOR_FINISHED;
568
569 }
570
571
572 void FILE_OT_parent(struct wmOperatorType *ot)
573 {
574         /* identifiers */
575         ot->name= "Parent File";
576         ot->idname= "FILE_OT_parent";
577         
578         /* api callbacks */
579         ot->exec= file_parent_exec;
580         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
581 }
582
583
584 int file_refresh_exec(bContext *C, wmOperator *unused)
585 {
586         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
587         
588         file_change_dir(sfile);
589
590         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
591
592         return OPERATOR_FINISHED;
593
594 }
595
596 void FILE_OT_previous(struct wmOperatorType *ot)
597 {
598         /* identifiers */
599         ot->name= "Previous Folder";
600         ot->idname= "FILE_OT_previous";
601         
602         /* api callbacks */
603         ot->exec= file_previous_exec;
604         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
605 }
606
607 int file_previous_exec(bContext *C, wmOperator *unused)
608 {
609         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
610
611         if(sfile->params) {
612                 if (!sfile->folders_next)
613                         sfile->folders_next = folderlist_new();
614
615                 folderlist_pushdir(sfile->folders_next, sfile->params->dir);
616                 folderlist_popdir(sfile->folders_prev, sfile->params->dir);
617                 folderlist_pushdir(sfile->folders_next, sfile->params->dir);
618
619                 file_change_dir(sfile);
620         }
621         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
622
623         return OPERATOR_FINISHED;
624 }
625
626 void FILE_OT_next(struct wmOperatorType *ot)
627 {
628         /* identifiers */
629         ot->name= "Next Folder";
630         ot->idname= "FILE_OT_next";
631         
632         /* api callbacks */
633         ot->exec= file_next_exec;
634         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
635 }
636
637 int file_next_exec(bContext *C, wmOperator *unused)
638 {
639         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
640                 if(sfile->params) {
641                         if (!sfile->folders_next)
642                         sfile->folders_next = folderlist_new();
643
644                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
645                 folderlist_popdir(sfile->folders_next, sfile->params->dir);
646
647                 // update folder_prev so we can check for it in folderlist_clear_next()
648                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
649
650                 file_change_dir(sfile);
651         }               
652         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
653
654         return OPERATOR_FINISHED;
655 }
656
657 int file_directory_new_exec(bContext *C, wmOperator *unused)
658 {
659         char tmpstr[FILE_MAX];
660         char tmpdir[FILE_MAXFILE];
661         int i = 1;
662
663         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
664         
665         if(sfile->params) {
666                  
667                 BLI_strncpy(tmpstr, sfile->params->dir, FILE_MAX);
668                 BLI_join_dirfile(tmpstr, tmpstr, "New Folder");
669                 while (BLI_exists(tmpstr)) {
670                         BLI_snprintf(tmpdir, FILE_MAXFILE, "New Folder(%d)", i++);
671                         BLI_strncpy(tmpstr, sfile->params->dir, FILE_MAX);
672                         BLI_join_dirfile(tmpstr, tmpstr, tmpdir);
673                 }
674                 BLI_recurdir_fileops(tmpstr);
675                 if (!BLI_exists(tmpstr)) {
676                         filelist_free(sfile->files);
677                         filelist_parent(sfile->files);
678                         BLI_strncpy(sfile->params->dir, filelist_dir(sfile->files), FILE_MAX);
679                 } 
680         }               
681         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
682
683         return OPERATOR_FINISHED;
684 }
685
686
687 void FILE_OT_directory_new(struct wmOperatorType *ot)
688 {
689         /* identifiers */
690         ot->name= "Create New Directory";
691         ot->idname= "FILE_OT_directory_new";
692         
693         /* api callbacks */
694         ot->invoke= WM_operator_confirm;
695         ot->exec= file_directory_new_exec;
696         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
697 }
698
699 int file_directory_exec(bContext *C, wmOperator *unused)
700 {
701         char tmpstr[FILE_MAX];
702
703         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
704         
705         if(sfile->params) {
706
707                 if ( sfile->params->dir[0] == '~' ) {
708                         if (sfile->params->dir[1] == '\0') {
709                                 BLI_strncpy(sfile->params->dir, BLI_gethome(), sizeof(sfile->params->dir) );
710                         } else {
711                                 /* replace ~ with home */
712                                 char homestr[FILE_MAX];
713                                 char *d = &sfile->params->dir[1];
714
715                                 while ( (*d == '\\') || (*d == '/') )
716                                         d++;
717                                 BLI_strncpy(homestr,  BLI_gethome(), FILE_MAX);
718                                 BLI_add_slash(homestr);
719                                 BLI_join_dirfile(tmpstr, homestr, d);
720                                 BLI_strncpy(sfile->params->dir, tmpstr, sizeof(sfile->params->dir));
721                         }
722                 }
723 #ifdef WIN32
724                 if (sfile->params->dir[0] == '\0')
725                         get_default_root(sfile->params->dir);
726 #endif
727                 BLI_add_slash(sfile->params->dir);
728                 file_change_dir(sfile);
729                 WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
730         }               
731         
732
733         return OPERATOR_FINISHED;
734 }
735
736 int file_filename_exec(bContext *C, wmOperator *unused)
737 {
738         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
739         
740         if(sfile->params) {
741                 if (file_select_match(sfile, sfile->params->file))
742                 {
743                         sfile->params->file[0] = '\0';
744                         WM_event_add_notifier(C, NC_FILE|ND_PARAMS, NULL);
745                 }
746         }               
747
748         return OPERATOR_FINISHED;
749 }
750
751
752 void FILE_OT_refresh(struct wmOperatorType *ot)
753 {
754         /* identifiers */
755         ot->name= "Refresh Filelist";
756         ot->idname= "FILE_OT_refresh";
757         
758         /* api callbacks */
759         ot->exec= file_refresh_exec;
760         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
761 }
762
763 int file_hidedot_exec(bContext *C, wmOperator *unused)
764 {
765         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
766         
767         if(sfile->params) {
768                 sfile->params->flag ^= FILE_HIDE_DOT;
769                 filelist_free(sfile->files);
770                 sfile->params->active_file = -1;
771                 WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
772         }
773         
774         return OPERATOR_FINISHED;
775
776 }
777
778
779 void FILE_OT_hidedot(struct wmOperatorType *ot)
780 {
781         /* identifiers */
782         ot->name= "Toggle Hide Dot Files";
783         ot->idname= "FILE_OT_hidedot";
784         
785         /* api callbacks */
786         ot->exec= file_hidedot_exec;
787         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
788 }
789
790 struct ARegion *file_buttons_region(struct ScrArea *sa)
791 {
792         ARegion *ar, *arnew;
793         
794         for(ar= sa->regionbase.first; ar; ar= ar->next)
795                 if(ar->regiontype==RGN_TYPE_CHANNELS)
796                         return ar;
797
798         /* add subdiv level; after header */
799         for(ar= sa->regionbase.first; ar; ar= ar->next)
800                 if(ar->regiontype==RGN_TYPE_HEADER)
801                         break;
802         
803         /* is error! */
804         if(ar==NULL) return NULL;
805         
806         arnew= MEM_callocN(sizeof(ARegion), "buttons for file panels");
807         
808         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
809         arnew->regiontype= RGN_TYPE_CHANNELS;
810         arnew->alignment= RGN_ALIGN_LEFT;
811         
812         arnew->flag = RGN_FLAG_HIDDEN;
813         
814         return arnew;
815 }
816
817 int file_bookmark_toggle_exec(bContext *C, wmOperator *unused)
818 {
819         ScrArea *sa= CTX_wm_area(C);
820         ARegion *ar= file_buttons_region(sa);
821         
822         if(ar) {
823                 ar->flag ^= RGN_FLAG_HIDDEN;
824                 ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
825                 
826                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
827                 ED_area_tag_redraw(sa);
828         }
829         return OPERATOR_FINISHED;
830 }
831
832 void FILE_OT_bookmark_toggle(struct wmOperatorType *ot)
833 {
834         /* identifiers */
835         ot->name= "Toggle Bookmarks";
836         ot->idname= "FILE_OT_bookmark_toggle";
837         
838         /* api callbacks */
839         ot->exec= file_bookmark_toggle_exec;
840         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
841 }
842
843
844 int file_filenum_exec(bContext *C, wmOperator *op)
845 {
846         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
847         
848         int inc = RNA_int_get(op->ptr, "increment");
849         if(sfile->params && (inc != 0)) {
850                 BLI_newname(sfile->params->file, inc);
851                 WM_event_add_notifier(C, NC_WINDOW, NULL);
852         }
853         
854         return OPERATOR_FINISHED;
855
856 }
857
858 void FILE_OT_filenum(struct wmOperatorType *ot)
859 {
860         /* identifiers */
861         ot->name= "Increment Number in Filename";
862         ot->idname= "FILE_OT_filenum";
863         
864         /* api callbacks */
865         ot->exec= file_filenum_exec;
866         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
867
868         /* props */
869         RNA_def_int(ot->srna, "increment", 1, 0, 100, "Increment", "", 0,100);
870 }
871
872 int file_delete_poll(bContext *C)
873 {
874         int poll = ED_operator_file_active(C);
875         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
876         struct direntry* file;
877
878         if(!sfile->params ) poll= 0;
879
880         if (sfile->params->active_file < 0) { 
881                 poll= 0;
882         } else {
883                 file = filelist_file(sfile->files, sfile->params->active_file);
884                 if (file && S_ISDIR(file->type)) poll= 0;
885         }
886         return poll;
887 }
888
889 int file_delete_exec(bContext *C, wmOperator *op)
890 {
891         char str[FILE_MAX];
892         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
893         struct direntry* file;
894         
895         
896         file = filelist_file(sfile->files, sfile->params->active_file);
897         BLI_make_file_string(G.sce, str, sfile->params->dir, file->relname);
898         BLI_delete(str, 0, 0);  
899         WM_event_add_notifier(C, NC_FILE | ND_FILELIST, NULL);
900         
901         return OPERATOR_FINISHED;
902
903 }
904
905 void FILE_OT_delete(struct wmOperatorType *ot)
906 {
907         /* identifiers */
908         ot->name= "Delete File";
909         ot->idname= "FILE_OT_delete";
910         
911         /* api callbacks */
912         ot->invoke= WM_operator_confirm;
913         ot->exec= file_delete_exec;
914         ot->poll= file_delete_poll; /* <- important, handler is on window level */
915 }
916