2.5
[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_view2d.h"
49
50 #include "WM_api.h"
51 #include "WM_types.h"
52
53 #include "file_intern.h"
54 #include "filelist.h"
55 #include "fsmenu.h"
56
57 #include <stdlib.h>
58 #include <string.h>
59 #include <stdio.h>
60
61 /* for events */
62 #define NOTACTIVE                       0
63 #define ACTIVATE                        1
64 #define INACTIVATE                      2
65
66 /* ---------- FILE SELECTION ------------ */
67
68 static int find_file_mouse_hor(SpaceFile *sfile, struct ARegion* ar, short x, short y)
69 {
70         float fx,fy;
71         int offsetx, offsety;
72         int columns;
73         int active_file = -1;
74         int numfiles = filelist_numfiles(sfile->files);
75         View2D* v2d = &ar->v2d;
76
77         UI_view2d_region_to_view(v2d, x, y, &fx, &fy);
78
79         offsetx = (fx - (v2d->cur.xmin+sfile->tile_border_x))/(sfile->tile_w + 2*sfile->tile_border_x);
80         offsety = (v2d->tot.ymax - sfile->tile_border_y - fy)/(sfile->tile_h + 2*sfile->tile_border_y);
81         columns = (v2d->cur.xmax - v2d->cur.xmin) / (sfile->tile_w+ 2*sfile->tile_border_x);
82         active_file = offsetx + columns*offsety;
83
84         if ( (active_file < 0) || (active_file >= numfiles) )
85         {
86                 active_file = -1;
87         }
88         return active_file;
89 }
90
91
92 static int find_file_mouse_vert(SpaceFile *sfile, struct ARegion* ar, short x, short y)
93 {
94         int offsetx, offsety;
95         float fx,fy;
96         int active_file = -1;
97         int numfiles = filelist_numfiles(sfile->files);
98         int rows;
99         View2D* v2d = &ar->v2d;
100
101         UI_view2d_region_to_view(v2d, x, y, &fx, &fy);
102         
103         offsetx = (fx-sfile->tile_border_x)/(sfile->tile_w + sfile->tile_border_x);
104         offsety = (v2d->cur.ymax-fy-sfile->tile_border_y)/(sfile->tile_h + sfile->tile_border_y);
105         rows = (v2d->cur.ymax - v2d->cur.ymin - 2*sfile->tile_border_y) / (sfile->tile_h+sfile->tile_border_y);
106         active_file = rows*offsetx + offsety;
107         if ( (active_file < 0) || (active_file >= numfiles) )
108         {
109                 active_file = -1;
110         }
111         return active_file;
112 }
113
114 static void file_deselect_all(SpaceFile* sfile)
115 {
116         int numfiles = filelist_numfiles(sfile->files);
117         int i;
118
119         for ( i=0; i < numfiles; ++i) {
120                 struct direntry* file = filelist_file(sfile->files, i);
121                 if (file && (file->flags & ACTIVE)) {
122                         file->flags &= ~ACTIVE;
123                 }
124         }
125 }
126
127 static void file_select(SpaceFile* sfile, FileSelectParams* params, ARegion* ar, const rcti* rect, short val)
128 {
129         int first_file = -1;
130         int last_file = -1;
131         int act_file;
132         short selecting = (val == LEFTMOUSE);
133
134         int numfiles = filelist_numfiles(sfile->files);
135
136         params->selstate = NOTACTIVE;
137         if (params->display) {
138                 first_file = find_file_mouse_hor(sfile, ar, rect->xmin, rect->ymax);
139                 last_file = find_file_mouse_hor(sfile, ar, rect->xmax, rect->ymin);
140         } else {
141                 first_file = find_file_mouse_vert(sfile, ar, rect->xmin, rect->ymax);
142                 last_file = find_file_mouse_vert(sfile, ar, rect->xmax, rect->ymin);
143         }
144         
145         /* select all valid files between first and last indicated */
146         if ( (first_file >= 0) && (first_file < numfiles) && (last_file >= 0) && (last_file < numfiles) ) {
147                 for (act_file = first_file; act_file <= last_file; act_file++) {
148                         struct direntry* file = filelist_file(sfile->files, act_file);
149                         if (selecting) 
150                                 file->flags |= ACTIVE;
151                         else
152                                 file->flags &= ~ACTIVE;
153                 }
154         }
155         
156         /* make the last file active */
157         if (last_file >= 0 && last_file < numfiles) {
158                 struct direntry* file = filelist_file(sfile->files, last_file);
159                 params->active_file = last_file;
160
161                 if(file && S_ISDIR(file->type)) {
162                         /* the path is too long and we are not going up! */
163                         if (strcmp(file->relname, ".") &&
164                                 strcmp(file->relname, "..") &&
165                                 strlen(params->dir) + strlen(file->relname) >= FILE_MAX ) 
166                         {
167                                 // XXX error("Path too long, cannot enter this directory");
168                         } else {
169                                 if (strcmp(file->relname, "..")==0) {
170                                         /* avoids /../../ */
171                                         BLI_parent_dir(params->dir);
172                                 } else {
173                                         strcat(params->dir, file->relname);
174                                         strcat(params->dir,"/");
175                                         params->file[0] = '\0';
176                                         BLI_cleanup_dir(G.sce, params->dir);
177                                 }
178                                 filelist_setdir(sfile->files, params->dir);
179                                 filelist_free(sfile->files);
180                                 params->active_file = -1;
181                         }
182                 }
183                 else if (file)
184                 {
185                         if (file->relname) {
186                                 BLI_strncpy(params->file, file->relname, FILE_MAXFILE);
187                                 /* XXX
188                                 if(event==MIDDLEMOUSE && filelist_gettype(sfile->files)) 
189                                         imasel_execute(sfile);
190                                 */
191                         }
192                         
193                 }       
194                 /* XXX
195                 if(BIF_filelist_gettype(sfile->files)==FILE_MAIN) {
196                         active_imasel_object(sfile);
197                 }
198                 */
199         }
200 }
201
202
203
204 static int file_border_select_exec(bContext *C, wmOperator *op)
205 {
206         ARegion *ar= CTX_wm_region(C);
207         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
208         short val;
209         rcti rect;
210
211         val= RNA_int_get(op->ptr, "event_type");
212         rect.xmin= RNA_int_get(op->ptr, "xmin");
213         rect.ymin= RNA_int_get(op->ptr, "ymin");
214         rect.xmax= RNA_int_get(op->ptr, "xmax");
215         rect.ymax= RNA_int_get(op->ptr, "ymax");
216
217         file_select(sfile, sfile->params, ar, &rect, val );
218         WM_event_add_notifier(C, NC_WINDOW, NULL);
219         return OPERATOR_FINISHED;
220 }
221
222 void ED_FILE_OT_border_select(wmOperatorType *ot)
223 {
224         /* identifiers */
225         ot->name= "Activate/Select File";
226         ot->idname= "ED_FILE_OT_border_select";
227         
228         /* api callbacks */
229         ot->invoke= WM_border_select_invoke;
230         ot->exec= file_border_select_exec;
231         ot->modal= WM_border_select_modal;
232
233         /* rna */
234         RNA_def_int(ot->srna, "event_type", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
235         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
236         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
237         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
238         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
239
240         ot->poll= ED_operator_file_active;
241 }
242
243 static int file_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
244 {
245         ARegion *ar= CTX_wm_region(C);
246         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
247         short val;
248         rcti rect;
249
250         rect.xmin = rect.xmax = event->x - ar->winrct.xmin;
251         rect.ymin = rect.ymax = event->y - ar->winrct.ymin;
252         val = event->val;
253
254         /* single select, deselect all selected first */
255         file_deselect_all(sfile);
256         file_select(sfile, sfile->params, ar, &rect, val );
257         WM_event_add_notifier(C, NC_WINDOW, NULL);
258         return OPERATOR_FINISHED;
259 }
260
261 void ED_FILE_OT_select(wmOperatorType *ot)
262 {
263         /* identifiers */
264         ot->name= "Activate/Select File";
265         ot->idname= "ED_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 ED_FILE_OT_select_all(wmOperatorType *ot)
306 {
307         /* identifiers */
308         ot->name= "Select/Deselect all files";
309         ot->idname= "ED_FILE_OT_select_all";
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();
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         params->active_bookmark = ((float)posy / (U.fontsize*3.0f/2.0f));
331         if (params->active_bookmark < 0 || params->active_bookmark > nentries) {
332                 params->active_bookmark = -1;
333         }
334 }
335
336 static void file_select_bookmark(SpaceFile* sfile, ARegion* ar, short x, short y)
337 {
338         if (BLI_in_rcti(&ar->v2d.mask, x, y)) {
339                 char *selected;
340                 set_active_bookmark(sfile->params, ar, x, y);
341                 selected= fsmenu_get_entry(sfile->params->active_bookmark);                     
342                 /* which string */
343                 if (selected) {
344                         FileSelectParams* params = sfile->params;
345                         BLI_strncpy(params->dir, selected, sizeof(params->dir));
346                         BLI_cleanup_dir(G.sce, params->dir);
347                         filelist_free(sfile->files);    
348                         filelist_setdir(sfile->files, params->dir);
349                         params->file[0] = '\0';                 
350                         params->active_file = -1;
351                 }
352         }
353 }
354
355 static int bookmark_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
356 {
357         ScrArea *sa= CTX_wm_area(C);
358         ARegion *ar= CTX_wm_region(C);  
359         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
360
361         short x, y;
362
363         x = event->x - ar->winrct.xmin;
364         y = event->y - ar->winrct.ymin;
365
366         file_select_bookmark(sfile, ar, x, y);
367         ED_area_tag_redraw(sa);
368         return OPERATOR_FINISHED;
369 }
370
371 void ED_FILE_OT_select_bookmark(wmOperatorType *ot)
372 {
373         /* identifiers */
374         ot->name= "Select Directory";
375         ot->idname= "ED_FILE_OT_select_bookmark";
376         
377         /* api callbacks */
378         ot->invoke= bookmark_select_invoke;
379         ot->poll= ED_operator_file_active;
380 }
381
382 static int loadimages_invoke(bContext *C, wmOperator *op, wmEvent *event)
383 {
384         ScrArea *sa= CTX_wm_area(C);
385         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
386         if (sfile->files) {
387                 filelist_loadimage_timer(sfile->files);
388                 if (filelist_changed(sfile->files)) {
389                         ED_area_tag_redraw(sa);
390                 }
391         }
392
393         return OPERATOR_FINISHED;
394 }
395
396 void ED_FILE_OT_loadimages(wmOperatorType *ot)
397 {
398         
399         /* identifiers */
400         ot->name= "Load Images";
401         ot->idname= "ED_FILE_OT_loadimages";
402         
403         /* api callbacks */
404         ot->invoke= loadimages_invoke;
405         
406         ot->poll= ED_operator_file_active;
407 }
408
409 int file_hilight_set(SpaceFile *sfile, ARegion *ar, int mx, int my)
410 {
411         FileSelectParams* params;
412         int numfiles, actfile;
413         
414         if(sfile==NULL || sfile->files==NULL) return 0;
415         
416         numfiles = filelist_numfiles(sfile->files);
417         params = ED_fileselect_get_params(sfile);
418         
419         if (params->display) {
420                 actfile = find_file_mouse_hor(sfile, ar, mx , my);
421         } else {
422                 actfile = find_file_mouse_vert(sfile, ar, mx, my);
423         }
424         
425         if (actfile >= 0 && actfile < numfiles ) {
426                 params->active_file=actfile;
427                 return 1;
428         }
429         return 0;
430 }
431
432 static int file_highlight_invoke(bContext *C, wmOperator *op, wmEvent *event)
433 {
434         ARegion *ar= CTX_wm_region(C);
435         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
436         
437         if( file_hilight_set(sfile, ar, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin)) {
438                 ED_area_tag_redraw(CTX_wm_area(C));
439         }
440         
441         return OPERATOR_FINISHED;
442 }
443
444 void ED_FILE_OT_highlight(struct wmOperatorType *ot)
445 {
446         /* identifiers */
447         ot->name= "Highlight File";
448         ot->idname= "ED_FILE_OT_highlight";
449         
450         /* api callbacks */
451         ot->invoke= file_highlight_invoke;
452         ot->poll= ED_operator_file_active;
453 }
454
455 int file_cancel_exec(bContext *C, wmOperator *unused)
456 {
457         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
458         
459         if(sfile->op) {
460                 WM_operator_free(sfile->op);
461                 sfile->op = NULL;
462         }
463         ED_screen_full_prevspace(C);
464         
465         return OPERATOR_FINISHED;
466 }
467
468 void ED_FILE_OT_cancel(struct wmOperatorType *ot)
469 {
470         /* identifiers */
471         ot->name= "Cancel File Load";
472         ot->idname= "ED_FILE_OT_cancel";
473         
474         /* api callbacks */
475         ot->exec= file_cancel_exec;
476         ot->poll= ED_operator_file_active;
477 }
478
479
480 int file_load_exec(bContext *C, wmOperator *unused)
481 {
482         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
483         char name[FILE_MAX];
484         
485         ED_screen_full_prevspace(C);
486         
487         if(sfile->op) {
488                 wmOperator *op= sfile->op;
489                 
490                 /* if load .blend, all UI pointers after exec are invalid! */
491                 /* but, operator can be freed still */
492                 
493                 sfile->op = NULL;
494                 BLI_strncpy(name, sfile->params->dir, sizeof(name));
495                 strcat(name, sfile->params->file);
496                 RNA_string_set(op->ptr, "filename", name);
497                 
498                 op->type->exec(C, op);
499                 
500                 WM_operator_free(op);
501         }
502                                 
503         return OPERATOR_FINISHED;
504 }
505
506 void ED_FILE_OT_load(struct wmOperatorType *ot)
507 {
508         /* identifiers */
509         ot->name= "Load File";
510         ot->idname= "ED_FILE_OT_load";
511         
512         /* api callbacks */
513         ot->exec= file_load_exec;
514         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
515 }
516
517 int file_parent_exec(bContext *C, wmOperator *unused)
518 {
519         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
520         
521         if(sfile->params) {
522                 BLI_parent_dir(sfile->params->dir);
523                 filelist_setdir(sfile->files, sfile->params->dir);
524                 filelist_free(sfile->files);
525                 sfile->params->active_file = -1;
526         }               
527         ED_area_tag_redraw(CTX_wm_area(C));
528
529         return OPERATOR_FINISHED;
530
531 }
532
533 void ED_FILE_OT_parent(struct wmOperatorType *ot)
534 {
535         /* identifiers */
536         ot->name= "Parent File";
537         ot->idname= "ED_FILE_OT_parent";
538         
539         /* api callbacks */
540         ot->exec= file_parent_exec;
541         ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
542 }
543
544
545