2.5 filebrowser WIP commit
[blender-staging.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(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         int nentries = fsmenu_get_nentries(category);
339         int linestep = U.fontsize*2.0f;
340         short xs, ys;
341         int i;
342         int selected = -1;
343
344         for (i=0; i < nentries; ++i) {
345                 fsmenu_get_pos(category, i, &xs, &ys);
346                 if ( (y<=ys) && (y>ys-linestep) ) {
347                         fsmenu_select_entry(category, i);
348                         selected = i;
349                         break;
350                 }
351         }
352         return selected;
353 }
354
355 static void file_select_bookmark(SpaceFile* sfile, ARegion* ar, short x, short y)
356 {
357         float fx, fy;
358         int selected;
359         FSMenuCategory category = FS_CATEGORY_SYSTEM;
360
361         if (BLI_in_rcti(&ar->v2d.mask, x, y)) {
362                 char *entry;
363
364                 UI_view2d_region_to_view(&ar->v2d, x, y, &fx, &fy);
365                 selected = file_select_bookmark_category(sfile, ar, fx, fy, FS_CATEGORY_SYSTEM);
366                 if (selected<0) {
367                         category = FS_CATEGORY_BOOKMARKS;
368                         selected = file_select_bookmark_category(sfile, ar, fx, fy, category);
369                 }
370                 if (selected<0) {
371                         category = FS_CATEGORY_RECENT;
372                         selected = file_select_bookmark_category(sfile, ar, fx, fy, category);
373                 }
374                 
375                 if (selected>=0) {
376                         entry= fsmenu_get_entry(category, selected);                    
377                         /* which string */
378                         if (entry) {
379                                 FileSelectParams* params = sfile->params;
380                                 BLI_strncpy(params->dir, entry, sizeof(params->dir));
381                                 BLI_cleanup_dir(G.sce, params->dir);
382                                 filelist_free(sfile->files);    
383                                 filelist_setdir(sfile->files, params->dir);
384                                 params->file[0] = '\0';                 
385                                 params->active_file = -1;
386                         }
387                 }
388         }
389 }
390
391 static int bookmark_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
392 {
393         ScrArea *sa= CTX_wm_area(C);
394         ARegion *ar= CTX_wm_region(C);  
395         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
396
397         short x, y;
398
399         x = event->x - ar->winrct.xmin;
400         y = event->y - ar->winrct.ymin;
401
402         file_select_bookmark(sfile, ar, x, y);
403         ED_area_tag_redraw(sa);
404         return OPERATOR_FINISHED;
405 }
406
407 void FILE_OT_select_bookmark(wmOperatorType *ot)
408 {
409         /* identifiers */
410         ot->name= "Select Directory";
411         ot->idname= "FILE_OT_select_bookmark";
412         
413         /* api callbacks */
414         ot->invoke= bookmark_select_invoke;
415         ot->poll= ED_operator_file_active;
416 }
417
418 static int loadimages_invoke(bContext *C, wmOperator *op, wmEvent *event)
419 {
420         ScrArea *sa= CTX_wm_area(C);
421         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
422         if (sfile->files) {
423                 filelist_loadimage_timer(sfile->files);
424                 if (filelist_changed(sfile->files)) {
425                         ED_area_tag_redraw(sa);
426                 }
427         }
428
429         return OPERATOR_FINISHED;
430 }
431
432 void FILE_OT_loadimages(wmOperatorType *ot)
433 {
434         
435         /* identifiers */
436         ot->name= "Load Images";
437         ot->idname= "FILE_OT_loadimages";
438         
439         /* api callbacks */
440         ot->invoke= loadimages_invoke;
441         
442         ot->poll= ED_operator_file_active;
443 }
444
445 int file_hilight_set(SpaceFile *sfile, ARegion *ar, int mx, int my)
446 {
447         FileSelectParams* params;
448         int numfiles, actfile;
449         
450         if(sfile==NULL || sfile->files==NULL) return 0;
451         
452         numfiles = filelist_numfiles(sfile->files);
453         params = ED_fileselect_get_params(sfile);
454         
455         if ( (params->display == FILE_IMGDISPLAY) || (params->display == FILE_LONGDISPLAY)) {
456                 actfile = find_file_mouse_hor(sfile, ar, mx , my);
457         } else {
458                 actfile = find_file_mouse_vert(sfile, ar, mx, my);
459         }
460         
461         if (actfile >= 0 && actfile < numfiles ) {
462                 params->active_file=actfile;
463                 return 1;
464         }
465         return 0;
466 }
467
468 static int file_highlight_invoke(bContext *C, wmOperator *op, wmEvent *event)
469 {
470         ARegion *ar= CTX_wm_region(C);
471         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
472         
473         if( file_hilight_set(sfile, ar, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin)) {
474                 ED_area_tag_redraw(CTX_wm_area(C));
475         }
476         
477         return OPERATOR_FINISHED;
478 }
479
480 void FILE_OT_highlight(struct wmOperatorType *ot)
481 {
482         /* identifiers */
483         ot->name= "Highlight File";
484         ot->idname= "FILE_OT_highlight";
485         
486         /* api callbacks */
487         ot->invoke= file_highlight_invoke;
488         ot->poll= ED_operator_file_active;
489 }
490
491 int file_cancel_exec(bContext *C, wmOperator *unused)
492 {
493         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
494         
495         WM_event_fileselect_event(C, sfile->op, EVT_FILESELECT_CANCEL);
496         sfile->op = NULL;
497         
498         return OPERATOR_FINISHED;
499 }
500
501 void FILE_OT_cancel(struct wmOperatorType *ot)
502 {
503         /* identifiers */
504         ot->name= "Cancel File Load";
505         ot->idname= "FILE_OT_cancel";
506         
507         /* api callbacks */
508         ot->exec= file_cancel_exec;
509         ot->poll= ED_operator_file_active;
510 }
511
512 /* sends events now, so things get handled on windowqueue level */
513 int file_exec(bContext *C, wmOperator *unused)
514 {
515         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
516         char name[FILE_MAX];
517         
518         if(sfile->op) {
519                 wmOperator *op= sfile->op;
520                 
521                 sfile->op = NULL;
522                 BLI_strncpy(name, sfile->params->dir, sizeof(name));
523                 strcat(name, sfile->params->file);
524                 RNA_string_set(op->ptr, "filename", name);
525                 
526                 WM_event_fileselect_event(C, op, EVT_FILESELECT_EXEC);
527         }
528                                 
529         return OPERATOR_FINISHED;
530 }
531
532 void FILE_OT_exec(struct wmOperatorType *ot)
533 {
534         /* identifiers */
535         ot->name= "Execute File Window";
536         ot->idname= "FILE_OT_exec";
537         
538         /* api callbacks */
539         ot->exec= file_exec;
540         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
541 }
542
543
544 int file_parent_exec(bContext *C, wmOperator *unused)
545 {
546         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
547         
548         if(sfile->params) {
549                 BLI_parent_dir(sfile->params->dir);
550                 filelist_setdir(sfile->files, sfile->params->dir);
551                 filelist_free(sfile->files);
552                 sfile->params->active_file = -1;
553         }               
554         ED_area_tag_redraw(CTX_wm_area(C));
555
556         return OPERATOR_FINISHED;
557
558 }
559
560
561 void FILE_OT_parent(struct wmOperatorType *ot)
562 {
563         /* identifiers */
564         ot->name= "Parent File";
565         ot->idname= "FILE_OT_parent";
566         
567         /* api callbacks */
568         ot->exec= file_parent_exec;
569         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
570 }
571
572 struct ARegion *file_buttons_region(struct ScrArea *sa)
573 {
574         ARegion *ar;
575         
576         for(ar= sa->regionbase.first; ar; ar= ar->next)
577                 if(ar->regiontype==RGN_TYPE_CHANNELS)
578                         return ar;
579         return NULL;
580 }
581
582 int file_bookmark_toggle_exec(bContext *C, wmOperator *unused)
583 {
584         ScrArea *sa= CTX_wm_area(C);
585         ARegion *ar= file_buttons_region(sa);
586         
587         if(ar) {
588                 ar->flag ^= RGN_FLAG_HIDDEN;
589                 ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
590                 
591                 ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
592                 ED_area_tag_redraw(sa);
593         }
594         return OPERATOR_FINISHED;
595 }
596
597 void FILE_OT_bookmark_toggle(struct wmOperatorType *ot)
598 {
599         /* identifiers */
600         ot->name= "Toggle Bookmarks";
601         ot->idname= "FILE_OT_bookmark_toggle";
602         
603         /* api callbacks */
604         ot->exec= file_bookmark_toggle_exec;
605         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
606 }