2.5 file browser
[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                                 strcat(params->dir, file->relname);
146                                 strcat(params->dir,"/");
147                                 params->file[0] = '\0';
148                                 BLI_cleanup_dir(G.sce, params->dir);
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                 BLI_parent_dir(sfile->params->dir);
561                 file_change_dir(sfile);
562         }               
563         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
564
565         return OPERATOR_FINISHED;
566
567 }
568
569
570 void FILE_OT_parent(struct wmOperatorType *ot)
571 {
572         /* identifiers */
573         ot->name= "Parent File";
574         ot->idname= "FILE_OT_parent";
575         
576         /* api callbacks */
577         ot->exec= file_parent_exec;
578         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
579 }
580
581
582 int file_refresh_exec(bContext *C, wmOperator *unused)
583 {
584         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
585         
586         file_change_dir(sfile);
587
588         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
589
590         return OPERATOR_FINISHED;
591
592 }
593
594 void FILE_OT_previous(struct wmOperatorType *ot)
595 {
596         /* identifiers */
597         ot->name= "Previous Folder";
598         ot->idname= "FILE_OT_previous";
599         
600         /* api callbacks */
601         ot->exec= file_previous_exec;
602         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
603 }
604
605 int file_previous_exec(bContext *C, wmOperator *unused)
606 {
607         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
608
609         if(sfile->params) {
610                 if (!sfile->folders_next)
611                         sfile->folders_next = folderlist_new();
612
613                 folderlist_pushdir(sfile->folders_next, sfile->params->dir);
614                 folderlist_popdir(sfile->folders_prev, sfile->params->dir);
615                 folderlist_pushdir(sfile->folders_next, sfile->params->dir);
616
617                 file_change_dir(sfile);
618         }
619         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
620
621         return OPERATOR_FINISHED;
622 }
623
624 void FILE_OT_next(struct wmOperatorType *ot)
625 {
626         /* identifiers */
627         ot->name= "Next Folder";
628         ot->idname= "FILE_OT_next";
629         
630         /* api callbacks */
631         ot->exec= file_next_exec;
632         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
633 }
634
635 int file_next_exec(bContext *C, wmOperator *unused)
636 {
637         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
638                 if(sfile->params) {
639                         if (!sfile->folders_next)
640                         sfile->folders_next = folderlist_new();
641
642                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
643                 folderlist_popdir(sfile->folders_next, sfile->params->dir);
644
645                 // update folder_prev so we can check for it in folderlist_clear_next()
646                 folderlist_pushdir(sfile->folders_prev, sfile->params->dir);
647
648                 file_change_dir(sfile);
649         }               
650         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
651
652         return OPERATOR_FINISHED;
653 }
654
655 int file_directory_new_exec(bContext *C, wmOperator *unused)
656 {
657         char tmpstr[FILE_MAX];
658         char tmpdir[FILE_MAXFILE];
659         int i = 1;
660
661         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
662         
663         if(sfile->params) {
664                  
665                 BLI_strncpy(tmpstr, sfile->params->dir, FILE_MAX);
666                 BLI_join_dirfile(tmpstr, tmpstr, "New Folder");
667                 while (BLI_exists(tmpstr)) {
668                         BLI_snprintf(tmpdir, FILE_MAXFILE, "New Folder(%d)", i++);
669                         BLI_strncpy(tmpstr, sfile->params->dir, FILE_MAX);
670                         BLI_join_dirfile(tmpstr, tmpstr, tmpdir);
671                 }
672                 BLI_recurdir_fileops(tmpstr);
673                 if (!BLI_exists(tmpstr)) {
674                         filelist_free(sfile->files);
675                         filelist_parent(sfile->files);
676                         BLI_strncpy(sfile->params->dir, filelist_dir(sfile->files), FILE_MAX);
677                 } 
678         }               
679         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
680
681         return OPERATOR_FINISHED;
682 }
683
684
685 void FILE_OT_directory_new(struct wmOperatorType *ot)
686 {
687         /* identifiers */
688         ot->name= "Create New Directory";
689         ot->idname= "FILE_OT_directory_new";
690         
691         /* api callbacks */
692         ot->invoke= WM_operator_confirm;
693         ot->exec= file_directory_new_exec;
694         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
695 }
696
697 int file_directory_exec(bContext *C, wmOperator *unused)
698 {
699         char tmpstr[FILE_MAX];
700
701         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
702         
703         if(sfile->params) {
704
705                 if ( sfile->params->dir[0] == '~' ) {
706                         if (sfile->params->dir[1] == '\0') {
707                                 BLI_strncpy(sfile->params->dir, BLI_gethome(), sizeof(sfile->params->dir) );
708                         } else {
709                                 /* replace ~ with home */
710                                 char homestr[FILE_MAX];
711                                 char *d = &sfile->params->dir[1];
712
713                                 while ( (*d == '\\') || (*d == '/') )
714                                         d++;
715                                 BLI_strncpy(homestr,  BLI_gethome(), FILE_MAX);
716                                 BLI_add_slash(homestr);
717                                 BLI_join_dirfile(tmpstr, homestr, d);
718                                 BLI_strncpy(sfile->params->dir, tmpstr, sizeof(sfile->params->dir));
719                         }
720                 }
721
722                 file_change_dir(sfile);
723         }               
724         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
725
726         return OPERATOR_FINISHED;
727 }
728
729 int file_filename_exec(bContext *C, wmOperator *unused)
730 {
731         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
732         
733         if(sfile->params) {
734                 if (file_select_match(sfile, sfile->params->file))
735                 {
736                         sfile->params->file[0] = '\0';
737                         WM_event_add_notifier(C, NC_FILE|ND_PARAMS, NULL);
738                 }
739         }               
740
741         return OPERATOR_FINISHED;
742 }
743
744
745 void FILE_OT_refresh(struct wmOperatorType *ot)
746 {
747         /* identifiers */
748         ot->name= "Refresh Filelist";
749         ot->idname= "FILE_OT_refresh";
750         
751         /* api callbacks */
752         ot->exec= file_refresh_exec;
753         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
754 }
755
756 int file_hidedot_exec(bContext *C, wmOperator *unused)
757 {
758         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
759         
760         if(sfile->params) {
761                 sfile->params->flag ^= FILE_HIDE_DOT;
762                 filelist_free(sfile->files);
763                 sfile->params->active_file = -1;
764                 WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
765         }
766         
767         return OPERATOR_FINISHED;
768
769 }
770
771
772 void FILE_OT_hidedot(struct wmOperatorType *ot)
773 {
774         /* identifiers */
775         ot->name= "Toggle Hide Dot Files";
776         ot->idname= "FILE_OT_hidedot";
777         
778         /* api callbacks */
779         ot->exec= file_hidedot_exec;
780         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
781 }
782
783 struct ARegion *file_buttons_region(struct ScrArea *sa)
784 {
785         ARegion *ar, *arnew;
786         
787         for(ar= sa->regionbase.first; ar; ar= ar->next)
788                 if(ar->regiontype==RGN_TYPE_CHANNELS)
789                         return ar;
790
791         /* add subdiv level; after header */
792         for(ar= sa->regionbase.first; ar; ar= ar->next)
793                 if(ar->regiontype==RGN_TYPE_HEADER)
794                         break;
795         
796         /* is error! */
797         if(ar==NULL) return NULL;
798         
799         arnew= MEM_callocN(sizeof(ARegion), "buttons for file panels");
800         
801         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
802         arnew->regiontype= RGN_TYPE_CHANNELS;
803         arnew->alignment= RGN_ALIGN_LEFT;
804         
805         arnew->flag = RGN_FLAG_HIDDEN;
806         
807         return arnew;
808 }
809
810 int file_bookmark_toggle_exec(bContext *C, wmOperator *unused)
811 {
812         ScrArea *sa= CTX_wm_area(C);
813         ARegion *ar= file_buttons_region(sa);
814         
815         if(ar) {
816                 ar->flag ^= RGN_FLAG_HIDDEN;
817                 ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
818                 
819                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
820                 ED_area_tag_redraw(sa);
821         }
822         return OPERATOR_FINISHED;
823 }
824
825 void FILE_OT_bookmark_toggle(struct wmOperatorType *ot)
826 {
827         /* identifiers */
828         ot->name= "Toggle Bookmarks";
829         ot->idname= "FILE_OT_bookmark_toggle";
830         
831         /* api callbacks */
832         ot->exec= file_bookmark_toggle_exec;
833         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
834 }
835
836
837 int file_filenum_exec(bContext *C, wmOperator *op)
838 {
839         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
840         
841         int inc = RNA_int_get(op->ptr, "increment");
842         if(sfile->params && (inc != 0)) {
843                 BLI_newname(sfile->params->file, inc);
844                 WM_event_add_notifier(C, NC_WINDOW, NULL);
845         }
846         
847         return OPERATOR_FINISHED;
848
849 }
850
851 void FILE_OT_filenum(struct wmOperatorType *ot)
852 {
853         /* identifiers */
854         ot->name= "Increment Number in Filename";
855         ot->idname= "FILE_OT_filenum";
856         
857         /* api callbacks */
858         ot->exec= file_filenum_exec;
859         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
860
861         /* props */
862         RNA_def_int(ot->srna, "increment", 1, 0, 100, "Increment", "", 0,100);
863 }
864
865 int file_delete_poll(bContext *C)
866 {
867         int poll = ED_operator_file_active(C);
868         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
869         struct direntry* file;
870
871         if(!sfile->params ) poll= 0;
872
873         if (sfile->params->active_file < 0) { 
874                 poll= 0;
875         } else {
876                 file = filelist_file(sfile->files, sfile->params->active_file);
877                 if (file && S_ISDIR(file->type)) poll= 0;
878         }
879         return poll;
880 }
881
882 int file_delete_exec(bContext *C, wmOperator *op)
883 {
884         char str[FILE_MAX];
885         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
886         struct direntry* file;
887         
888         
889         file = filelist_file(sfile->files, sfile->params->active_file);
890         BLI_make_file_string(G.sce, str, sfile->params->dir, file->relname);
891         BLI_delete(str, 0, 0);  
892         WM_event_add_notifier(C, NC_FILE | ND_FILELIST, NULL);
893         
894         return OPERATOR_FINISHED;
895
896 }
897
898 void FILE_OT_delete(struct wmOperatorType *ot)
899 {
900         /* identifiers */
901         ot->name= "Delete File";
902         ot->idname= "FILE_OT_delete";
903         
904         /* api callbacks */
905         ot->invoke= WM_operator_confirm;
906         ot->exec= file_delete_exec;
907         ot->poll= file_delete_poll; /* <- important, handler is on window level */
908 }
909