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