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