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