bb06104d4422b3928ba7742295ae0747032a071c
[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_main.h"
47 #include "BKE_context.h"
48 #include "BKE_screen.h"
49 #include "BKE_movieclip.h"
50 #include "BKE_tracking.h"
51
52 #include "IMB_imbuf_types.h"
53
54 #include "ED_mask.h"
55 #include "ED_space_api.h"
56 #include "ED_screen.h"
57 #include "ED_clip.h"
58 #include "ED_transform.h"
59
60 #include "IMB_imbuf.h"
61
62 #include "BIF_gl.h"
63
64 #include "WM_api.h"
65 #include "WM_types.h"
66
67 #include "UI_interface.h"
68 #include "UI_resources.h"
69 #include "UI_view2d.h"
70
71 #include "RNA_access.h"
72
73
74 #include "clip_intern.h"  /* own include */
75
76 static void init_preview_region(const bContext *C, ARegion *ar)
77 {
78         Scene *scene = CTX_data_scene(C);
79         ScrArea *sa = CTX_wm_area(C);
80         SpaceClip *sc = CTX_wm_space_clip(C);
81
82         ar->regiontype = RGN_TYPE_PREVIEW;
83         ar->alignment = RGN_ALIGN_TOP;
84         ar->flag |= RGN_FLAG_HIDDEN;
85
86         if (sc->view == SC_VIEW_DOPESHEET) {
87                 ar->v2d.tot.xmin = -10.0f;
88                 ar->v2d.tot.ymin = (float)(-sa->winy) / 3.0f;
89                 ar->v2d.tot.xmax = (float)(sa->winx);
90                 ar->v2d.tot.ymax = 0.0f;
91
92                 ar->v2d.cur = ar->v2d.tot;
93
94                 ar->v2d.min[0] = 0.0f;
95                 ar->v2d.min[1] = 0.0f;
96
97                 ar->v2d.max[0] = MAXFRAMEF;
98                 ar->v2d.max[1] = FLT_MAX;
99
100                 ar->v2d.minzoom = 0.01f;
101                 ar->v2d.maxzoom = 50;
102                 ar->v2d.scroll = (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
103                 ar->v2d.scroll |= (V2D_SCROLL_RIGHT);
104                 ar->v2d.keepzoom = V2D_LOCKZOOM_Y;
105                 ar->v2d.keepofs = V2D_KEEPOFS_Y;
106                 ar->v2d.align = V2D_ALIGN_NO_POS_Y;
107                 ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
108         }
109         else {
110                 ar->v2d.tot.xmin = 0.0f;
111                 ar->v2d.tot.ymin = -10.0f;
112                 ar->v2d.tot.xmax = (float)scene->r.efra;
113                 ar->v2d.tot.ymax = 10.0f;
114
115                 ar->v2d.cur = ar->v2d.tot;
116
117                 ar->v2d.min[0] = FLT_MIN;
118                 ar->v2d.min[1] = FLT_MIN;
119
120                 ar->v2d.max[0] = MAXFRAMEF;
121                 ar->v2d.max[1] = FLT_MAX;
122
123                 ar->v2d.scroll = (V2D_SCROLL_BOTTOM | V2D_SCROLL_SCALE_HORIZONTAL);
124                 ar->v2d.scroll |= (V2D_SCROLL_LEFT | V2D_SCROLL_SCALE_VERTICAL);
125
126                 ar->v2d.minzoom = 0.0f;
127                 ar->v2d.maxzoom = 0.0f;
128                 ar->v2d.keepzoom = 0;
129                 ar->v2d.keepofs = 0;
130                 ar->v2d.align = 0;
131                 ar->v2d.flag = 0;
132
133                 ar->v2d.keeptot = 0;
134         }
135 }
136
137 static void reinit_preview_region(const bContext *C, ARegion *ar)
138 {
139         SpaceClip *sc = CTX_wm_space_clip(C);
140
141         if (sc->view == SC_VIEW_DOPESHEET) {
142                 if ((ar->v2d.flag & V2D_VIEWSYNC_AREA_VERTICAL) == 0)
143                         init_preview_region(C, ar);
144         }
145         else {
146                 if (ar->v2d.flag & V2D_VIEWSYNC_AREA_VERTICAL)
147                         init_preview_region(C, ar);
148         }
149 }
150
151 static ARegion *ED_clip_has_preview_region(const bContext *C, ScrArea *sa)
152 {
153         ARegion *ar, *arnew;
154
155         ar = BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
156         if (ar)
157                 return ar;
158
159         /* add subdiv level; after header */
160         ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
161
162         /* is error! */
163         if (ar == NULL)
164                 return NULL;
165
166         arnew = MEM_callocN(sizeof(ARegion), "clip preview region");
167
168         BLI_insertlinkbefore(&sa->regionbase, ar, arnew);
169         init_preview_region(C, arnew);
170
171         return arnew;
172 }
173
174 static ARegion *ED_clip_has_channels_region(ScrArea *sa)
175 {
176         ARegion *ar, *arnew;
177
178         ar = BKE_area_find_region_type(sa, RGN_TYPE_CHANNELS);
179         if (ar)
180                 return ar;
181
182         /* add subdiv level; after header */
183         ar = BKE_area_find_region_type(sa, RGN_TYPE_PREVIEW);
184
185         /* is error! */
186         if (ar == NULL)
187                 return NULL;
188
189         arnew = MEM_callocN(sizeof(ARegion), "clip channels region");
190
191         BLI_insertlinkbefore(&sa->regionbase, ar, arnew);
192         arnew->regiontype = RGN_TYPE_CHANNELS;
193         arnew->alignment = RGN_ALIGN_LEFT;
194
195         arnew->v2d.scroll = V2D_SCROLL_BOTTOM;
196         arnew->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
197
198         return arnew;
199 }
200
201 static void clip_scopes_tag_refresh(ScrArea *sa)
202 {
203         SpaceClip *sc = (SpaceClip *)sa->spacedata.first;
204         ARegion *ar;
205
206         if (sc->mode != SC_MODE_TRACKING)
207                 return;
208
209         /* only while proeprties are visible */
210         for (ar = sa->regionbase.first; ar; ar = ar->next) {
211                 if (ar->regiontype == RGN_TYPE_UI && ar->flag & RGN_FLAG_HIDDEN)
212                         return;
213         }
214
215         sc->scopes.ok = FALSE;
216 }
217
218 static void clip_scopes_check_gpencil_change(ScrArea *sa)
219 {
220         SpaceClip *sc = (SpaceClip *)sa->spacedata.first;
221
222         if (sc->gpencil_src == SC_GPENCIL_SRC_TRACK) {
223                 clip_scopes_tag_refresh(sa);
224         }
225 }
226
227 static void clip_stabilization_tag_refresh(ScrArea *sa)
228 {
229         SpaceClip *sc = (SpaceClip *) sa->spacedata.first;
230         MovieClip *clip = ED_space_clip_get_clip(sc);
231
232         if (clip) {
233                 MovieTrackingStabilization *stab = &clip->tracking.stabilization;
234
235                 stab->ok = FALSE;
236         }
237 }
238
239 /* ******************** default callbacks for clip space ***************** */
240
241 static SpaceLink *clip_new(const bContext *C)
242 {
243         ARegion *ar;
244         SpaceClip *sc;
245
246         sc = MEM_callocN(sizeof(SpaceClip), "initclip");
247         sc->spacetype = SPACE_CLIP;
248         sc->flag = SC_SHOW_MARKER_PATTERN | SC_SHOW_TRACK_PATH | SC_MANUAL_CALIBRATION |
249                    SC_SHOW_GRAPH_TRACKS | SC_SHOW_GRAPH_FRAMES;
250         sc->zoom = 1.0f;
251         sc->path_length = 20;
252         sc->scopes.track_preview_height = 120;
253         sc->around = V3D_LOCAL;
254
255         /* header */
256         ar = MEM_callocN(sizeof(ARegion), "header for clip");
257
258         BLI_addtail(&sc->regionbase, ar);
259         ar->regiontype = RGN_TYPE_HEADER;
260         ar->alignment = RGN_ALIGN_BOTTOM;
261
262         /* tools view */
263         ar = MEM_callocN(sizeof(ARegion), "tools for clip");
264
265         BLI_addtail(&sc->regionbase, ar);
266         ar->regiontype = RGN_TYPE_TOOLS;
267         ar->alignment = RGN_ALIGN_LEFT;
268
269         /* tool properties */
270         ar = MEM_callocN(sizeof(ARegion), "tool properties for clip");
271
272         BLI_addtail(&sc->regionbase, ar);
273         ar->regiontype = RGN_TYPE_TOOL_PROPS;
274         ar->alignment = RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV;
275
276         /* properties view */
277         ar = MEM_callocN(sizeof(ARegion), "properties for clip");
278
279         BLI_addtail(&sc->regionbase, ar);
280         ar->regiontype = RGN_TYPE_UI;
281         ar->alignment = RGN_ALIGN_RIGHT;
282
283         /* channels view */
284         ar = MEM_callocN(sizeof(ARegion), "channels for clip");
285
286         BLI_addtail(&sc->regionbase, ar);
287         ar->regiontype = RGN_TYPE_CHANNELS;
288         ar->alignment = RGN_ALIGN_LEFT;
289
290         ar->v2d.scroll = V2D_SCROLL_BOTTOM;
291         ar->v2d.flag = V2D_VIEWSYNC_AREA_VERTICAL;
292
293         /* preview view */
294         ar = MEM_callocN(sizeof(ARegion), "preview for clip");
295
296         BLI_addtail(&sc->regionbase, ar);
297         init_preview_region(C, ar);
298
299         /* main area */
300         ar = MEM_callocN(sizeof(ARegion), "main area for clip");
301
302         BLI_addtail(&sc->regionbase, ar);
303         ar->regiontype = RGN_TYPE_WINDOW;
304
305         return (SpaceLink *) sc;
306 }
307
308 /* not spacelink itself */
309 static void clip_free(SpaceLink *sl)
310 {
311         SpaceClip *sc = (SpaceClip *) sl;
312
313         sc->clip = NULL;
314
315         if (sc->scopes.track_preview)
316                 IMB_freeImBuf(sc->scopes.track_preview);
317
318         if (sc->scopes.track_search)
319                 IMB_freeImBuf(sc->scopes.track_search);
320
321         ED_space_clip_free_texture_buffer(sc);
322 }
323
324 /* spacetype; init callback */
325 static void clip_init(struct wmWindowManager *UNUSED(wm), ScrArea *sa)
326 {
327         ListBase *lb = WM_dropboxmap_find("Clip", SPACE_CLIP, 0);
328
329         /* add drop boxes */
330         WM_event_add_dropbox_handler(&sa->handlers, lb);
331 }
332
333 static SpaceLink *clip_duplicate(SpaceLink *sl)
334 {
335         SpaceClip *scn = MEM_dupallocN(sl);
336
337         /* clear or remove stuff from old */
338         scn->scopes.track_search = NULL;
339         scn->scopes.track_preview = NULL;
340         scn->scopes.ok = FALSE;
341         scn->draw_context = NULL;
342
343         return (SpaceLink *)scn;
344 }
345
346 static void clip_listener(ScrArea *sa, wmNotifier *wmn)
347 {
348         /* context changes */
349         switch (wmn->category) {
350                 case NC_SCENE:
351                         switch (wmn->data) {
352                                 case ND_FRAME:
353                                         clip_scopes_tag_refresh(sa);
354                                         /* no break! */
355
356                                 case ND_FRAME_RANGE:
357                                         ED_area_tag_redraw(sa);
358                                         break;
359                         }
360                         break;
361                 case NC_MOVIECLIP:
362                         switch (wmn->data) {
363                                 case ND_DISPLAY:
364                                 case ND_SELECT:
365                                         clip_scopes_tag_refresh(sa);
366                                         ED_area_tag_redraw(sa);
367                                         break;
368                         }
369                         switch (wmn->action) {
370                                 case NA_REMOVED:
371                                 case NA_EDITED:
372                                 case NA_EVALUATED:
373                                         clip_stabilization_tag_refresh(sa);
374                                         /* no break! */
375
376                                 case NA_SELECTED:
377                                         clip_scopes_tag_refresh(sa);
378                                         ED_area_tag_redraw(sa);
379                                         break;
380                         }
381                         break;
382                 case NC_MASK:
383                         switch (wmn->data) {
384                                 case ND_SELECT:
385                                 case ND_DATA:
386                                 case ND_DRAW:
387                                         ED_area_tag_redraw(sa);
388                                         break;
389                         }
390                         switch (wmn->action) {
391                                 case NA_SELECTED:
392                                         ED_area_tag_redraw(sa);
393                                         break;
394                                 case NA_EDITED:
395                                         ED_area_tag_redraw(sa);
396                                         break;
397                         }
398                         break;
399                 case NC_GEOM:
400                         switch (wmn->data) {
401                                 case ND_SELECT:
402                                         clip_scopes_tag_refresh(sa);
403                                         ED_area_tag_redraw(sa);
404                                         break;
405                         }
406                         break;
407                 case NC_SCREEN:
408                         switch (wmn->data) {
409                                 case ND_ANIMPLAY:
410                                 case ND_GPENCIL:
411                                         clip_scopes_check_gpencil_change(sa);
412                                         ED_area_tag_redraw(sa);
413                                         break;
414                         }
415                         break;
416                 case NC_SPACE:
417                         if (wmn->data == ND_SPACE_CLIP) {
418                                 clip_scopes_tag_refresh(sa);
419                                 clip_stabilization_tag_refresh(sa);
420                                 ED_area_tag_redraw(sa);
421                         }
422                         break;
423         }
424 }
425
426 static void clip_operatortypes(void)
427 {
428         /* ** clip_ops.c ** */
429         WM_operatortype_append(CLIP_OT_open);
430         WM_operatortype_append(CLIP_OT_reload);
431         WM_operatortype_append(CLIP_OT_view_pan);
432         WM_operatortype_append(CLIP_OT_view_zoom);
433         WM_operatortype_append(CLIP_OT_view_zoom_in);
434         WM_operatortype_append(CLIP_OT_view_zoom_out);
435         WM_operatortype_append(CLIP_OT_view_zoom_ratio);
436         WM_operatortype_append(CLIP_OT_view_all);
437         WM_operatortype_append(CLIP_OT_view_selected);
438         WM_operatortype_append(CLIP_OT_change_frame);
439         WM_operatortype_append(CLIP_OT_rebuild_proxy);
440         WM_operatortype_append(CLIP_OT_mode_set);
441         WM_operatortype_append(CLIP_OT_view_ndof);
442
443         /* ** clip_toolbar.c ** */
444         WM_operatortype_append(CLIP_OT_tools);
445         WM_operatortype_append(CLIP_OT_properties);
446
447         /* ** tracking_ops.c ** */
448
449         /* navigation */
450         WM_operatortype_append(CLIP_OT_frame_jump);
451
452         /* foorage */
453         WM_operatortype_append(CLIP_OT_set_center_principal);
454
455         /* selection */
456         WM_operatortype_append(CLIP_OT_select);
457         WM_operatortype_append(CLIP_OT_select_all);
458         WM_operatortype_append(CLIP_OT_select_border);
459         WM_operatortype_append(CLIP_OT_select_lasso);
460         WM_operatortype_append(CLIP_OT_select_circle);
461         WM_operatortype_append(CLIP_OT_select_grouped);
462
463         /* markers */
464         WM_operatortype_append(CLIP_OT_add_marker);
465         WM_operatortype_append(CLIP_OT_slide_marker);
466         WM_operatortype_append(CLIP_OT_delete_track);
467         WM_operatortype_append(CLIP_OT_delete_marker);
468
469         /* track */
470         WM_operatortype_append(CLIP_OT_track_markers);
471
472         /* solving */
473         WM_operatortype_append(CLIP_OT_solve_camera);
474         WM_operatortype_append(CLIP_OT_clear_solution);
475
476         WM_operatortype_append(CLIP_OT_disable_markers);
477         WM_operatortype_append(CLIP_OT_hide_tracks);
478         WM_operatortype_append(CLIP_OT_hide_tracks_clear);
479         WM_operatortype_append(CLIP_OT_lock_tracks);
480
481         WM_operatortype_append(CLIP_OT_set_solver_keyframe);
482
483         /* orientation */
484         WM_operatortype_append(CLIP_OT_set_origin);
485         WM_operatortype_append(CLIP_OT_set_plane);
486         WM_operatortype_append(CLIP_OT_set_axis);
487         WM_operatortype_append(CLIP_OT_set_scale);
488         WM_operatortype_append(CLIP_OT_set_solution_scale);
489
490         /* detect */
491         WM_operatortype_append(CLIP_OT_detect_features);
492
493         /* stabilization */
494         WM_operatortype_append(CLIP_OT_stabilize_2d_add);
495         WM_operatortype_append(CLIP_OT_stabilize_2d_remove);
496         WM_operatortype_append(CLIP_OT_stabilize_2d_select);
497         WM_operatortype_append(CLIP_OT_stabilize_2d_set_rotation);
498
499         /* clean-up */
500         WM_operatortype_append(CLIP_OT_clear_track_path);
501         WM_operatortype_append(CLIP_OT_join_tracks);
502         WM_operatortype_append(CLIP_OT_track_copy_color);
503
504         WM_operatortype_append(CLIP_OT_clean_tracks);
505
506         /* object tracking */
507         WM_operatortype_append(CLIP_OT_tracking_object_new);
508         WM_operatortype_append(CLIP_OT_tracking_object_remove);
509
510         /* clipboard */
511         WM_operatortype_append(CLIP_OT_copy_tracks);
512         WM_operatortype_append(CLIP_OT_paste_tracks);
513
514         /* ** clip_graph_ops.c  ** */
515
516         /* graph editing */
517
518         /* selection */
519         WM_operatortype_append(CLIP_OT_graph_select);
520         WM_operatortype_append(CLIP_OT_graph_select_border);
521         WM_operatortype_append(CLIP_OT_graph_select_all_markers);
522
523         WM_operatortype_append(CLIP_OT_graph_delete_curve);
524         WM_operatortype_append(CLIP_OT_graph_delete_knot);
525         WM_operatortype_append(CLIP_OT_graph_view_all);
526         WM_operatortype_append(CLIP_OT_graph_center_current_frame);
527
528         WM_operatortype_append(CLIP_OT_graph_disable_markers);
529
530         /* ** clip_dopesheet_ops.c  ** */
531
532         WM_operatortype_append(CLIP_OT_dopesheet_select_channel);
533 }
534
535 static void clip_keymap(struct wmKeyConfig *keyconf)
536 {
537         wmKeyMap *keymap;
538         wmKeyMapItem *kmi;
539
540         /* ******** Global hotkeys avalaible for all regions ******** */
541
542         keymap = WM_keymap_find(keyconf, "Clip", SPACE_CLIP, 0);
543
544         WM_keymap_add_item(keymap, "CLIP_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
545
546         WM_keymap_add_item(keymap, "CLIP_OT_tools", TKEY, KM_PRESS, 0, 0);
547         WM_keymap_add_item(keymap, "CLIP_OT_properties", NKEY, KM_PRESS, 0, 0);
548
549         /* 2d tracking */
550         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", LEFTARROWKEY, KM_PRESS, KM_ALT, 0);
551         RNA_boolean_set(kmi->ptr, "backwards", TRUE);
552         RNA_boolean_set(kmi->ptr, "sequence", FALSE);
553         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", RIGHTARROWKEY, KM_PRESS, KM_ALT, 0);
554         RNA_boolean_set(kmi->ptr, "backwards", FALSE);
555         RNA_boolean_set(kmi->ptr, "sequence", FALSE);
556         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_CTRL, 0);
557         RNA_boolean_set(kmi->ptr, "backwards", FALSE);
558         RNA_boolean_set(kmi->ptr, "sequence", TRUE);
559         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
560         RNA_boolean_set(kmi->ptr, "backwards", TRUE);
561         RNA_boolean_set(kmi->ptr, "sequence", TRUE);
562
563         /* mode */
564         WM_keymap_add_menu(keymap, "CLIP_MT_select_mode", TABKEY, KM_PRESS, 0, 0);
565
566         WM_keymap_add_item(keymap, "CLIP_OT_solve_camera", SKEY, KM_PRESS, KM_SHIFT, 0);
567
568         kmi = WM_keymap_add_item(keymap, "CLIP_OT_set_solver_keyframe", QKEY, KM_PRESS, 0, 0);
569         RNA_enum_set(kmi->ptr, "keyframe", 0);
570
571         kmi = WM_keymap_add_item(keymap, "CLIP_OT_set_solver_keyframe", EKEY, KM_PRESS, 0, 0);
572         RNA_enum_set(kmi->ptr, "keyframe", 1);
573
574         /* ******** Hotkeys avalaible for main region only ******** */
575
576         keymap = WM_keymap_find(keyconf, "Clip Editor", SPACE_CLIP, 0);
577 //      keymap->poll = ED_space_clip_tracking_poll;
578         /* ** View/navigation ** */
579
580         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
581         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
582         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MOUSEPAN, 0, 0, 0);
583
584         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
585         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
586         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
587         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
588         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
589         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
590
591         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
592         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
593         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
594         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
595         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
596         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
597         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
598
599         WM_keymap_add_item(keymap, "CLIP_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
600
601         kmi = WM_keymap_add_item(keymap, "CLIP_OT_view_all", FKEY, KM_PRESS, 0, 0);
602         RNA_boolean_set(kmi->ptr, "fit_view", TRUE);
603
604         WM_keymap_add_item(keymap, "CLIP_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
605
606         WM_keymap_add_item(keymap, "CLIP_OT_view_all", NDOF_BUTTON_FIT, KM_PRESS, 0, 0);
607         WM_keymap_add_item(keymap, "CLIP_OT_view_ndof", NDOF_MOTION, 0, 0, 0);
608
609         /* jump to special frame */
610         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
611         RNA_enum_set(kmi->ptr, "position", 0);
612
613         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
614         RNA_enum_set(kmi->ptr, "position", 1);
615
616         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
617         RNA_enum_set(kmi->ptr, "position", 2);
618
619         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
620         RNA_enum_set(kmi->ptr, "position", 3);
621
622         /* "timeline" */
623         WM_keymap_add_item(keymap, "CLIP_OT_change_frame", LEFTMOUSE, KM_PRESS, 0, 0);
624
625         /* selection */
626         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
627         RNA_boolean_set(kmi->ptr, "extend", FALSE);
628         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
629         RNA_boolean_set(kmi->ptr, "extend", TRUE);
630         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_all", AKEY, KM_PRESS, 0, 0);
631         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
632         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
633         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
634         WM_keymap_add_item(keymap, "CLIP_OT_select_border", BKEY, KM_PRESS, 0, 0);
635         WM_keymap_add_item(keymap, "CLIP_OT_select_circle", CKEY, KM_PRESS, 0, 0);
636         WM_keymap_add_menu(keymap, "CLIP_MT_select_grouped", GKEY, KM_PRESS, KM_SHIFT, 0);
637
638         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_ALT, 0);
639         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
640         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_SHIFT | KM_ALT, 0);
641         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
642
643         /* marker */
644         WM_keymap_add_item(keymap, "CLIP_OT_add_marker_slide", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
645
646         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", DELKEY, KM_PRESS, KM_SHIFT, 0);
647         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", XKEY, KM_PRESS, KM_SHIFT, 0);
648
649         WM_keymap_add_item(keymap, "CLIP_OT_slide_marker", LEFTMOUSE, KM_PRESS, 0, 0);
650
651         kmi = WM_keymap_add_item(keymap, "CLIP_OT_disable_markers", DKEY, KM_PRESS, KM_SHIFT, 0);
652         RNA_enum_set(kmi->ptr, "action", 2);    /* toggle */
653
654         /* tracks */
655         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", DELKEY, KM_PRESS, 0, 0);
656         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", XKEY, KM_PRESS, 0, 0);
657
658         kmi = WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_CTRL, 0);
659         RNA_enum_set(kmi->ptr, "action", 0);    /* lock */
660
661         kmi = WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_ALT, 0);
662         RNA_enum_set(kmi->ptr, "action", 1);    /* unlock */
663
664         kmi = WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, 0, 0);
665         RNA_boolean_set(kmi->ptr, "unselected", FALSE);
666
667         kmi = WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, KM_SHIFT, 0);
668         RNA_boolean_set(kmi->ptr, "unselected", TRUE);
669
670         WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks_clear", HKEY, KM_PRESS, KM_ALT, 0);
671
672         /* clean-up */
673         WM_keymap_add_item(keymap, "CLIP_OT_join_tracks", JKEY, KM_PRESS, KM_CTRL, 0);
674
675         /* menus */
676         WM_keymap_add_menu(keymap, "CLIP_MT_tracking_specials", WKEY, KM_PRESS, 0, 0);
677
678         /* display */
679         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
680         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_selection");
681
682         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", DKEY, KM_PRESS, KM_ALT, 0);
683         RNA_string_set(kmi->ptr, "data_path", "space_data.show_disabled");
684
685         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_ALT, 0);
686         RNA_string_set(kmi->ptr, "data_path", "space_data.show_marker_search");
687
688         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0);
689         RNA_string_set(kmi->ptr, "data_path", "space_data.use_mute_footage");
690
691         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
692
693         /* clean-up */
694         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT, 0);
695         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_REMAINED);
696         RNA_boolean_set(kmi->ptr, "clear_active", FALSE);
697         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_SHIFT, 0);
698         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_UPTO);
699         RNA_boolean_set(kmi->ptr, "clear_active", FALSE);
700         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
701         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_ALL);
702         RNA_boolean_set(kmi->ptr, "clear_active", FALSE);
703
704         /* ******** Hotkeys avalaible for preview region only ******** */
705
706         keymap = WM_keymap_find(keyconf, "Clip Graph Editor", SPACE_CLIP, 0);
707
708         /* "timeline" */
709         WM_keymap_add_item(keymap, "CLIP_OT_change_frame", ACTIONMOUSE, KM_PRESS, 0, 0);
710
711         /* selection */
712         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select", SELECTMOUSE, KM_PRESS, 0, 0);
713         RNA_boolean_set(kmi->ptr, "extend", FALSE);
714         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
715         RNA_boolean_set(kmi->ptr, "extend", TRUE);
716
717         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select_all_markers", AKEY, KM_PRESS, 0, 0);
718         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
719         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select_all_markers", IKEY, KM_PRESS, KM_CTRL, 0);
720         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
721
722         WM_keymap_add_item(keymap, "CLIP_OT_graph_select_border", BKEY, KM_PRESS, 0, 0);
723
724         /* delete */
725         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_curve", DELKEY, KM_PRESS, 0, 0);
726         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_curve", XKEY, KM_PRESS, 0, 0);
727
728         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_knot", DELKEY, KM_PRESS, KM_SHIFT, 0);
729         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_knot", XKEY, KM_PRESS, KM_SHIFT, 0);
730
731         /* view */
732         WM_keymap_add_item(keymap, "CLIP_OT_graph_view_all", HOMEKEY, KM_PRESS, 0, 0);
733         WM_keymap_add_item(keymap, "CLIP_OT_graph_center_current_frame", PADPERIOD, KM_PRESS, 0, 0);
734
735         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
736         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_time_cursor");
737
738         /* clean-up */
739         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT, 0);
740         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_REMAINED);
741         RNA_boolean_set(kmi->ptr, "clear_active", TRUE);
742         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_SHIFT, 0);
743         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_UPTO);
744         RNA_boolean_set(kmi->ptr, "clear_active", TRUE);
745         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
746         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_ALL);
747         RNA_boolean_set(kmi->ptr, "clear_active", TRUE);
748
749         /* tracks */
750         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_disable_markers", DKEY, KM_PRESS, KM_SHIFT, 0);
751         RNA_enum_set(kmi->ptr, "action", 2);    /* toggle */
752
753         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
754
755         /* ******** Hotkeys avalaible for channels region only ******** */
756
757         keymap = WM_keymap_find(keyconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
758
759         kmi = WM_keymap_add_item(keymap, "CLIP_OT_dopesheet_select_channel", ACTIONMOUSE, KM_PRESS, 0, 0);
760         RNA_boolean_set(kmi->ptr, "extend", TRUE);  /* toggle */
761 }
762
763 const char *clip_context_dir[] = {"edit_movieclip", "edit_mask", NULL};
764
765 static int clip_context(const bContext *C, const char *member, bContextDataResult *result)
766 {
767         SpaceClip *sc = CTX_wm_space_clip(C);
768
769         if (CTX_data_dir(member)) {
770                 CTX_data_dir_set(result, clip_context_dir);
771
772                 return TRUE;
773         }
774         else if (CTX_data_equals(member, "edit_movieclip")) {
775                 if (sc->clip)
776                         CTX_data_id_pointer_set(result, &sc->clip->id);
777                 return TRUE;
778         }
779         else if (CTX_data_equals(member, "edit_mask")) {
780                 if (sc->mask_info.mask)
781                         CTX_data_id_pointer_set(result, &sc->mask_info.mask->id);
782                 return TRUE;
783         }
784
785         return FALSE;
786 }
787
788 /* dropboxes */
789 static int clip_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
790 {
791         if (drag->type == WM_DRAG_PATH)
792                 if (ELEM3(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_BLANK)) /* rule might not work? */
793                         return TRUE;
794
795         return FALSE;
796 }
797
798 static void clip_drop_copy(wmDrag *drag, wmDropBox *drop)
799 {
800         PointerRNA itemptr;
801         char dir[FILE_MAX], file[FILE_MAX];
802
803         BLI_split_dirfile(drag->path, dir, file, sizeof(dir), sizeof(file));
804
805         RNA_string_set(drop->ptr, "directory", dir);
806
807         RNA_collection_clear(drop->ptr, "files");
808         RNA_collection_add(drop->ptr, "files", &itemptr);
809         RNA_string_set(&itemptr, "name", file);
810 }
811
812 /* area+region dropbox definition */
813 static void clip_dropboxes(void)
814 {
815         ListBase *lb = WM_dropboxmap_find("Clip", SPACE_CLIP, 0);
816
817         WM_dropbox_add(lb, "CLIP_OT_open", clip_drop_poll, clip_drop_copy);
818 }
819
820 static void clip_refresh(const bContext *C, ScrArea *sa)
821 {
822         wmWindowManager *wm = CTX_wm_manager(C);
823         wmWindow *window = CTX_wm_window(C);
824         Scene *scene = CTX_data_scene(C);
825         SpaceClip *sc = (SpaceClip *)sa->spacedata.first;
826         ARegion *ar_main = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
827         ARegion *ar_tools = BKE_area_find_region_type(sa, RGN_TYPE_TOOLS);
828         ARegion *ar_tool_props = BKE_area_find_region_type(sa, RGN_TYPE_TOOL_PROPS);
829         ARegion *ar_preview = ED_clip_has_preview_region(C, sa);
830         ARegion *ar_properties = ED_clip_has_properties_region(sa);
831         ARegion *ar_channels = ED_clip_has_channels_region(sa);
832         int main_visible = FALSE, preview_visible = FALSE, tools_visible = FALSE;
833         int tool_props_visible = FALSE, properties_visible = FALSE, channels_visible = FALSE;
834         int view_changed = FALSE;
835
836         switch (sc->view) {
837                 case SC_VIEW_CLIP:
838                         main_visible = TRUE;
839                         preview_visible = FALSE;
840                         tools_visible = TRUE;
841                         tool_props_visible = TRUE;
842                         properties_visible = TRUE;
843                         channels_visible = FALSE;
844                         break;
845                 case SC_VIEW_GRAPH:
846                         main_visible = FALSE;
847                         preview_visible = TRUE;
848                         tools_visible = FALSE;
849                         tool_props_visible = FALSE;
850                         properties_visible = FALSE;
851                         channels_visible = FALSE;
852
853                         reinit_preview_region(C, ar_preview);
854                         break;
855                 case SC_VIEW_DOPESHEET:
856                         main_visible = FALSE;
857                         preview_visible = TRUE;
858                         tools_visible = FALSE;
859                         tool_props_visible = FALSE;
860                         properties_visible = FALSE;
861                         channels_visible = TRUE;
862
863                         reinit_preview_region(C, ar_preview);
864                         break;
865         }
866
867         if (main_visible) {
868                 if (ar_main && (ar_main->flag & RGN_FLAG_HIDDEN)) {
869                         ar_main->flag &= ~RGN_FLAG_HIDDEN;
870                         ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
871                         view_changed = TRUE;
872                 }
873
874                 if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
875                         ar_main->alignment = RGN_ALIGN_NONE;
876                         view_changed = TRUE;
877                 }
878         }
879         else {
880                 if (ar_main && !(ar_main->flag & RGN_FLAG_HIDDEN)) {
881                         ar_main->flag |= RGN_FLAG_HIDDEN;
882                         ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
883                         WM_event_remove_handlers((bContext *)C, &ar_main->handlers);
884                         view_changed = TRUE;
885                 }
886                 if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
887                         ar_main->alignment = RGN_ALIGN_NONE;
888                         view_changed = TRUE;
889                 }
890         }
891
892         if (properties_visible) {
893                 if (ar_properties && (ar_properties->flag & RGN_FLAG_HIDDEN)) {
894                         ar_properties->flag &= ~RGN_FLAG_HIDDEN;
895                         ar_properties->v2d.flag &= ~V2D_IS_INITIALISED;
896                         view_changed = TRUE;
897                 }
898                 if (ar_properties && ar_properties->alignment != RGN_ALIGN_RIGHT) {
899                         ar_properties->alignment = RGN_ALIGN_RIGHT;
900                         view_changed = TRUE;
901                 }
902         }
903         else {
904                 if (ar_properties && !(ar_properties->flag & RGN_FLAG_HIDDEN)) {
905                         ar_properties->flag |= RGN_FLAG_HIDDEN;
906                         ar_properties->v2d.flag &= ~V2D_IS_INITIALISED;
907                         WM_event_remove_handlers((bContext *)C, &ar_properties->handlers);
908                         view_changed = TRUE;
909                 }
910                 if (ar_properties && ar_properties->alignment != RGN_ALIGN_NONE) {
911                         ar_properties->alignment = RGN_ALIGN_NONE;
912                         view_changed = TRUE;
913                 }
914         }
915
916         if (tools_visible) {
917                 if (ar_tools && (ar_tools->flag & RGN_FLAG_HIDDEN)) {
918                         ar_tools->flag &= ~RGN_FLAG_HIDDEN;
919                         ar_tools->v2d.flag &= ~V2D_IS_INITIALISED;
920                         view_changed = TRUE;
921                 }
922                 if (ar_tools && ar_tools->alignment != RGN_ALIGN_LEFT) {
923                         ar_tools->alignment = RGN_ALIGN_LEFT;
924                         view_changed = TRUE;
925                 }
926         }
927         else {
928                 if (ar_tools && !(ar_tools->flag & RGN_FLAG_HIDDEN)) {
929                         ar_tools->flag |= RGN_FLAG_HIDDEN;
930                         ar_tools->v2d.flag &= ~V2D_IS_INITIALISED;
931                         WM_event_remove_handlers((bContext *)C, &ar_tools->handlers);
932                         view_changed = TRUE;
933                 }
934                 if (ar_tools && ar_tools->alignment != RGN_ALIGN_NONE) {
935                         ar_tools->alignment = RGN_ALIGN_NONE;
936                         view_changed = TRUE;
937                 }
938         }
939
940         if (tool_props_visible) {
941                 if (ar_tool_props && (ar_tool_props->flag & RGN_FLAG_HIDDEN)) {
942                         ar_tool_props->flag &= ~RGN_FLAG_HIDDEN;
943                         ar_tool_props->v2d.flag &= ~V2D_IS_INITIALISED;
944                         view_changed = TRUE;
945                 }
946                 if (ar_tool_props && (ar_tool_props->alignment != (RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV))) {
947                         ar_tool_props->alignment = RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV;
948                         view_changed = TRUE;
949                 }
950         }
951         else {
952                 if (ar_tool_props && !(ar_tool_props->flag & RGN_FLAG_HIDDEN)) {
953                         ar_tool_props->flag |= RGN_FLAG_HIDDEN;
954                         ar_tool_props->v2d.flag &= ~V2D_IS_INITIALISED;
955                         WM_event_remove_handlers((bContext *)C, &ar_tool_props->handlers);
956                         view_changed = TRUE;
957                 }
958                 if (ar_tool_props && ar_tool_props->alignment != RGN_ALIGN_NONE) {
959                         ar_tool_props->alignment = RGN_ALIGN_NONE;
960                         view_changed = TRUE;
961                 }
962         }
963
964         if (preview_visible) {
965                 if (ar_preview && (ar_preview->flag & RGN_FLAG_HIDDEN)) {
966                         ar_preview->flag &= ~RGN_FLAG_HIDDEN;
967                         ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
968                         ar_preview->v2d.cur = ar_preview->v2d.tot;
969                         view_changed = TRUE;
970                 }
971                 if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
972                         ar_preview->alignment = RGN_ALIGN_NONE;
973                         view_changed = TRUE;
974                 }
975         }
976         else {
977                 if (ar_preview && !(ar_preview->flag & RGN_FLAG_HIDDEN)) {
978                         ar_preview->flag |= RGN_FLAG_HIDDEN;
979                         ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
980                         WM_event_remove_handlers((bContext *)C, &ar_preview->handlers);
981                         view_changed = TRUE;
982                 }
983                 if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
984                         ar_preview->alignment = RGN_ALIGN_NONE;
985                         view_changed = TRUE;
986                 }
987         }
988
989         if (channels_visible) {
990                 if (ar_channels && (ar_channels->flag & RGN_FLAG_HIDDEN)) {
991                         ar_channels->flag &= ~RGN_FLAG_HIDDEN;
992                         ar_channels->v2d.flag &= ~V2D_IS_INITIALISED;
993                         view_changed = TRUE;
994                 }
995                 if (ar_channels && ar_channels->alignment != RGN_ALIGN_LEFT) {
996                         ar_channels->alignment = RGN_ALIGN_LEFT;
997                         view_changed = TRUE;
998                 }
999         }
1000         else {
1001                 if (ar_channels && !(ar_channels->flag & RGN_FLAG_HIDDEN)) {
1002                         ar_channels->flag |= RGN_FLAG_HIDDEN;
1003                         ar_channels->v2d.flag &= ~V2D_IS_INITIALISED;
1004                         WM_event_remove_handlers((bContext *)C, &ar_tools->handlers);
1005                         view_changed = TRUE;
1006                 }
1007                 if (ar_channels && ar_channels->alignment != RGN_ALIGN_NONE) {
1008                         ar_channels->alignment = RGN_ALIGN_NONE;
1009                         view_changed = TRUE;
1010                 }
1011         }
1012
1013         if (view_changed) {
1014                 ED_area_initialize(wm, window, sa);
1015                 ED_area_tag_redraw(sa);
1016         }
1017
1018         BKE_movieclip_user_set_frame(&sc->user, scene->r.cfra);
1019 }
1020
1021 /********************* main region ********************/
1022
1023 /* sets up the fields of the View2D from zoom and offset */
1024 static void movieclip_main_area_set_view2d(const bContext *C, ARegion *ar)
1025 {
1026         SpaceClip *sc = CTX_wm_space_clip(C);
1027         MovieClip *clip = ED_space_clip_get_clip(sc);
1028         float x1, y1, w, h;
1029         int width, height, winx, winy;
1030
1031         ED_space_clip_get_size(sc, &width, &height);
1032
1033         w = width;
1034         h = height;
1035
1036         if (clip)
1037                 h *= clip->aspy / clip->aspx / clip->tracking.camera.pixel_aspect;
1038
1039         winx = BLI_RCT_SIZE_X(&ar->winrct) + 1;
1040         winy = BLI_RCT_SIZE_Y(&ar->winrct) + 1;
1041
1042         ar->v2d.tot.xmin = 0;
1043         ar->v2d.tot.ymin = 0;
1044         ar->v2d.tot.xmax = w;
1045         ar->v2d.tot.ymax = h;
1046
1047         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
1048         ar->v2d.mask.xmax = winx;
1049         ar->v2d.mask.ymax = winy;
1050
1051         /* which part of the image space do we see? */
1052         x1 = ar->winrct.xmin + (winx - sc->zoom * w) / 2.0f;
1053         y1 = ar->winrct.ymin + (winy - sc->zoom * h) / 2.0f;
1054
1055         x1 -= sc->zoom * sc->xof;
1056         y1 -= sc->zoom * sc->yof;
1057
1058         /* relative display right */
1059         ar->v2d.cur.xmin = (ar->winrct.xmin - (float)x1) / sc->zoom;
1060         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sc->zoom);
1061
1062         /* relative display left */
1063         ar->v2d.cur.ymin = (ar->winrct.ymin - (float)y1) / sc->zoom;
1064         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sc->zoom);
1065
1066         /* normalize 0.0..1.0 */
1067         ar->v2d.cur.xmin /= w;
1068         ar->v2d.cur.xmax /= w;
1069         ar->v2d.cur.ymin /= h;
1070         ar->v2d.cur.ymax /= h;
1071 }
1072
1073 /* add handlers, stuff you only do once or on area/region changes */
1074 static void clip_main_area_init(wmWindowManager *wm, ARegion *ar)
1075 {
1076         wmKeyMap *keymap;
1077
1078         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
1079
1080         /* mask polls mode */
1081         keymap = WM_keymap_find(wm->defaultconf, "Mask Editing", 0, 0);
1082         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1083
1084         /* own keymap */
1085         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1086         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1087
1088         keymap = WM_keymap_find(wm->defaultconf, "Clip Editor", SPACE_CLIP, 0);
1089         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1090 }
1091
1092 static void clip_main_area_draw(const bContext *C, ARegion *ar)
1093 {
1094         /* draw entirely, view changes should be handled here */
1095         SpaceClip *sc = CTX_wm_space_clip(C);
1096         MovieClip *clip = ED_space_clip_get_clip(sc);
1097
1098         /* if tracking is in progress, we should synchronize framenr from clipuser
1099          * so latest tracked frame would be shown */
1100         if (clip && clip->tracking_context)
1101                 BKE_tracking_context_sync_user(clip->tracking_context, &sc->user);
1102
1103         if (sc->flag & SC_LOCK_SELECTION) {
1104                 ImBuf *tmpibuf = NULL;
1105
1106                 if (clip && clip->tracking.stabilization.flag & TRACKING_2D_STABILIZATION) {
1107                         tmpibuf = ED_space_clip_get_stable_buffer(sc, NULL, NULL, NULL);
1108                 }
1109
1110                 if (ED_clip_view_selection(C, ar, 0)) {
1111                         sc->xof += sc->xlockof;
1112                         sc->yof += sc->ylockof;
1113                 }
1114
1115                 if (tmpibuf)
1116                         IMB_freeImBuf(tmpibuf);
1117         }
1118
1119         /* clear and setup matrix */
1120         UI_ThemeClearColor(TH_BACK);
1121         glClear(GL_COLOR_BUFFER_BIT);
1122
1123         /* data... */
1124         movieclip_main_area_set_view2d(C, ar);
1125
1126         clip_draw_main(C, sc, ar);
1127
1128         if (sc->mode == SC_MODE_MASKEDIT) {
1129
1130                 Mask *mask = CTX_data_edit_mask(C);
1131                 if (mask) {
1132                         ScrArea *sa = CTX_wm_area(C);
1133                         int width, height;
1134                         ED_mask_get_size(sa, &width, &height);
1135                         ED_mask_draw_region(mask, ar,
1136                                             sc->mask_info.draw_flag, sc->mask_info.draw_type,
1137                                             width, height,
1138                                             TRUE, TRUE,
1139                                             sc->stabmat, C);
1140                 }
1141
1142
1143         }
1144
1145         /* Grease Pencil */
1146         clip_draw_grease_pencil((bContext *)C, 1);
1147
1148         /* reset view matrix */
1149         UI_view2d_view_restore(C);
1150
1151         /* draw Grease Pencil - screen space only */
1152         clip_draw_grease_pencil((bContext *)C, 0);
1153 }
1154
1155 static void clip_main_area_listener(ARegion *ar, wmNotifier *wmn)
1156 {
1157         /* context changes */
1158         switch (wmn->category) {
1159                 case NC_SCREEN:
1160                         if (wmn->data == ND_GPENCIL)
1161                                 ED_region_tag_redraw(ar);
1162                         break;
1163         }
1164 }
1165
1166 /****************** preview region ******************/
1167
1168 static void clip_preview_area_init(wmWindowManager *wm, ARegion *ar)
1169 {
1170         wmKeyMap *keymap;
1171
1172         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
1173
1174         /* own keymap */
1175         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1176         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1177
1178         keymap = WM_keymap_find(wm->defaultconf, "Clip Graph Editor", SPACE_CLIP, 0);
1179         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1180 }
1181
1182 static void graph_area_draw(const bContext *C, ARegion *ar)
1183 {
1184         View2D *v2d = &ar->v2d;
1185         View2DScrollers *scrollers;
1186         SpaceClip *sc = CTX_wm_space_clip(C);
1187         Scene *scene = CTX_data_scene(C);
1188         short unitx, unity;
1189
1190         if (sc->flag & SC_LOCK_TIMECURSOR)
1191                 ED_clip_graph_center_current_frame(scene, ar);
1192
1193         /* clear and setup matrix */
1194         UI_ThemeClearColor(TH_BACK);
1195         glClear(GL_COLOR_BUFFER_BIT);
1196
1197         UI_view2d_view_ortho(v2d);
1198
1199         /* data... */
1200         clip_draw_graph(sc, ar, scene);
1201
1202         /* reset view matrix */
1203         UI_view2d_view_restore(C);
1204
1205         /* scrollers */
1206         unitx = (sc->flag & SC_SHOW_SECONDS) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
1207         unity = V2D_UNIT_VALUES;
1208         scrollers = UI_view2d_scrollers_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
1209         UI_view2d_scrollers_draw(C, v2d, scrollers);
1210         UI_view2d_scrollers_free(scrollers);
1211 }
1212
1213 static void dopesheet_area_draw(const bContext *C, ARegion *ar)
1214 {
1215         Scene *scene = CTX_data_scene(C);
1216         SpaceClip *sc = CTX_wm_space_clip(C);
1217         MovieClip *clip = ED_space_clip_get_clip(sc);
1218         View2D *v2d = &ar->v2d;
1219         View2DGrid *grid;
1220         View2DScrollers *scrollers;
1221         short unit = 0;
1222
1223         if (clip)
1224                 BKE_tracking_dopesheet_update(&clip->tracking);
1225
1226         /* clear and setup matrix */
1227         UI_ThemeClearColor(TH_BACK);
1228         glClear(GL_COLOR_BUFFER_BIT);
1229
1230         UI_view2d_view_ortho(v2d);
1231
1232         /* time grid */
1233         unit = (sc->flag & SC_SHOW_SECONDS) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
1234         grid = UI_view2d_grid_calc(CTX_data_scene(C), v2d, unit, V2D_GRID_CLAMP,
1235                                    V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
1236         UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
1237         UI_view2d_grid_free(grid);
1238
1239         /* data... */
1240         clip_draw_dopesheet_main(sc, ar, scene);
1241
1242         /* reset view matrix */
1243         UI_view2d_view_restore(C);
1244
1245         /* scrollers */
1246         scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1247         UI_view2d_scrollers_draw(C, v2d, scrollers);
1248         UI_view2d_scrollers_free(scrollers);
1249 }
1250
1251 static void clip_preview_area_draw(const bContext *C, ARegion *ar)
1252 {
1253         SpaceClip *sc = CTX_wm_space_clip(C);
1254
1255         if (sc->view == SC_VIEW_GRAPH)
1256                 graph_area_draw(C, ar);
1257         else if (sc->view == SC_VIEW_DOPESHEET)
1258                 dopesheet_area_draw(C, ar);
1259 }
1260
1261 static void clip_preview_area_listener(ARegion *UNUSED(ar), wmNotifier *UNUSED(wmn))
1262 {
1263 }
1264
1265 /****************** channels region ******************/
1266
1267 static void clip_channels_area_init(wmWindowManager *wm, ARegion *ar)
1268 {
1269         wmKeyMap *keymap;
1270
1271         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
1272
1273         keymap = WM_keymap_find(wm->defaultconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
1274         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1275 }
1276
1277 static void clip_channels_area_draw(const bContext *C, ARegion *ar)
1278 {
1279         SpaceClip *sc = CTX_wm_space_clip(C);
1280         MovieClip *clip = ED_space_clip_get_clip(sc);
1281         View2D *v2d = &ar->v2d;
1282         View2DScrollers *scrollers;
1283
1284         if (clip)
1285                 BKE_tracking_dopesheet_update(&clip->tracking);
1286
1287         /* clear and setup matrix */
1288         UI_ThemeClearColor(TH_BACK);
1289         glClear(GL_COLOR_BUFFER_BIT);
1290
1291         UI_view2d_view_ortho(v2d);
1292
1293         /* data... */
1294         clip_draw_dopesheet_channels(C, ar);
1295
1296         /* reset view matrix */
1297         UI_view2d_view_restore(C);
1298
1299         /* scrollers */
1300         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1301         UI_view2d_scrollers_draw(C, v2d, scrollers);
1302         UI_view2d_scrollers_free(scrollers);
1303 }
1304
1305 static void clip_channels_area_listener(ARegion *UNUSED(ar), wmNotifier *UNUSED(wmn))
1306 {
1307 }
1308
1309 /****************** header region ******************/
1310
1311 /* add handlers, stuff you only do once or on area/region changes */
1312 static void clip_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
1313 {
1314         ED_region_header_init(ar);
1315 }
1316
1317 static void clip_header_area_draw(const bContext *C, ARegion *ar)
1318 {
1319         ED_region_header(C, ar);
1320 }
1321
1322 static void clip_header_area_listener(ARegion *ar, wmNotifier *wmn)
1323 {
1324         /* context changes */
1325         switch (wmn->category) {
1326                 case NC_SCENE:
1327                         switch (wmn->data) {
1328                                 /* for proportional editmode only */
1329                                 case ND_TOOLSETTINGS:
1330                                         /* TODO - should do this when in mask mode only but no datas available */
1331                                         // if (sc->mode == SC_MODE_MASKEDIT)
1332                                 {
1333                                         ED_region_tag_redraw(ar);
1334                                 }
1335                                 break;
1336                         }
1337                         break;
1338         }
1339 }
1340
1341
1342 /****************** tools region ******************/
1343
1344 /* add handlers, stuff you only do once or on area/region changes */
1345 static void clip_tools_area_init(wmWindowManager *wm, ARegion *ar)
1346 {
1347         ED_region_panels_init(wm, ar);
1348 }
1349
1350 static void clip_tools_area_draw(const bContext *C, ARegion *ar)
1351 {
1352         ED_region_panels(C, ar, 1, NULL, -1);
1353 }
1354
1355 /****************** tool properties region ******************/
1356
1357 static void clip_props_area_listener(ARegion *ar, wmNotifier *wmn)
1358 {
1359         /* context changes */
1360         switch (wmn->category) {
1361                 case NC_WM:
1362                         if (wmn->data == ND_HISTORY)
1363                                 ED_region_tag_redraw(ar);
1364                         break;
1365                 case NC_SCENE:
1366                         if (wmn->data == ND_MODE)
1367                                 ED_region_tag_redraw(ar);
1368                         break;
1369                 case NC_SPACE:
1370                         if (wmn->data == ND_SPACE_CLIP)
1371                                 ED_region_tag_redraw(ar);
1372                         break;
1373                 case NC_SCREEN:
1374                         if (wmn->data == ND_GPENCIL)
1375                                 ED_region_tag_redraw(ar);
1376                         break;
1377         }
1378 }
1379
1380 /****************** properties region ******************/
1381
1382 /* add handlers, stuff you only do once or on area/region changes */
1383 static void clip_properties_area_init(wmWindowManager *wm, ARegion *ar)
1384 {
1385         wmKeyMap *keymap;
1386
1387         ED_region_panels_init(wm, ar);
1388
1389         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1390         WM_event_add_keymap_handler(&ar->handlers, keymap);
1391 }
1392
1393 static void clip_properties_area_draw(const bContext *C, ARegion *ar)
1394 {
1395         SpaceClip *sc = CTX_wm_space_clip(C);
1396
1397         BKE_movieclip_update_scopes(sc->clip, &sc->user, &sc->scopes);
1398
1399         ED_region_panels(C, ar, 1, NULL, -1);
1400 }
1401
1402 static void clip_properties_area_listener(ARegion *ar, wmNotifier *wmn)
1403 {
1404         /* context changes */
1405         switch (wmn->category) {
1406                 case NC_SCREEN:
1407                         if (wmn->data == ND_GPENCIL)
1408                                 ED_region_tag_redraw(ar);
1409                         break;
1410                 case NC_BRUSH:
1411                         if (wmn->action == NA_EDITED)
1412                                 ED_region_tag_redraw(ar);
1413                         break;
1414         }
1415 }
1416
1417 /********************* registration ********************/
1418
1419 /* only called once, from space/spacetypes.c */
1420 void ED_spacetype_clip(void)
1421 {
1422         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype clip");
1423         ARegionType *art;
1424
1425         st->spaceid = SPACE_CLIP;
1426         strncpy(st->name, "Clip", BKE_ST_MAXNAME);
1427
1428         st->new = clip_new;
1429         st->free = clip_free;
1430         st->init = clip_init;
1431         st->duplicate = clip_duplicate;
1432         st->operatortypes = clip_operatortypes;
1433         st->keymap = clip_keymap;
1434         st->listener = clip_listener;
1435         st->context = clip_context;
1436         st->dropboxes = clip_dropboxes;
1437         st->refresh = clip_refresh;
1438
1439         /* regions: main window */
1440         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region");
1441         art->regionid = RGN_TYPE_WINDOW;
1442         art->init = clip_main_area_init;
1443         art->draw = clip_main_area_draw;
1444         art->listener = clip_main_area_listener;
1445         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_GPENCIL;
1446
1447         BLI_addhead(&st->regiontypes, art);
1448
1449         /* preview */
1450         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region preview");
1451         art->regionid = RGN_TYPE_PREVIEW;
1452         art->prefsizey = 240;
1453         art->init = clip_preview_area_init;
1454         art->draw = clip_preview_area_draw;
1455         art->listener = clip_preview_area_listener;
1456         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
1457
1458         BLI_addhead(&st->regiontypes, art);
1459
1460         /* regions: properties */
1461         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region properties");
1462         art->regionid = RGN_TYPE_UI;
1463         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1464         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1465         art->init = clip_properties_area_init;
1466         art->draw = clip_properties_area_draw;
1467         art->listener = clip_properties_area_listener;
1468         BLI_addhead(&st->regiontypes, art);
1469         ED_clip_buttons_register(art);
1470
1471         /* regions: tools */
1472         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region tools");
1473         art->regionid = RGN_TYPE_TOOLS;
1474         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1475         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1476         art->listener = clip_props_area_listener;
1477         art->init = clip_tools_area_init;
1478         art->draw = clip_tools_area_draw;
1479
1480         BLI_addhead(&st->regiontypes, art);
1481
1482         /* tool properties */
1483         art = MEM_callocN(sizeof(ARegionType), "spacetype clip tool properties region");
1484         art->regionid = RGN_TYPE_TOOL_PROPS;
1485         art->prefsizex = 0;
1486         art->prefsizey = 120;
1487         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1488         art->listener = clip_props_area_listener;
1489         art->init = clip_tools_area_init;
1490         art->draw = clip_tools_area_draw;
1491         ED_clip_tool_props_register(art);
1492
1493         BLI_addhead(&st->regiontypes, art);
1494
1495         /* regions: header */
1496         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region");
1497         art->regionid = RGN_TYPE_HEADER;
1498         art->prefsizey = HEADERY;
1499         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_HEADER;
1500
1501         art->init = clip_header_area_init;
1502         art->draw = clip_header_area_draw;
1503         art->listener = clip_header_area_listener;
1504
1505         BLI_addhead(&st->regiontypes, art);
1506
1507         BKE_spacetype_register(st);
1508
1509         /* channels */
1510         art = MEM_callocN(sizeof(ARegionType), "spacetype clip channels region");
1511         art->regionid = RGN_TYPE_CHANNELS;
1512         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1513         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1514         art->listener = clip_channels_area_listener;
1515         art->init = clip_channels_area_init;
1516         art->draw = clip_channels_area_draw;
1517
1518         BLI_addhead(&st->regiontypes, art);
1519 }