Merge branch 'master' into 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         /* callback */
892         ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
893
894         clip_draw_main(C, sc, ar);
895
896         /* TODO(sergey): would be nice to find a way to de-duplicate all this space conversions */
897         UI_view2d_view_to_region_fl(&ar->v2d, 0.0f, 0.0f, &x, &y);
898         ED_space_clip_get_size(sc, &width, &height);
899         ED_space_clip_get_zoom(sc, ar, &zoomx, &zoomy);
900         ED_space_clip_get_aspect(sc, &aspx, &aspy);
901
902         if (sc->mode == SC_MODE_MASKEDIT) {
903                 Mask *mask = CTX_data_edit_mask(C);
904                 if (mask && clip) {
905                         ScrArea *sa = CTX_wm_area(C);
906                         int mask_width, mask_height;
907                         ED_mask_get_size(sa, &mask_width, &mask_height);
908                         ED_mask_draw_region(mask, ar,
909                                             sc->mask_info.draw_flag,
910                                             sc->mask_info.draw_type,
911                                             sc->mask_info.overlay_mode,
912                                             mask_width, mask_height,
913                                             aspx, aspy,
914                                             true, true,
915                                             sc->stabmat, C);
916                 }
917         }
918
919         show_cursor |= sc->mode == SC_MODE_MASKEDIT;
920         show_cursor |= sc->around == V3D_AROUND_CURSOR;
921
922         if (show_cursor) {
923                 GPU_matrix_push();
924                 GPU_matrix_translate_2f(x, y);
925                 GPU_matrix_scale_2f(zoomx, zoomy);
926                 GPU_matrix_mul(sc->stabmat);
927                 GPU_matrix_scale_2f(width, height);
928                 ED_image_draw_cursor(ar, sc->cursor);
929                 GPU_matrix_pop();
930         }
931
932         clip_draw_cache_and_notes(C, sc, ar);
933
934         if (sc->flag & SC_SHOW_ANNOTATION) {
935                 /* Grease Pencil */
936                 clip_draw_grease_pencil((bContext *)C, true);
937         }
938
939         /* callback */
940         ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
941
942         /* reset view matrix */
943         UI_view2d_view_restore(C);
944
945         if (sc->flag & SC_SHOW_ANNOTATION) {
946                 /* draw Grease Pencil - screen space only */
947                 clip_draw_grease_pencil((bContext *)C, false);
948         }
949 }
950
951 static void clip_main_region_listener(
952         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
953         wmNotifier *wmn, const Scene *UNUSED(scene))
954 {
955         /* context changes */
956         switch (wmn->category) {
957                 case NC_GPENCIL:
958                         if (wmn->action == NA_EDITED)
959                                 ED_region_tag_redraw(ar);
960                         else if (wmn->data & ND_GPENCIL_EDITMODE)
961                                 ED_region_tag_redraw(ar);
962                         break;
963         }
964 }
965
966 /****************** preview region ******************/
967
968 static void clip_preview_region_init(wmWindowManager *wm, ARegion *ar)
969 {
970         wmKeyMap *keymap;
971
972         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
973
974         /* own keymap */
975         keymap = WM_keymap_ensure(wm->defaultconf, "Clip", SPACE_CLIP, 0);
976         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
977
978         keymap = WM_keymap_ensure(wm->defaultconf, "Clip Graph Editor", SPACE_CLIP, 0);
979         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
980
981         keymap = WM_keymap_ensure(wm->defaultconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
982         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
983 }
984
985 static void graph_region_draw(const bContext *C, ARegion *ar)
986 {
987         View2D *v2d = &ar->v2d;
988         View2DScrollers *scrollers;
989         SpaceClip *sc = CTX_wm_space_clip(C);
990         Scene *scene = CTX_data_scene(C);
991         short unitx, unity;
992         short cfra_flag = 0;
993
994         if (sc->flag & SC_LOCK_TIMECURSOR)
995                 ED_clip_graph_center_current_frame(scene, ar);
996
997         /* clear and setup matrix */
998         UI_ThemeClearColor(TH_BACK);
999         GPU_clear(GPU_COLOR_BIT);
1000
1001         UI_view2d_view_ortho(v2d);
1002
1003         /* data... */
1004         clip_draw_graph(sc, ar, scene);
1005
1006         /* current frame indicator line */
1007         if (sc->flag & SC_SHOW_SECONDS) cfra_flag |= DRAWCFRA_UNIT_SECONDS;
1008         ANIM_draw_cfra(C, v2d, cfra_flag);
1009
1010         /* reset view matrix */
1011         UI_view2d_view_restore(C);
1012
1013         /* scrollers */
1014         unitx = (sc->flag & SC_SHOW_SECONDS) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
1015         unity = V2D_UNIT_VALUES;
1016         scrollers = UI_view2d_scrollers_calc(C, v2d, NULL, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
1017         UI_view2d_scrollers_draw(C, v2d, scrollers);
1018         UI_view2d_scrollers_free(scrollers);
1019
1020         /* current frame indicator */
1021         if (sc->flag & SC_SHOW_SECONDS) cfra_flag |= DRAWCFRA_UNIT_SECONDS;
1022         UI_view2d_view_orthoSpecial(ar, v2d, 1);
1023         ANIM_draw_cfra_number(C, v2d, cfra_flag);
1024 }
1025
1026 static void dopesheet_region_draw(const bContext *C, ARegion *ar)
1027 {
1028         Scene *scene = CTX_data_scene(C);
1029         SpaceClip *sc = CTX_wm_space_clip(C);
1030         MovieClip *clip = ED_space_clip_get_clip(sc);
1031         View2D *v2d = &ar->v2d;
1032         View2DGrid *grid;
1033         View2DScrollers *scrollers;
1034         short unit = 0, cfra_flag = 0;
1035
1036         if (clip)
1037                 BKE_tracking_dopesheet_update(&clip->tracking);
1038
1039         /* clear and setup matrix */
1040         UI_ThemeClearColor(TH_BACK);
1041         GPU_clear(GPU_COLOR_BIT);
1042
1043         UI_view2d_view_ortho(v2d);
1044
1045         /* time grid */
1046         unit = (sc->flag & SC_SHOW_SECONDS) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
1047         grid = UI_view2d_grid_calc(scene, v2d, unit, V2D_GRID_CLAMP,
1048                                    V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
1049         UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
1050         UI_view2d_grid_free(grid);
1051
1052         /* data... */
1053         clip_draw_dopesheet_main(sc, ar, scene);
1054
1055         /* current frame indicator line */
1056         if (sc->flag & SC_SHOW_SECONDS) cfra_flag |= DRAWCFRA_UNIT_SECONDS;
1057         ANIM_draw_cfra(C, v2d, cfra_flag);
1058
1059         /* reset view matrix */
1060         UI_view2d_view_restore(C);
1061
1062         /* scrollers */
1063         scrollers = UI_view2d_scrollers_calc(C, v2d, NULL, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1064         UI_view2d_scrollers_draw(C, v2d, scrollers);
1065         UI_view2d_scrollers_free(scrollers);
1066
1067         /* current frame number indicator */
1068         UI_view2d_view_orthoSpecial(ar, v2d, 1);
1069         ANIM_draw_cfra_number(C, v2d, cfra_flag);
1070 }
1071
1072 static void clip_preview_region_draw(const bContext *C, ARegion *ar)
1073 {
1074         SpaceClip *sc = CTX_wm_space_clip(C);
1075
1076         if (sc->view == SC_VIEW_GRAPH)
1077                 graph_region_draw(C, ar);
1078         else if (sc->view == SC_VIEW_DOPESHEET)
1079                 dopesheet_region_draw(C, ar);
1080 }
1081
1082 static void clip_preview_region_listener(
1083         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *UNUSED(ar),
1084         wmNotifier *UNUSED(wmn), const Scene *UNUSED(scene))
1085 {
1086 }
1087
1088 /****************** channels region ******************/
1089
1090 static void clip_channels_region_init(wmWindowManager *wm, ARegion *ar)
1091 {
1092         wmKeyMap *keymap;
1093
1094         /* ensure the 2d view sync works - main region has bottom scroller */
1095         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
1096
1097         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
1098
1099         keymap = WM_keymap_ensure(wm->defaultconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
1100         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1101 }
1102
1103 static void clip_channels_region_draw(const bContext *C, ARegion *ar)
1104 {
1105         SpaceClip *sc = CTX_wm_space_clip(C);
1106         MovieClip *clip = ED_space_clip_get_clip(sc);
1107         View2D *v2d = &ar->v2d;
1108
1109         if (clip)
1110                 BKE_tracking_dopesheet_update(&clip->tracking);
1111
1112         /* clear and setup matrix */
1113         UI_ThemeClearColor(TH_BACK);
1114         GPU_clear(GPU_COLOR_BIT);
1115
1116         UI_view2d_view_ortho(v2d);
1117
1118         /* data... */
1119         clip_draw_dopesheet_channels(C, ar);
1120
1121         /* reset view matrix */
1122         UI_view2d_view_restore(C);
1123 }
1124
1125 static void clip_channels_region_listener(
1126         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *UNUSED(ar),
1127         wmNotifier *UNUSED(wmn), const Scene *UNUSED(scene))
1128 {
1129 }
1130
1131 /****************** header region ******************/
1132
1133 /* add handlers, stuff you only do once or on area/region changes */
1134 static void clip_header_region_init(wmWindowManager *UNUSED(wm), ARegion *ar)
1135 {
1136         ED_region_header_init(ar);
1137 }
1138
1139 static void clip_header_region_draw(const bContext *C, ARegion *ar)
1140 {
1141         ED_region_header(C, ar);
1142 }
1143
1144 static void clip_header_region_listener(
1145         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
1146         wmNotifier *wmn, const Scene *UNUSED(scene))
1147 {
1148         /* context changes */
1149         switch (wmn->category) {
1150                 case NC_SCENE:
1151                         switch (wmn->data) {
1152                                 /* for proportional editmode only */
1153                                 case ND_TOOLSETTINGS:
1154                                         /* TODO - should do this when in mask mode only but no datas available */
1155                                         // if (sc->mode == SC_MODE_MASKEDIT)
1156                                 {
1157                                         ED_region_tag_redraw(ar);
1158                                         break;
1159                                 }
1160                         }
1161                         break;
1162         }
1163 }
1164
1165
1166 /****************** tools region ******************/
1167
1168 /* add handlers, stuff you only do once or on area/region changes */
1169 static void clip_tools_region_init(wmWindowManager *wm, ARegion *ar)
1170 {
1171         wmKeyMap *keymap;
1172
1173         ED_region_panels_init(wm, ar);
1174
1175         keymap = WM_keymap_ensure(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1176         WM_event_add_keymap_handler(&ar->handlers, keymap);
1177 }
1178
1179 static void clip_tools_region_draw(const bContext *C, ARegion *ar)
1180 {
1181         ED_region_panels(C, ar);
1182 }
1183
1184 /****************** tool properties region ******************/
1185
1186 static void clip_props_region_listener(
1187         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
1188         wmNotifier *wmn, const Scene *UNUSED(scene))
1189 {
1190         /* context changes */
1191         switch (wmn->category) {
1192                 case NC_WM:
1193                         if (wmn->data == ND_HISTORY)
1194                                 ED_region_tag_redraw(ar);
1195                         break;
1196                 case NC_SCENE:
1197                         if (wmn->data == ND_MODE)
1198                                 ED_region_tag_redraw(ar);
1199                         break;
1200                 case NC_SPACE:
1201                         if (wmn->data == ND_SPACE_CLIP)
1202                                 ED_region_tag_redraw(ar);
1203                         break;
1204                 case NC_GPENCIL:
1205                         if (wmn->action == NA_EDITED)
1206                                 ED_region_tag_redraw(ar);
1207                         break;
1208         }
1209 }
1210
1211 /****************** properties region ******************/
1212
1213 /* add handlers, stuff you only do once or on area/region changes */
1214 static void clip_properties_region_init(wmWindowManager *wm, ARegion *ar)
1215 {
1216         wmKeyMap *keymap;
1217
1218         ED_region_panels_init(wm, ar);
1219
1220         keymap = WM_keymap_ensure(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1221         WM_event_add_keymap_handler(&ar->handlers, keymap);
1222 }
1223
1224 static void clip_properties_region_draw(const bContext *C, ARegion *ar)
1225 {
1226         SpaceClip *sc = CTX_wm_space_clip(C);
1227
1228         BKE_movieclip_update_scopes(sc->clip, &sc->user, &sc->scopes);
1229
1230         ED_region_panels(C, ar);
1231 }
1232
1233 static void clip_properties_region_listener(
1234         wmWindow *UNUSED(win), ScrArea *UNUSED(sa), ARegion *ar,
1235         wmNotifier *wmn, const Scene *UNUSED(scene))
1236 {
1237         /* context changes */
1238         switch (wmn->category) {
1239                 case NC_GPENCIL:
1240                         if (ELEM(wmn->data, ND_DATA, ND_GPENCIL_EDITMODE))
1241                                 ED_region_tag_redraw(ar);
1242                         break;
1243                 case NC_BRUSH:
1244                         if (wmn->action == NA_EDITED)
1245                                 ED_region_tag_redraw(ar);
1246                         break;
1247         }
1248 }
1249
1250 /********************* registration ********************/
1251
1252 static void clip_id_remap(ScrArea *UNUSED(sa), SpaceLink *slink, ID *old_id, ID *new_id)
1253 {
1254         SpaceClip *sclip = (SpaceClip *)slink;
1255
1256         if (!ELEM(GS(old_id->name), ID_MC, ID_MSK)) {
1257                 return;
1258         }
1259
1260         if ((ID *)sclip->clip == old_id) {
1261                 sclip->clip = (MovieClip *)new_id;
1262                 id_us_ensure_real(new_id);
1263         }
1264
1265         if ((ID *)sclip->mask_info.mask == old_id) {
1266                 sclip->mask_info.mask = (Mask *)new_id;
1267                 id_us_ensure_real(new_id);
1268         }
1269 }
1270
1271 /* only called once, from space/spacetypes.c */
1272 void ED_spacetype_clip(void)
1273 {
1274         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype clip");
1275         ARegionType *art;
1276
1277         st->spaceid = SPACE_CLIP;
1278         strncpy(st->name, "Clip", BKE_ST_MAXNAME);
1279
1280         st->new = clip_new;
1281         st->free = clip_free;
1282         st->init = clip_init;
1283         st->duplicate = clip_duplicate;
1284         st->operatortypes = clip_operatortypes;
1285         st->keymap = clip_keymap;
1286         st->listener = clip_listener;
1287         st->context = clip_context;
1288         st->dropboxes = clip_dropboxes;
1289         st->refresh = clip_refresh;
1290         st->id_remap = clip_id_remap;
1291
1292         /* regions: main window */
1293         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region");
1294         art->regionid = RGN_TYPE_WINDOW;
1295         art->init = clip_main_region_init;
1296         art->draw = clip_main_region_draw;
1297         art->listener = clip_main_region_listener;
1298         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_GPENCIL;
1299
1300         BLI_addhead(&st->regiontypes, art);
1301
1302         /* preview */
1303         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region preview");
1304         art->regionid = RGN_TYPE_PREVIEW;
1305         art->prefsizey = 240;
1306         art->init = clip_preview_region_init;
1307         art->draw = clip_preview_region_draw;
1308         art->listener = clip_preview_region_listener;
1309         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
1310
1311         BLI_addhead(&st->regiontypes, art);
1312
1313         /* regions: properties */
1314         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region properties");
1315         art->regionid = RGN_TYPE_UI;
1316         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1317         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1318         art->init = clip_properties_region_init;
1319         art->draw = clip_properties_region_draw;
1320         art->listener = clip_properties_region_listener;
1321         BLI_addhead(&st->regiontypes, art);
1322         ED_clip_buttons_register(art);
1323
1324         /* regions: tools */
1325         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region tools");
1326         art->regionid = RGN_TYPE_TOOLS;
1327         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1328         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1329         art->listener = clip_props_region_listener;
1330         art->init = clip_tools_region_init;
1331         art->draw = clip_tools_region_draw;
1332
1333         BLI_addhead(&st->regiontypes, art);
1334
1335         /* regions: header */
1336         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region");
1337         art->regionid = RGN_TYPE_HEADER;
1338         art->prefsizey = HEADERY;
1339         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_HEADER;
1340
1341         art->init = clip_header_region_init;
1342         art->draw = clip_header_region_draw;
1343         art->listener = clip_header_region_listener;
1344
1345         BLI_addhead(&st->regiontypes, art);
1346
1347         BKE_spacetype_register(st);
1348
1349         /* channels */
1350         art = MEM_callocN(sizeof(ARegionType), "spacetype clip channels region");
1351         art->regionid = RGN_TYPE_CHANNELS;
1352         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1353         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1354         art->listener = clip_channels_region_listener;
1355         art->init = clip_channels_region_init;
1356         art->draw = clip_channels_region_draw;
1357
1358         BLI_addhead(&st->regiontypes, art);
1359
1360         /* regions: hud */
1361         art = ED_area_type_hud(st->spaceid);
1362         BLI_addhead(&st->regiontypes, art);
1363 }