Welcome to mirror list, hosted at ThFree Co, Russian Federation.

file_ops.c « space_file « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3d845ac4be6c492ab9d4e6d42f3ebc91e1add5df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/**
 * $Id:
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2008 Blender Foundation.
 * All rights reserved.
 *
 * 
 * Contributor(s): Andrea Weikert (c) 2008 Blender Foundation
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include "BKE_context.h"
#include "BKE_screen.h"
#include "BKE_global.h"

#include "BLI_blenlib.h"
#include "BLI_storage_types.h"
#ifdef WIN32
#include "BLI_winstuff.h"
#endif
#include "DNA_space_types.h"
#include "DNA_userdef_types.h"

#include "ED_space_api.h"
#include "ED_screen.h"
#include "ED_fileselect.h"

#include "RNA_access.h"
#include "RNA_define.h"

#include "UI_interface.h"
#include "UI_view2d.h"

#include "WM_api.h"
#include "WM_types.h"

#include "file_intern.h"
#include "filelist.h"
#include "fsmenu.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

/* for events */
#define NOTACTIVE			0
#define ACTIVATE			1
#define INACTIVATE			2

/* ---------- FILE SELECTION ------------ */

static int find_file_mouse_hor(SpaceFile *sfile, struct ARegion* ar, short x, short y)
{
	float fx,fy;
	int active_file = -1;
	int numfiles = filelist_numfiles(sfile->files);
	View2D* v2d = &ar->v2d;

	UI_view2d_region_to_view(v2d, x, y, &fx, &fy);

	active_file = ED_fileselect_layout_offset(sfile->layout, v2d->tot.xmin + fx, v2d->tot.ymax - fy);

	printf("FINDFILE %d\n", active_file);
	if ( (active_file < 0) || (active_file >= numfiles) )
	{
		active_file = -1;
	}
	return active_file;
}


static int find_file_mouse_vert(SpaceFile *sfile, struct ARegion* ar, short x, short y)
{
	float fx,fy;
	int active_file = -1;
	int numfiles = filelist_numfiles(sfile->files);
	View2D* v2d = &ar->v2d;

	UI_view2d_region_to_view(v2d, x, y, &fx, &fy);
	
	active_file = ED_fileselect_layout_offset(sfile->layout, v2d->tot.xmin + fx, v2d->tot.ymax - fy);

	if ( (active_file < 0) || (active_file >= numfiles) )
	{
		active_file = -1;
	}
	return active_file;
}

static void file_deselect_all(SpaceFile* sfile)
{
	int numfiles = filelist_numfiles(sfile->files);
	int i;

	for ( i=0; i < numfiles; ++i) {
		struct direntry* file = filelist_file(sfile->files, i);
		if (file && (file->flags & ACTIVE)) {
			file->flags &= ~ACTIVE;
		}
	}
}

static void file_select(SpaceFile* sfile, ARegion* ar, const rcti* rect, short val)
{
	int first_file = -1;
	int last_file = -1;
	int act_file;
	short selecting = (val == LEFTMOUSE);
	FileSelectParams *params = ED_fileselect_get_params(sfile);
	FileLayout *layout = ED_fileselect_get_layout(sfile, ar);

	int numfiles = filelist_numfiles(sfile->files);

	params->selstate = NOTACTIVE;
	if  ( (layout->flag == FILE_LAYOUT_HOR) ) {
		first_file = find_file_mouse_hor(sfile, ar, rect->xmin, rect->ymax);
		last_file = find_file_mouse_hor(sfile, ar, rect->xmax, rect->ymin);
	} else {
		first_file = find_file_mouse_vert(sfile, ar, rect->xmin, rect->ymax);
		last_file = find_file_mouse_vert(sfile, ar, rect->xmax, rect->ymin);
	}
	
	/* select all valid files between first and last indicated */
	if ( (first_file >= 0) && (first_file < numfiles) && (last_file >= 0) && (last_file < numfiles) ) {
		for (act_file = first_file; act_file <= last_file; act_file++) {
			struct direntry* file = filelist_file(sfile->files, act_file);
			if (selecting) 
				file->flags |= ACTIVE;
			else
				file->flags &= ~ACTIVE;
		}
	}
	
	printf("Selecting %d %d\n", first_file, last_file);

	/* make the last file active */
	if (last_file >= 0 && last_file < numfiles) {
		struct direntry* file = filelist_file(sfile->files, last_file);
		params->active_file = last_file;

		if(file && S_ISDIR(file->type)) {
			/* the path is too long and we are not going up! */
			if (strcmp(file->relname, ".") &&
				strcmp(file->relname, "..") &&
				strlen(params->dir) + strlen(file->relname) >= FILE_MAX ) 
			{
				// XXX error("Path too long, cannot enter this directory");
			} else {
				if (strcmp(file->relname, "..")==0) {
					/* avoids /../../ */
					BLI_parent_dir(params->dir);
				} else {
					strcat(params->dir, file->relname);
					strcat(params->dir,"/");
					params->file[0] = '\0';
					BLI_cleanup_dir(G.sce, params->dir);
				}
				filelist_setdir(sfile->files, params->dir);
				filelist_free(sfile->files);
				params->active_file = -1;
			}
		}
		else if (file)
		{
			if (file->relname) {
				BLI_strncpy(params->file, file->relname, FILE_MAXFILE);
				/* XXX
				if(event==MIDDLEMOUSE && filelist_gettype(sfile->files)) 
					imasel_execute(sfile);
				*/
			}
			
		}	
		/* XXX
		if(BIF_filelist_gettype(sfile->files)==FILE_MAIN) {
			active_imasel_object(sfile);
		}
		*/
	}
}



static int file_border_select_exec(bContext *C, wmOperator *op)
{
	ARegion *ar= CTX_wm_region(C);
	SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
	short val;
	rcti rect;

	val= RNA_int_get(op->ptr, "event_type");
	rect.xmin= RNA_int_get(op->ptr, "xmin");
	rect.ymin= RNA_int_get(op->ptr, "ymin");
	rect.xmax= RNA_int_get(op->ptr, "xmax");
	rect.ymax= RNA_int_get(op->ptr, "ymax");

	file_select(sfile, ar, &rect, val );
	WM_event_add_notifier(C, NC_WINDOW, NULL);
	return OPERATOR_FINISHED;
}

void FILE_OT_select_border(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Activate/Select File";
	ot->idname= "FILE_OT_select_border";
	
	/* api callbacks */
	ot->invoke= WM_border_select_invoke;
	ot->exec= file_border_select_exec;
	ot->modal= WM_border_select_modal;

	/* rna */
	RNA_def_int(ot->srna, "event_type", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
	RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
	RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
	RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
	RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);

	ot->poll= ED_operator_file_active;
}

static int file_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
	ARegion *ar= CTX_wm_region(C);
	SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
	short val;
	rcti rect;

	rect.xmin = rect.xmax = event->x - ar->winrct.xmin;
	rect.ymin = rect.ymax = event->y - ar->winrct.ymin;
	val = event->val;

	if (BLI_in_rcti(&ar->v2d.mask, rect.xmin, rect.ymin)) { 

		/* single select, deselect all selected first */
		file_deselect_all(sfile);
		file_select(sfile, ar, &rect, val );
		WM_event_add_notifier(C, NC_WINDOW, NULL);
	}
	return OPERATOR_FINISHED;
}

void FILE_OT_select(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Activate/Select File";
	ot->idname= "FILE_OT_select";
	
	/* api callbacks */
	ot->invoke= file_select_invoke;

	/* rna */

	ot->poll= ED_operator_file_active;
}

static int file_select_all_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
	ScrArea *sa= CTX_wm_area(C);
	SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
	int numfiles = filelist_numfiles(sfile->files);
	int i;
	int select = 1;

	/* if any file is selected, deselect all first */
	for ( i=0; i < numfiles; ++i) {
		struct direntry* file = filelist_file(sfile->files, i);
		if (file && (file->flags & ACTIVE)) {
			file->flags &= ~ACTIVE;
			select = 0;
			ED_area_tag_redraw(sa);
		}
	}
	/* select all only if previously no file was selected */
	if (select) {
		for ( i=0; i < numfiles; ++i) {
			struct direntry* file = filelist_file(sfile->files, i);
			if(file && !S_ISDIR(file->type)) {
				file->flags |= ACTIVE;
				ED_area_tag_redraw(sa);
			}
		}
	}
	return OPERATOR_FINISHED;
}

void FILE_OT_select_all_toggle(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Select/Deselect all files";
	ot->idname= "FILE_OT_select_all_toggle";
	
	/* api callbacks */
	ot->invoke= file_select_all_invoke;

	/* rna */

	ot->poll= ED_operator_file_active;
}

/* ---------- BOOKMARKS ----------- */

static void set_active_bookmark(FileSelectParams* params, struct ARegion* ar, short x, short y)
{
	int nentries = fsmenu_get_nentries(fsmenu_get(), FS_CATEGORY_BOOKMARKS);
	float fx, fy;
	short posy;

	UI_view2d_region_to_view(&ar->v2d, x, y, &fx, &fy);

	posy = ar->v2d.cur.ymax - 2*TILE_BORDER_Y - fy;
	posy -= U.fontsize*2.0f;	/* header */
	
	params->active_bookmark = ((float)posy / (U.fontsize*2.0f));
	if (params->active_bookmark < 0 || params->active_bookmark > nentries) {
		params->active_bookmark = -1;
	}
}

static int file_select_bookmark_category(SpaceFile* sfile, ARegion* ar, short x, short y, FSMenuCategory category)
{
	struct FSMenu* fsmenu = fsmenu_get();
	int nentries = fsmenu_get_nentries(fsmenu, category);
	int linestep = U.fontsize*2.0f;
	short xs, ys;
	int i;
	int selected = -1;

	for (i=0; i < nentries; ++i) {
		fsmenu_get_pos(fsmenu, category, i, &xs, &ys);
		if ( (y<=ys) && (y>ys-linestep) ) {
			fsmenu_select_entry(fsmenu, category, i);
			selected = i;
			break;
		}
	}
	return selected;
}

static void file_select_bookmark(SpaceFile* sfile, ARegion* ar, short x, short y)
{
	float fx, fy;
	int selected;
	FSMenuCategory category = FS_CATEGORY_SYSTEM;

	if (BLI_in_rcti(&ar->v2d.mask, x, y)) {
		char *entry;

		UI_view2d_region_to_view(&ar->v2d, x, y, &fx, &fy);
		selected = file_select_bookmark_category(sfile, ar, fx, fy, FS_CATEGORY_SYSTEM);
		if (selected<0) {
			category = FS_CATEGORY_BOOKMARKS;
			selected = file_select_bookmark_category(sfile, ar, fx, fy, category);
		}
		if (selected<0) {
			category = FS_CATEGORY_RECENT;
			selected = file_select_bookmark_category(sfile, ar, fx, fy, category);
		}
		
		if (selected>=0) {
			entry= fsmenu_get_entry(fsmenu_get(), category, selected);			
			/* which string */
			if (entry) {
				FileSelectParams* params = sfile->params;
				BLI_strncpy(params->dir, entry, sizeof(params->dir));
				BLI_cleanup_dir(G.sce, params->dir);
				filelist_free(sfile->files);	
				filelist_setdir(sfile->files, params->dir);
				params->file[0] = '\0';			
				params->active_file = -1;
			}
		}
	}
}

static int bookmark_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
	ScrArea *sa= CTX_wm_area(C);
	ARegion *ar= CTX_wm_region(C);	
	SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);

	short x, y;

	x = event->x - ar->winrct.xmin;
	y = event->y - ar->winrct.ymin;

	file_select_bookmark(sfile, ar, x, y);
	ED_area_tag_redraw(sa);
	return OPERATOR_FINISHED;
}

void FILE_OT_select_bookmark(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Select Directory";
	ot->idname= "FILE_OT_select_bookmark";
	
	/* api callbacks */
	ot->invoke= bookmark_select_invoke;
	ot->poll= ED_operator_file_active;
}

static int loadimages_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
	ScrArea *sa= CTX_wm_area(C);
	SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
	if (sfile->files) {
		filelist_loadimage_timer(sfile->files);
		if (filelist_changed(sfile->files)) {
			ED_area_tag_redraw(sa);
		}
	}

	return OPERATOR_FINISHED;
}

void FILE_OT_loadimages(wmOperatorType *ot)
{
	
	/* identifiers */
	ot->name= "Load Images";
	ot->idname= "FILE_OT_loadimages";
	
	/* api callbacks */
	ot->invoke= loadimages_invoke;
	
	ot->poll= ED_operator_file_active;
}

int file_hilight_set(SpaceFile *sfile, ARegion *ar, int mx, int my)
{
	FileSelectParams* params;
	FileLayout* layout;
	int numfiles, actfile;
	
	if(sfile==NULL || sfile->files==NULL) return 0;
	
	numfiles = filelist_numfiles(sfile->files);
	params = ED_fileselect_get_params(sfile);
	layout = ED_fileselect_get_layout(sfile, ar);

	if ( (layout->flag == FILE_LAYOUT_HOR)) {
		actfile = find_file_mouse_hor(sfile, ar, mx , my);
	} else {
		actfile = find_file_mouse_vert(sfile, ar, mx, my);
	}
	
	if (params && (actfile >= 0) && (actfile < numfiles) ) {
		params->active_file=actfile;
		return 1;
	}
	return 0;
}

static int file_highlight_invoke(bContext *C, wmOperator *op, wmEvent *event)
{
	ARegion *ar= CTX_wm_region(C);
	SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
	
	if( file_hilight_set(sfile, ar, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin)) {
		ED_area_tag_redraw(CTX_wm_area(C));
	}
	
	return OPERATOR_FINISHED;
}

void FILE_OT_highlight(struct wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Highlight File";
	ot->idname= "FILE_OT_highlight";
	
	/* api callbacks */
	ot->invoke= file_highlight_invoke;
	ot->poll= ED_operator_file_active;
}

int file_cancel_exec(bContext *C, wmOperator *unused)
{
	SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
	
	WM_event_fileselect_event(C, sfile->op, EVT_FILESELECT_CANCEL);
	sfile->op = NULL;
	
	return OPERATOR_FINISHED;
}

void FILE_OT_cancel(struct wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Cancel File Load";
	ot->idname= "FILE_OT_cancel";
	
	/* api callbacks */
	ot->exec= file_cancel_exec;
	ot->poll= ED_operator_file_active;
}

/* sends events now, so things get handled on windowqueue level */
int file_exec(bContext *C, wmOperator *unused)
{
	SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
	char name[FILE_MAX];
	
	if(sfile->op) {
		wmOperator *op= sfile->op;
		
		sfile->op = NULL;
		BLI_strncpy(name, sfile->params->dir, sizeof(name));
		strcat(name, sfile->params->file);
		RNA_string_set(op->ptr, "filename", name);
		
		fsmenu_insert_entry(fsmenu_get(), FS_CATEGORY_RECENT, sfile->params->dir,0, 1);
		BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
		fsmenu_write_file(fsmenu_get(), name);
		WM_event_fileselect_event(C, op, EVT_FILESELECT_EXEC);
	}
				
	return OPERATOR_FINISHED;
}

void FILE_OT_exec(struct wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Execute File Window";
	ot->idname= "FILE_OT_exec";
	
	/* api callbacks */
	ot->exec= file_exec;
	ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
}


int file_parent_exec(bContext *C, wmOperator *unused)
{
	SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
	
	if(sfile->params) {
		BLI_parent_dir(sfile->params->dir);
		filelist_setdir(sfile->files, sfile->params->dir);
		filelist_free(sfile->files);
		sfile->params->active_file = -1;
	}		
	ED_area_tag_redraw(CTX_wm_area(C));

	return OPERATOR_FINISHED;

}


void FILE_OT_parent(struct wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Parent File";
	ot->idname= "FILE_OT_parent";
	
	/* api callbacks */
	ot->exec= file_parent_exec;
	ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
}


int file_refresh_exec(bContext *C, wmOperator *unused)
{
	SpaceFile *sfile= (SpaceFile*)CTX_wm_space_data(C);
	
	if(sfile->params) {
		filelist_setdir(sfile->files, sfile->params->dir);
		filelist_free(sfile->files);
		sfile->params->active_file = -1;
	}		
	ED_area_tag_redraw(CTX_wm_area(C));

	return OPERATOR_FINISHED;

}


void FILE_OT_refresh(struct wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Refresh Filelist";
	ot->idname= "FILE_OT_refresh";
	
	/* api callbacks */
	ot->exec= file_refresh_exec;
	ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
}

struct ARegion *file_buttons_region(struct ScrArea *sa)
{
	ARegion *ar;
	
	for(ar= sa->regionbase.first; ar; ar= ar->next)
		if(ar->regiontype==RGN_TYPE_CHANNELS)
			return ar;
	return NULL;
}

int file_bookmark_toggle_exec(bContext *C, wmOperator *unused)
{
	ScrArea *sa= CTX_wm_area(C);
	ARegion *ar= file_buttons_region(sa);
	
	if(ar) {
		ar->flag ^= RGN_FLAG_HIDDEN;
		ar->v2d.flag &= ~V2D_IS_INITIALISED; /* XXX should become hide/unhide api? */
		
		ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
		ED_area_tag_redraw(sa);
	}
	return OPERATOR_FINISHED;
}

void FILE_OT_bookmark_toggle(struct wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Toggle Bookmarks";
	ot->idname= "FILE_OT_bookmark_toggle";
	
	/* api callbacks */
	ot->exec= file_bookmark_toggle_exec;
	ot->poll= ED_operator_file_active; /* <- important, handler is on window level */
}