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