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