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