PyTemplates operator_modal_view3d_raycast: update to blender2.8 (*/@)
[blender.git] / source / blender / editors / space_clip / space_clip.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) 2011 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation,
23  *                 Sergey Sharybin
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_clip/space_clip.c
29  *  \ingroup spclip
30  */
31
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "DNA_scene_types.h"
36 #include "DNA_mask_types.h"
37 #include "DNA_movieclip_types.h"
38 #include "DNA_view3d_types.h"  /* for pivot point */
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
44 #include "BLI_math.h"
45
46 #include "BKE_context.h"
47 #include "BKE_library.h"
48 #include "BKE_movieclip.h"
49 #include "BKE_tracking.h"
50 #include "BKE_screen.h"
51
52 #include "IMB_imbuf_types.h"
53
54 #include "ED_anim_api.h" /* for timeline cursor drawing */
55 #include "ED_mask.h"
56 #include "ED_space_api.h"
57 #include "ED_screen.h"
58 #include "ED_select_utils.h"
59 #include "ED_clip.h"
60 #include "ED_transform.h"
61 #include "ED_uvedit.h"  /* just for ED_image_draw_cursor */
62
63 #include "IMB_imbuf.h"
64
65 #include "GPU_glew.h"
66 #include "GPU_matrix.h"
67 #include "GPU_framebuffer.h"
68
69 #include "WM_api.h"
70 #include "WM_types.h"
71
72 #include "UI_interface.h"
73 #include "UI_resources.h"
74 #include "UI_view2d.h"
75
76 #include "RNA_access.h"
77
78
79 #include "clip_intern.h"  /* own include */
80
81 static void init_preview_region(const Scene *scene, const ScrArea *sa, const SpaceClip *sc, ARegion *ar)
82 {
83         ar->regiontype = RGN_TYPE_PREVIEW;
84         ar->alignment = RGN_ALIGN_TOP;
85         ar->flag |= RGN_FLAG_HIDDEN;
86
87         if (sc->view == SC_VIEW_DOPESHEET) {
88                 ar->v2d.tot.xmin = -10.0f;
89                 ar->v2d.tot.ymin = (float)(-sa->winy) / 3.0f;
90                 ar->v2d.tot.xmax = (float)(sa->winx);
91                 ar->v2d.tot.ymax = 0.0f;
92
93                 ar->v2d.cur = ar->v2d.tot;
94
95                 ar->v2d.min[0] = 0.0f;
96                 ar->v2d.min[1] = 0.0f;
97
98                 ar->v2d.max[0] = MAXFRAMEF;
99                 ar->v2d.max[1] = FLT_MAX;
100
101                 ar->v2d.minzoom = 0.01f;
102                 ar->v2d.maxzoom = 50;
103                 ar->v2d.scroll = (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
104                 ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
105                 ar->v2d.keepzoom = V2D_LOCKZOOM_Y;
106                 ar->v2d.keepofs = V2D_KEEPOFS_Y;
107                 ar->v2d.align = V2D_ALIGN_NO_POS_Y;
108                 ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
109         }
110         else {
111                 ar->v2d.tot.xmin = 0.0f;
112                 ar->v2d.tot.ymin = -10.0f;
113                 ar->v2d.tot.xmax = (float)scene->r.efra;
114                 ar->v2d.tot.ymax = 10.0f;
115
116                 ar->v2d.cur = ar->v2d.tot;
117
118                 ar->v2d.min[0] = FLT_MIN;
119                 ar->v2d.min[1] = FLT_MIN;
120
121                 ar->v2d.max[0] = MAXFRAMEF;
122                 ar->v2d.max[1] = FLT_MAX;
123
124                 ar->v2d.scroll = (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
125                 ar->v2d.scroll |= (V2D_SCROLL_LEFT | V2D_SCROLL_SCALE_VERTICAL);
126
127                 ar->v2d.minzoom = 0.0f;
128                 ar->v2d.maxzoom = 0.0f;
129                 ar->v2d.keepzoom = 0;
130                 ar->v2d.keepofs = 0;
131                 ar->v2d.align = 0;
132                 ar->v2d.flag = 0;
133
134                 ar->v2d.keeptot = 0;
135         }
136 }
137
138 static void reinit_preview_region(const bContext *C, ARegion *ar)
139 {
140         Scene *scene = CTX_data_scene(C);
141         ScrArea *sa = CTX_wm_area(C);
142         SpaceClip *sc = CTX_wm_space_clip(C);
143
144         if (sc->view == SC_VIEW_DOPESHEET) {
145                 if ((ar->v2d.flag & V2D_VIEWSYNC_AREA_VERTICAL) == 0)
146                         init_preview_region(scene, sa, sc, ar);
147         }
148         else {
149                 if (ar->v2d.flag & V2D_VIEWSYNC_AREA_VERTICAL)
150                         init_preview_region(scene, sa, sc, ar);
151         }
152 }
153
154 static ARegion *ED_clip_has_preview_region(const bContext *C, ScrArea *sa)
155 {
156         ARegion *ar, *arnew;
157
158         ar = BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
159         if (ar)
160                 return ar;
161
162         /* add subdiv level; after header */
163         ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
164
165         /* is error! */
166         if (ar == NULL)
167                 return NULL;
168
169         arnew = MEM_callocN(sizeof(ARegion), "clip preview region");
170
171         BLI_insertlinkbefore(&sa->regionbase, ar, arnew);
172         init_preview_region(CTX_data_scene(C), sa, CTX_wm_space_clip(C), arnew);
173
174         return arnew;
175 }
176
177 static ARegion *ED_clip_has_channels_region(ScrArea *sa)
178 {
179         ARegion *ar, *arnew;
180
181         ar = BKE_area_find_region_type(sa, RGN_TYPE_CHANNELS);
182         if (ar)
183                 return ar;
184
185         /* add subdiv level; after header */
186         ar = BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
187
188         /* is error! */
189         if (ar == NULL)
190                 return NULL;
191
192         arnew = MEM_callocN(sizeof(ARegion), "clip channels region");
193
194         BLI_insertlinkbefore(&sa->regionbase, ar, arnew);
195         arnew->regiontype = RGN_TYPE_CHANNELS;
196         arnew->alignment = RGN_ALIGN_LEFT;
197
198         arnew->v2d.scroll = V2D_SCROLL_BOTTOM;
199         arnew->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
200
201         return arnew;
202 }
203
204 static void clip_scopes_tag_refresh(ScrArea *sa)
205 {
206         SpaceClip *sc = (SpaceClip *)sa->spacedata.first;
207         ARegion *ar;
208
209         if (sc->mode != SC_MODE_TRACKING)
210                 return;
211
212         /* only while properties are visible */
213         for (ar = sa->regionbase.first; ar; ar = ar->next) {
214                 if (ar->regiontype == RGN_TYPE_UI && ar->flag & RGN_FLAG_HIDDEN)
215                         return;
216         }
217
218         sc->scopes.ok = false;
219 }
220
221 static void clip_scopes_check_gpencil_change(ScrArea *sa)
222 {
223         SpaceClip *sc = (SpaceClip *)sa->spacedata.first;
224
225         if (sc->gpencil_src == SC_GPENCIL_SRC_TRACK) {
226                 clip_scopes_tag_refresh(sa);
227         }
228 }
229
230 /* ******************** default callbacks for clip space ***************** */
231
232 static SpaceLink *clip_new(const ScrArea *sa, const Scene *scene)
233 {
234         ARegion *ar;
235         SpaceClip *sc;
236
237         sc = MEM_callocN(sizeof(SpaceClip), "initclip");
238         sc->spacetype = SPACE_CLIP;
239         sc->flag = SC_SHOW_MARKER_PATTERN | SC_SHOW_TRACK_PATH |
240                    SC_SHOW_GRAPH_TRACKS_MOTION | SC_SHOW_GRAPH_FRAMES | SC_SHOW_ANNOTATION;
241         sc->zoom = 1.0f;
242         sc->path_length = 20;
243         sc->scopes.track_preview_height = 120;
244         sc->around = V3D_AROUND_LOCAL_ORIGINS;
245
246         /* header */
247         ar = MEM_callocN(sizeof(ARegion), "header for clip");
248
249         BLI_addtail(&sc->regionbase, ar);
250         ar->regiontype = RGN_TYPE_HEADER;
251         ar->alignment = RGN_ALIGN_TOP;
252
253         /* tools view */
254         ar = MEM_callocN(sizeof(ARegion), "tools for clip");
255
256         BLI_addtail(&sc->regionbase, ar);
257         ar->regiontype = RGN_TYPE_TOOLS;
258         ar->alignment = RGN_ALIGN_LEFT;
259
260         /* properties view */
261         ar = MEM_callocN(sizeof(ARegion), "properties for clip");
262
263         BLI_addtail(&sc->regionbase, ar);
264         ar->regiontype = RGN_TYPE_UI;
265         ar->alignment = RGN_ALIGN_RIGHT;
266
267         /* channels view */
268         ar = MEM_callocN(sizeof(ARegion), "channels for clip");
269
270         BLI_addtail(&sc->regionbase, ar);
271         ar->regiontype = RGN_TYPE_CHANNELS;
272         ar->alignment = RGN_ALIGN_LEFT;
273
274         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
275         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
276
277         /* preview view */
278         ar = MEM_callocN(sizeof(ARegion), "preview for clip");
279
280         BLI_addtail(&sc->regionbase, ar);
281         init_preview_region(scene, sa, sc, ar);
282
283         /* main region */
284         ar = MEM_callocN(sizeof(ARegion), "main region for clip");
285
286         BLI_addtail(&sc->regionbase, ar);
287         ar->regiontype = RGN_TYPE_WINDOW;
288
289         return (SpaceLink *) sc;
290 }
291
292 /* not spacelink itself */
293 static void clip_free(SpaceLink *sl)
294 {
295         SpaceClip *sc = (SpaceClip *) sl;
296
297         sc->clip = NULL;
298
299         if (sc->scopes.track_preview)
300                 IMB_freeImBuf(sc->scopes.track_preview);
301
302         if (sc->scopes.track_search)
303                 IMB_freeImBuf(sc->scopes.track_search);
304 }
305
306 /* spacetype; init callback */
307 static void clip_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
308 {
309         ListBase *lb = WM_dropboxmap_find("Clip", SPACE_CLIP, 0);
310
311         /* add drop boxes */
312         WM_event_add_dropbox_handler(&sa->handlers, lb);
313 }
314
315 static SpaceLink *clip_duplicate(SpaceLink *sl)
316 {
317         SpaceClip *scn = MEM_dupallocN(sl);
318
319         /* clear or remove stuff from old */
320         scn->scopes.track_search = NULL;
321         scn->scopes.track_preview = NULL;
322         scn->scopes.ok = false;
323
324         return (SpaceLink *)scn;
325 }
326
327 static void clip_listener(wmWindow *UNUSED(win), ScrArea *sa, wmNotifier *wmn, Scene *UNUSED(scene))
328 {
329         /* context changes */
330         switch (wmn->category) {
331                 case NC_SCENE:
332                         switch (wmn->data) {
333                                 case ND_FRAME:
334                                         clip_scopes_tag_refresh(sa);
335                                         ATTR_FALLTHROUGH;
336
337                                 case ND_FRAME_RANGE:
338                                         ED_area_tag_redraw(sa);
339                                         break;
340                         }
341                         break;
342                 case NC_MOVIECLIP:
343                         switch (wmn->data) {
344                                 case ND_DISPLAY:
345                                 case ND_SELECT:
346                                         clip_scopes_tag_refresh(sa);
347                                         ED_area_tag_redraw(sa);
348                                         break;
349                         }
350                         switch (wmn->action) {
351                                 case NA_REMOVED:
352                                 case NA_EDITED:
353                                 case NA_EVALUATED:
354                                         /* fall-through */
355
356                                 case NA_SELECTED:
357                                         clip_scopes_tag_refresh(sa);
358                                         ED_area_tag_redraw(sa);
359                                         break;
360                         }
361                         break;
362                 case NC_MASK:
363                         switch (wmn->data) {
364                                 case ND_SELECT:
365                                 case ND_DATA:
366                                 case ND_DRAW:
367                                         ED_area_tag_redraw(sa);
368                                         break;
369                         }
370                         switch (wmn->action) {
371                                 case NA_SELECTED:
372                                         ED_area_tag_redraw(sa);
373                                         break;
374                                 case NA_EDITED:
375                                         ED_area_tag_redraw(sa);
376                                         break;
377                         }
378                         break;
379                 case NC_GEOM:
380                         switch (wmn->data) {
381                                 case ND_SELECT:
382                                         clip_scopes_tag_refresh(sa);
383                                         ED_area_tag_redraw(sa);
384                                         break;
385                         }
386                         break;
387                 case NC_SCREEN:
388                         switch (wmn->data) {
389                                 case ND_ANIMPLAY:
390                                         ED_area_tag_redraw(sa);
391                                         break;
392                         }
393                         break;
394                 case NC_SPACE:
395                         if (wmn->data == ND_SPACE_CLIP) {
396                                 clip_scopes_tag_refresh(sa);
397                                 ED_area_tag_redraw(sa);
398                         }
399                         break;
400                 case NC_GPENCIL:
401                         if (wmn->action == NA_EDITED) {
402                                 clip_scopes_check_gpencil_change(sa);
403                                 ED_area_tag_redraw(sa);
404                         }
405                         else if (wmn->data & ND_GPENCIL_EDITMODE) {
406                                 ED_area_tag_redraw(sa);
407                         }
408                         break;
409         }
410 }
411
412 static void clip_operatortypes(void)
413 {
414         /* ** clip_ops.c ** */
415         WM_operatortype_append(CLIP_OT_open);
416         WM_operatortype_append(CLIP_OT_reload);
417         WM_operatortype_append(CLIP_OT_view_pan);
418         WM_operatortype_append(CLIP_OT_view_zoom);
419         WM_operatortype_append(CLIP_OT_view_zoom_in);
420         WM_operatortype_append(CLIP_OT_view_zoom_out);
421         WM_operatortype_append(CLIP_OT_view_zoom_ratio);
422         WM_operatortype_append(CLIP_OT_view_all);
423         WM_operatortype_append(CLIP_OT_view_selected);
424         WM_operatortype_append(CLIP_OT_change_frame);
425         WM_operatortype_append(CLIP_OT_rebuild_proxy);
426         WM_operatortype_append(CLIP_OT_mode_set);
427 #ifdef WITH_INPUT_NDOF
428         WM_operatortype_append(CLIP_OT_view_ndof);
429 #endif
430         WM_operatortype_append(CLIP_OT_prefetch);
431         WM_operatortype_append(CLIP_OT_set_scene_frames);
432         WM_operatortype_append(CLIP_OT_cursor_set);
433
434         /* ** clip_toolbar.c ** */
435         WM_operatortype_append(CLIP_OT_tools);
436         WM_operatortype_append(CLIP_OT_properties);
437
438         /* ** tracking_ops.c ** */
439
440         /* navigation */
441         WM_operatortype_append(CLIP_OT_frame_jump);
442
443         /* set optical center to frame center */
444         WM_operatortype_append(CLIP_OT_set_center_principal);
445
446         /* selection */
447         WM_operatortype_append(CLIP_OT_select);
448         WM_operatortype_append(CLIP_OT_select_all);
449         WM_operatortype_append(CLIP_OT_select_box);
450         WM_operatortype_append(CLIP_OT_select_lasso);
451         WM_operatortype_append(CLIP_OT_select_circle);
452         WM_operatortype_append(CLIP_OT_select_grouped);
453
454         /* markers */
455         WM_operatortype_append(CLIP_OT_add_marker);
456         WM_operatortype_append(CLIP_OT_add_marker_at_click);
457         WM_operatortype_append(CLIP_OT_slide_marker);
458         WM_operatortype_append(CLIP_OT_delete_track);
459         WM_operatortype_append(CLIP_OT_delete_marker);
460
461         /* track */
462         WM_operatortype_append(CLIP_OT_track_markers);
463         WM_operatortype_append(CLIP_OT_refine_markers);
464
465         /* solving */
466         WM_operatortype_append(CLIP_OT_solve_camera);
467         WM_operatortype_append(CLIP_OT_clear_solution);
468
469         WM_operatortype_append(CLIP_OT_disable_markers);
470         WM_operatortype_append(CLIP_OT_hide_tracks);
471         WM_operatortype_append(CLIP_OT_hide_tracks_clear);
472         WM_operatortype_append(CLIP_OT_lock_tracks);
473
474         WM_operatortype_append(CLIP_OT_set_solver_keyframe);
475
476         /* orientation */
477         WM_operatortype_append(CLIP_OT_set_origin);
478         WM_operatortype_append(CLIP_OT_set_plane);
479         WM_operatortype_append(CLIP_OT_set_axis);
480         WM_operatortype_append(CLIP_OT_set_scale);
481         WM_operatortype_append(CLIP_OT_set_solution_scale);
482         WM_operatortype_append(CLIP_OT_apply_solution_scale);
483
484         /* detect */
485         WM_operatortype_append(CLIP_OT_detect_features);
486
487         /* stabilization */
488         WM_operatortype_append(CLIP_OT_stabilize_2d_add);
489         WM_operatortype_append(CLIP_OT_stabilize_2d_remove);
490         WM_operatortype_append(CLIP_OT_stabilize_2d_select);
491         WM_operatortype_append(CLIP_OT_stabilize_2d_rotation_add);
492         WM_operatortype_append(CLIP_OT_stabilize_2d_rotation_remove);
493         WM_operatortype_append(CLIP_OT_stabilize_2d_rotation_select);
494
495         /* clean-up */
496         WM_operatortype_append(CLIP_OT_clear_track_path);
497         WM_operatortype_append(CLIP_OT_join_tracks);
498         WM_operatortype_append(CLIP_OT_track_copy_color);
499
500         WM_operatortype_append(CLIP_OT_clean_tracks);
501
502         /* object tracking */
503         WM_operatortype_append(CLIP_OT_tracking_object_new);
504         WM_operatortype_append(CLIP_OT_tracking_object_remove);
505
506         /* clipboard */
507         WM_operatortype_append(CLIP_OT_copy_tracks);
508         WM_operatortype_append(CLIP_OT_paste_tracks);
509
510         /* Plane tracker */
511         WM_operatortype_append(CLIP_OT_create_plane_track);
512         WM_operatortype_append(CLIP_OT_slide_plane_marker);
513
514         WM_operatortype_append(CLIP_OT_keyframe_insert);
515         WM_operatortype_append(CLIP_OT_keyframe_delete);
516
517         /* ** clip_graph_ops.c  ** */
518
519         /* graph editing */
520
521         /* selection */
522         WM_operatortype_append(CLIP_OT_graph_select);
523         WM_operatortype_append(CLIP_OT_graph_select_box);
524         WM_operatortype_append(CLIP_OT_graph_select_all_markers);
525
526         WM_operatortype_append(CLIP_OT_graph_delete_curve);
527         WM_operatortype_append(CLIP_OT_graph_delete_knot);
528         WM_operatortype_append(CLIP_OT_graph_view_all);
529         WM_operatortype_append(CLIP_OT_graph_center_current_frame);
530
531         WM_operatortype_append(CLIP_OT_graph_disable_markers);
532
533         /* ** clip_dopesheet_ops.c  ** */
534
535         WM_operatortype_append(CLIP_OT_dopesheet_select_channel);
536         WM_operatortype_append(CLIP_OT_dopesheet_view_all);
537 }
538
539 static void clip_keymap(struct wmKeyConfig *keyconf)
540 {
541         /* ******** Global hotkeys avalaible for all regions ******** */
542         WM_keymap_ensure(keyconf, "Clip", SPACE_CLIP, 0);
543
544         /* ******** Hotkeys avalaible for main region only ******** */
545         WM_keymap_ensure(keyconf, "Clip Editor", SPACE_CLIP, 0);
546 //      keymap->poll = ED_space_clip_tracking_poll;
547
548         /* ******** Hotkeys avalaible for preview region only ******** */
549         WM_keymap_ensure(keyconf, "Clip Graph Editor", SPACE_CLIP, 0);
550
551         /* ******** Hotkeys avalaible for channels region only ******** */
552         WM_keymap_ensure(keyconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
553 }
554
555 /* DO NOT make this static, this hides the symbol and breaks API generation script. */
556 const char *clip_context_dir[] = {"edit_movieclip", "edit_mask", NULL};
557
558 static int clip_context(const bContext *C, const char *member, bContextDataResult *result)
559 {
560         SpaceClip *sc = CTX_wm_space_clip(C);
561
562         if (CTX_data_dir(member)) {
563                 CTX_data_dir_set(result, clip_context_dir);
564
565                 return true;
566         }
567         else if (CTX_data_equals(member, "edit_movieclip")) {
568                 if (sc->clip)
569                         CTX_data_id_pointer_set(result, &sc->clip->id);
570                 return true;
571         }
572         else if (CTX_data_equals(member, "edit_mask")) {
573                 if (sc->mask_info.mask)
574                         CTX_data_id_pointer_set(result, &sc->mask_info.mask->id);
575                 return true;
576         }
577
578         return false;
579 }
580
581 /* dropboxes */
582 static bool clip_drop_poll(bContext *UNUSED(C), wmDrag *drag, const wmEvent *UNUSED(event), const char **UNUSED(tooltip))
583 {
584         if (drag->type == WM_DRAG_PATH)
585                 if (ELEM(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_MOVIE, ICON_FILE_BLANK)) /* rule might not work? */
586                         return true;
587
588         return false;
589 }
590
591 static void clip_drop_copy(wmDrag *drag, wmDropBox *drop)
592 {
593         PointerRNA itemptr;
594         char dir[FILE_MAX], file[FILE_MAX];
595
596         BLI_split_dirfile(drag->path, dir, file, sizeof(dir), sizeof(file));
597
598         RNA_string_set(drop->ptr, "directory", dir);
599
600         RNA_collection_clear(drop->ptr, "files");
601         RNA_collection_add(drop->ptr, "files", &itemptr);
602         RNA_string_set(&itemptr, "name", file);
603 }
604
605 /* area+region dropbox definition */
606 static void clip_dropboxes(void)
607 {
608         ListBase *lb = WM_dropboxmap_find("Clip", SPACE_CLIP, 0);
609
610         WM_dropbox_add(lb, "CLIP_OT_open", clip_drop_poll, clip_drop_copy);
611 }
612
613 static void clip_refresh(const bContext *C, ScrArea *sa)
614 {
615         wmWindowManager *wm = CTX_wm_manager(C);
616         wmWindow *window = CTX_wm_window(C);
617         Scene *scene = CTX_data_scene(C);
618         SpaceClip *sc = (SpaceClip *)sa->spacedata.first;
619         ARegion *ar_main = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
620         ARegion *ar_tools = BKE_area_find_region_type(sa, RGN_TYPE_TOOLS);
621         ARegion *ar_preview = ED_clip_has_preview_region(C, sa);
622         ARegion *ar_properties = ED_clip_has_properties_region(sa);
623         ARegion *ar_channels = ED_clip_has_channels_region(sa);
624         bool main_visible = false, preview_visible = false, tools_visible = false;
625         bool properties_visible = false, channels_visible = false;
626         bool view_changed = false;
627
628         switch (sc->view) {
629                 case SC_VIEW_CLIP:
630                         main_visible = true;
631                         preview_visible = false;
632                         tools_visible = true;
633                         properties_visible = true;
634                         channels_visible = false;
635                         break;
636                 case SC_VIEW_GRAPH:
637                         main_visible = false;
638                         preview_visible = true;
639                         tools_visible = false;
640                         properties_visible = false;
641                         channels_visible = false;
642
643                         reinit_preview_region(C, ar_preview);
644                         break;
645                 case SC_VIEW_DOPESHEET:
646                         main_visible = false;
647                         preview_visible = true;
648                         tools_visible = false;
649                         properties_visible = false;
650                         channels_visible = true;
651
652                         reinit_preview_region(C, ar_preview);
653                         break;
654         }
655
656         if (main_visible) {
657                 if (ar_main && (ar_main->flag & RGN_FLAG_HIDDEN)) {
658                         ar_main->flag &= ~RGN_FLAG_HIDDEN;
659                         ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
660                         view_changed = true;
661                 }
662
663                 if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
664                         ar_main->alignment = RGN_ALIGN_NONE;
665                         view_changed = true;
666                 }
667         }
668         else {
669                 if (ar_main && !(ar_main->flag & RGN_FLAG_HIDDEN)) {
670                         ar_main->flag |= RGN_FLAG_HIDDEN;
671                         ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
672                         WM_event_remove_handlers((bContext *)C, &ar_main->handlers);
673                         view_changed = true;
674                 }
675                 if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
676                         ar_main->alignment = RGN_ALIGN_NONE;
677                         view_changed = true;
678                 }
679         }
680
681         if (properties_visible) {
682                 if (ar_properties && (ar_properties->flag & RGN_FLAG_HIDDEN)) {
683                         ar_properties->flag &= ~RGN_FLAG_HIDDEN;
684                         ar_properties->v2d.flag &= ~V2D_IS_INITIALISED;
685                         view_changed = true;
686                 }
687                 if (ar_properties && ar_properties->alignment != RGN_ALIGN_RIGHT) {
688                         ar_properties->alignment = RGN_ALIGN_RIGHT;
689                         view_changed = true;
690                 }
691         }
692         else {
693                 if (ar_properties && !(ar_properties->flag & RGN_FLAG_HIDDEN)) {
694                         ar_properties->flag |= RGN_FLAG_HIDDEN;
695                         ar_properties->v2d.flag &= ~V2D_IS_INITIALISED;
696                         WM_event_remove_handlers((bContext *)C, &ar_properties->handlers);
697                         view_changed = true;
698                 }
699                 if (ar_properties && ar_properties->alignment != RGN_ALIGN_NONE) {
700                         ar_properties->alignment = RGN_ALIGN_NONE;
701                         view_changed = true;
702                 }
703         }
704
705         if (tools_visible) {
706                 if (ar_tools && (ar_tools->flag & RGN_FLAG_HIDDEN)) {
707                         ar_tools->flag &= ~RGN_FLAG_HIDDEN;
708                         ar_tools->v2d.flag &= ~V2D_IS_INITIALISED;
709                         view_changed = true;
710                 }
711                 if (ar_tools && ar_tools->alignment != RGN_ALIGN_LEFT) {
712                         ar_tools->alignment = RGN_ALIGN_LEFT;
713                         view_changed = true;
714                 }
715         }
716         else {
717                 if (ar_tools && !(ar_tools->flag & RGN_FLAG_HIDDEN)) {
718                         ar_tools->flag |= RGN_FLAG_HIDDEN;
719                         ar_tools->v2d.flag &= ~V2D_IS_INITIALISED;
720                         WM_event_remove_handlers((bContext *)C, &ar_tools->handlers);
721                         view_changed = true;
722                 }
723                 if (ar_tools && ar_tools->alignment != RGN_ALIGN_NONE) {
724                         ar_tools->alignment = RGN_ALIGN_NONE;
725                         view_changed = true;
726                 }
727         }
728
729         if (preview_visible) {
730                 if (ar_preview && (ar_preview->flag & RGN_FLAG_HIDDEN)) {
731                         ar_preview->flag &= ~RGN_FLAG_HIDDEN;
732                         ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
733                         ar_preview->v2d.cur = ar_preview->v2d.tot;
734                         view_changed = true;
735                 }
736                 if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
737                         ar_preview->alignment = RGN_ALIGN_NONE;
738                         view_changed = true;
739                 }
740         }
741         else {
742                 if (ar_preview && !(ar_preview->flag & RGN_FLAG_HIDDEN)) {
743                         ar_preview->flag |= RGN_FLAG_HIDDEN;
744                         ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
745                         WM_event_remove_handlers((bContext *)C, &ar_preview->handlers);
746                         view_changed = true;
747                 }
748                 if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
749                         ar_preview->alignment = RGN_ALIGN_NONE;
750                         view_changed = true;
751                 }
752         }
753
754         if (channels_visible) {
755                 if (ar_channels && (ar_channels->flag & RGN_FLAG_HIDDEN)) {
756                         ar_channels->flag &= ~RGN_FLAG_HIDDEN;
757                         ar_channels->v2d.flag &= ~V2D_IS_INITIALISED;
758                         view_changed = true;
759                 }
760                 if (ar_channels && ar_channels->alignment != RGN_ALIGN_LEFT) {
761                         ar_channels->alignment = RGN_ALIGN_LEFT;
762                         view_changed = true;
763                 }
764         }
765         else {
766                 if (ar_channels && !(ar_channels->flag & RGN_FLAG_HIDDEN)) {
767                         ar_channels->flag |= RGN_FLAG_HIDDEN;
768                         ar_channels->v2d.flag &= ~V2D_IS_INITIALISED;
769                         WM_event_remove_handlers((bContext *)C, &ar_channels->handlers);
770                         view_changed = true;
771                 }
772                 if (ar_channels && ar_channels->alignment != RGN_ALIGN_NONE) {
773                         ar_channels->alignment = RGN_ALIGN_NONE;
774                         view_changed = true;
775                 }
776         }
777
778         if (view_changed) {
779                 ED_area_initialize(wm, window, sa);
780                 ED_area_tag_redraw(sa);
781         }
782
783         BKE_movieclip_user_set_frame(&sc->user, scene->r.cfra);
784 }
785
786 /********************* main region ********************/
787
788 /* sets up the fields of the View2D from zoom and offset */
789 static void movieclip_main_area_set_view2d(const bContext *C, ARegion *ar)
790 {
791         SpaceClip *sc = CTX_wm_space_clip(C);
792         float x1, y1, w, h, aspx, aspy;
793         int width, height, winx, winy;
794
795         ED_space_clip_get_size(sc, &width, &height);
796         ED_space_clip_get_aspect(sc, &aspx, &aspy);
797
798         w = width * aspx;
799         h = height * aspy;
800
801         winx = BLI_rcti_size_x(&ar->winrct) + 1;
802         winy = BLI_rcti_size_y(&ar->winrct) + 1;
803
804         ar->v2d.tot.xmin = 0;
805         ar->v2d.tot.ymin = 0;
806         ar->v2d.tot.xmax = w;
807         ar->v2d.tot.ymax = h;
808
809         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
810         ar->v2d.mask.xmax = winx;
811         ar->v2d.mask.ymax = winy;
812
813         /* which part of the image space do we see? */
814         x1 = ar->winrct.xmin + (winx - sc->zoom * w) / 2.0f;
815         y1 = ar->winrct.ymin + (winy - sc->zoom * h) / 2.0f;
816
817         x1 -= sc->zoom * sc->xof;
818         y1 -= sc->zoom * sc->yof;
819
820         /* relative display right */
821         ar->v2d.cur.xmin = (ar->winrct.xmin - (float)x1) / sc->zoom;
822         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sc->zoom);
823
824         /* relative display left */
825         ar->v2d.cur.ymin = (ar->winrct.ymin - (float)y1) / sc->zoom;
826         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sc->zoom);
827
828         /* normalize 0.0..1.0 */
829         ar->v2d.cur.xmin /= w;
830         ar->v2d.cur.xmax /= w;
831         ar->v2d.cur.ymin /= h;
832         ar->v2d.cur.ymax /= h;
833 }
834
835 /* add handlers, stuff you only do once or on area/region changes */
836 static void clip_main_region_init(wmWindowManager *wm, ARegion *ar)
837 {
838         wmKeyMap *keymap;
839
840         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
841
842         /* mask polls mode */
843         keymap = WM_keymap_ensure(wm->defaultconf, "Mask Editing", 0, 0);
844         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
845
846         /* own keymap */
847         keymap = WM_keymap_ensure(wm->defaultconf, "Clip", SPACE_CLIP, 0);
848         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
849
850         keymap = WM_keymap_ensure(wm->defaultconf, "Clip Editor", SPACE_CLIP, 0);
851         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
852 }
853
854 static void clip_main_region_draw(const bContext *C, ARegion *ar)
855 {
856         /* draw entirely, view changes should be handled here */
857         SpaceClip *sc = CTX_wm_space_clip(C);
858         MovieClip *clip = ED_space_clip_get_clip(sc);
859         float aspx, aspy, zoomx, zoomy, x, y;
860         int width, height;
861         bool show_cursor = false;
862
863         /* if tracking is in progress, we should synchronize framenr from clipuser
864          * so latest tracked frame would be shown */
865         if (clip && clip->tracking_context)
866                 BKE_autotrack_context_sync_user(clip->tracking_context, &sc->user);
867
868         if (sc->flag & SC_LOCK_SELECTION) {
869                 ImBuf *tmpibuf = NULL;
870
871                 if (clip && clip->tracking.stabilization.flag & TRACKING_2D_STABILIZATION) {
872                         tmpibuf = ED_space_clip_get_stable_buffer(sc, NULL, NULL, NULL);
873                 }
874
875                 if (ED_clip_view_selection(C, ar, 0)) {
876                         sc->xof += sc->xlockof;
877                         sc->yof += sc->ylockof;
878                 }
879
880                 if (tmpibuf)
881                         IMB_freeImBuf(tmpibuf);
882         }
883
884         /* clear and setup matrix */
885         UI_ThemeClearColor(TH_BACK);
886         GPU_clear(GPU_COLOR_BIT);
887
888         /* data... */
889         movieclip_main_area_set_view2d(C, ar);
890
891         clip_draw_main(C, sc, ar);
892
893         /* TODO(sergey): would be nice to find a way to de-duplicate all this space conversions */
894         UI_view2d_view_to_region_fl(&ar->v2d, 0.0f, 0.0f, &x, &y);
895         ED_space_clip_get_size(sc, &width, &height);
896         ED_space_clip_get_zoom(sc, ar, &zoomx, &zoomy);
897         ED_space_clip_get_aspect(sc, &aspx, &aspy);
898
899         if (sc->mode == SC_MODE_MASKEDIT) {
900                 Mask *mask = CTX_data_edit_mask(C);
901                 if (mask && clip) {
902                         ScrArea *sa = CTX_wm_area(C);
903                         int mask_width, mask_height;
904                         ED_mask_get_size(sa, &mask_width, &mask_height);
905                         ED_mask_draw_region(mask, ar,
906                                             sc->mask_info.draw_flag,
907                                             sc->mask_info.draw_type,
908                                             sc->mask_info.overlay_mode,
909                                             mask_width, mask_height,
910                                             aspx, aspy,
911                                             true, true,
912                                             sc->stabmat, C);
913                 }
914         }
915
916         show_cursor |= sc->mode == SC_MODE_MASKEDIT;
917         show_cursor |= sc->around == V3D_AROUND_CURSOR;
918
919         if (show_cursor) {
920                 GPU_matrix_push();
921                 GPU_matrix_translate_2f(x, y);
922                 GPU_matrix_scale_2f(zoomx, zoomy);
923                 GPU_matrix_mul(sc->stabmat);
924                 GPU_matrix_scale_2f(width, height);
925                 ED_image_draw_cursor(ar, sc->cursor);
926                 GPU_matrix_pop();
927         }
928
929         clip_draw_cache_and_notes(C, sc, ar);
930
931         if (sc->flag & SC_SHOW_ANNOTATION) {
932                 /* Grease Pencil */
933                 clip_draw_grease_pencil((bContext *)C, true);
934         }
935
936         /* reset view matrix */
937         UI_view2d_view_restore(C);
938
939         if (sc->flag & SC_SHOW_ANNOTATION) {
940                 /* draw Grease Pencil - screen space only */
941                 clip_draw_grease_pencil((bContext *)C, false);
942         }
943 }
944
945 static void clip_main_region_listener(
946         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
947         wmNotifier *wmn, const Scene *UNUSED(scene))
948 {
949         /* context changes */
950         switch (wmn->category) {
951                 case NC_GPENCIL:
952                         if (wmn->action == NA_EDITED)
953                                 ED_region_tag_redraw(ar);
954                         else if (wmn->data & ND_GPENCIL_EDITMODE)
955                                 ED_region_tag_redraw(ar);
956                         break;
957         }
958 }
959
960 /****************** preview region ******************/
961
962 static void clip_preview_region_init(wmWindowManager *wm, ARegion *ar)
963 {
964         wmKeyMap *keymap;
965
966         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
967
968         /* own keymap */
969         keymap = WM_keymap_ensure(wm->defaultconf, "Clip", SPACE_CLIP, 0);
970         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
971
972         keymap = WM_keymap_ensure(wm->defaultconf, "Clip Graph Editor", SPACE_CLIP, 0);
973         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
974
975         keymap = WM_keymap_ensure(wm->defaultconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
976         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
977 }
978
979 static void graph_region_draw(const bContext *C, ARegion *ar)
980 {
981         View2D *v2d = &ar->v2d;
982         View2DScrollers *scrollers;
983         SpaceClip *sc = CTX_wm_space_clip(C);
984         Scene *scene = CTX_data_scene(C);
985         short unitx, unity;
986         short cfra_flag = 0;
987
988         if (sc->flag & SC_LOCK_TIMECURSOR)
989                 ED_clip_graph_center_current_frame(scene, ar);
990
991         /* clear and setup matrix */
992         UI_ThemeClearColor(TH_BACK);
993         GPU_clear(GPU_COLOR_BIT);
994
995         UI_view2d_view_ortho(v2d);
996
997         /* data... */
998         clip_draw_graph(sc, ar, scene);
999
1000         /* current frame indicator line */
1001         if (sc->flag & SC_SHOW_SECONDS) cfra_flag |= DRAWCFRA_UNIT_SECONDS;
1002         ANIM_draw_cfra(C, v2d, cfra_flag);
1003
1004         /* reset view matrix */
1005         UI_view2d_view_restore(C);
1006
1007         /* scrollers */
1008         unitx = (sc->flag & SC_SHOW_SECONDS) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
1009         unity = V2D_UNIT_VALUES;
1010         scrollers = UI_view2d_scrollers_calc(C, v2d, NULL, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
1011         UI_view2d_scrollers_draw(C, v2d, scrollers);
1012         UI_view2d_scrollers_free(scrollers);
1013
1014         /* current frame indicator */
1015         if (sc->flag & SC_SHOW_SECONDS) cfra_flag |= DRAWCFRA_UNIT_SECONDS;
1016         UI_view2d_view_orthoSpecial(ar, v2d, 1);
1017         ANIM_draw_cfra_number(C, v2d, cfra_flag);
1018 }
1019
1020 static void dopesheet_region_draw(const bContext *C, ARegion *ar)
1021 {
1022         Scene *scene = CTX_data_scene(C);
1023         SpaceClip *sc = CTX_wm_space_clip(C);
1024         MovieClip *clip = ED_space_clip_get_clip(sc);
1025         View2D *v2d = &ar->v2d;
1026         View2DGrid *grid;
1027         View2DScrollers *scrollers;
1028         short unit = 0, cfra_flag = 0;
1029
1030         if (clip)
1031                 BKE_tracking_dopesheet_update(&clip->tracking);
1032
1033         /* clear and setup matrix */
1034         UI_ThemeClearColor(TH_BACK);
1035         GPU_clear(GPU_COLOR_BIT);
1036
1037         UI_view2d_view_ortho(v2d);
1038
1039         /* time grid */
1040         unit = (sc->flag & SC_SHOW_SECONDS) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
1041         grid = UI_view2d_grid_calc(scene, v2d, unit, V2D_GRID_CLAMP,
1042                                    V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
1043         UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
1044         UI_view2d_grid_free(grid);
1045
1046         /* data... */
1047         clip_draw_dopesheet_main(sc, ar, scene);
1048
1049         /* current frame indicator line */
1050         if (sc->flag & SC_SHOW_SECONDS) cfra_flag |= DRAWCFRA_UNIT_SECONDS;
1051         ANIM_draw_cfra(C, v2d, cfra_flag);
1052
1053         /* reset view matrix */
1054         UI_view2d_view_restore(C);
1055
1056         /* scrollers */
1057         scrollers = UI_view2d_scrollers_calc(C, v2d, NULL, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1058         UI_view2d_scrollers_draw(C, v2d, scrollers);
1059         UI_view2d_scrollers_free(scrollers);
1060
1061         /* current frame number indicator */
1062         UI_view2d_view_orthoSpecial(ar, v2d, 1);
1063         ANIM_draw_cfra_number(C, v2d, cfra_flag);
1064 }
1065
1066 static void clip_preview_region_draw(const bContext *C, ARegion *ar)
1067 {
1068         SpaceClip *sc = CTX_wm_space_clip(C);
1069
1070         if (sc->view == SC_VIEW_GRAPH)
1071                 graph_region_draw(C, ar);
1072         else if (sc->view == SC_VIEW_DOPESHEET)
1073                 dopesheet_region_draw(C, ar);
1074 }
1075
1076 static void clip_preview_region_listener(
1077         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *UNUSED(ar),
1078         wmNotifier *UNUSED(wmn), const Scene *UNUSED(scene))
1079 {
1080 }
1081
1082 /****************** channels region ******************/
1083
1084 static void clip_channels_region_init(wmWindowManager *wm, ARegion *ar)
1085 {
1086         wmKeyMap *keymap;
1087
1088         /* ensure the 2d view sync works - main region has bottom scroller */
1089         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
1090
1091         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
1092
1093         keymap = WM_keymap_ensure(wm->defaultconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
1094         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1095 }
1096
1097 static void clip_channels_region_draw(const bContext *C, ARegion *ar)
1098 {
1099         SpaceClip *sc = CTX_wm_space_clip(C);
1100         MovieClip *clip = ED_space_clip_get_clip(sc);
1101         View2D *v2d = &ar->v2d;
1102
1103         if (clip)
1104                 BKE_tracking_dopesheet_update(&clip->tracking);
1105
1106         /* clear and setup matrix */
1107         UI_ThemeClearColor(TH_BACK);
1108         GPU_clear(GPU_COLOR_BIT);
1109
1110         UI_view2d_view_ortho(v2d);
1111
1112         /* data... */
1113         clip_draw_dopesheet_channels(C, ar);
1114
1115         /* reset view matrix */
1116         UI_view2d_view_restore(C);
1117 }
1118
1119 static void clip_channels_region_listener(
1120         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *UNUSED(ar),
1121         wmNotifier *UNUSED(wmn), const Scene *UNUSED(scene))
1122 {
1123 }
1124
1125 /****************** header region ******************/
1126
1127 /* add handlers, stuff you only do once or on area/region changes */
1128 static void clip_header_region_init(wmWindowManager *UNUSED(wm), ARegion *ar)
1129 {
1130         ED_region_header_init(ar);
1131 }
1132
1133 static void clip_header_region_draw(const bContext *C, ARegion *ar)
1134 {
1135         ED_region_header(C, ar);
1136 }
1137
1138 static void clip_header_region_listener(
1139         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
1140         wmNotifier *wmn, const Scene *UNUSED(scene))
1141 {
1142         /* context changes */
1143         switch (wmn->category) {
1144                 case NC_SCENE:
1145                         switch (wmn->data) {
1146                                 /* for proportional editmode only */
1147                                 case ND_TOOLSETTINGS:
1148                                         /* TODO - should do this when in mask mode only but no datas available */
1149                                         // if (sc->mode == SC_MODE_MASKEDIT)
1150                                 {
1151                                         ED_region_tag_redraw(ar);
1152                                         break;
1153                                 }
1154                         }
1155                         break;
1156         }
1157 }
1158
1159
1160 /****************** tools region ******************/
1161
1162 /* add handlers, stuff you only do once or on area/region changes */
1163 static void clip_tools_region_init(wmWindowManager *wm, ARegion *ar)
1164 {
1165         wmKeyMap *keymap;
1166
1167         ED_region_panels_init(wm, ar);
1168
1169         keymap = WM_keymap_ensure(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1170         WM_event_add_keymap_handler(&ar->handlers, keymap);
1171 }
1172
1173 static void clip_tools_region_draw(const bContext *C, ARegion *ar)
1174 {
1175         ED_region_panels(C, ar);
1176 }
1177
1178 /****************** tool properties region ******************/
1179
1180 static void clip_props_region_listener(
1181         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
1182         wmNotifier *wmn, const Scene *UNUSED(scene))
1183 {
1184         /* context changes */
1185         switch (wmn->category) {
1186                 case NC_WM:
1187                         if (wmn->data == ND_HISTORY)
1188                                 ED_region_tag_redraw(ar);
1189                         break;
1190                 case NC_SCENE:
1191                         if (wmn->data == ND_MODE)
1192                                 ED_region_tag_redraw(ar);
1193                         break;
1194                 case NC_SPACE:
1195                         if (wmn->data == ND_SPACE_CLIP)
1196                                 ED_region_tag_redraw(ar);
1197                         break;
1198                 case NC_GPENCIL:
1199                         if (wmn->action == NA_EDITED)
1200                                 ED_region_tag_redraw(ar);
1201                         break;
1202         }
1203 }
1204
1205 /****************** properties region ******************/
1206
1207 /* add handlers, stuff you only do once or on area/region changes */
1208 static void clip_properties_region_init(wmWindowManager *wm, ARegion *ar)
1209 {
1210         wmKeyMap *keymap;
1211
1212         ED_region_panels_init(wm, ar);
1213
1214         keymap = WM_keymap_ensure(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1215         WM_event_add_keymap_handler(&ar->handlers, keymap);
1216 }
1217
1218 static void clip_properties_region_draw(const bContext *C, ARegion *ar)
1219 {
1220         SpaceClip *sc = CTX_wm_space_clip(C);
1221
1222         BKE_movieclip_update_scopes(sc->clip, &sc->user, &sc->scopes);
1223
1224         ED_region_panels(C, ar);
1225 }
1226
1227 static void clip_properties_region_listener(
1228         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
1229         wmNotifier *wmn, const Scene *UNUSED(scene))
1230 {
1231         /* context changes */
1232         switch (wmn->category) {
1233                 case NC_GPENCIL:
1234                         if (ELEM(wmn->data, ND_DATA, ND_GPENCIL_EDITMODE))
1235                                 ED_region_tag_redraw(ar);
1236                         break;
1237                 case NC_BRUSH:
1238                         if (wmn->action == NA_EDITED)
1239                                 ED_region_tag_redraw(ar);
1240                         break;
1241         }
1242 }
1243
1244 /********************* registration ********************/
1245
1246 static void clip_id_remap(ScrArea *UNUSED(sa), SpaceLink *slink, ID *old_id, ID *new_id)
1247 {
1248         SpaceClip *sclip = (SpaceClip *)slink;
1249
1250         if (!ELEM(GS(old_id->name), ID_MC, ID_MSK)) {
1251                 return;
1252         }
1253
1254         if ((ID *)sclip->clip == old_id) {
1255                 sclip->clip = (MovieClip *)new_id;
1256                 id_us_ensure_real(new_id);
1257         }
1258
1259         if ((ID *)sclip->mask_info.mask == old_id) {
1260                 sclip->mask_info.mask = (Mask *)new_id;
1261                 id_us_ensure_real(new_id);
1262         }
1263 }
1264
1265 /* only called once, from space/spacetypes.c */
1266 void ED_spacetype_clip(void)
1267 {
1268         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype clip");
1269         ARegionType *art;
1270
1271         st->spaceid = SPACE_CLIP;
1272         strncpy(st->name, "Clip", BKE_ST_MAXNAME);
1273
1274         st->new = clip_new;
1275         st->free = clip_free;
1276         st->init = clip_init;
1277         st->duplicate = clip_duplicate;
1278         st->operatortypes = clip_operatortypes;
1279         st->keymap = clip_keymap;
1280         st->listener = clip_listener;
1281         st->context = clip_context;
1282         st->dropboxes = clip_dropboxes;
1283         st->refresh = clip_refresh;
1284         st->id_remap = clip_id_remap;
1285
1286         /* regions: main window */
1287         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region");
1288         art->regionid = RGN_TYPE_WINDOW;
1289         art->init = clip_main_region_init;
1290         art->draw = clip_main_region_draw;
1291         art->listener = clip_main_region_listener;
1292         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_GPENCIL;
1293
1294         BLI_addhead(&st->regiontypes, art);
1295
1296         /* preview */
1297         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region preview");
1298         art->regionid = RGN_TYPE_PREVIEW;
1299         art->prefsizey = 240;
1300         art->init = clip_preview_region_init;
1301         art->draw = clip_preview_region_draw;
1302         art->listener = clip_preview_region_listener;
1303         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
1304
1305         BLI_addhead(&st->regiontypes, art);
1306
1307         /* regions: properties */
1308         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region properties");
1309         art->regionid = RGN_TYPE_UI;
1310         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1311         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1312         art->init = clip_properties_region_init;
1313         art->draw = clip_properties_region_draw;
1314         art->listener = clip_properties_region_listener;
1315         BLI_addhead(&st->regiontypes, art);
1316         ED_clip_buttons_register(art);
1317
1318         /* regions: tools */
1319         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region tools");
1320         art->regionid = RGN_TYPE_TOOLS;
1321         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1322         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1323         art->listener = clip_props_region_listener;
1324         art->init = clip_tools_region_init;
1325         art->draw = clip_tools_region_draw;
1326
1327         BLI_addhead(&st->regiontypes, art);
1328
1329         /* regions: header */
1330         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region");
1331         art->regionid = RGN_TYPE_HEADER;
1332         art->prefsizey = HEADERY;
1333         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_HEADER;
1334
1335         art->init = clip_header_region_init;
1336         art->draw = clip_header_region_draw;
1337         art->listener = clip_header_region_listener;
1338
1339         BLI_addhead(&st->regiontypes, art);
1340
1341         BKE_spacetype_register(st);
1342
1343         /* channels */
1344         art = MEM_callocN(sizeof(ARegionType), "spacetype clip channels region");
1345         art->regionid = RGN_TYPE_CHANNELS;
1346         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1347         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1348         art->listener = clip_channels_region_listener;
1349         art->init = clip_channels_region_init;
1350         art->draw = clip_channels_region_draw;
1351
1352         BLI_addhead(&st->regiontypes, art);
1353
1354         /* regions: hud */
1355         art = ED_area_type_hud(st->spaceid);
1356         BLI_addhead(&st->regiontypes, art);
1357 }