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 y)
322 {
323         int nentries = fsmenu_get_nentries();
324         short posy = ar->v2d.mask.ymax - TILE_BORDER_Y - y;
325         params->active_bookmark = ((float)posy / (U.fontsize*3.0f/2.0f));
326         if (params->active_bookmark < 0 || params->active_bookmark > nentries) {
327                 params->active_bookmark = -1;
328         }
329 }
330
331 static void file_select_bookmark(SpaceFile* sfile, ARegion* ar, short x, short y)
332 {
333         if (BLI_in_rcti(&ar->v2d.mask, x, y)) {
334                 char *selected;
335                 set_active_bookmark(sfile->params, ar, y);
336                 selected= fsmenu_get_entry(sfile->params->active_bookmark);                     
337                 /* which string */
338                 if (selected) {
339                         FileSelectParams* params = sfile->params;
340                         BLI_strncpy(params->dir, selected, sizeof(params->dir));
341                         BLI_cleanup_dir(G.sce, params->dir);
342                         filelist_free(sfile->files);    
343                         filelist_setdir(sfile->files, params->dir);
344                         params->file[0] = '\0';                 
345                         params->active_file = -1;
346                 }
347         }
348 }
349
350 static int bookmark_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
351 {
352         ScrArea *sa= CTX_wm_area(C);
353         ARegion *ar= CTX_wm_region(C);  
354         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
355
356         short x, y;
357
358         x = event->x - ar->winrct.xmin;
359         y = event->y - ar->winrct.ymin;
360
361         file_select_bookmark(sfile, ar, x, y);
362         ED_area_tag_redraw(sa);
363         return OPERATOR_FINISHED;
364 }
365
366 void ED_FILE_OT_select_bookmark(wmOperatorType *ot)
367 {
368         /* identifiers */
369         ot->name= "Select Directory";
370         ot->idname= "ED_FILE_OT_select_bookmark";
371         
372         /* api callbacks */
373         ot->invoke= bookmark_select_invoke;
374         ot->poll= ED_operator_file_active;
375 }
376
377 static int loadimages_invoke(bContext *C, wmOperator *op, wmEvent *event)
378 {
379         ScrArea *sa= CTX_wm_area(C);
380         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
381         if (sfile->files) {
382                 filelist_loadimage_timer(sfile->files);
383                 if (filelist_changed(sfile->files)) {
384                         ED_area_tag_redraw(sa);
385                 }
386         }
387
388         return OPERATOR_FINISHED;
389 }
390
391 void ED_FILE_OT_loadimages(wmOperatorType *ot)
392 {
393         
394         /* identifiers */
395         ot->name= "Load Images";
396         ot->idname= "ED_FILE_OT_loadimages";
397         
398         /* api callbacks */
399         ot->invoke= loadimages_invoke;
400         
401         ot->poll= ED_operator_file_active;
402 }
403
404 int file_hilight_set(SpaceFile *sfile, ARegion *ar, int mx, int my)
405 {
406         FileSelectParams* params;
407         int numfiles, actfile;
408         
409         if(sfile==NULL || sfile->files==NULL) return 0;
410         
411         numfiles = filelist_numfiles(sfile->files);
412         params = ED_fileselect_get_params(sfile);
413         
414         if (params->display) {
415                 actfile = find_file_mouse_hor(sfile, ar, mx , my);
416         } else {
417                 actfile = find_file_mouse_vert(sfile, ar, mx, my);
418         }
419         
420         if (actfile >= 0 && actfile < numfiles ) {
421                 params->active_file=actfile;
422                 return 1;
423         }
424         return 0;
425 }
426
427 static int file_highlight_invoke(bContext *C, wmOperator *op, wmEvent *event)
428 {
429         ARegion *ar= CTX_wm_region(C);
430         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
431         
432         if( file_hilight_set(sfile, ar, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin)) {
433                 ED_area_tag_redraw(CTX_wm_area(C));
434         }
435         
436         return OPERATOR_FINISHED;
437 }
438
439 void ED_FILE_OT_highlight(struct wmOperatorType *ot)
440 {
441         /* identifiers */
442         ot->name= "Highlight File";
443         ot->idname= "ED_FILE_OT_highlight";
444         
445         /* api callbacks */
446         ot->invoke= file_highlight_invoke;
447         ot->poll= ED_operator_file_active;
448 }
449
450 int file_cancel_exec(bContext *C, wmOperator *unused)
451 {
452         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
453         
454         if(sfile->op) {
455                 WM_operator_free(sfile->op);
456                 sfile->op = NULL;
457         }
458         ED_screen_full_prevspace(C);
459         
460         return OPERATOR_FINISHED;
461 }
462
463 void ED_FILE_OT_cancel(struct wmOperatorType *ot)
464 {
465         /* identifiers */
466         ot->name= "Cancel File Load";
467         ot->idname= "ED_FILE_OT_cancel";
468         
469         /* api callbacks */
470         ot->exec= file_cancel_exec;
471         ot->poll= ED_operator_file_active;
472 }
473
474
475 int file_load_exec(bContext *C, wmOperator *unused)
476 {
477         SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
478         char name[FILE_MAX];
479         
480         ED_screen_full_prevspace(C);
481         
482         if(sfile->op) {
483                 wmOperator *op= sfile->op;
484                 
485                 /* if load .blend, all UI pointers after exec are invalid! */
486                 /* but, operator can be freed still */
487                 
488                 sfile->op = NULL;
489                 BLI_strncpy(name, sfile->params->dir, sizeof(name));
490                 strcat(name, sfile->params->file);
491                 RNA_string_set(op->ptr, "filename", name);
492                 
493                 op->type->exec(C, op);
494                 
495                 WM_operator_free(op);
496         }
497                                 
498         return OPERATOR_FINISHED;
499 }
500
501 void ED_FILE_OT_load(struct wmOperatorType *ot)
502 {
503         /* identifiers */
504         ot->name= "Load File";
505         ot->idname= "ED_FILE_OT_load";
506         
507         /* api callbacks */
508         ot->exec= file_load_exec;
509         ot->poll= ED_operator_file_active;
510 }
511
512
513