Cleanup: use static vars where appropriate
[blender.git] / source / blender / editors / space_sequencer / space_sequencer.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_sequencer/space_sequencer.c
28  *  \ingroup spseq
29  */
30
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_gpencil_types.h"
36 #include "DNA_scene_types.h"
37 #include "DNA_mask_types.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_utildefines.h"
43
44 #include "BKE_context.h"
45 #include "BKE_library.h"
46 #include "BKE_screen.h"
47 #include "BKE_sequencer.h"
48 #include "BKE_global.h"
49
50 #include "ED_space_api.h"
51 #include "ED_screen.h"
52 #include "ED_view3d.h" /* only for sequencer view3d drawing callback */
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56
57 #include "UI_interface.h"
58 #include "UI_resources.h"
59 #include "UI_view2d.h"
60
61 #include "IMB_imbuf.h"
62
63 #include "sequencer_intern.h"   // own include
64
65 /**************************** common state *****************************/
66
67 static void sequencer_scopes_tag_refresh(ScrArea *sa)
68 {
69         SpaceSeq *sseq = (SpaceSeq *)sa->spacedata.first;
70
71         sseq->scopes.reference_ibuf = NULL;
72 }
73
74 /* ******************** manage regions ********************* */
75
76 ARegion *sequencer_has_buttons_region(ScrArea *sa)
77 {
78         ARegion *ar, *arnew;
79
80         ar = BKE_area_find_region_type(sa, RGN_TYPE_UI);
81         if (ar) return ar;
82         
83         /* add subdiv level; after header */
84         ar = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
85
86         /* is error! */
87         if (ar == NULL) return NULL;
88         
89         arnew = MEM_callocN(sizeof(ARegion), "buttons for sequencer");
90         
91         BLI_insertlinkafter(&sa->regionbase, ar, arnew);
92         arnew->regiontype = RGN_TYPE_UI;
93         arnew->alignment = RGN_ALIGN_RIGHT;
94         
95         arnew->flag = RGN_FLAG_HIDDEN;
96         
97         return arnew;
98 }
99
100 static ARegion *sequencer_find_region(ScrArea *sa, short type)
101 {
102         ARegion *ar = NULL;
103         
104         for (ar = sa->regionbase.first; ar; ar = ar->next)
105                 if (ar->regiontype == type)
106                         return ar;
107
108         return ar;
109 }
110
111 /* ******************** default callbacks for sequencer space ***************** */
112
113 static SpaceLink *sequencer_new(const bContext *C)
114 {
115         Scene *scene = CTX_data_scene(C);
116         ARegion *ar;
117         SpaceSeq *sseq;
118         
119         sseq = MEM_callocN(sizeof(SpaceSeq), "initsequencer");
120         sseq->spacetype = SPACE_SEQ;
121         sseq->chanshown = 0;
122         sseq->view = SEQ_VIEW_SEQUENCE;
123         sseq->mainb = SEQ_DRAW_IMG_IMBUF;
124         sseq->flag = SEQ_SHOW_GPENCIL | SEQ_USE_ALPHA;
125
126         /* header */
127         ar = MEM_callocN(sizeof(ARegion), "header for sequencer");
128         
129         BLI_addtail(&sseq->regionbase, ar);
130         ar->regiontype = RGN_TYPE_HEADER;
131         ar->alignment = RGN_ALIGN_BOTTOM;
132         
133         /* buttons/list view */
134         ar = MEM_callocN(sizeof(ARegion), "buttons for sequencer");
135         
136         BLI_addtail(&sseq->regionbase, ar);
137         ar->regiontype = RGN_TYPE_UI;
138         ar->alignment = RGN_ALIGN_RIGHT;
139         ar->flag = RGN_FLAG_HIDDEN;
140         
141         /* preview region */
142         /* NOTE: if you change values here, also change them in sequencer_init_preview_region */
143         ar = MEM_callocN(sizeof(ARegion), "preview region for sequencer");
144         BLI_addtail(&sseq->regionbase, ar);
145         ar->regiontype = RGN_TYPE_PREVIEW;
146         ar->alignment = RGN_ALIGN_TOP;
147         ar->flag |= RGN_FLAG_HIDDEN;
148         /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
149         ar->v2d.keepzoom = V2D_KEEPASPECT | V2D_KEEPZOOM | V2D_LIMITZOOM;
150         ar->v2d.minzoom = 0.001f;
151         ar->v2d.maxzoom = 1000.0f;
152         ar->v2d.tot.xmin = -960.0f; /* 1920 width centered */
153         ar->v2d.tot.ymin = -540.0f; /* 1080 height centered */
154         ar->v2d.tot.xmax = 960.0f;
155         ar->v2d.tot.ymax = 540.0f;
156         ar->v2d.min[0] = 0.0f;
157         ar->v2d.min[1] = 0.0f;
158         ar->v2d.max[0] = 12000.0f;
159         ar->v2d.max[1] = 12000.0f;
160         ar->v2d.cur = ar->v2d.tot;
161         ar->v2d.align = V2D_ALIGN_FREE;
162         ar->v2d.keeptot = V2D_KEEPTOT_FREE;
163
164
165         /* main region */
166         ar = MEM_callocN(sizeof(ARegion), "main region for sequencer");
167         
168         BLI_addtail(&sseq->regionbase, ar);
169         ar->regiontype = RGN_TYPE_WINDOW;
170         
171         
172         /* seq space goes from (0,8) to (0, efra) */
173         
174         ar->v2d.tot.xmin = 0.0f;
175         ar->v2d.tot.ymin = 0.0f;
176         ar->v2d.tot.xmax = scene->r.efra;
177         ar->v2d.tot.ymax = 8.0f;
178         
179         ar->v2d.cur = ar->v2d.tot;
180         
181         ar->v2d.min[0] = 10.0f;
182         ar->v2d.min[1] = 0.5f;
183         
184         ar->v2d.max[0] = MAXFRAMEF;
185         ar->v2d.max[1] = MAXSEQ;
186         
187         ar->v2d.minzoom = 0.01f;
188         ar->v2d.maxzoom = 100.0f;
189
190         ar->v2d.scroll |= (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
191         ar->v2d.scroll |= (V2D_SCROLL_LEFT | V2D_SCROLL_SCALE_VERTICAL);
192         ar->v2d.keepzoom = 0;
193         ar->v2d.keeptot = 0;
194         ar->v2d.align = V2D_ALIGN_NO_NEG_Y;
195
196         return (SpaceLink *)sseq;
197 }
198
199 /* not spacelink itself */
200 static void sequencer_free(SpaceLink *sl)
201 {       
202         SpaceSeq *sseq = (SpaceSeq *) sl;
203         SequencerScopes *scopes = &sseq->scopes;
204
205 // XXX  if (sseq->gpd) BKE_gpencil_free(sseq->gpd);
206
207         if (scopes->zebra_ibuf)
208                 IMB_freeImBuf(scopes->zebra_ibuf);
209
210         if (scopes->waveform_ibuf)
211                 IMB_freeImBuf(scopes->waveform_ibuf);
212
213         if (scopes->sep_waveform_ibuf)
214                 IMB_freeImBuf(scopes->sep_waveform_ibuf);
215
216         if (scopes->vector_ibuf)
217                 IMB_freeImBuf(scopes->vector_ibuf);
218
219         if (scopes->histogram_ibuf)
220                 IMB_freeImBuf(scopes->histogram_ibuf);
221 }
222
223
224 /* spacetype; init callback */
225 static void sequencer_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(sa))
226 {
227         
228 }
229
230 static void sequencer_refresh(const bContext *C, ScrArea *sa)
231 {
232         wmWindowManager *wm = CTX_wm_manager(C);
233         wmWindow *window = CTX_wm_window(C);
234         SpaceSeq *sseq = (SpaceSeq *)sa->spacedata.first;
235         ARegion *ar_main = sequencer_find_region(sa, RGN_TYPE_WINDOW);
236         ARegion *ar_preview = sequencer_find_region(sa, RGN_TYPE_PREVIEW);
237         bool view_changed = false;
238
239         switch (sseq->view) {
240                 case SEQ_VIEW_SEQUENCE:
241                         if (ar_main && (ar_main->flag & RGN_FLAG_HIDDEN)) {
242                                 ar_main->flag &= ~RGN_FLAG_HIDDEN;
243                                 ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
244                                 view_changed = true;
245                         }
246                         if (ar_preview && !(ar_preview->flag & RGN_FLAG_HIDDEN)) {
247                                 ar_preview->flag |= RGN_FLAG_HIDDEN;
248                                 ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
249                                 WM_event_remove_handlers((bContext *)C, &ar_preview->handlers);
250                                 view_changed = true;
251                         }
252                         if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
253                                 ar_main->alignment = RGN_ALIGN_NONE;
254                                 view_changed = true;
255                         }
256                         if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
257                                 ar_preview->alignment = RGN_ALIGN_NONE;
258                                 view_changed = true;
259                         }
260                         break;
261                 case SEQ_VIEW_PREVIEW:
262                         if (ar_main && !(ar_main->flag & RGN_FLAG_HIDDEN)) {
263                                 ar_main->flag |= RGN_FLAG_HIDDEN;
264                                 ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
265                                 WM_event_remove_handlers((bContext *)C, &ar_main->handlers);
266                                 view_changed = true;
267                         }
268                         if (ar_preview && (ar_preview->flag & RGN_FLAG_HIDDEN)) {
269                                 ar_preview->flag &= ~RGN_FLAG_HIDDEN;
270                                 ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
271                                 ar_preview->v2d.cur = ar_preview->v2d.tot;
272                                 view_changed = true;
273                         }
274                         if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
275                                 ar_main->alignment = RGN_ALIGN_NONE;
276                                 view_changed = true;
277                         }
278                         if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
279                                 ar_preview->alignment = RGN_ALIGN_NONE;
280                                 view_changed = true;
281                         }
282                         break;
283                 case SEQ_VIEW_SEQUENCE_PREVIEW:
284                         if (ar_main && ar_preview) {
285                                 /* Get available height (without DPI correction). */
286                                 const float height = (sa->winy - ED_area_headersize()) / UI_DPI_FAC;
287
288                                 /* We reuse hidden region's size, allows to find same layout as before if we just switch
289                                  * between one 'full window' view and the combined one. This gets lost if we switch to both
290                                  * 'full window' views before, though... Better than nothing. */
291                                 if (ar_main->flag & RGN_FLAG_HIDDEN) {
292                                         ar_main->flag &= ~RGN_FLAG_HIDDEN;
293                                         ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
294                                         ar_preview->sizey = (int)(height - ar_main->sizey);
295                                         view_changed = true;
296                                 }
297                                 if (ar_preview->flag & RGN_FLAG_HIDDEN) {
298                                         ar_preview->flag &= ~RGN_FLAG_HIDDEN;
299                                         ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
300                                         ar_preview->v2d.cur = ar_preview->v2d.tot;
301                                         ar_main->sizey = (int)(height - ar_preview->sizey);
302                                         view_changed = true;
303                                 }
304                                 if (ar_main->alignment != RGN_ALIGN_NONE) {
305                                         ar_main->alignment = RGN_ALIGN_NONE;
306                                         view_changed = true;
307                                 }
308                                 if (ar_preview->alignment != RGN_ALIGN_TOP) {
309                                         ar_preview->alignment = RGN_ALIGN_TOP;
310                                         view_changed = true;
311                                 }
312                                 /* Final check that both preview and main height are reasonable! */
313                                 if (ar_preview->sizey < 10 || ar_main->sizey < 10 || ar_preview->sizey + ar_main->sizey > height) {
314                                         ar_preview->sizey = (int)(height * 0.4f + 0.5f);
315                                         ar_main->sizey = (int)(height - ar_preview->sizey);
316                                         view_changed = true;
317                                 }
318                         }
319                         break;
320         }
321
322         if (view_changed) {
323                 ED_area_initialize(wm, window, sa);
324                 ED_area_tag_redraw(sa);
325         }
326 }
327
328 static SpaceLink *sequencer_duplicate(SpaceLink *sl)
329 {
330         SpaceSeq *sseqn = MEM_dupallocN(sl);
331         
332         /* clear or remove stuff from old */
333 // XXX  sseq->gpd = gpencil_data_duplicate(sseq->gpd, false);
334
335         memset(&sseqn->scopes, 0, sizeof(sseqn->scopes));
336
337         return (SpaceLink *)sseqn;
338 }
339
340 static void sequencer_listener(bScreen *UNUSED(sc), ScrArea *sa, wmNotifier *wmn)
341 {
342         /* context changes */
343         switch (wmn->category) {
344                 case NC_SCENE:
345                         switch (wmn->data) {
346                                 case ND_FRAME:
347                                 case ND_SEQUENCER:
348                                         sequencer_scopes_tag_refresh(sa);
349                                         break;
350                         }
351                         break;
352                 case NC_WINDOW:
353                 case NC_SPACE:
354                         if (wmn->data == ND_SPACE_SEQUENCER)
355                                 sequencer_scopes_tag_refresh(sa);
356                         break;
357                 case NC_GPENCIL:
358                         if (wmn->data & ND_GPENCIL_EDITMODE)
359                                 ED_area_tag_redraw(sa);
360                         break;
361         }
362 }
363
364 /* ************* dropboxes ************* */
365
366 static int image_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event)
367 {
368         ARegion *ar = CTX_wm_region(C);
369         Scene *scene = CTX_data_scene(C);
370         int hand;
371
372         if (drag->type == WM_DRAG_PATH)
373                 if (ELEM(drag->icon, ICON_FILE_IMAGE, ICON_FILE_BLANK)) /* rule might not work? */
374                         if (find_nearest_seq(scene, &ar->v2d, &hand, event->mval) == NULL)
375                                 return 1;
376
377         return 0;
378 }
379
380 static int movie_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event)
381 {
382         ARegion *ar = CTX_wm_region(C);
383         Scene *scene = CTX_data_scene(C);
384         int hand;
385
386         if (drag->type == WM_DRAG_PATH)
387                 if (ELEM(drag->icon, 0, ICON_FILE_MOVIE, ICON_FILE_BLANK)) /* rule might not work? */
388                         if (find_nearest_seq(scene, &ar->v2d, &hand, event->mval) == NULL)
389                                 return 1;
390         return 0;
391 }
392
393 static int sound_drop_poll(bContext *C, wmDrag *drag, const wmEvent *event)
394 {
395         ARegion *ar = CTX_wm_region(C);
396         Scene *scene = CTX_data_scene(C);
397         int hand;
398
399         if (drag->type == WM_DRAG_PATH)
400                 if (ELEM(drag->icon, ICON_FILE_SOUND, ICON_FILE_BLANK)) /* rule might not work? */
401                         if (find_nearest_seq(scene, &ar->v2d, &hand, event->mval) == NULL)
402                                 return 1;
403         return 0;
404 }
405
406 static void sequencer_drop_copy(wmDrag *drag, wmDropBox *drop)
407 {
408         /* copy drag path to properties */
409         if (RNA_struct_find_property(drop->ptr, "filepath"))
410                 RNA_string_set(drop->ptr, "filepath", drag->path);
411
412         if (RNA_struct_find_property(drop->ptr, "directory")) {
413                 PointerRNA itemptr;
414                 char dir[FILE_MAX], file[FILE_MAX];
415
416                 BLI_split_dirfile(drag->path, dir, file, sizeof(dir), sizeof(file));
417                 
418                 RNA_string_set(drop->ptr, "directory", dir);
419
420                 RNA_collection_clear(drop->ptr, "files");
421                 RNA_collection_add(drop->ptr, "files", &itemptr);
422                 RNA_string_set(&itemptr, "name", file);
423         }
424 }
425
426 /* this region dropbox definition */
427 static void sequencer_dropboxes(void)
428 {
429         ListBase *lb = WM_dropboxmap_find("Sequencer", SPACE_SEQ, RGN_TYPE_WINDOW);
430
431         WM_dropbox_add(lb, "SEQUENCER_OT_image_strip_add", image_drop_poll, sequencer_drop_copy);
432         WM_dropbox_add(lb, "SEQUENCER_OT_movie_strip_add", movie_drop_poll, sequencer_drop_copy);
433         WM_dropbox_add(lb, "SEQUENCER_OT_sound_strip_add", sound_drop_poll, sequencer_drop_copy);
434 }
435
436 /* ************* end drop *********** */
437
438 static const char *sequencer_context_dir[] = {"edit_mask", NULL};
439
440 static int sequencer_context(const bContext *C, const char *member, bContextDataResult *result)
441 {
442         Scene *scene = CTX_data_scene(C);
443
444         if (CTX_data_dir(member)) {
445                 CTX_data_dir_set(result, sequencer_context_dir);
446
447                 return true;
448         }
449         else if (CTX_data_equals(member, "edit_mask")) {
450                 Mask *mask = BKE_sequencer_mask_get(scene);
451                 if (mask) {
452                         CTX_data_id_pointer_set(result, &mask->id);
453                 }
454                 return true;
455         }
456
457         return false;
458 }
459
460 /* *********************** sequencer (main) region ************************ */
461 /* add handlers, stuff you only do once or on area/region changes */
462 static void sequencer_main_region_init(wmWindowManager *wm, ARegion *ar)
463 {
464         wmKeyMap *keymap;
465         ListBase *lb;
466
467         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
468
469 #if 0
470         keymap = WM_keymap_find(wm->defaultconf, "Mask Editing", 0, 0);
471         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
472 #endif
473
474         keymap = WM_keymap_find(wm->defaultconf, "SequencerCommon", SPACE_SEQ, 0);
475         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
476
477         /* own keymap */
478         keymap = WM_keymap_find(wm->defaultconf, "Sequencer", SPACE_SEQ, 0);
479         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
480
481         /* add drop boxes */
482         lb = WM_dropboxmap_find("Sequencer", SPACE_SEQ, RGN_TYPE_WINDOW);
483
484         WM_event_add_dropbox_handler(&ar->handlers, lb);
485 }
486
487 static void sequencer_main_region_draw(const bContext *C, ARegion *ar)
488 {
489         /* NLE - strip editing timeline interface */
490         draw_timeline_seq(C, ar);
491 }
492
493 static void sequencer_main_region_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn)
494 {
495         /* context changes */
496         switch (wmn->category) {
497                 case NC_SCENE:
498                         switch (wmn->data) {
499                                 case ND_FRAME:
500                                 case ND_FRAME_RANGE:
501                                 case ND_MARKERS:
502                                 case ND_RENDER_OPTIONS: /* for FPS and FPS Base */
503                                 case ND_SEQUENCER:
504                                 case ND_RENDER_RESULT:
505                                         ED_region_tag_redraw(ar);
506                                         break;
507                         }
508                         break;
509                 case NC_ANIMATION:
510                         switch (wmn->data) {
511                                 case ND_KEYFRAME:
512                                         ED_region_tag_redraw(ar);
513                                         break;
514                         }
515                         break;
516                 case NC_SPACE:
517                         if (wmn->data == ND_SPACE_SEQUENCER)
518                                 ED_region_tag_redraw(ar);
519                         break;
520                 case NC_ID:
521                         if (wmn->action == NA_RENAME)
522                                 ED_region_tag_redraw(ar);
523                         break;
524                 case NC_SCREEN:
525                         if (ELEM(wmn->data, ND_SCREENCAST, ND_ANIMPLAY))
526                                 ED_region_tag_redraw(ar);
527                         break;
528         }
529 }
530
531 /* *********************** header region ************************ */
532 /* add handlers, stuff you only do once or on area/region changes */
533 static void sequencer_header_region_init(wmWindowManager *UNUSED(wm), ARegion *ar)
534 {
535         ED_region_header_init(ar);
536 }
537
538 static void sequencer_header_region_draw(const bContext *C, ARegion *ar)
539 {
540         ED_region_header(C, ar);
541 }
542
543 /* *********************** preview region ************************ */
544 static void sequencer_preview_region_init(wmWindowManager *wm, ARegion *ar)
545 {
546         wmKeyMap *keymap;
547
548         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
549
550 #if 0
551         keymap = WM_keymap_find(wm->defaultconf, "Mask Editing", 0, 0);
552         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
553 #endif
554
555         keymap = WM_keymap_find(wm->defaultconf, "SequencerCommon", SPACE_SEQ, 0);
556         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
557
558         /* own keymap */
559         keymap = WM_keymap_find(wm->defaultconf, "SequencerPreview", SPACE_SEQ, 0);
560         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
561 }
562
563 static void sequencer_preview_region_draw(const bContext *C, ARegion *ar)
564 {
565         ScrArea *sa = CTX_wm_area(C);
566         SpaceSeq *sseq = sa->spacedata.first;
567         Scene *scene = CTX_data_scene(C);
568         wmWindowManager *wm = CTX_wm_manager(C);
569         const bool show_split = (
570                 scene->ed &&
571                 (scene->ed->over_flag & SEQ_EDIT_OVERLAY_SHOW) &&
572                 (sseq->mainb == SEQ_DRAW_IMG_IMBUF));
573
574         /* XXX temp fix for wrong setting in sseq->mainb */
575         if (sseq->mainb == SEQ_DRAW_SEQUENCE) sseq->mainb = SEQ_DRAW_IMG_IMBUF;
576
577         if (!show_split || sseq->overlay_type != SEQ_DRAW_OVERLAY_REFERENCE)
578                 draw_image_seq(C, scene, ar, sseq, scene->r.cfra, 0, false, false);
579
580         if (show_split && sseq->overlay_type != SEQ_DRAW_OVERLAY_CURRENT) {
581                 int over_cfra;
582
583                 if (scene->ed->over_flag & SEQ_EDIT_OVERLAY_ABS)
584                         over_cfra = scene->ed->over_cfra;
585                 else
586                         over_cfra = scene->r.cfra + scene->ed->over_ofs;
587
588                 if (over_cfra != scene->r.cfra || sseq->overlay_type != SEQ_DRAW_OVERLAY_RECT)
589                         draw_image_seq(C, scene, ar, sseq, scene->r.cfra, over_cfra - scene->r.cfra, true, false);
590         }
591
592         if ((U.uiflag & USER_SHOW_FPS) && ED_screen_animation_no_scrub(wm)) {
593                 rcti rect;
594                 ED_region_visible_rect(ar, &rect);
595                 ED_scene_draw_fps(scene, &rect);
596         }
597 }
598
599 static void sequencer_preview_region_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn)
600 {
601         /* context changes */
602         switch (wmn->category) {
603                 case NC_GPENCIL:
604                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED)) {
605                                 ED_region_tag_redraw(ar);
606                         }
607                         break;
608                 case NC_SCENE:
609                         switch (wmn->data) {
610                                 case ND_FRAME:
611                                 case ND_MARKERS:
612                                 case ND_SEQUENCER:
613                                 case ND_RENDER_OPTIONS:
614                                 case ND_DRAW_RENDER_VIEWPORT:
615                                         ED_region_tag_redraw(ar);
616                                         break;
617                         }
618                         break;
619                 case NC_ANIMATION:
620                         switch (wmn->data) {
621                                 case ND_KEYFRAME:
622                                         /* Otherwise, often prevents seing immediately effects of keyframe editing... */
623                                         BKE_sequencer_cache_cleanup();
624                                         ED_region_tag_redraw(ar);
625                                         break;
626                         }
627                         break;
628                 case NC_SPACE:
629                         if (wmn->data == ND_SPACE_SEQUENCER)
630                                 ED_region_tag_redraw(ar);
631                         break;
632                 case NC_ID:
633                         switch (wmn->data) {
634                                 case NA_RENAME:
635                                         ED_region_tag_redraw(ar);
636                                         break;
637                         }
638                         break;
639                 case NC_MASK:
640                         if (wmn->action == NA_EDITED) {
641                                 ED_region_tag_redraw(ar);
642                         }
643                         break;
644         }
645 }
646
647 /* *********************** buttons region ************************ */
648
649 /* add handlers, stuff you only do once or on area/region changes */
650 static void sequencer_buttons_region_init(wmWindowManager *wm, ARegion *ar)
651 {
652         wmKeyMap *keymap;
653
654         keymap = WM_keymap_find(wm->defaultconf, "SequencerCommon", SPACE_SEQ, 0);
655         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
656
657         ED_region_panels_init(wm, ar);
658 }
659
660 static void sequencer_buttons_region_draw(const bContext *C, ARegion *ar)
661 {
662         ED_region_panels(C, ar, NULL, -1, true);
663 }
664
665 static void sequencer_buttons_region_listener(bScreen *UNUSED(sc), ScrArea *UNUSED(sa), ARegion *ar, wmNotifier *wmn)
666 {
667         /* context changes */
668         switch (wmn->category) {
669                 case NC_GPENCIL:
670                         if (ELEM(wmn->action, NA_EDITED, NA_SELECTED)) {
671                                 ED_region_tag_redraw(ar);
672                         }
673                         break;
674                 case NC_SCENE:
675                         switch (wmn->data) {
676                                 case ND_FRAME:
677                                 case ND_SEQUENCER:
678                                         ED_region_tag_redraw(ar);
679                                         break;
680                         }
681                         break;
682                 case NC_SPACE:
683                         if (wmn->data == ND_SPACE_SEQUENCER)
684                                 ED_region_tag_redraw(ar);
685                         break;
686                 case NC_ID:
687                         if (wmn->action == NA_RENAME)
688                                 ED_region_tag_redraw(ar);
689                         break;
690         }
691 }
692
693 static void sequencer_id_remap(ScrArea *UNUSED(sa), SpaceLink *slink, ID *old_id, ID *new_id)
694 {
695         SpaceSeq *sseq = (SpaceSeq *)slink;
696
697         if (!ELEM(GS(old_id->name), ID_GD)) {
698                 return;
699         }
700
701         if ((ID *)sseq->gpd == old_id) {
702                 sseq->gpd = (bGPdata *)new_id;
703                 id_us_min(old_id);
704                 id_us_plus(new_id);
705         }
706 }
707
708 /* ************************************* */
709
710 /* only called once, from space/spacetypes.c */
711 void ED_spacetype_sequencer(void)
712 {
713         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype sequencer");
714         ARegionType *art;
715
716         st->spaceid = SPACE_SEQ;
717         strncpy(st->name, "Sequencer", BKE_ST_MAXNAME);
718
719         st->new = sequencer_new;
720         st->free = sequencer_free;
721         st->init = sequencer_init;
722         st->duplicate = sequencer_duplicate;
723         st->operatortypes = sequencer_operatortypes;
724         st->keymap = sequencer_keymap;
725         st->context = sequencer_context;
726         st->dropboxes = sequencer_dropboxes;
727         st->refresh = sequencer_refresh;
728         st->listener = sequencer_listener;
729         st->id_remap = sequencer_id_remap;
730
731         /* regions: main window */
732         art = MEM_callocN(sizeof(ARegionType), "spacetype sequencer region");
733         art->regionid = RGN_TYPE_WINDOW;
734         art->init = sequencer_main_region_init;
735         art->draw = sequencer_main_region_draw;
736         art->listener = sequencer_main_region_listener;
737         art->keymapflag = ED_KEYMAP_VIEW2D | ED_KEYMAP_MARKERS | ED_KEYMAP_FRAMES | ED_KEYMAP_ANIMATION;
738
739         BLI_addhead(&st->regiontypes, art);
740
741         /* preview */
742         art = MEM_callocN(sizeof(ARegionType), "spacetype sequencer region");
743         art->regionid = RGN_TYPE_PREVIEW;
744         art->init = sequencer_preview_region_init;
745         art->draw = sequencer_preview_region_draw;
746         art->listener = sequencer_preview_region_listener;
747         art->keymapflag = ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_GPENCIL;
748         BLI_addhead(&st->regiontypes, art);
749
750         /* regions: listview/buttons */
751         art = MEM_callocN(sizeof(ARegionType), "spacetype sequencer region");
752         art->regionid = RGN_TYPE_UI;
753         art->prefsizex = 220; // XXX
754         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_FRAMES;
755         art->listener = sequencer_buttons_region_listener;
756         art->init = sequencer_buttons_region_init;
757         art->draw = sequencer_buttons_region_draw;
758         BLI_addhead(&st->regiontypes, art);
759
760         sequencer_buttons_register(art);
761
762         /* regions: header */
763         art = MEM_callocN(sizeof(ARegionType), "spacetype sequencer region");
764         art->regionid = RGN_TYPE_HEADER;
765         art->prefsizey = HEADERY;
766         art->keymapflag = ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_FRAMES | ED_KEYMAP_HEADER;
767
768         art->init = sequencer_header_region_init;
769         art->draw = sequencer_header_region_draw;
770         art->listener = sequencer_main_region_listener;
771
772         BLI_addhead(&st->regiontypes, art);
773
774         BKE_spacetype_register(st);
775
776         /* set the sequencer callback when not in background mode */
777         if (G.background == 0) {
778                 sequencer_view3d_cb = ED_view3d_draw_offscreen_imbuf_simple;
779         }
780 }