small merge needed for testing new animsys in 2.5 BGE, mixing own changes with merge...
[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 "RNA_access.h"
46 #include "RNA_define.h"
47
48 #include "UI_interface.h"
49 #include "UI_view2d.h"
50
51 #include "WM_api.h"
52 #include "WM_types.h"
53
54 #include "file_intern.h"
55 #include "filelist.h"
56 #include "fsmenu.h"
57
58 #include <stdlib.h>
59 #include <string.h>
60 #include <stdio.h>
61
62 /* for events */
63 #define NOTACTIVE                       0
64 #define ACTIVATE                        1
65 #define INACTIVATE                      2
66
67 /* ---------- FILE SELECTION ------------ */
68
69 static int find_file_mouse(SpaceFile *sfile, struct ARegion* ar, short x, short y, short clamp)
70 {
71         float fx,fy;
72         int active_file = -1;
73         int numfiles = filelist_numfiles(sfile->files);
74         View2D* v2d = &ar->v2d;
75
76         UI_view2d_region_to_view(v2d, x, y, &fx, &fy);
77
78         active_file = ED_fileselect_layout_offset(sfile->layout, v2d->tot.xmin + fx, v2d->tot.ymax - fy);
79
80         if(active_file < 0) {
81                 if(clamp)       active_file=  0;
82                 else            active_file= -1;
83         }
84         else if(active_file >= numfiles) {
85                 if(clamp)       active_file=  numfiles-1;
86                 else            active_file= -1;
87         }
88         
89         return active_file;
90 }
91
92
93 static void file_deselect_all(SpaceFile* sfile)
94 {
95         int numfiles = filelist_numfiles(sfile->files);
96         int i;
97
98         for ( i=0; i < numfiles; ++i) {
99                 struct direntry* file = filelist_file(sfile->files, i);
100                 if (file && (file->flags & ACTIVE)) {
101                         file->flags &= ~ACTIVE;
102                 }
103         }
104 }
105
106 static void file_select(SpaceFile* sfile, ARegion* ar, const rcti* rect, short val)
107 {
108         int first_file = -1;
109         int last_file = -1;
110         int act_file;
111         short selecting = (val == LEFTMOUSE);
112         FileSelectParams *params = ED_fileselect_get_params(sfile);
113         // FileLayout *layout = ED_fileselect_get_layout(sfile, ar);
114
115         int numfiles = filelist_numfiles(sfile->files);
116
117         params->selstate = NOTACTIVE;
118         first_file = find_file_mouse(sfile, ar, rect->xmin, rect->ymax, 1);
119         last_file = find_file_mouse(sfile, ar, rect->xmax, rect->ymin, 1);
120         
121         /* select all valid files between first and last indicated */
122         if ( (first_file >= 0) && (first_file < numfiles) && (last_file >= 0) && (last_file < numfiles) ) {
123                 for (act_file = first_file; act_file <= last_file; act_file++) {
124                         struct direntry* file = filelist_file(sfile->files, act_file);
125                         if (selecting) 
126                                 file->flags |= ACTIVE;
127                         else
128                                 file->flags &= ~ACTIVE;
129                 }
130         }
131
132         /* make the last file active */
133         if (selecting && (last_file >= 0 && last_file < numfiles)) {
134                 struct direntry* file = filelist_file(sfile->files, last_file);
135                 params->active_file = last_file;
136
137                 if(file && S_ISDIR(file->type)) {
138                         /* the path is too long and we are not going up! */
139                         if (strcmp(file->relname, ".") &&
140                                 strcmp(file->relname, "..") &&
141                                 strlen(params->dir) + strlen(file->relname) >= FILE_MAX ) 
142                         {
143                                 // XXX error("Path too long, cannot enter this directory");
144                         } else {
145                                 if (strcmp(file->relname, "..")==0) {
146                                         /* avoids /../../ */
147                                         BLI_parent_dir(params->dir);
148                                 } else {
149                                         strcat(params->dir, file->relname);
150                                         strcat(params->dir,"/");
151                                         params->file[0] = '\0';
152                                         BLI_cleanup_dir(G.sce, params->dir);
153                                 }
154                                 filelist_setdir(sfile->files, params->dir);
155                                 filelist_free(sfile->files);
156                                 params->active_file = -1;
157                         }
158                 }
159                 else if (file)
160                 {
161                         if (file->relname) {
162                                 BLI_strncpy(params->file, file->relname, FILE_MAXFILE);
163                                 /* XXX
164                                 if(event==MIDDLEMOUSE && filelist_gettype(sfile->files)) 
165                                         imasel_execute(sfile);
166                                 */
167                         }
168                         
169                 }       
170                 /* XXX
171                 if(BIF_filelist_gettype(sfile->files)==FILE_MAIN) {
172                         active_imasel_object(sfile);
173                 }
174                 */
175         }
176 }
177
178
179
180 static int file_border_select_exec(bContext *C, wmOperator *op)
181 {
182         ARegion *ar= CTX_wm_region(C);
183         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
184         short val;
185         rcti rect;
186
187         val= RNA_int_get(op->ptr, "event_type");
188         rect.xmin= RNA_int_get(op->ptr, "xmin");
189         rect.ymin= RNA_int_get(op->ptr, "ymin");
190         rect.xmax= RNA_int_get(op->ptr, "xmax");
191         rect.ymax= RNA_int_get(op->ptr, "ymax");
192
193         BLI_isect_rctf(&(ar->v2d.mask), &rect, &rect);
194         
195         file_select(sfile, ar, &rect, val );
196         WM_event_add_notifier(C, NC_WINDOW, NULL);
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= (SpaceFile*)CTX_wm_space_data(C);
225         short val;
226         rcti rect;
227
228         rect.xmin = rect.xmax = event->x - ar->winrct.xmin;
229         rect.ymin = rect.ymax = event->y - ar->winrct.ymin;
230         val = event->val;
231
232         if (BLI_in_rcti(&ar->v2d.mask, rect.xmin, rect.ymin)) { 
233
234                 /* single select, deselect all selected first */
235                 file_deselect_all(sfile);
236                 file_select(sfile, ar, &rect, val );
237                 WM_event_add_notifier(C, NC_WINDOW, NULL);
238         }
239         return OPERATOR_FINISHED;
240 }
241
242 void FILE_OT_select(wmOperatorType *ot)
243 {
244         /* identifiers */
245         ot->name= "Activate/Select File";
246         ot->idname= "FILE_OT_select";
247         
248         /* api callbacks */
249         ot->invoke= file_select_invoke;
250
251         /* rna */
252
253         ot->poll= ED_operator_file_active;
254 }
255
256 static int file_select_all_invoke(bContext *C, wmOperator *op, wmEvent *event)
257 {
258         ScrArea *sa= CTX_wm_area(C);
259         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
260         int numfiles = filelist_numfiles(sfile->files);
261         int i;
262         int select = 1;
263
264         /* if any file is selected, deselect all first */
265         for ( i=0; i < numfiles; ++i) {
266                 struct direntry* file = filelist_file(sfile->files, i);
267                 if (file && (file->flags & ACTIVE)) {
268                         file->flags &= ~ACTIVE;
269                         select = 0;
270                         ED_area_tag_redraw(sa);
271                 }
272         }
273         /* select all only if previously no file was selected */
274         if (select) {
275                 for ( i=0; i < numfiles; ++i) {
276                         struct direntry* file = filelist_file(sfile->files, i);
277                         if(file && !S_ISDIR(file->type)) {
278                                 file->flags |= ACTIVE;
279                                 ED_area_tag_redraw(sa);
280                         }
281                 }
282         }
283         return OPERATOR_FINISHED;
284 }
285
286 void FILE_OT_select_all_toggle(wmOperatorType *ot)
287 {
288         /* identifiers */
289         ot->name= "Select/Deselect all files";
290         ot->idname= "FILE_OT_select_all_toggle";
291         
292         /* api callbacks */
293         ot->invoke= file_select_all_invoke;
294
295         /* rna */
296
297         ot->poll= ED_operator_file_active;
298 }
299
300 /* ---------- BOOKMARKS ----------- */
301
302 static int file_select_bookmark_category(SpaceFile* sfile, ARegion* ar, short x, short y, FSMenuCategory category)
303 {
304         struct FSMenu* fsmenu = fsmenu_get();
305         int nentries = fsmenu_get_nentries(fsmenu, category);
306         int linestep = file_font_pointsize()*2.0f;
307         short xs, ys;
308         int i;
309         int selected = -1;
310
311         for (i=0; i < nentries; ++i) {
312                 fsmenu_get_pos(fsmenu, category, i, &xs, &ys);
313                 if ( (y<=ys) && (y>ys-linestep) ) {
314                         fsmenu_select_entry(fsmenu, category, i);
315                         selected = i;
316                         break;
317                 }
318         }
319         return selected;
320 }
321
322 static void file_select_bookmark(SpaceFile* sfile, ARegion* ar, short x, short y)
323 {
324         float fx, fy;
325         int selected;
326         FSMenuCategory category = FS_CATEGORY_SYSTEM;
327
328         if (BLI_in_rcti(&ar->v2d.mask, x, y)) {
329                 char *entry;
330
331                 UI_view2d_region_to_view(&ar->v2d, x, y, &fx, &fy);
332                 selected = file_select_bookmark_category(sfile, ar, fx, fy, FS_CATEGORY_SYSTEM);
333                 if (selected<0) {
334                         category = FS_CATEGORY_BOOKMARKS;
335                         selected = file_select_bookmark_category(sfile, ar, fx, fy, category);
336                 }
337                 if (selected<0) {
338                         category = FS_CATEGORY_RECENT;
339                         selected = file_select_bookmark_category(sfile, ar, fx, fy, category);
340                 }
341                 
342                 if (selected>=0) {
343                         entry= fsmenu_get_entry(fsmenu_get(), category, selected);                      
344                         /* which string */
345                         if (entry) {
346                                 FileSelectParams* params = sfile->params;
347                                 BLI_strncpy(params->dir, entry, sizeof(params->dir));
348                                 BLI_cleanup_dir(G.sce, params->dir);
349                                 filelist_free(sfile->files);    
350                                 filelist_setdir(sfile->files, params->dir);
351                                 params->file[0] = '\0';                 
352                                 params->active_file = -1;
353                         }
354                 }
355         }
356 }
357
358 static int bookmark_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
359 {
360         ScrArea *sa= CTX_wm_area(C);
361         ARegion *ar= CTX_wm_region(C);  
362         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
363
364         short x, y;
365
366         x = event->x - ar->winrct.xmin;
367         y = event->y - ar->winrct.ymin;
368
369         file_select_bookmark(sfile, ar, x, y);
370         ED_area_tag_redraw(sa);
371         return OPERATOR_FINISHED;
372 }
373
374 void FILE_OT_select_bookmark(wmOperatorType *ot)
375 {
376         /* identifiers */
377         ot->name= "Select Directory";
378         ot->idname= "FILE_OT_select_bookmark";
379         
380         /* api callbacks */
381         ot->invoke= bookmark_select_invoke;
382         ot->poll= ED_operator_file_active;
383 }
384
385 static int loadimages_invoke(bContext *C, wmOperator *op, wmEvent *event)
386 {
387         ScrArea *sa= CTX_wm_area(C);
388         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
389         if (sfile->files) {
390                 filelist_loadimage_timer(sfile->files);
391                 if (filelist_changed(sfile->files)) {
392                         ED_area_tag_redraw(sa);
393                 }
394         }
395
396         return OPERATOR_FINISHED;
397 }
398
399 void FILE_OT_loadimages(wmOperatorType *ot)
400 {
401         
402         /* identifiers */
403         ot->name= "Load Images";
404         ot->idname= "FILE_OT_loadimages";
405         
406         /* api callbacks */
407         ot->invoke= loadimages_invoke;
408         
409         ot->poll= ED_operator_file_active;
410 }
411
412 int file_hilight_set(SpaceFile *sfile, ARegion *ar, int mx, int my)
413 {
414         FileSelectParams* params;
415         int numfiles, actfile;
416         
417         if(sfile==NULL || sfile->files==NULL) return 0;
418         
419         numfiles = filelist_numfiles(sfile->files);
420         params = ED_fileselect_get_params(sfile);
421
422         actfile = find_file_mouse(sfile, ar, mx , my, 0);
423         
424         if (params && (actfile >= 0) && (actfile < numfiles) ) {
425                 params->active_file=actfile;
426                 return 1;
427         } 
428         params->active_file= -1;
429         return 0;
430 }
431
432 static int file_highlight_invoke(bContext *C, wmOperator *op, wmEvent *event)
433 {
434         ARegion *ar= CTX_wm_region(C);
435         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
436         
437         if( file_hilight_set(sfile, ar, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin)) {
438                 ED_area_tag_redraw(CTX_wm_area(C));
439         }
440         
441         return OPERATOR_FINISHED;
442 }
443
444 void FILE_OT_highlight(struct wmOperatorType *ot)
445 {
446         /* identifiers */
447         ot->name= "Highlight File";
448         ot->idname= "FILE_OT_highlight";
449         
450         /* api callbacks */
451         ot->invoke= file_highlight_invoke;
452         ot->poll= ED_operator_file_active;
453 }
454
455 int file_cancel_exec(bContext *C, wmOperator *unused)
456 {
457         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
458         
459         WM_event_fileselect_event(C, sfile->op, EVT_FILESELECT_CANCEL);
460         sfile->op = NULL;
461         
462         return OPERATOR_FINISHED;
463 }
464
465 void FILE_OT_cancel(struct wmOperatorType *ot)
466 {
467         /* identifiers */
468         ot->name= "Cancel File Load";
469         ot->idname= "FILE_OT_cancel";
470         
471         /* api callbacks */
472         ot->exec= file_cancel_exec;
473         ot->poll= ED_operator_file_active;
474 }
475
476 /* sends events now, so things get handled on windowqueue level */
477 int file_exec(bContext *C, wmOperator *unused)
478 {
479         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
480         char name[FILE_MAX];
481         
482         if(sfile->op) {
483                 wmOperator *op= sfile->op;
484                 
485                 sfile->op = NULL;
486                 BLI_strncpy(name, sfile->params->dir, sizeof(name));
487                 strcat(name, sfile->params->file);
488                 RNA_string_set(op->ptr, "filename", name);
489                 
490                 /* some ops have multiple files to select */
491                 {
492                         PointerRNA itemptr;
493                         int i, numfiles = filelist_numfiles(sfile->files);
494                         struct direntry *file;
495                         if(RNA_struct_find_property(op->ptr, "files")) {
496                                 for (i=0; i<numfiles; i++) {
497                                         file = filelist_file(sfile->files, i);
498                                         if(file->flags & ACTIVE) {
499                                                 if ((file->type & S_IFDIR)==0) {
500                                                         RNA_collection_add(op->ptr, "files", &itemptr);
501                                                         RNA_string_set(&itemptr, "name", file->relname);
502                                                 }
503                                         }
504                                 }
505                         }
506                         
507                         if(RNA_struct_find_property(op->ptr, "dirs")) {
508                                 for (i=0; i<numfiles; i++) {
509                                         file = filelist_file(sfile->files, i);
510                                         if(file->flags & ACTIVE) {
511                                                 if ((file->type & S_IFDIR)) {
512                                                         RNA_collection_add(op->ptr, "dirs", &itemptr);
513                                                         RNA_string_set(&itemptr, "name", file->relname);
514                                                 }
515                                         }
516                                 }
517                         }
518                 }
519                 
520                 fsmenu_insert_entry(fsmenu_get(), FS_CATEGORY_RECENT, sfile->params->dir,0, 1);
521                 BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
522                 fsmenu_write_file(fsmenu_get(), name);
523                 WM_event_fileselect_event(C, op, EVT_FILESELECT_EXEC);
524         }
525                                 
526         return OPERATOR_FINISHED;
527 }
528
529 void FILE_OT_exec(struct wmOperatorType *ot)
530 {
531         /* identifiers */
532         ot->name= "Execute File Window";
533         ot->idname= "FILE_OT_exec";
534         
535         /* api callbacks */
536         ot->exec= file_exec;
537         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
538 }
539
540
541 int file_parent_exec(bContext *C, wmOperator *unused)
542 {
543         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
544         
545         if(sfile->params) {
546                 BLI_parent_dir(sfile->params->dir);
547                 filelist_setdir(sfile->files, sfile->params->dir);
548                 filelist_free(sfile->files);
549                 sfile->params->active_file = -1;
550         }               
551         ED_area_tag_redraw(CTX_wm_area(C));
552
553         return OPERATOR_FINISHED;
554
555 }
556
557
558 void FILE_OT_parent(struct wmOperatorType *ot)
559 {
560         /* identifiers */
561         ot->name= "Parent File";
562         ot->idname= "FILE_OT_parent";
563         
564         /* api callbacks */
565         ot->exec= file_parent_exec;
566         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
567 }
568
569
570 int file_refresh_exec(bContext *C, wmOperator *unused)
571 {
572         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
573         
574         if(sfile->params) {
575                 filelist_setdir(sfile->files, sfile->params->dir);
576                 filelist_free(sfile->files);
577                 sfile->params->active_file = -1;
578         }               
579         ED_area_tag_redraw(CTX_wm_area(C));
580
581         return OPERATOR_FINISHED;
582
583 }
584
585
586 void FILE_OT_refresh(struct wmOperatorType *ot)
587 {
588         /* identifiers */
589         ot->name= "Refresh Filelist";
590         ot->idname= "FILE_OT_refresh";
591         
592         /* api callbacks */
593         ot->exec= file_refresh_exec;
594         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
595 }
596
597 struct ARegion *file_buttons_region(struct ScrArea *sa)
598 {
599         ARegion *ar;
600         
601         for(ar= sa->regionbase.first; ar; ar= ar->next)
602                 if(ar->regiontype==RGN_TYPE_CHANNELS)
603                         return ar;
604         return NULL;
605 }
606
607 int file_bookmark_toggle_exec(bContext *C, wmOperator *unused)
608 {
609         ScrArea *sa= CTX_wm_area(C);
610         ARegion *ar= file_buttons_region(sa);
611         
612         if(ar) {
613                 ar->flag ^= RGN_FLAG_HIDDEN;
614                 ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
615                 
616                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
617                 ED_area_tag_redraw(sa);
618         }
619         return OPERATOR_FINISHED;
620 }
621
622 void FILE_OT_bookmark_toggle(struct wmOperatorType *ot)
623 {
624         /* identifiers */
625         ot->name= "Toggle Bookmarks";
626         ot->idname= "FILE_OT_bookmark_toggle";
627         
628         /* api callbacks */
629         ot->exec= file_bookmark_toggle_exec;
630         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
631 }