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