2.5 filebrowser
[blender.git] / source / blender / editors / space_file / file_ops.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Andrea Weikert (c) 2008 Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include "BKE_context.h"
30 #include "BKE_screen.h"
31 #include "BKE_global.h"
32
33 #include "BLI_blenlib.h"
34 #include "BLI_storage_types.h"
35 #ifdef WIN32
36 #include "BLI_winstuff.h"
37 #endif
38 #include "DNA_space_types.h"
39 #include "DNA_userdef_types.h"
40
41 #include "ED_space_api.h"
42 #include "ED_screen.h"
43 #include "ED_fileselect.h"
44
45 #include "MEM_guardedalloc.h"
46
47 #include "RNA_access.h"
48 #include "RNA_define.h"
49
50 #include "UI_interface.h"
51 #include "UI_view2d.h"
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "file_intern.h"
57 #include "filelist.h"
58 #include "fsmenu.h"
59
60 #include <stdlib.h>
61 #include <string.h>
62 #include <stdio.h>
63
64 /* for events */
65 #define NOTACTIVE                       0
66 #define ACTIVATE                        1
67 #define INACTIVATE                      2
68
69 /* ---------- FILE SELECTION ------------ */
70
71 static int find_file_mouse(SpaceFile *sfile, struct ARegion* ar, short x, short y, short clamp)
72 {
73         float fx,fy;
74         int active_file = -1;
75         int numfiles = filelist_numfiles(sfile->files);
76         View2D* v2d = &ar->v2d;
77
78         UI_view2d_region_to_view(v2d, x, y, &fx, &fy);
79
80         active_file = ED_fileselect_layout_offset(sfile->layout, v2d->tot.xmin + fx, v2d->tot.ymax - fy);
81
82         if(active_file < 0) {
83                 if(clamp)       active_file=  0;
84                 else            active_file= -1;
85         }
86         else if(active_file >= numfiles) {
87                 if(clamp)       active_file=  numfiles-1;
88                 else            active_file= -1;
89         }
90         
91         return active_file;
92 }
93
94
95 static void file_deselect_all(SpaceFile* sfile)
96 {
97         int numfiles = filelist_numfiles(sfile->files);
98         int i;
99
100         for ( i=0; i < numfiles; ++i) {
101                 struct direntry* file = filelist_file(sfile->files, i);
102                 if (file && (file->flags & ACTIVE)) {
103                         file->flags &= ~ACTIVE;
104                 }
105         }
106 }
107
108 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 and we are not going up! */
141                         if (strcmp(file->relname, ".") &&
142                                 strcmp(file->relname, "..") &&
143                                 strlen(params->dir) + strlen(file->relname) >= FILE_MAX ) 
144                         {
145                                 // XXX error("Path too long, cannot enter this directory");
146                         } else {
147                                 if (strcmp(file->relname, "..")==0) {
148                                         /* avoids /../../ */
149                                         BLI_parent_dir(params->dir);
150                                 } else {
151                                         strcat(params->dir, file->relname);
152                                         strcat(params->dir,"/");
153                                         params->file[0] = '\0';
154                                         BLI_cleanup_dir(G.sce, params->dir);
155                                 }
156                                 filelist_setdir(sfile->files, params->dir);
157                                 filelist_free(sfile->files);
158                                 params->active_file = -1;
159                         }
160                 }
161                 else if (file)
162                 {
163                         if (file->relname) {
164                                 BLI_strncpy(params->file, file->relname, FILE_MAXFILE);
165                                 /* XXX
166                                 if(event==MIDDLEMOUSE && filelist_gettype(sfile->files)) 
167                                         imasel_execute(sfile);
168                                 */
169                         }
170                         
171                 }       
172                 /* XXX
173                 if(BIF_filelist_gettype(sfile->files)==FILE_MAIN) {
174                         active_imasel_object(sfile);
175                 }
176                 */
177         }
178 }
179
180
181
182 static int file_border_select_exec(bContext *C, wmOperator *op)
183 {
184         ARegion *ar= CTX_wm_region(C);
185         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
186         short val;
187         rcti rect;
188
189         val= RNA_int_get(op->ptr, "event_type");
190         rect.xmin= RNA_int_get(op->ptr, "xmin");
191         rect.ymin= RNA_int_get(op->ptr, "ymin");
192         rect.xmax= RNA_int_get(op->ptr, "xmax");
193         rect.ymax= RNA_int_get(op->ptr, "ymax");
194
195         BLI_isect_rcti(&(ar->v2d.mask), &rect, &rect);
196         
197         file_select(sfile, ar, &rect, val );
198         WM_event_add_notifier(C, NC_WINDOW, NULL);
199         return OPERATOR_FINISHED;
200 }
201
202 void FILE_OT_select_border(wmOperatorType *ot)
203 {
204         /* identifiers */
205         ot->name= "Activate/Select File";
206         ot->idname= "FILE_OT_select_border";
207         
208         /* api callbacks */
209         ot->invoke= WM_border_select_invoke;
210         ot->exec= file_border_select_exec;
211         ot->modal= WM_border_select_modal;
212
213         /* rna */
214         RNA_def_int(ot->srna, "event_type", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
215         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
216         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
217         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
218         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
219
220         ot->poll= ED_operator_file_active;
221 }
222
223 static int file_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
224 {
225         ARegion *ar= CTX_wm_region(C);
226         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
227         short val;
228         rcti rect;
229
230         rect.xmin = rect.xmax = event->x - ar->winrct.xmin;
231         rect.ymin = rect.ymax = event->y - ar->winrct.ymin;
232         val = event->val;
233
234         if (BLI_in_rcti(&ar->v2d.mask, rect.xmin, rect.ymin)) { 
235
236                 /* single select, deselect all selected first */
237                 file_deselect_all(sfile);
238                 file_select(sfile, ar, &rect, val );
239                 WM_event_add_notifier(C, NC_FILE|ND_PARAMS, NULL);
240         }
241         return OPERATOR_FINISHED;
242 }
243
244 void FILE_OT_select(wmOperatorType *ot)
245 {
246         /* identifiers */
247         ot->name= "Activate/Select File";
248         ot->idname= "FILE_OT_select";
249         
250         /* api callbacks */
251         ot->invoke= file_select_invoke;
252
253         /* rna */
254
255         ot->poll= ED_operator_file_active;
256 }
257
258 static int file_select_all_invoke(bContext *C, wmOperator *op, wmEvent *event)
259 {
260         ScrArea *sa= CTX_wm_area(C);
261         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
262         int numfiles = filelist_numfiles(sfile->files);
263         int i;
264         int select = 1;
265
266         /* if any file is selected, deselect all first */
267         for ( i=0; i < numfiles; ++i) {
268                 struct direntry* file = filelist_file(sfile->files, i);
269                 if (file && (file->flags & ACTIVE)) {
270                         file->flags &= ~ACTIVE;
271                         select = 0;
272                         ED_area_tag_redraw(sa);
273                 }
274         }
275         /* select all only if previously no file was selected */
276         if (select) {
277                 for ( i=0; i < numfiles; ++i) {
278                         struct direntry* file = filelist_file(sfile->files, i);
279                         if(file && !S_ISDIR(file->type)) {
280                                 file->flags |= ACTIVE;
281                                 ED_area_tag_redraw(sa);
282                         }
283                 }
284         }
285         return OPERATOR_FINISHED;
286 }
287
288 void FILE_OT_select_all_toggle(wmOperatorType *ot)
289 {
290         /* identifiers */
291         ot->name= "Select/Deselect all files";
292         ot->idname= "FILE_OT_select_all_toggle";
293         
294         /* api callbacks */
295         ot->invoke= file_select_all_invoke;
296
297         /* rna */
298
299         ot->poll= ED_operator_file_active;
300 }
301
302 /* ---------- BOOKMARKS ----------- */
303
304 static int bookmark_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
305 {
306         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
307
308         if(RNA_struct_find_property(op->ptr, "dir")) {
309                 char entry[256];
310                 FileSelectParams* params = sfile->params;
311
312                 RNA_string_get(op->ptr, "dir", entry);
313                 BLI_strncpy(params->dir, entry, sizeof(params->dir));
314                 BLI_cleanup_dir(G.sce, params->dir);
315                 filelist_free(sfile->files);    
316                 filelist_setdir(sfile->files, params->dir);
317                 params->file[0] = '\0';                 
318                 params->active_file = -1;
319
320                 WM_event_add_notifier(C, NC_FILE|ND_PARAMS, NULL);
321         }
322         
323         return OPERATOR_FINISHED;
324 }
325
326 void FILE_OT_select_bookmark(wmOperatorType *ot)
327 {
328         /* identifiers */
329         ot->name= "Select Directory";
330         ot->idname= "FILE_OT_select_bookmark";
331         
332         /* api callbacks */
333         ot->invoke= bookmark_select_invoke;
334         ot->poll= ED_operator_file_active;
335
336         RNA_def_string(ot->srna, "dir", "", 256, "Dir", "");
337 }
338
339 static int loadimages_invoke(bContext *C, wmOperator *op, wmEvent *event)
340 {
341         ScrArea *sa= CTX_wm_area(C);
342         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
343         if (sfile->files) {
344                 filelist_loadimage_timer(sfile->files);
345                 if (filelist_changed(sfile->files)) {
346                         ED_area_tag_redraw(sa);
347                 }
348         }
349
350         return OPERATOR_FINISHED;
351 }
352
353 void FILE_OT_loadimages(wmOperatorType *ot)
354 {
355         
356         /* identifiers */
357         ot->name= "Load Images";
358         ot->idname= "FILE_OT_loadimages";
359         
360         /* api callbacks */
361         ot->invoke= loadimages_invoke;
362         
363         ot->poll= ED_operator_file_active;
364 }
365
366 int file_hilight_set(SpaceFile *sfile, ARegion *ar, int mx, int my)
367 {
368         FileSelectParams* params;
369         int numfiles, actfile;
370         
371         if(sfile==NULL || sfile->files==NULL) return 0;
372         
373         numfiles = filelist_numfiles(sfile->files);
374         params = ED_fileselect_get_params(sfile);
375
376         actfile = find_file_mouse(sfile, ar, mx , my, 0);
377         
378         if (params && (actfile >= 0) && (actfile < numfiles) ) {
379                 params->active_file=actfile;
380                 return 1;
381         } 
382         params->active_file= -1;
383         return 0;
384 }
385
386 static int file_highlight_invoke(bContext *C, wmOperator *op, wmEvent *event)
387 {
388         ARegion *ar= CTX_wm_region(C);
389         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
390         
391         if( file_hilight_set(sfile, ar, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin)) {
392                 ED_area_tag_redraw(CTX_wm_area(C));
393         }
394         
395         return OPERATOR_FINISHED;
396 }
397
398 void FILE_OT_highlight(struct wmOperatorType *ot)
399 {
400         /* identifiers */
401         ot->name= "Highlight File";
402         ot->idname= "FILE_OT_highlight";
403         
404         /* api callbacks */
405         ot->invoke= file_highlight_invoke;
406         ot->poll= ED_operator_file_active;
407 }
408
409 int file_cancel_exec(bContext *C, wmOperator *unused)
410 {
411         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
412         
413         WM_event_fileselect_event(C, sfile->op, EVT_FILESELECT_CANCEL);
414         sfile->op = NULL;
415         
416         return OPERATOR_FINISHED;
417 }
418
419 void FILE_OT_cancel(struct wmOperatorType *ot)
420 {
421         /* identifiers */
422         ot->name= "Cancel File Load";
423         ot->idname= "FILE_OT_cancel";
424         
425         /* api callbacks */
426         ot->exec= file_cancel_exec;
427         ot->poll= ED_operator_file_active;
428 }
429
430 /* sends events now, so things get handled on windowqueue level */
431 int file_exec(bContext *C, wmOperator *unused)
432 {
433         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
434         char name[FILE_MAX];
435         
436         if(sfile->op) {
437                 wmOperator *op= sfile->op;
438                 
439                 sfile->op = NULL;
440                 BLI_strncpy(name, sfile->params->dir, sizeof(name));
441                 strcat(name, sfile->params->file);
442                 RNA_string_set(op->ptr, "filename", name);
443                 
444                 /* some ops have multiple files to select */
445                 {
446                         PointerRNA itemptr;
447                         int i, numfiles = filelist_numfiles(sfile->files);
448                         struct direntry *file;
449                         if(RNA_struct_find_property(op->ptr, "files")) {
450                                 for (i=0; i<numfiles; i++) {
451                                         file = filelist_file(sfile->files, i);
452                                         if(file->flags & ACTIVE) {
453                                                 if ((file->type & S_IFDIR)==0) {
454                                                         RNA_collection_add(op->ptr, "files", &itemptr);
455                                                         RNA_string_set(&itemptr, "name", file->relname);
456                                                 }
457                                         }
458                                 }
459                         }
460                         
461                         if(RNA_struct_find_property(op->ptr, "dirs")) {
462                                 for (i=0; i<numfiles; i++) {
463                                         file = filelist_file(sfile->files, i);
464                                         if(file->flags & ACTIVE) {
465                                                 if ((file->type & S_IFDIR)) {
466                                                         RNA_collection_add(op->ptr, "dirs", &itemptr);
467                                                         RNA_string_set(&itemptr, "name", file->relname);
468                                                 }
469                                         }
470                                 }
471                         }
472                 }
473                 
474                 fsmenu_insert_entry(fsmenu_get(), FS_CATEGORY_RECENT, sfile->params->dir,0, 1);
475                 BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
476                 fsmenu_write_file(fsmenu_get(), name);
477                 WM_event_fileselect_event(C, op, EVT_FILESELECT_EXEC);
478         }
479                                 
480         return OPERATOR_FINISHED;
481 }
482
483 void FILE_OT_exec(struct wmOperatorType *ot)
484 {
485         /* identifiers */
486         ot->name= "Execute File Window";
487         ot->idname= "FILE_OT_exec";
488         
489         /* api callbacks */
490         ot->exec= file_exec;
491         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
492 }
493
494
495 int file_parent_exec(bContext *C, wmOperator *unused)
496 {
497         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
498         
499         if(sfile->params) {
500                 BLI_parent_dir(sfile->params->dir);
501                 filelist_setdir(sfile->files, sfile->params->dir);
502                 filelist_free(sfile->files);
503                 sfile->params->active_file = -1;
504         }               
505         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
506
507         return OPERATOR_FINISHED;
508
509 }
510
511
512 void FILE_OT_parent(struct wmOperatorType *ot)
513 {
514         /* identifiers */
515         ot->name= "Parent File";
516         ot->idname= "FILE_OT_parent";
517         
518         /* api callbacks */
519         ot->exec= file_parent_exec;
520         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
521 }
522
523
524 int file_refresh_exec(bContext *C, wmOperator *unused)
525 {
526         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
527         
528         if(sfile->params) {
529                 filelist_setdir(sfile->files, sfile->params->dir);
530                 filelist_free(sfile->files);
531                 sfile->params->active_file = -1;
532         }
533         WM_event_add_notifier(C, NC_FILE|ND_FILELIST, NULL);
534
535         return OPERATOR_FINISHED;
536
537 }
538
539
540 void FILE_OT_refresh(struct wmOperatorType *ot)
541 {
542         /* identifiers */
543         ot->name= "Refresh Filelist";
544         ot->idname= "FILE_OT_refresh";
545         
546         /* api callbacks */
547         ot->exec= file_refresh_exec;
548         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
549 }
550
551 struct ARegion *file_buttons_region(struct ScrArea *sa)
552 {
553         ARegion *ar, *arnew;
554         
555         for(ar= sa->regionbase.first; ar; ar= ar->next)
556                 if(ar->regiontype==RGN_TYPE_CHANNELS)
557                         return ar;
558
559         /* add subdiv level; after header */
560         for(ar= sa->regionbase.first; ar; ar= ar->next)
561                 if(ar->regiontype==RGN_TYPE_HEADER)
562                         break;
563         
564         /* is error! */
565         if(ar==NULL) return NULL;
566         
567         arnew= MEM_callocN(sizeof(ARegion), "buttons for file panels");
568         
569         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
570         arnew->regiontype= RGN_TYPE_CHANNELS;
571         arnew->alignment= RGN_ALIGN_LEFT;
572         
573         arnew->flag = RGN_FLAG_HIDDEN;
574         
575         return arnew;
576 }
577
578 int file_bookmark_toggle_exec(bContext *C, wmOperator *unused)
579 {
580         ScrArea *sa= CTX_wm_area(C);
581         ARegion *ar= file_buttons_region(sa);
582         
583         if(ar) {
584                 ar->flag ^= RGN_FLAG_HIDDEN;
585                 ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
586                 
587                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
588                 ED_area_tag_redraw(sa);
589         }
590         return OPERATOR_FINISHED;
591 }
592
593 void FILE_OT_bookmark_toggle(struct wmOperatorType *ot)
594 {
595         /* identifiers */
596         ot->name= "Toggle Bookmarks";
597         ot->idname= "FILE_OT_bookmark_toggle";
598         
599         /* api callbacks */
600         ot->exec= file_bookmark_toggle_exec;
601         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
602 }