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