2f684f9e33012562fa7dc70d87bd9d5b2ee75faa
[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(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                                         clip_scopes_tag_refresh(sa);
393                                         ED_area_tag_redraw(sa);
394                                         break;
395                                 case NA_EDITED:
396                                         ED_area_tag_redraw(sa);
397                                         break;
398                         }
399                         break;
400                 case NC_GEOM:
401                         switch (wmn->data) {
402                                 case ND_SELECT:
403                                         clip_scopes_tag_refresh(sa);
404                                         ED_area_tag_redraw(sa);
405                                         break;
406                         }
407                         break;
408                 case NC_SCREEN:
409                         switch (wmn->data) {
410                                 case ND_ANIMPLAY:
411                                 case ND_GPENCIL:
412                                         clip_scopes_check_gpencil_change(sa);
413                                         ED_area_tag_redraw(sa);
414                                         break;
415                         }
416                         break;
417                 case NC_SPACE:
418                         if (wmn->data == ND_SPACE_CLIP) {
419                                 clip_scopes_tag_refresh(sa);
420                                 clip_stabilization_tag_refresh(sa);
421                                 ED_area_tag_redraw(sa);
422                         }
423                         break;
424         }
425 }
426
427 static void clip_operatortypes(void)
428 {
429         /* ** clip_ops.c ** */
430         WM_operatortype_append(CLIP_OT_open);
431         WM_operatortype_append(CLIP_OT_reload);
432         WM_operatortype_append(CLIP_OT_view_pan);
433         WM_operatortype_append(CLIP_OT_view_zoom);
434         WM_operatortype_append(CLIP_OT_view_zoom_in);
435         WM_operatortype_append(CLIP_OT_view_zoom_out);
436         WM_operatortype_append(CLIP_OT_view_zoom_ratio);
437         WM_operatortype_append(CLIP_OT_view_all);
438         WM_operatortype_append(CLIP_OT_view_selected);
439         WM_operatortype_append(CLIP_OT_change_frame);
440         WM_operatortype_append(CLIP_OT_rebuild_proxy);
441         WM_operatortype_append(CLIP_OT_mode_set);
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         /* orientation */
482         WM_operatortype_append(CLIP_OT_set_origin);
483         WM_operatortype_append(CLIP_OT_set_plane);
484         WM_operatortype_append(CLIP_OT_set_axis);
485         WM_operatortype_append(CLIP_OT_set_scale);
486         WM_operatortype_append(CLIP_OT_set_solution_scale);
487
488         /* detect */
489         WM_operatortype_append(CLIP_OT_detect_features);
490
491         /* stabilization */
492         WM_operatortype_append(CLIP_OT_stabilize_2d_add);
493         WM_operatortype_append(CLIP_OT_stabilize_2d_remove);
494         WM_operatortype_append(CLIP_OT_stabilize_2d_select);
495         WM_operatortype_append(CLIP_OT_stabilize_2d_set_rotation);
496
497         /* clean-up */
498         WM_operatortype_append(CLIP_OT_clear_track_path);
499         WM_operatortype_append(CLIP_OT_join_tracks);
500         WM_operatortype_append(CLIP_OT_track_copy_color);
501
502         WM_operatortype_append(CLIP_OT_clean_tracks);
503
504         /* object tracking */
505         WM_operatortype_append(CLIP_OT_tracking_object_new);
506         WM_operatortype_append(CLIP_OT_tracking_object_remove);
507
508         /* clipboard */
509         WM_operatortype_append(CLIP_OT_copy_tracks);
510         WM_operatortype_append(CLIP_OT_paste_tracks);
511
512         /* ** clip_graph_ops.c  ** */
513
514         /* graph editing */
515
516         /* selection */
517         WM_operatortype_append(CLIP_OT_graph_select);
518         WM_operatortype_append(CLIP_OT_graph_select_border);
519         WM_operatortype_append(CLIP_OT_graph_select_all_markers);
520
521         WM_operatortype_append(CLIP_OT_graph_delete_curve);
522         WM_operatortype_append(CLIP_OT_graph_delete_knot);
523         WM_operatortype_append(CLIP_OT_graph_view_all);
524         WM_operatortype_append(CLIP_OT_graph_center_current_frame);
525
526         WM_operatortype_append(CLIP_OT_graph_disable_markers);
527
528         /* ** clip_dopesheet_ops.c  ** */
529
530         WM_operatortype_append(CLIP_OT_dopesheet_select_channel);
531 }
532
533 static void clip_keymap(struct wmKeyConfig *keyconf)
534 {
535         wmKeyMap *keymap;
536         wmKeyMapItem *kmi;
537
538         /* ******** Global hotkeys avalaible for all regions ******** */
539
540         keymap = WM_keymap_find(keyconf, "Clip", SPACE_CLIP, 0);
541
542         WM_keymap_add_item(keymap, "CLIP_OT_open", OKEY, KM_PRESS, KM_ALT, 0);
543
544         WM_keymap_add_item(keymap, "CLIP_OT_tools", TKEY, KM_PRESS, 0, 0);
545         WM_keymap_add_item(keymap, "CLIP_OT_properties", NKEY, KM_PRESS, 0, 0);
546
547         /* 2d tracking */
548         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", LEFTARROWKEY, KM_PRESS, KM_ALT, 0);
549         RNA_boolean_set(kmi->ptr, "backwards", TRUE);
550         RNA_boolean_set(kmi->ptr, "sequence", FALSE);
551         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", RIGHTARROWKEY, KM_PRESS, KM_ALT, 0);
552         RNA_boolean_set(kmi->ptr, "backwards", FALSE);
553         RNA_boolean_set(kmi->ptr, "sequence", FALSE);
554         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_CTRL, 0);
555         RNA_boolean_set(kmi->ptr, "backwards", FALSE);
556         RNA_boolean_set(kmi->ptr, "sequence", TRUE);
557         kmi = WM_keymap_add_item(keymap, "CLIP_OT_track_markers", TKEY, KM_PRESS, KM_SHIFT | KM_CTRL, 0);
558         RNA_boolean_set(kmi->ptr, "backwards", TRUE);
559         RNA_boolean_set(kmi->ptr, "sequence", TRUE);
560
561         /* mode */
562         kmi = WM_keymap_add_item(keymap, "CLIP_OT_mode_set", TABKEY, KM_PRESS, 0, 0);
563         RNA_enum_set(kmi->ptr, "mode", SC_MODE_RECONSTRUCTION);
564         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
565
566         kmi = WM_keymap_add_item(keymap, "CLIP_OT_mode_set", TABKEY, KM_PRESS, KM_CTRL, 0);
567         RNA_enum_set(kmi->ptr, "mode", SC_MODE_DISTORTION);
568         RNA_boolean_set(kmi->ptr, "toggle", TRUE);
569
570         WM_keymap_add_item(keymap, "CLIP_OT_solve_camera", SKEY, KM_PRESS, KM_SHIFT, 0);
571
572         /* ******** Hotkeys avalaible for main region only ******** */
573
574         keymap = WM_keymap_find(keyconf, "Clip Editor", SPACE_CLIP, 0);
575 //      keymap->poll = ED_space_clip_tracking_poll;
576         /* ** View/navigation ** */
577
578         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
579         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
580         WM_keymap_add_item(keymap, "CLIP_OT_view_pan", MOUSEPAN, 0, 0, 0);
581
582         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
583         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom", MOUSEZOOM, 0, 0, 0);
584         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
585         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
586         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
587         WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_out", PADMINUS, KM_PRESS, 0, 0);
588
589         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 8.0f);
590         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 4.0f);
591         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, KM_SHIFT, 0)->ptr, "ratio", 2.0f);
592         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD1, KM_PRESS, 0, 0)->ptr, "ratio", 1.0f);
593         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD2, KM_PRESS, 0, 0)->ptr, "ratio", 0.5f);
594         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD4, KM_PRESS, 0, 0)->ptr, "ratio", 0.25f);
595         RNA_float_set(WM_keymap_add_item(keymap, "CLIP_OT_view_zoom_ratio", PAD8, KM_PRESS, 0, 0)->ptr, "ratio", 0.125f);
596
597         WM_keymap_add_item(keymap, "CLIP_OT_view_all", HOMEKEY, KM_PRESS, 0, 0);
598
599         kmi = WM_keymap_add_item(keymap, "CLIP_OT_view_all", FKEY, KM_PRESS, 0, 0);
600         RNA_boolean_set(kmi->ptr, "fit_view", TRUE);
601
602         WM_keymap_add_item(keymap, "CLIP_OT_view_selected", PADPERIOD, KM_PRESS, 0, 0);
603
604         /* jump to special frame */
605         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
606         RNA_enum_set(kmi->ptr, "position", 0);
607
608         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_CTRL | KM_SHIFT, 0);
609         RNA_enum_set(kmi->ptr, "position", 1);
610
611         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", LEFTARROWKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
612         RNA_enum_set(kmi->ptr, "position", 2);
613
614         kmi = WM_keymap_add_item(keymap, "CLIP_OT_frame_jump", RIGHTARROWKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
615         RNA_enum_set(kmi->ptr, "position", 3);
616
617         /* "timeline" */
618         WM_keymap_add_item(keymap, "CLIP_OT_change_frame", LEFTMOUSE, KM_PRESS, 0, 0);
619
620         /* selection */
621         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, 0, 0);
622         RNA_boolean_set(kmi->ptr, "extend", FALSE);
623         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
624         RNA_boolean_set(kmi->ptr, "extend", TRUE);
625         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_all", AKEY, KM_PRESS, 0, 0);
626         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
627         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_all", IKEY, KM_PRESS, KM_CTRL, 0);
628         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
629         WM_keymap_add_item(keymap, "CLIP_OT_select_border", BKEY, KM_PRESS, 0, 0);
630         WM_keymap_add_item(keymap, "CLIP_OT_select_circle", CKEY, KM_PRESS, 0, 0);
631         WM_keymap_add_menu(keymap, "CLIP_MT_select_grouped", GKEY, KM_PRESS, KM_SHIFT, 0);
632
633         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_ALT, 0);
634         RNA_boolean_set(kmi->ptr, "deselect", FALSE);
635         kmi = WM_keymap_add_item(keymap, "CLIP_OT_select_lasso", EVT_TWEAK_A, KM_ANY, KM_CTRL | KM_SHIFT | KM_ALT, 0);
636         RNA_boolean_set(kmi->ptr, "deselect", TRUE);
637
638         /* marker */
639         WM_keymap_add_item(keymap, "CLIP_OT_add_marker_slide", LEFTMOUSE, KM_PRESS, KM_CTRL, 0);
640
641         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", DELKEY, KM_PRESS, KM_SHIFT, 0);
642         WM_keymap_add_item(keymap, "CLIP_OT_delete_marker", XKEY, KM_PRESS, KM_SHIFT, 0);
643
644         WM_keymap_add_item(keymap, "CLIP_OT_slide_marker", LEFTMOUSE, KM_PRESS, 0, 0);
645
646         kmi = WM_keymap_add_item(keymap, "CLIP_OT_disable_markers", DKEY, KM_PRESS, KM_SHIFT, 0);
647         RNA_enum_set(kmi->ptr, "action", 2);    /* toggle */
648
649         /* tracks */
650         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", DELKEY, KM_PRESS, 0, 0);
651         WM_keymap_add_item(keymap, "CLIP_OT_delete_track", XKEY, KM_PRESS, 0, 0);
652
653         kmi = WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_CTRL, 0);
654         RNA_enum_set(kmi->ptr, "action", 0);    /* lock */
655
656         kmi = WM_keymap_add_item(keymap, "CLIP_OT_lock_tracks", LKEY, KM_PRESS, KM_ALT, 0);
657         RNA_enum_set(kmi->ptr, "action", 1);    /* unlock */
658
659         kmi = WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, 0, 0);
660         RNA_boolean_set(kmi->ptr, "unselected", FALSE);
661
662         kmi = WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks", HKEY, KM_PRESS, KM_SHIFT, 0);
663         RNA_boolean_set(kmi->ptr, "unselected", TRUE);
664
665         WM_keymap_add_item(keymap, "CLIP_OT_hide_tracks_clear", HKEY, KM_PRESS, KM_ALT, 0);
666
667         /* clean-up */
668         WM_keymap_add_item(keymap, "CLIP_OT_join_tracks", JKEY, KM_PRESS, KM_CTRL, 0);
669
670         /* menus */
671         WM_keymap_add_menu(keymap, "CLIP_MT_tracking_specials", WKEY, KM_PRESS, 0, 0);
672
673         /* display */
674         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
675         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_selection");
676
677         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", DKEY, KM_PRESS, KM_ALT, 0);
678         RNA_string_set(kmi->ptr, "data_path", "space_data.show_disabled");
679
680         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", SKEY, KM_PRESS, KM_ALT, 0);
681         RNA_string_set(kmi->ptr, "data_path", "space_data.show_marker_search");
682
683         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", MKEY, KM_PRESS, 0, 0);
684         RNA_string_set(kmi->ptr, "data_path", "space_data.use_mute_footage");
685
686         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
687
688         /* clean-up */
689         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT, 0);
690         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_REMAINED);
691         RNA_boolean_set(kmi->ptr, "clear_active", FALSE);
692         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_SHIFT, 0);
693         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_UPTO);
694         RNA_boolean_set(kmi->ptr, "clear_active", FALSE);
695         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
696         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_ALL);
697         RNA_boolean_set(kmi->ptr, "clear_active", FALSE);
698
699         /* ******** Hotkeys avalaible for preview region only ******** */
700
701         keymap = WM_keymap_find(keyconf, "Clip Graph Editor", SPACE_CLIP, 0);
702
703         /* "timeline" */
704         WM_keymap_add_item(keymap, "CLIP_OT_change_frame", ACTIONMOUSE, KM_PRESS, 0, 0);
705
706         /* selection */
707         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select", SELECTMOUSE, KM_PRESS, 0, 0);
708         RNA_boolean_set(kmi->ptr, "extend", FALSE);
709         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select", SELECTMOUSE, KM_PRESS, KM_SHIFT, 0);
710         RNA_boolean_set(kmi->ptr, "extend", TRUE);
711
712         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select_all_markers", AKEY, KM_PRESS, 0, 0);
713         RNA_enum_set(kmi->ptr, "action", SEL_TOGGLE);
714         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_select_all_markers", IKEY, KM_PRESS, KM_CTRL, 0);
715         RNA_enum_set(kmi->ptr, "action", SEL_INVERT);
716
717         WM_keymap_add_item(keymap, "CLIP_OT_graph_select_border", BKEY, KM_PRESS, 0, 0);
718
719         /* delete */
720         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_curve", DELKEY, KM_PRESS, 0, 0);
721         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_curve", XKEY, KM_PRESS, 0, 0);
722
723         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_knot", DELKEY, KM_PRESS, KM_SHIFT, 0);
724         WM_keymap_add_item(keymap, "CLIP_OT_graph_delete_knot", XKEY, KM_PRESS, KM_SHIFT, 0);
725
726         /* view */
727         WM_keymap_add_item(keymap, "CLIP_OT_graph_view_all", HOMEKEY, KM_PRESS, 0, 0);
728         WM_keymap_add_item(keymap, "CLIP_OT_graph_center_current_frame", PADPERIOD, KM_PRESS, 0, 0);
729
730         kmi = WM_keymap_add_item(keymap, "WM_OT_context_toggle", LKEY, KM_PRESS, 0, 0);
731         RNA_string_set(kmi->ptr, "data_path", "space_data.lock_time_cursor");
732
733         /* clean-up */
734         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT, 0);
735         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_REMAINED);
736         RNA_boolean_set(kmi->ptr, "clear_active", TRUE);
737         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_SHIFT, 0);
738         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_UPTO);
739         RNA_boolean_set(kmi->ptr, "clear_active", TRUE);
740         kmi = WM_keymap_add_item(keymap, "CLIP_OT_clear_track_path", TKEY, KM_PRESS, KM_ALT | KM_SHIFT, 0);
741         RNA_enum_set(kmi->ptr, "action", TRACK_CLEAR_ALL);
742         RNA_boolean_set(kmi->ptr, "clear_active", TRUE);
743
744         /* tracks */
745         kmi = WM_keymap_add_item(keymap, "CLIP_OT_graph_disable_markers", DKEY, KM_PRESS, KM_SHIFT, 0);
746         RNA_enum_set(kmi->ptr, "action", 2);    /* toggle */
747
748         transform_keymap_for_space(keyconf, keymap, SPACE_CLIP);
749
750         /* ******** Hotkeys avalaible for channels region only ******** */
751
752         keymap = WM_keymap_find(keyconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
753
754         kmi = WM_keymap_add_item(keymap, "CLIP_OT_dopesheet_select_channel", ACTIONMOUSE, KM_PRESS, 0, 0);
755         RNA_boolean_set(kmi->ptr, "extend", TRUE);  /* toggle */
756 }
757
758 const char *clip_context_dir[] = {"edit_movieclip", "edit_mask", NULL};
759
760 static int clip_context(const bContext *C, const char *member, bContextDataResult *result)
761 {
762         SpaceClip *sc = CTX_wm_space_clip(C);
763
764         if (CTX_data_dir(member)) {
765                 CTX_data_dir_set(result, clip_context_dir);
766
767                 return TRUE;
768         }
769         else if (CTX_data_equals(member, "edit_movieclip")) {
770                 if (sc->clip)
771                         CTX_data_id_pointer_set(result, &sc->clip->id);
772                 return TRUE;
773         }
774         else if (CTX_data_equals(member, "edit_mask")) {
775                 if (sc->mask)
776                         CTX_data_id_pointer_set(result, &sc->mask->id);
777                 return TRUE;
778         }
779
780         return FALSE;
781 }
782
783 /* dropboxes */
784 static int clip_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
785 {
786         if (drag->type == WM_DRAG_PATH)
787                 if (ELEM3(drag->icon, 0, ICON_FILE_IMAGE, ICON_FILE_BLANK)) /* rule might not work? */
788                         return TRUE;
789
790         return FALSE;
791 }
792
793 static void clip_drop_copy(wmDrag *drag, wmDropBox *drop)
794 {
795         /* copy drag path to properties */
796         RNA_string_set(drop->ptr, "filepath", drag->path);
797 }
798
799 /* area+region dropbox definition */
800 static void clip_dropboxes(void)
801 {
802         ListBase *lb = WM_dropboxmap_find("Clip", SPACE_CLIP, 0);
803
804         WM_dropbox_add(lb, "CLIP_OT_open", clip_drop_poll, clip_drop_copy);
805 }
806
807 static void clip_refresh(const bContext *C, ScrArea *sa)
808 {
809         wmWindowManager *wm = CTX_wm_manager(C);
810         wmWindow *window = CTX_wm_window(C);
811         Scene *scene = CTX_data_scene(C);
812         SpaceClip *sc = (SpaceClip *)sa->spacedata.first;
813         ARegion *ar_main = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
814         ARegion *ar_tools = BKE_area_find_region_type(sa, RGN_TYPE_TOOLS);
815         ARegion *ar_tool_props = BKE_area_find_region_type(sa, RGN_TYPE_TOOL_PROPS);
816         ARegion *ar_preview = ED_clip_has_preview_region(C, sa);
817         ARegion *ar_properties = ED_clip_has_properties_region(sa);
818         ARegion *ar_channels = ED_clip_has_channels_region(sa);
819         int main_visible = FALSE, preview_visible = FALSE, tools_visible = FALSE;
820         int tool_props_visible = FALSE, properties_visible = FALSE, channels_visible = FALSE;
821         int view_changed = FALSE;
822
823         switch (sc->view) {
824                 case SC_VIEW_CLIP:
825                         main_visible = TRUE;
826                         preview_visible = FALSE;
827                         tools_visible = TRUE;
828                         tool_props_visible = TRUE;
829                         properties_visible = TRUE;
830                         channels_visible = FALSE;
831                         break;
832                 case SC_VIEW_GRAPH:
833                         main_visible = FALSE;
834                         preview_visible = TRUE;
835                         tools_visible = FALSE;
836                         tool_props_visible = FALSE;
837                         properties_visible = FALSE;
838                         channels_visible = FALSE;
839
840                         reinit_preview_region(C, ar_preview);
841                         break;
842                 case SC_VIEW_DOPESHEET:
843                         main_visible = FALSE;
844                         preview_visible = TRUE;
845                         tools_visible = FALSE;
846                         tool_props_visible = FALSE;
847                         properties_visible = FALSE;
848                         channels_visible = TRUE;
849
850                         reinit_preview_region(C, ar_preview);
851                         break;
852         }
853
854         if (main_visible) {
855                 if (ar_main && (ar_main->flag & RGN_FLAG_HIDDEN)) {
856                         ar_main->flag &= ~RGN_FLAG_HIDDEN;
857                         ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
858                         view_changed = TRUE;
859                 }
860
861                 if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
862                         ar_main->alignment = RGN_ALIGN_NONE;
863                         view_changed = TRUE;
864                 }
865         }
866         else {
867                 if (ar_main && !(ar_main->flag & RGN_FLAG_HIDDEN)) {
868                         ar_main->flag |= RGN_FLAG_HIDDEN;
869                         ar_main->v2d.flag &= ~V2D_IS_INITIALISED;
870                         WM_event_remove_handlers((bContext *)C, &ar_main->handlers);
871                         view_changed = TRUE;
872                 }
873                 if (ar_main && ar_main->alignment != RGN_ALIGN_NONE) {
874                         ar_main->alignment = RGN_ALIGN_NONE;
875                         view_changed = TRUE;
876                 }
877         }
878
879         if (properties_visible) {
880                 if (ar_properties && (ar_properties->flag & RGN_FLAG_HIDDEN)) {
881                         ar_properties->flag &= ~RGN_FLAG_HIDDEN;
882                         ar_properties->v2d.flag &= ~V2D_IS_INITIALISED;
883                         view_changed = TRUE;
884                 }
885                 if (ar_properties && ar_properties->alignment != RGN_ALIGN_RIGHT) {
886                         ar_properties->alignment = RGN_ALIGN_RIGHT;
887                         view_changed = TRUE;
888                 }
889         }
890         else {
891                 if (ar_properties && !(ar_properties->flag & RGN_FLAG_HIDDEN)) {
892                         ar_properties->flag |= RGN_FLAG_HIDDEN;
893                         ar_properties->v2d.flag &= ~V2D_IS_INITIALISED;
894                         WM_event_remove_handlers((bContext *)C, &ar_properties->handlers);
895                         view_changed = TRUE;
896                 }
897                 if (ar_properties && ar_properties->alignment != RGN_ALIGN_NONE) {
898                         ar_properties->alignment = RGN_ALIGN_NONE;
899                         view_changed = TRUE;
900                 }
901         }
902
903         if (tools_visible) {
904                 if (ar_tools && (ar_tools->flag & RGN_FLAG_HIDDEN)) {
905                         ar_tools->flag &= ~RGN_FLAG_HIDDEN;
906                         ar_tools->v2d.flag &= ~V2D_IS_INITIALISED;
907                         view_changed = TRUE;
908                 }
909                 if (ar_tools && ar_tools->alignment != RGN_ALIGN_LEFT) {
910                         ar_tools->alignment = RGN_ALIGN_LEFT;
911                         view_changed = TRUE;
912                 }
913         }
914         else {
915                 if (ar_tools && !(ar_tools->flag & RGN_FLAG_HIDDEN)) {
916                         ar_tools->flag |= RGN_FLAG_HIDDEN;
917                         ar_tools->v2d.flag &= ~V2D_IS_INITIALISED;
918                         WM_event_remove_handlers((bContext *)C, &ar_tools->handlers);
919                         view_changed = TRUE;
920                 }
921                 if (ar_tools && ar_tools->alignment != RGN_ALIGN_NONE) {
922                         ar_tools->alignment = RGN_ALIGN_NONE;
923                         view_changed = TRUE;
924                 }
925         }
926
927         if (tool_props_visible) {
928                 if (ar_tool_props && (ar_tool_props->flag & RGN_FLAG_HIDDEN)) {
929                         ar_tool_props->flag &= ~RGN_FLAG_HIDDEN;
930                         ar_tool_props->v2d.flag &= ~V2D_IS_INITIALISED;
931                         view_changed = TRUE;
932                 }
933                 if (ar_tool_props && (ar_tool_props->alignment != (RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV))) {
934                         ar_tool_props->alignment = RGN_ALIGN_BOTTOM | RGN_SPLIT_PREV;
935                         view_changed = TRUE;
936                 }
937         }
938         else {
939                 if (ar_tool_props && !(ar_tool_props->flag & RGN_FLAG_HIDDEN)) {
940                         ar_tool_props->flag |= RGN_FLAG_HIDDEN;
941                         ar_tool_props->v2d.flag &= ~V2D_IS_INITIALISED;
942                         WM_event_remove_handlers((bContext *)C, &ar_tool_props->handlers);
943                         view_changed = TRUE;
944                 }
945                 if (ar_tool_props && ar_tool_props->alignment != RGN_ALIGN_NONE) {
946                         ar_tool_props->alignment = RGN_ALIGN_NONE;
947                         view_changed = TRUE;
948                 }
949         }
950
951         if (preview_visible) {
952                 if (ar_preview && (ar_preview->flag & RGN_FLAG_HIDDEN)) {
953                         ar_preview->flag &= ~RGN_FLAG_HIDDEN;
954                         ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
955                         ar_preview->v2d.cur = ar_preview->v2d.tot;
956                         view_changed = TRUE;
957                 }
958                 if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
959                         ar_preview->alignment = RGN_ALIGN_NONE;
960                         view_changed = TRUE;
961                 }
962         }
963         else {
964                 if (ar_preview && !(ar_preview->flag & RGN_FLAG_HIDDEN)) {
965                         ar_preview->flag |= RGN_FLAG_HIDDEN;
966                         ar_preview->v2d.flag &= ~V2D_IS_INITIALISED;
967                         WM_event_remove_handlers((bContext *)C, &ar_preview->handlers);
968                         view_changed = TRUE;
969                 }
970                 if (ar_preview && ar_preview->alignment != RGN_ALIGN_NONE) {
971                         ar_preview->alignment = RGN_ALIGN_NONE;
972                         view_changed = TRUE;
973                 }
974         }
975
976         if (channels_visible) {
977                 if (ar_channels && (ar_channels->flag & RGN_FLAG_HIDDEN)) {
978                         ar_channels->flag &= ~RGN_FLAG_HIDDEN;
979                         ar_channels->v2d.flag &= ~V2D_IS_INITIALISED;
980                         view_changed = TRUE;
981                 }
982                 if (ar_channels && ar_channels->alignment != RGN_ALIGN_LEFT) {
983                         ar_channels->alignment = RGN_ALIGN_LEFT;
984                         view_changed = TRUE;
985                 }
986         }
987         else {
988                 if (ar_channels && !(ar_channels->flag & RGN_FLAG_HIDDEN)) {
989                         ar_channels->flag |= RGN_FLAG_HIDDEN;
990                         ar_channels->v2d.flag &= ~V2D_IS_INITIALISED;
991                         WM_event_remove_handlers((bContext *)C, &ar_tools->handlers);
992                         view_changed = TRUE;
993                 }
994                 if (ar_channels && ar_channels->alignment != RGN_ALIGN_NONE) {
995                         ar_channels->alignment = RGN_ALIGN_NONE;
996                         view_changed = TRUE;
997                 }
998         }
999
1000         if (view_changed) {
1001                 ED_area_initialize(wm, window, sa);
1002                 ED_area_tag_redraw(sa);
1003         }
1004
1005         BKE_movieclip_user_set_frame(&sc->user, scene->r.cfra);
1006 }
1007
1008 /********************* main region ********************/
1009
1010 /* sets up the fields of the View2D from zoom and offset */
1011 static void movieclip_main_area_set_view2d(SpaceClip *sc, ARegion *ar)
1012 {
1013         MovieClip *clip = ED_space_clip(sc);
1014         float x1, y1, w, h;
1015         int width, height, winx, winy;
1016
1017         ED_space_clip_size(sc, &width, &height);
1018
1019         w = width;
1020         h = height;
1021
1022         if (clip)
1023                 h *= clip->aspy / clip->aspx / clip->tracking.camera.pixel_aspect;
1024
1025         winx = ar->winrct.xmax - ar->winrct.xmin + 1;
1026         winy = ar->winrct.ymax - ar->winrct.ymin + 1;
1027
1028         ar->v2d.tot.xmin = 0;
1029         ar->v2d.tot.ymin = 0;
1030         ar->v2d.tot.xmax = w;
1031         ar->v2d.tot.ymax = h;
1032
1033         ar->v2d.mask.xmin = ar->v2d.mask.ymin = 0;
1034         ar->v2d.mask.xmax = winx;
1035         ar->v2d.mask.ymax = winy;
1036
1037         /* which part of the image space do we see? */
1038         x1 = ar->winrct.xmin + (winx - sc->zoom * w) / 2.0f;
1039         y1 = ar->winrct.ymin + (winy - sc->zoom * h) / 2.0f;
1040
1041         x1 -= sc->zoom * sc->xof;
1042         y1 -= sc->zoom * sc->yof;
1043
1044         /* relative display right */
1045         ar->v2d.cur.xmin = (ar->winrct.xmin - (float)x1) / sc->zoom;
1046         ar->v2d.cur.xmax = ar->v2d.cur.xmin + ((float)winx / sc->zoom);
1047
1048         /* relative display left */
1049         ar->v2d.cur.ymin = (ar->winrct.ymin - (float)y1) / sc->zoom;
1050         ar->v2d.cur.ymax = ar->v2d.cur.ymin + ((float)winy / sc->zoom);
1051
1052         /* normalize 0.0..1.0 */
1053         ar->v2d.cur.xmin /= w;
1054         ar->v2d.cur.xmax /= w;
1055         ar->v2d.cur.ymin /= h;
1056         ar->v2d.cur.ymax /= h;
1057 }
1058
1059 /* add handlers, stuff you only do once or on area/region changes */
1060 static void clip_main_area_init(wmWindowManager *wm, ARegion *ar)
1061 {
1062         wmKeyMap *keymap;
1063
1064         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_STANDARD, ar->winx, ar->winy);
1065
1066         /* own keymap */
1067         keymap = WM_keymap_find(wm->defaultconf, "Mask Editing", 0, 0);
1068         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1069
1070         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1071         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1072
1073         keymap = WM_keymap_find(wm->defaultconf, "Clip Editor", SPACE_CLIP, 0);
1074         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1075 }
1076
1077 static void clip_main_area_draw(const bContext *C, ARegion *ar)
1078 {
1079         /* draw entirely, view changes should be handled here */
1080         SpaceClip *sc = CTX_wm_space_clip(C);
1081         Scene *scene = CTX_data_scene(C);
1082         MovieClip *clip = ED_space_clip(sc);
1083
1084         /* if tracking is in progress, we should synchronize framenr from clipuser
1085          * so latest tracked frame would be shown */
1086         if (clip && clip->tracking_context)
1087                 BKE_tracking_sync_user(&sc->user, clip->tracking_context);
1088
1089         if (sc->flag & SC_LOCK_SELECTION) {
1090                 ImBuf *tmpibuf = NULL;
1091
1092                 if (clip && clip->tracking.stabilization.flag & TRACKING_2D_STABILIZATION) {
1093                         tmpibuf = ED_space_clip_get_stable_buffer(sc, NULL, NULL, NULL);
1094                 }
1095
1096                 if (ED_clip_view_selection(sc, ar, 0)) {
1097                         sc->xof += sc->xlockof;
1098                         sc->yof += sc->ylockof;
1099                 }
1100
1101                 if (tmpibuf)
1102                         IMB_freeImBuf(tmpibuf);
1103         }
1104
1105         /* clear and setup matrix */
1106         UI_ThemeClearColor(TH_BACK);
1107         glClear(GL_COLOR_BUFFER_BIT);
1108
1109         /* data... */
1110         movieclip_main_area_set_view2d(sc, ar);
1111
1112         clip_draw_main(sc, ar, scene);
1113
1114         if (sc->mode == SC_MODE_MASKEDIT) {
1115                 int x, y;
1116                 int width, height;
1117                 float zoomx, zoomy, aspx, aspy;
1118
1119                 /* frame image */
1120                 float maxdim;
1121                 float xofs, yofs;
1122
1123                 /* find window pixel coordinates of origin */
1124                 UI_view2d_to_region_no_clip(&ar->v2d, 0.0f, 0.0f, &x, &y);
1125
1126                 ED_space_clip_size(sc, &width, &height);
1127                 ED_space_clip_zoom(sc, ar, &zoomx, &zoomy);
1128                 ED_space_clip_aspect(sc, &aspx, &aspy);
1129
1130                 /* frame the image */
1131                 maxdim = maxf(width, height);
1132                 if (width == height) {
1133                         xofs = yofs = 0;
1134                 }
1135                 else if (width < height) {
1136                         xofs = ((height - width) / -2.0f) * zoomx;
1137                         yofs = 0.0f;
1138                 }
1139                 else { /* (width > height) */
1140                         xofs = 0.0f;
1141                         yofs = ((width - height) / -2.0f) * zoomy;
1142                 }
1143
1144                 /* apply transformation so mask editing tools will assume drawing from the origin in normalized space */
1145                 glPushMatrix();
1146                 glTranslatef(x + xofs, y + yofs, 0);
1147                 glScalef(maxdim * zoomx, maxdim * zoomy, 0);
1148                 glMultMatrixf(sc->stabmat);
1149
1150                 ED_mask_draw((bContext *)C, sc->mask_draw_flag, sc->mask_draw_type);
1151
1152                 ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
1153
1154                 glPopMatrix();
1155         }
1156
1157         /* Grease Pencil */
1158         clip_draw_grease_pencil((bContext *)C, 1);
1159
1160         /* reset view matrix */
1161         UI_view2d_view_restore(C);
1162
1163         /* draw Grease Pencil - screen space only */
1164         clip_draw_grease_pencil((bContext *)C, 0);
1165 }
1166
1167 static void clip_main_area_listener(ARegion *ar, wmNotifier *wmn)
1168 {
1169         /* context changes */
1170         switch (wmn->category) {
1171                 case NC_SCREEN:
1172                         if (wmn->data == ND_GPENCIL)
1173                                 ED_region_tag_redraw(ar);
1174                         break;
1175         }
1176 }
1177
1178 /****************** preview region ******************/
1179
1180 static void clip_preview_area_init(wmWindowManager *wm, ARegion *ar)
1181 {
1182         wmKeyMap *keymap;
1183
1184         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
1185
1186         /* own keymap */
1187         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1188         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1189
1190         keymap = WM_keymap_find(wm->defaultconf, "Clip Graph Editor", SPACE_CLIP, 0);
1191         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1192 }
1193
1194 static void graph_area_draw(const bContext *C, ARegion *ar)
1195 {
1196         View2D *v2d = &ar->v2d;
1197         View2DScrollers *scrollers;
1198         SpaceClip *sc = CTX_wm_space_clip(C);
1199         Scene *scene = CTX_data_scene(C);
1200         short unitx, unity;
1201
1202         if (sc->flag & SC_LOCK_TIMECURSOR)
1203                 ED_clip_graph_center_current_frame(scene, ar);
1204
1205         /* clear and setup matrix */
1206         UI_ThemeClearColor(TH_BACK);
1207         glClear(GL_COLOR_BUFFER_BIT);
1208
1209         UI_view2d_view_ortho(v2d);
1210
1211         /* data... */
1212         clip_draw_graph(sc, ar, scene);
1213
1214         /* reset view matrix */
1215         UI_view2d_view_restore(C);
1216
1217         /* scrollers */
1218         unitx = (sc->flag & SC_SHOW_SECONDS) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
1219         unity = V2D_UNIT_VALUES;
1220         scrollers = UI_view2d_scrollers_calc(C, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP);
1221         UI_view2d_scrollers_draw(C, v2d, scrollers);
1222         UI_view2d_scrollers_free(scrollers);
1223 }
1224
1225 static void dopesheet_area_draw(const bContext *C, ARegion *ar)
1226 {
1227         Scene *scene = CTX_data_scene(C);
1228         SpaceClip *sc = CTX_wm_space_clip(C);
1229         MovieClip *clip = ED_space_clip(sc);
1230         View2D *v2d = &ar->v2d;
1231         View2DGrid *grid;
1232         View2DScrollers *scrollers;
1233         short unit = 0;
1234
1235         if (clip)
1236                 BKE_tracking_dopesheet_update(&clip->tracking);
1237
1238         /* clear and setup matrix */
1239         UI_ThemeClearColor(TH_BACK);
1240         glClear(GL_COLOR_BUFFER_BIT);
1241
1242         UI_view2d_view_ortho(v2d);
1243
1244         /* time grid */
1245         unit = (sc->flag & SC_SHOW_SECONDS) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMES;
1246         grid = UI_view2d_grid_calc(CTX_data_scene(C), v2d, unit, V2D_GRID_CLAMP,
1247                                    V2D_ARG_DUMMY, V2D_ARG_DUMMY, ar->winx, ar->winy);
1248         UI_view2d_grid_draw(v2d, grid, V2D_GRIDLINES_ALL);
1249         UI_view2d_grid_free(grid);
1250
1251         /* data... */
1252         clip_draw_dopesheet_main(sc, ar, scene);
1253
1254         /* reset view matrix */
1255         UI_view2d_view_restore(C);
1256
1257         /* scrollers */
1258         scrollers = UI_view2d_scrollers_calc(C, v2d, unit, V2D_GRID_CLAMP, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1259         UI_view2d_scrollers_draw(C, v2d, scrollers);
1260         UI_view2d_scrollers_free(scrollers);
1261 }
1262
1263 static void clip_preview_area_draw(const bContext *C, ARegion *ar)
1264 {
1265         SpaceClip *sc = CTX_wm_space_clip(C);
1266
1267         if (sc->view == SC_VIEW_GRAPH)
1268                 graph_area_draw(C, ar);
1269         else if (sc->view == SC_VIEW_DOPESHEET)
1270                 dopesheet_area_draw(C, ar);
1271 }
1272
1273 static void clip_preview_area_listener(ARegion *UNUSED(ar), wmNotifier *UNUSED(wmn))
1274 {
1275 }
1276
1277 /****************** channels region ******************/
1278
1279 static void clip_channels_area_init(wmWindowManager *wm, ARegion *ar)
1280 {
1281         wmKeyMap *keymap;
1282
1283         UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_LIST, ar->winx, ar->winy);
1284
1285         keymap = WM_keymap_find(wm->defaultconf, "Clip Dopesheet Editor", SPACE_CLIP, 0);
1286         WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
1287 }
1288
1289 static void clip_channels_area_draw(const bContext *C, ARegion *ar)
1290 {
1291         SpaceClip *sc = CTX_wm_space_clip(C);
1292         MovieClip *clip = ED_space_clip(sc);
1293         View2D *v2d = &ar->v2d;
1294         View2DScrollers *scrollers;
1295
1296         if (clip)
1297                 BKE_tracking_dopesheet_update(&clip->tracking);
1298
1299         /* clear and setup matrix */
1300         UI_ThemeClearColor(TH_BACK);
1301         glClear(GL_COLOR_BUFFER_BIT);
1302
1303         UI_view2d_view_ortho(v2d);
1304
1305         /* data... */
1306         clip_draw_dopesheet_channels(C, ar);
1307
1308         /* reset view matrix */
1309         UI_view2d_view_restore(C);
1310
1311         /* scrollers */
1312         scrollers = UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1313         UI_view2d_scrollers_draw(C, v2d, scrollers);
1314         UI_view2d_scrollers_free(scrollers);
1315 }
1316
1317 static void clip_channels_area_listener(ARegion *UNUSED(ar), wmNotifier *UNUSED(wmn))
1318 {
1319 }
1320
1321 /****************** header region ******************/
1322
1323 /* add handlers, stuff you only do once or on area/region changes */
1324 static void clip_header_area_init(wmWindowManager *UNUSED(wm), ARegion *ar)
1325 {
1326         ED_region_header_init(ar);
1327 }
1328
1329 static void clip_header_area_draw(const bContext *C, ARegion *ar)
1330 {
1331         ED_region_header(C, ar);
1332 }
1333
1334 static void clip_header_area_listener(ARegion *ar, wmNotifier *wmn)
1335 {
1336         /* context changes */
1337         switch (wmn->category) {
1338                 case NC_SCENE:
1339                         switch (wmn->data) {
1340                                 /* for proportional editmode only */
1341                                 case ND_TOOLSETTINGS:
1342                                         /* TODO - should do this when in mask mode only but no datas available */
1343                                         // if (sc->mode == SC_MODE_MASKEDIT)
1344                                 {
1345                                         ED_region_tag_redraw(ar);
1346                                 }
1347                                 break;
1348                         }
1349                         break;
1350         }
1351 }
1352
1353
1354 /****************** tools region ******************/
1355
1356 /* add handlers, stuff you only do once or on area/region changes */
1357 static void clip_tools_area_init(wmWindowManager *wm, ARegion *ar)
1358 {
1359         ED_region_panels_init(wm, ar);
1360 }
1361
1362 static void clip_tools_area_draw(const bContext *C, ARegion *ar)
1363 {
1364         ED_region_panels(C, ar, 1, NULL, -1);
1365 }
1366
1367 /****************** tool properties region ******************/
1368
1369 static void clip_props_area_listener(ARegion *ar, wmNotifier *wmn)
1370 {
1371         /* context changes */
1372         switch (wmn->category) {
1373                 case NC_WM:
1374                         if (wmn->data == ND_HISTORY)
1375                                 ED_region_tag_redraw(ar);
1376                         break;
1377                 case NC_SCENE:
1378                         if (wmn->data == ND_MODE)
1379                                 ED_region_tag_redraw(ar);
1380                         break;
1381                 case NC_SPACE:
1382                         if (wmn->data == ND_SPACE_CLIP)
1383                                 ED_region_tag_redraw(ar);
1384                         break;
1385                 case NC_SCREEN:
1386                         if (wmn->data == ND_GPENCIL)
1387                                 ED_region_tag_redraw(ar);
1388                         break;
1389         }
1390 }
1391
1392 /****************** properties region ******************/
1393
1394 /* add handlers, stuff you only do once or on area/region changes */
1395 static void clip_properties_area_init(wmWindowManager *wm, ARegion *ar)
1396 {
1397         wmKeyMap *keymap;
1398
1399         ED_region_panels_init(wm, ar);
1400
1401         keymap = WM_keymap_find(wm->defaultconf, "Clip", SPACE_CLIP, 0);
1402         WM_event_add_keymap_handler(&ar->handlers, keymap);
1403 }
1404
1405 static void clip_properties_area_draw(const bContext *C, ARegion *ar)
1406 {
1407         SpaceClip *sc = CTX_wm_space_clip(C);
1408
1409         BKE_movieclip_update_scopes(sc->clip, &sc->user, &sc->scopes);
1410
1411         ED_region_panels(C, ar, 1, NULL, -1);
1412 }
1413
1414 static void clip_properties_area_listener(ARegion *ar, wmNotifier *wmn)
1415 {
1416         /* context changes */
1417         switch (wmn->category) {
1418                 case NC_SCREEN:
1419                         if (wmn->data == ND_GPENCIL)
1420                                 ED_region_tag_redraw(ar);
1421                         break;
1422                 case NC_BRUSH:
1423                         if (wmn->action == NA_EDITED)
1424                                 ED_region_tag_redraw(ar);
1425                         break;
1426         }
1427 }
1428
1429 /********************* registration ********************/
1430
1431 /* only called once, from space/spacetypes.c */
1432 void ED_spacetype_clip(void)
1433 {
1434         SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype clip");
1435         ARegionType *art;
1436
1437         st->spaceid = SPACE_CLIP;
1438         strncpy(st->name, "Clip", BKE_ST_MAXNAME);
1439
1440         st->new = clip_new;
1441         st->free = clip_free;
1442         st->init = clip_init;
1443         st->duplicate = clip_duplicate;
1444         st->operatortypes = clip_operatortypes;
1445         st->keymap = clip_keymap;
1446         st->listener = clip_listener;
1447         st->context = clip_context;
1448         st->dropboxes = clip_dropboxes;
1449         st->refresh = clip_refresh;
1450
1451         /* regions: main window */
1452         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region");
1453         art->regionid = RGN_TYPE_WINDOW;
1454         art->init = clip_main_area_init;
1455         art->draw = clip_main_area_draw;
1456         art->listener = clip_main_area_listener;
1457         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_GPENCIL;
1458
1459         BLI_addhead(&st->regiontypes, art);
1460
1461         /* preview */
1462         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region preview");
1463         art->regionid = RGN_TYPE_PREVIEW;
1464         art->prefsizey = 240;
1465         art->init = clip_preview_area_init;
1466         art->draw = clip_preview_area_draw;
1467         art->listener = clip_preview_area_listener;
1468         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_VIEW2D;
1469
1470         BLI_addhead(&st->regiontypes, art);
1471
1472         /* regions: properties */
1473         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region properties");
1474         art->regionid = RGN_TYPE_UI;
1475         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1476         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1477         art->init = clip_properties_area_init;
1478         art->draw = clip_properties_area_draw;
1479         art->listener = clip_properties_area_listener;
1480         BLI_addhead(&st->regiontypes, art);
1481         ED_clip_buttons_register(art);
1482
1483         /* regions: tools */
1484         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region tools");
1485         art->regionid = RGN_TYPE_TOOLS;
1486         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
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
1492         BLI_addhead(&st->regiontypes, art);
1493
1494         /* tool properties */
1495         art = MEM_callocN(sizeof(ARegionType), "spacetype clip tool properties region");
1496         art->regionid = RGN_TYPE_TOOL_PROPS;
1497         art->prefsizex = 0;
1498         art->prefsizey = 120;
1499         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1500         art->listener = clip_props_area_listener;
1501         art->init = clip_tools_area_init;
1502         art->draw = clip_tools_area_draw;
1503         ED_clip_tool_props_register(art);
1504
1505         BLI_addhead(&st->regiontypes, art);
1506
1507         /* regions: header */
1508         art = MEM_callocN(sizeof(ARegionType), "spacetype clip region");
1509         art->regionid = RGN_TYPE_HEADER;
1510         art->prefsizey = HEADERY;
1511         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI | ED_KEYMAP_VIEW2D | ED_KEYMAP_HEADER;
1512
1513         art->init = clip_header_area_init;
1514         art->draw = clip_header_area_draw;
1515         art->listener = clip_header_area_listener;
1516
1517         BLI_addhead(&st->regiontypes, art);
1518
1519         BKE_spacetype_register(st);
1520
1521         /* channels */
1522         art = MEM_callocN(sizeof(ARegionType), "spacetype clip channels region");
1523         art->regionid = RGN_TYPE_CHANNELS;
1524         art->prefsizex = UI_COMPACT_PANEL_WIDTH;
1525         art->keymapflag = ED_KEYMAP_FRAMES | ED_KEYMAP_UI;
1526         art->listener = clip_channels_area_listener;
1527         art->init = clip_channels_area_init;
1528         art->draw = clip_channels_area_draw;
1529
1530         BLI_addhead(&st->regiontypes, art);
1531 }