Bugfix #21032: sound strips could not be drag-n-dropped from filebrowser to sequencer
[blender.git] / source / blender / editors / space_sequencer / space_sequencer.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): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "DNA_object_types.h"
33 #include "DNA_space_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_math.h"
41 #include "BLI_rand.h"
42
43 #include "BKE_colortools.h"
44 #include "BKE_context.h"
45 #include "BKE_screen.h"
46 #include "BKE_sequencer.h"
47
48 #include "ED_space_api.h"
49 #include "ED_screen.h"
50
51 #include "BIF_gl.h"
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56 #include "UI_interface.h"
57 #include "UI_resources.h"
58 #include "UI_view2d.h"
59
60 #include "ED_markers.h"
61
62 #include "sequencer_intern.h"   // own include
63
64 /* ******************** manage regions ********************* */
65
66 ARegion *sequencer_has_buttons_region(ScrArea *sa)
67 {
68         ARegion *ar, *arnew;
69         
70         for(ar= sa->regionbase.first; ar; ar= ar->next)
71                 if(ar->regiontype==RGN_TYPE_UI)
72                         return ar;
73         
74         /* add subdiv level; after header */
75         for(ar= sa->regionbase.first; ar; ar= ar->next)
76                 if(ar->regiontype==RGN_TYPE_HEADER)
77                         break;
78         
79         /* is error! */
80         if(ar==NULL) return NULL;
81         
82         arnew= MEM_callocN(sizeof(ARegion), "buttons for sequencer");
83         
84         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
85         arnew->regiontype= RGN_TYPE_UI;
86         arnew->alignment= RGN_ALIGN_RIGHT;
87         
88         arnew->flag = RGN_FLAG_HIDDEN;
89         
90         return arnew;
91 }
92
93 ARegion *sequencer_find_region(ScrArea *sa, short type)
94 {
95         ARegion *ar=NULL;
96         
97         for(ar= sa->regionbase.first; ar; ar= ar->next)
98                 if(ar->regiontype==type)
99                         return ar;
100
101         return ar;
102 }
103
104 void ED_sequencer_update_view(bContext *C, int view)
105 {
106         ScrArea *sa= CTX_wm_area(C);
107         
108         ARegion *ar_main= sequencer_find_region(sa, RGN_TYPE_WINDOW);
109         ARegion *ar_preview= sequencer_find_region(sa, RGN_TYPE_PREVIEW);
110
111         switch (view) {
112                 case SEQ_VIEW_SEQUENCE:
113                         if (ar_main->flag & RGN_FLAG_HIDDEN) {
114                                 ar_main->flag &= ~RGN_FLAG_HIDDEN;
115                                 ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
116                         }
117                         if (!(ar_preview->flag & RGN_FLAG_HIDDEN)) {
118                                 ar_preview->flag |= RGN_FLAG_HIDDEN;
119                                 ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
120                                 WM_event_remove_handlers(C, &ar_preview->handlers);
121                         }
122                         ar_main->alignment= RGN_ALIGN_NONE;
123                         ar_preview->alignment= RGN_ALIGN_NONE;
124                         break;
125                 case SEQ_VIEW_PREVIEW:
126                         if (!(ar_main->flag & RGN_FLAG_HIDDEN)) {
127                                 ar_main->flag |= RGN_FLAG_HIDDEN;
128                                 ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
129                                 WM_event_remove_handlers(C, &ar_main->handlers);
130                         }
131                         if (ar_preview->flag & RGN_FLAG_HIDDEN) {
132                                 ar_preview->flag &= ~RGN_FLAG_HIDDEN;
133                                 ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
134                                 ar_preview->v2d.cur = ar_preview->v2d.tot;
135                         }
136                         ar_main->alignment= RGN_ALIGN_NONE;
137                         ar_preview->alignment= RGN_ALIGN_NONE;
138                         break;
139                 case SEQ_VIEW_SEQUENCE_PREVIEW:
140                         if (ar_main->flag & RGN_FLAG_HIDDEN) {
141                                 ar_main->flag &= ~RGN_FLAG_HIDDEN;
142                                 ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
143                         }
144                         if (ar_preview->flag & RGN_FLAG_HIDDEN) {
145                                 ar_preview->flag &= ~RGN_FLAG_HIDDEN;
146                                 ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
147                                 ar_preview->v2d.cur = ar_preview->v2d.tot;
148                         }
149                         ar_main->alignment= RGN_ALIGN_NONE;
150                         ar_preview->alignment= RGN_ALIGN_TOP;
151                         break;
152         }
153
154         ED_area_initialize(CTX_wm_manager(C), CTX_wm_window(C), sa);
155         ED_area_tag_redraw(sa);
156 }
157
158
159 /* ******************** default callbacks for sequencer space ***************** */
160
161 static SpaceLink *sequencer_new(const bContext *C)
162 {
163         Scene *scene= CTX_data_scene(C);
164         ARegion *ar;
165         SpaceSeq *sseq;
166         
167         sseq= MEM_callocN(sizeof(SpaceSeq), "initsequencer");
168         sseq->spacetype= SPACE_SEQ;
169         sseq->zoom= 4;
170         sseq->chanshown = 0;
171         sseq->view = SEQ_VIEW_SEQUENCE;
172         sseq->mainb = SEQ_DRAW_IMG_IMBUF;
173         /* header */
174         ar= MEM_callocN(sizeof(ARegion), "header for sequencer");
175         
176         BLI_addtail(&sseq->regionbase, ar);
177         ar->regiontype= RGN_TYPE_HEADER;
178         ar->alignment= RGN_ALIGN_BOTTOM;
179         
180         /* buttons/list view */
181         ar= MEM_callocN(sizeof(ARegion), "buttons for sequencer");
182         
183         BLI_addtail(&sseq->regionbase, ar);
184         ar->regiontype= RGN_TYPE_UI;
185         ar->alignment= RGN_ALIGN_RIGHT;
186         ar->flag = RGN_FLAG_HIDDEN;
187         
188         /* preview area */
189         /* NOTE: if you change values here, also change them in sequencer_init_preview_region */
190         ar= MEM_callocN(sizeof(ARegion), "preview area for sequencer");
191         BLI_addtail(&sseq->regionbase, ar);
192         ar->regiontype= RGN_TYPE_PREVIEW;
193         ar->alignment= RGN_ALIGN_TOP;
194         ar->flag |= RGN_FLAG_HIDDEN;
195         /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
196         ar->v2d.keepzoom= V2D_KEEPASPECT | V2D_KEEPZOOM;
197         ar->v2d.minzoom= 0.00001f;
198     ar->v2d.maxzoom= 100000.0f;
199     ar->v2d.tot.xmin= -960.0f; /* 1920 width centered */
200     ar->v2d.tot.ymin= -540.0f; /* 1080 height centered */
201     ar->v2d.tot.xmax= 960.0f;
202     ar->v2d.tot.ymax= 540.0f;
203     ar->v2d.min[0]= 0.0f;
204     ar->v2d.min[1]= 0.0f;
205     ar->v2d.max[0]= 12000.0f;
206     ar->v2d.max[1]= 12000.0f;
207         ar->v2d.cur= ar->v2d.tot;
208         ar->v2d.align= V2D_ALIGN_FREE; 
209         ar->v2d.keeptot= V2D_KEEPTOT_FREE;
210
211
212         /* main area */
213         ar= MEM_callocN(sizeof(ARegion), "main area for sequencer");
214         
215         BLI_addtail(&sseq->regionbase, ar);
216         ar->regiontype= RGN_TYPE_WINDOW;
217         
218         
219         /* seq space goes from (0,8) to (0, efra) */
220         
221         ar->v2d.tot.xmin= 0.0f;
222         ar->v2d.tot.ymin= 0.0f;
223         ar->v2d.tot.xmax= scene->r.efra;
224         ar->v2d.tot.ymax= 8.0f;
225         
226         ar->v2d.cur= ar->v2d.tot;
227         
228         ar->v2d.min[0]= 10.0f;
229         ar->v2d.min[1]= 4.0f;
230         
231         ar->v2d.max[0]= MAXFRAMEF;
232         ar->v2d.max[1]= MAXSEQ;
233         
234         ar->v2d.minzoom= 0.01f;
235         ar->v2d.maxzoom= 100.0f;
236         
237         ar->v2d.scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
238         ar->v2d.scroll |= (V2D_SCROLL_LEFT|V2D_SCROLL_SCALE_VERTICAL);
239         ar->v2d.keepzoom= 0;
240         ar->v2d.keeptot= 0;
241         ar->v2d.align= V2D_ALIGN_NO_NEG_Y;
242
243         return (SpaceLink *)sseq;
244 }
245
246 /* not spacelink itself */
247 static void sequencer_free(SpaceLink *sl)
248 {       
249 //      SpaceSeq *sseq= (SpaceSequencer*) sl;
250         
251 // XXX  if(sseq->gpd) free_gpencil_data(sseq->gpd);
252
253 }
254
255
256 /* spacetype; init callback */
257 static void sequencer_init(struct wmWindowManager *wm, ScrArea *sa)
258 {
259         
260 }
261
262 static SpaceLink *sequencer_duplicate(SpaceLink *sl)
263 {
264         SpaceSeq *sseqn= MEM_dupallocN(sl);
265         
266         /* clear or remove stuff from old */
267 // XXX  sseq->gpd= gpencil_data_duplicate(sseq->gpd);
268
269         return (SpaceLink *)sseqn;
270 }
271
272
273
274 /* *********************** sequencer (main) region ************************ */
275 /* add handlers, stuff you only do once or on area/region changes */
276 static void sequencer_main_area_init(wmWindowManager *wm, ARegion *ar)
277 {
278         wmKeyMap *keymap;
279         ListBase *lb;
280         
281         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
282         
283         keymap= WM_keymap_find(wm->defaultconf, "SequencerCommon", SPACE_SEQ, 0);
284         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
285         
286         /* own keymap */
287         keymap= WM_keymap_find(wm->defaultconf, "Sequencer", SPACE_SEQ, 0);
288         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
289         
290         /* add drop boxes */
291         lb= WM_dropboxmap_find("Sequencer", SPACE_SEQ, RGN_TYPE_WINDOW);
292         
293         WM_event_add_dropbox_handler(&ar->handlers, lb);
294         
295 }
296
297 static void sequencer_main_area_draw(const bContext *C, ARegion *ar)
298 {
299 //      ScrArea *sa= CTX_wm_area(C);
300         
301         /* NLE - strip editing timeline interface */
302         draw_timeline_seq(C, ar);
303 }
304
305 /* ************* dropboxes ************* */
306
307 static int image_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
308 {
309         if(drag->type==WM_DRAG_PATH)
310                 if(ELEM(drag->icon, ICON_FILE_IMAGE, ICON_FILE_BLANK))  /* rule might not work? */
311                         return 1;
312         return 0;
313 }
314
315 static int movie_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
316 {
317         if(drag->type==WM_DRAG_PATH)
318                 if(ELEM(drag->icon, ICON_FILE_MOVIE, ICON_FILE_BLANK))  /* rule might not work? */
319                         return 1;
320         return 0;
321 }
322
323 static int sound_drop_poll(bContext *C, wmDrag *drag, wmEvent *event)
324 {
325         if(drag->type==WM_DRAG_PATH)
326                 if(ELEM(drag->icon, ICON_FILE_SOUND, ICON_FILE_BLANK))  /* rule might not work? */
327                         return 1;
328         return 0;
329 }
330
331 static void sequencer_drop_copy(wmDrag *drag, wmDropBox *drop)
332 {
333         /* copy drag path to properties */
334         RNA_string_set(drop->ptr, "path", drag->path);
335 }
336
337 /* this region dropbox definition */
338 static void sequencer_dropboxes(void)
339 {
340         ListBase *lb= WM_dropboxmap_find("Sequencer", SPACE_SEQ, RGN_TYPE_WINDOW);
341         
342         WM_dropbox_add(lb, "SEQUENCER_OT_image_strip_add", image_drop_poll, sequencer_drop_copy);
343         WM_dropbox_add(lb, "SEQUENCER_OT_movie_strip_add", movie_drop_poll, sequencer_drop_copy);
344         WM_dropbox_add(lb, "SEQUENCER_OT_sound_strip_add", sound_drop_poll, sequencer_drop_copy);
345 }
346
347 /* ************* end drop *********** */
348
349 /* add handlers, stuff you only do once or on area/region changes */
350 static void sequencer_header_area_init(wmWindowManager *wm, ARegion *ar)
351 {
352         ED_region_header_init(ar);
353 }
354
355 static void sequencer_header_area_draw(const bContext *C, ARegion *ar)
356 {
357         ED_region_header(C, ar);
358 }
359
360 static void sequencer_main_area_listener(ARegion *ar, wmNotifier *wmn)
361 {
362         /* context changes */
363         switch(wmn->category) {
364                 case NC_SCENE:
365                         switch(wmn->data) {
366                                 case ND_FRAME:
367                                 case ND_MARKERS:
368                                 case ND_SEQUENCER:
369                                 case ND_SEQUENCER_SELECT:
370                                         ED_region_tag_redraw(ar);
371                                         break;
372                         }
373                         break;
374                 case NC_SPACE:
375                         if(wmn->data == ND_SPACE_SEQUENCER)
376                                 ED_region_tag_redraw(ar);
377                         break;
378                 case NC_ID:
379                         if(wmn->action == NA_RENAME)
380                                 ED_region_tag_redraw(ar);
381                         break;
382         }
383 }
384
385 /* *********************** preview region ************************ */
386 static void sequencer_preview_area_init(wmWindowManager *wm, ARegion *ar)
387 {
388         wmKeyMap *keymap;
389
390         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
391         
392         keymap= WM_keymap_find(wm->defaultconf, "SequencerCommon", SPACE_SEQ, 0);
393         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
394
395         /* own keymap */
396         keymap= WM_keymap_find(wm->defaultconf, "SequencerPreview", SPACE_SEQ, 0);
397         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
398 }
399
400 static void sequencer_preview_area_draw(const bContext *C, ARegion *ar)
401 {
402         ScrArea *sa= CTX_wm_area(C);
403         SpaceSeq *sseq= sa->spacedata.first;
404         Scene *scene= CTX_data_scene(C);
405         
406         /* XXX temp fix for wrong setting in sseq->mainb */
407         if (sseq->mainb == SEQ_DRAW_SEQUENCE) sseq->mainb = SEQ_DRAW_IMG_IMBUF;
408         draw_image_seq(C, scene, ar, sseq);
409 }
410
411 static void sequencer_preview_area_listener(ARegion *ar, wmNotifier *wmn)
412 {
413         /* context changes */
414         switch(wmn->category) {
415                 case NC_SCENE:
416                         switch(wmn->data) {
417                                 case ND_FRAME:
418                                 case ND_MARKERS:
419                                 case ND_SEQUENCER:
420                                 case ND_SEQUENCER_SELECT:
421                                         ED_region_tag_redraw(ar);
422                                         break;
423                         }
424                         break;
425                 case NC_SPACE:
426                         if(wmn->data == ND_SPACE_SEQUENCER)
427                                 ED_region_tag_redraw(ar);
428                         break;
429                 case NC_ID:
430                         switch(wmn->data) {
431                                 case NA_RENAME:
432                                         ED_region_tag_redraw(ar);
433                                         break;
434                         }
435                         break;
436         }
437 }
438
439 /* *********************** buttons region ************************ */
440
441 /* add handlers, stuff you only do once or on area/region changes */
442 static void sequencer_buttons_area_init(wmWindowManager *wm, ARegion *ar)
443 {
444         
445         ED_region_panels_init(wm, ar);
446         
447 }
448
449 static void sequencer_buttons_area_draw(const bContext *C, ARegion *ar)
450 {
451         ED_region_panels(C, ar, 1, NULL, -1);
452 }
453
454 static void sequencer_buttons_area_listener(ARegion *ar, wmNotifier *wmn)
455 {
456         /* context changes */
457         switch(wmn->category) {
458                 case NC_SCENE:
459                 switch(wmn->data) {
460                         case ND_FRAME:
461                         case ND_SEQUENCER:
462                         case ND_SEQUENCER_SELECT:
463                                 ED_region_tag_redraw(ar);
464                                 break;
465                 }
466                 break;
467                 case NC_SPACE:
468                         if(wmn->data == ND_SPACE_SEQUENCER)
469                                 ED_region_tag_redraw(ar);
470                         break;
471                 case NC_ID:
472                         if(wmn->action == NA_RENAME)
473                                 ED_region_tag_redraw(ar);
474                         break;
475         }
476 }
477 /* ************************************* */
478
479 /* only called once, from space/spacetypes.c */
480 void ED_spacetype_sequencer(void)
481 {
482         SpaceType *st= MEM_callocN(sizeof(SpaceType), "spacetype sequencer");
483         ARegionType *art;
484         
485         st->spaceid= SPACE_SEQ;
486         strncpy(st->name, "Sequencer", BKE_ST_MAXNAME);
487         
488         st->new= sequencer_new;
489         st->free= sequencer_free;
490         st->init= sequencer_init;
491         st->duplicate= sequencer_duplicate;
492         st->operatortypes= sequencer_operatortypes;
493         st->keymap= sequencer_keymap;
494         st->dropboxes= sequencer_dropboxes;
495
496         /* regions: main window */
497         art= MEM_callocN(sizeof(ARegionType), "spacetype sequencer region");
498         art->regionid = RGN_TYPE_WINDOW;
499         art->init= sequencer_main_area_init;
500         art->draw= sequencer_main_area_draw;
501         art->listener= sequencer_main_area_listener;
502         art->keymapflag= ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_ANIMATION;
503
504         BLI_addhead(&st->regiontypes, art);
505
506         /* preview */
507         art= MEM_callocN(sizeof(ARegionType), "spacetype sequencer region");
508         art->regionid = RGN_TYPE_PREVIEW;
509         art->prefsizey = 240; // XXX
510         art->init= sequencer_preview_area_init;
511         art->draw= sequencer_preview_area_draw;
512         art->listener= sequencer_preview_area_listener;
513         art->keymapflag= ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_ANIMATION;
514         BLI_addhead(&st->regiontypes, art);
515         
516         /* regions: listview/buttons */
517         art= MEM_callocN(sizeof(ARegionType), "spacetype sequencer region");
518         art->regionid = RGN_TYPE_UI;
519         art->prefsizex= 220; // XXX
520         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_FRAMES;
521         art->listener= sequencer_buttons_area_listener;
522         art->init= sequencer_buttons_area_init;
523         art->draw= sequencer_buttons_area_draw;
524         BLI_addhead(&st->regiontypes, art);
525         
526         /* Keep as python only for now
527         sequencer_buttons_register(art);
528         */
529
530         /* regions: header */
531         art= MEM_callocN(sizeof(ARegionType), "spacetype sequencer region");
532         art->regionid = RGN_TYPE_HEADER;
533         art->prefsizey= HEADERY;
534         art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_HEADER;
535         
536         art->init= sequencer_header_area_init;
537         art->draw= sequencer_header_area_draw;
538         art->listener= sequencer_main_area_listener;
539         
540         BLI_addhead(&st->regiontypes, art);
541         
542         BKE_spacetype_register(st);
543 }
544